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