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

[cpp]  view plain  copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10. #include   
  11. #include   
  12.   
  13. using std::cout;  
  14. using std::endl;  
  15. using std::thread;  
  16. using std::vector;  
  17. using std::string;  
  18.   
  19.   
  20. //解析数据包  
  21. void getPacket(u_char * arg,const struct pcap_pkthdr *pkthdr,const u_char * packet){  
  22.     unsigned char src_mac[18] = "";  
  23.     unsigned char dst_mac[18] = "";  
  24.     unsigned char src_addr[20] = "";      
  25.     unsigned char dst_addr[20] = "";  
  26.       
  27.     unsigned char head_str[50] = "";  
  28.     unsigned char body_str[512] = "";  
  29.   
  30.     vector split_vector;  
  31.     char *p = NULL;  
  32.     const char *split = "|";  
  33.   
  34.     int *id = (int *)arg;  
  35.     cout << "id: " << ++(*id) << endl;  
  36.     cout << "Packet length: " << pkthdr->len << endl;  
  37.     cout << "Number of bytes: " << pkthdr->caplen << endl;  
  38.     cout << "Recieved time: " << ctime((const time_t *)&pkthdr->ts.tv_sec);  
  39.   
  40.     if (pkthdr->len != 94)  
  41.     {  
  42.         cout << "wifi TanZhen message length error." << endl;  
  43.         exit(1);  
  44.     }  
  45.       
  46.     memcpy(head_str, (char *)packet, 42);  
  47.     memcpy(body_str, (char *)packet + 42, 52);  
  48.     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]);      
  49.     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]);    
  50.   
  51.     //消息头  
  52.     if (head_str[12] == 0x08 && head_str[13] == 0x00)  
  53.     {  
  54.         printf("____________________IP Protocol____________________\n");  
  55.         printf("MAC:%s >> %s\n", src_mac, dst_mac);  
  56.         sprintf((char *)src_addr, "%02d.%02d.%02d.%02d", head_str[26], head_str[27], head_str[28], head_str[29]);     
  57.         sprintf((char *)dst_addr, "%02d.%02d.%02d.%02d", head_str[30], head_str[31], head_str[32], head_str[33]);  
  58.         printf("IP:%s >> %s\n", src_addr, dst_addr);  
  59.   
  60.         if (head_str[23] == 0x01)  
  61.         {  
  62.             printf("Type:ICMP\n");  
  63.         }  
  64.         else if (head_str[23] == 0x02)  
  65.         {  
  66.             printf("Type:IGMP\n");  
  67.         }  
  68.         else if (head_str[23] == 0x06)  
  69.         {  
  70.             printf("Type:TCP\n");  
  71.         }         
  72.         else if (head_str[23] == 0x11)  
  73.         {  
  74.             printf("Type:UDP\n");  
  75.         }  
  76.   
  77.         printf("Port: %d >> %d\n", ntohs(*(unsigned short *)(head_str + 34)), ntohs(*(unsigned short *)(head_str + 36)));  
  78.     }  
  79.   
  80.     //消息体  
  81.     for (unsigned int i=42; ilen; ++i)  
  82.     {  
  83.         printf("%c", *(packet + i));  
  84.     }  
  85.     cout << endl;  
  86.   
  87.     //拆分消息体  
  88.     p = strtok((char *)body_str, split);  
  89.     while(p != NULL){  
  90.         split_vector.push_back(p);  
  91.         p = strtok(NULL, split);  
  92.     }  
  93.   
  94.     cout << "split vector size:" << split_vector.size() << endl;  
  95.     for (auto itr = split_vector.cbegin(); itr != split_vector.cend(); itr++){  
  96.         cout << *itr << endl;  
  97.     }  
  98.       
  99.     cout << "-------------------------------------------------------" << endl;  
  100. }  
  101.   
  102.   
  103. int main(int argc, char *argv[]){  
  104.     char errBuf[PCAP_ERRBUF_SIZE] = {0};  
  105.     char *device = nullptr;  
  106.   
  107.     //获取网络接口  
  108.     device = pcap_lookupdev(errBuf);  
  109.   
  110.     if (device){  
  111.         cout << "succeed get device: " << device << endl;  
  112.     }  
  113.     else{  
  114.         cout << "error: " << errBuf << endl;  
  115.         exit(1);  
  116.     }  
  117.   
  118.     //打开网络接口  
  119.     pcap_t *live_device = pcap_open_live(device, 65535, 1, 0, errBuf);//任何一个协议的一个数据包长度必然小于65535,1表示混杂模式,0表示一直等待数据包到来  
  120.   
  121.     if (!live_device){  
  122.         cout << "error: pcap_open_live(): " << errBuf << endl;  
  123.         exit(1);  
  124.     }  
  125.       
  126.     //构造一个过滤器  
  127.     struct bpf_program filter;  
  128.     //编译过滤器  
  129.     pcap_compile(live_device, &filter, "udp dst port 9900", 1, 0);//在wifi探针平台设置接收消息的服务器和端口  
  130.     //设置过滤器  
  131.     pcap_setfilter(live_device, &filter);  
  132.       
  133.     //循环获取数据  
  134.     int id = 0;  
  135.     pcap_loop(live_device, -1, getPacket, (u_char *)&id);//-1表示循环抓包   
  136.   
  137.     //关闭网络接口  
  138.     pcap_close(live_device);  
  139.       
  140.     return 0;  
  141. }  

猜你喜欢

转载自blog.csdn.net/lsg_down/article/details/80451026