简体   繁体   English

linux以太网框架套接字说明

[英]linux ethernet frame socket clarification

I am trying to understand below lines in the sample socket code in found in google. 我试图了解下面的示例套接字代码在google中找到的行。

struct ether_header *eh = (struct ether_header *) sendbuf;
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));

struct ether_header *eh -> So far in know *eh used to access the struct variable struct ether_header *eh >到目前为止*eh用于访问struct变量

i just want to understand these assignment 我只想了解这些任务

  1. (struct ether_header *) sendbuf;
  2. (struct iphdr *) (sendbuf + sizeof(struct ether_header));

In the first line 在第一行

(struct ether_header *) sendbuf;

the variable sendbuf is cast to a pointer to the struct ether_header , you can read more about casting here 将变量sendbuf转换为指向struct ether_header的指针,您可以在此处阅读有关强制转换的更多信息

The second line 第二行

(struct iphdr *) (sendbuf + sizeof(struct ether_header));

it's adding sizeof(struct ether_header) to the pointer sendbuf , by doing that, it reaches the memory zone after the one occupied by the pointer to the struct ether_header , which seems to contain a pointer to the struct iphdr 它在指针sendbuf上添加了sizeof(struct ether_header) ,通过这样做,它到达了struct ether_header指针所占据的那个区域之后的内存区域,该指针似乎包含了指向struct iphdr的指针

This is the schematic representation of sendbuf 这是sendbuf的示意图

+------------------------------------------------------+
|    eh                                                |
+------------------------------------------------------+
|    iph = eh + sizeof(struct ether_header)            |
+------------------------------------------------------+

                   -- sendbuf --

first one is accessing ethernet header ptr, and next is accessing the iphdr ptr. 第一个是访问以太网头ptr,第二个是访问iphdr ptr。 ( ethernet packet contains IP packet) (以太网数据包包含IP数据包)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM