7d8ca49e9abd1f39f8ae910bafd2fe6eb822e660
[ira/wip.git] / libcli / nbt / libnbt.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    a raw async NBT library
5
6    Copyright (C) Andrew Tridgell 2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef __LIBNBT_H__
23 #define __LIBNBT_H__
24
25 #include "librpc/gen_ndr/nbt.h"
26 #include "librpc/ndr/libndr.h"
27
28 /*
29   possible states for pending requests
30 */
31 enum nbt_request_state {NBT_REQUEST_SEND,
32                         NBT_REQUEST_WAIT,
33                         NBT_REQUEST_DONE,
34                         NBT_REQUEST_TIMEOUT,
35                         NBT_REQUEST_ERROR};
36
37 /*
38   a nbt name request
39 */
40 struct nbt_name_request {
41         struct nbt_name_request *next, *prev;
42
43         enum nbt_request_state state;
44
45         NTSTATUS status;
46
47         /* the socket this was on */
48         struct nbt_name_socket *nbtsock;
49
50         /* where to send the request */
51         struct socket_address *dest;
52
53         /* timeout between retries */
54         int timeout;
55
56         /* how many retries to send on timeout */
57         int num_retries;
58
59         /* whether we have received a WACK */
60         bool received_wack;
61
62         /* the timeout event */
63         struct timed_event *te;
64
65         /* the name transaction id */
66         uint16_t name_trn_id;
67
68         /* is it a reply? */
69         bool is_reply;
70
71         /* the encoded request */
72         DATA_BLOB encoded;
73
74         /* shall we allow multiple replies? */
75         bool allow_multiple_replies;
76
77         unsigned int num_replies;
78         struct nbt_name_reply {
79                 struct nbt_name_packet *packet;
80                 struct socket_address *dest;
81         } *replies;
82
83         /* information on what to do on completion */
84         struct {
85                 void (*fn)(struct nbt_name_request *);
86                 void *private_data;
87         } async;
88 };
89
90
91
92 /*
93   context structure for operations on name queries
94 */
95 struct nbt_name_socket {
96         struct socket_context *sock;
97         struct event_context *event_ctx;
98         struct smb_iconv_convenience *iconv_convenience;
99
100         /* a queue of requests pending to be sent */
101         struct nbt_name_request *send_queue;
102
103         /* the fd event */
104         struct fd_event *fde;
105
106         /* mapping from name_trn_id to pending event */
107         struct idr_context *idr;
108
109         /* how many requests are waiting for a reply */
110         uint16_t num_pending;
111
112         /* what to do with incoming request packets */
113         struct {
114                 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
115                                 struct socket_address *);
116                 void *private_data;
117         } incoming;
118
119         /* what to do with unexpected replies */
120         struct {
121                 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
122                                 struct socket_address *);
123                 void *private_data;
124         } unexpected;
125
126         uint32_t wack_timeout;
127 };
128
129
130 /* a simple name query */
131 struct nbt_name_query {
132         struct {
133                 struct nbt_name name;
134                 const char *dest_addr;
135                 uint16_t dest_port;
136                 bool broadcast;
137                 bool wins_lookup;
138                 int timeout; /* in seconds */
139                 int retries;
140         } in;
141         struct {
142                 const char *reply_from;
143                 struct nbt_name name;
144                 int16_t num_addrs;
145                 const char **reply_addrs;
146         } out;
147 };
148
149 /* a simple name status query */
150 struct nbt_name_status {
151         struct {
152                 struct nbt_name name;
153                 const char *dest_addr;
154                 uint16_t dest_port;
155                 int timeout; /* in seconds */
156                 int retries;
157         } in;
158         struct {
159                 const char *reply_from;
160                 struct nbt_name name;
161                 struct nbt_rdata_status status;
162         } out;
163 };
164
165 /* a name registration request */
166 struct nbt_name_register {
167         struct {
168                 struct nbt_name name;
169                 const char *dest_addr;
170                 uint16_t dest_port;
171                 const char *address;
172                 uint16_t nb_flags;
173                 bool register_demand;
174                 bool broadcast;
175                 bool multi_homed;
176                 uint32_t ttl;
177                 int timeout; /* in seconds */
178                 int retries;
179         } in;
180         struct {
181                 const char *reply_from;
182                 struct nbt_name name;
183                 const char *reply_addr;
184                 uint8_t rcode;
185         } out;
186 };
187
188 /* a send 3 times then demand name broadcast name registration */
189 struct nbt_name_register_bcast {
190         struct {
191                 struct nbt_name name;
192                 const char *dest_addr;
193                 uint16_t dest_port;
194                 const char *address;
195                 uint16_t nb_flags;
196                 uint32_t ttl;
197         } in;
198 };
199
200
201 /* wins name register with multiple wins servers to try and multiple
202    addresses to register */
203 struct nbt_name_register_wins {
204         struct {
205                 struct nbt_name name;
206                 const char **wins_servers;
207                 uint16_t wins_port;
208                 const char **addresses;
209                 uint16_t nb_flags;
210                 uint32_t ttl;
211         } in;
212         struct {
213                 const char *wins_server;
214                 uint8_t rcode;
215         } out;
216 };
217
218
219
220 /* a name refresh request */
221 struct nbt_name_refresh {
222         struct {
223                 struct nbt_name name;
224                 const char *dest_addr;
225                 uint16_t dest_port;
226                 const char *address;
227                 uint16_t nb_flags;
228                 bool broadcast;
229                 uint32_t ttl;
230                 int timeout; /* in seconds */
231                 int retries;
232         } in;
233         struct {
234                 const char *reply_from;
235                 struct nbt_name name;
236                 const char *reply_addr;
237                 uint8_t rcode;
238         } out;
239 };
240
241 /* wins name refresh with multiple wins servers to try and multiple
242    addresses to register */
243 struct nbt_name_refresh_wins {
244         struct {
245                 struct nbt_name name;
246                 const char **wins_servers;
247                 uint16_t wins_port;
248                 const char **addresses;
249                 uint16_t nb_flags;
250                 uint32_t ttl;
251         } in;
252         struct {
253                 const char *wins_server;
254                 uint8_t rcode;
255         } out;
256 };
257
258
259 /* a name release request */
260 struct nbt_name_release {
261         struct {
262                 struct nbt_name name;
263                 const char *dest_addr;
264                 uint16_t dest_port;
265                 const char *address;
266                 uint16_t nb_flags;
267                 bool broadcast;
268                 int timeout; /* in seconds */
269                 int retries;
270         } in;
271         struct {
272                 const char *reply_from;
273                 struct nbt_name name;
274                 const char *reply_addr;
275                 uint8_t rcode;
276         } out;
277 };
278
279 struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx,
280                                              struct event_context *event_ctx,
281                                              struct smb_iconv_convenience *iconv_convenience);
282 struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock,
283                                              struct nbt_name_query *io);
284 NTSTATUS nbt_name_query_recv(struct nbt_name_request *req,
285                              TALLOC_CTX *mem_ctx, struct nbt_name_query *io);
286 NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock,
287                         TALLOC_CTX *mem_ctx, struct nbt_name_query *io);
288 struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
289                                               struct nbt_name_status *io);
290 NTSTATUS nbt_name_status_recv(struct nbt_name_request *req,
291                              TALLOC_CTX *mem_ctx, struct nbt_name_status *io);
292 NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock,
293                         TALLOC_CTX *mem_ctx, struct nbt_name_status *io);
294
295 NTSTATUS nbt_name_dup(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name *newname);
296 NTSTATUS nbt_name_to_blob(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, DATA_BLOB *blob, struct nbt_name *name);
297 NTSTATUS nbt_name_from_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, struct nbt_name *name);
298 void nbt_choose_called_name(TALLOC_CTX *mem_ctx, struct nbt_name *n, const char *name, int type);
299 char *nbt_name_string(TALLOC_CTX *mem_ctx, const struct nbt_name *name);
300 NTSTATUS nbt_name_register(struct nbt_name_socket *nbtsock,
301                            TALLOC_CTX *mem_ctx, struct nbt_name_register *io);
302 NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
303                            TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io);
304 NTSTATUS nbt_name_release(struct nbt_name_socket *nbtsock,
305                            TALLOC_CTX *mem_ctx, struct nbt_name_release *io);
306 NTSTATUS nbt_name_register_wins(struct nbt_name_socket *nbtsock,
307                                 TALLOC_CTX *mem_ctx,
308                                 struct nbt_name_register_wins *io);
309 NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
310                                 TALLOC_CTX *mem_ctx,
311                                 struct nbt_name_refresh_wins *io);
312 NTSTATUS nbt_name_register_recv(struct nbt_name_request *req,
313                                 TALLOC_CTX *mem_ctx, struct nbt_name_register *io);
314 struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock,
315                                                 struct nbt_name_register *io);
316 NTSTATUS nbt_name_release_recv(struct nbt_name_request *req,
317                                TALLOC_CTX *mem_ctx, struct nbt_name_release *io);
318
319 struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock,
320                                                struct nbt_name_release *io);
321
322 NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
323                                TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io);
324
325 NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock,
326                                   void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
327                                                   struct socket_address *),
328                                   void *private_data);
329 NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock,
330                              struct socket_address *dest,
331                              struct nbt_name_packet *request);
332
333
334 NDR_SCALAR_PROTO(wrepl_nbt_name, const struct nbt_name *)
335 NDR_SCALAR_PROTO(nbt_string, const char *)
336 NDR_BUFFER_PROTO(nbt_name, struct nbt_name)
337 NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode);
338
339 struct composite_context;
340 struct composite_context *nbt_name_register_bcast_send(struct nbt_name_socket *nbtsock,
341                                                        struct nbt_name_register_bcast *io);
342 NTSTATUS nbt_name_register_bcast_recv(struct composite_context *c);
343 struct composite_context *nbt_name_register_wins_send(struct nbt_name_socket *nbtsock,
344                                                       struct nbt_name_register_wins *io);
345 NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
346                                      struct nbt_name_refresh_wins *io);
347 struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
348                                                       struct nbt_name_refresh_wins *io);
349 NTSTATUS nbt_name_register_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
350                                      struct nbt_name_register_wins *io);
351
352
353 #endif /* __LIBNBT_H__ */