Search code examples
socketsnetwork-programmingtcpposixsetsockopt

setsockopt SO_BROADCAST on TCP socket


In an existing networking library I've been tasked to work on there is a call to setsockopt which I don't understand

Here you can see a TCP socket begin created:

[socket] fd(11) domain(2:AF_INET) type(1:SOCK_STREAM) protocol(0:default)

Immediately afterward, a call to setsockopt is made for option SO_BROADCAST at the IPPROTO_TCP protocol level, with option value 5

[setsockopt] fd(11) level(6:IPPROTO_TCP) option(6:SO_BROADCAST) ret(0) option:
    0    0500 0000                                  ....

According to Beej's guide to networking this "Does nothing—NOTHING!!—to TCP stream sockets! Hahaha!"

Questions:

  • What exactly are they doing here?
  • Does this make any sense?
  • If anything, surely it should be option_value=1, so what is the 5 about?

Solution

  • I think your setsockopt decoder is wrong. Are you sure it isn't one of these?

    #define TCP_NODELAY              1  /* Don't delay send to coalesce packets  */
    #define TCP_MAXSEG               2  /* Set maximum segment size  */
    #define TCP_CORK                 3  /* Control sending of partial frames  */
    #define TCP_KEEPIDLE             4  /* Start keeplives after this period */
    #define TCP_KEEPINTVL            5  /* Interval between keepalives */
    #define TCP_KEEPCNT              6  /* Number of keepalives before death */
    

    That isn't a full list. See /usr/include/netinet/tcp.h for everything.