Reputation: 1
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
(struct ether_header *) sendbuf;
(struct iphdr *) (sendbuf + sizeof(struct ether_header));
Upvotes: 0
Views: 726
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
Reputation: 4749
first one is accessing ethernet header ptr, and next is accessing the iphdr ptr. ( ethernet packet contains IP packet)
Upvotes: 1