在 Windows 下编译程序, libnet 出错没看懂调试。。

2016-11-24 07:59:45 +08:00
 lslqtz

之前没碰过c,没看懂调试。。,显示如下:

libnet.dll!5fa3ad01() 未知 [下面的框架可能不正确和 /或缺失,没有为 libnet.dll 加载符号] [外部代码] Packet.dll!012647b9() 未知 libnet.dll!5fa3a0ff() 未知 ConsoleApplication3.exe!main(int argc, char * * argv) 行 147 C++ [外部代码]

编译的程序是 net_speeder ,替换了网上找到的多个 libnet.dll 不管用, libnet 版本是 1.2-rc3 。

在程序中的反应是走到 libnet_t *libnet_handler = start_libnet(dev);后出现停止响应,用的是 VS2015 , C++。

代码如下,WinPcap没问题。:

#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <libnet.h>
#pragma comment(lib,"libnet.lib")
#pragma comment(lib,"wsock32.lib")

/* default snap length (maximum bytes per packet to capture) */
#define SNAP_LEN 65535

#ifdef COOKED
#define ETHERNET_H_LEN 16
#else
#define ETHERNET_H_LEN 14
#endif

#define SPECIAL_TTL 88

void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
void print_usage(void);


/*
* print help text
*/
void print_usage(void) {
	printf("Usage: %s [interface][\"filter rule\"]\n", "net_speeder");
	printf("\n");
	printf("Options:\n");
	printf("    interface    Listen on <interface> for packets.\n");
	printf("    filter       Rules to filter packets.\n");
	printf("\n");
}

void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
	static int count = 1;
	struct libnet_ipv4_hdr *ip;

	libnet_t *libnet_handler = (libnet_t *)args;
	count++;

	ip = (struct libnet_ipv4_hdr*)(packet + ETHERNET_H_LEN);

	if (ip->ip_ttl != SPECIAL_TTL) {
		ip->ip_ttl = SPECIAL_TTL;
		int len_written = libnet_adv_write_raw_ipv4(libnet_handler, (u_int8_t *)ip, ntohs(ip->ip_len));
		if (len_written < 0) {
			printf("packet len:[%d] actual write:[%d]\n", ntohs(ip->ip_len), len_written);
			printf("err msg:[%s]\n", libnet_geterror(libnet_handler));
		}
	}
	else {
		//The packet net_speeder sent. nothing todo
	}
	return;
}

libnet_t* start_libnet(char *dev) {
	char errbuf[LIBNET_ERRBUF_SIZE];
	libnet_t *libnet_handler = libnet_init(LIBNET_RAW4_ADV, dev, errbuf);

	if (NULL == libnet_handler) {
		printf("libnet_init: error %s\n", errbuf);
	}
	return libnet_handler;
}

#define ARGC_NUM 3
int main(int argc, char **argv) {
	char *dev = NULL;
	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_t *handle;

	char *filter_rule = NULL;
	struct bpf_program fp;
	bpf_u_int32 net, mask;

	if (argc == ARGC_NUM) {
		dev = argv[1];
		filter_rule = argv[2];
		printf("Device: %s\n", dev);
		printf("Filter rule: %s\n", filter_rule);
	}
	else {
		print_usage();
		return -1;
	}

	printf("ethernet header len:[%d](14:normal, 16:cooked)\n", ETHERNET_H_LEN);

	if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
		printf("Couldn't get netmask for device %s: %s\n", dev, errbuf);
		net = 0;
		mask = 0;
	}

	printf("init pcap\n");
	handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
	if (handle == NULL) {
		printf("pcap_open_live dev:[%s] err:[%s]\n", dev, errbuf);
		printf("init pcap failed\n");
		return -1;
	}

	printf("init libnet\n");
	libnet_t *libnet_handler = start_libnet(dev);
	if (NULL == libnet_handler) {
		printf("init libnet failed\n");
		return -1;
	}

	if (pcap_compile(handle, &fp, filter_rule, 0, net) == -1) {
		printf("filter rule err:[%s][%s]\n", filter_rule, pcap_geterr(handle));
		return -1;
	}

	if (pcap_setfilter(handle, &fp) == -1) {
		printf("set filter failed:[%s][%s]\n", filter_rule, pcap_geterr(handle));
		return -1;
	}

	while (1) {
		pcap_loop(handle, 1, got_packet, (u_char *)libnet_handler);
	}

	/* cleanup */
	pcap_freecode(&fp);
	pcap_close(handle);
	libnet_destroy(libnet_handler);
	return 0;
}
2055 次点击
所在节点    C
11 条回复
lcdtyph
2016-11-24 09:34:15 +08:00
你的 libpcap 似乎是静态链接。。。
把 libnet.dll 放在工程目录里试试?
lslqtz
2016-11-24 10:03:13 +08:00
@lcdtyph 我是直接把 libnet.dll 放在程序目录下运行的,否则找不到。。
libpcap 是打了驱动后直接丢进文件的,但看起来似乎正常工作。
lslqtz
2016-11-24 10:22:49 +08:00
我觉得我一会应该把工程文件和 exe 上传。。
enenaaa
2016-11-24 11:42:54 +08:00
自个编译 libnet , 生成 pdb 文件, 这样就可以看到崩溃的具体位置。
enenaaa
2016-11-24 11:45:18 +08:00
看了下 libnet 最后更新是 03 年了, 也可能是系统或 pcap 不兼容所致。
lslqtz
2016-11-24 12:00:10 +08:00
@enenaaa 我就是用 libnet 的源码包中的 bat 编出的 dll 和 lib 文件。。
lslqtz
2016-11-24 12:04:25 +08:00
@enenaaa 因为是在 libnet_t *libnet_handler = start_libnet(dev); 出错,所以我感觉兼容问题应该不大,系统的话不太清楚。。我试着在 Win7 下运行,提示的是 0xc0000007b 。
enenaaa
2016-11-24 12:11:35 +08:00
0xc000007b 的话, 可能是兼容性错误。 检查一下是不是都是 32 位程序。
lslqtz
2016-11-24 12:23:27 +08:00
@enenaaa 程序确定是 32 位的,库的话下的东西基本都是 32 位的,除了 vs ,我要不要开个 64 的再去看看。。
lslqtz
2016-11-25 06:07:51 +08:00
@enenaaa 刚刚传完程序的工程文件和库, h 文件,需要 WinPcap...
https://osu.acgvideo.cn/1.rar
lslqtz
2016-11-26 12:24:54 +08:00
@enenaaa 抱歉,我发现我忘了绑域名。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/322844

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX