以太网 IP TCP UDP 头部

来源:互联网 发布:我的世界展示框js 编辑:程序博客网 时间:2024/05/22 13:36

以太网头部结构


1、目的地址/源地址:目的主机或源主机的MAC地址

2、类型:

0800:IP数据报

0806:ARP请求

3、数据:

不足46字节的数据会被补足到46字节再发送。

4、内核数据结构

#define ETH_ALEN 6struct ethhdr {unsigned charh_dest[ETH_ALEN];/* destination eth addr*/unsigned charh_source[ETH_ALEN];/* source ether addr*/__be16h_proto;/* packet type ID field*/} __attribute__((packed));



IP头部结构

1、版本:指定IP协议的版本号

2、首部长度:IP协议首部的长度

因为有一个可选项,所以IP首部的长度不是固定值;

单位长度是4个字节;

IPv4 首部的最小长度是20个字节(首部长度值为5),最大长度是24个字节(首部长度为6)。

总长度:IP包的总长度(首部+数据部分)

内核数据结构

struct iphdr {#if defined(__LITTLE_ENDIAN_BITFIELD)__u8ihl:4,version:4;#elif defined (__BIG_ENDIAN_BITFIELD)__u8version:4,  ihl:4;#else#error"Please fix <asm/byteorder.h>"#endif__u8tos;__be16tot_len;__be16id;__be16frag_off;__u8ttl;__u8protocol;__sum16check;__be32saddr;__be32daddr;/*The options start here. */};


UDP 头部结构(8个字节)

内核数据结构

struct udphdr {__be16source;__be16dest;__be16len;__sum16check;};typedef __u16 __bitwise __sum16;