2012-11-07 2 views
0

프로그램을 작성했으며 pcap_open_live()을 사용하는 필터를 점진적으로 적용합니다 (즉, pcap 필터를 다시 컴파일하고 초기 pcap_loop 이후 필터를 다시 설정 함). 그리고 내가 저장 한 파일 중 일부를 pcap에 테스트하고 싶습니다. Wireshark.pcap_open_offline()에서 pcap_set_filter()를 사용할 수 있습니까?

그러나 프로그램을 실행할 때 pcap_compile_filter에 빈 필터를 제공하지 않으면 패킷을 인쇄 할 수 없습니다.

저장된 파일에 lpcap을 사용하는 기능일까요? 아니면 잘못된 것입니까? 여기

가 열람에 대한 코드의 조각이다 :

int main(int argc, char **argv) 
{ 
char *dev = NULL;   /* capture device name */ 
char errbuf[PCAP_ERRBUF_SIZE];  /* error buffer */ 
pcap_t *handle;    /* packet capture handle */ 
char filter_exp[] = "ip";   /* filter expression [3] */ 
struct bpf_program fp;   /* compiled filter program (expression) */ 
bpf_u_int32 mask;   /* subnet mask */ 
bpf_u_int32 net;   /* ip */ 
int num_packets = -1;   /* number of packets to capture -1 => capture forever! */ 

printf("Filter expression: %s\n", filter_exp); 

// open capture device 
handle = pcap_open_offline("heartbeats2", errbuf); 
if (handle == NULL) { 
    fprintf(stderr, "pcap_open_offline failed: %s\n", errbuf); 
    exit(EXIT_FAILURE); 
} 

/* make sure we're capturing on an Ethernet device*/ 
if (pcap_datalink(handle) != DLT_EN10MB) { 
    fprintf(stderr, "%s is not an Ethernet\n", dev); 
    exit(EXIT_FAILURE); 
} 

/* compile the filter expression */ 
if (pcap_compile(handle, &fp, filter_exp, 0, 0) == -1) { 
    fprintf(stderr, "Couldn't parse filter %s: %s\n", 
     filter_exp, pcap_geterr(handle)); 
    exit(EXIT_FAILURE); 
} 

/* apply the compiled filter */ 
if (pcap_setfilter(handle, &fp) == -1) { 
    fprintf(stderr, "Couldn't install filter %s: %s\n", 
     filter_exp, pcap_geterr(handle)); 
    exit(EXIT_FAILURE); 
} 

pcap_loop(handle, -1, gotPacket, NULL); 

pcap_freecode(&fp); 
pcap_close(handle); 

printf("\nCapture complete.\n"); 

return(0); 
} 

(가) 패킷 기능은 단지 패킷의 페이로드를 출력 얻었다; 출력은 다음과 같습니다

Filter expression: ip 

Capture complete. 
+1

@nos 패킷에 VLAN이 없는데 이더넷 헤더 크기가 14로 설정되어 있습니다. 프로토콜 스택은 내가 관심있는 패킷의 이더넷, IP, udp입니다. – Eosis

+0

글쎄, 나는 Mountain Lion에서 캡처 파일,'got a packet '을 출력하는'gotPacket' 함수를 사용합니다. 'pcap_compile()'과'pcap_setfilter()'호출을'#if 0 '하면, 파일의 모든 패킷에 대해 "Got a packet"을보고합니다. '#if 0'과'# endif '를 제거하면, 파일에있는 모든 IP 패킷에 대해 "Got a packet"을보고합니다. 그러나 "ip"는 IPv4, * NOT * IPv6을 의미합니다. IPv6의 경우 "ip6"이 필요합니다. –

답변

0

당신의 gotPacket 기능을 보지 않고, 거기에 무슨 일이 일어나고 있는지 말할 수 없습니다.

다른 오류 메시지가 없으므로 프로그램이 코드 끝까지 실행되어 "Capture complete"가 인쇄됩니다. 메시지.

프로그램이 pcap_open_live()와 함께 작동하고 빈 필터를 사용하는 경우 pcap 파일에 ip 패킷이 포함되어 있지 않을 수도 있습니다.

wireshark로 pcap 파일을 열고 wireshark 필터 표현식에 "ip"필터를 사용할 수 있습니다. wireshark에서 일부 패킷을 볼 수 있다면 위의 프로그램도 필터로 작동해야합니다.

BPF 예제가있는 사이트가 많이 있습니다. 사이트의 예가 http://biot.com/capstats/bpf.html 일 수 있습니다.