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 C)Write 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 DisplayTCPSegment(unsigned 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: 80
Destination Port: 59494
Sequence No: 4126498417
Ack No: 2904855160
Flags: 18
ACK SYN
Here is what I have so far:
#include
#include
///////////////////////////////////////////////////////
// CONSTANTS
///////////////////////////////////////////////////////
#define CWR_FLAG 0x80// bitmaska to use on the flags byte
#define ECE_FLAG 0x40
#define URG_FLAG 0x20
#define ACK_FLAG 0x10
#define PSH_FLAG 0x08
#define RST_FLAG 0x04
#define SYN_FLAG 0x02
#define FIN_FLAG 0x01
///////////////////////////////////////////////////////
// typedefs and structures
//////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// globalVariables
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// FunctionPrototypes
///////////////////////////////////////////////////////
void DumpHex(const void* data, size_t size, int width);
/*****************************************************
*
* Function: main()
*
* Parameters:
*
* argc - main parameter for argument count
* argv - main parameter for argument values
*
******************************************************/
int main(int argc, char *argv[])
{
unsigned char segmentBuffer[1500];
int bytesRead;
// Read the TCP segment from stdin
bytesRead = fread(segmentBuffer,1,1500, stdin);
// Uncomment this line to dump the packet to the console
DumpHex(segmentBuffer, bytesRead, 16);
// ADD YOUR CALL TO DisplayTCPSegment() HERE
DisplayTCPSegment(segmentBuffer, bytesRead);
return 0;
}
int DisplayTCPSegment(unsigned char *pSegment, int segmentLen)
{
unsigned short sourcePort;
unsigned short destPort;
int sequenceNumber =0;
int ackNumber;
/////DISPLAY
printf("TCP Header Fields:
");
/// SOURCE PORT
sourcePort =(pSegment[0]<<8)| pSegment[1]; //
printf("Source Port: %u
", sourcePort);
/// DESTINATION PORT
//destPort =(pSegment[0]<<8)| pSegment[1]; //
printf("Destination Port: %u
", destPort);
/// SEQUENCE NUMBER
//sequenceNumber =(pSegment[4]<<24)|(pSegment[5]<<16)|(pSegment[6]<<8)| pSegment[7];
printf("Sequence No: %u
", sequenceNumber);
/// ACKNOWLEDGEMENT NUMBER
printf("Ack No: %u
", ackNumber);
/// FLAGS (8 bits)
pSegment +=13;
unsigned char flags =*pSegment;
printf("Flags: %u
", flags);
if((flags & SYN_FLAG)!=0)// If not zero, the flag IS set
{
printf("SYN
");
}
return 0;
}
/*******************************************************************
*
* DumpHex()
*
* Formatted output of raw data.
*
*******************************************************************/
void DumpHex(const void* data, size_t size, int width)
{
char ascii[17];
size_t i, j;
ascii[width]='\0';
for (i =0; i < size; ++i)
{
// Print the value
printf("%02X ",((unsigned char*)data)[i]);
// save the ascii display value in the relative position
if (((unsigned char*)data)[i]>='' && ((unsigned char*)data)[i]<='~')
{
ascii[i % width]=((unsigned char*)data)[i];
}
else
{
ascii[i % width]='.';
}
if ((i+1)%(width /2)==0|| i+1== size)
{
printf("");
if ((i+1)% width ==

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!