libcli: continue to read from the socket even if the size is 0
[mat/samba.git] / source4 / libcli / dgram / dgramsocket.c
index 6c6a45f48e49a5b8c811daf97d28abe5d615f97d..cd6d3e4c745bf5069cf8cac8566cd23cb645ac77 100644 (file)
@@ -7,7 +7,7 @@
    
    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 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/events/events.h"
-#include "dlinklist.h"
-#include "libcli/nbt/libnbt.h"
+#include "../lib/util/dlinklist.h"
 #include "libcli/dgram/libdgram.h"
 #include "lib/socket/socket.h"
+#include "librpc/gen_ndr/ndr_nbt.h"
 
 
 /*
@@ -35,11 +34,12 @@ static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
 {
        TALLOC_CTX *tmp_ctx = talloc_new(dgmsock);
        NTSTATUS status;
-       struct nbt_peer_socket src;
+       struct socket_address *src;
        DATA_BLOB blob;
        size_t nread, dsize;
        struct nbt_dgram_packet *packet;
        const char *mailslot_name;
+       enum ndr_err_code ndr_err;
 
        status = socket_pending(dgmsock->sock, &dsize);
        if (!NT_STATUS_IS_OK(status)) {
@@ -48,22 +48,21 @@ static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
        }
 
        blob = data_blob_talloc(tmp_ctx, NULL, dsize);
-       if (blob.data == NULL) {
+       if ((dsize != 0) && (blob.data == NULL)) {
                talloc_free(tmp_ctx);
                return;
        }
 
-       status = socket_recvfrom(dgmsock->sock, blob.data, blob.length, &nread, 0,
-                                &src.addr, &src.port);
+       status = socket_recvfrom(dgmsock->sock, blob.data, blob.length, &nread,
+                                tmp_ctx, &src);
        if (!NT_STATUS_IS_OK(status)) {
                talloc_free(tmp_ctx);
                return;
        }
-       talloc_steal(tmp_ctx, src.addr);
        blob.length = nread;
 
-       DEBUG(2,("Received dgram packet of length %d from %s:%d\n", 
-                (int)blob.length, src.addr, src.port));
+       DEBUG(5,("Received dgram packet of length %d from %s:%d\n", 
+                (int)blob.length, src->addr, src->port));
 
        packet = talloc(tmp_ctx, struct nbt_dgram_packet);
        if (packet == NULL) {
@@ -72,9 +71,10 @@ static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
        }
 
        /* parse the request */
-       status = ndr_pull_struct_blob(&blob, packet, packet, 
+       ndr_err = ndr_pull_struct_blob(&blob, packet, packet,
                                      (ndr_pull_flags_fn_t)ndr_pull_nbt_dgram_packet);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
                DEBUG(2,("Failed to parse incoming NBT DGRAM packet - %s\n",
                         nt_errstr(status)));
                talloc_free(tmp_ctx);
@@ -87,14 +87,14 @@ static void dgm_socket_recv(struct nbt_dgram_socket *dgmsock)
                struct dgram_mailslot_handler *dgmslot;
                dgmslot = dgram_mailslot_find(dgmsock, mailslot_name);
                if (dgmslot) {
-                       dgmslot->handler(dgmslot, packet, &src);
+                       dgmslot->handler(dgmslot, packet, src);
                } else {
                        DEBUG(2,("No mailslot handler for '%s'\n", mailslot_name));
                }
        } else {
                /* dispatch if there is a general handler */
                if (dgmsock->incoming.handler) {
-                       dgmsock->incoming.handler(dgmsock, packet, &src);
+                       dgmsock->incoming.handler(dgmsock, packet, src);
                }
        }
 
@@ -114,11 +114,11 @@ static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
                size_t len;
                
                len = req->encoded.length;
-               status = socket_sendto(dgmsock->sock, &req->encoded, &len, 0, 
-                                      req->dest.addr, req->dest.port);
+               status = socket_sendto(dgmsock->sock, &req->encoded, &len,
+                                      req->dest);
                if (NT_STATUS_IS_ERR(status)) {
                        DEBUG(3,("Failed to send datagram of length %u to %s:%d: %s\n",
-                                (unsigned)req->encoded.length, req->dest.addr, req->dest.port, 
+                                (unsigned)req->encoded.length, req->dest->addr, req->dest->port, 
                                 nt_errstr(status)));
                        DLIST_REMOVE(dgmsock->send_queue, req);
                        talloc_free(req);
@@ -131,7 +131,7 @@ static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
                talloc_free(req);
        }
 
-       EVENT_FD_NOT_WRITEABLE(dgmsock->fde);
+       TEVENT_FD_NOT_WRITEABLE(dgmsock->fde);
        return;
 }
 
@@ -139,15 +139,15 @@ static void dgm_socket_send(struct nbt_dgram_socket *dgmsock)
 /*
   handle fd events on a nbt_dgram_socket
 */
-static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
-                              uint16_t flags, void *private)
+static void dgm_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
+                              uint16_t flags, void *private_data)
 {
-       struct nbt_dgram_socket *dgmsock = talloc_get_type(private
+       struct nbt_dgram_socket *dgmsock = talloc_get_type(private_data,
                                                           struct nbt_dgram_socket);
-       if (flags & EVENT_FD_WRITE) {
+       if (flags & TEVENT_FD_WRITE) {
                dgm_socket_send(dgmsock);
        } 
-       if (flags & EVENT_FD_READ) {
+       if (flags & TEVENT_FD_READ) {
                dgm_socket_recv(dgmsock);
        }
 }
@@ -157,7 +157,7 @@ static void dgm_socket_handler(struct event_context *ev, struct fd_event *fde,
   then operations will use that event context
 */
 struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx, 
-                                             struct event_context *event_ctx)
+                                             struct tevent_context *event_ctx)
 {
        struct nbt_dgram_socket *dgmsock;
        NTSTATUS status;
@@ -165,11 +165,7 @@ struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx,
        dgmsock = talloc(mem_ctx, struct nbt_dgram_socket);
        if (dgmsock == NULL) goto failed;
 
-       if (event_ctx == NULL) {
-               dgmsock->event_ctx = event_context_init(dgmsock);
-       } else {
-               dgmsock->event_ctx = talloc_reference(dgmsock, event_ctx);
-       }
+       dgmsock->event_ctx = event_ctx;
        if (dgmsock->event_ctx == NULL) goto failed;
 
        status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0);
@@ -179,7 +175,7 @@ struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx,
 
        talloc_steal(dgmsock, dgmsock->sock);
 
-       dgmsock->fde = event_add_fd(dgmsock->event_ctx, dgmsock, 
+       dgmsock->fde = tevent_add_fd(dgmsock->event_ctx, dgmsock,
                                    socket_get_fd(dgmsock->sock), 0,
                                    dgm_socket_handler, dgmsock);
 
@@ -201,12 +197,12 @@ failed:
 NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
                                    void (*handler)(struct nbt_dgram_socket *, 
                                                    struct nbt_dgram_packet *, 
-                                                   const struct nbt_peer_socket *),
-                                   void *private)
+                                                   struct socket_address *),
+                                   void *private_data)
 {
        dgmsock->incoming.handler = handler;
-       dgmsock->incoming.private = private;
-       EVENT_FD_READABLE(dgmsock->fde);
+       dgmsock->incoming.private_data = private_data;
+       TEVENT_FD_READABLE(dgmsock->fde);
        return NT_STATUS_OK;
 }
 
@@ -216,25 +212,28 @@ NTSTATUS dgram_set_incoming_handler(struct nbt_dgram_socket *dgmsock,
 */
 NTSTATUS nbt_dgram_send(struct nbt_dgram_socket *dgmsock,
                        struct nbt_dgram_packet *packet,
-                       const struct nbt_peer_socket *dest)
+                       struct socket_address *dest)
 {
        struct nbt_dgram_request *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
+       enum ndr_err_code ndr_err;
 
        req = talloc(dgmsock, struct nbt_dgram_request);
        if (req == NULL) goto failed;
 
-       req->dest.port = dest->port;
-       req->dest.addr = talloc_strdup(req, dest->addr);
-       if (req->dest.addr == NULL) goto failed;
+       req->dest = dest;
+       if (talloc_reference(req, dest) == NULL) goto failed;
 
-       status = ndr_push_struct_blob(&req->encoded, req, packet, 
+       ndr_err = ndr_push_struct_blob(&req->encoded, req, packet,
                                      (ndr_push_flags_fn_t)ndr_push_nbt_dgram_packet);
-       if (!NT_STATUS_IS_OK(status)) goto failed;
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = ndr_map_error2ntstatus(ndr_err);
+               goto failed;
+       }
 
        DLIST_ADD_END(dgmsock->send_queue, req, struct nbt_dgram_request *);
 
-       EVENT_FD_WRITEABLE(dgmsock->fde);
+       TEVENT_FD_WRITEABLE(dgmsock->fde);
 
        return NT_STATUS_OK;