s3: Use cli_connect_nb in SMBC_server_internal
authorVolker Lendecke <vl@samba.org>
Sun, 29 May 2011 18:22:38 +0000 (20:22 +0200)
committerVolker Lendecke <vl@samba.org>
Sun, 29 May 2011 19:10:26 +0000 (21:10 +0200)
source3/libsmb/libsmb_server.c

index 45be66072f552e0fbeaa30e67c4b00445a1f162e..80faaed1af722b8b5f92972e1ad140e4c1d061ac 100644 (file)
@@ -247,19 +247,13 @@ SMBC_server_internal(TALLOC_CTX *ctx,
        SMBCSRV *srv=NULL;
        char *workgroup = NULL;
        struct cli_state *c;
-       struct nmb_name called, calling;
        const char *server_n = server;
-       struct sockaddr_storage ss;
-       int tried_reverse = 0;
-        int port_try_first;
-        int port_try_next;
         int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
        uint32 fs_attrs = 0;
         const char *username_used;
        NTSTATUS status;
        char *newserver, *newshare;
 
-       zero_sockaddr(&ss);
        ZERO_STRUCT(c);
        *in_cache = false;
 
@@ -393,20 +387,32 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                return NULL;
        }
 
-       make_nmb_name(&calling, smbc_getNetbiosName(context), 0x0);
-       make_nmb_name(&called , server, 0x20);
-
        DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
 
        DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
 
-again:
+       status = NT_STATUS_UNSUCCESSFUL;
 
-       zero_sockaddr(&ss);
+        if (share == NULL || *share == '\0' || is_ipc) {
+               /*
+                * Try 139 first for IPC$
+                */
+               status = cli_connect_nb(server_n, NULL, 139, 0x20,
+                                       smbc_getNetbiosName(context),
+                                       Undefined, &c);
+       }
 
-       /* have to open a new connection */
-       if ((c = cli_initialise()) == NULL) {
-               errno = ENOMEM;
+       if (!NT_STATUS_IS_OK(status)) {
+               /*
+                * No IPC$ or 139 did not work
+                */
+               status = cli_connect_nb(server_n, NULL, 0, 0x20,
+                                       smbc_getNetbiosName(context),
+                                       Undefined, &c);
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
                return NULL;
        }
 
@@ -424,73 +430,6 @@ again:
 
        c->timeout = smbc_getTimeout(context);
 
-        /*
-         * Force use of port 139 for first try if share is $IPC, empty, or
-         * null, so browse lists can work
-         */
-        if (share == NULL || *share == '\0' || is_ipc) {
-                port_try_first = 139;
-                port_try_next = 445;
-        } else {
-                port_try_first = 445;
-                port_try_next = 139;
-        }
-
-        c->port = port_try_first;
-
-       status = cli_connect(c, server_n, &ss);
-       if (!NT_STATUS_IS_OK(status)) {
-
-                /* First connection attempt failed.  Try alternate port. */
-                c->port = port_try_next;
-
-                status = cli_connect(c, server_n, &ss);
-               if (!NT_STATUS_IS_OK(status)) {
-                       cli_shutdown(c);
-                       errno = ETIMEDOUT;
-                       return NULL;
-               }
-       }
-
-       if (!cli_session_request(c, &calling, &called)) {
-               cli_shutdown(c);
-               if (strcmp(called.name, "*SMBSERVER")) {
-                       make_nmb_name(&called , "*SMBSERVER", 0x20);
-                       goto again;
-               } else {  /* Try one more time, but ensure we don't loop */
-
-                       /* Only try this if server is an IP address ... */
-
-                       if (is_ipaddress(server) && !tried_reverse) {
-                               fstring remote_name;
-                               struct sockaddr_storage rem_ss;
-
-                               if (!interpret_string_addr(&rem_ss, server,
-                                                           NI_NUMERICHOST)) {
-                                       DEBUG(4, ("Could not convert IP address "
-                                                  "%s to struct sockaddr_storage\n",
-                                                  server));
-                                       errno = ETIMEDOUT;
-                                       return NULL;
-                               }
-
-                               tried_reverse++; /* Yuck */
-
-                               if (name_status_find("*", 0, 0,
-                                                     &rem_ss, remote_name)) {
-                                       make_nmb_name(&called,
-                                                      remote_name,
-                                                      0x20);
-                                       goto again;
-                               }
-                       }
-               }
-               errno = ETIMEDOUT;
-               return NULL;
-       }
-
-       DEBUG(4,(" session request ok\n"));
-
        status = cli_negprot(c);
 
        if (!NT_STATUS_IS_OK(status)) {