【个人总结系列-46】计算机网络基础知识学习-数据包格式分析-传输过程-IP地址分类-网络设备.docx
文本预览下载声明
计算机网络基础知识学习-数据包格式分析-传输过程-IP地址分类-网络设备计算机网络基础知识学习对数据包格式的分析由于在对包进行分析时都要参考数据包的格式,所以数据包的格式是相当重要的。在抓包时,首先是获得链路层的帧,根据帧头可以获得源mac和目的mac以及上层的协议。一般帧头是14byte,链路层帧的包头结构在程序中的表示如下:/* 6字节的mac地址 */typedefstructmac_address{u_char byte1;u_char byte2;u_char byte3;u_char byte4;u_char byte5;u_char byte6;} mac_address;/* 14字节的ether帧头 */typedefstructether_header{mac_addressdest_mac;mac_addresssrc_mac;u_shortprotocal;} ether_header;根据帧头的长度将指针往后移,然后可以获得IP数据报的头部指针,根据报头信息可以获得源IP、目的IP、上层协议、头部长度、总长度等信息,IP数据报的头部格式如下图所示:图 IPV4头部格式图 IPV6头部格式IPV4报文结构在程序中的表示:/* 4字节的IP地址 */typedefstructip_address{u_char byte1;u_char byte2;u_char byte3;u_char byte4;} ip_address;/* IP头部 */typedefstructip_header{u_charver_ihl; // 版本 (4 bits) + 首部长度 (4 bits)u_chartos; // 服务类型(Type of service)u_shorttlen; // 总长(Total length)u_short identification; // 标识(Identification)u_shortflags_fo; // 标志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)u_charttl; // 存活时间(Time to live)u_char proto; // 协议(Protocol)u_shortcrc; // 首部校验和(Header checksum)ip_addresssaddr; // 源地址(Source address)ip_addressdaddr; // 目的地址(Destination address)u_intop_pad; // 选项与填充(Option + Padding)} ip_header;然后根据报头长度又可以计算出TCP或UDP的头部指针,根据TCP或UDP的头部信息可以获得源端口号和目的端口号等信息,一般TCP的头部长度为20bytes,UDP的头部长度为8bytes,TCP和UDP的报文格式如下所示:图TCP报文格式图UDP报文格式TCP/UDP包头结构在程序中的表示:/* TCP头部 */typedefstructtcp_header{u_shortsrcPort; // 源端口号 16bitsu_shortdestPort; // 目的端口号 16bitsu_intseqNum; // 序号 32bitsu_intackNum;// 确认号 32bitsu_shortheadLen_other; // 首部长度+保留未用+其他字段 16bitsu_shortwindowSize; // 窗口大小 16bitsu_shortcheckSum; // 检验和 16bitsu_short pointer; // 紧急数据指针 16bitsu_int option;// 选项可选、不定长} tcp_header;/* UDP头部 */typedefstructudp_header{u_shortsrcPort; // 源端口号 16bitsu_shortdestPort; // 目的端口号 16bitsu_shortudpLen; // udp长度 16bitsu_shortcheckSum; // 检验和 16bits} udp_header;最后就是应用层的数据了,根据上层的报文头部信息可以计算出应用层数据的头部指针,同时根据IP数据包的头部信息可以计算出应用层数据的长度,因此就可以通
显示全部