Search code examples
udpsockets

When is IPPROTO_UDP required?


When is IPPROTO_UDP required?

Is there ever a case where UDP is not the default protocol for SOCK_DGRAM? (real cases, not hypothetical "it might be", please")

i.e., what are the situations where the following two lines would not produce identical behavior?

if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1)

Solution

  • Given these declarations:

    tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
    udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    raw_socket = socket(AF_INET, SOCK_RAW, protocol);
    

    the ip(7) manual page in linux says:

    The only valid values for protocol are 0 and IPPROTO_TCP for TCP sockets, and 0 and IPPROTO_UDP for UDP sockets. For SOCK_RAW you may specify a valid IANA IP protocol defined in RFC 1700 assigned numbers.

    Those two lines in your questions will always produce the same result.