libpcap报文解析: ipv4、ipv6(待优化)
#include <string.h>
#include <stdlib.h>
#include <pcap.h>
#include <netinet/in.h>
#include "packet_header.h" #define MAXBYTE2CAPTURE 2048 int isprint(char c)
{
return ;
} void print_buf(u_char* pBuf, u_int32 len)
{
if (!pBuf)
{
return;
} for(int i=; i<len; i++)
{
printf("%02x ", (u_char*)pBuf[i]); if ((i% == && i!=) || i == len-)
{
printf("\r\n");
}
}
} void parse_ethII(u_char* pData, u_int32 len)
{
if (!pData || len <)
{
return;
} printf("eth II frame: \r\n");
print_buf(pData, ); /* parse src mac and dst mac */
EthHeader_t* pEth = (EthHeader_t*)pData;
printf("destination: %02x:%02x:%02x:%02x:%02x:%02x ",
pEth->dest_hwaddr[],
pEth->dest_hwaddr[],
pEth->dest_hwaddr[],
pEth->dest_hwaddr[],
pEth->dest_hwaddr[],
pEth->dest_hwaddr[]); printf("source : %02x:%02x:%02x:%02x:%02x:%02x",
pEth->source_hwaddr[],
pEth->source_hwaddr[],
pEth->source_hwaddr[],
pEth->source_hwaddr[],
pEth->source_hwaddr[],
pEth->source_hwaddr[]); /* parse frame type */
printf("\r\nframe type: 0x%x\r\n", ntohs(pEth->frame_type));
} void parse_ipheader(u_char* pData, u_int32 len)
{
if (!pData || len <)
{
return;
} printf("ip header: \r\n");
print_buf(pData, ); /* parse ip header */
IPHeader_t* pIpHeader = (IPHeader_t*)pData;
printf("\tversion : %02x\r\n"
"\ttos : %02x\r\n"
"\ttotal length: %d(0x%02x)\r\n"
"\tid : %d(0x%02x)\r\n"
"\tsegment flag: %d(0x%02x)\r\n"
"\tttl : %02x\r\n"
"\tprotocol : %02x\r\n"
"\tchecksum : %d(0x%02x)\r\n"
"\tsrc ip : %d.%d.%d.%d\r\n"
"\tdst ip : %d.%d.%d.%d\r\n",
pIpHeader->Ver_HLen,
pIpHeader->TOS,
ntohs(pIpHeader->TotalLen), ntohs(pIpHeader->TotalLen),
ntohs(pIpHeader->ID), ntohs(pIpHeader->ID),
ntohs(pIpHeader->Flag_Segment), ntohs(pIpHeader->Flag_Segment),
pIpHeader->TTL,
pIpHeader->Protocol,
ntohs(pIpHeader->Checksum), ntohs(pIpHeader->Checksum),
pIpHeader->SrcIP[],pIpHeader->SrcIP[],pIpHeader->SrcIP[],pIpHeader->SrcIP[],
pIpHeader->DstIP[],pIpHeader->DstIP[],pIpHeader->DstIP[],pIpHeader->DstIP[]);
} void parse_ip6header(u_char* pData, u_int32 len)
{
if (!pData || len <)
{
return;
} printf("ipv6 header: \r\n");
print_buf(pData, ); /* parse ipv6 header */
IPv6Header_t* pIpv6Header = (IPv6Header_t*)pData;
printf("\tversion : %x\r\n"
"\ttraffic class : %x\r\n"
"\tflow label : %x\r\n"
"\tpayload length : %x\r\n"
"\tnext header : %x\r\n"
"\thop limit : %x\r\n"
"\tsource : %x\r\n"
"\tdestination : %x\r\n",
pIpv6Header->ip6_ctlun.ip6_un2_vfc,
pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_flow,
pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_flow,
pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_plen,
pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_nxt,
pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_hlim,
pIpv6Header->ip6_src,
pIpv6Header->ip6_dst);
} void parse_packet(const u_char* packet, u_int32 len)
{
u_short ftype = ; if (!packet)
{
return ;
} u_char* pMbuf = (u_char*)packet;
parse_ethII(pMbuf, len); ftype = ntohs(((EthHeader_t*)pMbuf)->frame_type);
switch(ftype)
{
case 0x0800: /* ipv4 */
pMbuf = (u_char*)packet + ;
parse_ipheader(pMbuf, len-);
break;
case 0x86dd: /* ipv6 */
pMbuf = (u_char*)packet + ;
parse_ip6header(pMbuf, len-);
break;
default:
printf("frame type : 0x%x\r\n", ftype);
break;
} printf("\r\n");
} void processPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
int i = , *counter = (int *)arg; printf("--------------------------------------------\r\n");
printf("Packet Count: %d\n", ++(*counter));
printf("Received Packet Size: %d\n", pkthdr->len);
printf("Payload:\n"); #if 1
for (i = ; i < pkthdr->len; i++)
{
if (isprint(packet[i]))
{
printf("%02d ", packet[i]);
}
else
{
printf("%02x ", packet[i]);
} if ((i % == && i != ) || i == pkthdr->len-)
{
printf("\n");
} }
#endif parse_packet(packet, pkthdr->len); return;
} int main()
{ int i = , count = ;
pcap_t *descr = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device = NULL;
memset(errbuf, , PCAP_ERRBUF_SIZE); /* Get the name of the first device suitable for capture */
device = pcap_lookupdev(errbuf);
if (!device)
{
printf("Open device failed.");
return -;
} printf("Opening device %s\n", device); /* Open device in promiscuous mode */
descr = pcap_open_live(device, MAXBYTE2CAPTURE, , , errbuf); /* Loop forever & call processPacket() for every received packet */
pcap_loop(descr, -, processPacket, (u_char *)&count); return ;
}
#ifndef PACKET_HEADER_H
#define PACKET_HEADER_H #ifndef u_char
#define u_char unsigned char
#endif #ifndef u_int8
#define u_int8 unsigned char
#endif #ifndef u_int16
#define u_int16 unsigned short
#endif #ifndef u_int32
#define u_int32 unsigned int
#endif #ifndef u_int64
#define u_int64 unsigned long long
#endif #ifndef u_short
#define u_short unsigned short
#endif /* 以太帧头 */
typedef struct tagEthHeader_t
{
//Pcap捕获的数据帧头
u_int8 dest_hwaddr[]; //目的MAC地址
u_int8 source_hwaddr[]; //源MAC地址
u_short frame_type; //帧类型
}EthHeader_t; //IP数据报头
typedef struct tagIPHeader_t
{
//IP数据报头
u_int8 Ver_HLen; //版本+报头长度
u_int8 TOS; //服务类型
u_int16 TotalLen;//总长度
u_int16 ID; //标识
u_int16 Flag_Segment; //标志+片偏移
u_int8 TTL; //生存周期
u_int8 Protocol; //协议类型
u_int16 Checksum;//头部校验和
u_int8 SrcIP[]; //源IP地址
u_int8 DstIP[]; //目的IP地址
} IPHeader_t; //IPv6基本首部
#if 0
typedef struct tagIPv6Header_t
{
u_char version:; // 4-bit版本号
u_char traffic_class:; // 8-bit流量等级
u_int32 label:; // 20-bit流标签
u_short payload_len; // 16-bit 载荷长度
u_char next_header; // 8-bit 下一首部
u_char hop_limit; // 8-bit 跳数限制
struct
{
u_int64 prefix_subnetid;
u_char interface_id[];
} src_ip; // 128-bit 源地址
struct
{
u_int64 prefix_subnetid;
u_char interface_id[];
} dst_ip; // 128-bit 目的地址 } IPv6Header_t; typedef struct in6_addr {
union {
u_char Byte[];
u_short Word[];
} u;
} IN6_ADDR, *PIN6_ADDR, FAR *LPIN6_ADDR; #endif typedef struct tagIPv6Header_t
{
union
{
struct ip6_hdrctl
{
u_int32_t ip6_unl_flow;/* 4位的版本,8位的传输与分类,20位的流标识符 */
u_int16_t ip6_unl_plen;/* 报头长度 */
u_int8_t ip6_unl_nxt; /* 下一个报头 */
u_int8_t ip6_unl_hlim; /* 跨度限制 */
}ip6_unl ; u_int8_t ip6_un2_vfc;/* 4位的版本号,跨度为4位的传输分类 */
}ip6_ctlun ; #define ip6_vfc ip6_ctlun.ip6_un2_vfc
#define ip6_flow ip6_ctlun.ip6_unl.ip6_unl_flow
#define ip6_plen ip6_ctlun.ip6_unl.ip6_unl_plen
#define ip6_nxt ip6_ctlun.ip6_unl.ip6_unl_nxt
#define ip6_hlim ip6_ctlun.ip6_unl.ip6_unl_hlim
#define ip6_hops ip6_ctlun.ip6_unl.ip6_unl_hops struct in6_addr ip6_src;/* 发送端地址 */
struct in6_addr ip6_dst;/* 接收端地址 */
}IPv6Header_t; //TCP数据报头
typedef struct tagTCPHeader_t
{
//TCP数据报头
u_int16 SrcPort; //源端口
u_int16 DstPort; //目的端口
u_int32 SeqNO; //序号
u_int32 AckNO; //确认号
} TCPHeader_t; #endif
libpcap报文解析: ipv4、ipv6(待优化)的更多相关文章
- libpcap报文解析: ipv4、ipv6 @ 2014.7.2
#include <string.h> #include <stdlib.h> #include <pcap.h> #include <stdio.h> ...
- 解析IPV4报文 和IPV6 报文的 checksum
解析IPV4报文和IPV6报文的checksum的算法: 校验和(checksum)算法,简单的说就是16位累加的反码运算: 计算函数如下: 我们在计算时是主机字节序,计算的结果封装成IP包时是网络字 ...
- Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小
Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一般在内网环境中,我们几乎是用不到IPV6,因此我们没有必要把多不 ...
- win10 localhost 解析为 ipv6地址 ::1 的解决办法
今天遇到个奇怪的问题,localhost 访问时提示 not found 404,但是有127.0.0.1可以访问.最后找到原因,是因为 windows 把 localhost 解析为 ipv6 地址 ...
- 基于DPI(深度报文解析)的应用识别
一.概述 1.DPI(Deep packet inspection,深度报文解析) 所谓“深度”是和普通的报文分析层次相比较而言的,“普通报文检测”仅分析IP包4 层以下的内容,包括源地址.目的地址. ...
- IP协议/地址(IPv4&IPv6)概要
IP协议/地址(IPv4&IPv6)概要 IP协议 什么是IP协议 IP是Internet Protocol(网际互连协议)的缩写,是TCP/IP体系中的网络层协议. [1] 协议的特征 无连 ...
- 报文解析及CRC类
/// <summary> /// 报文解析转换类 /// </summary> public class DatagramConvert { public static En ...
- IPv4&IPv6双重协议栈
IPV4 TCP客户与IPV6服务器之间的通信: 1 启动IPV6服务器,创建套接监听口,绑定通配地址 2 IPV4调用gethostbyname找到该服务器对应的A记录 3 调用connect,向服 ...
- ISO8583报文解析
在此只写了一个8583报文的拆包,组包其实也差不多的. 不多说直接上文件, 具体思路过程,在解析类里面写的有. 其中包含了四个文件 8583resp.txt报文 ISO8583medata配置文件 B ...
随机推荐
- 转载__Android-屏幕适配需要注意的地方
1.尽量使用线性布局(LinearLayout)和相对布局(RelativeLayout),不要使用绝对布局. 2.尽量使用dip和sp,不要使用px. 3.为不同的分辨率提供不同的布局文件和图片. ...
- gerrit 配置 apache2 反向代理(转载)
Apache 2 Configuration To run Gerrit behind an Apache server using mod_proxy, enable the necessary A ...
- linux内核hash list
源码: #ifndef _LINUX_HLIST_H #define _LINUX_HLIST_H /* * Double linked lists with a single pointer lis ...
- CodeForces 617C【序枚举】
题意: 有两个点喷水,有很多个点有花,给出坐标. 求使得每个花都可以被喷到,两个喷水的半径的平方的和最小是多少. 思路: 枚举其中一个喷水的最大半径. 坑: 这题我贪心的思路有很大问题.一开始也是想这 ...
- poj 2240 Arbitrage bellman-ford算法
点击打开链接 Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13434 Accepted: 5657 ...
- ubuntu14.04离线配置cm5.5.1
cd /opt/cm-5.5.1/具体参考http://www.aboutyun.com/thread-10852-1-1.html 离线安装:下载所需文件 http://archive.cloude ...
- vmstat 命令详解 转载
vmstat 命令详解 procs:r-->在运行队列中等待的进程数b-->在等待io的进程数w-->可以进入运行队列但被替换的进程 memoyswap-->现时可用的交换 ...
- 共享一个MVC通过NPOI导出excel的通用方法
public static System.IO.MemoryStream ExportExcel<T>(string title, List<T> objList, param ...
- linux tar 命令 --致力于“一眼看懂,随手就用”的随笔
基本玩法: 压缩: tar -czf txt.tar.gz *.txt // 将当前目录下的所有txt文件,创建一个tar包,并用gzip算法,压缩成txt.tar.gz 文件 解压: tar -xz ...
- C++编译错误syntax error : identifier 'THIS_FILE' 解决方法
你到代码里搜索 THIS_FILE看是不是它定义在别的头文件前面了,如果是,把它们的头文件紧跟到Stdafx.h后面 我遇到过这问题,这样搞定的 今天遇到个编译错误:..\vc98\include\n ...