s3-build: only include async headers where needed.
[samba.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 "async_smb.h"
22
23 /****************************************************************************
24 send an ack for an oplock break request
25 ****************************************************************************/
26
27 struct cli_oplock_ack_state {
28         uint16_t vwv[8];
29 };
30
31 static void cli_oplock_ack_done(struct tevent_req *subreq);
32
33 struct tevent_req *cli_oplock_ack_send(TALLOC_CTX *mem_ctx,
34                                        struct tevent_context *ev,
35                                        struct cli_state *cli,
36                                        uint16_t fnum, uint8_t level)
37 {
38         struct tevent_req *req, *subreq;
39         struct cli_oplock_ack_state *state;
40
41         req = tevent_req_create(mem_ctx, &state, struct cli_oplock_ack_state);;
42         if (req == NULL) {
43                 return NULL;
44         }
45         SCVAL(state->vwv+0, 0, 0xff);
46         SCVAL(state->vwv+0, 1, 0);
47         SSVAL(state->vwv+1, 0, 0);
48         SSVAL(state->vwv+2, 0, fnum);
49         SCVAL(state->vwv+3, 0, LOCKING_ANDX_OPLOCK_RELEASE);
50         SCVAL(state->vwv+3, 1, level);
51         SIVAL(state->vwv+4, 0, 0); /* timeout */
52         SSVAL(state->vwv+6, 0, 0); /* unlockcount */
53         SSVAL(state->vwv+7, 0, 0); /* lockcount */
54
55         subreq = cli_smb_send(state, ev, cli, SMBlockingX, 0, 8, state->vwv,
56                               0, NULL);
57         if (tevent_req_nomem(subreq, req)) {
58                 return tevent_req_post(req, ev);
59         }
60         tevent_req_set_callback(subreq, cli_oplock_ack_done, req);
61         return req;
62 }
63
64 static void cli_oplock_ack_done(struct tevent_req *subreq)
65 {
66         struct tevent_req *req = tevent_req_callback_data(
67                 subreq, struct tevent_req);
68         NTSTATUS status;
69
70         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
71         TALLOC_FREE(subreq);
72         if (!NT_STATUS_IS_OK(status)) {
73                 tevent_req_nterror(req, status);
74                 return;
75         }
76         tevent_req_done(req);
77 }
78
79 NTSTATUS cli_oplock_ack_recv(struct tevent_req *req)
80 {
81         return tevent_req_simple_recv_ntstatus(req);
82 }
83
84 NTSTATUS cli_oplock_ack(struct cli_state *cli, uint16_t fnum, unsigned char level)
85 {
86         TALLOC_CTX *frame = talloc_stackframe();
87         struct event_context *ev;
88         struct tevent_req *req;
89         NTSTATUS status = NT_STATUS_OK;
90
91         if (cli_has_async_calls(cli)) {
92                 /*
93                  * Can't use sync call while an async call is in flight
94                  */
95                 status = NT_STATUS_INVALID_PARAMETER;
96                 goto fail;
97         }
98
99         ev = event_context_init(frame);
100         if (ev == NULL) {
101                 status = NT_STATUS_NO_MEMORY;
102                 goto fail;
103         }
104
105         req = cli_oplock_ack_send(frame, ev, cli, fnum, level);
106         if (req == NULL) {
107                 status = NT_STATUS_NO_MEMORY;
108                 goto fail;
109         }
110
111         if (!tevent_req_poll(req, ev)) {
112                 status = map_nt_error_from_unix(errno);
113                 goto fail;
114         }
115
116         status = cli_oplock_ack_recv(req);
117  fail:
118         TALLOC_FREE(frame);
119         if (!NT_STATUS_IS_OK(status)) {
120                 cli_set_error(cli, status);
121         }
122         return status;
123 }
124
125 /****************************************************************************
126 set the oplock handler for a connection
127 ****************************************************************************/
128
129 void cli_oplock_handler(struct cli_state *cli, 
130                         NTSTATUS (*handler)(struct cli_state *, uint16_t, unsigned char))
131 {
132         cli->oplock_handler = handler;
133 }