*程序功能:对捕获的数据包只输出网络访问层报头为以太祯 网际层报头为ip4 传输层报头为tcp的 源目的主机的,mac地址,ip地址,端口号*/


 

 
  
  1. *程序功能:对捕获的数据包只输出网络访问层报头为以太祯 网际层报头为ip4 传输层报头为tcp的 源目的主机的,mac地址,ip地址,端口号*/  
  2.  
  3.  
  4. #include <stdio.h>  
  5.  
  6. #include <stdlib.h>  
  7.  
  8. #include <unistd.h>  
  9.  
  10. #include <pcap.h>  
  11.  
  12.    
  13.  
  14. #include <netinet/if_ether.h>  
  15.  
  16.  
  17. #define IP 2048  
  18. #define TCP 6  
  19.  
  20. typedef unsigned char UINT1;  
  21.  
  22. typedef unsigned short UINT2;  
  23.  
  24. typedef unsigned int UINT4;  
  25.  
  26.    
  27.  
  28. typedef struct ether  
  29. {  
  30.  
  31.   UINT1 dest[6];  
  32.  
  33.   UINT1 src[6];  
  34.  
  35.   UINT2 proto;  
  36.  
  37.   UINT1 data[0];  
  38.  
  39. } tEther;  
  40.  
  41.    
  42.  
  43. typedef struct ip  
  44. {  
  45.  
  46.   UINT1 hlen;   /*首部长度+版本 */ 
  47.  
  48.   UINT1 tos;   /*服务 */ 
  49.  
  50.   UINT2 len;   /*总长度 */ 
  51.  
  52.   UINT2 ipid;   /*标示 */ 
  53.  
  54.   UINT2 flagoff;  /*标示加偏移 */ 
  55.  
  56.   UINT1 ttl;   /*生存时间 */ 
  57.  
  58.   UINT1 proto;   /*协议 */ 
  59.  
  60.   UINT2 cksum;   /*首部检验和 */ 
  61.  
  62.   UINT4 src;   /*源地址 */ 
  63.  
  64.   UINT4 dest;   /*目地地址 */ 
  65.  
  66.   UINT1 data[0];  
  67.  
  68.    
  69.  
  70. } tIp;  
  71.  
  72.  
  73. typedef struct tcp  
  74. {  
  75.  
  76.   UINT2 sport;  
  77.  
  78.   UINT2 dport;  
  79.  
  80.   UINT4 seq;  
  81.  
  82.   UINT4 ack;  
  83.  
  84.   UINT1 hlen;  
  85.  
  86.   UINT1 code;  
  87.  
  88.   UINT2 window;  
  89.  
  90.   UINT2 chsum;  
  91.  
  92.   UINT2 urg;  
  93.  
  94.   char data[0];  
  95.  
  96.    
  97.  
  98. } tTcp;  
  99.  
  100.    
  101.  
  102.    
  103.  
  104. void proc_pkt (u_char * user, const struct pcap_pkthdr *hp,  
  105.         const u_char * packet);  
  106.  
  107. void net_host (UINT4 ipaddr);  
  108.  
  109. int 
  110. main ()  
  111. {  
  112.  
  113.   char *dev = NULL;  
  114.  
  115.   pcap_t *descr;  
  116.  
  117.   struct pcap_pkthdr hdr;  
  118.  
  119.   u_char *packet;  
  120.  
  121.   char errbuf[PCAP_ERRBUF_SIZE];  
  122.  
  123.   int promisc = 0, cnt = 5;  
  124.  
  125.   int pcap_time_out = 100;  
  126.  
  127.   struct tEther *pEpkt;  
  128.  
  129.   UINT4 net, mask;  
  130.  
  131.   dev = pcap_lookupdev (errbuf);  
  132.  
  133.   pcap_lookupnet (dev, &net, &mask, errbuf);  
  134.  
  135.   descr = pcap_open_live (dev, BUFSIZ, promisc, pcap_time_out, errbuf);  
  136.   printf ("网络号:");  
  137.   net_host (net);  
  138.   printf ("网络掩码:");  
  139.   net_host (mask);  
  140.   printf ("\n\n");  
  141.   pcap_loop (descr, -1, proc_pkt, NULL);  
  142.  
  143.    
  144.  
  145.    
  146.  
  147.   printf ("%s\n", dev);  
  148.  
  149.  
  150.   return 0;  
  151. }  
  152.  
  153.    
  154.  
  155. void 
  156. proc_pkt (u_char * user, const struct pcap_pkthdr *hp, const u_char * packet)  
  157. {  
  158.   tEther *pEther;  
  159.   tIp *pIp;  
  160.   int i;  
  161.   pEther = (tEther *) packet;  
  162.  
  163.  
  164.   if (ntohs (pEther->proto) == IP) /*网络层的报头为IP*/ 
  165.     {  
  166.  
  167.  
  168.       pIp = (tIp *) pEther->data;  
  169.  
  170.       if (pIp->proto == TCP) /*传输层的报头为为tcp */ 
  171.  {  
  172.  
  173.  
  174.    tTcp *pTcp;  
  175.  
  176.    pTcp = (tTcp *) pIp->data;  
  177.  
  178.  
  179.    printf ("目的MAC地址:");  
  180.    for (i = 0; i < 6; i++)  
  181.      {  
  182.        if ((pEther->dest[i]) < 16)  
  183.   printf ("0");  
  184.        printf ("%x ", pEther->dest[i]);  
  185.      }  
  186.    printf ("\n源MAC地址:");  
  187.    for (i = 0; i < 6; i++)  
  188.      {  
  189.        if (((pEther->src[i]) < 16))  
  190.   printf ("0");  
  191.        printf ("%x ", pEther->src[i]);  
  192.  
  193.      }  
  194.    printf ("\n");  
  195.    printf ("源IP地址:");  
  196.  
  197.    net_host (pIp->src);  
  198.  
  199.    printf ("目的IP地址:");  
  200.  
  201.    net_host (pIp->dest);  
  202.  
  203.  
  204.    printf ("源端口:");  
  205.  
  206.    printf ("%hu\n", ntohs (pTcp->sport));  
  207.  
  208.    printf ("目的端口:");  
  209.  
  210.    printf ("%hu\n\n\n", ntohs (pTcp->dport));  
  211.  
  212.  }  
  213.  
  214.     }  
  215.  
  216.   return;  
  217. }  
  218.  
  219. void 
  220. net_host (UINT4 ip_mask) /*将网络字节序的ip地址以及网络掩码转换转化为人们常用的形式 */ 
  221. {  
  222.   UINT4 one, two, three, four;  
  223.  
  224.   one = ip_mask;  
  225.  
  226.  
  227.   one = one >> 24;  
  228.  
  229.   two = ip_mask;  
  230.   two = two >> 16;  
  231.  
  232.   two = two & 0xff;  
  233.   three = ip_mask;  
  234.  
  235.   three = three >> 8;  
  236.  
  237.   three = three & 0xff;  
  238.  
  239.   four = ip_mask;  
  240.  
  241.  
  242.   four = four & 0xff;  
  243.  
  244.   printf ("%u.%u.%u.%u\n", four, three, two, one);  
  245. }