开发中正好用到,这个知识点先前不曾接触过,mark下。
首先贴下一个协议定义的头文件(可下载),该文件中定义了MAC头,IP头,TCP/UDP头。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <inttypes.h> #include <sys/types.h> #ifndef _PROTOCOL_H_ #define _PROTOCOL_H_ //MAC header typedef struct { unsigned char DesMacAddr[6]; unsigned char SrcMacAddr[6]; short LengthOrType; }__attribute__((packed)) MAC_HEADER, *PMAC_HEADER; //IP header typedef struct { unsigned char hdr_len :4; unsigned char version :4; unsigned char tos; unsigned short total_len; unsigned short identifier; unsigned short frag_and_flags; unsigned char ttl; unsigned char protocol; unsigned short checksum; unsigned int source_ip; unsigned int dest_ip; }__attribute__((packed)) IP_HEADER, *PIP_HEADER; /*TCP*/ typedef struct _TCP_HEADER { short m_sSourPort; short m_sDestPort; unsigned int m_uiSequNum; unsigned int m_uiAcknowledgeNum; short m_sHeaderLenAndFlag; short m_sWindowSize; short m_sCheckSum; short m_surgentPointer; }__attribute__((packed)) TCP_HEADER, *PTCP_HEADER; typedef struct _TCP_OPTIONS { char m_ckind; char m_cLength; char m_cContext[32]; }__attribute__((packed))TCP_OPTIONS, *PTCP_OPTIONS; typedef struct _UDP_HEADER { unsigned short m_usSourPort; unsigned short m_usDestPort; unsigned short m_usLength; unsigned short m_usCheckSum; }__attribute__((packed)) UDP_HEADER, *PUDP_HEADER; #endif //_PROTOCOL_H_ /////////////////////////////////////////////////////////
一、MAC帧头定义
/*数据帧定义,头14个字节,尾4个字节*/ typedef struct _MAC_FRAME_HEADER { char m_cDstMacAddress[6]; //目的mac地址 char m_cSrcMacAddress[6]; //源mac地址 short m_cType; //上一层协议类型,如0x0800代表上一层是IP协议,0x0806为arp }__attribute__((packed))MAC_FRAME_HEADER,*PMAC_FRAME_HEADER; typedef struct _MAC_FRAME_TAIL { unsigned int m_sCheckSum; //数据帧尾校验和 }__attribute__((packed))MAC_FRAME_TAIL, *PMAC_FRAME_TAIL;
二、IP头结构的定义
/*IP头定义,共20个字节*/ typedef struct _IP_HEADER { char m_cVersionAndHeaderLen; //版本信息(前4位),头长度(后4位) char m_cTypeOfService; // 服务类型8位 short m_sTotalLenOfPacket; //数据包长度 short m_sPacketID; //数据包标识 short m_sSliceinfo; //分片使用 char m_cTTL; //存活时间 char m_cTypeOfProtocol; //协议类型 short m_sCheckSum; //校验和 unsigned int m_uiSourIp; //源ip unsigned int m_uiDestIp; //目的ip } __attribute__((packed))IP_HEADER, *PIP_HEADER ;
三、tcp头结构定义
/*TCP头定义,共20个字节*/ typedef struct _TCP_HEADER { short m_sSourPort; // 源端口号16bit short m_sDestPort; // 目的端口号16bit unsigned int m_uiSequNum; // 序列号32bit unsigned int m_uiAcknowledgeNum; // 确认号32bit short m_sHeaderLenAndFlag; // 前4位:TCP头长度;中6位:保留;后6位:标志位 short m_sWindowSize; // 窗口大小16bit short m_sCheckSum; // 检验和16bit short m_surgentPointer; // 紧急数据偏移量16bit }__attribute__((packed))TCP_HEADER, *PTCP_HEADER; /*TCP头中的选项定义 kind(8bit)+Length(8bit,整个选项的长度,包含前两部分)+内容(如果有的话) KIND = 1表示 无操作NOP,无后面的部分 2表示 maximum segment 后面的LENGTH就是maximum segment选项的长度(以byte为单位,1+1+内容部分长度) 3表示 windows scale 后面的LENGTH就是 windows scale选项的长度(以byte为单位,1+1+内容部分长度) 4表示 SACK permitted LENGTH为2,没有内容部分 5表示这是一个SACK包 LENGTH为2,没有内容部分 8表示时间戳,LENGTH为10,含8个字节的时间戳 */ typedef struct _TCP_OPTIONS { char m_ckind; char m_cLength; char m_cContext[32]; }__attribute__((packed))TCP_OPTIONS, *PTCP_OPTIONS;
四、UDP头结构的定义
/*UDP头定义,共8个字节*/ typedef struct _UDP_HEADER { unsigned short m_usSourPort; // 源端口号16bit unsigned short m_usDestPort; // 目的端口号16bit unsigned short m_usLength; // 数据包长度16bit unsigned short m_usCheckSum; // 校验和16bit }__attribute__((packed))UDP_HEADER, *PUDP_HEADER;
文章的脚注信息由WordPress的wp-posturl插件自动生成