Skip to content

Commit 63f86a7

Browse files
committed
checksum for send and receive
1 parent a517d15 commit 63f86a7

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

go-back-n/gbn.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
3434
state.seq_max=state.seq_base+state.win_size-1;
3535
int status=-1;
3636
int count=0;
37-
printf("INFO:Length of file: %d bytes\n",len);
37+
printf("INFO: Length of file: %d bytes\n",len);
3838
/*Calculate number of packets*/
3939
int packet_num;
4040
packet_num=len/DATALEN+1;
@@ -50,7 +50,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
5050
gbnhdr *data_packet=malloc(sizeof(*data_packet));
5151
data_packet->type=DATA;
5252
data_packet->seqnum=state.seq_curr;
53-
data_packet->checksum=0;
53+
data_packet->checksum= (uint16_t) 0;
5454
if (state.seq_curr<packet_num){
5555
memcpy(data_packet->data,buf+(state.seq_curr-1)*DATALEN,DATALEN);
5656
}
@@ -62,12 +62,16 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
6262
reset_alrm=false;
6363
}
6464

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+
6569
status=sendto(sockfd,data_packet,sizeof(*data_packet),0,state.address,state.socklen);
6670
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);
6872
}
6973
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);
7175
}
7276

7377
/*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){
101105

102106
status = recvfrom(sockfd, packet, sizeof(*packet), 0, state.address, &state.socklen);
103107
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);
104112
if(packet->type == FIN){
105113
/* Sender has initiated connection teardown */
106114
printf("INFO: Connection teardown initiated ...\n");
@@ -146,7 +154,7 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
146154
/* Sender has sent a DATA packet */
147155
printf("INFO: DATA received successfully.\n");
148156
/* 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);
150158
if(state.seqnum == packet->seqnum){
151159
printf("INFO: Received DATA packet is in sequence.\n");
152160
state.seqnum = packet->seqnum + 1;
@@ -198,7 +206,8 @@ int gbn_close(int sockfd){
198206
/* Sender to initiate connection teardown with FIN packet */
199207
fin_packet->type = FIN;
200208
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));
202211
status = sendto(sockfd, fin_packet, sizeof(*fin_packet), 0, state.address, state.socklen);
203212
if(status == -1){
204213
printf("ERROR: FIN send failed.Retrying ...\n");
@@ -234,7 +243,8 @@ int gbn_close(int sockfd){
234243
/* Sender responding with a FINACK */
235244
fin_ack_packet->type = FINACK;
236245
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));
238248
status = sendto(sockfd, fin_ack_packet, sizeof(*fin_ack_packet), 0, state.address, state.socklen);
239249
if(status == -1){
240250
printf("ERROR: FINACK send failed.Retrying ...\n");
@@ -273,14 +283,16 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
273283
state.state = CLOSED;
274284
int retryCount = 1;
275285
int status = 0;
286+
276287

277288
while(retryCount <= MAX_RETRY_ATTEMPTS && state.state != ESTABLISHED){
278289
if(state.state != SYN_SENT){
279290
/* SYN to be sent to the receiver */
280291
gbnhdr *syn_packet = malloc(sizeof(*syn_packet));
281292
syn_packet->type = SYN;
282293
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));
284296
status = sendto(sockfd, syn_packet, sizeof(*syn_packet), 0, server, socklen);
285297
if(status == -1){
286298
printf("ERROR: SYN send failed.Retrying ...\n");
@@ -304,7 +316,8 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
304316
gbnhdr *ack_packet = malloc(sizeof(*ack_packet));
305317
ack_packet->type = ACK;
306318
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));
308321
status = sendto(sockfd, ack_packet, sizeof(*ack_packet), 0, server, socklen);
309322
if(status != -1){
310323
state.state = ESTABLISHED;
@@ -394,7 +407,8 @@ int gbn_accept(int sockfd, struct sockaddr *client, socklen_t *socklen){
394407
gbnhdr *syn_ack_packet = malloc(sizeof(*syn_ack_packet));
395408
syn_ack_packet->type = SYNACK;
396409
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));
398412
status = sendto(sockfd, syn_ack_packet, sizeof(*syn_ack_packet), 0, client, *socklen);
399413
if(status != -1){
400414
printf("INFO: SYN_ACK sent successfully.\n");

go-back-n/gbn.o

640 Bytes
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)