iOS multi-Nic Grab pack

Source: Internet
Author: User

Linux under Libpcap support from multi-NIC capture package, set to any can

Unable to crawl all network card messages on iOS or Mac using the secondary method

1. By setting the Libevent event callback, each network card registers the read event, the FD passes

PD = Pcap_open_live (device, Snaplen, Dopromisc, timeout, ebuf);

Get.

#ifdef IOSStatic intAdd_event (pcap_t *p) {    if(!p)return-1; //event_init ();    if(!Base)      Base=event_base_new (); struct Event*ev =NULL; intFD =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); structtimeval timeout; Timeout.tv_sec=1; Timeout.tv_usec=0; //Event_base_set (Base,&evlisten); event_add (EV, NULL); return 0;}#endif

2. Thread Call Event loop

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

3. Other read messages, processed and PCAP before similar

#ifdef IOSvoidUser_pcap_run (evutil_socket_t S, ShortTvoid*Arg) {  if(!Arg)return; pcap_t*p = (pcap_t *) Arg; Constuint8_t *packet; structPCAP_PKTHDR *Pcap_header;  int32_t retval; Staticuint64_t recv_bytes =0; StaticUnsignedLongStart_time =0; unsignedLongEnd_time, Diff_time; intLink_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 ; }    Staticuint32_t Last_drop_num =0; structPcap_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 >= -)    {      floatMbps; Mbps= Recv_bytes *1.0/ ( -*1024x768*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 multi-Nic Grab pack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.