当前位置: 编程技术>综合
本页文章导读:
▪pcap文件解析--pcap文件头与包文件头(一) 前段时间接到一个公司关于解析pacp文件的培训(我是被培训的),在完成了一部分的功能后决定把一些关于pcap文件的了解记录到博客中。
初识Pcap文件
在开始读取pcap文件之前,先让我们来看.........
▪reinterpret_cast在编程中的应用 在某些情况下,为了适应接口类型要求,需要将int转化成uint, double转化成int64_t等,但是经过这种转化之后可能会有数据损失。
如果只是为了适应接口,最终还是希望读出原始数据,那么可以.........
▪onblur和onclick冲突
文本框给定一个onblur事件,按钮给定一个onclick事件,
这样的话,当点击按钮之后,文本框失去事件触发,但是按钮的onclick事件却触发不了
原因如下:
onclick 相当于 在某一元素上触.........
[1]pcap文件解析--pcap文件头与包文件头(一)
来源: 互联网 发布时间: 2013-11-10
前段时间接到一个公司关于解析pacp文件的培训(我是被培训的),在完成了一部分的功能后决定把一些关于pcap文件的了解记录到博客中。
初识Pcap文件在开始读取pcap文件之前,先让我们来看看Pcap文件的大概结构。
如上图所示在一个Pcap文件中存在1个Pcap文件头和多个数据包,其中每个数据包都有自己的头和包内容。
下面我们先看看PCAP文件头每个字段是什么意思:
magic为文件识别头,pcap固定为:0xA1B2C3D4。(4个字节)
magor version为主版本号(2个字节)
minor version为次要版本号(2个字节)
timezone为当地的标准时间(4个字节)
sigflags为时间戳的精度(4个字节)
snaplen为最大的存储长度(4个字节)
linktype为链路类型(4个字节)
常用类型:
0 BSD loopback devices, except for later OpenBSD
1 Ethernet, and Linux loopback devices
6 802.5 Token Ring
7 ARCnet
8 SLIP
9 PPP
10 FDDI
100 LLC/SNAP-encapsulated ATM
101 “raw IP”, with no link
102 BSD/OS SLIP
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 later OpenBSD loopback devices (with the AF_value in network byte order)
113 special Linux “cooked” capture
114 LocalTalk
1 Ethernet, and Linux loopback devices
6 802.5 Token Ring
7 ARCnet
8 SLIP
9 PPP
10 FDDI
100 LLC/SNAP-encapsulated ATM
101 “raw IP”, with no link
102 BSD/OS SLIP
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 later OpenBSD loopback devices (with the AF_value in network byte order)
113 special Linux “cooked” capture
114 LocalTalk
数据包头则依次为:时间戳(秒)、时间戳(微妙)、抓包长度和实际长度,依次各占4个字节。
c代码:
pcap_header.h
#pragma pack( push, 1)
// 为了保证在windows和linux下都能正常编译,放弃使用INT64或者_int_64
typedef short _Int16;
typedef long _Int32;
typedef char Byte;
// Pcap文件头
struct __file_header
{
_Int32 iMagic;
_Int16 iMaVersion;
_Int16 iMiVersion;
_Int32 iTimezone;
_Int32 iSigFlags;
_Int32 iSnapLen;
_Int32 iLinkType;
};
// 数据包头
struct __pkthdr
{
_Int32 iTimeSecond;
_Int32 iTimeSS;
_Int32 iPLength;
_Int32 iLength;
};
#pragma pack( pop)
main.c
#include<stdio.h>
#include"pcap_header.h"
#include<memory.h>
#include<stdlib.h>
#include<math.h>
int main()
{
struct __pkthdr data;
struct __file_header header;
FILE* pFile = fopen( "iupsc.pcap", "rb");
if( pFile == 0)
{
printf( "打开pcap文件失败");
return 0;
}
fseek( pFile, 0, SEEK_END);
long iFileLen = ftell( pFile);
fseek( pFile, 0, SEEK_SET);
Byte* pBuffer = (Byte*)malloc( iFileLen);
fread( (void*)pBuffer, 1, iFileLen, pFile);
fclose( pFile);
memcpy( (void*)&header, (void*)(pBuffer)
, sizeof(struct __file_header));
int iIndex = sizeof(struct __file_header);
int iNo = 1;
while(iIndex <= iFileLen)
{
memcpy( (void*)&data, (void*)(pBuffer + iIndex)
, sizeof(struct __pkthdr));
char strPath[51];
sprintf( strPath, "export/%d-%d.pcap", iNo++, (int)data.iTimeSecond);
strPath[50] = '\0';
FILE* pwFile = fopen( strPath, "wb");
fwrite((void*)&header, 1, sizeof(struct __file_header), pwFile);
fwrite( (void*)(pBuffer + iIndex), 1,
sizeof(struct __pkthdr) + data.iPLength, pwFile);
fclose( pwFile);
iIndex += sizeof(struct __pkthdr) + data.iPLength;
}
free( pBuffer);
printf( "成功导出%d个文件", iNo - 1);
return 1;
}
作者:yhangleo 发表于2013-1-9 13:18:33 原文链接
阅读:47 评论:0 查看评论
[2]reinterpret_cast在编程中的应用
来源: 互联网 发布时间: 2013-11-10
在某些情况下,为了适应接口类型要求,需要将int转化成uint, double转化成int64_t等,但是经过这种转化之后可能会有数据损失。
如果只是为了适应接口,最终还是希望读出原始数据,那么可以选用reinterpret_cast。例如:
#include <stdio.h>
#include <stdint.h>
int main()
{
int64_t i = 0;
double d = 1.1;
int64_t j = reinterpret_cast<int64_t&>(d);
double j2 = reinterpret_cast<double&>(j);
int64_t k = static_cast<int64_t>(d);
double k2 = static_cast<double>(k);
printf("org=%lf, reintterpret forw(double=>int64_t)=%ld\t, reintterpret back(int64_t=>double)=%lf\n", d, j, j2);
printf("org=%lf, static forw(double=>int64_t)=%ld\t, static back(int64_t=>double)=%lf\n", d, k, k2);
}编译后的输出结果:
[xiaochu.yh@tfs035040 cpp]$ ./a.out org=1.100000, reintterpret forw(double=>int64_t)=4607632778762754458 , reintterpret back(int64_t=>double)=1.100000 org=1.100000, static forw(double=>int64_t)=1 , static back(int64_t=>double)=1.000000
可以看到,使用了static_cast之后精度数据被丢失了。而reinterpret_cast是一种二进制转化,并不关心数据的语义。
使用reinterpret的时候需要注意的是存储空间需要足够,如果将double转成int32_t,最终结果会出错。
作者:maray 发表于2013-1-9 13:56:55 原文链接
阅读:0 评论:0 查看评论
[3]onblur和onclick冲突
文本框给定一个onblur事件,按钮给定一个onclick事件,
这样的话,当点击按钮之后,文本框失去事件触发,但是按钮的onclick事件却触发不了
原因如下:
onclick 相当于 在某一元素上触发了 onmousedown(即鼠标按下)后 任然在该元素 上 触发了onmouseup(鼠标按键弹起)才触发 onclick;
对于某元素A 绑定了 click事件 并同时对另外 的元素B 绑定onblur事件, 这时候,当在A上mousedown后,即触发了B元素的onblur事件,该事件函数执行后的效果是 改变了DOM结构,使得鼠标已经不在在元素A之上。 这时鼠标任然没有mouseup ,在mouseup之后,以为会触发click事件,实际上却不能触发。
解决方法:
在按扭上加个onmouseover函数,里面的代码是把焦点移到按扭上,这样很合乎情理,顺便加个鼠标放到提交按扭上时按钮样式变化的特效就更完美了。
已有 1 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
最新技术文章: