Question: I'm trying to implement my own transport layer protocol, but I'm perfectly happy to leave the network layer as-is and not need to mess with the actual IP header information. 

But of course, when calling recvfrom() on a raw socket, you are given the raw IP datagram, while the sockaddr struct is not filled in. 

Is there anyway to coax the stack to fill in those structs and leave the ip header out of the data portion, or does that need to be implemented by hand?

Receiver: 

```
struct sockaddr_in sender;
int sender_len;

raw_socket = socket(AF_INET, SOCK_RAW, 56);

...

if((n = recvfrom(raw_socket, buf, 1024, 0, (struct sockaddr*)&sender, &sender_len)) == -1){
    perror("recvfrom");
    return -1;
}

```

Answer: 
The IP header will always be included when receiving on a SOCK\_RAW socket.

Per [`raw(7)`](http://man7.org/linux/man-pages/man7/raw.7.html):

> 
> The IPv4 layer generates an IP header when sending a packet unless the `IP_HDRINCL` socket option is enabled on the socket. When it is enabled, the packet must contain an IP header. **For receiving the IP header is always included in the packet.**
> 
> 
> 

Reference:

* [`ip(7)` man page](http://man7.org/linux/man-pages/man7/ip.7.html)
* [SOCK\_RAW Demystified](http://sock-raw.org/papers/sock_raw)
* [Advanced TCP/IP - THE RAW SOCKET PROGRAM EXAMPLES](http://www.tenouk.com/Module43a.html)
Use `recvmsg()` with the `msg[]` buffers initialized so that the first one receives the IP header, then the second one will only contain data.