프로그램을 작성했으며 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.
@nos 패킷에 VLAN이 없는데 이더넷 헤더 크기가 14로 설정되어 있습니다. 프로토콜 스택은 내가 관심있는 패킷의 이더넷, IP, udp입니다. – Eosis
글쎄, 나는 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"이 필요합니다. –