libpcap报文解析: ipv4、ipv6 @ 2014.7.2

  1 #include <string.h>
  2 #include <stdlib.h>
  3 #include <pcap.h>
  4 #include <stdio.h>
  5 #include <sys/time.h>
  6 #include <unistd.h>
  7 #include <netinet/in.h>
  8 #include <pthread.h>
  9 #include "packet_header.h"
 10 #include <iostream>
 11 #include <string>
 12 using namespace std;
 13
 14 #define MAXBYTE2CAPTURE 2048
 15
 16 pthread_t g_thread[2];
 17 pthread_mutex_t g_mutex;
 18
 19 int isprint(char c)
 20 {
 21     return 0;
 22 }
 23
 24 void print_buf(u_char* pBuf, u_int32 len)
 25 {
 26     if (!pBuf)
 27     {
 28         return;
 29     }
 30
 31     for(int i=0; i<len; i++)
 32     {
 33         printf("%02x ",  (u_char*)pBuf[i]);
 34
 35         if ((i%16 == 0 && i!=0) || i == len-1)
 36         {
 37             printf("\r\n");
 38         }
 39     }
 40 }
 41
 42 void parse_ethII(u_char* pData, u_int32 len)
 43 {
 44     if (!pData || len <14)
 45     {
 46         return;
 47     }
 48
 49     printf("eth II frame: \r\n");
 50     print_buf(pData, 14);
 51
 52     /* parse src mac and dst mac */
 53     EthHeader_t* pEth = (EthHeader_t*)pData;
 54     printf("destination: %02x:%02x:%02x:%02x:%02x:%02x ",
 55         pEth->dest_hwaddr[0],
 56         pEth->dest_hwaddr[1],
 57         pEth->dest_hwaddr[2],
 58         pEth->dest_hwaddr[3],
 59         pEth->dest_hwaddr[4],
 60         pEth->dest_hwaddr[5]);
 61
 62     printf("source : %02x:%02x:%02x:%02x:%02x:%02x",
 63         pEth->source_hwaddr[0],
 64         pEth->source_hwaddr[1],
 65         pEth->source_hwaddr[2],
 66         pEth->source_hwaddr[3],
 67         pEth->source_hwaddr[4],
 68         pEth->source_hwaddr[5]);
 69
 70     /* parse frame type */
 71     printf("\r\nframe type: 0x%x\r\n", ntohs(pEth->frame_type));
 72 }
 73
 74
 75 void parse_ipheader(u_char* pData, u_int32 len)
 76 {
 77     if (!pData || len <14)
 78     {
 79         return;
 80     }
 81
 82     printf("ip header: \r\n");
 83     print_buf(pData, 20);
 84
 85     /* parse ip header */
 86     IPHeader_t* pIpHeader = (IPHeader_t*)pData;
 87     printf("\tversion     : %02x\r\n"
 88            "\ttos         : %02x\r\n"
 89            "\ttotal length: %d(0x%02x)\r\n"
 90            "\tid          : %d(0x%02x)\r\n"
 91            "\tsegment flag: %d(0x%02x)\r\n"
 92            "\tttl         : %02x\r\n"
 93            "\tprotocol    : %02x\r\n"
 94            "\tchecksum    : %d(0x%02x)\r\n"
 95            "\tsrc ip      : %d.%d.%d.%d\r\n"
 96            "\tdst ip      : %d.%d.%d.%d\r\n",
 97         pIpHeader->Ver_HLen,
 98         pIpHeader->TOS,
 99         ntohs(pIpHeader->TotalLen), ntohs(pIpHeader->TotalLen),
100         ntohs(pIpHeader->ID), ntohs(pIpHeader->ID),
101         ntohs(pIpHeader->Flag_Segment), ntohs(pIpHeader->Flag_Segment),
102         pIpHeader->TTL,
103         pIpHeader->Protocol,
104         ntohs(pIpHeader->Checksum), ntohs(pIpHeader->Checksum),
105         pIpHeader->SrcIP[0],pIpHeader->SrcIP[1],pIpHeader->SrcIP[2],pIpHeader->SrcIP[3],
106         pIpHeader->DstIP[0],pIpHeader->DstIP[1],pIpHeader->DstIP[2],pIpHeader->DstIP[3]);
107 }
108
109 void parse_ip6header(u_char* pData, u_int32 len)
110 {
111     if (!pData || len <14)
112     {
113         return;
114     }
115
116     printf("ipv6 header: \r\n");
117     print_buf(pData, 40);
118
119     /* parse ipv6 header */
120     IPv6Header_t* pIpv6Header = (IPv6Header_t*)pData;
121     printf("\tversion           : %x\r\n"
122            "\ttraffic class     : %x\r\n"
123            "\tflow label        : %x\r\n"
124            "\tpayload length    : %x\r\n"
125            "\tnext header       : %x\r\n"
126            "\thop limit         : %x\r\n"
127            "\tsource            : %x\r\n"
128            "\tdestination       : %x\r\n",
129            pIpv6Header->ip6_ctlun.ip6_un2_vfc,
130            pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_flow,
131            pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_flow,
132            pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_plen,
133            pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_nxt,
134            pIpv6Header->ip6_ctlun.ip6_unl.ip6_unl_hlim,
135            pIpv6Header->ip6_src,
136            pIpv6Header->ip6_dst);
137 }
138
139
140 void parse_packet(const u_char* packet, u_int32 len)
141 {
142     u_short ftype = 0;
143
144     if (!packet)
145     {
146         return ;
147     }
148
149     u_char* pMbuf = (u_char*)packet;
150     parse_ethII(pMbuf, len);
151
152     ftype = ntohs(((EthHeader_t*)pMbuf)->frame_type);
153     switch(ftype)
154     {
155         case 0x0800:  /* ipv4 */
156             pMbuf = (u_char*)packet + 14;
157             parse_ipheader(pMbuf, len-14);
158             break;
159         case 0x86dd: /* ipv6 */
160             pMbuf = (u_char*)packet + 14;
161             parse_ip6header(pMbuf, len-14);
162             break;
163         default:
164             printf("frame type : 0x%x\r\n", ftype);
165             break;
166     }
167
168     printf("\r\n");
169 }
170
171 void processPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
172 {
173     int i = 0, *counter = (int *)arg;
174
175     printf("--------------------------------------------\r\n");
176     printf("Packet Count: %d\n", ++(*counter));
177     printf("Received Packet Size: %d\n", pkthdr->len);
178     printf("Payload:\n");
179
180 #if 1
181     for (i = 0; i < pkthdr->len; i++)
182     {
183         if (isprint(packet[i]))
184         {
185             printf("%02d ", packet[i]);
186         }
187         else
188         {
189             printf("%02x ", packet[i]);
190         }
191
192         if ((i % 16 == 0 && i != 0) || i == pkthdr->len-1)
193         {
194             printf("\n");
195         }
196
197     }
198 #endif
199
200     parse_packet(packet, pkthdr->len);
201
202     return;
203 }
204
205
206 void* thread_recv_pkt(void *)
207 {
208     while(1)
209     {
210         cout << "recv pkt: " << endl;
211         sleep(1);
212     }
213 }
214
215 void* thread_send_pkt(void *)
216 {
217     while (1)
218     {
219         cout << "send pkt: " << endl;
220         sleep(1);
221     }
222 }
223
224 int create_pkt_process_task()
225 {
226     int ret = 0;
227
228     memset(&g_thread, 0, sizeof(g_thread));
229
230     ret = pthread_create(&g_thread[0], NULL, thread_send_pkt, NULL);
231     if (0 == ret)
232     {
233         cout << "packet send thread create successfully." << endl;
234     }
235     else
236     {
237         cout << "packet send thread create failed." << endl;
238     }
239
240     ret = pthread_create(&g_thread[1], NULL, thread_recv_pkt, NULL);
241     if (0 == ret)
242     {
243         cout << "packet send thread create successfully." << endl;
244     }
245     else
246     {
247         cout << "packet send thread create failed." << endl;
248     }
249
250     return 0;
251 }
252
253 void pkt_process_task_wait()
254 {
255     if(g_thread[0] !=0)
256     {                   //comment4
257        pthread_join(g_thread[0],NULL);
258        printf("线程1 已经结束\n");
259     }
260
261     if(g_thread[1] !=0)
262     {                //comment5
263         pthread_join(g_thread[1],NULL);
264         printf("线程2 已经结束\n");
265     }
266 }
267
268 int main()
269 {
270
271     int i = 0, count = 0;
272     pcap_t *descr = NULL;
273     char errbuf[PCAP_ERRBUF_SIZE], *device = NULL;
274
275     memset(errbuf, 0, PCAP_ERRBUF_SIZE);
276
277     create_pkt_process_task();
278     pkt_process_task_wait();
279
280     /* Get the name of the first device suitable for capture */
281     device = pcap_lookupdev(errbuf);
282     if (!device)
283     {
284         printf("Open device failed.");
285         return -1;
286     }
287
288     printf("Opening device %s\n", device);
289
290     /* Open device in promiscuous mode */
291     descr = pcap_open_live(device, MAXBYTE2CAPTURE, 1, 512, errbuf);
292
293     /* Loop forever & call processPacket() for every received packet */
294     pcap_loop(descr, -1, processPacket, (u_char *)&count);
295
296     return 0;
297 }

libpcap报文解析: ipv4、ipv6 @ 2014.7.2

时间: 2024-07-29 05:01:09

libpcap报文解析: ipv4、ipv6 @ 2014.7.2的相关文章

内核参数优化之2-1 tcp/ip 标志位报文解析

以下内容纯属虚构,切勿轻易相信! 众所周知,tcp/ip三次握手和四次挥手,均由syn/ack/fin三个标志位报文决定,但是这三个标志位报文,并不是说在构建连接的时候只发送一次的,因为协议不知道网络状况. 故而就存在了以下参数,可以调节发送次数 net.ipv4.tcp_syn_retries 这个参数从字面上来看就是syn标志位报文的重试次数,什么时候发送syn标志位呢?三次握手中,请求端第一次构建连接的时候,默认是5次,但是对于一个处于网络状况好的请 求端,5次显然是多了,因此,我们来个2

通用报文解析服务的演进之路(基于磁盘目录的分布式消息消费者服务)之一

通用报文解析服务,用C#开发,经历了三版更新,支撑起了关区内的绝大多数数据交换业务,截止至今,每日收发报文约20万,数据量约5G,平均延迟在1分钟内. 回想起那些半夜处理积压报文的场景,不胜唏嘘,决定把这个演进过程向大家讲述一下.回顾历史,展望未来,如果能给大家一些启发,是再好不过的了. 由于某些历史和非历史原因,我们的数据交换在已经有IBMMQ等中间件做支撑的情况下,还需要将报文落地到磁盘目录下再做下一步解析.入库.因此就有了这么一个需求,基于磁盘目录的报文解析服务. 初步计划,按照演进过程中

win10 localhost 解析为 ipv6地址 ::1 的解决办法

localhost 访问时提示 not found 404,但是有127.0.0.1可以访问.最后找到原因,是因为 windows 把 localhost 解析为 ipv6 地址 ::1 而不是 127.0.0.1.查了hosts 把ipv6已经屏蔽了,127.0.0.1 localhost 也有,就是不能正常解析. 解决办法: 打开注册表,找到键 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\tcpip6\Parameters,添加类

基于DPI(深度报文解析)的应用识别

一.概述 1.DPI(Deep packet inspection,深度报文解析) 所谓"深度"是和普通的报文分析层次相比较而言的,"普通报文检测"仅分析IP包4 层以下的内容,包括源地址.目的地址.源端口.目的端口以及协议类型,而DPI 除了对前面的层次分析外,还增加了应用层分析,识别各种应用及其内容,主要实现一下功能: 1)应用分析--网络流量构成分析.性能分析.流向分析等: 2)用户分析--用户群区分.行为分析.终端分析.趋势分析等: 3)网元分析--根据区域

移动支付平台间接口报文解析技术核心架构实现、及平台交易处理项目全程实录教程

<基于移动支付平台间接口报文解析技术核心架构实现.及平台交易处理项目全程实录>课程讲师:MoMo 课程分类:Java框架适合人群:中级课时数量:52课时用到技术:JavaBean .Spring3.X. SpringMVC. Hibernate3.X.Apache HttpClient 3.x.JUnit4.x.自定义Annotation + java反射技术涉及项目:移动支付平台间接口咨询QQ:1337192913 课程介绍:   本课程抛开理论.以项目为驱动,适用于初次接触报文收发.组装解

HTTP报文解析

//我是一个Post请求,我告诉服务器:我要http://localhost:1538/WebForm1.aspx页面,我支持的httpb版本是1.1 POST http://localhost:1538/WebForm1.aspx HTTP/1.1 ----------------------------------------------------------------------------------------------------- Accept: text/html, app

移动支付平台间接口报文解析核心架构及平台交易全程实录

移动支付平台间接口报文解析核心架构及平台交易全程实录 (HttpClient+SpringMVC+Spring3+Hibernate3+自定义Annotation) 课程分类:Java框架 适合人群:中级 课时数量:52课时 用到技术:JavaBean .Spring3.X. SpringMVC. Hibernate3.X.Apache HttpClient 3.x.JUnit4.x.自定义Annotation + java反射技术 涉及项目:移动支付平台间接口 咨询qq:1840215592

第29篇ip地址,mac地址 IPV4 IPV6 TCP UDP协议

回顾 2018-12-31 或者 2018.12.31 或者 2018*12*31 的正则表达式: [1-9]\d{3}(?P<sep>.)(1[12]|0?[1-9])(?P=sep)([12]\d|3[01]|0?[1-9])内容总览: ip地址 mac地址 IPV4 IPV6 TCP UDP协议 同一台机器的两个程序通讯-->文件 两台机器的两个程序之间通讯 -->网络 mac 每一台计算机的网卡 上面会有一个mac地址,也就是相当于改计算机在网络上的唯一身份表示 xx-xx

mysql IPv4 IPv6

w如何通过一个mysql方法,而不是借助脚本判断?INET6_ATON(expr) https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-aton Given an IPv6 or IPv4 network a