r4055: fixed more places to use type safe allocation macros
[bbaumbach/samba-autobuild/.git] / source4 / libcli / raw / rawnotify.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client change notify operations
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23
24 /****************************************************************************
25 change notify (async send)
26 ****************************************************************************/
27 struct smbcli_request *smb_raw_changenotify_send(struct smbcli_tree *tree, struct smb_notify *parms)
28 {
29         struct smb_nttrans nt;
30         uint16_t setup[4];
31
32         nt.in.max_setup = 0;
33         nt.in.max_param = parms->in.buffer_size;
34         nt.in.max_data = 0;
35         nt.in.setup_count = 4;
36         nt.in.setup = setup;
37         SIVAL(setup, 0, parms->in.completion_filter);
38         SSVAL(setup, 4, parms->in.fnum);
39         SSVAL(setup, 6, parms->in.recursive);   
40         nt.in.function = NT_TRANSACT_NOTIFY_CHANGE;
41         nt.in.params = data_blob(NULL, 0);
42         nt.in.data = data_blob(NULL, 0);
43
44         return smb_raw_nttrans_send(tree, &nt);
45 }
46
47 /****************************************************************************
48 change notify (async recv)
49 ****************************************************************************/
50 NTSTATUS smb_raw_changenotify_recv(struct smbcli_request *req, 
51                                    TALLOC_CTX *mem_ctx, struct smb_notify *parms)
52 {
53         struct smb_nttrans nt;
54         NTSTATUS status;
55         uint32_t ofs, i;
56         struct smbcli_session *session = req?req->session:NULL;
57
58         status = smb_raw_nttrans_recv(req, mem_ctx, &nt);
59         if (!NT_STATUS_IS_OK(status)) {
60                 return status;
61         }
62
63         parms->out.changes = NULL;
64         parms->out.num_changes = 0;
65         
66         /* count them */
67         for (ofs=0; nt.out.params.length - ofs > 12; ) {
68                 uint32_t next = IVAL(nt.out.params.data, ofs);
69                 parms->out.num_changes++;
70                 if (next == 0 ||
71                     ofs + next >= nt.out.params.length) break;
72                 ofs += next;
73         }
74
75         /* allocate array */
76         parms->out.changes = talloc_array_p(mem_ctx, struct notify_changes, parms->out.num_changes);
77         if (!parms->out.changes) {
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         for (i=ofs=0; i<parms->out.num_changes; i++) {
82                 parms->out.changes[i].action = IVAL(nt.out.params.data, ofs+4);
83                 smbcli_blob_pull_string(session, mem_ctx, &nt.out.params, 
84                                      &parms->out.changes[i].name, 
85                                      ofs+8, ofs+12, STR_UNICODE);
86                 ofs += IVAL(nt.out.params.data, ofs);
87         }
88
89         return NT_STATUS_OK;
90 }
91
92
93 /****************************************************************************
94  Send a NT Cancel request - used to hurry along a pending request. Usually
95  used to cancel a pending change notify request
96  note that this request does not expect a response!
97 ****************************************************************************/
98 NTSTATUS smb_raw_ntcancel(struct smbcli_request *oldreq)
99 {
100         struct smbcli_request *req;
101
102         req = smbcli_request_setup_transport(oldreq->transport, SMBntcancel, 0, 0);
103
104         SSVAL(req->out.hdr, HDR_MID, SVAL(oldreq->out.hdr, HDR_MID));   
105         SSVAL(req->out.hdr, HDR_PID, SVAL(oldreq->out.hdr, HDR_PID));   
106         SSVAL(req->out.hdr, HDR_TID, SVAL(oldreq->out.hdr, HDR_TID));   
107         SSVAL(req->out.hdr, HDR_UID, SVAL(oldreq->out.hdr, HDR_UID));   
108
109         /* this request does not expect a reply, so tell the signing
110            subsystem not to allocate an id for a reply */
111         req->sign_single_increment = 1;
112         req->one_way_request = 1;
113
114         smbcli_request_send(req);
115
116         return NT_STATUS_OK;
117 }