rpc_server: Don't rely on TCP-bind() to return EADDRINUSE
authorVolker Lendecke <vl@samba.org>
Sat, 12 Jun 2021 10:45:30 +0000 (12:45 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 15 Jun 2021 18:11:35 +0000 (18:11 +0000)
socket_wrapper can't do EADDRINUSE because unix domain sockets don't
do it.

This currently works correctly because right now all RPC servers
either use explicit ports or all listen on the same socket.

The new code uses a static variable, so it only helps if a single
process listens for multiple RPC sockets. It won't work if multiple
processes start listening. But in case samba-dcerpcd goes in this will
be exactly the right thing to do.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/rpc_server/rpc_sock_helper.c

index 22a0cd17aeb28b6b0d489617098c62db69ba3577..8ae637a71b4534f1ba9ffc007e271fc43b5bbcfe 100644 (file)
@@ -117,12 +117,18 @@ static NTSTATUS dcesrv_create_ncacn_ip_tcp_socket(
        int fd = -1;
 
        if (*port == 0) {
+               static uint16_t low = 0;
                uint16_t i;
 
-               for (i = lp_rpc_low_port(); i <= lp_rpc_high_port(); i++) {
+               if (low == 0) {
+                       low = lp_rpc_low_port();
+               }
+
+               for (i = low; i <= lp_rpc_high_port(); i++) {
                        fd = open_socket_in(SOCK_STREAM, ifss, i, false);
                        if (fd >= 0) {
                                *port = i;
+                               low = i+1;
                                break;
                        }
                }