Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is IPPROTO_UDP required?

Tags:

sockets

udp

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)
like image 990
Mark Harrison Avatar asked Dec 23 '09 20:12

Mark Harrison


People also ask

Does UDP client need to bind?

As far as I remember bind is not required for a UDP socket because a bind call is made for you by the stack.

What is Ipproto_ip?

IPPROTO_IP creates a socket that sends/receives raw data for IPv4-based protocols (TCP, UDP, etc). It will handle the IP headers for you, but you are responsible for processing/creating additional protocol data inside the IP payload.

What is sock Dgram?

Item. Description. SOCK_DGRAM. Provides datagrams, which are connectionless messages of a fixed maximum length. This type of socket is generally used for short messages, such as a name server or time server, because the order and reliability of message delivery is not guaranteed.

What is UDP socket?

Description. UDP socket routines enable simple IP communication using the user datagram protocol (UDP). The User Datagram Protocol (UDP) runs on top of the Internet Protocol (IP) and was developed for applications that do not require reliability, acknowledgment, or flow control features at the transport layer.


2 Answers

Some operating systems (eg. Linux kernel after 2.6.20) support a second protocol for SOCK_DGRAM, called UDP-Lite. If supported by your system, it would be enabled by providing IPPROTO_UDPLITE as the third argument to the socket() call.

It is differentiated from normal UDP by allowing checksumming to be applied to only a portion of the datagram. (Normally, UDP checksumming is an all-or-nothing effort.) That way, the protocol can be more resistant to checksum failures due to fragmented transmission, in the event that some fragments outside the checksummed area may have been lost in transit. As long as the fragments covering the checksummed portion were successfully received, as much of the datagram as possible will still be delivered to the application.

For backwards compatibility with existing code, I suspect (but I cannot guarantee) that the call socket(AF_INET,SOCK_DGRAM,0) will continue to default to normal UDP, even in systems that additionally support UDP-Lite.

like image 194
Goosnarrggh Avatar answered Oct 17 '22 07:10

Goosnarrggh


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.

like image 33
Gonzalo Avatar answered Oct 17 '22 05:10

Gonzalo