Add new async libsmb infrastructure
[abartlet/samba.git/.git] / source3 / include / async_smb.h
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async SMB client requests
4    Copyright (C) Volker Lendecke 2008
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 #ifndef __ASYNC_SMB_H__
21 #define __ASYNC_SMB_H__
22
23 #include "includes.h"
24
25 /**
26  * struct cli_request is the state holder for an async client request we sent
27  * to the server. It can consist of more than one struct async_req that we
28  * have to server if the application did a cli_chain_cork() and
29  * cli_chain_uncork()
30  */
31
32 struct cli_request {
33         /**
34          * "prev" and "next" form the doubly linked list in
35          * cli_state->outstanding_requests
36          */
37         struct cli_request *prev, *next;
38
39         /**
40          * num_async: How many chained requests do we serve?
41          */
42         int num_async;
43
44         /**
45          * async: This is the list of chained requests that were queued up by
46          * cli_request_chain before we sent out this request
47          */
48         struct async_req **async;
49
50         /**
51          * The client connection for this request
52          */
53         struct cli_state *cli;
54
55         /**
56          * The enc_state to decrypt the reply
57          */
58         struct smb_trans_enc_state *enc_state;
59
60         /**
61          * The mid we used for this request. Mainly used to demultiplex on
62          * receiving replies.
63          */
64         uint16_t mid;
65
66         uint32_t seqnum;
67
68         /**
69          * The bytes we have to ship to the server
70          */
71         uint8_t *outbuf;
72
73         /**
74          * How much from "outbuf" did we already send
75          */
76         size_t sent;
77
78         /**
79          * The reply comes in here. Its intended size is implicit by
80          * smb_len(), its current size can be read via talloc_get_size()
81          */
82         char *inbuf;
83
84         /**
85          * Specific requests might add stuff here. Maybe convert this to a
86          * private_pointer at some point.
87          */
88         union {
89                 struct {
90                         off_t ofs;
91                         size_t size;
92                         ssize_t received;
93                         uint8_t *rcvbuf;
94                 } read;
95                 struct {
96                         DATA_BLOB data;
97                         uint16_t num_echos;
98                 } echo;
99         } data;
100
101         /**
102          * For requests that don't follow the strict request/reply pattern
103          * such as the transaction request family and echo requests it is
104          * necessary to break the standard procedure in
105          * handle_incoming_pdu(). For a simple example look at
106          * cli_echo_recv_helper().
107          */
108         struct {
109                 void (*fn)(struct async_req *req);
110                 void *priv;
111         } recv_helper;
112 };
113
114 /*
115  * Ship a new smb request to the server
116  */
117
118 struct async_req *cli_request_send(TALLOC_CTX *mem_ctx,
119                                    struct event_context *ev,
120                                    struct cli_state *cli,
121                                    uint8_t smb_command,
122                                    uint8_t additional_flags,
123                                    uint8_t wct, const uint16_t *vwv,
124                                    size_t bytes_alignment,
125                                    uint32_t num_bytes, const uint8_t *bytes);
126
127 uint16_t cli_wct_ofs(const struct cli_state *cli);
128
129 bool cli_chain_cork(struct cli_state *cli, struct event_context *ev,
130                     size_t size_hint);
131 void cli_chain_uncork(struct cli_state *cli);
132 bool cli_in_chain(struct cli_state *cli);
133 bool smb_splice_chain(uint8_t **poutbuf, uint8_t smb_command,
134                       uint8_t wct, const uint16_t *vwv,
135                       size_t bytes_alignment,
136                       uint32_t num_bytes, const uint8_t *bytes);
137
138 NTSTATUS cli_pull_reply(struct async_req *req,
139                         uint8_t *pwct, uint16_t **pvwv,
140                         uint16_t *pnum_bytes, uint8_t **pbytes);
141
142 /*
143  * Fetch an error out of a NBT packet
144  */
145
146 NTSTATUS cli_pull_error(char *buf);
147
148 /*
149  * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
150  */
151
152 void cli_set_error(struct cli_state *cli, NTSTATUS status);
153
154 struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
155                                       struct event_context *ev,
156                                       struct cli_state *cli,
157                                       uint8_t smb_command,
158                                       uint8_t additional_flags,
159                                       uint8_t wct, uint16_t *vwv,
160                                       int iov_count,
161                                       struct iovec *bytes_iov);
162 bool cli_smb_req_send(struct tevent_req *req);
163 size_t cli_smb_wct_ofs(struct tevent_req **reqs, int num_reqs);
164 bool cli_smb_chain_send(struct tevent_req **reqs, int num_reqs);
165 uint8_t *cli_smb_inbuf(struct tevent_req *req);
166 bool cli_has_async_calls(struct cli_state *cli);
167 void cli_smb_req_unset_pending(struct tevent_req *req);
168 bool cli_smb_req_set_pending(struct tevent_req *req);
169 uint16_t cli_smb_req_mid(struct tevent_req *req);
170 void cli_smb_req_set_mid(struct tevent_req *req, uint16_t mid);
171 struct tevent_req *cli_smb_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
172                                 struct cli_state *cli,
173                                 uint8_t smb_command, uint8_t additional_flags,
174                                 uint8_t wct, uint16_t *vwv,
175                                 uint32_t num_bytes,
176                                 const uint8_t *bytes);
177 NTSTATUS cli_smb_recv(struct tevent_req *req, uint8_t min_wct,
178                       uint8_t *pwct, uint16_t **pvwv,
179                       uint32_t *pnum_bytes, uint8_t **pbytes);
180
181 #endif