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 "../libcli/nbt/nbt_proto.h"
27 #include "lib/socket/socket.h"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "param/param.h"
31 #define NBT_MAX_REPLIES 1000
34 destroy a pending request
36 static int nbt_name_request_destructor(struct nbt_name_request *req)
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);
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,
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);
107 } else if (req->is_reply) {
115 handle a request timeout
117 static void nbt_name_socket_timeout(struct event_context *ev, struct timed_event *te,
118 struct timeval t, void *private)
120 struct nbt_name_request *req = talloc_get_type(private,
121 struct nbt_name_request);
123 if (req->num_retries != 0) {
125 req->te = event_add_timed(req->nbtsock->event_ctx, req,
126 timeval_add(&t, req->timeout, 0),
127 nbt_name_socket_timeout, req);
128 if (req->state != NBT_REQUEST_SEND) {
129 req->state = NBT_REQUEST_SEND;
130 DLIST_ADD_END(req->nbtsock->send_queue, req,
131 struct nbt_name_request *);
133 EVENT_FD_WRITEABLE(req->nbtsock->fde);
137 nbt_name_request_destructor(req);
138 if (req->num_replies == 0) {
139 req->state = NBT_REQUEST_TIMEOUT;
140 req->status = NT_STATUS_IO_TIMEOUT;
142 req->state = NBT_REQUEST_DONE;
143 req->status = NT_STATUS_OK;
147 } else if (req->is_reply) {
155 handle recv events on a nbt name socket
157 static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock)
159 TALLOC_CTX *tmp_ctx = talloc_new(nbtsock);
161 enum ndr_err_code ndr_err;
162 struct socket_address *src;
165 struct nbt_name_packet *packet;
166 struct nbt_name_request *req;
168 status = socket_pending(nbtsock->sock, &dsize);
169 if (!NT_STATUS_IS_OK(status)) {
170 talloc_free(tmp_ctx);
174 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
175 if (blob.data == NULL) {
176 talloc_free(tmp_ctx);
180 status = socket_recvfrom(nbtsock->sock, blob.data, blob.length, &nread,
182 if (!NT_STATUS_IS_OK(status)) {
183 talloc_free(tmp_ctx);
187 packet = talloc(tmp_ctx, struct nbt_name_packet);
188 if (packet == NULL) {
189 talloc_free(tmp_ctx);
193 /* parse the request */
194 ndr_err = ndr_pull_struct_blob(&blob, packet, nbtsock->iconv_convenience, packet,
195 (ndr_pull_flags_fn_t)ndr_pull_nbt_name_packet);
196 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
197 status = ndr_map_error2ntstatus(ndr_err);
198 DEBUG(2,("Failed to parse incoming NBT name packet - %s\n",
200 talloc_free(tmp_ctx);
205 DEBUG(10,("Received nbt packet of length %d from %s:%d\n",
206 (int)blob.length, src->addr, src->port));
207 NDR_PRINT_DEBUG(nbt_name_packet, packet);
210 /* if its not a reply then pass it off to the incoming request
212 if (!(packet->operation & NBT_FLAG_REPLY)) {
213 if (nbtsock->incoming.handler) {
214 nbtsock->incoming.handler(nbtsock, packet, src);
216 talloc_free(tmp_ctx);
220 /* find the matching request */
221 req = (struct nbt_name_request *)idr_find(nbtsock->idr,
222 packet->name_trn_id);
224 if (nbtsock->unexpected.handler) {
225 nbtsock->unexpected.handler(nbtsock, packet, src);
227 DEBUG(10,("Failed to match request for incoming name packet id 0x%04x on %p\n",
228 packet->name_trn_id, nbtsock));
230 talloc_free(tmp_ctx);
234 /* if this is a WACK response, this we need to go back to waiting,
235 but perhaps increase the timeout */
236 if ((packet->operation & NBT_OPCODE) == NBT_OPCODE_WACK) {
237 if (req->received_wack || packet->ancount < 1) {
238 nbt_name_request_destructor(req);
239 req->status = NT_STATUS_INVALID_NETWORK_RESPONSE;
240 req->state = NBT_REQUEST_ERROR;
243 talloc_free(req->te);
244 /* we know we won't need any more retries - the server
245 has received our request */
246 req->num_retries = 0;
247 req->received_wack = true;
248 /* although there can be a timeout in the packet, w2k3 screws it up,
249 so better to set it ourselves */
250 req->timeout = lp_parm_int(global_loadparm, NULL, "nbt", "wack_timeout", 30);
251 req->te = event_add_timed(req->nbtsock->event_ctx, req,
252 timeval_current_ofs(req->timeout, 0),
253 nbt_name_socket_timeout, req);
254 talloc_free(tmp_ctx);
259 req->replies = talloc_realloc(req, req->replies, struct nbt_name_reply, req->num_replies+1);
260 if (req->replies == NULL) {
261 nbt_name_request_destructor(req);
262 req->state = NBT_REQUEST_ERROR;
263 req->status = NT_STATUS_NO_MEMORY;
267 talloc_steal(req, src);
268 req->replies[req->num_replies].dest = src;
269 talloc_steal(req, packet);
270 req->replies[req->num_replies].packet = packet;
273 /* if we don't want multiple replies then we are done */
274 if (req->allow_multiple_replies &&
275 req->num_replies < NBT_MAX_REPLIES) {
276 talloc_free(tmp_ctx);
280 nbt_name_request_destructor(req);
281 req->state = NBT_REQUEST_DONE;
282 req->status = NT_STATUS_OK;
285 talloc_free(tmp_ctx);
292 handle fd events on a nbt_name_socket
294 static void nbt_name_socket_handler(struct event_context *ev, struct fd_event *fde,
295 uint16_t flags, void *private)
297 struct nbt_name_socket *nbtsock = talloc_get_type(private,
298 struct nbt_name_socket);
299 if (flags & EVENT_FD_WRITE) {
300 nbt_name_socket_send(nbtsock);
302 if (flags & EVENT_FD_READ) {
303 nbt_name_socket_recv(nbtsock);
309 initialise a nbt_name_socket. The event_ctx is optional, if provided
310 then operations will use that event context
312 _PUBLIC_ struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx,
313 struct event_context *event_ctx,
314 struct smb_iconv_convenience *iconv_convenience)
316 struct nbt_name_socket *nbtsock;
319 nbtsock = talloc(mem_ctx, struct nbt_name_socket);
320 if (nbtsock == NULL) goto failed;
322 nbtsock->event_ctx = talloc_reference(nbtsock, event_ctx);
323 if (nbtsock->event_ctx == NULL) goto failed;
325 status = socket_create("ip", SOCKET_TYPE_DGRAM, &nbtsock->sock, 0);
326 if (!NT_STATUS_IS_OK(status)) goto failed;
328 socket_set_option(nbtsock->sock, "SO_BROADCAST", "1");
330 talloc_steal(nbtsock, nbtsock->sock);
332 nbtsock->idr = idr_init(nbtsock);
333 if (nbtsock->idr == NULL) goto failed;
335 nbtsock->send_queue = NULL;
336 nbtsock->num_pending = 0;
337 nbtsock->incoming.handler = NULL;
338 nbtsock->unexpected.handler = NULL;
339 nbtsock->iconv_convenience = iconv_convenience;
341 nbtsock->fde = event_add_fd(nbtsock->event_ctx, nbtsock,
342 socket_get_fd(nbtsock->sock), 0,
343 nbt_name_socket_handler, nbtsock);
348 talloc_free(nbtsock);
353 send off a nbt name request
355 struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock,
356 struct socket_address *dest,
357 struct nbt_name_packet *request,
358 int timeout, int retries,
359 bool allow_multiple_replies)
361 struct nbt_name_request *req;
363 enum ndr_err_code ndr_err;
365 req = talloc_zero(nbtsock, struct nbt_name_request);
366 if (req == NULL) goto failed;
368 req->nbtsock = nbtsock;
369 req->allow_multiple_replies = allow_multiple_replies;
370 req->state = NBT_REQUEST_SEND;
371 req->is_reply = false;
372 req->timeout = timeout;
373 req->num_retries = retries;
375 if (talloc_reference(req, dest) == NULL) goto failed;
377 /* we select a random transaction id unless the user supplied one */
378 if (request->name_trn_id == 0) {
379 id = idr_get_new_random(req->nbtsock->idr, req, UINT16_MAX);
381 if (idr_find(req->nbtsock->idr, request->name_trn_id)) goto failed;
382 id = idr_get_new_above(req->nbtsock->idr, req, request->name_trn_id,
385 if (id == -1) goto failed;
387 request->name_trn_id = id;
388 req->name_trn_id = id;
390 req->te = event_add_timed(nbtsock->event_ctx, req,
391 timeval_current_ofs(req->timeout, 0),
392 nbt_name_socket_timeout, req);
394 talloc_set_destructor(req, nbt_name_request_destructor);
396 ndr_err = ndr_push_struct_blob(&req->encoded, req,
397 req->nbtsock->iconv_convenience,
399 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
400 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
402 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
405 DEBUG(10,("Queueing nbt packet to %s:%d\n",
406 req->dest->addr, req->dest->port));
407 NDR_PRINT_DEBUG(nbt_name_packet, request);
410 EVENT_FD_WRITEABLE(nbtsock->fde);
421 send off a nbt name reply
423 _PUBLIC_ NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock,
424 struct socket_address *dest,
425 struct nbt_name_packet *request)
427 struct nbt_name_request *req;
428 enum ndr_err_code ndr_err;
430 req = talloc_zero(nbtsock, struct nbt_name_request);
431 NT_STATUS_HAVE_NO_MEMORY(req);
433 req->nbtsock = nbtsock;
435 if (talloc_reference(req, dest) == NULL) goto failed;
436 req->state = NBT_REQUEST_SEND;
437 req->is_reply = true;
439 talloc_set_destructor(req, nbt_name_request_destructor);
442 NDR_PRINT_DEBUG(nbt_name_packet, request);
445 ndr_err = ndr_push_struct_blob(&req->encoded, req,
446 req->nbtsock->iconv_convenience,
448 (ndr_push_flags_fn_t)ndr_push_nbt_name_packet);
449 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
451 return ndr_map_error2ntstatus(ndr_err);
454 DLIST_ADD_END(nbtsock->send_queue, req, struct nbt_name_request *);
456 EVENT_FD_WRITEABLE(nbtsock->fde);
462 return NT_STATUS_NO_MEMORY;
466 wait for a nbt request to complete
468 NTSTATUS nbt_name_request_recv(struct nbt_name_request *req)
470 if (!req) return NT_STATUS_NO_MEMORY;
472 while (req->state < NBT_REQUEST_DONE) {
473 if (event_loop_once(req->nbtsock->event_ctx) != 0) {
474 req->state = NBT_REQUEST_ERROR;
475 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
484 setup a handler for incoming requests
486 _PUBLIC_ NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock,
487 void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *,
488 struct socket_address *),
491 nbtsock->incoming.handler = handler;
492 nbtsock->incoming.private_data = private;
493 EVENT_FD_READABLE(nbtsock->fde);
499 turn a NBT rcode into a NTSTATUS
501 _PUBLIC_ NTSTATUS nbt_rcode_to_ntstatus(uint8_t rcode)
505 enum nbt_rcode rcode;
508 { NBT_RCODE_FMT, NT_STATUS_INVALID_PARAMETER },
509 { NBT_RCODE_SVR, NT_STATUS_SERVER_DISABLED },
510 { NBT_RCODE_NAM, NT_STATUS_OBJECT_NAME_NOT_FOUND },
511 { NBT_RCODE_IMP, NT_STATUS_NOT_SUPPORTED },
512 { NBT_RCODE_RFS, NT_STATUS_ACCESS_DENIED },
513 { NBT_RCODE_ACT, NT_STATUS_ADDRESS_ALREADY_EXISTS },
514 { NBT_RCODE_CFT, NT_STATUS_CONFLICTING_ADDRESSES }
516 for (i=0;i<ARRAY_SIZE(map);i++) {
517 if (map[i].rcode == rcode) {
518 return map[i].status;
521 return NT_STATUS_UNSUCCESSFUL;