UPDATE: As reported in comments, the last case (tcp nolisten reuseaddr) is not reprdocuing anymore on recent kernels. All ports are unique: $ ulimit -n 10000Ĭreate 7100 non-listening TCP sockets, enable SO_REUSEADDR. All ports are unique: $ ulimit -n 10000Ĭreate 7100 listening TCP sockets, enable SO_REUSEADDR. a.out 1000 udp nolisten reuseaddrĬonflict: port = port = 34061Ĭonflict: port = port = 36124Ĭonflict: port = port = 33533Ĭonflict: port = port = 47015Ĭonflict: port = port = 50594Ĭonflict: port = port = 59259Ĭonflict: port = port = 43698Ĭonflict: port = port = 39131Ĭonflict: port = port = 48673Ĭonflict: port = port = 35236Ĭonflict: port = port = 59258Ĭonflict: port = port = 41065Ĭonflict: port = port = 50470Ĭonflict: port = port = 52191Ĭonflict: port = port = 46756Ĭonflict: port = port = 44039Ĭonflict: port = port = 47422Ĭonflict: port = port = 47125Ĭonflict: port = port = 39183Ĭonflict: port = port = 60800Ĭonflict: port = port = 40282Ĭreate 7100 non-listening TCP sockets, disable SO_REUSEADDR. a.out 1000 udp nolisten noreuseaddrĬreate 1000 UDP sockets, enable SO_REUSEADDR. All sockets are kept open until the program exits.Ĭreate 1000 UDP sockets, disable SO_REUSEADDR. The program opens the given number of listening or non-listening UDP or TCP sockets bound to ephemeral ports and checks whether all ports are unique or not. I’ve prepared a small program demonstrating the issue: ephemeral_reuse.c Thus, to prevent the probability of stealing a port of a random running application, take care not to accidentally enable SO_REUSEADDR when using ephemeral ports, both for UDP and TCP sockets. Note that when some application uses bind to allocate an ephemeral port for a TCP socket, and then immediately calls listen, there is still a short period of time when the socket is in the non-listening state. This means that the kernel is free to reuse an ephemeral port of any opened UDP socket (because listen is not used for datagram sockets) and any opened TCP socket for which listen was not called yet. The important point here is that the kernel doesn’t check whether there is an opened socket for an ephemeral port, it only checks whether there is a socket in the listening state for that port. Hence, when an ephemeral port is allocated, SO_REUSEADDR enables the kernel to reuse any other non-listening ephemeral port. There is an active listening socket bound to the address. ForĪF_INET sockets this means that a socket may bind, except when In a bind(2) call should allow reuse of local addresses. Indicates that the rules used in validating addresses supplied The socket(7) man page states the following: SO_REUSEADDR Reusing addressīe careful when using SO_REUSEADDR and the port is allowed to be ephemeral. This feature is not specified in POSIX but is available in many operating systems that implement BSD sockets, including Linux. This behavior is described in the ip_local_port_range section of the ip(7) man page. In particular, bind, listen, connect, and sendto may automatically allocate an ephemeral port for AF_INET and AF_INET6 sockets. The ephemeral port range is a range of ports used by the kernel when the user wants the socket to be bound to a random unused port.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |