libpcap报文解析: ipv4、ipv6 @ 2014.7.2
#include <string.h>
#include <stdlib.h>
#include <pcap.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>
#include "packet_header.h"
#include <iostream>
#include <string>
using namespace std; #define MAXBYTE2CAPTURE 2048 pthread_t g_thread[];
pthread_mutex_t g_mutex; 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;
} void* thread_recv_pkt(void *)
{
while()
{
cout << "recv pkt: " << endl;
sleep();
}
} void* thread_send_pkt(void *)
{
while ()
{
cout << "send pkt: " << endl;
sleep();
}
} int create_pkt_process_task()
{
int ret = ; memset(&g_thread, , sizeof(g_thread)); ret = pthread_create(&g_thread[], NULL, thread_send_pkt, NULL);
if ( == ret)
{
cout << "packet send thread create successfully." << endl;
}
else
{
cout << "packet send thread create failed." << endl;
} ret = pthread_create(&g_thread[], NULL, thread_recv_pkt, NULL);
if ( == ret)
{
cout << "packet send thread create successfully." << endl;
}
else
{
cout << "packet send thread create failed." << endl;
} return ;
} void pkt_process_task_wait()
{
if(g_thread[] !=)
{ //comment4
pthread_join(g_thread[],NULL);
printf("线程1 已经结束\n");
} if(g_thread[] !=)
{ //comment5
pthread_join(g_thread[],NULL);
printf("线程2 已经结束\n");
}
} int main()
{ int i = , count = ;
pcap_t *descr = NULL;
char errbuf[PCAP_ERRBUF_SIZE], *device = NULL; memset(errbuf, , PCAP_ERRBUF_SIZE); create_pkt_process_task();
pkt_process_task_wait(); /* 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 ;
}
libpcap报文解析: ipv4、ipv6 @ 2014.7.2的更多相关文章
- libpcap报文解析: ipv4、ipv6(待优化)
#include <string.h> #include <stdlib.h> #include <pcap.h> #include <netinet/in. ...
- 解析IPV4报文 和IPV6 报文的 checksum
解析IPV4报文和IPV6报文的checksum的算法: 校验和(checksum)算法,简单的说就是16位累加的反码运算: 计算函数如下: 我们在计算时是主机字节序,计算的结果封装成IP包时是网络字 ...
- win10 localhost 解析为 ipv6地址 ::1 的解决办法
今天遇到个奇怪的问题,localhost 访问时提示 not found 404,但是有127.0.0.1可以访问.最后找到原因,是因为 windows 把 localhost 解析为 ipv6 地址 ...
- 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 ...
- LwIP Application Developers Manual10---LwIP IPv4/IPv6 stacks
1.前言 lwIP正在加入IPv6,一个实验性的版本可以通过git下载,该版本实现了一个IPv4/IPv6的双协议栈.通过在lwipopts.h定义LWIP_IPV6可以使能IPv6 2.已实现的IP ...
- 基于DPI(深度报文解析)的应用识别
一.概述 1.DPI(Deep packet inspection,深度报文解析) 所谓“深度”是和普通的报文分析层次相比较而言的,“普通报文检测”仅分析IP包4 层以下的内容,包括源地址.目的地址. ...
随机推荐
- kuangbin RMQ
这是kuangbin的RMQ,一维的,代码很简洁,附上: //kuangbin templet(查询最大值) 一维 //若想查最小,看提示更改 + ; ];//第二维是范围,即2^20约等于100万 ...
- hdu 5437 Alisha’s Party 模拟 优先队列
Problem Description Princess Alisha invites her friends to come to her birthday party. Each of her f ...
- python 判断内网IP方法及实例应用
一.初衷: 一般在CMDB里会存储一台服务器的内网IP.管理IP.电信IP.联通IP,我们在使用的时候只需要拿到其中一个外网IP地址即可.那么我们就需要判断内网IP.管理IP并剔除掉,获取第一个外网I ...
- imageView--转-如何代码动态的获取和设置ImageView的宽度和高度?
11- 12- 13-android获取图片原始长宽; ========= 11--国内某金融企业的一道面试题:如何在代码中获得一个ImageVIew的高度和宽度? 相信很大一部分人,会直接想到用im ...
- 将博客搬至51CTO
为了统一博客文章,将文章搬至51cto个人博客
- phpStudy(lnmp)集成环境安装
phpStudy phpStudy » PHP教程 » phpStudy for Linux (lnmp+lamp一键安装包) phpStudy for Linux (lnmp+lamp一键安装包) ...
- 关于 MySQL UTF8 编码下生僻字符插入失败/假死问题的分析
原文:http://my.oschina.net/leejun2005/blog/343353 目录[-] 1.问题:mysql 遇到某些中文插入异常 2.原因:此 utf8 非彼 utf8 3.解决 ...
- SPR EAD NET 6
SPR EAD_NET6 下载地址 http://www.gcpowertools.com.cn/downloads/trial/Spread.NET/EN_SPREAD_NET6_SETUP_RA_ ...
- python 批量重命名文件后缀
# batch_file_rename.py # Created: 6th August 2012 ''' This will batch rename a group of files in a g ...
- C++ 多继承和虚继承的内存布局(转)
转自:http://www.oschina.net/translate/cpp-virtual-inheritance 警告. 本文有点技术难度,需要读者了解C++和一些汇编语言知识. 在本文中,我们 ...