2010-02-18 3 views
2

Windows 용 WinPcap 4.1.1 라이브러리를 실험하고 있지만 라이브러리와 함께 제공되는 예제 소스를 컴파일 할 수 없습니다.Winpcap MinGW 컴파일 오류

나는 이러한 오류를 받고 있어요 :

'PCAP_OPENFLAG_PROMISCUOUS'선언되지 않은 (이 기능에서 처음 사용)
'PCAP_SRC_IF_STRING'

그리고 무리를 (먼저이 기능에 사용) 선언되지 않은 경고 :

암시 적으로 'localtime_s'함수 선언
기능의 mplicit 선언 'pcap_findalldevs_ex'기능 'pcap_open'기능의
암시 적 선언의
암시 적 선언 'scanf_s'

나는 비트를 봤 내가 줄 #define HAVE_REMOTE을 추가해야합니다 발견 (나는 어떤이 없습니다 단서는 할)하지만이 같은 더 많은 오류를 초래하는 일 :

정의되지 않은 참조 'pcap_findalldevs_ex'
UNDEFINE에 'pcap_open'
정의되지 않은 참조에 d 'localtime_s'에 대한 참조

"pcap.h"이 포함되어있는 것 같습니다 (eclipse는 오류를 포함하여보고하지 않습니다). * .lib 파일을 MinGW/lib 디렉터리로 복사하고 Path and Symbols->Library Paths (이클립스 프로젝트 속성)에이 경로를 설정하십시오.

다음에 시도 할 내용이 없습니다. 어떤 아이디어라도 환영합니다.

#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); 

} 
+2

"정의되지 않은 참조"메시지는 당신이'libpcap'과 링크하지 않는다는 것을 의미합니다. 'pcap-config --libs'는 링크에 필요한 플래그를줍니다. http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/pcap-config.1.html –

+1

@Alok 당신이 옳다고 생각합니다. 저는 라이브러리를 연결하는 대신 라이브러리 디렉토리를 설정했습니다. . 'wpcap.lib'와'Packet.lib'을 링크 한 후에는'localtime_s'와'scanf_s' 함수에 대해서 정의되지 않은 참조 에러가 2 개 밖에 없습니다. 이 기능들은 어디에 위치해 있습니까? –

답변

4

당신은 당신의 localtime_s 전화를 대체 할 수있다 : 여기

사전

에서 덕분에 코드의

localtime_r(&local_tv_sec, &ltime); 

(스왑 된 인수를 참고.)

또한

, 당신의 scanf_s 교체 scanf으로 전화하십시오.

localtime_s()scanf_s()은 Microsoft 관련 확장 프로그램이며 MinGW에서는 사용할 수 없습니다.

+0

나는'localtime_r'과 같은 에러를 보았습니다.하지만'ltime = localtime (& local_tv_sec); 대신'localtime'을 사용했습니다. –

+0

MinGW에서는'localtime_r'을 사용할 수 없습니다. 스레드 안전성에 대해 걱정하지 않는다면 적절한 'localtime' 호출로 바꿉니다. –

+0

또한,'ltime'의 타입을 포인터로 변경했다고 가정합니다. –

4

다소 느슨하게 관련되어 있지만, 문제가있는 부분이 있으므로 극복해야한다는 것을 알았 기 때문에 기여하고 싶습니다. 그래서이 스레드를보고있는 다른 사람들은 실제로 해결책을 찾습니다.

GCC 에 전달할 매개 변수의 순서는입니다. 중요한 것은 "."-lwpcap C"를하기 전에 파일 ", 그렇지 않으면 당신은 연결 오류를 얻을 것이다 :

iflist.c:(.text+0x9d): undefined reference to `pcap_freealldevs' 

또는

sendpack.c:(.text+0x178): undefined reference to `pcap_close' 

같은 그것은 당신의와 Mingw 설치, 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 
,

도움이 되었기를 바랍니다.