tcp udp数据报定义结构体

文章链接:https://codemouse.online/archives/2020-06-30-21-08-22

tcp udp数据报定义结构体

由于结构体写在结构体里面容易出现结构体内存对齐问题,需要将对齐值改为1,防止出错。

#pragma pack(1)    //设置对齐数

UDP报文头

udp报文头定义:

struct udphdr {

	unsigned short sport; 
	unsigned short dport;
	unsigned short length;
	unsigned short check;

};

tcp报文头
tcp报文头定义:

struct tcphdr {
	unsigned short	sport; 
	unsigned short	dport;
	unsigned int	seq;
	unsigned int	ack;
	unsigned char 	hdrlen:4
					resc:4;
	unsigned char	cwr:1,
					ece:1,
					urg:1,
					ack:1,
					psh:1,
					rst:1,
					syn:1,
					fin:1;
	unsigned short	wsize;
	unsigned short	check;
	unsigned short	upoint;
	unsigned char	options[0];
};

以太网帧格式

#define ETH_LENGTH		6

struct ethhdr {
	unsigned char dest[ETH_LENGTH];
	unsigned char src[ETH_LENGTH];
	unsigned short proto;
};

ip报文头
以太网帧格式与ip头格式的定义:

struct iphdr {

	unsigned char version:4,
				  hdrlen:4;

	unsigned char tos; //
	unsigned short tot_len;

	unsigned short id;
	unsigned short flag:3,
				   offset:13;

	unsigned char ttl;
	unsigned char proto;
	unsigned short check;

	unsigned int sip;
	unsigned int dip;
	

};

udp数据报定义:

struct udppkt {
	struct ethhdr eh;
	struct iphdr ip;
	struct udphdr udp;
	unsigned char body[0]; 
};

tcp数据报定义:

struct tcppkt {
	struct ethhdr eh;
	struct iphdr ip;
	struct tcphdr tcp;
	unsigned char body[0]; 
};

展开阅读全文
©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值