2 Unix SMB/CIFS implementation.
4 low level socket handling for nbt requests
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "lib/events/events.h"
25 #include "dlinklist.h"
26 #include "libcli/nbt/libnbt.h"
27 #include "lib/socket/socket.h"
29 #define NBT_MAX_REPLIES 1000
32 destroy a pending request
34 static int nbt_name_request_destructor(void *ptr)
36 struct nbt_name_request *req = talloc_get_type(ptr, struct nbt_name_request);
38 if (req->state == NBT_REQUEST_SEND) {
39 DLIST_REMOVE(req->nbtsock->send_queue, req);
41 if (req->state == NBT_REQUEST_WAIT) {
42 req->nbtsock->num_pending--;
44 if (req->name_trn_id != 0 && !req->is_reply) {
45 idr_remove(req->nbtsock->idr, req->name_trn_id);
51 if (req->nbtsock->send_queue == NULL) {
52 EVENT_FD_NOT_WRITEABLE(req->nbtsock->fde);
54 if (req->nbtsock->num_pending == 0 &&
55 req->nbtsock->incoming.handler == NULL) {
56 EVENT_FD_NOT_READABLE(req->nbtsock->fde);
63 handle send events on a nbt name socket
65 static void nbt_name_socket_send(struct nbt_name_socket *nbtsock)
67 struct nbt_name_request *req = nbtsock->send_queue;
68 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
71 while ((req = nbtsock->send_queue)) {
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;
79 if (!NT_STATUS_IS_OK(status)) {
84 DLIST_REMOVE(nbtsock->send_queue, req);
85 req->state = NBT_REQUEST_WAIT;
89 EVENT_FD_READABLE(nbtsock->fde);
90 nbtsock->num_pending++;
94 EVENT_FD_NOT_WRITEABLE(nbtsock->fde);
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);
112 handle a request timeout
114 static void nbt_name_socket_timeout(struct event_context *ev, struct timed_event *te,
115 struct timeval t, void *private)
117 struct nbt_name_request *req = talloc_get_type(private,
118 struct nbt_name_request);
120 if (req->num_retries != 0) {
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 *);
130 EVENT_FD_WRITEABLE(req->nbtsock->fde);
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;
139 req->state = NBT_REQUEST_DONE;
140 req->status = NT_STATUS_OK;
150 handle recv events on a nbt name socket
152 static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock)
154 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
156 const char *src_addr;
160 struct nbt_name_packet *packet;
161 struct nbt_name_request *req;
163 status = socket_pending(nbtsock->sock, &dsize);
164 if (!NT_STATUS_IS_OK(status)) {
165 talloc_free(tmp_ctx);
169 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
170 if (blob.data == NULL) {
171 talloc_free(tmp_ctx);
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);
181 talloc_steal(tmp_ctx, src_addr);
184 packet = talloc(tmp_ctx, struct nbt_name_packet);
185 if (packet == NULL) {
186 talloc_free(tmp_ctx);
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",
196 talloc_free(tmp_ctx);
201 DEBUG(10,("Received nbt packet of length %d from %s:%d\n",
202 blob.length, src_addr, src_port));
203 NDR_PRINT_DEBUG(nbt_name_packet, packet);
206 /* if its not a reply then pass it off to the incoming request
208 if (!(packet->operation & NBT_FLAG_REPLY)) {
209 if (nbtsock->incoming.handler) {
210 nbtsock->incoming.handler(nbtsock, packet, src_addr, src_port);
212 talloc_free(tmp_ctx);
216 /* find the matching request */
217 req = idr_find(nbtsock->idr, packet->name_trn_id);
219 if (nbtsock->unexpected.handler) {
220 nbtsock->unexpected.handler(nbtsock, packet, src_addr, src_port);
222 DEBUG(2,("Failed to match request for incoming name packet id 0x%04x on %p\n",
223 packet->name_trn_id, nbtsock));
225 talloc_free(tmp_ctx);
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;
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);
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;
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);
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);
274 nbt_name_request_destructor(req);
275 req->state = NBT_REQUEST_DONE;
276 req->status = NT_STATUS_OK;
279 talloc_free(tmp_ctx);
286 handle fd events on a nbt_name_socket
288 static void nbt_name_socket_handler(struct event_context *ev, struct fd_event *fde,
289 uint16_t flags, void *private)
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 } else if (flags & EVENT_FD_READ) {
296 nbt_name_socket_recv(nbtsock);
302 initialise a nbt_name_socket. The event_ctx is optional, if provided
303 then operations will use that event context
305 struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx,
306 struct event_context *event_ctx)
308 struct nbt_name_socket *nbtsock;
311 nbtsock = talloc(mem_ctx, struct nbt_name_socket);
312 if (nbtsock == NULL) goto failed;
314 if (event_ctx == NULL) {
315 nbtsock->event_ctx = event_context_init(nbtsock);
317 nbtsock->event_ctx = talloc_reference(nbtsock, event_ctx);
319 if (nbtsock->event_ctx == NULL) goto failed;
321 status = socket_create("ip", SOCKET_TYPE_DGRAM, &nbtsock->sock, 0);
322 if (!NT_STATUS_IS_OK(status)) goto failed;
324 socket_set_option(nbtsock->sock, "SO_BROADCAST", "1");
326 talloc_steal(nbtsock, nbtsock->sock);
328 nbtsock->idr = idr_init(nbtsock);
329 if (nbtsock->idr == NULL) goto failed;
331 nbtsock->send_queue = NULL;
332 nbtsock->num_pending = 0;
333 nbtsock->incoming.handler = NULL;
334 nbtsock->unexpected.handler = NULL;
336 nbtsock->fde = event_add_fd(nbtsock->event_ctx, nbtsock,
337 socket_get_fd(nbtsock->sock), 0,
338 nbt_name_socket_handler, nbtsock);
343 talloc_free(nbtsock);
348 send off a nbt name request
350 struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock,
351 const char *dest_addr, int dest_port,
352 struct nbt_name_packet *request,
353 int timeout, int retries,
354 BOOL allow_multiple_replies)
356 struct nbt_name_request *req;
360 req = talloc_zero(nbtsock, struct nbt_name_request);
361 if (req == NULL) goto failed;
363 req->nbtsock = nbtsock;
364 req->dest_port = dest_port;
365 req->allow_multiple_replies = allow_multiple_replies;
366 req->state = NBT_REQUEST_SEND;
367 req->is_reply = False;
368 req->timeout = timeout;
369 req->num_retries = retries;
370 req->dest_addr = talloc_strdup(req, dest_addr);
371 if (req->dest_addr == NULL) goto failed;
373 /* we select a random transaction id unless the user supplied one */
374 if (request->name_trn_id == 0) {
375 id = idr_get_new_random(req->nbtsock->idr, req, UINT16_MAX);
377 if (idr_find(req->nbtsock->idr, request->name_trn_id)) goto failed;
378 id = idr_get_new_above(req->nbtsock->idr, req, request->name_trn_id,
381 if (id == -1) goto failed;
383 request->name_trn_id = id;
384 req->name_trn_id = id;
386 req->te = event_add_timed(nbtsock->event_ctx, req,
387 timeval_current_ofs(req->timeout, 0),
388 nbt_name_socket_timeout, req);
390 talloc_set_destructor(req, nbt_name_request_destructor);
392 status = ndr_push_struct_blob(&req->encoded, req, request,
393 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
394 if (!NT_STATUS_IS_OK(status)) goto failed;
396 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
399 DEBUG(10,("Queueing nbt packet to %s:%d\n",
400 req->dest_addr, req->dest_port));
401 NDR_PRINT_DEBUG(nbt_name_packet, request);
404 EVENT_FD_WRITEABLE(nbtsock->fde);
415 send off a nbt name reply
417 NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock,
418 const char *dest_addr, int dest_port,
419 struct nbt_name_packet *request)
421 struct nbt_name_request *req;
424 req = talloc_zero(nbtsock, struct nbt_name_request);
425 NT_STATUS_HAVE_NO_MEMORY(req);
427 req->nbtsock = nbtsock;
428 req->dest_addr = talloc_strdup(req, dest_addr);
429 if (req->dest_addr == NULL) goto failed;
430 req->dest_port = dest_port;
431 req->state = NBT_REQUEST_SEND;
432 req->is_reply = True;
434 talloc_set_destructor(req, nbt_name_request_destructor);
437 NDR_PRINT_DEBUG(nbt_name_packet, request);
440 status = ndr_push_struct_blob(&req->encoded, req, request,
441 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
442 if (!NT_STATUS_IS_OK(status)) {
447 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
449 EVENT_FD_WRITEABLE(nbtsock->fde);
455 return NT_STATUS_NO_MEMORY;
459 wait for a nbt request to complete
461 NTSTATUS nbt_name_request_recv(struct nbt_name_request *req)
463 if (!req) return NT_STATUS_NO_MEMORY;
465 while (req->state < NBT_REQUEST_DONE) {
466 if (event_loop_once(req->nbtsock->event_ctx) != 0) {
467 req->state = NBT_REQUEST_ERROR;
468 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
479 setup a handler for incoming requests
481 NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock,
482 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
486 nbtsock->incoming.handler = handler;
487 nbtsock->incoming.private = private;
488 EVENT_FD_READABLE(nbtsock->fde);
494 turn a NBT rcode into a NTSTATUS
496 NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode)
500 enum nbt_rcode rcode;
503 { NBT_RCODE_FMT, NT_STATUS_INVALID_PARAMETER },
504 { NBT_RCODE_SVR, NT_STATUS_SERVER_DISABLED },
505 { NBT_RCODE_NAM, NT_STATUS_OBJECT_NAME_NOT_FOUND },
506 { NBT_RCODE_IMP, NT_STATUS_NOT_SUPPORTED },
507 { NBT_RCODE_RFS, NT_STATUS_ACCESS_DENIED },
508 { NBT_RCODE_ACT, NT_STATUS_ADDRESS_ALREADY_EXISTS },
509 { NBT_RCODE_ACT, NT_STATUS_CONFLICTING_ADDRESSES }
511 for (i=0;i<ARRAY_SIZE(map);i++) {
512 if (map[i].rcode == rcode) {
513 return map[i].status;
516 return NT_STATUS_UNSUCCESSFUL;