Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write code to read binary data of a UDP header and generate a response

ID: 3857912 • Letter: Y

Question

You will write code to read binary data of a UDP header and generate a response header.

The data in a UDP header is shown in the table below. The header has 64-bit (8 bytes) long data. It is composed of four parts: source port, destination port, length, and checksum. As you see, each part is 16-bit (2 bytes) long.

Instructions

Write a program that will do the following:

Read the binary file given below into an unsigned char array (unsigned char [8]).

Print the header in the format described below.

Create a response header based on the rules listed below.

Print the response header in the same way the original header was printed.

Write it to a new file.

The code must have the following functions:

int printheader(unsigned char []);
prints out given header data

int readfile(unsigned char [], char []);
reads header data from a file to an array
Tip: To read binary data from a file, use the fread() function

int writefile(unsigned char [], char []); writes header data from an array to a file
Tip: To write binary data to a file, use the fwrite() function

Submit the following:

Proof of compilation by Makefile

Source code

Output for the given binary data (each for test1.bin and test2.bin)
Your program must print required parts of both request and response headers,
and the output must be self-explanatory (don't just print a bunch of numbers).

Response header generated by your program (each for test1.bin and test2.bin)

Input files

Download the following UDP header binary files. For your guidance, the data in each file is displayed in hexadecimal.

test1.bin

02 38 5C AD 00 04 03 B0

test2.bin

FF FF CA 1D 70 E1 AA BE

Format to print a header

You must print the following information about a UDP header. Source port, destination port and length must be printed in unsigned decimal. Checksum must be printedn in hexadecimal.

Source port (unsigned decimal)

Destination port (unsigned decimal)

Length (unsigned decimal)

Checksum (hexadecimal)

Rules to generate a response header file

For every part, don't use any arithmetic operators, i.e., "+", "-", "*" and "/".

By bitwise operation, check if the source port is above 61439 (EFFF). If so, zero out the first (most significant) two bits of it. Otherwise, don't change it.

After that, the source and destination ports must be switched.

By bitwise operation, multiply Length by 2. You can ignore overflow.

Flip each of the red bits specified below in Checksum, i.e., 1 should be changed into 0, and vice versa.

Example:

If you have the following input data in hexadecimal:

00 53 13 56 02 00 FF 00

The output would be:

13 56 00 53 04 00 EB 28

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Source Port Destination Port 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 Length Checksum

Explanation / Answer

Code:

#include <stdio.h>

int printheader(unsigned char []);

int readfile(unsigned char [], char []);

int create_response(unsigned char [], unsigned char []);

int writefile(unsigned char [], char []);

int main()

{

char inf[30]="test1.bin";

char outf[30]="test2.bin";

unsigned char in_buf[9]="", out_buf[9]="";

  

if(readfile(in_buf,inf)!=8)

{

printf("read data failed ");

return -1;

}

printf("input header is ");

  

printheader(in_buf);

create_response(in_buf,out_buf);

printf("output header is ");

printheader(out_buf);

  

if(writefile(out_buf,outf)!=8)

{

printf("wrtite response to file got failed ");

return -1;

}

  

  

return 0;

}

int printheader(unsigned char buf[])

{

int i=0;

for(i=0;i<8;i++)

{

printf("%x%x ",(buf[i]>>4),(buf[i]&0xf));

}

printf(" ");

}

int readfile(unsigned char buf[], char file_name[])

{

FILE *fp;

fp = fopen(file_name,"rb");

/* Seek to the beginning of the file */

fseek(fp, 0, SEEK_SET);

/* Read and display data */

return fread(buf, 1, 8, fp);

}

int writefile(unsigned char buf[], char file_name[])

{

FILE *fp;

fp = fopen(file_name, "wb");

/* Seek to the beginning of the file */

fseek(fp, 0, SEEK_SET);

/* Read and display data */

return fwrite(buf, 1, 8, fp);

}

int create_response(unsigned char in_buf[], unsigned char out_buf[])

{

  

out_buf[0] = in_buf[2];

out_buf[1] = in_buf[3];

  

  

if((in_buf[0]&0xf)==0xf)

out_buf[2]=in_buf[0]&3;

else

out_buf[2]=in_buf[0];

out_buf[3]=in_buf[1];

  

out_buf[4]=(in_buf[4]<<1) | (in_buf[5]>>7);

out_buf[5]=(in_buf[5]<<1);

out_buf[6] = (in_buf[6]^0xff);

out_buf[7] = (in_buf[7]^0xff);

  

}

Output:

test@user:~$ ./a.out
input header is
05 05 05 05 05 05 05 05
output header is
05 05 05 05 0a 0a fa fa

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote