zoukankan      html  css  js  c++  java
  • 利用libpcap打印ip包

      1 #include <stdio.h>
      2 #include <pcap.h>
      3 #include <time.h>
      4 #include <netinet/in.h>
      5 #include <arpa/inet.h>
      6 #include <errno.h>
      7 #include <string.h>
      8 #include <iostream>
      9 
     10 using namespace std;
     11 
     12 //IP层数据包格式
     13 typedef struct 
     14 {
     15     int header_len:4;
     16     int version:4;
     17     u_char tos:8;
     18     int total_len:16;
     19     int ident:16;
     20     int flags:16;
     21     u_char ttl:8;
     22     u_char proto:8;
     23     int checksum:16;
     24     u_char sourceIP[4];
     25     u_char destIP[4];
     26 }IPHEADER;
     27 
     28 void callback(u_char* user,const struct pcap_pkthdr* header,const u_char* pkt_data);
     29 
     30 int main(int argc, char **argv)
     31 {
     32     char *device = NULL;
     33     char errbuf[1024];
     34     pcap_t *pcap;
     35     int link_data;
     36     
     37     if((device = pcap_lookupdev(errbuf)) == NULL)
     38     {
     39         perror(errbuf);
     40         return 1;
     41     }
     42     
     43     pcap = pcap_open_live(device, 65535, 1, 0, errbuf);
     44     if(pcap == NULL)
     45     {
     46         perror(errbuf);
     47         return 1;
     48     }
     49     
     50     if((link_data = pcap_datalink(pcap)) == -1)
     51     {
     52         fprintf(stderr,"pcap_link_data: %s
    ",pcap_geterr(pcap));
     53         return 1;
     54     }
     55 
     56     cout<<"开始抓包"<<endl;
     57     pcap_loop(pcap, -1, callback, NULL);
     58     
     59     return 0;
     60 }
     61 
     62 void callback(u_char* user,const struct pcap_pkthdr* header,const u_char* pkt_data)
     63 {
     64     cout<<"		抓到一个包"<<endl;
     65     cout<<"-------------------------------------------------"<<endl;
     66     //解析数据包IP头部
     67     if(header->len>=14){
     68         IPHEADER *ip_header=(IPHEADER*)(pkt_data+14);
     69         //解析协议类型
     70         cout<<"|版本 "<<ip_header->version<<"|首部长度"<<ip_header->header_len*4<<"字节|		|"
     71             "总长度"<<ip_header->total_len<<"字节|"<<endl;
     72         cout<<"-------------------------------------------------"<<endl;
     73         cout<<"|			|	|		|"<<endl;
     74         cout<<"-------------------------------------------------"<<endl;
     75         cout<<"|ttl "<<int(ip_header->ttl)<<"	|协议 ";
     76         switch(ip_header->proto)
     77         {
     78           case 1:
     79             cout<<"ICMP";
     80             break;
     81           case 2:
     82             cout<<"IGMP";
     83             break;
     84           case 6:
     85             cout<<"TCP ";
     86             break;
     87           case 17:
     88             cout<<"UDP ";
     89             break;
     90           case 41:
     91             cout<<"IPv6";
     92             break;
     93           default:
     94             cout<<"IPv4";
     95         }
     96         cout<<"	|首部校验和 "<<ip_header->checksum<<"	|"<<endl;
     97         cout<<"-------------------------------------------------"<<endl;
     98         printf("|		源地址 : %d.%d.%d.%d		|
    ",
     99           ip_header->sourceIP[0],ip_header->sourceIP[1],ip_header->sourceIP[2],ip_header->sourceIP[3]);
    100         cout<<"-------------------------------------------------"<<endl;
    101         printf("|		目的地址 : %d.%d.%d.%d		|
    ",
    102           ip_header->destIP[0],ip_header->destIP[1],ip_header->destIP[2],ip_header->destIP[3]);
    103         cout<<"-------------------------------------------------"<<endl;
    104         cout<<endl;
    105     }
    106 }

  • 相关阅读:
    Redis史上最全文章教程
    一目了然的 Node.js Windows10 安装篇
    maven 报错 Failed to execute goal on project ...: Could not resolve dependencies for project ...
    SpringCloud 的版本问题 (深入)
    shell编程题(二十八)
    shell编程题(二十七)
    shell编程题(二十六)
    C++之Boost Asio相关
    C++之function函数
    C++之正则表达式
  • 原文地址:https://www.cnblogs.com/zxh1210603696/p/3425515.html
Copyright © 2011-2022 走看看