以太网 帧头 分析

2660阅读 0评论2015-05-18 TrollWarlord686
分类:LINUX

Linux中,以太网帧头部的结构体如下:/usr/include/linux/if_ether.h

802.3以太网卡帧 大小 省略了 前导探测段和CRC校验位

#define ETH_ALEN        6               /* Octets in one ethernet addr   */
#define ETH_HLEN        14              /* Total octets in header.       */
#define ETH_ZLEN        60              /* Min. octets in frame sans FCS */
#define ETH_DATA_LEN    1500            /* Max. octets in payload        */
#define ETH_FRAME_LEN   1514            /* Max. octets in frame sans FCS */
#define ETH_FCS_LEN     4               /* Octets in the FCS             */

不含校验位的帧最大为 1514字节,最小60字节。数据最大为1500字节

帧头为 6+6+2 = 14字节

struct ether_header
{
  u_int8_t  ether_dhost[ETH_ALEN]; /* destination eth addr */ 目的mac地址
  u_int8_t  ether_shost[ETH_ALEN]; /* source ether addr */ 源mac地址
  u_int16_t ether_type;          /* packet type ID field */ 包中帧的类型
} __attribute__ ((__packed__));

常见的帧类型有:
#define ETHERTYPE_IP  0x0800  /* IP */
#define ETHERTYPE_ARP  0x0806  /* Address resolution */
#define ETHERTYPE_REVARP 0x8035  /* Reverse ARP */
#define ETHERTYPE_IPV6  0x86dd  /* IP protocol version 6 */


__attribute__语法格式为:__attribute__ ((attribute-list)) GNU C的一个特性

__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)
和类型属性(Type Attribute)

__attrubte__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,
按照实际占用字节数进行对齐。 也就是说sizeof(ether_header) = 6+6+2=14
packed属性:使用该属性可以使得变量或者结构体成员使用最小的对齐方式,即对变量
是一字节对齐,对域(field)是位对齐。
上一篇:ssh 配置讲解大全
下一篇:没有了