我正在试验Windows4.1.1库,但我甚至无法编译库中提供的示例源代码。
我发现了这些错误:
'PCAP_OPENFLAG_PROMISCUOUS‘未声明(在此函数中首次使用)
未声明的“PCAP_SRC_IF_STRING”(在此函数中首次使用)
还有一大堆警告:
函数'localtime_s‘的隐式声明
函数“pcap_findalldevs_ex”的隐式声明
函数的隐式声明“pcap_open”函数的隐式声明“scanf_s”
我在谷歌上搜索了一下,发现我应该添加一行#define HAVE_REMOTE (我不知道它是做什么的),但它会导致更多这样的错误:
未定义的对“pcap_open”的引用
对“pcap_findalldevs_ex”的未定义引用
对“localtime_s”的未定义引用
"pcap.h"似乎被正确地包含了(eclipse没有报告任何包含错误)。我已经将*.lib文件复制到MinGW/lib direcotry中,并在Path and Symbols->Library Paths (eclipse属性)中设置了这个路径。
我不知道下一步该做什么。任何想法都欢迎。提前感谢
下面是代码:
#include "pcap.h"
/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header,
const u_char *pkt_data);
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list on the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf_s("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* Open the device */
if ( (adhandle= pcap_open(d->name, // name of the device
65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured
on all the link layers
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1000, // read timeout
NULL, // authentication on the remote machine
errbuf // error buffer
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by
WinPcap\n", d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->description);
/* At this point, we don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
/* start the capture */
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *param, const struct pcap_pkthdr *header,
const u_char *pkt_data)
{
struct tm ltime;
char timestr[16];
time_t local_tv_sec;
/*
* unused variables
*/
(VOID)(param);
(VOID)(pkt_data);
/* convert the timestamp to readable format */
local_tv_sec = header->ts.tv_sec;
localtime_s(<ime, &local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", <ime);
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}发布于 2010-02-18 10:06:42
您可以将localtime_s调用替换为:
localtime_r(&local_tv_sec, <ime);(注意交换的参数。)
另外,将scanf_s调用替换为scanf。
localtime_s()和scanf_s()是微软特有的扩展,在MinGW中不可用.
发布于 2012-12-27 11:56:04
这在某种程度上是松散相关的,但是我发现要克服它是有问题的,所以我想做出贡献,所以其他研究这个线程的人实际上找到了解决方案。
传递给GCC的参数顺序很重要,您需要在".c“之前指定"-lwpcap”文件,否则将得到链接错误:
iflist.c:(.text+0x9d): undefined reference to `pcap_freealldevs'或
sendpack.c:(.text+0x178): undefined reference to `pcap_close'这并不是你的混合安装,Windows 7或XP,64位或32位,这只是传递给GCC的论点的顺序。
因此,我用于编译的实际命令是:
cd C:\install\winpcap\WpdPack_4_1_2\WpdPack\Examples-pcap\iflist
gcc -I ../../include -L ../../lib iflist.c -lwpcap -o iflist.exe我希望这能帮到你。
https://stackoverflow.com/questions/2287526
复制相似问题