利用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\n",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<<"\t\t抓到一个包"<<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<<"字节|\t\t|"
 71             "总长度"<<ip_header->total_len<<"字节|"<<endl;
 72         cout<<"-------------------------------------------------"<<endl;
 73         cout<<"|\t\t\t|\t|\t\t|"<<endl;
 74         cout<<"-------------------------------------------------"<<endl;
 75         cout<<"|ttl "<<int(ip_header->ttl)<<"\t|协议 ";
 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<<"\t|首部校验和 "<<ip_header->checksum<<"\t|"<<endl;
 97         cout<<"-------------------------------------------------"<<endl;
 98         printf("|\t\t源地址 : %d.%d.%d.%d\t\t|\n",\
 99           ip_header->sourceIP[0],ip_header->sourceIP[1],ip_header->sourceIP[2],ip_header->sourceIP[3]);
100         cout<<"-------------------------------------------------"<<endl;
101         printf("|\t\t目的地址 : %d.%d.%d.%d\t\t|\n",\
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 }

posted @ 2013-11-15 15:25  老司机  阅读(925)  评论(0编辑  收藏  举报