@@ -34,7 +34,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
34
34
state .seq_max = state .seq_base + state .win_size - 1 ;
35
35
int status = -1 ;
36
36
int count = 0 ;
37
- printf ("INFO:Length of file: %d bytes\n" ,len );
37
+ printf ("INFO: Length of file: %d bytes\n" ,len );
38
38
/*Calculate number of packets*/
39
39
int packet_num ;
40
40
packet_num = len /DATALEN + 1 ;
@@ -50,7 +50,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
50
50
gbnhdr * data_packet = malloc (sizeof (* data_packet ));
51
51
data_packet -> type = DATA ;
52
52
data_packet -> seqnum = state .seq_curr ;
53
- data_packet -> checksum = 0 ;
53
+ data_packet -> checksum = ( uint16_t ) 0 ;
54
54
if (state .seq_curr < packet_num ){
55
55
memcpy (data_packet -> data ,buf + (state .seq_curr - 1 )* DATALEN ,DATALEN );
56
56
}
@@ -62,12 +62,16 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
62
62
reset_alrm = false;
63
63
}
64
64
65
+ data_packet -> checksum = checksum (data_packet , sizeof (* data_packet ) / sizeof (uint16_t ));
66
+
67
+ printf ("INFO: Checksum of packet: %d\n" ,data_packet -> checksum );
68
+
65
69
status = sendto (sockfd ,data_packet ,sizeof (* data_packet ),0 ,state .address ,state .socklen );
66
70
if (status == -1 ){
67
- printf ("ERROR: DATA packet %d send failed." ,state .seq_curr - 1 );
71
+ printf ("ERROR: DATA packet %d send failed.\n " ,state .seq_curr - 1 );
68
72
}
69
73
else {
70
- printf ("INFO: DATA packet %d sent successfully." ,state .seq_curr - 1 );
74
+ printf ("INFO: DATA packet %d sent successfully.\n " ,state .seq_curr - 1 );
71
75
}
72
76
73
77
/*Reached end of window, wait for the correct ACK*/
@@ -101,6 +105,10 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
101
105
102
106
status = recvfrom (sockfd , packet , sizeof (* packet ), 0 , state .address , & state .socklen );
103
107
if (status != -1 ){
108
+ uint16_t checksum_actual = packet -> checksum ;
109
+ packet -> checksum = (uint16_t ) 0 ;
110
+ uint16_t checksum_expected = checksum (packet , sizeof (* packet ) / sizeof (uint16_t ));
111
+ printf ("INFO: Expected checksum value : %d . Actual checksum value : %d\n" ,checksum_expected ,checksum_actual );
104
112
if (packet -> type == FIN ){
105
113
/* Sender has initiated connection teardown */
106
114
printf ("INFO: Connection teardown initiated ...\n" );
@@ -146,7 +154,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
146
154
/* Sender has sent a DATA packet */
147
155
printf ("INFO: DATA received successfully.\n" );
148
156
/* Receiver responds with DATAACK */
149
- printf ("Packet seqnum : %d State seqnum: %d\n" ,packet -> seqnum ,state .seqnum );
157
+ printf ("INFO: Packet seqnum : %d State seqnum: %d\n" ,packet -> seqnum ,state .seqnum );
150
158
if (state .seqnum == packet -> seqnum ){
151
159
printf ("INFO: Received DATA packet is in sequence.\n" );
152
160
state .seqnum = packet -> seqnum + 1 ;
@@ -198,7 +206,8 @@ int gbn_close(int sockfd){
198
206
/* Sender to initiate connection teardown with FIN packet */
199
207
fin_packet -> type = FIN ;
200
208
fin_packet -> seqnum = 0 ;
201
- fin_packet -> checksum = 0 ;
209
+ fin_packet -> checksum = (uint16_t ) 0 ;
210
+ fin_packet -> checksum = checksum (fin_packet , sizeof (* fin_packet ) / sizeof (uint16_t ));
202
211
status = sendto (sockfd , fin_packet , sizeof (* fin_packet ), 0 , state .address , state .socklen );
203
212
if (status == -1 ){
204
213
printf ("ERROR: FIN send failed.Retrying ...\n" );
@@ -234,7 +243,8 @@ int gbn_close(int sockfd){
234
243
/* Sender responding with a FINACK */
235
244
fin_ack_packet -> type = FINACK ;
236
245
fin_ack_packet -> seqnum = 0 ;
237
- fin_ack_packet -> checksum = 0 ;
246
+ fin_ack_packet -> checksum = (uint16_t ) 0 ;
247
+ fin_ack_packet -> checksum = checksum (fin_ack_packet , sizeof (* fin_ack_packet ) / sizeof (uint16_t ));
238
248
status = sendto (sockfd , fin_ack_packet , sizeof (* fin_ack_packet ), 0 , state .address , state .socklen );
239
249
if (status == -1 ){
240
250
printf ("ERROR: FINACK send failed.Retrying ...\n" );
@@ -273,14 +283,16 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
273
283
state .state = CLOSED ;
274
284
int retryCount = 1 ;
275
285
int status = 0 ;
286
+
276
287
277
288
while (retryCount <= MAX_RETRY_ATTEMPTS && state .state != ESTABLISHED ){
278
289
if (state .state != SYN_SENT ){
279
290
/* SYN to be sent to the receiver */
280
291
gbnhdr * syn_packet = malloc (sizeof (* syn_packet ));
281
292
syn_packet -> type = SYN ;
282
293
syn_packet -> seqnum = 0 ;
283
- syn_packet -> checksum = 0 ;
294
+ syn_packet -> checksum = (uint16_t ) 0 ;
295
+ syn_packet -> checksum = checksum (syn_packet , sizeof (* syn_packet ) / sizeof (uint16_t ));
284
296
status = sendto (sockfd , syn_packet , sizeof (* syn_packet ), 0 , server , socklen );
285
297
if (status == -1 ){
286
298
printf ("ERROR: SYN send failed.Retrying ...\n" );
@@ -304,7 +316,8 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
304
316
gbnhdr * ack_packet = malloc (sizeof (* ack_packet ));
305
317
ack_packet -> type = ACK ;
306
318
ack_packet -> seqnum = 1 ;
307
- ack_packet -> checksum = 0 ;
319
+ ack_packet -> checksum = (uint16_t ) 0 ;
320
+ ack_packet -> checksum = checksum (ack_packet , sizeof (* ack_packet ) / sizeof (uint16_t ));
308
321
status = sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , server , socklen );
309
322
if (status != -1 ){
310
323
state .state = ESTABLISHED ;
@@ -394,7 +407,8 @@ int gbn_accept(int sockfd, struct sockaddr *client, socklen_t *socklen){
394
407
gbnhdr * syn_ack_packet = malloc (sizeof (* syn_ack_packet ));
395
408
syn_ack_packet -> type = SYNACK ;
396
409
syn_ack_packet -> seqnum = 1 ;
397
- syn_ack_packet -> checksum = 0 ;
410
+ syn_ack_packet -> checksum = (uint16_t ) 0 ;
411
+ syn_ack_packet -> checksum = checksum (syn_ack_packet , sizeof (* syn_ack_packet ) / sizeof (uint16_t ));
398
412
status = sendto (sockfd , syn_ack_packet , sizeof (* syn_ack_packet ), 0 , client , * socklen );
399
413
if (status != -1 ){
400
414
printf ("INFO: SYN_ACK sent successfully.\n" );
0 commit comments