对MAC、IP、TCP等的数据包进行抓取分析其结构,使用了pcap库
1.mac.h
#include <string>
#include <iostream>
#include <cstdio>
class PMacHeader
{
private:
const unsigned char *m;
std::string dst, src, type, tpname;
public:
PMacHeader(const unsigned char *p = 0){m = p;}
~PMacHeader(){m = 0;}
void ResetPt(const unsigned char * p){m = p;}
unsigned short GetProtocol();
std::string& GetDstStr();
std::string& GetSrcStr();
};
2.mac.cpp
#include "stdafx.h"
#include "mac.h"
unsigned short PMacHeader::GetProtocol()
{
unsigned short thetype;
thetype = (unsigned short)m[12] << 8;
thetype += (unsigned short)m[12+1];
return thetype;
}
std::string& PMacHeader::GetDstStr()
{
char dstBuf[6*3+1];
std::sprintf(dstBuf, "%.2x %.2x %.2x %.2x %.2x %.2x ", m[0], m[1], m[2], m[3], m[4], m[5]);
dst = dstBuf;
return dst;
}
std::string& PMacHeader::GetSrcStr()
{
char srcBuf[6*3+1];
std::sprintf(srcBuf,"%.2x %.2x %.2x %.2x %.2x %.2x ", m[6], m[7], m[8], m[9], m[10], m[11]);
src = srcBuf;
return src;
}
3.ip.h#include <string>
#include <iostream>
#include <cstdio>
class PIpHeader
{
private:
const unsigned char *p;
std::string src, dst;
public:
PIpHeader(const unsigned char *pt = 0):p(pt){}
~PIpHeader(){p = 0;}
void Reset(const unsigned char *pt){ p = pt;}
short GetVesion();//取IP的版本
short GetHeaderLen();//取IP报头的长度 (1单位/4字节)
short GetServer();//服务
unsigned short GetTotalLen();//总长度
unsigned short GetID();//标识
short GetFlag();
short GetEx();//偏移量
short GetTTL();
unsigned short GetProtocol();
unsigned short GetCheck();//校验码
std