r3443: the next stage in the include files re-organisation.
[samba.git] / source4 / libcli / raw / libcliraw.h
1 /*
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6    Copyright (C) Jeremy Allison 1998
7    Copyright (C) James Myers 2003 <myersjj@samba.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 struct smbcli_tree;  /* forward declare */
25 struct smbcli_request;  /* forward declare */
26 struct smbcli_session;  /* forward declare */
27 struct smbcli_transport;  /* forward declare */
28
29 /* context that will be and has been negotiated between the client and server */
30 struct smbcli_negotiate {
31         /* 
32          * negotiated maximum transmit size - this is given to us by the server
33          */
34         uint32_t max_xmit;
35
36         /* maximum number of requests that can be multiplexed */
37         uint16_t max_mux;
38
39         /* the negotiatiated protocol */
40         enum protocol_types protocol;
41
42         uint8_t sec_mode;               /* security mode returned by negprot */
43         uint8_t key_len;
44         DATA_BLOB server_guid;      /* server_guid */
45         DATA_BLOB secblob;      /* cryptkey or negTokenInit blob */
46         uint32_t sesskey;
47         
48         struct smb_signing_context sign_info;
49
50         /* capabilities that the server reported */
51         uint32_t capabilities;
52         
53         int server_zone;
54         time_t server_time;
55         uint_t readbraw_supported:1;
56         uint_t writebraw_supported:1;
57
58         char *server_domain;
59 };
60         
61 /* this is the context for a SMB socket associated with the socket itself */
62 struct smbcli_socket {
63         struct ipv4_addr dest_ip;
64         /* dest hostname (which may or may not be a DNS name) */
65         char *hostname;
66
67         /* the port used */
68         int port;
69         
70         struct socket_context *sock;
71
72         /* a count of the number of packets we have received. We
73          * actually only care about zero/non-zero at this stage */
74         uint_t pkt_count;
75
76         /* the network address of the client */
77         char *client_addr;
78         
79         /* timeout for socket operations in milliseconds. */
80         int timeout;
81 };
82
83 /*
84   this structure allows applications to control the behaviour of the
85   client library
86 */
87 struct smbcli_options {
88         uint_t use_oplocks:1;
89         uint_t use_level2_oplocks:1;
90         uint_t use_spnego:1;
91         uint32_t max_xmit;
92         uint16_t max_mux;
93 };
94
95 /* this is the context for the client transport layer */
96 struct smbcli_transport {
97         /* socket level info */
98         struct smbcli_socket *socket;
99
100         /* the next mid to be allocated - needed for signing and
101            request matching */
102         uint16_t next_mid;
103         
104         /* negotiated protocol information */
105         struct smbcli_negotiate negotiate;
106
107         /* options to control the behaviour of the client code */
108         struct smbcli_options options;
109
110         /* is a readbraw pending? we need to handle that case
111            specially on receiving packets */
112         uint_t readbraw_pending:1;
113         
114         /* an idle function - if this is defined then it will be
115            called once every period seconds while we are waiting
116            for a packet */
117         struct {
118                 void (*func)(struct smbcli_transport *, void *);
119                 void *private;
120                 uint_t period;
121         } idle;
122
123         /* the error fields from the last message */
124         struct {
125                 enum {ETYPE_NONE, ETYPE_DOS, ETYPE_NT, ETYPE_SOCKET, ETYPE_NBT} etype;
126                 union {
127                         struct {
128                                 uint8_t eclass;
129                                 uint16_t ecode;
130                         } dos;
131                         NTSTATUS nt_status;
132                         enum {SOCKET_READ_TIMEOUT,
133                               SOCKET_READ_EOF,
134                               SOCKET_READ_ERROR,
135                               SOCKET_WRITE_ERROR,
136                               SOCKET_READ_BAD_SIG} socket_error;
137                         uint_t nbt_error;
138                 } e;
139         } error;
140
141         struct {
142                 /* a oplock break request handler */
143                 BOOL (*handler)(struct smbcli_transport *transport, 
144                                 uint16_t tid, uint16_t fnum, uint8_t level, void *private);
145                 /* private data passed to the oplock handler */
146                 void *private;
147         } oplock;
148
149         /* a list of async requests that are pending for send on this connection */
150         struct smbcli_request *pending_send;
151
152         /* a list of async requests that are pending for receive on this connection */
153         struct smbcli_request *pending_recv;
154
155         /* remember the called name - some sub-protocols require us to
156            know the server name */
157         struct nmb_name called;
158
159         /* a buffer for partially received SMB packets. */
160         struct {
161                 uint8_t header[NBT_HDR_SIZE];
162                 size_t req_size;
163                 size_t received;
164                 uint8_t *buffer;
165         } recv_buffer;
166
167         /* the event handle for waiting for socket IO */
168         struct {
169                 struct event_context *ctx;
170                 struct fd_event *fde;
171                 struct timed_event *te;
172         } event;
173 };
174
175 /* this is the context for the user */
176
177 /* this is the context for the session layer */
178 struct smbcli_session { 
179         /* transport layer info */
180         struct smbcli_transport *transport;
181         
182         /* after a session setup the server provides us with
183            a vuid identifying the security context */
184         uint16_t vuid;
185
186         /* default pid for this session */
187         uint32_t pid;
188
189         /* the flags2 for each packet - this allows
190            the user to control these for torture testing */
191         uint16_t flags2;
192
193         DATA_BLOB user_session_key;
194
195         /* the spnego context if we use extented security */
196         struct gensec_security *gensec;
197 };
198
199 /* 
200    smbcli_tree context: internal state for a tree connection. 
201  */
202 struct smbcli_tree {
203         /* session layer info */
204         struct smbcli_session *session;
205
206         uint16_t tid;                   /* tree id, aka cnum */
207         char *device;
208         char *fs_type;
209 };
210
211
212 /*
213   a client request moves between the following 4 states.
214 */
215 enum smbcli_request_state {SMBCLI_REQUEST_INIT, /* we are creating the request */
216                         SMBCLI_REQUEST_SEND, /* the request is in the outgoing socket Q */
217                         SMBCLI_REQUEST_RECV, /* we are waiting for a matching reply */
218                         SMBCLI_REQUEST_DONE, /* the request is finished */
219                         SMBCLI_REQUEST_ERROR}; /* a packet or transport level error has occurred */
220
221 /* the context for a single SMB request. This is passed to any request-context 
222  * functions (similar to context.h, the server version).
223  * This will allow requests to be multi-threaded. */
224 struct smbcli_request {
225         /* allow a request to be part of a list of requests */
226         struct smbcli_request *next, *prev;
227
228         /* each request is in one of 4 possible states */
229         enum smbcli_request_state state;
230         
231         /* a request always has a transport context, nearly always has
232            a session context and usually has a tree context */
233         struct smbcli_transport *transport;
234         struct smbcli_session *session;
235         struct smbcli_tree *tree;
236
237         /* the flags2 from the SMB request, in raw form (host byte
238            order). Used to parse strings */
239         uint16_t flags2;
240
241         /* the NT status for this request. Set by packet receive code
242            or code detecting error. */
243         NTSTATUS status;
244         
245         /* the sequence number of this packet - used for signing */
246         uint_t seq_num;
247
248         /* set if this is a one-way request, meaning we are not
249            expecting a reply from the server. */
250         uint_t one_way_request:1;
251
252         /* set this when the request should only increment the signing
253            counter by one */
254         uint_t sign_single_increment:1;
255
256         /* the mid of this packet - used to match replies */
257         uint16_t mid;
258
259         struct request_buffer in;
260         struct request_buffer out;
261
262         /* information on what to do with a reply when it is received
263            asyncronously. If this is not setup when a reply is received then
264            the reply is discarded
265
266            The private pointer is private to the caller of the client
267            library (the application), not private to the library
268         */
269         struct {
270                 void (*fn)(struct smbcli_request *);
271                 void *private;
272         } async;
273 };
274
275 /* useful way of catching wct errors with file and line number */
276 #define SMBCLI_CHECK_MIN_WCT(req, wcount) if ((req)->in.wct < (wcount)) { \
277       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected min %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
278       req->status = NT_STATUS_INVALID_PARAMETER; \
279       goto failed; \
280 }
281
282 #define SMBCLI_CHECK_WCT(req, wcount) if ((req)->in.wct != (wcount)) { \
283       DEBUG(1,("Unexpected WCT %d at %s(%d) - expected %d\n", (req)->in.wct, __FILE__, __LINE__, wcount)); \
284       req->status = NT_STATUS_INVALID_PARAMETER; \
285       goto failed; \
286 }