suma_ds
asked on
netinet/tcp.h -- tcphdr structure compile error
Hi experts.
I've written a program to send tcp/ip packets using raw sockets on unix. I've got rid of all the compile errors, except for one remaining problem.
First of all, here are the libraries I've included:
# include <stdio.h>
# include <string.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
My code just basically creates one big buffer and points "ip" and "tcphdr" structures within this buffer. I then fill out all the values and attempt to fire off the packet.
All my remaining compile errors relate to tcphdr... I've looked at /usr/include/tcp.h and cannot make any sense of this at all. The tcphdr structure DOES have the members it is complaining about and they are spelt correctly!
net.c: In function ‘SEND’:
net.c:41: error: ‘struct tcphdr’ has no member named ‘th_sport’
net.c:42: error: ‘struct tcphdr’ has no member named ‘th_dport’
net.c:43: error: ‘struct tcphdr’ has no member named ‘th_seq’
net.c:44: error: ‘struct tcphdr’ has no member named ‘th_ack’
net.c:45: error: ‘struct tcphdr’ has no member named ‘th_x2’
net.c:46: error: ‘struct tcphdr’ has no member named ‘th_off’
net.c:47: error: ‘struct tcphdr’ has no member named ‘th_flags’
net.c:47: error: ‘TH_SYN’ undeclared (first use in this function)
net.c:47: error: (Each undeclared identifier is reported only once
net.c:47: error: for each function it appears in.)
net.c:48: error: ‘struct tcphdr’ has no member named ‘th_win’
net.c:49: error: ‘struct tcphdr’ has no member named ‘th_sum’
net.c:50: error: ‘struct tcphdr’ has no member named ‘th_urp’
I created an instance of the tcphdr structure like this:
struct tcphdr * tcph = ( struct tcphdr * ) datagram + sizeof ( struct ip ) ;
And I was setting the members like follows:
tcph->th_dport = htons ( port ) ;
Suggestions?
I've written a program to send tcp/ip packets using raw sockets on unix. I've got rid of all the compile errors, except for one remaining problem.
First of all, here are the libraries I've included:
# include <stdio.h>
# include <string.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
My code just basically creates one big buffer and points "ip" and "tcphdr" structures within this buffer. I then fill out all the values and attempt to fire off the packet.
All my remaining compile errors relate to tcphdr... I've looked at /usr/include/tcp.h and cannot make any sense of this at all. The tcphdr structure DOES have the members it is complaining about and they are spelt correctly!
net.c: In function ‘SEND’:
net.c:41: error: ‘struct tcphdr’ has no member named ‘th_sport’
net.c:42: error: ‘struct tcphdr’ has no member named ‘th_dport’
net.c:43: error: ‘struct tcphdr’ has no member named ‘th_seq’
net.c:44: error: ‘struct tcphdr’ has no member named ‘th_ack’
net.c:45: error: ‘struct tcphdr’ has no member named ‘th_x2’
net.c:46: error: ‘struct tcphdr’ has no member named ‘th_off’
net.c:47: error: ‘struct tcphdr’ has no member named ‘th_flags’
net.c:47: error: ‘TH_SYN’ undeclared (first use in this function)
net.c:47: error: (Each undeclared identifier is reported only once
net.c:47: error: for each function it appears in.)
net.c:48: error: ‘struct tcphdr’ has no member named ‘th_win’
net.c:49: error: ‘struct tcphdr’ has no member named ‘th_sum’
net.c:50: error: ‘struct tcphdr’ has no member named ‘th_urp’
I created an instance of the tcphdr structure like this:
struct tcphdr * tcph = ( struct tcphdr * ) datagram + sizeof ( struct ip ) ;
And I was setting the members like follows:
tcph->th_dport = htons ( port ) ;
Suggestions?
Sound like you may be including another definition of tcphdr which prevents the one in netinet/tcp.h being seen. Check the -I dirs to make sure you're grabbing the right version.
ASKER
hey thanks for the responce.
i'm new to unix and as such, not quite sure how to do what you're asking. is that a bash command or something to do with gcc?
cheers,
suma
i'm new to unix and as such, not quite sure how to do what you're asking. is that a bash command or something to do with gcc?
cheers,
suma
It's to do with gcc. What is your compile command-line?
BTW, what operating system are you doing this on ? Windows? Linux? Some other Unix?
BTW, what operating system are you doing this on ? Windows? Linux? Some other Unix?
ASKER
I'm just using "gcc net.c -o net.exe" to compile it.
I tried "gcc -net.c -I dirs -o net.exe" but that didnt seem to do anything different.
I'm running Linux...
I tried "gcc -net.c -I dirs -o net.exe" but that didnt seem to do anything different.
I'm running Linux...
ASKER
I also tried it on a completely different box -- running a different distro of linux. Same problem.
change your compile line to:
gcc -D__FAVOR_BSD net.c -o net
Just wondering - why call the executable "net.exe" since .exe is Windows only?
gcc -D__FAVOR_BSD net.c -o net
Just wondering - why call the executable "net.exe" since .exe is Windows only?
ASKER
no joy... i'm still getting the same tcphdr errors.
as for the exe thing, i've just recently migrated from windows.. and although i really do prefer linux, life without the win32 api is a pretty big change.... so i guess i just call it exe to make it feel more like home :)
btw if it would help at all i could post the complete code of net.c... let me know.
regards,
suma
as for the exe thing, i've just recently migrated from windows.. and although i really do prefer linux, life without the win32 api is a pretty big change.... so i guess i just call it exe to make it feel more like home :)
btw if it would help at all i could post the complete code of net.c... let me know.
regards,
suma
OK - if you posted the code, it might help. This machine has Windows, but has cygwin, so I should be able to approximate the linux environment.
Just checked ...
If I use gcc 4.0.2 something similar to your code compiles fine, but gcc 3.4.6 fails with the same error you're getting. I looked at the code, and the include files are quite different, although the comments indicate that they're both BSD v8.1. I think it has got to be some -D flag to get it to do the right thing.
I also have another toolchain installed for my GP2X, and that's got the naughty version too, even though this is 4.0.2 too. Hmmmmm! I'll continue to hack ....
If I use gcc 4.0.2 something similar to your code compiles fine, but gcc 3.4.6 fails with the same error you're getting. I looked at the code, and the include files are quite different, although the comments indicate that they're both BSD v8.1. I think it has got to be some -D flag to get it to do the right thing.
I also have another toolchain installed for my GP2X, and that's got the naughty version too, even though this is 4.0.2 too. Hmmmmm! I'll continue to hack ....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sweet! it works no problem.
many thanks.
many thanks.