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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "lib/events/events.h"
24 #include "lib/util/dlinklist.h"
25 #include "libcli/nbt/libnbt.h"
26 #include "lib/socket/socket.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "param/param.h"
30 #define NBT_MAX_REPLIES 1000
33 destroy a pending request
35 static int nbt_name_request_destructor(struct nbt_name_request *req)
37 if (req->state == NBT_REQUEST_SEND) {
38 DLIST_REMOVE(req->nbtsock->send_queue, req);
40 if (req->state == NBT_REQUEST_WAIT) {
41 req->nbtsock->num_pending--;
43 if (req->name_trn_id != 0 && !req->is_reply) {
44 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,
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);
106 } else if (req->is_reply) {
114 handle a request timeout
116 static void nbt_name_socket_timeout(struct event_context *ev, struct timed_event *te,
117 struct timeval t, void *private)
119 struct nbt_name_request *req = talloc_get_type(private,
120 struct nbt_name_request);
122 if (req->num_retries != 0) {
124 req->te = event_add_timed(req->nbtsock->event_ctx, req,
125 timeval_add(&t, req->timeout, 0),
126 nbt_name_socket_timeout, req);
127 if (req->state != NBT_REQUEST_SEND) {
128 req->state = NBT_REQUEST_SEND;
129 DLIST_ADD_END(req->nbtsock->send_queue, req,
130 struct nbt_name_request *);
132 EVENT_FD_WRITEABLE(req->nbtsock->fde);
136 nbt_name_request_destructor(req);
137 if (req->num_replies == 0) {
138 req->state = NBT_REQUEST_TIMEOUT;
139 req->status = NT_STATUS_IO_TIMEOUT;
141 req->state = NBT_REQUEST_DONE;
142 req->status = NT_STATUS_OK;
146 } else if (req->is_reply) {
154 handle recv events on a nbt name socket
156 static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock)
158 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
160 enum ndr_err_code ndr_err;
161 struct socket_address *src;
164 struct nbt_name_packet *packet;
165 struct nbt_name_request *req;
167 status = socket_pending(nbtsock->sock, &dsize);
168 if (!NT_STATUS_IS_OK(status)) {
169 talloc_free(tmp_ctx);
173 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
174 if (blob.data == NULL) {
175 talloc_free(tmp_ctx);
179 status = socket_recvfrom(nbtsock->sock, blob.data, blob.length, &nread,
181 if (!NT_STATUS_IS_OK(status)) {
182 talloc_free(tmp_ctx);
186 packet = talloc(tmp_ctx, struct nbt_name_packet);
187 if (packet == NULL) {
188 talloc_free(tmp_ctx);
192 /* parse the request */
193 ndr_err = ndr_pull_struct_blob(&blob, packet, nbtsock->iconv_convenience, packet,
194 (ndr_pull_flags_fn_t)ndr_pull_nbt_name_packet);
195 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
196 status = ndr_map_error2ntstatus(ndr_err);
197 DEBUG(2,("Failed to parse incoming NBT name packet - %s\n",
199 talloc_free(tmp_ctx);
204 DEBUG(10,("Received nbt packet of length %d from %s:%d\n",
205 (int)blob.length, src->addr, src->port));
206 NDR_PRINT_DEBUG(nbt_name_packet, packet);
209 /* if its not a reply then pass it off to the incoming request
211 if (!(packet->operation & NBT_FLAG_REPLY)) {
212 if (nbtsock->incoming.handler) {
213 nbtsock->incoming.handler(nbtsock, packet, src);
215 talloc_free(tmp_ctx);
219 /* find the matching request */
220 req = (struct nbt_name_request *)idr_find(nbtsock->idr,
221 packet->name_trn_id);
223 if (nbtsock->unexpected.handler) {
224 nbtsock->unexpected.handler(nbtsock, packet, src);
226 DEBUG(10,("Failed to match request for incoming name packet id 0x%04x on %p\n",
227 packet->name_trn_id, nbtsock));
229 talloc_free(tmp_ctx);
233 /* if this is a WACK response, this we need to go back to waiting,
234 but perhaps increase the timeout */
235 if ((packet->operation & NBT_OPCODE) == NBT_OPCODE_WACK) {
236 if (req->received_wack || packet->ancount < 1) {
237 nbt_name_request_destructor(req);
238 req->status = NT_STATUS_INVALID_NETWORK_RESPONSE;
239 req->state = NBT_REQUEST_ERROR;
242 talloc_free(req->te);
243 /* we know we won't need any more retries - the server
244 has received our request */
245 req->num_retries = 0;
246 req->received_wack = true;
247 /* although there can be a timeout in the packet, w2k3 screws it up,
248 so better to set it ourselves */
249 req->timeout = lp_parm_int(global_loadparm, NULL, "nbt", "wack_timeout", 30);
250 req->te = event_add_timed(req->nbtsock->event_ctx, req,
251 timeval_current_ofs(req->timeout, 0),
252 nbt_name_socket_timeout, req);
253 talloc_free(tmp_ctx);
258 req->replies = talloc_realloc(req, req->replies, struct nbt_name_reply, req->num_replies+1);
259 if (req->replies == NULL) {
260 nbt_name_request_destructor(req);
261 req->state = NBT_REQUEST_ERROR;
262 req->status = NT_STATUS_NO_MEMORY;
266 talloc_steal(req, src);
267 req->replies[req->num_replies].dest = src;
268 talloc_steal(req, packet);
269 req->replies[req->num_replies].packet = packet;
272 /* if we don't want multiple replies then we are done */
273 if (req->allow_multiple_replies &&
274 req->num_replies < NBT_MAX_REPLIES) {
275 talloc_free(tmp_ctx);
279 nbt_name_request_destructor(req);
280 req->state = NBT_REQUEST_DONE;
281 req->status = NT_STATUS_OK;
284 talloc_free(tmp_ctx);
291 handle fd events on a nbt_name_socket
293 static void nbt_name_socket_handler(struct event_context *ev, struct fd_event *fde,
294 uint16_t flags, void *private)
296 struct nbt_name_socket *nbtsock = talloc_get_type(private,
297 struct nbt_name_socket);
298 if (flags & EVENT_FD_WRITE) {
299 nbt_name_socket_send(nbtsock);
301 if (flags & EVENT_FD_READ) {
302 nbt_name_socket_recv(nbtsock);
308 initialise a nbt_name_socket. The event_ctx is optional, if provided
309 then operations will use that event context
311 _PUBLIC_ struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx,
312 struct event_context *event_ctx,
313 struct smb_iconv_convenience *iconv_convenience)
315 struct nbt_name_socket *nbtsock;
318 nbtsock = talloc(mem_ctx, struct nbt_name_socket);
319 if (nbtsock == NULL) goto failed;
321 if (event_ctx == NULL) {
322 nbtsock->event_ctx = event_context_init(nbtsock);
324 nbtsock->event_ctx = talloc_reference(nbtsock, event_ctx);
326 if (nbtsock->event_ctx == NULL) goto failed;
328 status = socket_create("ip", SOCKET_TYPE_DGRAM, &nbtsock->sock, 0);
329 if (!NT_STATUS_IS_OK(status)) goto failed;
331 socket_set_option(nbtsock->sock, "SO_BROADCAST", "1");
333 talloc_steal(nbtsock, nbtsock->sock);
335 nbtsock->idr = idr_init(nbtsock);
336 if (nbtsock->idr == NULL) goto failed;
338 nbtsock->send_queue = NULL;
339 nbtsock->num_pending = 0;
340 nbtsock->incoming.handler = NULL;
341 nbtsock->unexpected.handler = NULL;
342 nbtsock->iconv_convenience = iconv_convenience;
344 nbtsock->fde = event_add_fd(nbtsock->event_ctx, nbtsock,
345 socket_get_fd(nbtsock->sock), 0,
346 nbt_name_socket_handler, nbtsock);
351 talloc_free(nbtsock);
356 send off a nbt name request
358 struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock,
359 struct socket_address *dest,
360 struct nbt_name_packet *request,
361 int timeout, int retries,
362 bool allow_multiple_replies)
364 struct nbt_name_request *req;
366 enum ndr_err_code ndr_err;
368 req = talloc_zero(nbtsock, struct nbt_name_request);
369 if (req == NULL) goto failed;
371 req->nbtsock = nbtsock;
372 req->allow_multiple_replies = allow_multiple_replies;
373 req->state = NBT_REQUEST_SEND;
374 req->is_reply = false;
375 req->timeout = timeout;
376 req->num_retries = retries;
378 if (talloc_reference(req, dest) == NULL) goto failed;
380 /* we select a random transaction id unless the user supplied one */
381 if (request->name_trn_id == 0) {
382 id = idr_get_new_random(req->nbtsock->idr, req, UINT16_MAX);
384 if (idr_find(req->nbtsock->idr, request->name_trn_id)) goto failed;
385 id = idr_get_new_above(req->nbtsock->idr, req, request->name_trn_id,
388 if (id == -1) goto failed;
390 request->name_trn_id = id;
391 req->name_trn_id = id;
393 req->te = event_add_timed(nbtsock->event_ctx, req,
394 timeval_current_ofs(req->timeout, 0),
395 nbt_name_socket_timeout, req);
397 talloc_set_destructor(req, nbt_name_request_destructor);
399 ndr_err = ndr_push_struct_blob(&req->encoded, req,
400 req->nbtsock->iconv_convenience,
402 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
403 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
405 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
408 DEBUG(10,("Queueing nbt packet to %s:%d\n",
409 req->dest->addr, req->dest->port));
410 NDR_PRINT_DEBUG(nbt_name_packet, request);
413 EVENT_FD_WRITEABLE(nbtsock->fde);
424 send off a nbt name reply
426 NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock,
427 struct socket_address *dest,
428 struct nbt_name_packet *request)
430 struct nbt_name_request *req;
431 enum ndr_err_code ndr_err;
433 req = talloc_zero(nbtsock, struct nbt_name_request);
434 NT_STATUS_HAVE_NO_MEMORY(req);
436 req->nbtsock = nbtsock;
438 if (talloc_reference(req, dest) == NULL) goto failed;
439 req->state = NBT_REQUEST_SEND;
440 req->is_reply = true;
442 talloc_set_destructor(req, nbt_name_request_destructor);
445 NDR_PRINT_DEBUG(nbt_name_packet, request);
448 ndr_err = ndr_push_struct_blob(&req->encoded, req,
449 req->nbtsock->iconv_convenience,
451 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
452 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
454 return ndr_map_error2ntstatus(ndr_err);
457 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
459 EVENT_FD_WRITEABLE(nbtsock->fde);
465 return NT_STATUS_NO_MEMORY;
469 wait for a nbt request to complete
471 NTSTATUS nbt_name_request_recv(struct nbt_name_request *req)
473 if (!req) return NT_STATUS_NO_MEMORY;
475 while (req->state < NBT_REQUEST_DONE) {
476 if (event_loop_once(req->nbtsock->event_ctx) != 0) {
477 req->state = NBT_REQUEST_ERROR;
478 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
487 setup a handler for incoming requests
489 NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock,
490 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
491 struct socket_address *),
494 nbtsock->incoming.handler = handler;
495 nbtsock->incoming.private = private;
496 EVENT_FD_READABLE(nbtsock->fde);
502 turn a NBT rcode into a NTSTATUS
504 NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode)
508 enum nbt_rcode rcode;
511 { NBT_RCODE_FMT, NT_STATUS_INVALID_PARAMETER },
512 { NBT_RCODE_SVR, NT_STATUS_SERVER_DISABLED },
513 { NBT_RCODE_NAM, NT_STATUS_OBJECT_NAME_NOT_FOUND },
514 { NBT_RCODE_IMP, NT_STATUS_NOT_SUPPORTED },
515 { NBT_RCODE_RFS, NT_STATUS_ACCESS_DENIED },
516 { NBT_RCODE_ACT, NT_STATUS_ADDRESS_ALREADY_EXISTS },
517 { NBT_RCODE_CFT, NT_STATUS_CONFLICTING_ADDRESSES }
519 for (i=0;i<ARRAY_SIZE(map);i++) {
520 if (map[i].rcode == rcode) {
521 return map[i].status;
524 return NT_STATUS_UNSUCCESSFUL;