Suppose I want to implement my own transport layer protocol. If I open a socket with:
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
or
int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
then this is not satisfactory since (I think), the kernel/network stack will automatically wrap any data we send as TCP or UDP, respectively (i.e. it will put the appropriate headers in front of our payload).
If I do:
int socket_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
then this is too far in the other direction, now the kernel expects me to provide it with a pre-formatted IP datagram, so I have to implement the internet protocol myself as well.
Is there a way to get the “sweet spot”, where I can do my own transport protocol but let the sockets library do all the IP encapsulation for me?
2