Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40187
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 11:25
文章分类
文章存档

2015年(2)

2014年(4)

2013年(2)

我的朋友

分类: LINUX

2015-05-18 13:31:39

原文地址:以太网 帧头 分析 作者:crazyhadoop

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)是位对齐。
阅读(2565) | 评论(0) | 转发(0) |
0

上一篇:ssh 配置讲解大全

下一篇:没有了

给主人留下些什么吧!~~