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

In C only please(not C++ or C#)!!! Write a program that will read a set of Winte

ID: 3571929 • Letter: I

Question

In C only please(not C++ or C#)!!!

Write a program that will read a set of WinterNet Protocol (WP) packets from a file named “packets” and display the data in each packet. Supply the file name as a command line argument. Use binary file input and the read()function. WP packets have the structure given below. Read the entire header from the file into a struct s_header variable first, then process each field. You will have to use some bit shifting and bit masking to get the data out of some of the fields.

Sample Code in no particular order:

// header struct
struct s_header {

   unsigned char verCode;
   unsigned char serviceType;
   unsigned short totLength;
   unsigned char reserved;
   unsigned char flags;
   unsigned short hopCount;
   unsigned long srcAddr;
   unsigned long destAddr;
};
// string tables
char * codes[] = {
   "OK"
   ,
   "Possible data loss"
};
char * services[] = {
   "Normal"
   ,
   "Low Priority"
   ,
   "High Priority"
   ,
   "Medium Priority"
   ,
   "So
   - so Priority"
   ,
   "Needed Yesterday"
   ,
   "Nevermind"
};
char * flags[] = {
   " Grumpy: "
   ,
   " Sneezy: "
   ,
   " Bashful: "
   ,
   " Doc: "
   ,
   " Dopey: "
   ,
   " Tipsy: "
   ,
   " Surly: "
   ,
   " SnowWhite: "
};
// prototypes
void displayHeader(struct s_header);
// a header
struct s_header header;
// open file
//find where these go
header.verCode >> 4
header.verCode & 0x0F
// display flags
x = header.flags;
for (i = 0; i < 8; i++) {
   printf("%s%d
       ", flags[i], x & 1);
       x = x >> 1;
}
// this will display a piece of the source addresses
addr_ptr = (unsigned char *)&header.srcAddr;
printf("Source Address: %dn", addr_ptr[0]);
// display header data
displayHeader(header);
// read in header
bytesRead = fread(&header, 1, 16, fp);
// read and display data portion of packet
idataSize = header.totLength - 16; // calculate data size
dbuf = (char *) new[idataSize + 1]; // get a buffer
fread(dbuf, 1, idataSize, fp); // read data into buffer
dbuf[idataSize] = 0; // add a null terminator
printf("Data: %s ", dbuf);

Packet(This is what will be in the file named packets):

@_ Å* ÞÞÞÞMarzidotes and dozidotes and little lambsydivy. A kiddlydivy too wouldn't you?AU IÛ****Now is the time for all good men to come to the aid of their country.!Ê Øï5dÏ.æÚSpace. The final frontier. These are the voyages of the starship Enterprise. Its 5-year mission, to seek out new life and new civilizations, to boldly go where no man has gone before!

Packet Format: 15 16 24 25 3 4 7 8 Ver Code Service Type Total Length Flags Reserved Hop Count Source Address Destination Address Data 31

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>   

#include<string.h>   

#include<netinet/ip_icmp.h>  

#include<netinet/udp.h>  

#include<netinet/tcp.h>  

#include<netinet/ip.h>   

#include<sys/socket.h>

#include<arpa/inet.h>

void ProcessPak(unsigned char* , int);

void print_ip_header(unsigned char* , int);

void TCP-PACK(unsigned char* , int);

void UDP-PACK(unsigned char * , int);

void ICMP-PACK(unsigned char* , int);

void PrintData (unsigned char* , int);

int sock_raw;

FILE *logfile;

int tcp=0,udp=0,icmp=0,others=0,igmp=0,total=0,i,j;

struct sockaddr_in source,dest;

int main()

{

    int saddr_size , data_size;

    struct sockaddr saddr;

    struct in_addr in;

     

    unsigned char *buffer = (unsigned char *)malloc(65536); //Its Big!

     

    logfile=fopen("log.txt","w");

    if(logfile==NULL) printf("Unable to create file.");

    printf("Starting... ");

    //Create a raw socket that shall sniff

    sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);

    if(sock_raw < 0)

    {

        printf("Socket Error ");

        return 1;

    }

    while(1)

    {

        saddr_size = sizeof saddr;

        //Receive a packet

        data_size = recvfrom(sock_raw , buffer , 65536 , 0 , &saddr , &saddr_size);

        if(data_size <0 )

        {

            printf("Recvfrom error , failed to get packets ");

            return 1;

        }

        //Now process the packet

        ProcessPak(buffer , data_size);

    }

    close(sock_raw);

    printf("Finished");

    return 0;

}

void ProcessPak(unsigned char* buffer, int size)

{

    //Get the IP Header part of this packet

    struct iphdr *iph = (struct iphdr*)buffer;

    ++total;

    switch (iph->protocol) //Check the Protocol and do accordingly...

    {

        case 1: //ICMP Protocol

            ++icmp;

            //PrintIcmpPacket(Buffer,Size);

            break;

         

        case 2: //IGMP Protocol

            ++igmp;

            break;

         

        case 6: //TCP Protocol

            ++tcp;

            TCP-PACK(buffer , size);

            break;

         

        case 17: //UDP Protocol

            ++udp;

            UDP-PACK(buffer , size);

            break;

         

        default: //Some Other Protocol like ARP etc.

            ++others;

            break;

    }

    printf("TCP : %d   UDP : %d   ICMP : %d   IGMP : %d   Others : %d   Total : %d ",tcp,udp,icmp,igmp,others,total);

}

void print_ip_header(unsigned char* Buffer, int Size)

{

    unsigned short iphdrlen;

         

    struct iphdr *iph = (struct iphdr *)Buffer;

    iphdrlen =iph->ihl*4;

     

    memset(&source, 0, sizeof(source));

    source.sin_addr.s_addr = iph->saddr;

     

    memset(&dest, 0, sizeof(dest));

    dest.sin_addr.s_addr = iph->daddr;

     

    fprintf(logfile," ");

    fprintf(logfile,"IP Header ");

    fprintf(logfile,"   |-IP Version        : %d ",(unsigned int)iph->version);

    fprintf(logfile,"   |-IP Header Length : %d DWORDS or %d Bytes ",(unsigned int)iph->ihl,((unsigned int)(iph->ihl))*4);

    fprintf(logfile,"   |-Type Of Service   : %d ",(unsigned int)iph->tos);

    fprintf(logfile,"   |-IP Total Length   : %d Bytes(Size of Packet) ",ntohs(iph->tot_len));

    fprintf(logfile,"   |-Identification    : %d ",ntohs(iph->id));

    //fprintf(logfile,"   |-Reserved ZERO Field   : %d ",(unsigned int)iphdr->ip_reserved_zero);

    //fprintf(logfile,"   |-Dont Fragment Field   : %d ",(unsigned int)iphdr->ip_dont_fragment);

    //fprintf(logfile,"   |-More Fragment Field   : %d ",(unsigned int)iphdr->ip_more_fragment);

    fprintf(logfile,"   |-TTL      : %d ",(unsigned int)iph->ttl);

    fprintf(logfile,"   |-Protocol : %d ",(unsigned int)iph->protocol);

    fprintf(logfile,"   |-Checksum : %d ",ntohs(iph->check));

    fprintf(logfile,"   |-Source IP        : %s ",inet_ntoa(source.sin_addr));

    fprintf(logfile,"   |-Destination IP   : %s ",inet_ntoa(dest.sin_addr));

}

void TCP-PACK(unsigned char* Buffer, int Size)

{

    unsigned short iphdrlen;

     

    struct iphdr *iph = (struct iphdr *)Buffer;

    iphdrlen = iph->ihl*4;

     

    struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen);

             

    fprintf(logfile," ***********************TCP Packet************************* ");   

         

    print_ip_header(Buffer,Size);

         

    fprintf(logfile," ");

    fprintf(logfile,"TCP Header ");

    fprintf(logfile,"   |-Source Port      : %u ",ntohs(tcph->source));

    fprintf(logfile,"   |-Destination Port : %u ",ntohs(tcph->dest));

    fprintf(logfile,"   |-Sequence Number    : %u ",ntohl(tcph->seq));

    fprintf(logfile,"   |-Acknowledge Number : %u ",ntohl(tcph->ack_seq));

    fprintf(logfile,"   |-Header Length      : %d DWORDS or %d BYTES " ,(unsigned int)tcph->doff,(unsigned int)tcph->doff*4);

    //fprintf(logfile,"   |-CWR Flag : %d ",(unsigned int)tcph->cwr);

    //fprintf(logfile,"   |-ECN Flag : %d ",(unsigned int)tcph->ece);

    fprintf(logfile,"   |-Urgent Flag          : %d ",(unsigned int)tcph->urg);

    fprintf(logfile,"   |-Acknowledgement Flag : %d ",(unsigned int)tcph->ack);

    fprintf(logfile,"   |-Push Flag            : %d ",(unsigned int)tcph->psh);

    fprintf(logfile,"   |-Reset Flag           : %d ",(unsigned int)tcph->rst);

    fprintf(logfile,"   |-Synchronise Flag     : %d ",(unsigned int)tcph->syn);

    fprintf(logfile,"   |-Finish Flag          : %d ",(unsigned int)tcph->fin);

    fprintf(logfile,"   |-Window         : %d ",ntohs(tcph->window));

    fprintf(logfile,"   |-Checksum       : %d ",ntohs(tcph->check));

    fprintf(logfile,"   |-Urgent Pointer : %d ",tcph->urg_ptr);

    fprintf(logfile," ");

    fprintf(logfile,"                        DATA Dump                         ");

    fprintf(logfile," ");

         

    fprintf(logfile,"IP Header ");

    PrintData(Buffer,iphdrlen);

         

    fprintf(logfile,"TCP Header ");

    PrintData(Buffer+iphdrlen,tcph->doff*4);

         

    fprintf(logfile,"Data Payload ");

    PrintData(Buffer + iphdrlen + tcph->doff*4 , (Size - tcph->doff*4-iph->ihl*4) );

                         

    fprintf(logfile," ###########################################################");

}

void UDP-PACK(unsigned char *Buffer , int Size)

{

     

    unsigned short iphdrlen;

     

    struct iphdr *iph = (struct iphdr *)Buffer;

    iphdrlen = iph->ihl*4;

     

    struct udphdr *udph = (struct udphdr*)(Buffer + iphdrlen);

     

    fprintf(logfile," ***********************UDP Packet************************* ");

     

    print_ip_header(Buffer,Size);          

     

    fprintf(logfile," UDP Header ");

    fprintf(logfile,"   |-Source Port      : %d " , ntohs(udph->source));

    fprintf(logfile,"   |-Destination Port : %d " , ntohs(udph->dest));

    fprintf(logfile,"   |-UDP Length       : %d " , ntohs(udph->len));

    fprintf(logfile,"   |-UDP Checksum     : %d " , ntohs(udph->check));

     

    fprintf(logfile," ");

    fprintf(logfile,"IP Header ");

    PrintData(Buffer , iphdrlen);

         

    fprintf(logfile,"UDP Header ");

    PrintData(Buffer+iphdrlen , sizeof udph);

         

    fprintf(logfile,"Data Payload ");

    PrintData(Buffer + iphdrlen + sizeof udph ,( Size - sizeof udph - iph->ihl * 4 ));

     

    fprintf(logfile," ###########################################################");

}

void ICMP-PACK(unsigned char* Buffer , int Size)

{

    unsigned short iphdrlen;

     

    struct iphdr *iph = (struct iphdr *)Buffer;

    iphdrlen = iph->ihl*4;

     

    struct icmphdr *icmph = (struct icmphdr *)(Buffer + iphdrlen);

             

    fprintf(logfile," ***********************ICMP Packet************************* ");  

     

    print_ip_header(Buffer , Size);

             

    fprintf(logfile," ");

         

    fprintf(logfile,"ICMP Header ");

    fprintf(logfile,"   |-Type : %d",(unsigned int)(icmph->type));

             

    if((unsigned int)(icmph->type) == 11)

        fprintf(logfile," (TTL Expired) ");

    else if((unsigned int)(icmph->type) == ICMP_ECHOREPLY)

        fprintf(logfile," (ICMP Echo Reply) ");

    fprintf(logfile,"   |-Code : %d ",(unsigned int)(icmph->code));

    fprintf(logfile,"   |-Checksum : %d ",ntohs(icmph->checksum));

    //fprintf(logfile,"   |-ID       : %d ",ntohs(icmph->id));

    //fprintf(logfile,"   |-Sequence : %d ",ntohs(icmph->sequence));

    fprintf(logfile," ");

    fprintf(logfile,"IP Header ");

    PrintData(Buffer,iphdrlen);

         

    fprintf(logfile,"UDP Header ");

    PrintData(Buffer + iphdrlen , sizeof icmph);

         

    fprintf(logfile,"Data Payload ");

    PrintData(Buffer + iphdrlen + sizeof icmph , (Size - sizeof icmph - iph->ihl * 4));

     

    fprintf(logfile," ###########################################################");

}

void PrintData (unsigned char* data , int Size)

{

     

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

    {

        if( i!=0 && i%16==0)   //if one line of hex printing is complete...

        {

            fprintf(logfile,"         ");

            for(j=i-16 ; j<i ; j++)

            {

                if(data[j]>=32 && data[j]<=128)

                    fprintf(logfile,"%c",(unsigned char)data[j]); //if its a number or alphabet

                 

                else fprintf(logfile,"."); //otherwise print a dot

            }

            fprintf(logfile," ");

        }

         

        if(i%16==0) fprintf(logfile,"   ");

            fprintf(logfile," %02X",(unsigned int)data[i]);

                 

        if( i==Size-1) //print the last spaces

        {

            for(j=0;j<15-i%16;j++) fprintf(logfile,"   "); //extra spaces

             

            fprintf(logfile,"         ");

             

            for(j=i-i%16 ; j<=i ; j++)

            {

                if(data[j]>=32 && data[j]<=128) fprintf(logfile,"%c",(unsigned char)data[j]);

                else fprintf(logfile,".");

            }

            fprintf(logfile," ");

        }

    }