In C language: This question has some of the sample code but it\'s not complete,
ID: 3572013 • Letter: I
Question
In C language: This question has some of the sample code but it's not complete, so can u plz complete and run it to make sure it works, cuz i didn't work with me. 1) Packet Processing. 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. Packet Format: 24 25 15 16 Total Length Hop Count Reserved Flags Source Address Destination Address Data Ver: FP version number. The most common version is 4. Code: Indicates status of this packet. Meaning Valu Possible data comuption Reserved Service Type: Valu Low Priority High Priority Medium Priority So-so Priority Needed Yesterday Nevermind Total Length: Total length of the packet in bytes. ld with the following field namesExplanation / Answer
Please find below the program for packet processing:
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cctype>
using namespace std;
// header struct
struct s_header { //Decleration
unsigned char verCode;
unsigned char serviceType;
unsigned short totLength;
unsigned char reserved;
unsigned short hopCount;
unsigned char flags;
unsigned char srcAddr0; //Source Address Declaration
unsigned char srcAddr1;
unsigned char destAddr0; //Dest. Address Declaration
unsigned char destAddr1;
} header;
// string tables
char * codes[] = {
"OK",
"Possible data loss"
};
//services decleration
char * services[] = {
"Normal",
"Low Priority",
"High Priority",
"Medium Priority",
"So-so Priority",
"Needed Yesterday",
"Nevermind"
};
//flags decleration
char * flags[] = {
"Grumpy",
"Sneezy",
"Bashful",
"Doc",
"Dopey",
"Tipsy",
"Surly",
"Snow White"
};
int main(int argc, char * argv)
{
const char* const file_name = "PACKET.txt";
int x;
char ch;
ifstream file(file_name, ios::in | ios::binary); // This will open for input in binary mode
if (!file.is_open())
{
std::cerr << "failed to open file "; //When it fails to open file
return 1;
}
do
{
s_header header; //This will create an object of s_header struct
const auto hdr_size = sizeof(header); //This will get the size of header file
if (file.read(reinterpret_cast<char*>(std::addressof(header)), hdr_size) &&
file.gcount() == hdr_size)
{
// This will print out the contents of header in a readable form
const auto idata_size = header.totLength - 16;
vector<char> buffer(idata_size); //idata_size to calculate the data size
if (file.read(buffer.data(), buffer.size()) && file.gcount() == idata_size)
{
// This will display the data from the header
cout << "Version:" << setw(9) << (header.verCode >> 4) << endl; // header version
cout << "Code:" << setw(12) << (header.verCode & 0x0F) << " - " << codes[(header.verCode & 0x0f)] << endl; // Print Area
cout << "Total Length:" << setw(5) << header.totLength << endl; //Total Length will be fetched
cout << "Service Type:" << setw(4) << (int)header.serviceType << " - " << services[header.serviceType] << endl;
//This will display the flags using a mask and the loop
x = header.flags;
for (int i = 0; i < 8; i++)
{
cout << " " << flags[i] << ": " << (x & 1) << endl;
x = x >> 1;
}
cout << "Hop Count:" << setw(8) << header.hopCount << endl;
cout << "Source Address:" << setw(3) << (int)header.srcAddr0 << "." << (int)header.srcAddr1 << "." << (int)header.srcAddr2
<< endl;
cout << "Dest Address:" << setw(3) << (int)header.destAddr0 << "." << (int)header.destAddr1 << "." << (int)header.destAddr2
<< endl;
cout << "Data:" << endl;
for (int i = 0; i < idata_size; i++)
{
cout << (char)buffer[i];
}
}
else
{
cerr << "failed to read data "; // If it fails to read the data
return 1;
}
}
else
{
std::cerr << "failed to read header "; // If it fails to read header
return 1;
}
cout << ("Read in next packets? Y/N "); // Is read has to be done in next packets
ch = getchar();
cout << " " << endl;
} while (ch == 'Y' || ch == 'y'); //while statement
<< endl;
system("pause");
}