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"
28 #include "librpc/gen_ndr/ndr_nbt.h"
30 #define NBT_MAX_REPLIES 1000
33 destroy a pending request
35 static int nbt_name_request_destructor(void *ptr)
37 struct nbt_name_request *req = talloc_get_type(ptr, struct nbt_name_request);
39 if (req->state == NBT_REQUEST_SEND) {
40 DLIST_REMOVE(req->nbtsock->send_queue, req);
42 if (req->state == NBT_REQUEST_WAIT) {
43 req->nbtsock->num_pending--;
45 if (req->name_trn_id != 0 && !req->is_reply) {
46 idr_remove(req->nbtsock->idr, req->name_trn_id);
52 if (req->nbtsock->send_queue == NULL) {
53 EVENT_FD_NOT_WRITEABLE(req->nbtsock->fde);
55 if (req->nbtsock->num_pending == 0 &&
56 req->nbtsock->incoming.handler == NULL) {
57 EVENT_FD_NOT_READABLE(req->nbtsock->fde);
64 handle send events on a nbt name socket
66 static void nbt_name_socket_send(struct nbt_name_socket *nbtsock)
68 struct nbt_name_request *req = nbtsock->send_queue;
69 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
72 while ((req = nbtsock->send_queue)) {
75 len = req->encoded.length;
76 status = socket_sendto(nbtsock->sock, &req->encoded, &len, 0,
78 if (NT_STATUS_IS_ERR(status)) goto failed;
80 if (!NT_STATUS_IS_OK(status)) {
85 DLIST_REMOVE(nbtsock->send_queue, req);
86 req->state = NBT_REQUEST_WAIT;
90 EVENT_FD_READABLE(nbtsock->fde);
91 nbtsock->num_pending++;
95 EVENT_FD_NOT_WRITEABLE(nbtsock->fde);
100 DLIST_REMOVE(nbtsock->send_queue, req);
101 nbt_name_request_destructor(req);
102 req->status = status;
103 req->state = NBT_REQUEST_ERROR;
104 talloc_free(tmp_ctx);
113 handle a request timeout
115 static void nbt_name_socket_timeout(struct event_context *ev, struct timed_event *te,
116 struct timeval t, void *private)
118 struct nbt_name_request *req = talloc_get_type(private,
119 struct nbt_name_request);
121 if (req->num_retries != 0) {
123 req->te = event_add_timed(req->nbtsock->event_ctx, req,
124 timeval_add(&t, req->timeout, 0),
125 nbt_name_socket_timeout, req);
126 if (req->state != NBT_REQUEST_SEND) {
127 req->state = NBT_REQUEST_SEND;
128 DLIST_ADD_END(req->nbtsock->send_queue, req,
129 struct nbt_name_request *);
131 EVENT_FD_WRITEABLE(req->nbtsock->fde);
135 nbt_name_request_destructor(req);
136 if (req->num_replies == 0) {
137 req->state = NBT_REQUEST_TIMEOUT;
138 req->status = NT_STATUS_IO_TIMEOUT;
140 req->state = NBT_REQUEST_DONE;
141 req->status = NT_STATUS_OK;
151 handle recv events on a nbt name socket
153 static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock)
155 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
157 struct socket_address *src;
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,
177 if (!NT_STATUS_IS_OK(status)) {
178 talloc_free(tmp_ctx);
182 packet = talloc(tmp_ctx, struct nbt_name_packet);
183 if (packet == NULL) {
184 talloc_free(tmp_ctx);
188 /* parse the request */
189 status = ndr_pull_struct_blob(&blob, packet, packet,
190 (ndr_pull_flags_fn_t)ndr_pull_nbt_name_packet);
191 if (!NT_STATUS_IS_OK(status)) {
192 DEBUG(2,("Failed to parse incoming NBT name packet - %s\n",
194 talloc_free(tmp_ctx);
199 DEBUG(10,("Received nbt packet of length %d from %s:%d\n",
200 (int)blob.length, src->addr, src->port));
201 NDR_PRINT_DEBUG(nbt_name_packet, packet);
204 /* if its not a reply then pass it off to the incoming request
206 if (!(packet->operation & NBT_FLAG_REPLY)) {
207 if (nbtsock->incoming.handler) {
208 nbtsock->incoming.handler(nbtsock, packet, src);
210 talloc_free(tmp_ctx);
214 /* find the matching request */
215 req = idr_find(nbtsock->idr, packet->name_trn_id);
217 if (nbtsock->unexpected.handler) {
218 nbtsock->unexpected.handler(nbtsock, packet, src);
220 DEBUG(2,("Failed to match request for incoming name packet id 0x%04x on %p\n",
221 packet->name_trn_id, nbtsock));
223 talloc_free(tmp_ctx);
227 /* if this is a WACK response, this we need to go back to waiting,
228 but perhaps increase the timeout */
229 if ((packet->operation & NBT_OPCODE) == NBT_OPCODE_WACK) {
230 if (req->received_wack || packet->ancount < 1) {
231 nbt_name_request_destructor(req);
232 req->status = NT_STATUS_INVALID_NETWORK_RESPONSE;
233 req->state = NBT_REQUEST_ERROR;
236 talloc_free(req->te);
237 /* we know we won't need any more retries - the server
238 has received our request */
239 req->num_retries = 0;
240 req->received_wack = True;
241 /* although there can be a timeout in the packet, w2k3 screws it up,
242 so better to set it ourselves */
243 req->timeout = lp_parm_int(-1, "nbt", "wack_timeout", 30);
244 req->te = event_add_timed(req->nbtsock->event_ctx, req,
245 timeval_current_ofs(req->timeout, 0),
246 nbt_name_socket_timeout, req);
247 talloc_free(tmp_ctx);
252 req->replies = talloc_realloc(req, req->replies, struct nbt_name_reply, req->num_replies+1);
253 if (req->replies == NULL) {
254 nbt_name_request_destructor(req);
255 req->state = NBT_REQUEST_ERROR;
256 req->status = NT_STATUS_NO_MEMORY;
260 talloc_steal(req, src);
261 req->replies[req->num_replies].dest = src;
262 talloc_steal(req, packet);
263 req->replies[req->num_replies].packet = packet;
266 /* if we don't want multiple replies then we are done */
267 if (req->allow_multiple_replies &&
268 req->num_replies < NBT_MAX_REPLIES) {
269 talloc_free(tmp_ctx);
273 nbt_name_request_destructor(req);
274 req->state = NBT_REQUEST_DONE;
275 req->status = NT_STATUS_OK;
278 talloc_free(tmp_ctx);
285 handle fd events on a nbt_name_socket
287 static void nbt_name_socket_handler(struct event_context *ev, struct fd_event *fde,
288 uint16_t flags, void *private)
290 struct nbt_name_socket *nbtsock = talloc_get_type(private,
291 struct nbt_name_socket);
292 if (flags & EVENT_FD_WRITE) {
293 nbt_name_socket_send(nbtsock);
295 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 struct socket_address *dest,
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->allow_multiple_replies = allow_multiple_replies;
365 req->state = NBT_REQUEST_SEND;
366 req->is_reply = False;
367 req->timeout = timeout;
368 req->num_retries = retries;
370 if (talloc_reference(req, dest) == NULL) goto failed;
372 /* we select a random transaction id unless the user supplied one */
373 if (request->name_trn_id == 0) {
374 id = idr_get_new_random(req->nbtsock->idr, req, UINT16_MAX);
376 if (idr_find(req->nbtsock->idr, request->name_trn_id)) goto failed;
377 id = idr_get_new_above(req->nbtsock->idr, req, request->name_trn_id,
380 if (id == -1) goto failed;
382 request->name_trn_id = id;
383 req->name_trn_id = id;
385 req->te = event_add_timed(nbtsock->event_ctx, req,
386 timeval_current_ofs(req->timeout, 0),
387 nbt_name_socket_timeout, req);
389 talloc_set_destructor(req, nbt_name_request_destructor);
391 status = ndr_push_struct_blob(&req->encoded, req, request,
392 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
393 if (!NT_STATUS_IS_OK(status)) goto failed;
395 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
398 DEBUG(10,("Queueing nbt packet to %s:%d\n",
399 req->dest->addr, req->dest->port));
400 NDR_PRINT_DEBUG(nbt_name_packet, request);
403 EVENT_FD_WRITEABLE(nbtsock->fde);
414 send off a nbt name reply
416 NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock,
417 struct socket_address *dest,
418 struct nbt_name_packet *request)
420 struct nbt_name_request *req;
423 req = talloc_zero(nbtsock, struct nbt_name_request);
424 NT_STATUS_HAVE_NO_MEMORY(req);
426 req->nbtsock = nbtsock;
428 if (talloc_reference(req, dest) == NULL) goto failed;
429 req->state = NBT_REQUEST_SEND;
430 req->is_reply = True;
432 talloc_set_destructor(req, nbt_name_request_destructor);
435 NDR_PRINT_DEBUG(nbt_name_packet, request);
438 status = ndr_push_struct_blob(&req->encoded, req, request,
439 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
440 if (!NT_STATUS_IS_OK(status)) {
445 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
447 EVENT_FD_WRITEABLE(nbtsock->fde);
453 return NT_STATUS_NO_MEMORY;
457 wait for a nbt request to complete
459 NTSTATUS nbt_name_request_recv(struct nbt_name_request *req)
461 if (!req) return NT_STATUS_NO_MEMORY;
463 while (req->state < NBT_REQUEST_DONE) {
464 if (event_loop_once(req->nbtsock->event_ctx) != 0) {
465 req->state = NBT_REQUEST_ERROR;
466 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
477 setup a handler for incoming requests
479 NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock,
480 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
481 struct socket_address *),
484 nbtsock->incoming.handler = handler;
485 nbtsock->incoming.private = private;
486 EVENT_FD_READABLE(nbtsock->fde);
492 turn a NBT rcode into a NTSTATUS
494 NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode)
498 enum nbt_rcode rcode;
501 { NBT_RCODE_FMT, NT_STATUS_INVALID_PARAMETER },
502 { NBT_RCODE_SVR, NT_STATUS_SERVER_DISABLED },
503 { NBT_RCODE_NAM, NT_STATUS_OBJECT_NAME_NOT_FOUND },
504 { NBT_RCODE_IMP, NT_STATUS_NOT_SUPPORTED },
505 { NBT_RCODE_RFS, NT_STATUS_ACCESS_DENIED },
506 { NBT_RCODE_ACT, NT_STATUS_ADDRESS_ALREADY_EXISTS },
507 { NBT_RCODE_CFT, NT_STATUS_CONFLICTING_ADDRESSES }
509 for (i=0;i<ARRAY_SIZE(map);i++) {
510 if (map[i].rcode == rcode) {
511 return map[i].status;
514 return NT_STATUS_UNSUCCESSFUL;