IOS多網卡抓包

來源:互聯網
上載者:User

標籤:

linux下libpcap支援從多網卡抓包,設定為any即可

在IOS或者mac上就無法通過次方法抓取所有網卡報文

 

1.通過設定libevent事件回調,每個網卡註冊讀事件, fd通過

  pd = pcap_open_live(device, snaplen, dopromisc, timeout, ebuf);

獲得。

#ifdef IOSstatic int add_event(pcap_t *p){    if(!p)        return -1;    //event_init();    if(!base)      base = event_base_new();          struct event *ev = NULL;        int fd = pcap_fileno(p);    printf("---------fd:%d \n", fd);        //event_set(&evListen, fd, EV_READ, user_pcap_run, p );      ev = event_new(base,fd, EV_READ | EV_PERSIST, user_pcap_run,p);        struct timeval timeout;    timeout.tv_sec = 1;    timeout.tv_usec = 0;    //event_base_set(base,&evListen);        event_add(ev, NULL);      return 0;}#endif

2.開線程 呼叫事件迴圈

void *mt_vqmon_thread(void *param){#ifdef ANDROID    mmap_cap_run();    #elif defined IOS   event_base_dispatch(base);     printf("exit monitor thread\n");#else    pcap_t *p = (pcap_t *)param;  user_pcap_loop(p);#endif  return NULL;}

 

3.其他讀取報文,處理和pcap之前類似

#ifdef IOSvoid user_pcap_run(evutil_socket_t s, short t, void *arg){  if(!arg)      return;    pcap_t *p = (pcap_t *)arg;      const uint8_t *packet;  struct pcap_pkthdr *pcap_header;  int32_t retval;  static  uint64_t recv_bytes = 0;  static unsigned long start_time = 0;  unsigned long end_time, diff_time;  int link_type;    link_type = pcap_datalink(p);     while(1)    {    retval = pcap_next_ex(p, &pcap_header, &packet);      if(retval >= 1 && packet)    {            //handle_dissector(link_type,packet,pcap_header->caplen,&pcap_header->ts);      recv_bytes += pcap_header->caplen;    }    else if(retval == 0) //time out    {      printf("call user_pcap_run failed : No packets arrived before the timeout expired,try again!!!\n");      break;    }    else if(retval == -2 )  // error    {        printf("No more packets to read from the file !\n");            return ;    }    else       {        printf("call pcap_next_ex failed : %s\n",pcap_geterr(p));        return ;    }    static uint32_t last_drop_num = 0;    struct pcap_stat stat;    pcap_stats(p,&stat);    if(stat.ps_drop && stat.ps_drop != last_drop_num)    {        printf("========>total packet = %u drop packet = %u \n",stat.ps_recv,stat.ps_drop);        last_drop_num = stat.ps_drop;    }    // Print debug info.    end_time = time(0);        if(start_time == 0)    {      start_time = end_time;    }    diff_time = end_time - start_time;        if(diff_time >= 30)    {      float mbps;      mbps = recv_bytes * 1.0 / (128 * 1024 * diff_time);      start_time = end_time;      recv_bytes = 0;      printf("==================================================> captured speed = %.2f Mbps !!!\n",mbps);    }        /*        * Check for savagely closed TCP connections.         */    tcp_check_timeouts(&pcap_header->ts);    }  return ;}#endif

 

IOS多網卡抓包

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.