Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335936
  • 博文数量: 56
  • 博客积分: 2058
  • 博客等级: 中尉
  • 技术积分: 688
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-11 09:19
个人简介

code rush

文章分类

全部博文(56)

文章存档

2012年(2)

2011年(54)

分类: LINUX

2011-06-10 00:47:05

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)是位对齐。
阅读(5532) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~