-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitksocket.c
More file actions
1329 lines (1119 loc) · 42.9 KB
/
Copy pathinitksocket.c
File metadata and controls
1329 lines (1119 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* KTP Protocol Management Daemon
*
* Central controller process for KTP networking protocol
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <errno.h>
#include "ksocket.h"
/* Terminal color codes for log output */
#define BOLD_CYAN "\033[1;36m"
#define COLOR_BLUE "\033[0;36m"
#define COLOR_RESET "\033[0m"
#define COLOR_MAGENTA "\033[0;35m"
#define BOLD_RED "\033[1;31m"
#define COLOR_RED "\033[0;31m"
#define COLOR_GREEN "\033[0;32m"
#define COLOR_YELLOW "\033[0;33m"
/* Global state variables */
static int isRunning = 1; /* Flag controlling thread execution */
static int shmHandle = -1; /* Shared memory ID */
static ktp_socket_t *ktpSockets = NULL; /* Socket array in shared memory */
/**
* Signal handler for graceful shutdown
*
* @param sig Signal number received
*/
void handle_signal(int sig)
{
printf(COLOR_MAGENTA "KTP Manager: Signal %d received, initiating shutdown sequence...\n", sig);
isRunning = 0;
}
/**
* Establish shared memory region for KTP protocol communication
*
* @return Status code: 0=success, -1=failure
*/
int init_shared_memory(void)
{
int status = 0;
const char *KEY_PATH = "/";
const int KEY_ID = 'A';
// Generate unique system key for memory segment
key_t memoryKey = ftok(KEY_PATH, KEY_ID);
if (memoryKey == -1)
{
fprintf(stderr, COLOR_MAGENTA "KTP Manager: Unable to generate memory key\n");
return -1;
}
// Allocate shared memory block
const size_t requiredBytes = sizeof(ktp_socket_t) * KTP_MAX_SOCKETS;
int memoryId = shmget(memoryKey, requiredBytes, 0666 | IPC_CREAT);
if (memoryId < 0)
{
fprintf(stderr, COLOR_MAGENTA "KTP Manager: Memory segment creation failed\n");
return -1;
}
shmHandle = memoryId;
// Connect to allocated memory
void *memoryRegion = shmat(memoryId, NULL, 0);
if (memoryRegion == (void *)-1)
{
fprintf(stderr, COLOR_MAGENTA "KTP Manager: Cannot attach to memory segment\n");
return -1;
}
ktpSockets = (ktp_socket_t *)memoryRegion;
// Configure individual socket slots
for (int socketIndex = 0; socketIndex < KTP_MAX_SOCKETS; socketIndex++)
{
// Clear memory slot
ktp_socket_t *currentSocket = &ktpSockets[socketIndex];
memset(currentSocket, 0, sizeof(ktp_socket_t));
// Set up underlying transport socket
int transportDescriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (transportDescriptor < 0)
{
printf(COLOR_MAGENTA "KTP Manager: Warning - Socket %d initialization failed\n", socketIndex);
status = -1;
continue;
}
// Configure slot properties
currentSocket->udp_sockfd = transportDescriptor;
currentSocket->is_allocated = 0;
// Initialize synchronization primitives
pthread_mutexattr_t mutexConfig;
pthread_mutexattr_init(&mutexConfig);
pthread_mutexattr_setpshared(&mutexConfig, PTHREAD_PROCESS_SHARED);
if (pthread_mutex_init(¤tSocket->socket_mutex, &mutexConfig) != 0)
{
printf(COLOR_MAGENTA "KTP Manager: Warning - Mutex initialization failed for socket %d\n", socketIndex);
close(transportDescriptor);
status = -1;
}
pthread_mutexattr_destroy(&mutexConfig);
printf(COLOR_MAGENTA "KTP Manager: Initialized socket %d (fd=%d)\n",
socketIndex, transportDescriptor);
}
if (status == 0)
{
printf(COLOR_MAGENTA "KTP Manager: Socket memory region ready with %d slots\n", KTP_MAX_SOCKETS);
}
return status;
}
/**
* Performs resource deallocation
*
* This function ensures all allocated system resources are properly
* released, avoiding memory leaks and abandoned IPC segments.
*/
void cleanup(void)
{
int cleanup_status = 0;
// Log beginning of cleanup process
fprintf(stdout, COLOR_BLUE "KTP Service: Beginning resource cleanup\n");
// Handle socket memory region
if (ktpSockets)
{
// Only attempt detachment if pointer is valid
if (ktpSockets != (void *)-1)
{
// Release connection to shared memory
int result = shmdt(ktpSockets);
if (result != 0)
{
fprintf(stderr, COLOR_RED "KTP Service: Error %d detaching from memory segment\n", errno);
cleanup_status = -1;
}
else
{
fprintf(stdout, COLOR_BLUE "KTP Service: Memory segment detached\n");
}
}
else
{
fprintf(stderr, COLOR_RED "KTP Service: Invalid memory segment pointer detected\n");
cleanup_status = -1;
}
// Reset pointer to avoid double-free
ktpSockets = NULL;
}
// Remove shared memory segment
if (shmHandle >= 0)
{
// Request removal of shared memory from system
int result = shmctl(shmHandle, IPC_RMID, NULL);
if (result != 0)
{
fprintf(stderr, COLOR_RED "KTP Service: Error %d removing shared memory segment\n", errno);
cleanup_status = -1;
}
else
{
fprintf(stdout, COLOR_BLUE "KTP Service: Shared memory segment released\n");
}
// Mark handle as invalid
shmHandle = -1;
}
// Report final cleanup status
if (cleanup_status == 0)
{
fprintf(stdout, COLOR_GREEN "KTP Service: Resource cleanup completed successfully\n");
}
else
{
fprintf(stderr, COLOR_YELLOW "KTP Service: Resource cleanup completed with warnings\n");
}
}
// funtion defintions
static void process_pending_bindings(void);
static int prepare_descriptor_set(fd_set *descriptorSet);
static int try_lock_active_socket(int socketId);
static int handle_incoming_packet(int socketId, unsigned int *packetCounter,
unsigned int *lossCounter, int *notificationArray);
static void process_data_packet(int socketId, ktp_message_t *message, struct sockaddr_in *sender);
static void process_ack_packet(int socketId, ktp_message_t *message);
static void process_acknowledged_packets(int socketId, int ackCount);
static void store_data_packet(int socketId, ktp_message_t *message);
static void update_receive_window_state(int socketId);
static void send_acknowledgment(int socketId);
static void send_buffer_state_updates(int *notificationArray);
/**
* Receiver thread *
* Network packet reception and processing thread
*
* Handles incoming network traffic and maintains protocol state
*
* @param threadArgs Thread parameters (unused)
* @return Always returns NULL
*/
void *R_thread(void *threadArgs)
{
// Initialize statistics counters
unsigned int totalPackets = 0;
unsigned int lostPackets = 0;
// Prepare socket monitoring structures
fd_set activeDescriptors;
struct timeval pollTimeout;
// Setup buffer notification tracking array
int pendingBufferNotifications[KTP_MAX_SOCKETS];
memset(pendingBufferNotifications, 0, sizeof(pendingBufferNotifications));
fprintf(stdout, BOLD_CYAN "KTP: R_thread initialized and running\n" COLOR_RESET);
// Main processing loop
while (isRunning)
{
// Handle socket binding operations
process_pending_bindings();
// Prepare monitoring set for active sockets
int highestDescriptor = prepare_descriptor_set(&activeDescriptors);
// Skip polling if no active sockets exist
if (highestDescriptor < 0)
{
usleep(150000); // 150ms pause before retry
continue;
}
// Configure polling timeout
pollTimeout.tv_sec = 0;
pollTimeout.tv_usec = 750000; // 750ms timeout
// Monitor sockets for activity
int readyCount = select(highestDescriptor + 1, &activeDescriptors, NULL, NULL, &pollTimeout);
// Handle select errors
if (readyCount < 0)
{
if (isRunning)
{
fprintf(stderr, COLOR_RED "KTP: Socket monitoring failed: %s\n" COLOR_RESET, strerror(errno));
}
continue;
}
// Process sockets with pending data
for (int socketIndex = 0; socketIndex < KTP_MAX_SOCKETS; socketIndex++)
{
// Skip locked or inactive sockets
if (!try_lock_active_socket(socketIndex))
continue;
int descriptor = ktpSockets[socketIndex].udp_sockfd;
// Check if this socket has data available
if (FD_ISSET(descriptor, &activeDescriptors))
{
// Process incoming packet
if (handle_incoming_packet(socketIndex, &totalPackets, &lostPackets,
pendingBufferNotifications) != 0)
{
// Error handling if needed
}
}
pthread_mutex_unlock(&ktpSockets[socketIndex].socket_mutex);
}
// Handle buffer state notifications
send_buffer_state_updates(pendingBufferNotifications);
}
// Output final statistics before termination
if (totalPackets > 0)
{
fprintf(stdout, COLOR_YELLOW "KTP: Traffic summary - %u packets received, "
"%u lost (%.1f%% loss rate)\n" COLOR_RESET,
totalPackets, lostPackets, (lostPackets * 100.0) / totalPackets);
}
fprintf(stdout, BOLD_CYAN "KTP: R_thread terminated\n" COLOR_RESET);
return NULL;
}
/**
* Process all pending socket binding requests
*/
static void process_pending_bindings(void)
{
for (int socketId = 0; socketId < KTP_MAX_SOCKETS; socketId++)
{
if (pthread_mutex_trylock(&ktpSockets[socketId].socket_mutex) != 0)
continue;
// Check if this socket needs binding
int bindingNeeded = ktpSockets[socketId].is_allocated &&
ktpSockets[socketId].bind_requested &&
!ktpSockets[socketId].is_bound;
if (bindingNeeded)
{
fprintf(stdout, COLOR_GREEN "KTP: Processing bind request for socket %d\n" COLOR_RESET, socketId);
// Create fresh UDP socket
close(ktpSockets[socketId].udp_sockfd);
int newSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
ktpSockets[socketId].udp_sockfd = newSocket;
// Attempt binding operation
if (bind(newSocket, (struct sockaddr *)&ktpSockets[socketId].src_addr,
sizeof(struct sockaddr_in)) == 0)
{
// Binding succeeded
ktpSockets[socketId].is_bound = 1;
ktpSockets[socketId].bind_requested = 0;
fprintf(stdout, COLOR_GREEN "KTP: Socket %d successfully bound\n" COLOR_RESET, socketId);
}
else
{
fprintf(stderr, COLOR_RED "KTP: Failed to bind socket %d: %s\n" COLOR_RESET,
socketId, strerror(errno));
}
}
pthread_mutex_unlock(&ktpSockets[socketId].socket_mutex);
}
}
/**
* Prepare the file descriptor set for select()
*
* @param descriptorSet Pointer to fd_set to populate
* @return Highest file descriptor value, or -1 if no active sockets
*/
static int prepare_descriptor_set(fd_set *descriptorSet)
{
int maxDescriptor = -1;
FD_ZERO(descriptorSet);
for (int socketId = 0; socketId < KTP_MAX_SOCKETS; socketId++)
{
if (pthread_mutex_trylock(&ktpSockets[socketId].socket_mutex) != 0)
continue;
int socketActive = ktpSockets[socketId].is_allocated &&
ktpSockets[socketId].is_bound &&
ktpSockets[socketId].udp_sockfd >= 0;
if (socketActive)
{
int descriptor = ktpSockets[socketId].udp_sockfd;
// Validate socket before using
int errorState = 0;
socklen_t errorLen = sizeof(errorState);
if (getsockopt(descriptor, SOL_SOCKET, SO_ERROR, &errorState, &errorLen) == 0 &&
errorState == 0)
{
// Socket is valid
FD_SET(descriptor, descriptorSet);
maxDescriptor = (descriptor > maxDescriptor) ? descriptor : maxDescriptor;
}
else
{
// Invalid socket - mark for cleanup
fprintf(stdout, COLOR_YELLOW "KTP: Invalid socket %d detected, marking for cleanup\n" COLOR_RESET,
socketId);
ktpSockets[socketId].is_allocated = 0;
ktpSockets[socketId].udp_sockfd = -1;
}
}
pthread_mutex_unlock(&ktpSockets[socketId].socket_mutex);
}
return maxDescriptor;
}
/**
* Attempt to lock a socket if it's active
*
* @param socketId Socket index to check
* @return 1 if socket was locked and is active, 0 otherwise
*/
static int try_lock_active_socket(int socketId)
{
if (pthread_mutex_trylock(&ktpSockets[socketId].socket_mutex) != 0)
return 0;
if (!ktpSockets[socketId].is_allocated || !ktpSockets[socketId].is_bound)
{
pthread_mutex_unlock(&ktpSockets[socketId].socket_mutex);
return 0;
}
return 1;
}
/**
* Process an incoming network packet
*
* @param socketId Socket index
* @param packetCounter Pointer to packet counter
* @param lossCounter Pointer to loss counter
* @param notificationArray Buffer notification array
* @return 0 on success, non-zero on error
*/
static int handle_incoming_packet(int socketId, unsigned int *packetCounter,
unsigned int *lossCounter, int *notificationArray)
{
ktp_message_t incomingMsg;
struct sockaddr_in senderAddr;
socklen_t addrLen = sizeof(senderAddr);
// Receive the data
ssize_t bytesRead = recvfrom(ktpSockets[socketId].udp_sockfd, &incomingMsg,
sizeof(incomingMsg), 0,
(struct sockaddr *)&senderAddr, &addrLen);
// Update statistics
(*packetCounter)++;
// Check for simulated packet loss
if (dropMessage(KTP_PACKET_LOSS_PROB))
{
fprintf(stdout, COLOR_RED "KTP: Simulated packet loss on socket %d\n" COLOR_RESET, socketId);
(*lossCounter)++;
return 0;
}
// Handle receive errors
if (bytesRead <= 0)
return -1;
// Process by message type
switch (incomingMsg.header.type)
{
case KTP_TYPE_DATA:
process_data_packet(socketId, &incomingMsg, &senderAddr);
// Reset buffer notification counter
notificationArray[socketId] = 0;
break;
case KTP_TYPE_ACK:
process_ack_packet(socketId, &incomingMsg);
break;
default:
fprintf(stderr, COLOR_RED "KTP: Unknown packet type %d received\n" COLOR_RESET,
incomingMsg.header.type);
break;
}
return 0;
}
/**
* Process DATA type packet
*/
static void process_data_packet(int socketId, ktp_message_t *message, struct sockaddr_in *sender)
{
uint8_t sequenceNum = message->header.seq_num;
uint8_t expectedSeq = ktpSockets[socketId].rwnd.expected_seq_num;
fprintf(stdout, COLOR_GREEN "KTP: Socket %d received DATA packet sequence=%d\n" COLOR_RESET,
socketId, sequenceNum);
// Check for duplicate packet
int isDuplicate = 0;
for (int i = 0; i < KTP_RECV_BUFFER_SIZE; i++)
{
if (ktpSockets[socketId].rwnd.received_msgs[i] == sequenceNum)
{
isDuplicate = 1;
break;
}
}
// Calculate window boundaries
int inWindow = 0;
uint8_t windowEnd = (expectedSeq + ktpSockets[socketId].rwnd.size - 1) % 256;
// Handle window wrap-around
if (expectedSeq <= windowEnd)
{
inWindow = (sequenceNum >= expectedSeq && sequenceNum <= windowEnd);
}
else
{
inWindow = (sequenceNum >= expectedSeq || sequenceNum <= windowEnd);
}
// Handle packet based on duplicate status and window position
if (isDuplicate)
{
// Send duplicate ACK
send_acknowledgment(socketId);
}
else if (inWindow)
{
// New in-window packet
store_data_packet(socketId, message);
}
else
{
fprintf(stdout, COLOR_RED "KTP: Socket %d discarded out-of-window packet seq=%d\n" COLOR_RESET,
socketId, sequenceNum);
}
}
/**
* Process ACK type packet
*/
static void process_ack_packet(int socketId, ktp_message_t *message)
{
uint8_t lastAcknowledged = message->header.last_ack;
uint8_t advertisedWindow = message->header.rwnd;
fprintf(stdout, COLOR_GREEN "KTP: Socket %d received ACK for seq=%d window=%d\n" COLOR_RESET,
socketId, lastAcknowledged, advertisedWindow);
// Update send window size based on receiver's advertised window
ktpSockets[socketId].swnd.size = advertisedWindow;
// Count acknowledged packets
int acknowledgedCount = 0;
int foundAck = 0;
for (int i = 0; i < ktpSockets[socketId].swnd.num_unacked; i++)
{
int windowPos = (ktpSockets[socketId].swnd.base + i) % KTP_MAX_WINDOW_SIZE;
uint8_t seqNum = ktpSockets[socketId].swnd.seq_nums[windowPos];
acknowledgedCount++;
if (seqNum == lastAcknowledged)
{
foundAck = 1;
break;
}
}
// Handle duplicate or spurious ACK
if (!foundAck)
{
fprintf(stdout, COLOR_YELLOW "KTP: Socket %d received duplicate ACK %d\n" COLOR_RESET,
socketId, lastAcknowledged);
return;
}
// Process valid ACK
process_acknowledged_packets(socketId, acknowledgedCount);
}
/**
* Process acknowledged packets and update window
*/
static void process_acknowledged_packets(int socketId, int ackCount)
{
fprintf(stdout, COLOR_GREEN "KTP: Socket %d processing %d acknowledged packets\n" COLOR_RESET,
socketId, ackCount);
// Clear acknowledged packets from buffer
for (int i = 0; i < ackCount; i++)
{
int bufferIndex = (ktpSockets[socketId].swnd.base + i) % KTP_SEND_BUFFER_SIZE;
memset(ktpSockets[socketId].send_buffer[bufferIndex], 0, KTP_MSG_SIZE);
ktpSockets[socketId].send_buffer_occ[bufferIndex] = 0;
}
// Update window state
ktpSockets[socketId].swnd.base = (ktpSockets[socketId].swnd.base + ackCount) % KTP_MAX_WINDOW_SIZE;
ktpSockets[socketId].swnd.num_unacked -= ackCount;
}
/**
* Store data packet in receive buffer
*/
static void store_data_packet(int socketId, ktp_message_t *message)
{
uint8_t sequenceNum = message->header.seq_num;
uint8_t expectedSeq = ktpSockets[socketId].rwnd.expected_seq_num;
// Check buffer capacity
if (ktpSockets[socketId].rwnd.buffer_occupied >= KTP_RECV_BUFFER_SIZE)
{
// Buffer is full
ktpSockets[socketId].rwnd.nospace_flag = 1;
ktpSockets[socketId].rwnd.size = 0;
fprintf(stdout, COLOR_YELLOW "KTP: Socket %d buffer full, packet %d dropped\n" COLOR_RESET,
socketId, sequenceNum);
return;
}
// Store packet in receive buffer
int writePos = ktpSockets[socketId].rwnd.buffer_write_pos;
int offset = ((sequenceNum - expectedSeq + 256) % 256) % KTP_RECV_BUFFER_SIZE;
int bufferPos = (writePos + offset) % KTP_RECV_BUFFER_SIZE;
// Copy data to buffer
memcpy(ktpSockets[socketId].recv_buffer[bufferPos], message->data, KTP_MSG_SIZE);
ktpSockets[socketId].rwnd.received_msgs[bufferPos] = sequenceNum;
// Process in-order packets
update_receive_window_state(socketId);
// Update acknowledgment information
ktpSockets[socketId].rwnd.last_ack_sent = (ktpSockets[socketId].rwnd.expected_seq_num - 1 + 256) % 256;
// Send acknowledgment
send_acknowledgment(socketId);
// Check if buffer is now full
if (ktpSockets[socketId].rwnd.buffer_occupied >= KTP_RECV_BUFFER_SIZE)
{
ktpSockets[socketId].rwnd.nospace_flag = 1;
ktpSockets[socketId].rwnd.size = 0;
}
}
/**
* Update receive window state based on received packets
*/
static void update_receive_window_state(int socketId)
{
// Process consecutive in-order packets
while (ktpSockets[socketId].rwnd.received_msgs[ktpSockets[socketId].rwnd.buffer_write_pos] ==
ktpSockets[socketId].rwnd.expected_seq_num)
{
// Update state for this in-order packet
ktpSockets[socketId].rwnd.expected_seq_num =
(ktpSockets[socketId].rwnd.expected_seq_num + 1) % 256;
ktpSockets[socketId].rwnd.buffer_write_pos =
(ktpSockets[socketId].rwnd.buffer_write_pos + 1) % KTP_RECV_BUFFER_SIZE;
ktpSockets[socketId].rwnd.buffer_occupied++;
ktpSockets[socketId].rwnd.size--;
}
}
/**
* Send acknowledgment packet
*/
static void send_acknowledgment(int socketId)
{
ktp_message_t ackMsg;
memset(&ackMsg, 0, sizeof(ackMsg));
ackMsg.header.type = KTP_TYPE_ACK;
ackMsg.header.last_ack = ktpSockets[socketId].rwnd.last_ack_sent;
ackMsg.header.rwnd = ktpSockets[socketId].rwnd.size;
fprintf(stdout, COLOR_GREEN "KTP: Socket %d sending ACK seq=%d window=%d\n" COLOR_RESET,
socketId, ackMsg.header.last_ack, ackMsg.header.rwnd);
sendto(ktpSockets[socketId].udp_sockfd, &ackMsg, sizeof(ackMsg.header), 0,
(struct sockaddr *)&ktpSockets[socketId].dst_addr, sizeof(struct sockaddr_in));
}
/**
* Send buffer state updates for sockets that need them
*/
static void send_buffer_state_updates(int *notificationArray)
{
for (int socketId = 0; socketId < KTP_MAX_SOCKETS; socketId++)
{
if (pthread_mutex_trylock(&ktpSockets[socketId].socket_mutex) != 0)
continue;
if (ktpSockets[socketId].is_allocated && ktpSockets[socketId].is_bound)
{
// Check if buffer was previously full but now has space
int bufferFreed = ktpSockets[socketId].rwnd.nospace_flag &&
ktpSockets[socketId].rwnd.buffer_occupied < KTP_RECV_BUFFER_SIZE;
if (bufferFreed)
{
// Buffer now has space, send update notifications
ktpSockets[socketId].rwnd.nospace_flag = 0;
notificationArray[socketId] = 10; // Schedule 10 notification messages
fprintf(stdout, COLOR_GREEN "KTP: Socket %d buffer now available, scheduling updates\n" COLOR_RESET,
socketId);
}
// Send any scheduled buffer notifications
if (notificationArray[socketId] > 0)
{
ktp_message_t ackMsg;
memset(&ackMsg, 0, sizeof(ackMsg));
ackMsg.header.type = KTP_TYPE_ACK;
ackMsg.header.last_ack = ktpSockets[socketId].rwnd.last_ack_sent;
ackMsg.header.rwnd = ktpSockets[socketId].rwnd.size;
fprintf(stdout, COLOR_GREEN "KTP: Socket %d buffer notification %d/10: ack=%d window=%d\n" COLOR_RESET, socketId, 11 - notificationArray[socketId],
ackMsg.header.last_ack, ackMsg.header.rwnd);
sendto(ktpSockets[socketId].udp_sockfd, &ackMsg, sizeof(ackMsg.header), 0,
(struct sockaddr *)&ktpSockets[socketId].dst_addr, sizeof(struct sockaddr_in));
notificationArray[socketId]--;
}
}
pthread_mutex_unlock(&ktpSockets[socketId].socket_mutex);
}
}
// functions in S_thread
/**
* Process packet timeouts for Socket
* @returns Number of packets retransmitted
*/
static int handle_Socket_timeouts(int SocketId, struct timeval *now)
{
int retransmittedCount = 0;
int hasTimedOutPackets = 0;
ktp_socket_t *Socket = &ktpSockets[SocketId];
// First detect if any packet has timed out
for (int packetIdx = 0; packetIdx < Socket->swnd.num_unacked; packetIdx++)
{
int windowPos = (Socket->swnd.base + packetIdx) % KTP_MAX_WINDOW_SIZE;
// Calculate elapsed time
double elapsedSecs = (now->tv_sec - Socket->swnd.send_times[windowPos].tv_sec) +
(now->tv_usec - Socket->swnd.send_times[windowPos].tv_usec) / 1000000.0;
if (elapsedSecs >= KTP_TIMEOUT_SEC)
{
hasTimedOutPackets = 1;
break;
}
}
// If timeout detected, resend entire window
if (hasTimedOutPackets && Socket->swnd.num_unacked > 0)
{
fprintf(stdout, COLOR_RED "KTP: Socket %d timeout detected - "
"resending %d packets\n" COLOR_RESET,
SocketId, Socket->swnd.num_unacked);
// Go through all unacknowledged packets
for (int packetIdx = 0; packetIdx < Socket->swnd.num_unacked; packetIdx++)
{
// Calculate relevant buffer positions
int windowPos = (Socket->swnd.base + packetIdx) % KTP_MAX_WINDOW_SIZE;
int bufferPos = (Socket->swnd.base + packetIdx) % KTP_SEND_BUFFER_SIZE;
uint8_t seqNumber = Socket->swnd.seq_nums[windowPos];
// Prepare retransmission packet
ktp_message_t pkt;
memset(&pkt, 0, sizeof(pkt));
pkt.header.type = KTP_TYPE_DATA;
pkt.header.seq_num = seqNumber;
pkt.header.rwnd = Socket->rwnd.size;
pkt.header.last_ack = (Socket->rwnd.expected_seq_num - 1) % 256;
// Copy payload data
memcpy(pkt.data, Socket->send_buffer[bufferPos], KTP_MSG_SIZE);
// Transmit packet
if (sendto(Socket->udp_sockfd, &pkt, sizeof(pkt), 0,
(struct sockaddr *)&Socket->dst_addr,
sizeof(struct sockaddr_in)) > 0)
{
retransmittedCount++;
// Update timestamp to prevent immediate retransmission
gettimeofday(&Socket->swnd.send_times[windowPos], NULL);
}
}
}
return retransmittedCount;
}
/**
* Transmit new packets for Socket within window constraints
* @returns Number of packets sent
*/
static int transmit_new_packets(int SocketId, unsigned int *stats)
{
int packetsSent = 0;
ktp_socket_t *Socket = &ktpSockets[SocketId];
// Try to send as many packets as window allows
while (Socket->swnd.num_unacked < Socket->swnd.size)
{
// Find next packet to send
int nextPacketPos = (Socket->swnd.base + Socket->swnd.num_unacked) % KTP_SEND_BUFFER_SIZE;
// Check if buffer slot contains data ready to send
if (Socket->send_buffer_occ[nextPacketPos] == 0)
{
break; // No more data waiting to be sent
}
// Prepare new data packet
ktp_message_t pkt;
memset(&pkt, 0, sizeof(pkt));
// Configure packet headers
pkt.header.type = KTP_TYPE_DATA;
pkt.header.seq_num = Socket->swnd.next_seq_num;
pkt.header.rwnd = Socket->rwnd.size;
pkt.header.last_ack = (Socket->rwnd.expected_seq_num - 1) % 256;
// Copy payload data
memcpy(pkt.data, Socket->send_buffer[nextPacketPos], KTP_MSG_SIZE);
// Update window tracking
int windowPos = (Socket->swnd.base + Socket->swnd.num_unacked) % KTP_MAX_WINDOW_SIZE;
Socket->swnd.seq_nums[windowPos] = pkt.header.seq_num;
// Record transmission time
gettimeofday(&Socket->swnd.send_times[windowPos], NULL);
// Transmit packet
if (sendto(Socket->udp_sockfd, &pkt, sizeof(pkt), 0,
(struct sockaddr *)&Socket->dst_addr,
sizeof(struct sockaddr_in)) > 0)
{
packetsSent++;
fprintf(stdout, COLOR_GREEN "KTP: Socket %d sent packet seq=%d\n" COLOR_RESET,
SocketId, pkt.header.seq_num);
// Update window state
Socket->swnd.next_seq_num = (Socket->swnd.next_seq_num + 1) % 256;
Socket->swnd.num_unacked++;
// Update statistics
stats[SocketId]++;
}
else
{
fprintf(stderr, COLOR_RED "KTP: Socket %d send error: %s\n" COLOR_RESET,
SocketId, strerror(errno));
break; // Stop on error
}
}
return packetsSent;
}
/**
* Check if flow control is limiting transmission on an Socket
* @return 1 if window is full, 0 otherwise
*/
static int is_flow_controlled(int SocketId)
{
return ktpSockets[SocketId].swnd.num_unacked >= ktpSockets[SocketId].swnd.size;
}
/**
* Sender thread *
* Data transmission control thread - Handles reliable packet delivery
*
* @param threadContext Thread context (unused)
* @return Always NULL on thread exit
*/
void *S_thread(void *threadContext)
{
fprintf(stdout, COLOR_YELLOW "KTP: S_thread activated\n" COLOR_RESET);
// Operational statistics tracking
unsigned int txPackets[KTP_MAX_SOCKETS] = {0};
unsigned int retransmissions[KTP_MAX_SOCKETS] = {0};
unsigned int flowControlHits[KTP_MAX_SOCKETS] = {0};
// Main controller loop
while (isRunning)
{
int activeSockets = 0;
int totalNewPacketsSent = 0;
int totalRetransmissions = 0;
// Process each transport Socket
for (int SocketId = 0; SocketId < KTP_MAX_SOCKETS; SocketId++)
{
// Try to acquire exclusive access
if (pthread_mutex_trylock(&ktpSockets[SocketId].socket_mutex) != 0)
continue; // Skip if in use by another thread
// Verify Socket state
if (!ktpSockets[SocketId].is_allocated || !ktpSockets[SocketId].is_bound)
{
pthread_mutex_unlock(&ktpSockets[SocketId].socket_mutex);
continue;
}
activeSockets++;
// Get current timestamp for timeout checks
struct timeval currentTimestamp;
gettimeofday(¤tTimestamp, NULL);
// RELIABILITY: Handle retransmissions for timed-out packets
int retransmitted = handle_Socket_timeouts(SocketId, ¤tTimestamp);
totalRetransmissions += retransmitted;
retransmissions[SocketId] += retransmitted;
// Check if flow control is limiting transmission
if (is_flow_controlled(SocketId))
{
flowControlHits[SocketId]++;
if (flowControlHits[SocketId] % 10 == 0)
{
fprintf(stdout, COLOR_BLUE "KTP: Socket %d: Window full (size=%d)\n" COLOR_RESET,
SocketId, ktpSockets[SocketId].swnd.size);
}
}
// THROUGHPUT: Send new packets as window permits
int sentPackets = transmit_new_packets(SocketId, txPackets);
totalNewPacketsSent += sentPackets;
// Release Socket
pthread_mutex_unlock(&ktpSockets[SocketId].socket_mutex);
}
// Adapt sleep time based on activity
if (activeSockets > 0)
{
// activity summary if anything happened
if (totalNewPacketsSent > 0 || totalRetransmissions > 0)
{
// Activity summary with performance metrics
// float retransmissionRate = totalNewPacketsSent > 0 ?
// (100.0f * totalRetransmissions) / (totalNewPacketsSent + totalRetransmissions) : 0.0f;
fprintf(stdout, COLOR_BLUE "KTP: Activity summary - "
"sent: %d new, %d retx, %d active sockets\n" COLOR_RESET,
totalNewPacketsSent, totalRetransmissions, activeSockets);
}
// More frequent checks when active
usleep(KTP_TIMEOUT_SEC * 300000); // 30% of timeout value
}
else
{
// Sleep longer when no active Sockets
usleep(KTP_TIMEOUT_SEC * 800000); // 80% of timeout value
}
}
// Generate final statistics report
fprintf(stdout, COLOR_YELLOW "KTP: S_thread shutting down\n" COLOR_RESET);
for (int SocketId = 0; SocketId < KTP_MAX_SOCKETS; SocketId++)
{
if (txPackets[SocketId] > 0)
{
float retransmissionRate = txPackets[SocketId] > 0 ? (100.0 * retransmissions[SocketId]) / txPackets[SocketId] : 0.0;
fprintf(stdout, COLOR_YELLOW "KTP: Socket %d statistics - "
"%u packets sent, %u retransmitted (%.1f%%), "
"%u flow control pauses\n" COLOR_RESET,
SocketId,
txPackets[SocketId],
retransmissions[SocketId],
retransmissionRate,
flowControlHits[SocketId]);
}
}
fprintf(stdout, COLOR_YELLOW "KTP: S_thread terminated\n" COLOR_RESET);
return NULL;