r8520: fixed a pile of warnings from the build farm gcc -Wall output on
[jra/samba/.git] / source4 / libcli / nbt / nbtsocket.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    low level socket handling for nbt requests
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 "includes.h"
24 #include "lib/events/events.h"
25 #include "dlinklist.h"
26 #include "libcli/nbt/libnbt.h"
27 #include "lib/socket/socket.h"
28
29 #define NBT_MAX_REPLIES 1000
30
31 /*
32   destroy a pending request
33 */
34 static int nbt_name_request_destructor(void *ptr)
35 {
36         struct nbt_name_request *req = talloc_get_type(ptr, struct nbt_name_request);
37         
38         if (req->state == NBT_REQUEST_SEND) {
39                 DLIST_REMOVE(req->nbtsock->send_queue, req);
40         }
41         if (req->state == NBT_REQUEST_WAIT) {
42                 req->nbtsock->num_pending--;
43         }
44         if (req->name_trn_id != 0 && !req->is_reply) {
45                 idr_remove(req->nbtsock->idr, req->name_trn_id);
46                 req->name_trn_id = 0;
47         }
48         if (req->te) {
49                 req->te = NULL;
50         }
51         if (req->nbtsock->send_queue == NULL) {
52                 EVENT_FD_NOT_WRITEABLE(req->nbtsock->fde);
53         }
54         if (req->nbtsock->num_pending == 0 && 
55             req->nbtsock->incoming.handler == NULL) {
56                 EVENT_FD_NOT_READABLE(req->nbtsock->fde);
57         }
58         return 0;
59 }
60
61
62 /*
63   handle send events on a nbt name socket
64 */
65 static void nbt_name_socket_send(struct nbt_name_socket *nbtsock)
66 {
67         struct nbt_name_request *req = nbtsock->send_queue;
68         TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
69         NTSTATUS status;
70
71         while ((req = nbtsock->send_queue)) {
72                 size_t len;
73                 
74                 len = req->encoded.length;
75                 status = socket_sendto(nbtsock->sock, &req->encoded, &len, 0, 
76                                        req->dest_addr, req->dest_port);
77                 if (NT_STATUS_IS_ERR(status)) goto failed;              
78
79                 if (!NT_STATUS_IS_OK(status)) {
80                         talloc_free(tmp_ctx);
81                         return;
82                 }
83
84                 DLIST_REMOVE(nbtsock->send_queue, req);
85                 req->state = NBT_REQUEST_WAIT;
86                 if (req->is_reply) {
87                         talloc_free(req);
88                 } else {
89                         EVENT_FD_READABLE(nbtsock->fde);
90                         nbtsock->num_pending++;
91                 }
92         }
93
94         EVENT_FD_NOT_WRITEABLE(nbtsock->fde);
95         talloc_free(tmp_ctx);
96         return;
97
98 failed:
99         DLIST_REMOVE(nbtsock->send_queue, req);
100         nbt_name_request_destructor(req);
101         req->status = status;
102         req->state = NBT_REQUEST_ERROR;
103         talloc_free(tmp_ctx);
104         if (req->async.fn) {
105                 req->async.fn(req);
106         }
107         return;
108 }
109
110
111 /*
112   handle a request timeout
113 */
114 static void nbt_name_socket_timeout(struct event_context *ev, struct timed_event *te,
115                                     struct timeval t, void *private)
116 {
117         struct nbt_name_request *req = talloc_get_type(private, 
118                                                        struct nbt_name_request);
119
120         if (req->num_retries != 0) {
121                 req->num_retries--;
122                 req->te = event_add_timed(req->nbtsock->event_ctx, req, 
123                                           timeval_add(&t, req->timeout, 0),
124                                           nbt_name_socket_timeout, req);
125                 if (req->state != NBT_REQUEST_SEND) {
126                         req->state = NBT_REQUEST_SEND;
127                         DLIST_ADD_END(req->nbtsock->send_queue, req, 
128                                       struct nbt_name_request *);
129                 }
130                 EVENT_FD_WRITEABLE(req->nbtsock->fde);
131                 return;
132         }
133
134         nbt_name_request_destructor(req);
135         if (req->num_replies == 0) {
136                 req->state = NBT_REQUEST_TIMEOUT;
137                 req->status = NT_STATUS_IO_TIMEOUT;
138         } else {
139                 req->state = NBT_REQUEST_DONE;
140                 req->status = NT_STATUS_OK;
141         }
142         if (req->async.fn) {
143                 req->async.fn(req);
144         }
145 }
146
147
148
149 /*
150   handle recv events on a nbt name socket
151 */
152 static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock)
153 {
154         TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
155         NTSTATUS status;
156         const char *src_addr;
157         int src_port;
158         DATA_BLOB blob;
159         size_t nread, dsize;
160         struct nbt_name_packet *packet;
161         struct nbt_name_request *req;
162
163         status = socket_pending(nbtsock->sock, &dsize);
164         if (!NT_STATUS_IS_OK(status)) {
165                 talloc_free(tmp_ctx);
166                 return;
167         }
168
169         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
170         if (blob.data == NULL) {
171                 talloc_free(tmp_ctx);
172                 return;
173         }
174
175         status = socket_recvfrom(nbtsock->sock, blob.data, blob.length, &nread, 0,
176                                  &src_addr, &src_port);
177         if (!NT_STATUS_IS_OK(status)) {
178                 talloc_free(tmp_ctx);
179                 return;
180         }
181         talloc_steal(tmp_ctx, src_addr);
182         blob.length = nread;
183
184         packet = talloc(tmp_ctx, struct nbt_name_packet);
185         if (packet == NULL) {
186                 talloc_free(tmp_ctx);
187                 return;
188         }
189
190         /* parse the request */
191         status = ndr_pull_struct_blob(&blob, packet, packet, 
192                                       (ndr_pull_flags_fn_t)ndr_pull_nbt_name_packet);
193         if (!NT_STATUS_IS_OK(status)) {
194                 DEBUG(2,("Failed to parse incoming NBT name packet - %s\n",
195                          nt_errstr(status)));
196                 talloc_free(tmp_ctx);
197                 return;
198         }
199
200         if (DEBUGLVL(10)) {
201                 DEBUG(10,("Received nbt packet of length %d from %s:%d\n", 
202                           (int)blob.length, src_addr, src_port));
203                 NDR_PRINT_DEBUG(nbt_name_packet, packet);
204         }
205
206         /* if its not a reply then pass it off to the incoming request
207            handler, if any */
208         if (!(packet->operation & NBT_FLAG_REPLY)) {
209                 if (nbtsock->incoming.handler) {
210                         nbtsock->incoming.handler(nbtsock, packet, src_addr, src_port);
211                 }
212                 talloc_free(tmp_ctx);
213                 return;
214         }
215
216         /* find the matching request */
217         req = idr_find(nbtsock->idr, packet->name_trn_id);
218         if (req == NULL) {
219                 if (nbtsock->unexpected.handler) {
220                         nbtsock->unexpected.handler(nbtsock, packet, src_addr, src_port);
221                 } else {
222                         DEBUG(2,("Failed to match request for incoming name packet id 0x%04x on %p\n",
223                                  packet->name_trn_id, nbtsock));
224                 }
225                 talloc_free(tmp_ctx);
226                 return;
227         }
228
229         /* if this is a WACK response, this we need to go back to waiting,
230            but perhaps increase the timeout */
231         if ((packet->operation & NBT_OPCODE) == NBT_OPCODE_WACK) {
232                 if (req->received_wack || packet->ancount < 1) {
233                         nbt_name_request_destructor(req);
234                         req->status = NT_STATUS_INVALID_NETWORK_RESPONSE;
235                         req->state  = NBT_REQUEST_ERROR;
236                         goto done;
237                 }
238                 talloc_free(req->te);
239                 /* we know we won't need any more retries - the server
240                    has received our request */
241                 req->num_retries   = 0;
242                 req->received_wack = True;
243                 /* although there can be a timeout in the packet, w2k3 screws it up,
244                    so better to set it ourselves */                
245                 req->timeout = lp_parm_int(-1, "nbt", "wack_timeout", 30);
246                 req->te = event_add_timed(req->nbtsock->event_ctx, req, 
247                                           timeval_current_ofs(req->timeout, 0),
248                                           nbt_name_socket_timeout, req);
249                 talloc_free(tmp_ctx);
250                 return;
251         }
252         
253
254         req->replies = talloc_realloc(req, req->replies, struct nbt_name_reply, req->num_replies+1);
255         if (req->replies == NULL) {
256                 nbt_name_request_destructor(req);
257                 req->state  = NBT_REQUEST_ERROR;
258                 req->status = NT_STATUS_NO_MEMORY;
259                 goto done;
260         }
261
262         req->replies[req->num_replies].reply_addr = talloc_steal(req, src_addr);
263         req->replies[req->num_replies].reply_port = src_port;
264         req->replies[req->num_replies].packet     = talloc_steal(req, packet);
265         req->num_replies++;
266
267         /* if we don't want multiple replies then we are done */
268         if (req->allow_multiple_replies &&
269             req->num_replies < NBT_MAX_REPLIES) {
270                 talloc_free(tmp_ctx);
271                 return;
272         }
273
274         nbt_name_request_destructor(req);
275         req->state  = NBT_REQUEST_DONE;
276         req->status = NT_STATUS_OK;
277
278 done:
279         talloc_free(tmp_ctx);
280         if (req->async.fn) {
281                 req->async.fn(req);
282         }
283 }
284
285 /*
286   handle fd events on a nbt_name_socket
287 */
288 static void nbt_name_socket_handler(struct event_context *ev, struct fd_event *fde,
289                                     uint16_t flags, void *private)
290 {
291         struct nbt_name_socket *nbtsock = talloc_get_type(private, 
292                                                           struct nbt_name_socket);
293         if (flags & EVENT_FD_WRITE) {
294                 nbt_name_socket_send(nbtsock);
295         } 
296         if (flags & EVENT_FD_READ) {
297                 nbt_name_socket_recv(nbtsock);
298         }
299 }
300
301
302 /*
303   initialise a nbt_name_socket. The event_ctx is optional, if provided
304   then operations will use that event context
305 */
306 struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx, 
307                                              struct event_context *event_ctx)
308 {
309         struct nbt_name_socket *nbtsock;
310         NTSTATUS status;
311
312         nbtsock = talloc(mem_ctx, struct nbt_name_socket);
313         if (nbtsock == NULL) goto failed;
314
315         if (event_ctx == NULL) {
316                 nbtsock->event_ctx = event_context_init(nbtsock);
317         } else {
318                 nbtsock->event_ctx = talloc_reference(nbtsock, event_ctx);
319         }
320         if (nbtsock->event_ctx == NULL) goto failed;
321
322         status = socket_create("ip", SOCKET_TYPE_DGRAM, &nbtsock->sock, 0);
323         if (!NT_STATUS_IS_OK(status)) goto failed;
324
325         socket_set_option(nbtsock->sock, "SO_BROADCAST", "1");
326
327         talloc_steal(nbtsock, nbtsock->sock);
328
329         nbtsock->idr = idr_init(nbtsock);
330         if (nbtsock->idr == NULL) goto failed;
331
332         nbtsock->send_queue = NULL;
333         nbtsock->num_pending = 0;
334         nbtsock->incoming.handler = NULL;
335         nbtsock->unexpected.handler = NULL;
336
337         nbtsock->fde = event_add_fd(nbtsock->event_ctx, nbtsock, 
338                                     socket_get_fd(nbtsock->sock), 0,
339                                     nbt_name_socket_handler, nbtsock);
340         
341         return nbtsock;
342
343 failed:
344         talloc_free(nbtsock);
345         return NULL;
346 }
347
348 /*
349   send off a nbt name request
350 */
351 struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock, 
352                                                const char *dest_addr, int dest_port,
353                                                struct nbt_name_packet *request,
354                                                int timeout, int retries,
355                                                BOOL allow_multiple_replies)
356 {
357         struct nbt_name_request *req;
358         int id;
359         NTSTATUS status;
360
361         req = talloc_zero(nbtsock, struct nbt_name_request);
362         if (req == NULL) goto failed;
363
364         req->nbtsock                = nbtsock;
365         req->dest_port              = dest_port;
366         req->allow_multiple_replies = allow_multiple_replies;
367         req->state                  = NBT_REQUEST_SEND;
368         req->is_reply               = False;
369         req->timeout                = timeout;
370         req->num_retries            = retries;
371         req->dest_addr              = talloc_strdup(req, dest_addr);
372         if (req->dest_addr == NULL) goto failed;
373
374         /* we select a random transaction id unless the user supplied one */
375         if (request->name_trn_id == 0) {
376                 id = idr_get_new_random(req->nbtsock->idr, req, UINT16_MAX);
377         } else {
378                 if (idr_find(req->nbtsock->idr, request->name_trn_id)) goto failed;
379                 id = idr_get_new_above(req->nbtsock->idr, req, request->name_trn_id, 
380                                        UINT16_MAX);
381         }
382         if (id == -1) goto failed;
383
384         request->name_trn_id = id;
385         req->name_trn_id     = id;
386
387         req->te = event_add_timed(nbtsock->event_ctx, req, 
388                                   timeval_current_ofs(req->timeout, 0),
389                                   nbt_name_socket_timeout, req);
390         
391         talloc_set_destructor(req, nbt_name_request_destructor);        
392
393         status = ndr_push_struct_blob(&req->encoded, req, request, 
394                                       (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
395         if (!NT_STATUS_IS_OK(status)) goto failed;
396
397         DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
398
399         if (DEBUGLVL(10)) {
400                 DEBUG(10,("Queueing nbt packet to %s:%d\n", 
401                           req->dest_addr, req->dest_port));
402                 NDR_PRINT_DEBUG(nbt_name_packet, request);
403         }
404
405         EVENT_FD_WRITEABLE(nbtsock->fde);
406
407         return req;
408
409 failed:
410         talloc_free(req);
411         return NULL;
412 }
413
414
415 /*
416   send off a nbt name reply
417 */
418 NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock, 
419                              const char *dest_addr, int dest_port,
420                              struct nbt_name_packet *request)
421 {
422         struct nbt_name_request *req;
423         NTSTATUS status;
424
425         req = talloc_zero(nbtsock, struct nbt_name_request);
426         NT_STATUS_HAVE_NO_MEMORY(req);
427
428         req->nbtsock   = nbtsock;
429         req->dest_addr = talloc_strdup(req, dest_addr);
430         if (req->dest_addr == NULL) goto failed;
431         req->dest_port = dest_port;
432         req->state     = NBT_REQUEST_SEND;
433         req->is_reply = True;
434
435         talloc_set_destructor(req, nbt_name_request_destructor);        
436
437         if (DEBUGLVL(10)) {
438                 NDR_PRINT_DEBUG(nbt_name_packet, request);              
439         }
440
441         status = ndr_push_struct_blob(&req->encoded, req, request, 
442                                       (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
443         if (!NT_STATUS_IS_OK(status)) {
444                 talloc_free(req);
445                 return status;
446         }
447
448         DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
449
450         EVENT_FD_WRITEABLE(nbtsock->fde);
451
452         return NT_STATUS_OK;
453
454 failed:
455         talloc_free(req);
456         return NT_STATUS_NO_MEMORY;
457 }
458
459 /*
460   wait for a nbt request to complete
461 */
462 NTSTATUS nbt_name_request_recv(struct nbt_name_request *req)
463 {
464         if (!req) return NT_STATUS_NO_MEMORY;
465
466         while (req->state < NBT_REQUEST_DONE) {
467                 if (event_loop_once(req->nbtsock->event_ctx) != 0) {
468                         req->state = NBT_REQUEST_ERROR;
469                         req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
470                         if (req->async.fn) {
471                                 req->async.fn(req);
472                         }
473                 }
474         }
475         return req->status;
476 }
477
478
479 /*
480   setup a handler for incoming requests
481 */
482 NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock,
483                                   void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, 
484                                                   const char *, int ),
485                                   void *private)
486 {
487         nbtsock->incoming.handler = handler;
488         nbtsock->incoming.private = private;
489         EVENT_FD_READABLE(nbtsock->fde);
490         return NT_STATUS_OK;
491 }
492
493
494 /*
495   turn a NBT rcode into a NTSTATUS
496 */
497 NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode)
498 {
499         int i;
500         struct {
501                 enum nbt_rcode rcode;
502                 NTSTATUS status;
503         } map[] = {
504                 { NBT_RCODE_FMT, NT_STATUS_INVALID_PARAMETER },
505                 { NBT_RCODE_SVR, NT_STATUS_SERVER_DISABLED },
506                 { NBT_RCODE_NAM, NT_STATUS_OBJECT_NAME_NOT_FOUND },
507                 { NBT_RCODE_IMP, NT_STATUS_NOT_SUPPORTED },
508                 { NBT_RCODE_RFS, NT_STATUS_ACCESS_DENIED },
509                 { NBT_RCODE_ACT, NT_STATUS_ADDRESS_ALREADY_EXISTS },
510                 { NBT_RCODE_ACT, NT_STATUS_CONFLICTING_ADDRESSES }
511         };
512         for (i=0;i<ARRAY_SIZE(map);i++) {
513                 if (map[i].rcode == rcode) {
514                         return map[i].status;
515                 }
516         }
517         return NT_STATUS_UNSUCCESSFUL;
518 }