Hello! I am making a program to parse wire-shark dump files. This is my first foray into C, but I am fairly experience with C++. I am having some trouble with my character arrays.
I am wanting to use two character arrays, one called "line" used to grab lines of text form the file, and the other "chunk" is a collection of these lines of text that is separated by the presences of the word "No." and will be passed to a function that a friend of mine is working on.
I have so far tested my program on two dump files, and I have been testing my parsing by using printf to print the "chunk" to the screen before I call my friends function. When I test the file "dump1" get this output:
Code:
No. Time Source Destination Protocol Info
��������� 65 16.033438 192.168.1.108 208.111.158.52 UDP Source port: 50883 Destination port: 27017
Frame 65: 126 bytes on wire (1008 bits), 126 bytes captured (1008 bits)
Ethernet II, Src: HonHaiPr_d4:68:5a (0c:ee:e6:d4:68:5a), Dst: Netgear_a4:e2:38 (c4:3d:c7:a4:e2:38)
Internet Protocol, Src: 192.168.1.108 (192.168.1.108), Dst: 208.111.158.52 (208.111.158.52)
User Datagram Protocol, Src Port: 50883 (50883), Dst Port: 27017 (27017)
Data (84 bytes)
0000 56 53 30 31 30 00 07 04 00 16 00 00 00 3d d2 41 VS010........=.A
0010 00 00 00 00 c8 00 00 00 01 00 00 00 00 00 00 00 ................
0020 30 00 00 00 cd e9 61 4f b7 19 0c de 06 47 88 ad 0.....aO.....G..
0030 4a 5a 4a 74 74 19 a2 06 48 58 1c 49 24 7a 3e 22 JZJtt...HX.I$z>"
0040 56 d6 76 b1 55 23 30 47 cf 6c 9f d6 f7 d3 3f bf V.v.U#0G.l....?.
0050 6c 3e 11 e1 l>..
Complete with the garbled characters on each frame I print out.
Even worse, when I try and load in "dump2" I get an error about "double free", which leads me to believe that cause for my error is the way that I am attempting to reuse my character arrays. My code with memory calls is shown below:
Code:
char * chunk;/*chunk of data to be passed to represent one packet */
char * line; /*designed to hold a line of input from dump file */
..............................
chunk = malloc(2500);/*2500 bytes is an oversized char* on purpose*/
line = malloc(250);/*unsure of how many chars per line so so large again*/
..........................
while(fgets(line,250,dump) != NULL){ /*reads file for generating array */
if(strstr(line,"No.") != NULL && first != 1){/*Controls reading into chunk by ID 'No.' in dump */
strcat(chunk,line);
printf("%s\n",chunk);
packetPointerArray[count] = newPacket(chunk);
free(line);
free(chunk);
chunk = malloc(2500);
line = malloc(250);
}
strcat(chunk,line);
free(line);
line = malloc(250);
first = 0;/*not the first line of text */
}
So the errors that I am trying to resolve, which I feel are related, deal with the garbled characters, and the double free error upon opening dump2. Would anyone have any advice about how to deal with these? Perhaps some advice on reusing char*'s?
Thanks so much guys!