Skip to content
Snippets Groups Projects
Commit 9b8b603f authored by Alexandru-Daniel BARBU's avatar Alexandru-Daniel BARBU
Browse files

Merge branch 'AlexD/lab03' into 'master'

Lab 3 completed also part of lab 2 is here

See merge request !1
parents 74d349da 80f3992e
No related branches found
No related tags found
1 merge request!1Lab 3 completed also part of lab 2 is here
...@@ -23,28 +23,80 @@ int main(int argc,char** argv) { ...@@ -23,28 +23,80 @@ int main(int argc,char** argv) {
init(HOST,PORT); init(HOST,PORT);
struct l3_msg t; 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 */ /* We have to convert it to host order */
int len = link_recv(&t, sizeof(struct l3_msg)); uint32_t recv_sum = ntohl(t.hdr.sum);
DIE(len < 0, "Receive message"); 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 */ /* Since we are sending messages with a payload of 1500 - sizeof(header), most of the times the bytes from
uint32_t recv_sum = ntohl(t.hdr.sum); * 30 - 1500 will be corrupted and thus when we are printing or string message "Hello world" we see no probems.
t.hdr.sum = 0; * This will be visible when we will be sending a file */
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 printf("[RECV] len=%d; sum(%s)=0x%04hx; payload=\"%s\";\n", t.hdr.len, sum_ok ? "GOOD" : "BAD", recv_sum, t.payload);
* 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); /* 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 */ /* TODO 3.3: Adjust the corruption rate */
......
...@@ -20,29 +20,47 @@ int main(int argc,char** argv) { ...@@ -20,29 +20,47 @@ int main(int argc,char** argv) {
/* Look in common.h for the definition of l3_msg */ /* Look in common.h for the definition of l3_msg */
struct l3_msg t; struct l3_msg t;
/* We set the payload */ while (1)
sprintf(t.payload, "Hello my World of PC!"); {
t.hdr.len = strlen(t.payload) + 1; /* We set the payload */
sprintf(t.payload, "Hello my World of PC!");
t.hdr.len = strlen(t.payload) + 1;
/* Add the checksum */ /* Add the checksum */
/* Note that we compute the checksum for both header and data. Thus /* Note that we compute the checksum for both header and data. Thus
* we set the checksum equal to 0 when computing it */ * we set the checksum equal to 0 when computing it */
t.hdr.sum = 0; t.hdr.sum = 0;
/* Since sum is on 32 bits, we have to convert it to network order */ /* 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))); 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 */ /* Send the message */
link_send(&t, sizeof(struct l3_msg));
/* 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 /* We have to convert it to host order */
* chunks of that file given a MTU of 1500 bytes */ 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; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment