由于字节顺序错误,无法使用 tcp.h 编译简单程序?

发布于 2024-10-19 15:10:22 字数 3593 浏览 9 评论 0原文

帮我用 tcp.h 编译这个简单的 C 程序?得到“mitmc.c:82: error: struct tcphdr has no member named th_off”,因为字节顺序不是用“# ifdef __FAVOR_BSD”设置的,

这对你们中的一些人来说可能是一个简单的问题。我正在尝试编译 this 程序,这是书中的一个简单的中间人实用程序。在linux上用gcc编译(因为Windows没有posix库)。

首先我得到这个错误:

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â

看起来这是因为在“/usr/include/netinet/tcp.h”中 th_off 仅在以下情况下定义 # ifdef __FAVOR_BSD

但是当我通过添加来定义它时 #定义_BSD_SOURCE (因为它是在 features.h http://www .linuxquestions.org/questions/programming-9/tcp-hdr-variables-388003/),然后我得到了一系列其他错误:

$ 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

感谢您的帮助。

另外,顺便说一句,为什么我的编译器错误有时会用 â 而不是引号? 谢谢,在 bash 中使用 LANG=C 修复了引号。

在这里回复以便我可以使用格式。 出色的!我不知道 libnet-config 是一个可以在命令行上使用进行设置的二进制文件。因此,对于其他菜鸟来说,

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

您可以逃避它,以便它作为命令运行

$ 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

感谢您的帮助,这是正确的答案。现在看来代码是为 openbsd 编写的(参见 wikipedia Strlcpy)。我想我会用 strncpy 替换 strlcat 并希望得到最好的结果。成功,编译成功!哦,FFS,

$ sudo ./mitmc
pcap_compile: syntax error

啊,这只是我的用法。

谢谢弗拉德,谢谢耶利米!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-10-26 15:10:22

您必须为编译器/链接器指定特定标志才能使用libnet。在您的情况下,您会看到链接器错误,因为它找不到您在程序中使用的函数的定义。显然,这是因为您需要链接 libnetlibpcap。为了得到正确的结果,请尝试以下操作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文