Can't compile simple program with tcp.h because of error from endianess?

4.4k views Asked by At

Help me compile this simple C program using tcp.h? Getting "mitmc.c:82: error: struct tcphdr has no member named th_off" because the byte order is not set with "# ifdef __FAVOR_BSD"

This is prolly a simple problem for some of you. I'm trying to compile this program, a simple man in the middle utility from a book. Compiling with gcc on linux (because Windows didnt have the posix library).

First I got this error:

mitmc.c: In function âcorrupt_ip_pktâ:
mitmc.c:82: error: âstruct tcphdrâ has no member named âth_offâ
mitmc.c:109: error: âstruct udphdrâ has no member named âuh_ulenâ

and it looks like this is because in "/usr/include/netinet/tcp.h" th_off is only defined if # ifdef __FAVOR_BSD

But when I define it by adding #define _BSD_SOURCE (because it is set in features.h http://www.linuxquestions.org/questions/programming-9/tcp-hdr-variables-388003/), then I get a whole other set of errors:

$ gcc mitmc.c
/tmp/ccuzRcNp.o: In function `corrupt_ip_pkt':
mitmc.c:(.text+0x15f): undefined reference to `libnet_do_checksum'
mitmc.c:(.text+0x174): undefined reference to `libnet_geterror'
mitmc.c:(.text+0x231): undefined reference to `libnet_do_checksum'
mitmc.c:(.text+0x242): undefined reference to `libnet_geterror'
mitmc.c:(.text+0x296): undefined reference to `libnet_do_checksum'
mitmc.c:(.text+0x2a7): undefined reference to `libnet_geterror'
/tmp/ccuzRcNp.o: In function `pkt_handler':
mitmc.c:(.text+0x346): undefined reference to `libnet_write_raw_ipv4'
mitmc.c:(.text+0x357): undefined reference to `libnet_geterror'
/tmp/ccuzRcNp.o: In function `main':
mitmc.c:(.text+0x466): undefined reference to `libnet_init'
mitmc.c:(.text+0x4ae): undefined reference to `libnet_get_hwaddr'
mitmc.c:(.text+0x4c6): undefined reference to `libnet_geterror'
mitmc.c:(.text+0x590): undefined reference to `strlcat'
mitmc.c:(.text+0x5c0): undefined reference to `strlcat'
mitmc.c:(.text+0x5df): undefined reference to `pcap_lookupdev'
mitmc.c:(.text+0x63c): undefined reference to `pcap_lookupnet'
mitmc.c:(.text+0x697): undefined reference to `pcap_open_live'
mitmc.c:(.text+0x6fc): undefined reference to `pcap_compile'
mitmc.c:(.text+0x715): undefined reference to `pcap_perror'
mitmc.c:(.text+0x736): undefined reference to `pcap_setfilter'
mitmc.c:(.text+0x74f): undefined reference to `pcap_perror'
mitmc.c:(.text+0x767): undefined reference to `pcap_freecode'
mitmc.c:(.text+0x7ad): undefined reference to `pcap_loop'
mitmc.c:(.text+0x7cd): undefined reference to `pcap_perror'
mitmc.c:(.text+0x7e6): undefined reference to `pcap_close'
mitmc.c:(.text+0x7f3): undefined reference to `libnet_destroy'
collect2: ld returned 1 exit status

Thanks for any help.

Plus, BTW, why do my compiler errors sometimes have â instead of quotes? Thank you, using LANG=C in bash fixed the quotes.

Repsonding here to answer so I can use formatting. Excellent! I had no idea that libnet-config is a binary you can use on the command line for settings. So for other noobs,

$ libnet-config  --defines
-D_BSD_SOURCE -D__BSD_SOURCE -D__FAVOR_BSD -DHAVE_NET_ETHERNET_H
$ libnet-config --libs
-lnet

so you escape that so it runs as a command

$ gcc -Wall `libnet-config --defines` mitmc.c -o mitmc `libnet-config --libs` -lpcap
mitmc.c: In function 'main':
mitmc.c:232: warning: implicit declaration of function 'strlcat'
/tmp/ccSDMkcZ.o: In function `main':
mitmc.c:(.text+0x590): undefined reference to `strlcat'
mitmc.c:(.text+0x5c0): undefined reference to `strlcat'
collect2: ld returned 1 exit status

Thanks for the help, that is the right answer. Now it appears the code was written for openbsd(see wikipedia Strlcpy). I guess i'll just replace the strlcat with strncpy and hope for the best. SUCCESS, it compiled! Oh FFS,

$ sudo ./mitmc
pcap_compile: syntax error

Ah that is just my usage.

Thank you Vlad, thank you Jeremiah!

1

There are 1 answers

2
AudioBubble On

You have to specify specific flags to your compiler/linker in order to use libnet. In your case, you see linker errors because it cannot find definitions for functions you are using in your program. Obviously, this is because you need to link with libnet and libpcap. To get it right, try something like:

gcc -Wall `libnet-config --defines` mitmc.c -o mitmc `libnet-config --libs` -lpcap