免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2687 | 回复: 7
打印 上一主题 下一主题

关于用libpcap库取得数据包并进行分类还存在的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-28 09:42 |只看该作者 |倒序浏览
本帖最后由 shujunz 于 2010-10-28 09:45 编辑

无聊时写了一个抓包的软件,但是在获取源ip地址和目标ip地址的时候总是显示不出来,大家看看是哪里出了问题:

  1. /*we must run this program in root*/
  2. /*存在的问题:不能读取到ip地址*/
  3. #include<pcap.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. char errbuf[PCAP_ERRBUF_SIZE];



  8. struct ether_header{//Ethernet
  9.         u_int8_t ether_dhost[6];
  10.         u_int8_t ether_shost[6];
  11.         u_int16_t ether_type;
  12. };
  13. typedef u_int32_t in_addr_t;
  14. struct in_addr{
  15.         in_addr_t s_addr;
  16. };
  17. /*
  18. -------------------------------------------
  19.       A R P
  20. -------------------------------------------
  21. */
  22. struct arp_header{//Arp
  23.         u_int16_t arp_hardware_type;
  24.         u_int16_t arp_protocol_type;
  25.         u_int8_t arp_hardware_length;
  26.         u_int8_t arp_protocol_length;
  27.         u_int16_t arp_operation_code;//arp Opcode!
  28.         u_int8_t arp_source_ethernet_address[6];
  29.         u_int8_t arp_destination_ethernet_address[6];
  30.        
  31.         //u_int16_t arp_source_ip_address[4];
  32.         u_int8_t arp_source_ip_address[4];
  33.         u_int8_t arp_destination_ip_address[4];

  34. };

  35. void ip_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content);
  36. void arp_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content);
  37. void ethernet_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content);
  38. pcap_t * init_pcap();
  39. void get_packet(pcap_t *pcap_handle);
  40. void close_pcap(pcap_t *pcap_handle);


  41. void ip_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content)
  42. {//IP call_back function


  43. }



  44. void arp_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content)
  45. {//ARP call_back function
  46.         struct arp_header *arp_protocol;
  47.         u_short protocol_type;
  48.         u_short hardware_type;
  49.         u_short operation_code;
  50.         u_char *mac_string;
  51.         struct in_addr source_ip_address;
  52.         struct in_addr destination_ip_address;
  53.         u_char hardware_length;
  54.         u_char protocol_length;
  55.         printf("---------ARP Protocol(Network Layer)----------\n");
  56.         arp_protocol=(struct arp_header *)(packet_content+14);
  57.     /*
  58.      * 获得ARP协议数据。注意在这里要跳过以太网数据部分,它的长度刚好是14,所以在这里加上14,是指针跳过14个字节
  59.      */
  60.         hardware_type=ntohs(arp_protocol->arp_hardware_type);
  61.         operation_code=ntohs(arp_protocol->arp_operation_code);
  62.         protocol_type=ntohs(arp_protocol->arp_protocol_type);
  63.         hardware_length=arp_protocol->arp_hardware_length;
  64.         protocol_length=arp_protocol->arp_protocol_length;
  65.         printf("ARP Hardware Type:%d\n",hardware_type);
  66.         printf("ARP Protocol Type:%d\n",protocol_type);
  67.         printf("ARP Hardware Length:%d\n",hardware_length);
  68.         printf("ARP Protocol Length:%d\n",protocol_length);
  69.         printf("ARP Operation Code:%d\n",operation_code);

  70. //------------------------------------------------
  71.         //Judge up layer
  72.         switch(operation_code)
  73.         {
  74.                 case 1:
  75.                         printf("ARP request protocol\n");
  76.                         break;
  77.                 case 2:
  78.                         printf("ARP reply protocol\n");
  79.                         break;
  80.                 case 3:
  81.                         printf("RARP request protocol\n");
  82.                         break;
  83.                 case 4:
  84.                         printf("RARP reply protocol\n");
  85.                         break;
  86.                 default :
  87.                         break;
  88.         }
  89. //--------------------------------------
  90.         printf("Ethernet source address is:\n");
  91.         mac_string=arp_protocol->arp_source_ethernet_address;
  92.         printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
  93.         memcpy((void*)&source_ip_address,(void *)arp_protocol->arp_source_ip_address,sizeof(struct in_addr));
  94.                
  95.         printf("Source Ip address is :\n",inet_ntoa(source_ip_address));

  96.         printf("Ethernet destination address is:\n");
  97.         mac_string=arp_protocol->arp_destination_ethernet_address;
  98.         printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
  99.         memcpy((void*)&destination_ip_address,(void *)arp_protocol->arp_destination_ip_address,sizeof(struct in_addr));
  100.         printf("Destination Ip address is :\n",inet_ntoa(source_ip_address));

  101. }



  102. void ethernet_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content)//Ethernet call_back function
  103. {
  104.         u_short ethernet_type;//Ethernet type
  105.         struct ether_header *ethernet_protocol;//Ethernet protocol variables
  106.         u_char *mac_string;//Ethernet address---MAC address
  107.         static int packet_num=1;//default packet number!
  108.         printf("***********************************************\n");
  109.         printf("The %d packet is captured..\n",packet_num);
  110.         printf("--------Ethernet protocol (Link Layer)--------\n");
  111.         ethernet_protocol=(struct ether_header *)packet_content;
  112.         printf("Ethernet type is:");
  113.         ethernet_type=ntohs(ethernet_protocol->ether_type);
  114.         printf("%04x\n",ethernet_type);
  115.   //Judge the up_layer protocol
  116.         //coding....
  117.         switch(ethernet_type)
  118.         {
  119.                 case 0x0800:
  120.                         printf("The network layer is IP protocol\n");
  121.                         ip_protocol_packet_callback(argument,packet_header,packet_content);
  122.                         break;
  123.                 case 0x0806:
  124.                         printf("The network layer is ARP protocol\n");
  125.                         arp_protocol_packet_callback(argument,packet_header,packet_content);
  126.                         break;
  127.                 case 0x08035:
  128.                         printf("The network layer is RARP protocol\n");
  129.                         break;
  130.                 default :
  131.                         break;
  132.         }
  133.         printf("Mac Source Address is:\n");
  134.         mac_string=ethernet_protocol->ether_shost;
  135.         printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
  136.         printf("Mac Destination Address is:\n");
  137.         mac_string=ethernet_protocol->ether_dhost;
  138.         printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
  139.         printf("***********************************************\n");
  140.         packet_num++;
  141.        
  142. }       

  143. pcap_t * init_pcap()
  144. {       
  145.         pcap_t *pcap_handle;
  146.         char *interface;//
  147.         struct bpf_program bpf_filter;
  148.         char bpf_filter_string[]="";//设置过滤规则为空,表示捕获所有数据包
  149. //        bpf_u_int32 net_mask;
  150. //        bpf_u_int32 net_ip;
  151.         u_int32_t net_mask;
  152.         u_int32_t net_ip;
  153.         interface=pcap_lookupdev(errbuf);//get net interface
  154.         if(interface==NULL)
  155.                 {
  156.                         printf("get net interface error:%s\n",errbuf);
  157.                         //return 1;
  158.                         return NULL;
  159.                 }
  160.         if(pcap_lookupnet(interface,&net_ip,&net_mask,errbuf)==-1)//get mask and ip address
  161.                 {
  162.                         printf("get mask and ip error:%s\n",errbuf);
  163. //                        return 2;
  164.                         return NULL;
  165.                 }
  166.         pcap_handle=pcap_open_live(interface,BUFSIZ,1,0,errbuf);//open the Interface
  167.         if(pcap_handle==NULL)
  168.                 {
  169.                         printf("open the Inerface:%s\n",pcap_geterr(pcap_handle));
  170. //                        return 3;
  171.                         return NULL;
  172.                 }
  173.         if(pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip)==-1)//Compile the filtering rules
  174.                 {
  175.                         printf("Compile the filtering rules:%s\n",pcap_geterr(pcap_handle));
  176. //                        return 4;
  177.                         return NULL;
  178.                 }
  179.         if(pcap_setfilter(pcap_handle,&bpf_filter)==-1)//set filter rules!
  180.                 {
  181.                         printf("set filter rules:%s\n",pcap_geterr(pcap_handle));
  182. //                        return 5;
  183.                         return NULL;
  184.                 }
  185.        
  186. return pcap_handle;
  187. }

  188. void get_packet(pcap_t *pcap_handle)
  189. {
  190.         if(pcap_datalink(pcap_handle)!=DLT_EN10MB)//Judge the packet is not Ethernet packet
  191.                 {
  192.                         return;
  193.                 }
  194.         if(pcap_loop(pcap_handle,-1,ethernet_protocol_packet_callback,NULL)!=0)
  195.                 {
  196.                         printf("get packet error:%s\n",pcap_geterr(pcap_handle));
  197.                 }

  198. }
  199. void close_pcap(pcap_t *pcap_handle)
  200. {
  201.         pcap_close(pcap_handle);
  202. }
  203. int main()
  204. {
  205.         pcap_t *pcap_handle;
  206.         pcap_handle=init_pcap();
  207.         if(pcap_handle==NULL)
  208.         {
  209.                 return 1;
  210.         }
  211.         get_packet(pcap_handle);
  212.         close_pcap(pcap_handle);


  213. return 0;
  214. }

复制代码
原来是写到几个c文件中的 为了方便我把他们整合到一个文件中了

论坛徽章:
0
2 [报告]
发表于 2010-10-28 09:50 |只看该作者
这是编译和运行的结果 由于抓的包比较多 我就选了几个

  1. % gcc get_packet.c -lpcap
  2. % ./a.out
  3. get net interface error:no suitable device found
  4. % sudo ./a.out



  5. The 33 packet is captured..
  6. --------Ethernet protocol (Link Layer)--------
  7. Ethernet type is:0806
  8. The network layer is ARP protocol
  9. ---------ARP Protocol(Network Layer)----------
  10. ARP Hardware Type:1
  11. ARP Protocol Type:2048
  12. ARP Hardware Length:6
  13. ARP Protocol Length:4
  14. ARP Operation Code:1
  15. ARP request protocol
  16. Ethernet source address is:
  17. 00:1a:4d:96:81:da
  18. Source Ip address is :
  19. Ethernet destination address is:
  20. ac:10:12:4e:00:00
  21. Destination Ip address is :
  22. Mac Source Address is:
  23. 00:1a:4d:96:81:da
  24. Mac Destination Address is:
  25. ff:ff:ff:ff:ff:ff
  26. ***********************************************
  27. ***********************************************
  28. The 34 packet is captured..
  29. --------Ethernet protocol (Link Layer)--------
  30. Ethernet type is:0806
  31. The network layer is ARP protocol
  32. ---------ARP Protocol(Network Layer)----------
  33. ARP Hardware Type:1
  34. ARP Protocol Type:2048
  35. ARP Hardware Length:6
  36. ARP Protocol Length:4
  37. ARP Operation Code:1
  38. ARP request protocol
  39. Ethernet source address is:
  40. 00:1a:4d:96:81:da
  41. Source Ip address is :
  42. Ethernet destination address is:
  43. ac:10:12:4e:00:00
  44. Destination Ip address is :
  45. Mac Source Address is:
  46. 00:1a:4d:96:81:da
  47. Mac Destination Address is:
  48. ff:ff:ff:ff:ff:ff
  49. ***********************************************
  50. ***********************************************
  51. The 35 packet is captured..
  52. --------Ethernet protocol (Link Layer)--------
  53. Ethernet type is:0800
  54. The network layer is IP protocol
  55. Mac Source Address is:
  56. 40:61:86:fe:ff:3f
  57. Mac Destination Address is:
  58. 00:19:66:4c:02:08
  59. ***********************************************
  60. ***********************************************
  61. The 36 packet is captured..
  62. --------Ethernet protocol (Link Layer)--------
  63. Ethernet type is:0800
  64. The network layer is IP protocol
  65. Mac Source Address is:
  66. 00:19:66:4c:02:08
  67. Mac Destination Address is:
  68. 40:61:86:fe:ff:3f
  69. ***********************************************

复制代码
另外代码只写到分析ARP ,IP以及其上层协议的都还没有写,因为发现问题 就不得不停下来  大家帮忙看看

论坛徽章:
0
3 [报告]
发表于 2010-10-28 10:56 |只看该作者
106#         memcpy((void*)&source_ip_address,(void *)arp_protocol->arp_source_ip_address,sizeof(struct in_addr));
      #               
      #         printf("Source Ip address is :\n",inet_ntoa(source_ip_address));
      #

printf("Source Ip address is :%s\n",inet_ntoa(source_ip_address));

注意inet_ntoa这个函数返回结果存储的位置 .

论坛徽章:
0
4 [报告]
发表于 2010-10-28 10:57 |只看该作者
113# memcpy((void*)&destination_ip_address,(void *)arp_protocol->arp_destination_ip_address,sizeof(struct in_addr));
#         printf("Destination Ip address is :\n",inet_ntoa(source_ip_address));

自己认真的看

论坛徽章:
0
5 [报告]
发表于 2010-10-28 11:29 |只看该作者

  1. printf("Source Ip address is :%s\n",inet_ntoa(source_ip_address));
复制代码
打印出来的效果是 Source Ip address is :0.0.0.0

这是怎么回事阿?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2010-10-28 11:55 |只看该作者
【现 在 写 代 碼 的 都 不 带 注 視 嗎? 】
何止是注视,还要行注目礼呢。

论坛徽章:
0
7 [报告]
发表于 2010-10-28 18:13 |只看该作者
本帖最后由 shujunz 于 2010-10-28 19:50 编辑

注释掉// 注视
求代码!

论坛徽章:
0
8 [报告]
发表于 2010-10-29 09:22 |只看该作者
{:3_191:}来人啊  
  帮帮忙
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP