Question: ( Program in C ) Write a function that parses the header structure of a TCP segment and displays the Source Port, Destination Port, Sequence
Program in CWrite a function that parses the header structure of a TCP segment and displays the Source Port, Destination Port, Sequence Number, Acknowledgement Number, and Flags values on the console.
For this exercise, you are provided with a program where the main function will read the segment data from stdin and call your function with a pointer to the buffer containing the segment and a parameter specifying the segment length.
The function youre writing has the following definition:
int DisplayTCPSegmentunsigned char pSegment int segmentLen;
where pSegment points to the segment data and segmentLen specifies the size of the segment. The function must display the port numbers in decimal form and the value for each of the eight flags contained in the headers flags field. The function must use bitwise operators to evaluate the individual values for each flag. The program must list the flags that are set in the flags field. The TCP Header flags are defined in the table below.
Your program coded in c must use preprocessor constants to represent the individual flag values. The output of the function should look something like this:
TCP Header Fields:
Source Port:
Destination Port:
Sequence No:
Ack No:
Flags:
ACK SYN
Here is what I have so far:
#include
#include
CONSTANTS
#define CWRFLAG x bitmaska to use on the flags byte
#define ECEFLAG x
#define URGFLAG x
#define ACKFLAG x
#define PSHFLAG x
#define RSTFLAG x
#define SYNFLAG x
#define FINFLAG x
typedefs and structures
globalVariables
FunctionPrototypes
void DumpHexconst void data, sizet size, int width;
Function: main
Parameters:
argc main parameter for argument count
argv main parameter for argument values
int mainint argc, char argv
unsigned char segmentBuffer;
int bytesRead;
Read the TCP segment from stdin
bytesRead freadsegmentBuffer stdin;
Uncomment this line to dump the packet to the console
DumpHexsegmentBuffer bytesRead, ;
ADD YOUR CALL TO DisplayTCPSegment HERE
DisplayTCPSegmentsegmentBuffer bytesRead;
return ;
int DisplayTCPSegmentunsigned char pSegment int segmentLen
unsigned short sourcePort;
unsigned short destPort;
int sequenceNumber ;
int ackNumber;
DISPLAY
printfTCP Header Fields:
;
SOURCE PORT
sourcePort pSegment pSegment;
printfSource Port: u
sourcePort;
DESTINATION PORT
destPort pSegment pSegment;
printfDestination Port: u
destPort;
SEQUENCE NUMBER
sequenceNumber pSegmentpSegmentpSegment pSegment;
printfSequence No: u
sequenceNumber;
ACKNOWLEDGEMENT NUMBER
printfAck No: u
ackNumber;
FLAGS bits
pSegment ;
unsigned char flags pSegment;
printfFlags: u
flags;
ifflags & SYNFLAG If not zero, the flag IS set
printfSYN
;
return ;
DumpHex
Formatted output of raw data.
void DumpHexconst void data, sizet size, int width
char ascii;
sizet i j;
asciiwidth;
for i ; i size; i
Print the value
printfX unsigned chardatai;
save the ascii display value in the relative position
if unsigned chardatai && unsigned chardatai~
asciii widthunsigned chardatai;
else
asciii width;
if iwidth i size
printf;
if i width
