s3:libsmb: get rid of cli_smb_req_*,cli_smb_wct_ofs,cli_smb_chain_send
[obnox/samba/samba-obnox.git] / source3 / libsmb / clioplock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client oplock functions
4    Copyright (C) Andrew Tridgell 2001
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
23 #include "libsmb/libsmb.h"
24 #include "../libcli/smb/smbXcli_base.h"
25
26 struct cli_smb_oplock_break_waiter_state {
27         uint16_t fnum;
28         uint8_t level;
29 };
30
31 static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq);
32
33 struct tevent_req *cli_smb_oplock_break_waiter_send(TALLOC_CTX *mem_ctx,
34                                                     struct event_context *ev,
35                                                     struct cli_state *cli)
36 {
37         struct tevent_req *req, *subreq;
38         struct cli_smb_oplock_break_waiter_state *state;
39
40         req = tevent_req_create(mem_ctx, &state,
41                                 struct cli_smb_oplock_break_waiter_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45
46         /*
47          * Create a fake SMB request that we will never send out. This is only
48          * used to be set into the pending queue with the right mid.
49          */
50         subreq = cli_smb_req_create(mem_ctx, ev, cli, 0, 0, 0, NULL, 0, NULL);
51         if (tevent_req_nomem(subreq, req)) {
52                 return tevent_req_post(req, ev);
53         }
54         smb1cli_req_set_mid(subreq, 0xffff);
55
56         if (!smbXcli_req_set_pending(subreq)) {
57                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
58                 return tevent_req_post(req, ev);
59         }
60         tevent_req_set_callback(subreq, cli_smb_oplock_break_waiter_done, req);
61         return req;
62 }
63
64 static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq)
65 {
66         struct tevent_req *req = tevent_req_callback_data(
67                 subreq, struct tevent_req);
68         struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
69                 req, struct cli_smb_oplock_break_waiter_state);
70         uint8_t wct;
71         uint16_t *vwv;
72         uint32_t num_bytes;
73         uint8_t *bytes;
74         uint8_t *inbuf;
75         NTSTATUS status;
76
77         status = cli_smb_recv(subreq, state, &inbuf, 8, &wct, &vwv,
78                               &num_bytes, &bytes);
79         TALLOC_FREE(subreq);
80         if (!NT_STATUS_IS_OK(status)) {
81                 tevent_req_nterror(req, status);
82                 return;
83         }
84         state->fnum = SVAL(vwv+2, 0);
85         state->level = CVAL(vwv+3, 1);
86         tevent_req_done(req);
87 }
88
89 NTSTATUS cli_smb_oplock_break_waiter_recv(struct tevent_req *req,
90                                           uint16_t *pfnum,
91                                           uint8_t *plevel)
92 {
93         struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
94                 req, struct cli_smb_oplock_break_waiter_state);
95         NTSTATUS status;
96
97         if (tevent_req_is_nterror(req, &status)) {
98                 return status;
99         }
100         *pfnum = state->fnum;
101         *plevel = state->level;
102         return NT_STATUS_OK;
103 }
104
105 /****************************************************************************
106 send an ack for an oplock break request
107 ****************************************************************************/
108
109 struct cli_oplock_ack_state {
110         uint16_t vwv[8];
111 };
112
113 static void cli_oplock_ack_done(struct tevent_req *subreq);
114
115 struct tevent_req *cli_oplock_ack_send(TALLOC_CTX *mem_ctx,
116                                        struct tevent_context *ev,
117                                        struct cli_state *cli,
118                                        uint16_t fnum, uint8_t level)
119 {
120         struct tevent_req *req, *subreq;
121         struct cli_oplock_ack_state *state;
122
123         req = tevent_req_create(mem_ctx, &state, struct cli_oplock_ack_state);
124         if (req == NULL) {
125                 return NULL;
126         }
127         SCVAL(state->vwv+0, 0, 0xff);
128         SCVAL(state->vwv+0, 1, 0);
129         SSVAL(state->vwv+1, 0, 0);
130         SSVAL(state->vwv+2, 0, fnum);
131         SCVAL(state->vwv+3, 0, LOCKING_ANDX_OPLOCK_RELEASE);
132         SCVAL(state->vwv+3, 1, level);
133         SIVAL(state->vwv+4, 0, 0); /* timeout */
134         SSVAL(state->vwv+6, 0, 0); /* unlockcount */
135         SSVAL(state->vwv+7, 0, 0); /* lockcount */
136
137         subreq = cli_smb_send(state, ev, cli, SMBlockingX, 0, 8, state->vwv,
138                               0, NULL);
139         if (tevent_req_nomem(subreq, req)) {
140                 return tevent_req_post(req, ev);
141         }
142         tevent_req_set_callback(subreq, cli_oplock_ack_done, req);
143         return req;
144 }
145
146 static void cli_oplock_ack_done(struct tevent_req *subreq)
147 {
148         struct tevent_req *req = tevent_req_callback_data(
149                 subreq, struct tevent_req);
150         NTSTATUS status;
151
152         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
153         TALLOC_FREE(subreq);
154         if (!NT_STATUS_IS_OK(status)) {
155                 tevent_req_nterror(req, status);
156                 return;
157         }
158         tevent_req_done(req);
159 }
160
161 NTSTATUS cli_oplock_ack_recv(struct tevent_req *req)
162 {
163         return tevent_req_simple_recv_ntstatus(req);
164 }
165