2013-09-03 1 views
3

libpcap에 약간 문제가 있습니다. 나는이 콜백 pcap_loop()을 사용하고 있습니다 : 콘솔에PCAP 대상 및 소스가 동일합니다.

void pcap_callback(u_char *useless, const struct pcap_pkthdr *pcap_header, const u_char *packet) { 
    struct ether_header *head = (struct ether_header *)packet; 
    struct ip *ip = (struct ip *)(packet + sizeof(struct ether_header)); 

    u_short eth_type = ntohs(head->ether_type); 
    const char *s_ipad = inet_ntoa(ip->ip_src); 
    const char *d_ipad = inet_ntoa(ip->ip_dst); 
    const char *s_host = ether_ntoa((struct ether_addr *)head->ether_shost); 
    const char *d_host = ether_ntoa((struct ether_addr *)head->ether_dhost); 

    if (eth_type == ETHERTYPE_IP || eth_type == ETHERTYPE_IPV6) { 
     NSLog(@"\nPACKET\ns IP : %s\ns MAC: %s\n\nd IP : %s\nd MAC: %s\n\n", s_ipad, s_host, d_ipad, d_host); 
    } 
} 

나는 그 목적지와 소스 정확히 동일 패킷을 얻을. 그래서 IP와 MAC 주소는 다르지 않습니다. 필터를 사용하여 패킷에 모두 대상 주소가 있음을 알 수있었습니다. '남자 inet_ntoa'에서

void sniff() { 
    pcap_t *handle; 
    char *device; 
    char errbuf[PCAP_ERRBUF_SIZE]; 

    char *net; 
    char *mask; 
    bpf_u_int32 netp; 
    bpf_u_int32 maskp; 
    struct in_addr addr; 

    device = "en1"; 

    pcap_lookupnet(device, &netp, &maskp, errbuff); 

    addr.s_addr = netp; 
    net = inet_ntoa(addr); 
    NSLog(@"ROUTER: %s", net); 

    addr.s_addr = maskp; 
    mask = inet_ntoa(addr); 
    NSLog(@"SNMASK: %s", mask); 

    handle = pcap_open_live(device, BUFSIZ, 0, 10, errbuf); 

    struct bpf_program filterProgram; 
    pcap_compile(handle, &filterProgram, "src 10.0.10.40 or dst 10.0.10.40", 1, maskp); 
    pcap_setfilter(handle, &filterProgram); 

    pcap_loop(handle, 100, pcap_callback, NULL); 
    pcap_close(handle); 
} 

답변

6

:

내가 PCAP 시작하는 데 사용하고 코드입니다 inet_ntoa() 함수에, 네트워크 바이트 순서로 주어진 인터넷 호스트 주소를 변환 IPv4의 점 분리 십진수 표기법. 문자열은 정적으로 할당 된 버퍼에 반환되며 이후의 호출은이를 덮어 씁니다.

따라서 inet_ntoa 및 ether_ntoa를 다시 호출하기 전에 s_ipad 및 s_host를 복사해야합니다. 이 같은 것 :

const char *aux = inet_ntoa(ip->ip_src); 
const char *s_ipad = strcpy(new char[strlen(aux)+1], aux); 
aux = inet_ntoa(ip->ip_dst); 
const char *d_ipad = strcpy(new char[strlen(aux)+1], aux); 
aux = ether_ntoa((struct ether_addr *)head->ether_shost); 
const char *s_host = strcpy(new char[strlen(aux)+1], aux); 
aux = ether_ntoa((struct ether_addr *)head->ether_dhost); 
const char *d_host = strcpy(new char[strlen(aux)+1], aux); 
//do whatever...  
delete[] s_ipad; delete[] d_ipad;delete[] s_host; delete[] d_host; 
+0

절대적으로 매력처럼 작동합니다! –