1

I have a simple P2P connection between my peers on a TCP socket. My client and server both are running on Linux. I have turned on TCP keep_alive functionality on my TCP sockets on both sides. I am using boost::asio to connect, read ane write data on my tcp sockets on both sides.

The tcp keep_alive expamples use IPPROTO_TCP and SOL_SOCKET which is confusing. Following are properties I set on my socket. But I am confused whether to use IPPROTO_TCP or SOL_SOCKET because both of them compile well on both the platforms.

Code:

int on = 1;
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(int)
int timeOut = 3; //seconds
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_KEEPIDLE, &timeOut, sizeof(int)
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_KEEPINTVL, &timeOut, sizeof(int))
int unackCount = 1;
setsockopt(socketNativeHandle, IPPROTO_TCP, TCP_KEEPCNT, &unackCount, sizeof(int))

OR

int on = 1;
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_NODELAY, &on, sizeof(int)
int timeOut = 3; //seconds
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_KEEPIDLE, &timeOut, sizeof(int)
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_KEEPINTVL, &timeOut, sizeof(int))
int unackCount = 1;
setsockopt(socketNativeHandle, SOL_SOCKET, TCP_KEEPCNT, &unackCount, sizeof(int))

Question:
Should I use SOL_SOCKET on both sides or should I use IPPROTO_TCP on both sides? Is there a way to decide this at runtime? Note that I have very simple peer to peer wifi connection.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Successful compilation is necessary but not sufficient to prove correctness. Which of them actually works? You *certainly* don't need a way to decide this at runtime. – user207421 Nov 26 '19 at 09:34
  • 1
    `IPPROTO_TCP` seems to work for the numbers based properties like `TCP_KEEPIDLE`, `TCP_KEEPINTVL` and `TCP_KEEPCNT`. Looks like the boolean based on/off properties are set `SOL_SOCKET` level. Slowly getting to understand this better. – TheWaterProgrammer Nov 26 '19 at 09:53
  • 1
    I suggest `SOL_SOCKET` for the `SO_*` options and `IPPROTO_TCP` for the `TCP_*` options. – user207421 Nov 27 '19 at 03:55

2 Answers2

5

SOL is abbreviation of socket_level, which designed for SO_* options, such as SO_REUSEPORT

IPPROTO_* is designed for options of a specific network protocol, such as IPPROTO_IP, IPPROTO_TCP

More:

Jiacai Liu
  • 2,623
  • 2
  • 22
  • 42
1

here a IBM doc says:

level is the protocol level for which the socket option is being set. SOL_SOCKET and IPPROTO_TCP are supported.
All optname values beginning with “SO_” are for protocol level SOL_SOCKET and are interpreted by the general socket code.
All optname values beginning with “TCP_” are for protocol level IPPROTO_TCP and are interpreted by the TCP/IP internal code.

but I'm not sure if it applies to linux too.

https://www.ibm.com/docs/en/zvm/7.1?topic=functions-setsockopt



TCP_* options
https://man7.org/linux/man-pages/man7/tcp.7.html#:~:text=set%20to-,IPPROTO_TCP

SO_* options
https://man7.org/linux/man-pages/man7/socket.7.html#:~:text=level%20set%20to-,SOL_SOCKET

NOTE: use chrome can scroll to highlight keyword in man page.



here also a SOL_TCP, seems same as IPPROTO_TCP

$ ag 'SOL_SOCKET|SOL_TCP|IPPROTO_TCP' -R /usr/include/ --file-search-regex .h
/usr/include/netinet/tcp.h
206:# define SOL_TCP        6   /* TCP level */

/usr/include/netinet/in.h
50:    IPPROTO_TCP = 6,    /* Transmission Control Protocol.  */
51:#define IPPROTO_TCP      IPPROTO_TCP

/usr/include/linux/in.h
37:  IPPROTO_TCP = 6,       /* Transmission Control Protocol    */
38:#define IPPROTO_TCP      IPPROTO_TCP


/usr/include/asm-generic/socket.h
9:#define SOL_SOCKET    1

/usr/include/x86_64-linux-gnu/bits/socket-constants.h
25:#define SOL_SOCKET 1

yurenchen
  • 1,897
  • 19
  • 17