Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[amitay/samba.git] / source4 / libcli / dgram / mailslot.c
index ca9a66a729d94294d7dada0b36d1e19d4f8a3941..4f9d0bfc7c03ee985270c5887759bf31486ed339 100644 (file)
@@ -1,13 +1,13 @@
 /* 
    Unix SMB/CIFS implementation.
 
-   packet handling for mailslot requests
-
+   packet handling for mailslot requests
+   
    Copyright (C) Andrew Tridgell 2005
    
    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/>.
+*/
+
+/*
+   This implements "Class 2 mailslots", i.e. the communication mechanism 
+   used for all mailslot packets smaller than 425 bytes. 
+
+   "Class 1 mailslots" (which use SMB) are used for messages larger 
+   than 426 bytes and are supported on some systems. These are not implemented
+   in Samba4 yet, as there don't appear to be any core services that use
+   them.
+
+   425 and 426-byte sized messages are not supported at all.
 */
 
 #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 "param/param.h"
 
 /*
   destroy a mailslot handler
 */
-static int dgram_mailslot_destructor(void *ptr)
+static int dgram_mailslot_destructor(struct dgram_mailslot_handler *dgmslot)
 {
-       struct dgram_mailslot_handler *dgmslot = 
-               talloc_get_type(ptr, struct dgram_mailslot_handler);
-       
        DLIST_REMOVE(dgmslot->dgmsock->mailslot_handlers, dgmslot);
        return 0;
 }
@@ -97,7 +105,6 @@ const char *dgram_mailslot_name(struct nbt_dgram_packet *packet)
        }
        if (packet->data.msg.dgram_body_type != DGRAM_SMB) return NULL;
        if (packet->data.msg.body.smb.smb_command != SMB_TRANSACTION) return NULL;
-       if (packet->data.msg.body.smb.smb_command != SMB_TRANSACTION) return NULL;
        return packet->data.msg.body.smb.body.trans.mailslot_name;
 }
 
@@ -127,7 +134,9 @@ struct dgram_mailslot_handler *dgram_mailslot_temp(struct nbt_dgram_socket *dgms
                }
                dgmslot = dgram_mailslot_listen(dgmsock, name, handler, private);
                talloc_free(name);
-               return dgmslot;
+               if (dgmslot != NULL) {
+                       return dgmslot;
+               }
        }
        DEBUG(2,("Unable to create temporary mailslot from %s\n", mailslot_name));
        return NULL;
@@ -141,8 +150,7 @@ NTSTATUS dgram_mailslot_send(struct nbt_dgram_socket *dgmsock,
                             enum dgram_msg_type msg_type,
                             const char *mailslot_name,
                             struct nbt_name *dest_name,
-                            const char *dest_address,
-                            int dest_port,
+                            struct socket_address *dest,
                             struct nbt_name *src_name,
                             DATA_BLOB *request)
 {
@@ -151,23 +159,28 @@ NTSTATUS dgram_mailslot_send(struct nbt_dgram_socket *dgmsock,
        struct dgram_message *msg;
        struct dgram_smb_packet *smb;
        struct smb_trans_body *trans;
+       struct socket_address *src;
        NTSTATUS status;
 
-       if (dest_port == 0) {
-               dest_port = lp_dgram_port();
+       if (dest->port == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
        ZERO_STRUCT(packet);
        packet.msg_type = msg_type;
        packet.flags = DGRAM_FLAG_FIRST | DGRAM_NODE_NBDD;
        packet.dgram_id = generate_random() % UINT16_MAX;
-       packet.source = socket_get_my_addr(dgmsock->sock, tmp_ctx);
-       packet.src_port = socket_get_my_port(dgmsock->sock);
+       src = socket_get_my_addr(dgmsock->sock, tmp_ctx);
+       if (!src) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       packet.src_addr = src->addr;
+       packet.src_port = src->port;
 
        msg = &packet.data.msg;
        /* this length calculation is very crude - it should be based on gensize
           calls */
-       msg->length = 138 + strlen(mailslot_name) + request->length; 
+       msg->length = 138 + strlen(mailslot_name) + request->length;
        msg->offset = 0;
 
        msg->source_name = *src_name;
@@ -188,9 +201,27 @@ NTSTATUS dgram_mailslot_send(struct nbt_dgram_socket *dgmsock,
        trans->mailslot_name = mailslot_name;
        trans->data = *request;
 
-       status = nbt_dgram_send(dgmsock, &packet, dest_address, dest_port);
+       status = nbt_dgram_send(dgmsock, &packet, dest);
 
        talloc_free(tmp_ctx);
 
        return status;
 }
+
+/*
+  return the mailslot data portion from a mailslot packet
+*/
+DATA_BLOB dgram_mailslot_data(struct nbt_dgram_packet *dgram)
+{
+       struct smb_trans_body *trans = &dgram->data.msg.body.smb.body.trans;
+       DATA_BLOB ret = trans->data;
+       int pad = trans->data_offset - (70 + strlen(trans->mailslot_name));
+
+       if (pad < 0 || pad > ret.length) {
+               DEBUG(2,("Badly formatted data in mailslot - pad = %d\n", pad));
+               return data_blob(NULL, 0);
+       }
+       ret.data += pad;
+       ret.length -= pad;
+       return ret;     
+}