Some socket options

In fact, both network socket client & server will call a set_common_sockopts function which will set some socket options according to command line options:

  1. void
  2. set_common_sockopts(int s, int af)
  3. {
  4. int x = 1;
  5. if (Sflag) {
  6. if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
  7. &x, sizeof(x)) == -1)
  8. err(1, NULL);
  9. }
  10. if (Dflag) {
  11. if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
  12. &x, sizeof(x)) == -1)
  13. err(1, NULL);
  14. }
  15. if (Tflag != -1) {
  16. if (af == AF_INET && setsockopt(s, IPPROTO_IP,
  17. IP_TOS, &Tflag, sizeof(Tflag)) == -1)
  18. err(1, "set IP ToS");
  19. else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
  20. IPV6_TCLASS, &Tflag, sizeof(Tflag)) == -1)
  21. err(1, "set IPv6 traffic class");
  22. }
  23. if (Iflag) {
  24. if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
  25. &Iflag, sizeof(Iflag)) == -1)
  26. err(1, "set TCP receive buffer size");
  27. }
  28. if (Oflag) {
  29. if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
  30. &Oflag, sizeof(Oflag)) == -1)
  31. err(1, "set TCP send buffer size");
  32. }
  33. if (ttl != -1) {
  34. if (af == AF_INET && setsockopt(s, IPPROTO_IP,
  35. IP_TTL, &ttl, sizeof(ttl)))
  36. err(1, "set IP TTL");
  37. else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
  38. IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)))
  39. err(1, "set IPv6 unicast hops");
  40. }
  41. if (minttl != -1) {
  42. if (af == AF_INET && setsockopt(s, IPPROTO_IP,
  43. IP_MINTTL, &minttl, sizeof(minttl)))
  44. err(1, "set IP min TTL");
  45. else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
  46. IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)))
  47. err(1, "set IPv6 min hop count");
  48. }
  49. }

Below is the table which summarize these options:










VariableCommand optionSocket level & optionMeaning
Sflag-SIPPROTO_TCP&TCP_MD5SIGUses TCP MD5 signatures per RFC 2385
Dflag-DSOL_SOCKET&SO_DEBUGEnables recording of debugging information
Tflag-TIPPROTO_IP&IP_TOS IPPROTO_IPV6&IPV6_TCLASSChanges IPv4 TOS value (refer process_tos_opt function) or sets IPv6 traffic class field
Iflag-I lengthSOL_SOCKET&SO_RCVBUFSets buffer size for input
Oflag -O lengthSOL_SOCKET&SO_SNDBUFSets buffer size for output
ttl -M ttlIPPROTO_IP&IP_TTL IPPROTO_IPV6&IPV6_UNICAST_HOPSSets the TTL / hop limit of outgoing packets
minttl -m minttlIPPROTO_IP&IP_MINTTL IPPROTO_IPV6&IPV6_MINHOPCOUNTDiscards packets with a TTL lower than the option value