r10997: r11980@SERNOX (orig r10037): metze | 2005-09-05 14:21:40 +0200
[kai/samba-autobuild/.git] / source4 / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "librpc/gen_ndr/ndr_nbt.h"
24
25 /*
26   possible states for pending requests
27 */
28 enum nbt_request_state {NBT_REQUEST_SEND, 
29                         NBT_REQUEST_WAIT, 
30                         NBT_REQUEST_DONE,
31                         NBT_REQUEST_TIMEOUT,
32                         NBT_REQUEST_ERROR};
33
34 /* where to send the request/replies */
35 struct nbt_peer_socket {
36         const char *addr;
37         int port;
38 };
39
40 /*
41   a nbt name request
42 */
43 struct nbt_name_request {
44         struct nbt_name_request *next, *prev;
45
46         enum nbt_request_state state;
47
48         NTSTATUS status;
49
50         /* the socket this was on */
51         struct nbt_name_socket *nbtsock;
52
53         /* where to send the request */
54         struct nbt_peer_socket dest;
55
56         /* timeout between retries */
57         int timeout;
58
59         /* how many retries to send on timeout */
60         int num_retries;
61
62         /* whether we have received a WACK */
63         BOOL received_wack;
64
65         /* the timeout event */
66         struct timed_event *te;
67
68         /* the name transaction id */
69         uint16_t name_trn_id;
70
71         /* is it a reply? */
72         BOOL is_reply;
73         
74         /* the encoded request */
75         DATA_BLOB encoded;
76
77         /* shall we allow multiple replies? */
78         BOOL allow_multiple_replies;
79
80         uint_t num_replies;
81         struct nbt_name_reply {
82                 struct nbt_name_packet *packet;
83                 struct nbt_peer_socket dest;
84         } *replies;
85
86         /* information on what to do on completion */
87         struct {
88                 void (*fn)(struct nbt_name_request *);
89                 void *private;
90         } async;
91 };
92
93
94
95 /*
96   context structure for operations on name queries
97 */
98 struct nbt_name_socket {
99         struct socket_context *sock;
100         struct event_context *event_ctx;
101
102         /* a queue of requests pending to be sent */
103         struct nbt_name_request *send_queue;
104
105         /* the fd event */
106         struct fd_event *fde;
107
108         /* mapping from name_trn_id to pending event */
109         struct idr_context *idr;
110
111         /* how many requests are waiting for a reply */
112         uint16_t num_pending;
113
114         /* what to do with incoming request packets */
115         struct {
116                 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, 
117                                 const struct nbt_peer_socket *);
118                 void *private;
119         } incoming;
120
121         /* what to do with unexpected replies */
122         struct {
123                 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, 
124                                 const struct nbt_peer_socket *);
125                 void *private;
126         } unexpected;
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                 BOOL broadcast;
136                 BOOL wins_lookup;
137                 int timeout; /* in seconds */
138                 int retries;
139         } in;
140         struct {
141                 const char *reply_from;
142                 struct nbt_name name;
143                 int16_t num_addrs;
144                 const char **reply_addrs;
145         } out;
146 };
147
148 /* a simple name status query */
149 struct nbt_name_status {
150         struct {
151                 struct nbt_name name;
152                 const char *dest_addr;
153                 int timeout; /* in seconds */
154                 int retries;
155         } in;
156         struct {
157                 const char *reply_from;
158                 struct nbt_name name;
159                 struct nbt_rdata_status status;
160         } out;
161 };
162
163 /* a name registration request */
164 struct nbt_name_register {
165         struct {
166                 struct nbt_name name;
167                 const char *dest_addr;
168                 const char *address;
169                 uint16_t nb_flags;
170                 BOOL register_demand;
171                 BOOL broadcast;
172                 BOOL multi_homed;
173                 uint32_t ttl;
174                 int timeout; /* in seconds */
175                 int retries;
176         } in;
177         struct {
178                 const char *reply_from;
179                 struct nbt_name name;
180                 const char *reply_addr;
181                 uint8_t rcode;
182         } out;
183 };
184
185 /* a send 3 times then demand name broadcast name registration */
186 struct nbt_name_register_bcast {
187         struct {
188                 struct nbt_name name;
189                 const char *dest_addr;
190                 const char *address;
191                 uint16_t nb_flags;
192                 uint32_t ttl;
193         } in;
194 };
195
196
197 /* wins name register with multiple wins servers to try and multiple
198    addresses to register */
199 struct nbt_name_register_wins {
200         struct {
201                 struct nbt_name name;
202                 const char **wins_servers;
203                 const char **addresses;
204                 uint16_t nb_flags;
205                 uint32_t ttl;
206         } in;
207         struct {
208                 const char *wins_server;
209                 uint8_t rcode;
210         } out;
211 };
212
213
214
215 /* a name refresh request */
216 struct nbt_name_refresh {
217         struct {
218                 struct nbt_name name;
219                 const char *dest_addr;
220                 const char *address;
221                 uint16_t nb_flags;
222                 BOOL broadcast;
223                 uint32_t ttl;
224                 int timeout; /* in seconds */
225                 int retries;
226         } in;
227         struct {
228                 const char *reply_from;
229                 struct nbt_name name;
230                 const char *reply_addr;
231                 uint8_t rcode;
232         } out;
233 };
234
235 /* wins name refresh with multiple wins servers to try and multiple
236    addresses to register */
237 struct nbt_name_refresh_wins {
238         struct {
239                 struct nbt_name name;
240                 const char **wins_servers;
241                 const char **addresses;
242                 uint16_t nb_flags;
243                 uint32_t ttl;
244         } in;
245         struct {
246                 const char *wins_server;
247                 uint8_t rcode;
248         } out;
249 };
250
251
252 /* a name release request */
253 struct nbt_name_release {
254         struct {
255                 struct nbt_name name;
256                 const char *dest_addr;
257                 const char *address;
258                 uint16_t nb_flags;
259                 BOOL broadcast;
260                 int timeout; /* in seconds */
261                 int retries;
262         } in;
263         struct {
264                 const char *reply_from;
265                 struct nbt_name name;
266                 const char *reply_addr;
267                 uint8_t rcode;
268         } out;
269 };