r3315: converted the libcli/raw/ code to use the generic socket library. This
authorAndrew Tridgell <tridge@samba.org>
Thu, 28 Oct 2004 08:15:12 +0000 (08:15 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:04:53 +0000 (13:04 -0500)
allows me to test with the socket:testnonblock option. It passes.
(This used to be commit 7cb4bf8662825d507d8246647ffb10aa08bad794)

source4/include/cli_context.h
source4/libcli/raw/clisocket.c
source4/libcli/raw/clitransport.c

index fa6e293cd910ceeeb8b7528337f96a996c606fa0..bdf90018d5bd379084361270d91d2731e5bebc56 100644 (file)
@@ -70,8 +70,7 @@ struct smbcli_socket {
        /* the port used */
        int port;
        
-       /* the open file descriptor */
-       int fd;
+       struct socket_context *sock;
 
        /* a count of the number of packets we have received. We
         * actually only care about zero/non-zero at this stage */
index 14862a39f068c29138cd63cb7f4e758210ec48be..907206a5e8da886351ae1f412104ee10ea0a50f9 100644 (file)
 
 #include "includes.h"
 
-/*
-  destroy a socket
- */
-static int sock_destructor(void *ptr)
-{
-       struct smbcli_socket *sock = ptr;
-       if (sock->fd != -1) {
-               close(sock->fd);
-               sock->fd = -1;
-       }
-       return 0;
-}
-
 /*
   create a smbcli_socket context
 */
@@ -47,15 +34,13 @@ struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx)
        }
 
        ZERO_STRUCTP(sock);
-       sock->fd = -1;
+       sock->sock = NULL;
        sock->port = 0;
 
        /* 20 second default timeout */
        sock->timeout = 20000;
        sock->hostname = NULL;
 
-       talloc_set_destructor(sock, sock_destructor);
-
        return sock;
 }
 
@@ -65,10 +50,7 @@ struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx)
 */
 BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int port)
 {
-       if (getenv("LIBSMB_PROG")) {
-               sock->fd = sock_exec(getenv("LIBSMB_PROG"));
-               return sock->fd != -1;
-       }
+       NTSTATUS status;
 
        if (port == 0) {
                int i;
@@ -82,18 +64,23 @@ BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int por
                return False;
        }
 
-       sock->dest_ip = *ip;
-       sock->port = port;
-       sock->fd = open_socket_out(SOCK_STREAM,
-                                  &sock->dest_ip,
-                                  sock->port, 
-                                  LONG_CONNECT_TIMEOUT);
-       if (sock->fd == -1) {
+       status = socket_create("ip", SOCKET_TYPE_STREAM, &sock->sock, 0);
+       if (!NT_STATUS_IS_OK(status)) {
                return False;
        }
+       talloc_steal(sock, sock->sock);
 
-       set_blocking(sock->fd, False);
-       set_socket_options(sock->fd, lp_socket_options());
+       status = socket_connect(sock->sock, NULL, 0, inet_ntoa(*ip), port, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(sock->sock);
+               sock->sock = NULL;
+               return False;
+       }
+
+       sock->dest_ip = *ip;
+       sock->port = port;
+
+       socket_set_option(sock->sock, lp_socket_options(), NULL);
 
        return True;
 }
@@ -104,9 +91,9 @@ BOOL smbcli_sock_connect(struct smbcli_socket *sock, struct in_addr *ip, int por
 ****************************************************************************/
 void smbcli_sock_dead(struct smbcli_socket *sock)
 {
-       if (sock->fd != -1) {
-               close(sock->fd);
-               sock->fd = -1;
+       if (sock->sock != NULL) {
+               talloc_free(sock->sock);
+               sock->sock = NULL;
        }
 }
 
@@ -115,7 +102,7 @@ void smbcli_sock_dead(struct smbcli_socket *sock)
 ****************************************************************************/
 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
 {
-       set_socket_options(sock->fd, options);
+       socket_set_option(sock->sock, options, NULL);
 }
 
 /****************************************************************************
@@ -123,12 +110,24 @@ void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
 ****************************************************************************/
 ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t len)
 {
-       if (sock->fd == -1) {
+       NTSTATUS status;
+       DATA_BLOB blob;
+       size_t nsent;
+
+       if (sock->sock == NULL) {
                errno = EIO;
                return -1;
        }
 
-       return write(sock->fd, data, len);
+       blob.data = discard_const(data);
+       blob.length = len;
+
+       status = socket_send(sock->sock, &blob, &nsent, 0);
+       if (NT_STATUS_IS_ERR(status)) {
+               return -1;
+       }
+
+       return nsent;
 }
 
 
@@ -137,12 +136,20 @@ ssize_t smbcli_sock_write(struct smbcli_socket *sock, const char *data, size_t l
 ****************************************************************************/
 ssize_t smbcli_sock_read(struct smbcli_socket *sock, char *data, size_t len)
 {
-       if (sock->fd == -1) {
+       NTSTATUS status;
+       size_t nread;
+
+       if (sock->sock == NULL) {
                errno = EIO;
                return -1;
        }
 
-       return read(sock->fd, data, len);
+       status = socket_recv(sock->sock, data, len, &nread, 0);
+       if (NT_STATUS_IS_ERR(status)) {
+               return -1;
+       }
+
+       return nread;
 }
 
 /****************************************************************************
@@ -155,10 +162,12 @@ BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, in
        char *name, *p;
        BOOL ret;
 
+#if 0
        if (getenv("LIBSMB_PROG")) {
                sock->fd = sock_exec(getenv("LIBSMB_PROG"));
                return sock->fd != -1;
        }
+#endif
 
        name = talloc_strdup(sock, host);
 
index c84c025c7433e7960c7f2f928e7d75ddb83b882b..d82d97a0d9d3ed92aa7cf92639a1851df0c1e6e1 100644 (file)
@@ -82,7 +82,7 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
 
        ZERO_STRUCT(transport->called);
 
-       fde.fd = sock->fd;
+       fde.fd = socket_get_fd(sock->sock);
        fde.flags = EVENT_FD_READ;
        fde.handler = smbcli_transport_event_handler;
        fde.private = transport;
@@ -501,7 +501,7 @@ BOOL smbcli_transport_process(struct smbcli_transport *transport)
 {
        smbcli_transport_process_send(transport);
        smbcli_transport_process_recv(transport);
-       if (transport->socket->fd == -1) {
+       if (transport->socket->sock == NULL) {
                return False;
        }
        return True;
@@ -515,7 +515,7 @@ BOOL smbcli_transport_process(struct smbcli_transport *transport)
 void smbcli_transport_send(struct smbcli_request *req)
 {
        /* check if the transport is dead */
-       if (req->transport->socket->fd == -1) {
+       if (req->transport->socket->sock == NULL) {
                req->state = SMBCLI_REQUEST_ERROR;
                req->status = NT_STATUS_NET_WRITE_FAULT;
                return;