|
Home > Archive > Unix Programming > April 2005 > IP packet problem..segfault when trying to read data
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
IP packet problem..segfault when trying to read data
|
|
| wetherbean 2005-04-14, 7:51 am |
| Hello group..I am writing a network analysis tool that reads packets
from a data file...it only interacts with the file so it should be
pretty simple but I am getting this segfault when I try to read the
data in the packet. I do an initial read for the ip header then read
the UDP or TCP info according to which protocol it is. Then I try to
read the actual data into a buffer and that is when I get my
segfault....I am sure it is a simple mistake but I have tried and
failed to come up with a solution..here is a segment of my code that
contains the problem...any suggestions would be great..thanks in
advance
wetherbean
while ((!feof(fp)))
{
fread(&iphdr,sizeof(iphdr),1,fp);
if(first==0){
iphdr.sec = ntohl(iphdr.sec);
timeinfo=localtime(&iphdr.sec);
printf("Capture starts on %s and %d
microseconds\n",asctime(timeinfo),ntohl(iphdr.msec));
//printf("%d:%d:%d\n",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
first= 1;
count++;
printf("%d\n",count);
}
if(iphdr.ip_prot==17){
fread(&udp_info,sizeof(udp_info),1,fp);
data_len=iphdr.ip_total_length-sizeof(iphdr)-sizeof(udp_info);
fread(buffer,data_len,1,fp);
count++;
printf("%d\n",count);
printf("I made it past prot=17");
}
| |
| Ralf Fassel 2005-04-14, 6:03 pm |
| * "wetherbean" <bjenkin1@gmail.com>
| any suggestions would be great
Post _all_ relevant code, not only parts of it. You need to show the
declarations (and possibly initializations) of all relevant variables
in your code, especially the `buffer' variable. Do you have allocated
enough memory to read into?
R'
| |
| wetherbean 2005-04-14, 6:03 pm |
| sorry..didn't want the post to be too long...here is the complete code
that I have right now..
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
/*struct head{
unsigned long int sec;
unsigned long int msec;
};*/
struct ip {
unsigned long int sec;
unsigned long int msec;
unsigned int ip_length:4; /*little-endian*/
unsigned int ip_version:4;
unsigned char ip_tos;
unsigned short ip_total_length;
unsigned short ip_id;
unsigned short ip_flags;
unsigned char ip_ttl;
unsigned char ip_prot;
unsigned short ip_cksum;
unsigned int ip_source;
unsigned int ip_dest;
};
/* Structure of a TCP header */
struct tcp {
unsigned short tcp_src;
unsigned short tcp_dest;
unsigned int tcp_seqno;
unsigned int tcp_ackno;
unsigned int tcp_res1:4, /*little-endian*/
tcp_hlen:4,
tcp_fin:1,
tcp_syn:1,
tcp_rst:1,
tcp_psh:1,
tcp_ack:1,
tcp_urg:1,
tcp_res2:2;
unsigned short tcp_winsize;
unsigned short tcp_cksum;
unsigned short tcp_urgent;
};
struct udp{
unsigned short int udp_src;
unsigned short int udp_dest;
unsigned short int len;
unsigned short int udp_cksum;
};
int main(int argc,char* argv) {
FILE* fp;
char buffer[2048];
struct ip iphdr;
struct udp udp_info;
struct tcp tcp_info;
struct tm *timeinfo;
int count=0,first=0;
unsigned short data_len=0;
fp = fopen("sniff.data","rb");
while ((!feof(fp)))
{
fread(&iphdr,sizeof(iphdr),1,fp);
if(first==0){
iphdr.sec = ntohl(iphdr.sec);
timeinfo=localtime(&iphdr.sec);
printf("Capture starts on %s and %d
microseconds\n",asctime(timeinfo),ntohl(iphdr.msec));
printf("%d:%d:%d\n",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
first= 1;
count++;
printf("%d\n",count);
}
if(iphdr.ip_prot==17){
fread(&udp_info,sizeof(udp_info),1,fp);
data_len=iphdr.ip_total_length-sizeof(iphdr)-sizeof(udp_info);
fread(buffer,data_len,1,fp);
perror("FREAD");
count++;
printf("%d\n",count);
printf("I made it past prot=17");
}
else if(iphdr.ip_prot==6){
fread(&tcp_info,sizeof(tcp_info),1,fp);
data_len=iphdr.ip_total_length-sizeof(iphdr)-sizeof(tcp_info);
fread(buffer,data_len,1,fp);
count++;
printf("%d\n",count);
}
else{
printf("undefined protocol\n");
exit(1);
}
}
fclose(fp);
}
| |
| Ralf Fassel 2005-04-14, 6:03 pm |
| * "wetherbean" <bjenkin1@gmail.com>
| char buffer[2048];
--<snip-snip>--
| if(iphdr.ip_prot==17){
| fread(&udp_info,sizeof(udp_info),1,fp);
|
| data_len=iphdr.ip_total_length-sizeof(iphdr)-sizeof(udp_info);
|
| fread(buffer,data_len,1,fp);
| perror("FREAD");
Looks like you should be able to run this in a debugger and check the
relevant variables when it crashes. E.g, have you checked that
`data_len' is <= sizeof(buffer) in the fread() line, etc? Also note
that calling `perror' makes only sense when fread() indicates failure.
R'
| |
| wetherbean 2005-04-14, 6:03 pm |
| Yes...it started crashing after i put that fread in there...it was
giving me an illegal seek...I have also tried running it in gnu
debugger but I can;t figure out how to track the variables?? I have
never used a debugger before..looks complicated...thinking about asking
for my money back for this first class education I am supposed to
have...
| |
| Ralf Fassel 2005-04-14, 6:03 pm |
| * "wetherbean" <bjenkin1@gmail.com>
| I have also tried running it in gnu debugger but I can;t figure out
| how to track the variables?? I have never used a debugger
| before..looks complicated...
I'm afraid that if you find using a debugger complicated, `writing a
network analysis tool' might be the Wrong Thing [TM] to try now.
Good luck anyway...
R'
| |
| Fletcher Glenn 2005-04-14, 6:03 pm |
| Out of curiosity, what happens when the data length is > 2048 (the size of
your buffer)?
--
Fletcher Glenn
"wetherbean" <bjenkin1@gmail.com> wrote in message
news:1113489832.666040.19620@g14g2000cwa.googlegroups.com...
> Yes...it started crashing after i put that fread in there...it was
> giving me an illegal seek...I have also tried running it in gnu
> debugger but I can;t figure out how to track the variables?? I have
> never used a debugger before..looks complicated...thinking about asking
> for my money back for this first class education I am supposed to
> have...
>
| |
| wetherbean 2005-04-14, 6:03 pm |
| I obviously need to learn how to use the debugger but I did get through
a web server and a web client without it....just not enough time to
spend to figure it out before I need to have this analysis tool
complete....I would assume if the data length is > than 2048 I would
get a segfault but I don't think that is my problem...none of the
packets I am reading are more than a couple hundred bytes
| |
| David Schwartz 2005-04-14, 8:48 pm |
|
"wetherbean" <bjenkin1@gmail.com> wrote in message
news:1113520617.167442.298040@f14g2000cwb.googlegroups.com...
>I obviously need to learn how to use the debugger but I did get through
> a web server and a web client without it....just not enough time to
> spend to figure it out before I need to have this analysis tool
> complete....I would assume if the data length is > than 2048 I would
> get a segfault but I don't think that is my problem...none of the
> packets I am reading are more than a couple hundred bytes
Compile the program with '-g'. Then when it faults, analyze the core
file like this "gdb <executable_name> <core_name>". When in gdb (or whatever
debugger is on your system), type 'where'. Then you can use the 'up' and
'down' commands to move through the call stack to the level where you can
see the parameters passed to the problem function. Then use 'print
<parameter>' to view them.
DS
|
|
|
|
|