s3-rpc_server: Added common function to create tcpip socket.
authorAndreas Schneider <asn@samba.org>
Mon, 23 May 2011 14:00:30 +0000 (16:00 +0200)
committerAndreas Schneider <asn@samba.org>
Mon, 1 Aug 2011 06:50:34 +0000 (08:50 +0200)
source3/include/proto.h
source3/rpc_server/rpc_server.c

index 095c52c7c0ad5a325d3f2d41c61b85aa18780c83..2619e7bbdeb044846aca9c1f71e46dc5aa81383e 100644 (file)
@@ -793,6 +793,7 @@ int get_remote_hostname(const struct tsocket_address *remote_address,
 int create_pipe_sock(const char *socket_dir,
                     const char *socket_name,
                     mode_t dir_perms);
+int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port);
 const char *get_mydnsfullname(void);
 bool is_myname_or_ipaddr(const char *s);
 struct tevent_req *getaddrinfo_send(TALLOC_CTX *mem_ctx,
index 2e109a5a9c7cef6f0fd0a42c332de8439bb72e35..0caf20a9909520b796fda49b3c632b6ae82e69d8 100644 (file)
@@ -625,6 +625,41 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
                                        uint16_t flags,
                                        void *private_data);
 
+int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
+{
+       int fd = -1;
+
+       if (*port == 0) {
+               uint16_t i;
+
+               for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
+                       fd = open_socket_in(SOCK_STREAM,
+                                           i,
+                                           0,
+                                           ifss,
+                                           false);
+                       if (fd > 0) {
+                               *port = i;
+                               break;
+                       }
+               }
+       } else {
+               fd = open_socket_in(SOCK_STREAM,
+                                   *port,
+                                   0,
+                                   ifss,
+                                   true);
+       }
+       if (fd == -1) {
+               DEBUG(0, ("Failed to create socket on port %u!\n", *port));
+               return -1;
+       }
+
+       DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd, *port));
+
+       return fd;
+}
+
 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
                                         struct messaging_context *msg_ctx,
                                         const struct sockaddr_storage *ifss,
@@ -644,30 +679,8 @@ uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
        state->ep.port = port;
        state->disconnect_fn = NULL;
 
-       if (state->ep.port == 0) {
-               uint16_t i;
-
-               for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
-                       state->fd = open_socket_in(SOCK_STREAM,
-                                                  i,
-                                                  0,
-                                                  ifss,
-                                                  false);
-                       if (state->fd > 0) {
-                               state->ep.port = i;
-                               break;
-                       }
-               }
-       } else {
-               state->fd = open_socket_in(SOCK_STREAM,
-                                          state->ep.port,
-                                          0,
-                                          ifss,
-                                          true);
-       }
+       state->fd = create_tcpip_socket(ifss, &state->ep.port);
        if (state->fd == -1) {
-               DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
-                         "socket on port %u!\n", state->ep.port));
                goto out;
        }