Skip to content

Commit f2f1179

Browse files
committed
Giant1 doesn't work sometime
1 parent 7296496 commit f2f1179

File tree

5 files changed

+9
-14
lines changed

5 files changed

+9
-14
lines changed

go-back-n/gbn.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,20 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
8686
reset_alrm=false;
8787
}
8888
if(state.seq_curr-1 == packet_num && len%DATALEN > 0){
89-
uint8_t temp[DATALEN];
90-
memset(temp,0,DATALEN);
91-
memcpy(temp,data_packet->data,len%DATALEN);
92-
memcpy(data_packet->data,temp,DATALEN);
93-
//data_packet->checksum = checksum(data_packet, (len%DATALEN + sizeof(data_packet->type)+ sizeof(data_packet->seqnum)+ sizeof(data_packet->checksum) )/ sizeof(uint16_t));
94-
data_packet->checksum = checksum(data_packet, sizeof(*data_packet) / sizeof(uint16_t));
89+
data_packet->checksum = checksum(data_packet, (len%DATALEN + sizeof(data_packet->type)+ sizeof(data_packet->seqnum)+ sizeof(data_packet->checksum) )/ sizeof(uint16_t));
9590
}else{
9691
data_packet->checksum = checksum(data_packet, sizeof(*data_packet) / sizeof(uint16_t));
9792
}
9893
printf("INFO: Checksum of packet: %d\n",data_packet->checksum);
9994

10095
if(state.seq_curr-1==packet_num){
10196
if (len%DATALEN>0)
102-
status=sendto(sockfd,data_packet,5+len%DATALEN,0,state.address,state.socklen);
97+
status=maybe_sendto(sockfd,data_packet,5+len%DATALEN,0,state.address,state.socklen);
10398
else
104-
status=sendto(sockfd,data_packet,sizeof(*data_packet),0,state.address,state.socklen);
99+
status=maybe_sendto(sockfd,data_packet,sizeof(*data_packet),0,state.address,state.socklen);
105100
state.win_size=1;
106101
}else if(state.seq_curr-1<packet_num)
107-
status=sendto(sockfd,data_packet,sizeof(*data_packet),0,state.address,state.socklen);
102+
status=maybe_sendto(sockfd,data_packet,sizeof(*data_packet),0,state.address,state.socklen);
108103

109104
if(status==-1){
110105
printf("ERROR: DATA packet %d send failed.\n",state.seq_curr-1);
@@ -156,7 +151,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
156151
if(status != -1){
157152
uint16_t checksum_actual = packet->checksum;
158153
packet->checksum = (uint16_t) 0;
159-
uint16_t checksum_expected = checksum(packet, sizeof(*packet) / sizeof(uint16_t));
154+
uint16_t checksum_expected = checksum(packet, status / sizeof(uint16_t));
160155
printf("INFO: Expected checksum value : %d . Actual checksum value : %d\n",checksum_expected,checksum_actual);
161156
if(packet->type == DATA){
162157
/* Sender has sent a DATA packet */
@@ -187,7 +182,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
187182
is_seq = false;
188183
}
189184
/* Sending ACK / duplicate ACK */
190-
if (sendto(sockfd, ack_packet, sizeof(*ack_packet), 0, state.address, state.socklen) == -1) {
185+
if (maybe_sendto(sockfd, ack_packet, sizeof(*ack_packet), 0, state.address, state.socklen) == -1) {
191186
printf("ERROR: ACK sending failed.\n");
192187
state.state = CLOSED;
193188
break;
@@ -364,7 +359,7 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
364359
syn_packet->seqnum = 0;
365360
syn_packet->checksum = (uint16_t) 0;
366361
syn_packet->checksum = checksum(syn_packet, sizeof(*syn_packet) / sizeof(uint16_t));
367-
status = sendto(sockfd, syn_packet, sizeof(*syn_packet), 0, server, socklen);
362+
status = maybe_sendto(sockfd, syn_packet, sizeof(*syn_packet), 0, server, socklen);
368363
if(status == -1){
369364
printf("ERROR: SYN send failed.Retrying ...\n");
370365
state.state = CLOSED;
@@ -389,7 +384,7 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
389384
ack_packet->seqnum = 1;
390385
ack_packet->checksum = (uint16_t) 0;
391386
ack_packet->checksum = checksum(ack_packet, sizeof(*ack_packet) / sizeof(uint16_t));
392-
status = sendto(sockfd, ack_packet, sizeof(*ack_packet), 0, server, socklen);
387+
status = maybe_sendto(sockfd, ack_packet, sizeof(*ack_packet), 0, server, socklen);
393388
if(status != -1){
394389
state.state = ESTABLISHED;
395390
state.seqnum=ack_packet->seqnum;
@@ -480,7 +475,7 @@ int gbn_accept(int sockfd, struct sockaddr *client, socklen_t *socklen){
480475
syn_ack_packet->seqnum = 1;
481476
syn_ack_packet->checksum = (uint16_t) 0;
482477
syn_ack_packet->checksum = checksum(syn_ack_packet, sizeof(*syn_ack_packet) / sizeof(uint16_t));
483-
status = sendto(sockfd, syn_ack_packet, sizeof(*syn_ack_packet), 0, client, *socklen);
478+
status = maybe_sendto(sockfd, syn_ack_packet, sizeof(*syn_ack_packet), 0, client, *socklen);
484479
if(status != -1){
485480
printf("INFO: SYN_ACK sent successfully.\n");
486481
free(syn_ack_packet);

go-back-n/gbn.o

-520 Bytes
Binary file not shown.

go-back-n/output

3.93 MB
Binary file not shown.

go-back-n/receiver

0 Bytes
Binary file not shown.

go-back-n/sender

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)