V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lslqtz
V2EX  ›  C

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

  •  
  •   lslqtz · 2016-11-24 07:59:45 +08:00 · 2052 次点击
    这是一个创建于 2703 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前没碰过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;
    }
    
    第 1 条附言  ·  2016-11-24 09:11:45 +08:00
    libpcap 用的是 winpcap ,用 gcc 没法搞定 libnet 。。
    第 2 条附言  ·  2016-11-25 06:07:43 +08:00
    刚刚传完程序的工程文件和库, h 文件,需要 WinPcap...
    https://osu.acgvideo.cn/1.rar
    11 条回复    2016-11-26 12:24:54 +08:00
    lcdtyph
        1
    lcdtyph  
       2016-11-24 09:34:15 +08:00 via Android
    你的 libpcap 似乎是静态链接。。。
    把 libnet.dll 放在工程目录里试试?
    lslqtz
        2
    lslqtz  
    OP
       2016-11-24 10:03:13 +08:00
    @lcdtyph 我是直接把 libnet.dll 放在程序目录下运行的,否则找不到。。
    libpcap 是打了驱动后直接丢进文件的,但看起来似乎正常工作。
    lslqtz
        3
    lslqtz  
    OP
       2016-11-24 10:22:49 +08:00 via iPhone
    我觉得我一会应该把工程文件和 exe 上传。。
    enenaaa
        4
    enenaaa  
       2016-11-24 11:42:54 +08:00
    自个编译 libnet , 生成 pdb 文件, 这样就可以看到崩溃的具体位置。
    enenaaa
        5
    enenaaa  
       2016-11-24 11:45:18 +08:00   ❤️ 1
    看了下 libnet 最后更新是 03 年了, 也可能是系统或 pcap 不兼容所致。
    lslqtz
        6
    lslqtz  
    OP
       2016-11-24 12:00:10 +08:00
    @enenaaa 我就是用 libnet 的源码包中的 bat 编出的 dll 和 lib 文件。。
    lslqtz
        7
    lslqtz  
    OP
       2016-11-24 12:04:25 +08:00
    @enenaaa 因为是在 libnet_t *libnet_handler = start_libnet(dev); 出错,所以我感觉兼容问题应该不大,系统的话不太清楚。。我试着在 Win7 下运行,提示的是 0xc0000007b 。
    enenaaa
        8
    enenaaa  
       2016-11-24 12:11:35 +08:00
    0xc000007b 的话, 可能是兼容性错误。 检查一下是不是都是 32 位程序。
    lslqtz
        9
    lslqtz  
    OP
       2016-11-24 12:23:27 +08:00
    @enenaaa 程序确定是 32 位的,库的话下的东西基本都是 32 位的,除了 vs ,我要不要开个 64 的再去看看。。
    lslqtz
        10
    lslqtz  
    OP
       2016-11-25 06:07:51 +08:00
    @enenaaa 刚刚传完程序的工程文件和库, h 文件,需要 WinPcap...
    https://osu.acgvideo.cn/1.rar
    lslqtz
        11
    lslqtz  
    OP
       2016-11-26 12:24:54 +08:00
    @enenaaa 抱歉,我发现我忘了绑域名。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1461 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:18 · PVG 01:18 · LAX 10:18 · JFK 13:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.