1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
struct s_header {
unsigned char verCode;
unsigned char serviceType;
unsigned short totLength;
unsigned char reserved;
unsigned char flags;
unsigned short hopCount;
unsigned char srcAddr0;
unsigned char srcAddr1;
unsigned char srcAddr2;
unsigned char srcAddr3;
unsigned char destAddr0;
unsigned char destAddr1;
unsigned char destAddr2;
unsigned char destAddr3;
} header;
// string tables
char * codes[] = {
"OK",
"Possible data corruption"
};
char * services[] = {
"Normal",
"Low Priority",
"High Priority",
"Medium Priority",
"So-so Priority",
"Needed Yesterday",
"Nevermind"
};
char * flags[] = {
"Grumpy",
"Sneezy",
"Bashful",
"Doc",
"Dopey",
"Tipsy",
"Surly",
"Snow White"
};
int main(int argc, char * argv) {
int bytesRead, idataSize, x;
char buffer[800], user_answer;
FILE * fp = 0;
fp = fopen("PACKETS.txt", "r");
if (fp == 0)
{
cerr << "Error: Could not open file" << endl;
exit(1);
}
// read in header
bytesRead = fread(&header, 1, 16, fp);
// read and display data portion of packet
idataSize = header.totLength - 16; // calculate data size
fread(buffer, 1, idataSize, fp); // read data into buffer
// display data from header
cout << "Version:" << setw(9) << (header.verCode >> 4) << endl;
cout << "Code:" << setw(12) << (header.verCode & 0x0F) << " - " << codes[(header.verCode & 0x0f)] << endl;
cout << "Total Length:" << setw(5) << header.totLength << endl;
cout << "Service Type:" << setw(4) << (int)header.serviceType << " - " << services[header.serviceType] << endl;
//display flags using a mask and loop
x = header.flags;
for (int i = 0; i < 8; i++)
{
cout << " " << flags[i] << ":\t" << (x & 1) << endl;
x = x >> 1;
}
cout << "Hop Count:" << setw(8) << header.hopCount << endl;
cout << "Source Address:" << setw(4) << (int)header.srcAddr0 << "." << (int)header.srcAddr1 << "." << (int)header.srcAddr2
<< "." << (int)header.srcAddr3 << endl;
cout << "Dest Address:" << setw(4) << (int)header.destAddr0 << "." << (int)header.destAddr1 << "." << (int)header.destAddr2
<< "." << (int)header.destAddr3 << endl;
cout << "Data:" << endl;
for (int i = 0; i < idataSize; i++)
{
cout << (char)buffer[i];
}
cout << endl;
system("pause");
}
|