2 Unix SMB/CIFS implementation.
4 nbt list of addresses name resolution module
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 TODO: we should lower the timeout, and add retries for each name
28 #include "libcli/raw/libcliraw.h"
29 #include "libcli/nbt/libnbt.h"
30 #include "libcli/composite/composite.h"
32 struct nbtlist_state {
34 struct nbt_name_socket *nbtsock;
36 struct nbt_name_request **queries;
37 struct nbt_name_query *io_queries;
38 const char *reply_addr;
42 handle events during nbtlist name resolution
44 static void nbtlist_handler(struct nbt_name_request *req)
46 struct composite_context *c = talloc_get_type(req->async.private,
47 struct composite_context);
48 struct nbtlist_state *state = talloc_get_type(c->private, struct nbtlist_state);
51 for (i=0;i<state->num_queries;i++) {
52 if (req == state->queries[i]) break;
54 if (i == state->num_queries) {
56 c->status = NT_STATUS_INTERNAL_ERROR;
57 c->state = SMBCLI_REQUEST_ERROR;
61 c->status = nbt_name_query_recv(req, state, &state->io_queries[i]);
62 if (!NT_STATUS_IS_OK(c->status)) {
63 c->state = SMBCLI_REQUEST_ERROR;
65 if (state->io_queries[i].out.num_addrs < 1) {
66 c->state = SMBCLI_REQUEST_ERROR;
67 c->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
69 c->state = SMBCLI_REQUEST_DONE;
70 state->reply_addr = talloc_steal(state, state->io_queries[i].out.reply_addrs[0]);
75 talloc_free(state->nbtsock);
82 nbtlist name resolution method - async send
84 struct composite_context *resolve_name_nbtlist_send(struct nbt_name *name,
85 struct event_context *event_ctx,
86 const char **address_list,
90 struct composite_context *c;
91 struct nbtlist_state *state;
95 c = talloc_zero(NULL, struct composite_context);
96 if (c == NULL) goto failed;
98 state = talloc(c, struct nbtlist_state);
99 if (state == NULL) goto failed;
101 status = nbt_name_dup(state, name, &state->name);
102 if (!NT_STATUS_IS_OK(status)) goto failed;
104 state->name.name = strupper_talloc(state, state->name.name);
105 if (state->name.name == NULL) goto failed;
106 if (state->name.scope) {
107 state->name.scope = strupper_talloc(state, state->name.scope);
108 if (state->name.scope == NULL) goto failed;
111 state->nbtsock = nbt_name_socket_init(state, event_ctx);
112 if (state->nbtsock == NULL) goto failed;
114 /* count the address_list size */
115 for (i=0;address_list[i];i++) /* noop */ ;
117 state->num_queries = i;
118 state->io_queries = talloc_array(state, struct nbt_name_query, state->num_queries);
119 if (!state->io_queries) goto failed;
121 state->queries = talloc_array(state, struct nbt_name_request *, state->num_queries);
122 if (!state->queries) goto failed;
124 for (i=0;i<state->num_queries;i++) {
125 state->io_queries[i].in.name = state->name;
126 state->io_queries[i].in.dest_addr = talloc_strdup(state->io_queries, address_list[i]);
127 if (!state->io_queries[i].in.dest_addr) goto failed;
128 state->io_queries[i].in.broadcast = broadcast;
129 state->io_queries[i].in.wins_lookup = wins_lookup;
130 state->io_queries[i].in.timeout = lp_parm_int(-1, "nbt", "timeout", 3);
132 state->queries[i] = nbt_name_query_send(state->nbtsock, &state->io_queries[i]);
133 if (!state->queries[i]) goto failed;
135 state->queries[i]->async.fn = nbtlist_handler;
136 state->queries[i]->async.private = c;
139 c->state = SMBCLI_REQUEST_SEND;
141 c->event_ctx = talloc_reference(c, state->nbtsock->event_ctx);
151 nbt list of addresses name resolution method - recv side
153 NTSTATUS resolve_name_nbtlist_recv(struct composite_context *c,
154 TALLOC_CTX *mem_ctx, const char **reply_addr)
158 status = composite_wait(c);
160 if (NT_STATUS_IS_OK(status)) {
161 struct nbtlist_state *state = talloc_get_type(c->private, struct nbtlist_state);
162 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
170 nbt list of addresses name resolution method - sync call
172 NTSTATUS resolve_name_nbtlist(struct nbt_name *name,
174 const char **address_list,
175 BOOL broadcast, BOOL wins_lookup,
176 const char **reply_addr)
178 struct composite_context *c = resolve_name_nbtlist_send(name, NULL, address_list,
179 broadcast, wins_lookup);
180 return resolve_name_nbtlist_recv(c, mem_ctx, reply_addr);