libnet windows下编译,出错与解决办法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Hot_VC/article/details/51101126

一、环境说明

1.编译器:VS2013

2.系统WIN7

二、编译

1.在libnet的源码目录下新建一个lib的静态库工程如下图


2.将libnet的源码添加到工程中,参考工程中的msvcbuild.bat中的内容移除不必要的源码文件。


3.此时编译还会出现如下错误

1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(204): error C2275: “uint8_t”: 将此类型用作表达式非法
1>          e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\include\libnet\stdint.h(85) : 参见“uint8_t”的声明
1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(204): error C2146: 语法错误: 缺少“;”(在标识符“ip_nh”的前面)
1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(204): error C2065: “ip_nh”: 未声明的标识符
1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(217): error C2065: “ip_nh”: 未声明的标识符
1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(220): error C2065: “ip_nh”: 未声明的标识符
1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(239): error C2065: “ip_nh”: 未声明的标识符
1>e:\work_proc\shy_redirect\deps\libnet-libnet-1.2-rc3\libnet\src\libnet_checksum.c(244): error C2065: “ip_nh”: 未声明的标识符

解决办法为:

找到ip_nh变量的定义。代码如下:

ip6h_p = (struct libnet_ipv6_hdr *)iph_p;
iph_p = NULL;
ip_hl   = 40;
uint8_t ip_nh = ip6h_p->ip_nh;

修改为:

uint8_t ip_nh;
ip6h_p = (struct libnet_ipv6_hdr *)iph_p;
iph_p = NULL;
ip_hl   = 40;
ip_nh = ip6h_p->ip_nh;


这个错误是由于c的编译器要求将变量的申明放在一个函数块的头部,而c++没有这样的要求造成的。所以我们需要修改,将ip_nh的声明提到函数块的头部。







展开阅读全文

Linux 下编译C出错(windows编译正常)

01-25

Ubuntu 15rngcc version 5.2.1rn错误:rn![图片说明](https://img-ask.csdn.net/upload/201601/25/1453733873_357191.png)rnrnscritpt.hpp下是一个 命名空间rnnamespace scriptrn ....rn int setData( PyObject * pObj, Vector4 & rVal, const char * varName = "" );rn int setData( PyObject * pObj, Matrix & rVal, const char * varName = "" );rn int setData( PyObject * pObj, PyObject * & rVal,rn const char * varName = "" );rn int setData( PyObject * pObj, SmartPointer & rPyObject,rn const char * varName = "" );rn int setData( PyObject * pObj, Capabilities & rCaps,rn const char * varName = "" );rn int setData( PyObject * pObj, std::string & rString,rn const char * varName = "" );rn int setData( PyObject * pObj, std::wstring & rString,rn const char * varName = "" );rn int setData( PyObject * pObj, char * & rString,rn const char * varName = "" );rn int setData( PyObject * pObj, Mercury::Address & rAddr,rn const char * varName = "" );rn rn template rn PyObject * getReadOnlyData( const T & d )rn rn return getData( d ); //出问题的地方126行rn rnrn PyObject * getData( const bool data );rn PyObject * getData( const int data );rn PyObject * getData( const uint data );rn ....rnrnrnstl_to_py.cpprnPyObject * Script::getData( const PySTLSequenceHolderBase & holder ) //出错位置rnrn return new PySTLSequence(rn const_cast(holder), holder.pOwner() );rnrnrnstl_to_py.hpp 中rnnamespace Scriptrnrn int setData( PyObject * pObject, PySTLSequenceHolderBase & data,rn const char * varName = "" );rnrn PyObject * getData( const PySTLSequenceHolderBase & data );rn;rnrn不知道哪里出错了,求大神指点rnrnrn是有定义了的rn定义在st_to_py.hpp中rnnote:rnstl_to_py.cpp:506 'PyObjest * Script::getData(const PySTLSequenceHoldBase &) ' declared here ,later in the translation unitrnPyObjest * Script::getData(const PySTLSequenceHoldBase & holder)rnrnst_to_py.hpp 是 include了 script.hpprnrn这个在window环境下是能编译通过的rn 问答

libnet程序运行出错:no network interface found

04-16

这个基于libnet的程序,实现一个构造ARP包并发送的功能rn同样一个程序,我的电脑上(WINDOWS XP系统)运行没问题,正常构造数据包正常发送rn但是在我同学的电脑(也是XP 系统)就出问题了,问题出在libnet_init这个函数上,出错信息是libnet_select_device():no network interface foundrnrn有人知道怎么回事么?先谢谢啦rnrnrn原程序如下:rn #if (HAVE_CONFIG_H) rn #include "../include/config.h" rn #endif rn #include "../sample/libnet_test.h" rn #ifdef __WIN32__ rn #include "../include/win32/getopt.h" rn #endif rn #include rn#pragma comment(lib, "libnet.lib")rnint rn main(int argc, char *argv[]) rn rn int c; rn libnet_t *l; rn libnet_ptag_t t; rn char *payload; rn u_short payload_s; rn u_long src_ip, dst_ip; /*源IP的地址和目的IP地址*/ rn u_short src_prt, dst_prt; /*源端口号和目的端口号*/ rn char errbuf[LIBNET_ERRBUF_SIZE]; rn char szSrc[255],szDes[255]; rn rn int i=0; rn rn printf("libnet 1.1 packet shaping: TCP + options[link]\n"); rn rn l = libnet_init( rn LIBNET_LINK, /*Libnet类型 */ rn NULL, /* 网络设置接口 */ rn errbuf); /* 错误信息 */ rn rn if (l == NULL) rn rn fprintf(stderr, "libnet_init() failed: %s", errbuf); rn system("pause");rn exit(EXIT_FAILURE); rn rn rn src_ip = 0; rn dst_ip = 0; rn src_prt = 0; rn dst_prt = 0; rn payload = NULL; /*负载*/ rn payload_s = 0; /*负载大小*/ rn strcpy(szSrc,"127.0.0.1"); rn strcpy(szDes,"www.fighterforum.com"); rn src_ip=libnet_name2addr4(l,szSrc,LIBNET_RESOLVE); rn dst_ip=libnet_name2addr4(l,szDes,LIBNET_RESOLVE); rn payload="33333333"; rn payload_s = strlen(payload); rn dst_prt=80; rn src_prt=99; rn rn /*构造TCP选项*/ rn t = libnet_build_tcp_options( rn (unsigned char *)"\003\003\012\001\002\004\001\011\010\012\077\077\077\077\000\000\000\000\000\000", 20, rn l, rn 0); rn if (t == -1) rn rn fprintf(stderr, "Can't build TCP options: %s\n", libnet_geterror(l)); rn goto bad; rn rn /*构造TCP协议*/ rn t = libnet_build_tcp( rn src_prt, /* 源端口号 */ rndst_prt, /* 目的端口号 */ rn0x01010101, /* 序列号 */ rn0x02020202, /* 确认序列号 */ rn TH_SYN, /* 控制标记 */ rn 32767, /* 窗口大小 */ rn 0, /* 校验和s */ rn 10, /* 紧急指针 */ rn LIBNET_TCP_H + 20 + payload_s, /* 数据包大小 */ rn (unsigned char *)payload, /* 负载 */ rn payload_s, /* 负载大小 */ rn l, /* libnet 句柄 */ rn 0); /* libnet 协议ID */ rn if (t == -1) rn rn fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l)); rn goto bad; rn rn /*构造IPV4协议块*/ rn t = libnet_build_ipv4( rn LIBNET_IPV4_H + LIBNET_TCP_H + 20 + payload_s,/* 长度 */ rn 0, /* 服务质量 */ rn 242, /* 标识 */ rn 0, /* 偏移 */ rn 64, /* 生存时间 */ rn IPPROTO_TCP, /* 上层协议类型 */ 0, /* 校验和 */ rn src_ip, /* 源IP地址 */ rndst_ip, /* 目的IP地址 */ rnNULL, /* 负载 */ rn0, /* 负载大小 */ l, /* libnet 句柄 */ rn0); /* libnet 协议ID */ if (t == -1) rn rn fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l)); rn goto bad; rn rn /*构造以太网协议块*/ rn t = libnet_build_ethernet(enet_dst,enet_src, ETHERTYPE_IP, /* 上层协议类型*/NULL, /* 负载 */0, /* 负载大小 */ rnl, /* libnet 句柄 */ rn 0); /* libnet 协议ID */ rn if (t == -1) rn rn fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l)); rn goto bad; rn rn rn /*打印IP信息*/ rn printf("源地址IP:%d,目的IP adress:%d\n",src_ip,dst_ip); rn printf("解码后的源地址IP:%s,目的IP adress:%s\n",libnet_addr2name4(src_ip,1),libnet_addr2name4(dst_ip,1)); rnrn c = libnet_write(l); /*发送数据包*/ rn if (c == -1) rn rn fprintf(stderr, "Write error: %s\n", libnet_geterror(l)); rn goto bad; rn rn else rn rn fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c); rn rn libnet_destroy(l); /*销毁Libnet*/ rn rn getch(); rn return (EXIT_SUCCESS); rn bad: rn libnet_destroy(l); rn printf("出错了你知道不?妈妈的~\n"); rn rn getch(); rn return (EXIT_FAILURE); rn rn rn void rn usage(char *name) rn rn fprintf(stderr, rn "usage: %s -s source_ip.source_port -d destination_ip.destination_port" rn " [-p payload]\n", rn name); rn rn rn #if defined(__WIN32__) rn #include <../include/win32/getopt.h> rn #include rn #include rn #endif /* __WIN32__ */ rn 论坛

没有更多推荐了,返回首页