libpcap报文解析: ipv4、ipv6(待优化)

本文详细探讨了使用libpcap库解析IPv4和IPv6报文的方法,涵盖了捕获网络数据包、解析头部信息以及关键字段的解读。通过对libpcap API的深入剖析,读者将能够更好地理解和处理网络抓包数据。
摘要由CSDN通过智能技术生成
  1 #include <string.h>
  2 #include <stdlib.h>
  3 #include <pcap.h>
  4 #include <netinet/in.h>
  5 #include "packet_header.h"
  6 
  7 #define MAXBYTE2CAPTURE 2048
  8 
  9 int isprint(char c)
 10 {
 11     return 0;
 12 }
 13 
 14 void print_buf(u_char* pBuf, u_int32 len)
 15 {
 16     if (!pBuf)
 17     {
 18         return;
 19     }
 20 
 21     for(int i=0; i<len; i++)
 22     {
 23         printf("%02x ",  (u_char*)pBuf[i]);
 24 
 25         if ((i%16 == 0 && i!=0) || i == len-1)
 26         {
 27             printf("\r\n");
 28         }
 29     }
 30 }
 31 
 32 void parse_ethII(u_char* pData, u_int32 len)
 33 {
 34     if (!pData || len <14)
 35     {
 36         return;
 37     }
 38 
 39     printf("eth II frame: \r\n");
 40     print_buf(pData, 14);
 41 
 42     /* parse src mac and dst mac */
 43     EthHeader_t* pEth = (EthHeader_t*)pData;
 44     printf("destination: %02x:%02x:%02x:%02x:%02x:%02x ",
 45         pEth->dest_hwaddr[0],
 46         pEth->dest_hwaddr[1],
 47         pEth->dest_hwaddr[2],
 48         pEth->dest_hwaddr[3],
 49         pEth->dest_hwaddr[4],
 50         pEth->dest_hwaddr[5]);
 51 
 52     printf("source : %02x:%02x:%02x:%02x:%02x:%02x",
 53         pEth->source_hwaddr[0],
 54         pEth->source_hwaddr[1],
 55         pEth->source_hwaddr[2],
 56         pEth->source_hwaddr[3],
 57         pEth->source_hwaddr[4],
 58         pEth->source_hwaddr[5]);
 59 
 60     /* parse frame type */
 61     printf("\r\nframe type: 0x%x
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值