I'm trying to write a C++ program using the WinPcap libraries that will intake a pcap file and scan through each packet to extract IP addresses, port numbers, HTTP information and DNS information. Everything is working very well except for when I try to print out DNS or HTTP packet information.
If your familiar with the WinPcap libraries you should recognize this snippet:
// Use pcap_open_offline. Opens a saved pcap file for reading. Store this data in pointer object pcap_t.
// http://www.winpcap.org/docs/docs_41b5/html/group__wpcapfunc.html#g91078168a13de8848df2b7b83d1f5b69
pcap_t *pcap = pcap_open_offline(file.c_str(), errbuff);
// Create a header object:
// http://www.winpcap.org/docs/docs_40_2/html/structpcap__pkthdr.html
struct pcap_pkthdr *header;
// Create the u_char character array.
const u_char *data;
// Loop through the packets and print information about them.
u_int packetCount = 0;
while (int returnValue = pcap_next_ex(pcap, &header, &data) >= 0)
{
This iterates through each packet and stores the packet header as a struct, and the packet data as a u_char.
This creates a problem. If I try to print an item from the data array say:
printf("DNS Response: ");
for(unsigned int y=76; (y < header->caplen +1); y++)
printf("%c", data[y-1]);
printf("\n")
Position 76 of the data array of a UDP DNS response packet is where the IP address starts. Instead of printing out the IP address I get characters and smiling faces, asterixs, arrows, and other assorted garbage. I believe this is due to stdout reading the value as a character, since it's stored as a u_char, when its not and should be read as an integer or something.
If I use:
printf("%.2x ", data[i-1]);
I can see the data as hex, and the hex values are correct. But I want to see it as 192.168.0.1 or something that looks like a legitimate IP address and is human readable. Anyone have any suggestions as to how I can print this stuff out without seeing all the garbage characters? It's very annoying when every 01 or 02 is printed out as a smiley face so you have www

New Topic/Question
Reply



MultiQuote




|