2 Unix SMB/CIFS implementation.
4 general name resolution interface
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Jelmer Vernooij 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "lib/events/events.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/resolve/resolve.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "param/param.h"
29 #include "system/network.h"
30 #include "util/dlinklist.h"
32 struct resolve_state {
33 struct resolve_context *ctx;
34 struct resolve_method *method;
36 struct composite_context *creq;
37 const char *reply_addr;
40 static struct composite_context *setup_next_method(struct composite_context *c);
43 struct resolve_context {
44 struct resolve_method {
45 resolve_name_send_fn send_fn;
46 resolve_name_recv_fn recv_fn;
48 struct resolve_method *prev, *next;
53 * Initialize a resolve context
55 struct resolve_context *resolve_context_init(TALLOC_CTX *mem_ctx)
57 return talloc_zero(mem_ctx, struct resolve_context);
61 * Add a resolve method
63 bool resolve_context_add_method(struct resolve_context *ctx, resolve_name_send_fn send_fn,
64 resolve_name_recv_fn recv_fn, void *userdata)
66 struct resolve_method *method = talloc_zero(ctx, struct resolve_method);
71 method->send_fn = send_fn;
72 method->recv_fn = recv_fn;
73 method->privdata = userdata;
74 DLIST_ADD_END(ctx->methods, method, struct resolve_method *);
79 handle completion of one name resolve method
81 static void resolve_handler(struct composite_context *creq)
83 struct composite_context *c = (struct composite_context *)creq->async.private_data;
84 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
85 const struct resolve_method *method = state->method;
87 c->status = method->recv_fn(creq, state, &state->reply_addr);
89 if (!NT_STATUS_IS_OK(c->status)) {
90 state->method = state->method->next;
91 state->creq = setup_next_method(c);
92 if (state->creq != NULL) {
97 if (!NT_STATUS_IS_OK(c->status)) {
98 c->state = COMPOSITE_STATE_ERROR;
100 c->state = COMPOSITE_STATE_DONE;
108 static struct composite_context *setup_next_method(struct composite_context *c)
110 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
111 struct composite_context *creq = NULL;
115 creq = state->method->send_fn(c, c->event_ctx, state->method->privdata, &state->name);
117 if (creq == NULL && state->method) state->method = state->method->next;
119 } while (!creq && state->method);
122 creq->async.fn = resolve_handler;
123 creq->async.private_data = c;
130 general name resolution - async send
132 struct composite_context *resolve_name_send(struct resolve_context *ctx,
133 struct nbt_name *name,
134 struct event_context *event_ctx)
136 struct composite_context *c;
137 struct resolve_state *state;
139 c = composite_create(event_ctx, event_ctx);
140 if (c == NULL) return NULL;
143 composite_error(c, NT_STATUS_INVALID_PARAMETER);
147 if (event_ctx == NULL) {
148 c->event_ctx = event_context_init(c);
150 c->event_ctx = talloc_reference(c, event_ctx);
152 if (composite_nomem(c->event_ctx, c)) return c;
154 state = talloc(c, struct resolve_state);
155 if (composite_nomem(state, c)) return c;
156 c->private_data = state;
158 c->status = nbt_name_dup(state, name, &state->name);
159 if (!composite_is_ok(c)) return c;
161 state->ctx = talloc_reference(state, ctx);
162 if (composite_nomem(state->ctx, c)) return c;
164 if (is_ipaddress(state->name.name) ||
165 strcasecmp(state->name.name, "localhost") == 0) {
166 struct in_addr ip = interpret_addr2(state->name.name);
167 state->reply_addr = talloc_strdup(state, inet_ntoa(ip));
168 if (composite_nomem(state->reply_addr, c)) return c;
173 state->method = ctx->methods;
174 state->creq = setup_next_method(c);
175 if (composite_nomem(state->creq, c)) return c;
181 general name resolution method - recv side
183 NTSTATUS resolve_name_recv(struct composite_context *c,
184 TALLOC_CTX *mem_ctx, const char **reply_addr)
188 status = composite_wait(c);
190 if (NT_STATUS_IS_OK(status)) {
191 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
192 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
200 general name resolution - sync call
202 NTSTATUS resolve_name(struct resolve_context *ctx, struct nbt_name *name, TALLOC_CTX *mem_ctx, const char **reply_addr, struct event_context *ev)
204 struct composite_context *c = resolve_name_send(ctx, name, ev);
205 return resolve_name_recv(c, mem_ctx, reply_addr);
208 /* Initialise a struct nbt_name with a NULL scope */
210 void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
217 /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
219 void make_nbt_name_client(struct nbt_name *nbt, const char *name)
221 make_nbt_name(nbt, name, NBT_NAME_CLIENT);
224 /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
226 void make_nbt_name_server(struct nbt_name *nbt, const char *name)
228 make_nbt_name(nbt, name, NBT_NAME_SERVER);
231 struct resolve_context *lp_resolve_context(struct loadparm_context *lp_ctx)
233 const char **methods = lp_name_resolve_order(lp_ctx);
235 struct resolve_context *ret = resolve_context_init(lp_ctx);
240 for (i = 0; methods != NULL && methods[i] != NULL; i++) {
241 if (!strcmp(methods[i], "wins")) {
242 resolve_context_add_wins_method(ret, lp_wins_server_list(lp_ctx));
243 } else if (!strcmp(methods[i], "bcast")) {
244 resolve_context_add_bcast_method(ret);
245 } else if (!strcmp(methods[i], "host")) {
246 resolve_context_add_host_method(ret);
248 DEBUG(0, ("Unknown resolve method '%s'", methods[i]));