@@ -54,7 +54,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
54
54
if (state .seq_curr < packet_num ){
55
55
memcpy (data_packet -> data ,buf + (state .seq_curr - 1 )* DATALEN ,DATALEN );
56
56
}
57
- else memcpy (data_packet -> data ,buf + (state .seq_curr - 1 )* DATALEN , len % DATALEN );
57
+ else strcpy (data_packet -> data ,buf + (state .seq_curr - 1 )* DATALEN );
58
58
59
59
state .seq_curr ++ ;
60
60
if (reset_alrm == true){
@@ -66,7 +66,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
66
66
67
67
printf ("INFO: Checksum of packet: %d\n" ,data_packet -> checksum );
68
68
69
- status = sendto (sockfd ,data_packet ,sizeof (* data_packet ),0 ,state .address ,state .socklen );
69
+ status = maybe_sendto (sockfd ,data_packet ,sizeof (* data_packet ),0 ,state .address ,state .socklen );
70
70
if (status == -1 ){
71
71
printf ("ERROR: DATA packet %d send failed.\n" ,state .seq_curr - 1 );
72
72
}
@@ -118,7 +118,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
118
118
ack_packet -> type = FINACK ;
119
119
ack_packet -> seqnum = 0 ;
120
120
ack_packet -> checksum = 0 ;
121
- status = sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , state .address , state .socklen );
121
+ status = maybe_sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , state .address , state .socklen );
122
122
if (status == -1 ){
123
123
printf ("ERROR: FINACK send failed.Retrying ...\n" );
124
124
state .state = ESTABLISHED ;
@@ -131,7 +131,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
131
131
packet -> type = FIN ;
132
132
packet -> seqnum = 0 ;
133
133
packet -> checksum = 0 ;
134
- status = sendto (sockfd , packet , sizeof (* packet ), 0 , state .address , state .socklen );
134
+ status = maybe_sendto (sockfd , packet , sizeof (* packet ), 0 , state .address , state .socklen );
135
135
if (status == -1 ){
136
136
printf ("ERROR: FIN send failed.Retrying ...\n" );
137
137
state .state = FIN_SENT ;
@@ -162,6 +162,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
162
162
ack_packet -> seqnum = state .seqnum ;
163
163
ack_packet -> checksum = 0 ;
164
164
is_seq = true;
165
+ printf ("LEN: %d\n" ,strlen (packet -> data ));
165
166
return strlen (packet -> data );
166
167
}else {
167
168
printf ("INFO: DATA packet has the incorrect sequence number.\n" );
@@ -170,7 +171,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
170
171
is_seq = false;
171
172
}
172
173
/* Sending ACK / duplicate ACK */
173
- if (sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , state .address , state .socklen ) == -1 ) {
174
+ if (maybe_sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , state .address , state .socklen ) == -1 ) {
174
175
printf ("ERROR: ACK sending failed.\n" );
175
176
state .state = CLOSED ;
176
177
break ;
@@ -210,7 +211,7 @@ int gbn_close(int sockfd){
210
211
fin_packet -> seqnum = 0 ;
211
212
fin_packet -> checksum = (uint16_t ) 0 ;
212
213
fin_packet -> checksum = checksum (fin_packet , sizeof (* fin_packet ) / sizeof (uint16_t ));
213
- status = sendto (sockfd , fin_packet , sizeof (* fin_packet ), 0 , state .address , state .socklen );
214
+ status = maybe_sendto (sockfd , fin_packet , sizeof (* fin_packet ), 0 , state .address , state .socklen );
214
215
if (status == -1 ){
215
216
printf ("ERROR: FIN send failed.Retrying ...\n" );
216
217
state .state = ESTABLISHED ;
@@ -247,7 +248,7 @@ int gbn_close(int sockfd){
247
248
fin_ack_packet -> seqnum = 0 ;
248
249
fin_ack_packet -> checksum = (uint16_t ) 0 ;
249
250
fin_ack_packet -> checksum = checksum (fin_ack_packet , sizeof (* fin_ack_packet ) / sizeof (uint16_t ));
250
- status = sendto (sockfd , fin_ack_packet , sizeof (* fin_ack_packet ), 0 , state .address , state .socklen );
251
+ status = maybe_sendto (sockfd , fin_ack_packet , sizeof (* fin_ack_packet ), 0 , state .address , state .socklen );
251
252
if (status == -1 ){
252
253
printf ("ERROR: FINACK send failed.Retrying ...\n" );
253
254
state .state = FIN_RCVD ;
@@ -295,7 +296,7 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
295
296
syn_packet -> seqnum = 0 ;
296
297
syn_packet -> checksum = (uint16_t ) 0 ;
297
298
syn_packet -> checksum = checksum (syn_packet , sizeof (* syn_packet ) / sizeof (uint16_t ));
298
- status = sendto (sockfd , syn_packet , sizeof (* syn_packet ), 0 , server , socklen );
299
+ status = maybe_sendto (sockfd , syn_packet , sizeof (* syn_packet ), 0 , server , socklen );
299
300
if (status == -1 ){
300
301
printf ("ERROR: SYN send failed.Retrying ...\n" );
301
302
state .state = CLOSED ;
@@ -320,7 +321,7 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
320
321
ack_packet -> seqnum = 1 ;
321
322
ack_packet -> checksum = (uint16_t ) 0 ;
322
323
ack_packet -> checksum = checksum (ack_packet , sizeof (* ack_packet ) / sizeof (uint16_t ));
323
- status = sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , server , socklen );
324
+ status = maybe_sendto (sockfd , ack_packet , sizeof (* ack_packet ), 0 , server , socklen );
324
325
if (status != -1 ){
325
326
state .state = ESTABLISHED ;
326
327
state .seqnum = ack_packet -> seqnum ;
@@ -411,7 +412,7 @@ int gbn_accept(int sockfd, struct sockaddr *client, socklen_t *socklen){
411
412
syn_ack_packet -> seqnum = 1 ;
412
413
syn_ack_packet -> checksum = (uint16_t ) 0 ;
413
414
syn_ack_packet -> checksum = checksum (syn_ack_packet , sizeof (* syn_ack_packet ) / sizeof (uint16_t ));
414
- status = sendto (sockfd , syn_ack_packet , sizeof (* syn_ack_packet ), 0 , client , * socklen );
415
+ status = maybe_sendto (sockfd , syn_ack_packet , sizeof (* syn_ack_packet ), 0 , client , * socklen );
415
416
if (status != -1 ){
416
417
printf ("INFO: SYN_ACK sent successfully.\n" );
417
418
free (syn_ack_packet );
0 commit comments