diff --git a/lab3/link_emulator.err b/lab3/link_emulator.err new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/lab3/link_emulator.out b/lab3/link_emulator.out new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/lab3/recv.c b/lab3/recv.c index 84f3e99bfe2aa8d2f7399955da3d1cbb810a49d9..46915188f013bbd66b98c997e130be96bc132a82 100644 --- a/lab3/recv.c +++ b/lab3/recv.c @@ -23,28 +23,80 @@ int main(int argc,char** argv) { init(HOST,PORT); struct l3_msg t; + + int output_file = open("recv.data", O_WRONLY | O_APPEND); + + while (1) + { + /* Receive the frame from the link */ + int len = link_recv(&t, sizeof(struct l3_msg)); + DIE(len < 0, "Receive message"); - /* Receive the frame from the link */ - int len = link_recv(&t, sizeof(struct l3_msg)); - DIE(len < 0, "Receive message"); + /* We have to convert it to host order */ + uint32_t recv_sum = ntohl(t.hdr.sum); + t.hdr.sum = 0; + int sum_ok = (simple_csum((void *) &t, sizeof(struct l3_msg)) == recv_sum); + /* TODO 2: Change to crc32 */ - /* We have to convert it to host order */ - uint32_t recv_sum = ntohl(t.hdr.sum); - t.hdr.sum = 0; - int sum_ok = (simple_csum((void *) &t, sizeof(struct l3_msg)) == recv_sum); - /* TODO 2: Change to crc32 */ + /* Since we are sending messages with a payload of 1500 - sizeof(header), most of the times the bytes from + * 30 - 1500 will be corrupted and thus when we are printing or string message "Hello world" we see no probems. + * This will be visible when we will be sending a file */ - /* Since we are sending messages with a payload of 1500 - sizeof(header), most of the times the bytes from - * 30 - 1500 will be corrupted and thus when we are printing or string message "Hello world" we see no probems. - * This will be visible when we will be sending a file */ + printf("[RECV] len=%d; sum(%s)=0x%04hx; payload=\"%s\";\n", t.hdr.len, sum_ok ? "GOOD" : "BAD", recv_sum, t.payload); - printf("[RECV] len=%d; sum(%s)=0x%04hx; payload=\"%s\";\n", t.hdr.len, sum_ok ? "GOOD" : "BAD", recv_sum, t.payload); + /* TODO 3.1: In a loop, recv a frame and check if the CRC is good */ + if (sum_ok == 0) + { + /* TODO 3.2: If the crc is bad, send a NACK frame */ + + printf("Frame is busted\n"); + + /* Look in common.h for the definition of l3_msg */ + struct l3_msg nack; - /* TODO 3.1: In a loop, recv a frame and check if the CRC is good */ + /* We set the payload */ + sprintf(nack.payload, "nnnnnnnn"); + nack.hdr.len = strlen(nack.payload) + 1; - /* TODO 3.2: If the crc is bad, send a NACK frame */ + /* Add the checksum */ + /* Note that we compute the checksum for both header and data. Thus + * we set the checksum equal to 0 when computing it */ + nack.hdr.sum = 0; + + /* Since sum is on 32 bits, we have to convert it to network order */ + nack.hdr.sum = htonl(simple_csum((void *) &nack, sizeof(struct l3_msg))); + + /* Send the message */ + link_send(&nack, sizeof(struct l3_msg)); + + } else { + /* TODO 3.2: Otherwise, write the frame payload to a file recv.data */ + printf("Good frame\n"); + + + write(output_file, t.payload, strlen(t.payload)); + /* Look in common.h for the definition of l3_msg */ + struct l3_msg ack; + + /* We set the payload */ + sprintf(ack.payload, "aaaaaaaa"); + ack.hdr.len = strlen(ack.payload) + 1; + + /* Add the checksum */ + /* Note that we compute the checksum for both header and data. Thus + * we set the checksum equal to 0 when computing it */ + ack.hdr.sum = 0; + + /* Since sum is on 32 bits, we have to convert it to network order */ + ack.hdr.sum = htonl(simple_csum((void *) &ack, sizeof(struct l3_msg))); + + /* Send the message */ + link_send(&ack, sizeof(struct l3_msg)); + break; + } + } + - /* TODO 3.2: Otherwise, write the frame payload to a file recv.data */ /* TODO 3.3: Adjust the corruption rate */ diff --git a/lab3/send.c b/lab3/send.c index c9f4e5b203d9838cf1b06a7e09ff81187bded35a..cc5c43c6c752b28ac4775e6d798888712160f5db 100644 --- a/lab3/send.c +++ b/lab3/send.c @@ -20,29 +20,47 @@ int main(int argc,char** argv) { /* Look in common.h for the definition of l3_msg */ struct l3_msg t; - /* We set the payload */ - sprintf(t.payload, "Hello my World of PC!"); - t.hdr.len = strlen(t.payload) + 1; + while (1) + { + /* We set the payload */ + sprintf(t.payload, "Hello my World of PC!"); + t.hdr.len = strlen(t.payload) + 1; - /* Add the checksum */ - /* Note that we compute the checksum for both header and data. Thus - * we set the checksum equal to 0 when computing it */ - t.hdr.sum = 0; + /* Add the checksum */ + /* Note that we compute the checksum for both header and data. Thus + * we set the checksum equal to 0 when computing it */ + t.hdr.sum = 0; - /* Since sum is on 32 bits, we have to convert it to network order */ - t.hdr.sum = htonl(simple_csum((void *) &t, sizeof(struct l3_msg))); + /* Since sum is on 32 bits, we have to convert it to network order */ + t.hdr.sum = htonl(simple_csum((void *) &t, sizeof(struct l3_msg))); - /* TODO 2.0: Call crc32 function */ + /* TODO 2.0: Call crc32 function */ - /* Send the message */ - link_send(&t, sizeof(struct l3_msg)); + /* Send the message */ - /* TODO 3.1: Receive the confirmation */ + link_send(&t, sizeof(struct l3_msg)); - /* TODO 3.2: If we received a NACK, retransmit the previous frame */ + /* TODO 3.1: Receive the confirmation */ + struct l3_msg response; + int len_resp = link_recv(&response, sizeof(struct l3_msg)); + DIE(len_resp < 0, "Receive message"); - /* TODO 3.3: Update this to read the content of a file and send it as - * chunks of that file given a MTU of 1500 bytes */ + /* We have to convert it to host order */ + uint32_t recv_sum_resp = ntohl(response.hdr.sum); + response.hdr.sum = 0; + int sum_ok_resp = (simple_csum((void *) &response, sizeof(struct l3_msg)) == recv_sum_resp); + /* TODO 2: Change to crc32 */ + + /* TODO 3.2: If we received a NACK, retransmit the previous frame */ + if (strchr(response.payload, 'n')) + { + link_send(&t, sizeof(struct l3_msg)); + } else break; + + /* TODO 3.3: Update this to read the content of a file and send it as + * chunks of that file given a MTU of 1500 bytes */ + } + return 0; }