日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

libpcap的简单使用--抓取特定类型和端口的网络数据

發布時間:2024/4/24 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 libpcap的简单使用--抓取特定类型和端口的网络数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[cpp]?view plaincopy
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • #include???
  • ??
  • using?std::cout;??
  • using?std::endl;??
  • using?std::thread;??
  • using?std::vector;??
  • using?std::string;??
  • ??
  • ??
  • //解析數據包??
  • void?getPacket(u_char?*?arg,const?struct?pcap_pkthdr?*pkthdr,const?u_char?*?packet){??
  • ????unsigned?char?src_mac[18]?=?"";??
  • ????unsigned?char?dst_mac[18]?=?"";??
  • ????unsigned?char?src_addr[20]?=?"";??????
  • ????unsigned?char?dst_addr[20]?=?"";??
  • ??????
  • ????unsigned?char?head_str[50]?=?"";??
  • ????unsigned?char?body_str[512]?=?"";??
  • ??
  • ????vector?split_vector;??
  • ????char?*p?=?NULL;??
  • ????const?char?*split?=?"|";??
  • ??
  • ????int?*id?=?(int?*)arg;??
  • ????cout?<<?"id:?"?<<?++(*id)?<<?endl;??
  • ????cout?<<?"Packet?length:?"?<<?pkthdr->len?<<?endl;??
  • ????cout?<<?"Number?of?bytes:?"?<<?pkthdr->caplen?<<?endl;??
  • ????cout?<<?"Recieved?time:?"?<<?ctime((const?time_t?*)&pkthdr->ts.tv_sec);??
  • ??
  • ????if?(pkthdr->len?!=?94)??
  • ????{??
  • ????????cout?<<?"wifi?TanZhen?message?length?error."?<<?endl;??
  • ????????exit(1);??
  • ????}??
  • ??????
  • ????memcpy(head_str,?(char?*)packet,?42);??
  • ????memcpy(body_str,?(char?*)packet?+?42,?52);??
  • ????sprintf((char?*)dst_mac,?"%02x:%02x:%02x:%02x:%02x:%02x",?head_str[0],?head_str[1],?head_str[2],?head_str[3],?head_str[4],?head_str[5]);??????
  • ????sprintf((char?*)src_mac,?"%02x:%02x:%02x:%02x:%02x:%02x",?head_str[6],?head_str[7],?head_str[8],?head_str[9],?head_str[10],?head_str[11]);????
  • ??
  • ????//消息頭??
  • ????if?(head_str[12]?==?0x08?&&?head_str[13]?==?0x00)??
  • ????{??
  • ????????printf("____________________IP?Protocol____________________\n");??
  • ????????printf("MAC:%s?>>?%s\n",?src_mac,?dst_mac);??
  • ????????sprintf((char?*)src_addr,?"%02d.%02d.%02d.%02d",?head_str[26],?head_str[27],?head_str[28],?head_str[29]);?????
  • ????????sprintf((char?*)dst_addr,?"%02d.%02d.%02d.%02d",?head_str[30],?head_str[31],?head_str[32],?head_str[33]);??
  • ????????printf("IP:%s?>>?%s\n",?src_addr,?dst_addr);??
  • ??
  • ????????if?(head_str[23]?==?0x01)??
  • ????????{??
  • ????????????printf("Type:ICMP\n");??
  • ????????}??
  • ????????else?if?(head_str[23]?==?0x02)??
  • ????????{??
  • ????????????printf("Type:IGMP\n");??
  • ????????}??
  • ????????else?if?(head_str[23]?==?0x06)??
  • ????????{??
  • ????????????printf("Type:TCP\n");??
  • ????????}?????????
  • ????????else?if?(head_str[23]?==?0x11)??
  • ????????{??
  • ????????????printf("Type:UDP\n");??
  • ????????}??
  • ??
  • ????????printf("Port:?%d?>>?%d\n",?ntohs(*(unsigned?short?*)(head_str?+?34)),?ntohs(*(unsigned?short?*)(head_str?+?36)));??
  • ????}??
  • ??
  • ????//消息體??
  • ????for?(unsigned?int?i=42;?ilen;?++i)??
  • ????{??
  • ????????printf("%c",?*(packet?+?i));??
  • ????}??
  • ????cout?<<?endl;??
  • ??
  • ????//拆分消息體??
  • ????p?=?strtok((char?*)body_str,?split);??
  • ????while(p?!=?NULL){??
  • ????????split_vector.push_back(p);??
  • ????????p?=?strtok(NULL,?split);??
  • ????}??
  • ??
  • ????cout?<<?"split?vector?size:"?<<?split_vector.size()?<<?endl;??
  • ????for?(auto?itr?=?split_vector.cbegin();?itr?!=?split_vector.cend();?itr++){??
  • ????????cout?<<?*itr?<<?endl;??
  • ????}??
  • ??????
  • ????cout?<<?"-------------------------------------------------------"?<<?endl;??
  • }??
  • ??
  • ??
  • int?main(int?argc,?char?*argv[]){??
  • ????char?errBuf[PCAP_ERRBUF_SIZE]?=?{0};??
  • ????char?*device?=?nullptr;??
  • ??
  • ????//獲取網絡接口??
  • ????device?=?pcap_lookupdev(errBuf);??
  • ??
  • ????if?(device){??
  • ????????cout?<<?"succeed?get?device:?"?<<?device?<<?endl;??
  • ????}??
  • ????else{??
  • ????????cout?<<?"error:?"?<<?errBuf?<<?endl;??
  • ????????exit(1);??
  • ????}??
  • ??
  • ????//打開網絡接口??
  • ????pcap_t?*live_device?=?pcap_open_live(device,?65535,?1,?0,?errBuf);//任何一個協議的一個數據包長度必然小于65535,1表示混雜模式,0表示一直等待數據包到來??
  • ??
  • ????if?(!live_device){??
  • ????????cout?<<?"error:?pcap_open_live():?"?<<?errBuf?<<?endl;??
  • ????????exit(1);??
  • ????}??
  • ??????
  • ????//構造一個過濾器??
  • ????struct?bpf_program?filter;??
  • ????//編譯過濾器??
  • ????pcap_compile(live_device,?&filter,?"udp?dst?port?9900",?1,?0);//在wifi探針平臺設置接收消息的服務器和端口??
  • ????//設置過濾器??
  • ????pcap_setfilter(live_device,?&filter);??
  • ??????
  • ????//循環獲取數據??
  • ????int?id?=?0;??
  • ????pcap_loop(live_device,?-1,?getPacket,?(u_char?*)&id);//-1表示循環抓包???
  • ??
  • ????//關閉網絡接口??
  • ????pcap_close(live_device);??
  • ??????
  • ????return?0;??
  • } ?
  • 與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的libpcap的简单使用--抓取特定类型和端口的网络数据的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。