For UDP sockets, is the ip address unlimited closing time ()?

While reading this wonderful answer, I found out that a TCP socket can have a state called TIME_WAIT . Due to this state, it is possible that the TCP socket did not release the address to which it is bound, although the close(int fd) function returned 0 .

Given that UDP is connectionless and has no reliability requirements for delivering data such as TCP, can we assume that as soon as close(int fd) returns 0 , the address will be unbound?

+6
source share
2 answers

Yes, according to the source code https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/udp.c?id=refs/tags/v3.19 -rc6 , udp_destroy_sock() (~ line 2028) clears any deferred frames and frees a socket that frees the address.

You can demonstrate this with a simple example. You will need netcat , client and server. One of the servers, run this code:

 #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <string.h> int main() { struct sockaddr_in me; int sock; if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { perror("socket error:"); return -1; } memset(&me, 0, sizeof(me)); me.sin_family = AF_INET; me.sin_port = htons(60000); me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr*)&me, sizeof(me)) == -1) { perror("bind error: "); return -1; } printf("On client execute:\n"); printf(" nc -u {servers ip address} 60000\n\n"); printf("type: hello world<enter>\n"); printf("Hit enter when you've done this..."); getchar(); printf("\nNow check the input queue on this server\n"); printf(" netstat -an|grep 60000\n"); printf("Notice that we have buffered data we have not read\n"); printf("(probably about 360 bytes)\n"); printf("Hit enter to continue..."); getchar(); printf("\nI'm going to end. After I do, run netstat -an again\n"); printf("and you'll notice port 60000 is gone.\n\n"); printf("Re-run this program on server again and see you\n"); printf("have no problem re-acquiring the UDP port.\n"); return 0; } 
+6
source

TL DR:. UDP sockets will be closed and immediately disconnected (unless it is a broadcast / multicast address with other listeners).

TIME WAIT is defined by the source RFC 793 and applies only to TCP . To expire a socket, the TCP requires a 2 * Maximum Segment Lifetime.

Stevens' famous Unix Network Programming also explains TCP TIME WAIT in more detail for the curious.

UDP has no connections. TIME WAIT is not part of this protocol.

The Linux source, although related to potentially proprietary behavior on Linux-based systems, is not an authority on such matters.

The original 1981 DARPA TCP RFC 793 is authoritative as well as the Berkeley Sockets api on which POSIX sockets are based on the expected behavior of the socket API.

Also relevant is the BSD network stack , which connected TCP / IP on the early Internet to Windows, iOS and OSX today and provides an authoritative reference implementation of TCP / IP RFC protocols. The BSD stack is still considered the benchmark for the future Linux stack in a few years. (Facebook posted a mid-2014 role for an expert to help make Linux comparable or exceed the reliability and performance of the FreeBSD stack.)

+1
source

Source: https://habr.com/ru/post/981496/


All Articles