之前没碰过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;
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.