以太网实现
来源:互联网 发布:集体智慧编程 怎么样 编辑:程序博客网 时间:2024/04/28 03:04
以太网的头部为14字节,6字节源地址,6字节目的地址,2字节类型域。它的定义如下:
[ include/uapi/linux/if_ether.h ]
/* *This is an Ethernet frame header. *ETH_ALEN = 6 */struct 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));其中h_proto为此frame是什么类型,如对于IP协议,类型为
[ include/uapi/linux/if_ether.h ]
#define ETH_P_IP0x0800/* Internet Protocol packet*/还有一个特殊的类型:
[ include/uapi/linux/if_ether.h ]
#define ETH_P_ALL0x0003/* Every packet (be careful!!!) */如果通过RAW socket获取所有的包,可以用此类型,如:socket( PF_PACKET, SOCK_RAW, htons( ETH_P_ALL) );
内核对于创建每层的协议头,提供了一个抽象结构,其中创建以太网头部的结构如下:
[ net/ethernet/eth.c ]
const struct header_ops eth_header_ops ____cacheline_aligned = {.create= eth_header,.parse= eth_header_parse,.rebuild= eth_rebuild_header,.cache= eth_header_cache,.cache_update= eth_header_cache_update,};
创建以太网头部的函数如下:
[ net/ethernet/eth.c ]
/** * eth_header - create the Ethernet header * @skb:buffer to alter * @dev:source device * @type:Ethernet type field * @daddr: destination address (NULL leave destination address) * @saddr: source address (NULL use device source address) * @len: packet length (<= skb->len) * * * Set the protocol type. For a packet of type ETH_P_802_3/2 we put the length * in here instead. */int eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type, const void *daddr, const void *saddr, unsigned int len){/* ETH_HLEN = 14 * 在skb头部增加尺寸来容纳以太网头部 */struct ethhdr *eth = (struct ethhdr *)skb_push(skb, ETH_HLEN);/* 类型为ETH_P_802_3 或ETH_P_802_2 时, eth->h_proto 放的是长度 */if (type != ETH_P_802_3 && type != ETH_P_802_2)eth->h_proto = htons(type);elseeth->h_proto = htons(len);/* * Set the source hardware address. */if (!saddr)saddr = dev->dev_addr;// 源地址为设备的地址memcpy(eth->h_source, saddr, ETH_ALEN);// 设置源地址if (daddr) {memcpy(eth->h_dest, daddr, ETH_ALEN);// 设置目的地址return ETH_HLEN;}/* * Anyway, the loopback-device should never use this function... */if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {// 设备为环回地址或不使用ARPmemset(eth->h_dest, 0, ETH_ALEN);// 设置目的地址为0return ETH_HLEN;}return -ETH_HLEN;}EXPORT_SYMBOL(eth_header);
0 0
- 以太网实现
- 以太网调试器的实现
- Verilog实现千兆以太网传输
- 嵌入式以太网第三部分——以太网协议实现
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 以太网
- 我做的简单以太网交换机实现
- 基于以太网的嵌入式WebSever实现
- WebRTC音视频引擎研究(1)--整体架构分析
- Lambda Expressions
- 盘点互联网巨头奉献的十大开源安全工具
- 给View添加一个角标---ViewBadger的使用
- fatal error C1900: Il mismatch between 'P1' version '20080116' and 'P2' version '20070207'
- 以太网实现
- 社交招聘网络Linkedin的使用体验(HR招聘篇3)
- 这样选择省份是不是会快点?
- WebRTC源码分析二:音频模块结构
- POJ 3070 Fibonacci(矩阵快速幂)
- 删除字符串开始和结尾空格,并将中间多个空格合并为一个空格
- Android UI系列-----Dialog对话框
- lua 开发学习之旅三
- 有关音视频文件解析和提取等一系列问题的帖子集合,持续更新