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
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.
| 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 | ||||||||||||||||||||||||||||||
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.
| 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Checksum | |||||||||||||||
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
