user3864932
user3864932

Reputation: 1

linux ethernet frame socket clarification

I am trying to understand below lines in the sample socket code in found in 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

i just want to understand these assignment

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

Upvotes: 0

Views: 726

Answers (2)

Mansuro
Mansuro

Reputation: 4617

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

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

This is the schematic representation of sendbuf

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

                   -- sendbuf --

Upvotes: 4

Nikole
Nikole

Reputation: 4749

first one is accessing ethernet header ptr, and next is accessing the iphdr ptr. ( ethernet packet contains IP packet)

Upvotes: 1

Related Questions