gpt4 book ai didi

c - Winpcap MinGW编译错误

转载 作者:可可西里 更新时间:2023-11-01 09:43:50 28 4
gpt4 key购买 nike

我正在尝试使用适用于 Windows 的 WinPcap 4.1.1 库,但我什至无法编译该库提供的示例源代码。

我遇到了这些错误:

'PCAP_OPENFLAG_PROMISCUOUS' undeclared (first use in this function)
'PCAP_SRC_IF_STRING' undeclared (first use in this function)

还有一堆警告:

implicit declaration of function 'localtime_s'
implicit declaration of function 'pcap_findalldevs_ex'
implicit declaration of function 'pcap_open'
implicit declaration of function 'scanf_s'

我用 Google 搜索了一下,发现我应该添加一行 #define HAVE_REMOTE(我不知道它做了什么)但是它会导致更多这样的错误:

undefined reference to 'pcap_open'
undefined reference to 'pcap_findalldevs_ex'
undefined reference to 'localtime_s'

"pcap.h" 似乎被正确包含(eclipse 不报告任何包含错误)。我已将 *.lib 文件复制到 MinGW/lib 目录中,并在 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(&ltime, &local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", &ltime);

printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);

}

最佳答案

您可以将 localtime_s 调用替换为:

localtime_r(&local_tv_sec, &ltime);

(注意交换的参数。)

此外,将您的 scanf_s 调用替换为 scanf

localtime_s()scanf_s() 是 Microsoft 特定的扩展,在 MinGW 中不可用。

关于c - Winpcap MinGW编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2287526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com