The branch, master has been updated
       via  03b8b8e s3: Remove unused open_any_socket_out
       via  5d4ac40 s3: Use smbsock_any_connect in winbind
       via  d5188fc s3: Retry *SMBSERVER in nb_connect
       via  3ae7ef7 s3: Test smbsock_any_connect
       via  d8e3d0a s3: Add smbsock_any_connect
       via  49cd92b s3: Add an async smbsock_connect
       via  02a74fd s3: Add async cli_session_request
       via  444dcc5 s3: Add some const to name_mangle()
      from  87934d8 s3-waf: fix the build.

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 03b8b8e19952c40eb3eb22e16e7fbae4073b7b4b
Author: Volker Lendecke <[email protected]>
Date:   Wed Dec 22 14:40:16 2010 +0100

    s3: Remove unused open_any_socket_out
    
    Autobuild-User: Volker Lendecke <[email protected]>
    Autobuild-Date: Wed Dec 22 17:27:29 CET 2010 on sn-devel-104

commit 5d4ac40f69aed6de033c0ca637dbab02db01740b
Author: Volker Lendecke <[email protected]>
Date:   Tue Dec 21 21:55:01 2010 +0100

    s3: Use smbsock_any_connect in winbind

commit d5188fcd185f518c2b2b78f9e4aa6c49b3d139c7
Author: Volker Lendecke <[email protected]>
Date:   Tue Dec 21 18:52:53 2010 +0100

    s3: Retry *SMBSERVER in nb_connect

commit 3ae7ef73b020789634df40d4b74dd4bad36ba4fa
Author: Volker Lendecke <[email protected]>
Date:   Tue Dec 14 12:50:57 2010 +0100

    s3: Test smbsock_any_connect

commit d8e3d0af9e52c46fa2ab37dc263ae3a5ea5ce020
Author: Volker Lendecke <[email protected]>
Date:   Mon Dec 13 17:17:51 2010 +0100

    s3: Add smbsock_any_connect

commit 49cd92b2186addebba79efffc435083e6086aab1
Author: Volker Lendecke <[email protected]>
Date:   Sun Dec 12 18:55:06 2010 +0100

    s3: Add an async smbsock_connect
    
    This connects to 445 and after 5 milliseconds also to 139. It treats a 
netbios
    session setup failure as equivalent as a TCP connect failure. So if 139 is
    faster but fails the nb session setup, the 445 still has the chance to 
succeed.

commit 02a74fd36db962eaf6da14b920bccbee7d25f07e
Author: Volker Lendecke <[email protected]>
Date:   Sun Dec 12 18:54:31 2010 +0100

    s3: Add async cli_session_request
    
    This does not do the redirects, but I think that might be obsolete anyway

commit 444dcc59c2b0fe616c833efa75a061c46ac12274
Author: Volker Lendecke <[email protected]>
Date:   Sun Dec 12 18:53:49 2010 +0100

    s3: Add some const to name_mangle()

-----------------------------------------------------------------------

Summary of changes:
 source3/Makefile.in                        |    2 +
 source3/include/async_smb.h                |    7 +
 source3/include/proto.h                    |   26 ++-
 source3/lib/util_sock.c                    |  165 --------
 source3/libsmb/async_smb.c                 |  127 ++++++
 source3/libsmb/nmblib.c                    |    2 +-
 source3/libsmb/smbsock_connect.c           |  573 ++++++++++++++++++++++++++++
 source3/torture/proto.h                    |    1 +
 source3/torture/test_smbsock_any_connect.c |   46 +++
 source3/torture/torture.c                  |    1 +
 source3/winbindd/winbindd_cm.c             |   58 +---
 11 files changed, 794 insertions(+), 214 deletions(-)
 create mode 100644 source3/libsmb/smbsock_connect.c
 create mode 100644 source3/torture/test_smbsock_any_connect.c


Changeset truncated at 500 lines:

diff --git a/source3/Makefile.in b/source3/Makefile.in
index ce60055..3e49869 100644
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -592,6 +592,7 @@ LIBSMB_OBJ = libsmb/clientgen.o libsmb/cliconnect.o 
libsmb/clifile.o \
             libsmb/clioplock.o libsmb/clirap2.o \
             libsmb/smb_seal.o libsmb/async_smb.o \
             libsmb/cli_np_tstream.o \
+            libsmb/smbsock_connect.o \
             $(LIBSAMBA_OBJ) \
             $(LIBNMB_OBJ) \
             $(LIBNBT_OBJ) \
@@ -1198,6 +1199,7 @@ SMBTORTURE_OBJ1 = torture/torture.o torture/nbio.o 
torture/scanner.o torture/uta
                torture/denytest.o torture/mangle_test.o \
                torture/nbench.o \
                torture/test_async_echo.o \
+               torture/test_smbsock_any_connect.o \
                torture/test_posix_append.o
 
 SMBTORTURE_OBJ = $(SMBTORTURE_OBJ1) $(PARAM_OBJ) $(TLDAP_OBJ) \
diff --git a/source3/include/async_smb.h b/source3/include/async_smb.h
index b73aed0..d2303cc 100644
--- a/source3/include/async_smb.h
+++ b/source3/include/async_smb.h
@@ -69,4 +69,11 @@ NTSTATUS cli_smb_oplock_break_waiter_recv(struct tevent_req 
*req,
                                          uint16_t *pfnum,
                                          uint8_t *plevel);
 
+struct tevent_req *cli_session_request_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           int sock,
+                                           const struct nmb_name *called,
+                                           const struct nmb_name *calling);
+bool cli_session_request_recv(struct tevent_req *req, int *err, uint8_t *resp);
+
 #endif
diff --git a/source3/include/proto.h b/source3/include/proto.h
index 566b3f3..f9bf72f 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -1331,8 +1331,6 @@ struct tevent_req *open_socket_out_defer_send(TALLOC_CTX 
*mem_ctx,
                                              uint16_t port,
                                              int timeout);
 NTSTATUS open_socket_out_defer_recv(struct tevent_req *req, int *pfd);
-bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
-                        int timeout, int *fd_index, int *fd);
 int open_udp_socket(const char *host, int port);
 const char *get_peer_name(int fd, bool force_lookup);
 const char *get_peer_addr(int fd, char *addr, size_t addr_len);
@@ -2786,7 +2784,7 @@ struct packet_struct *receive_dgram_packet(int fd, int t,
 bool match_mailslot_name(struct packet_struct *p, const char *mailslot_name);
 int matching_len_bits(unsigned char *p1, unsigned char *p2, size_t len);
 void sort_query_replies(char *data, int n, struct in_addr ip);
-char *name_mangle(TALLOC_CTX *mem_ctx, char *In, char name_type);
+char *name_mangle(TALLOC_CTX *mem_ctx, const char *In, char name_type);
 int name_extract(unsigned char *buf,size_t buf_len, unsigned int ofs, fstring 
name);
 int name_len(unsigned char *s1, size_t buf_len);
 
@@ -5617,6 +5615,28 @@ struct tevent_req *fncall_send(TALLOC_CTX *mem_ctx, 
struct tevent_context *ev,
                               void *private_data);
 int fncall_recv(struct tevent_req *req, int *perr);
 
+struct tevent_req *smbsock_connect_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       const struct sockaddr_storage *addr,
+                                       const char *called_name,
+                                       const char *calling_name);
+NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
+                             uint16_t *port);
+NTSTATUS smbsock_connect(const struct sockaddr_storage *addr,
+                        const char *called_name, const char *calling_name,
+                        int *pfd, uint16_t *port);
+
+struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           const struct sockaddr_storage 
*addrs,
+                                           const char **called_names,
+                                           size_t num_addrs);
+NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
+                                 size_t *chosen_index, uint16_t *port);
+NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
+                            const char **called_names, size_t num_addrs,
+                            int *pfd, size_t *chosen_index, uint16_t *port);
+
 /* The following definitions come from rpc_server/srv_samr_nt.c */
 NTSTATUS access_check_object( struct security_descriptor *psd, struct 
security_token *token,
                              enum sec_privilege needed_priv_1, enum 
sec_privilege needed_priv_2,
diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c
index 64cd74c..8b25e01 100644
--- a/source3/lib/util_sock.c
+++ b/source3/lib/util_sock.c
@@ -1111,171 +1111,6 @@ NTSTATUS open_socket_out_defer_recv(struct tevent_req 
*req, int *pfd)
        return NT_STATUS_OK;
 }
 
-/*******************************************************************
- Create an outgoing TCP socket to the first addr that connects.
-
- This is for simultaneous connection attempts to port 445 and 139 of a host
- or for simultatneous connection attempts to multiple DCs at once.  We return
- a socket fd of the first successful connection.
-
- @param[in] addrs list of Internet addresses and ports to connect to
- @param[in] num_addrs number of address/port pairs in the addrs list
- @param[in] timeout time after which we stop waiting for a socket connection
-            to succeed, given in milliseconds
- @param[out] fd_index the entry in addrs which we successfully connected to
- @param[out] fd fd of the open and connected socket
- @return true on a successful connection, false if all connection attempts
-         failed or we timed out
-*******************************************************************/
-
-bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
-                        int timeout, int *fd_index, int *fd)
-{
-       int i, resulting_index, res;
-       int *sockets;
-       bool good_connect;
-
-       fd_set r_fds, wr_fds;
-       struct timeval tv;
-       int maxfd;
-
-       int connect_loop = 10000; /* 10 milliseconds */
-
-       timeout *= 1000;        /* convert to microseconds */
-
-       sockets = SMB_MALLOC_ARRAY(int, num_addrs);
-
-       if (sockets == NULL)
-               return false;
-
-       resulting_index = -1;
-
-       for (i=0; i<num_addrs; i++)
-               sockets[i] = -1;
-
-       for (i=0; i<num_addrs; i++) {
-               sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
-               if (sockets[i] < 0)
-                       goto done;
-               set_blocking(sockets[i], false);
-       }
-
- connect_again:
-       good_connect = false;
-
-       for (i=0; i<num_addrs; i++) {
-               const struct sockaddr * a = 
-                   (const struct sockaddr *)&(addrs[i]);
-
-               if (sockets[i] == -1)
-                       continue;
-
-               if (sys_connect(sockets[i], a) == 0) {
-                       /* Rather unlikely as we are non-blocking, but it
-                        * might actually happen. */
-                       resulting_index = i;
-                       goto done;
-               }
-
-               if (errno == EINPROGRESS || errno == EALREADY ||
-#ifdef EISCONN
-                       errno == EISCONN ||
-#endif
-                   errno == EAGAIN || errno == EINTR) {
-                       /* These are the error messages that something is
-                          progressing. */
-                       good_connect = true;
-               } else if (errno != 0) {
-                       /* There was a direct error */
-                       close(sockets[i]);
-                       sockets[i] = -1;
-               }
-       }
-
-       if (!good_connect) {
-               /* All of the connect's resulted in real error conditions */
-               goto done;
-       }
-
-       /* Lets see if any of the connect attempts succeeded */
-
-       maxfd = 0;
-       FD_ZERO(&wr_fds);
-       FD_ZERO(&r_fds);
-
-       for (i=0; i<num_addrs; i++) {
-               if (sockets[i] == -1)
-                       continue;
-               FD_SET(sockets[i], &wr_fds);
-               FD_SET(sockets[i], &r_fds);
-               if (sockets[i]>maxfd)
-                       maxfd = sockets[i];
-       }
-
-       tv.tv_sec = 0;
-       tv.tv_usec = connect_loop;
-
-       res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
-
-       if (res < 0)
-               goto done;
-
-       if (res == 0)
-               goto next_round;
-
-       for (i=0; i<num_addrs; i++) {
-
-               if (sockets[i] == -1)
-                       continue;
-
-               /* Stevens, Network Programming says that if there's a
-                * successful connect, the socket is only writable. Upon an
-                * error, it's both readable and writable. */
-
-               if (FD_ISSET(sockets[i], &r_fds) &&
-                   FD_ISSET(sockets[i], &wr_fds)) {
-                       /* readable and writable, so it's an error */
-                       close(sockets[i]);
-                       sockets[i] = -1;
-                       continue;
-               }
-
-               if (!FD_ISSET(sockets[i], &r_fds) &&
-                   FD_ISSET(sockets[i], &wr_fds)) {
-                       /* Only writable, so it's connected */
-                       resulting_index = i;
-                       goto done;
-               }
-       }
-
- next_round:
-
-       timeout -= connect_loop;
-       if (timeout <= 0)
-               goto done;
-       connect_loop *= 1.5;
-       if (connect_loop > timeout)
-               connect_loop = timeout;
-       goto connect_again;
-
- done:
-       for (i=0; i<num_addrs; i++) {
-               if (i == resulting_index)
-                       continue;
-               if (sockets[i] >= 0)
-                       close(sockets[i]);
-       }
-
-       if (resulting_index >= 0) {
-               *fd_index = resulting_index;
-               *fd = sockets[*fd_index];
-               set_blocking(*fd, true);
-       }
-
-       free(sockets);
-
-       return (resulting_index >= 0);
-}
 /****************************************************************************
  Open a connected UDP socket to host on port
 **************************************************************************/
diff --git a/source3/libsmb/async_smb.c b/source3/libsmb/async_smb.c
index b04c274..24df6a6 100644
--- a/source3/libsmb/async_smb.c
+++ b/source3/libsmb/async_smb.c
@@ -1095,3 +1095,130 @@ NTSTATUS cli_smb_oplock_break_waiter_recv(struct 
tevent_req *req,
        *plevel = state->level;
        return NT_STATUS_OK;
 }
+
+
+struct cli_session_request_state {
+       struct tevent_context *ev;
+       int sock;
+       uint32 len_hdr;
+       struct iovec iov[3];
+       uint8_t nb_session_response;
+};
+
+static void cli_session_request_sent(struct tevent_req *subreq);
+static void cli_session_request_recvd(struct tevent_req *subreq);
+
+struct tevent_req *cli_session_request_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           int sock,
+                                           const struct nmb_name *called,
+                                           const struct nmb_name *calling)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_session_request_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_session_request_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->ev = ev;
+       state->sock = sock;
+
+       state->iov[1].iov_base = name_mangle(
+               state, called->name, called->name_type);
+       if (tevent_req_nomem(state->iov[1].iov_base, req)) {
+               return tevent_req_post(req, ev);
+       }
+       state->iov[1].iov_len = name_len(
+               (unsigned char *)state->iov[1].iov_base,
+               talloc_get_size(state->iov[1].iov_base));
+
+       state->iov[2].iov_base = name_mangle(
+               state, calling->name, calling->name_type);
+       if (tevent_req_nomem(state->iov[2].iov_base, req)) {
+               return tevent_req_post(req, ev);
+       }
+       state->iov[2].iov_len = name_len(
+               (unsigned char *)state->iov[2].iov_base,
+               talloc_get_size(state->iov[2].iov_base));
+
+       _smb_setlen(((char *)&state->len_hdr),
+                   state->iov[1].iov_len + state->iov[2].iov_len);
+       SCVAL((char *)&state->len_hdr, 0, 0x81);
+
+       state->iov[0].iov_base = &state->len_hdr;
+       state->iov[0].iov_len = sizeof(state->len_hdr);
+
+       subreq = writev_send(state, ev, NULL, sock, true, state->iov, 3);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_session_request_sent, req);
+       return req;
+}
+
+static void cli_session_request_sent(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_session_request_state *state = tevent_req_data(
+               req, struct cli_session_request_state);
+       ssize_t ret;
+       int err;
+
+       ret = writev_recv(subreq, &err);
+       TALLOC_FREE(subreq);
+       if (ret == -1) {
+               tevent_req_error(req, err);
+               return;
+       }
+       subreq = read_smb_send(state, state->ev, state->sock);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, cli_session_request_recvd, req);
+}
+
+static void cli_session_request_recvd(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_session_request_state *state = tevent_req_data(
+               req, struct cli_session_request_state);
+       uint8_t *buf;
+       ssize_t ret;
+       int err;
+
+       ret = read_smb_recv(subreq, talloc_tos(), &buf, &err);
+       TALLOC_FREE(subreq);
+
+       if (ret < 4) {
+               ret = -1;
+               err = EIO;
+       }
+       if (ret == -1) {
+               tevent_req_error(req, err);
+               return;
+       }
+       /*
+        * In case of an error there is more information in the data
+        * portion according to RFC1002. We're not subtle enough to
+        * respond to the different error conditions, so drop the
+        * error info here.
+        */
+       state->nb_session_response = CVAL(buf, 0);
+       tevent_req_done(req);
+}
+
+bool cli_session_request_recv(struct tevent_req *req, int *err, uint8_t *resp)
+{
+       struct cli_session_request_state *state = tevent_req_data(
+               req, struct cli_session_request_state);
+
+       if (tevent_req_is_unix_error(req, err)) {
+               return false;
+       }
+       *resp = state->nb_session_response;
+       return true;
+}
diff --git a/source3/libsmb/nmblib.c b/source3/libsmb/nmblib.c
index 943cbcb..e10bd7a 100644
--- a/source3/libsmb/nmblib.c
+++ b/source3/libsmb/nmblib.c
@@ -1293,7 +1293,7 @@ static int name_interpret(unsigned char *buf, size_t 
buf_len,
  Note:  <Out> must be (33 + strlen(scope) + 2) bytes long, at minimum.
 ****************************************************************************/
 
-char *name_mangle(TALLOC_CTX *mem_ctx, char *In, char name_type)
+char *name_mangle(TALLOC_CTX *mem_ctx, const char *In, char name_type)
 {
        int   i;
        int   len;
diff --git a/source3/libsmb/smbsock_connect.c b/source3/libsmb/smbsock_connect.c
new file mode 100644
index 0000000..8ab12c5
--- /dev/null
+++ b/source3/libsmb/smbsock_connect.c
@@ -0,0 +1,573 @@
+/*
+   Unix SMB/CIFS implementation.
+   Connect to 445 and 139/nbsesssetup
+   Copyright (C) Volker Lendecke 2010
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "../lib/async_req/async_sock.h"
+#include "async_smb.h"
+
+struct nb_connect_state {
+       struct tevent_context *ev;
+       const struct sockaddr_storage *addr;
+       const char *called_name;
+       int sock;
+
+       struct nmb_name called;
+       struct nmb_name calling;
+};
+
+static int nb_connect_state_destructor(struct nb_connect_state *state);
+static void nb_connect_connected(struct tevent_req *subreq);
+static void nb_connect_done(struct tevent_req *subreq);
+
+static struct tevent_req *nb_connect_send(TALLOC_CTX *mem_ctx,
+                                         struct tevent_context *ev,
+                                         const struct sockaddr_storage *addr,
+                                         const char *called_name,
+                                         int called_type,
+                                         const char *calling_name,
+                                         int calling_type)
+{
+       struct tevent_req *req, *subreq;
+       struct nb_connect_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct nb_connect_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->ev = ev;
+       state->called_name = called_name;
+       state->addr = addr;
+
+       state->sock = -1;
+       make_nmb_name(&state->called, called_name, called_type);
+       make_nmb_name(&state->calling, calling_name, calling_type);
+
+       talloc_set_destructor(state, nb_connect_state_destructor);
+
+       subreq = open_socket_out_send(state, ev, addr, 139, 5000);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, nb_connect_connected, req);
+       return req;
+}
+
+static int nb_connect_state_destructor(struct nb_connect_state *state)
+{
+       if (state->sock != -1) {
+               close(state->sock);
+       }
+       return 0;
+}
+
+static void nb_connect_connected(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct nb_connect_state *state = tevent_req_data(


-- 
Samba Shared Repository

Reply via email to