s4-resolve: fixed a crash bug on timeout
[Samba/aatanasov.git] / source4 / libcli / resolve / resolve.c
blob0ad3a75e893e71e88c2d0ca0c299dff65ba72acd
1 /*
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/>.
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/resolve/resolve.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "system/network.h"
28 #include "lib/socket/socket.h"
29 #include "../lib/util/dlinklist.h"
31 struct resolve_state {
32 struct resolve_context *ctx;
33 struct resolve_method *method;
34 uint32_t flags;
35 uint16_t port;
36 struct nbt_name name;
37 struct composite_context *creq;
38 struct socket_address **addrs;
39 char **names;
42 static struct composite_context *setup_next_method(struct composite_context *c);
45 struct resolve_context {
46 struct resolve_method {
47 resolve_name_send_fn send_fn;
48 resolve_name_recv_fn recv_fn;
49 void *privdata;
50 struct resolve_method *prev, *next;
51 } *methods;
54 /**
55 * Initialize a resolve context
57 struct resolve_context *resolve_context_init(TALLOC_CTX *mem_ctx)
59 return talloc_zero(mem_ctx, struct resolve_context);
62 /**
63 * Add a resolve method
65 bool resolve_context_add_method(struct resolve_context *ctx, resolve_name_send_fn send_fn,
66 resolve_name_recv_fn recv_fn, void *userdata)
68 struct resolve_method *method = talloc_zero(ctx, struct resolve_method);
70 if (method == NULL)
71 return false;
73 method->send_fn = send_fn;
74 method->recv_fn = recv_fn;
75 method->privdata = userdata;
76 DLIST_ADD_END(ctx->methods, method, struct resolve_method *);
77 return true;
80 /**
81 handle completion of one name resolve method
83 static void resolve_handler(struct composite_context *creq)
85 struct composite_context *c = (struct composite_context *)creq->async.private_data;
86 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
87 const struct resolve_method *method = state->method;
89 c->status = method->recv_fn(creq, state, &state->addrs, &state->names);
91 if (!NT_STATUS_IS_OK(c->status)) {
92 state->method = state->method->next;
93 state->creq = setup_next_method(c);
94 if (state->creq != NULL) {
95 return;
99 if (!NT_STATUS_IS_OK(c->status)) {
100 c->state = COMPOSITE_STATE_ERROR;
101 } else {
102 c->state = COMPOSITE_STATE_DONE;
104 if (c->async.fn) {
105 c->async.fn(c);
110 static struct composite_context *setup_next_method(struct composite_context *c)
112 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
113 struct composite_context *creq = NULL;
115 do {
116 if (state->method) {
117 creq = state->method->send_fn(c, c->event_ctx,
118 state->method->privdata,
119 state->flags,
120 state->port,
121 &state->name);
123 if (creq == NULL && state->method) state->method = state->method->next;
125 } while (!creq && state->method);
127 if (creq) {
128 creq->async.fn = resolve_handler;
129 creq->async.private_data = c;
132 return creq;
136 general name resolution - async send
138 struct composite_context *resolve_name_all_send(struct resolve_context *ctx,
139 TALLOC_CTX *mem_ctx,
140 uint32_t flags,
141 uint16_t port,
142 struct nbt_name *name,
143 struct tevent_context *event_ctx)
145 struct composite_context *c;
146 struct resolve_state *state;
148 if (ctx == NULL || event_ctx == NULL) {
149 return NULL;
152 c = composite_create(mem_ctx, event_ctx);
153 if (c == NULL) return NULL;
155 if (composite_nomem(c->event_ctx, c)) return c;
157 state = talloc(c, struct resolve_state);
158 if (composite_nomem(state, c)) return c;
159 c->private_data = state;
161 state->flags = flags;
162 state->port = port;
164 c->status = nbt_name_dup(state, name, &state->name);
165 if (!composite_is_ok(c)) return c;
167 state->ctx = talloc_reference(state, ctx);
168 if (composite_nomem(state->ctx, c)) return c;
170 if (is_ipaddress(state->name.name) ||
171 strcasecmp(state->name.name, "localhost") == 0) {
172 struct in_addr ip = interpret_addr2(state->name.name);
174 state->addrs = talloc_array(state, struct socket_address *, 2);
175 if (composite_nomem(state->addrs, c)) return c;
176 state->addrs[0] = socket_address_from_strings(state->addrs, "ipv4",
177 inet_ntoa(ip), 0);
178 if (composite_nomem(state->addrs[0], c)) return c;
179 state->addrs[1] = NULL;
180 state->names = talloc_array(state, char *, 2);
181 if (composite_nomem(state->names, c)) return c;
182 state->names[0] = talloc_strdup(state->names, state->name.name);
183 if (composite_nomem(state->names[0], c)) return c;
184 state->names[1] = NULL;
185 composite_done(c);
186 return c;
189 state->method = ctx->methods;
190 if (state->method == NULL) {
191 composite_error(c, NT_STATUS_BAD_NETWORK_NAME);
192 return c;
194 state->creq = setup_next_method(c);
195 if (composite_nomem(state->creq, c)) return c;
197 return c;
201 general name resolution method - recv side
203 NTSTATUS resolve_name_all_recv(struct composite_context *c,
204 TALLOC_CTX *mem_ctx,
205 struct socket_address ***addrs,
206 char ***names)
208 NTSTATUS status;
210 status = composite_wait(c);
212 if (NT_STATUS_IS_OK(status)) {
213 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
214 *addrs = talloc_steal(mem_ctx, state->addrs);
215 if (names) {
216 *names = talloc_steal(mem_ctx, state->names);
220 talloc_free(c);
221 return status;
224 struct composite_context *resolve_name_send(struct resolve_context *ctx,
225 TALLOC_CTX *mem_ctx,
226 struct nbt_name *name,
227 struct tevent_context *event_ctx)
229 return resolve_name_all_send(ctx, mem_ctx, 0, 0, name, event_ctx);
232 NTSTATUS resolve_name_recv(struct composite_context *c,
233 TALLOC_CTX *mem_ctx,
234 const char **reply_addr)
236 NTSTATUS status;
237 struct socket_address **addrs = NULL;
239 status = resolve_name_all_recv(c, mem_ctx, &addrs, NULL);
241 if (NT_STATUS_IS_OK(status)) {
242 *reply_addr = talloc_steal(mem_ctx, addrs[0]->addr);
243 talloc_free(addrs);
246 return status;
250 general name resolution - sync call
252 NTSTATUS resolve_name(struct resolve_context *ctx,
253 struct nbt_name *name,
254 TALLOC_CTX *mem_ctx,
255 const char **reply_addr,
256 struct tevent_context *ev)
258 struct composite_context *c = resolve_name_send(ctx, mem_ctx, name, ev);
259 return resolve_name_recv(c, mem_ctx, reply_addr);
262 /* Initialise a struct nbt_name with a NULL scope */
264 void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
266 nbt->name = name;
267 nbt->scope = NULL;
268 nbt->type = type;
271 /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
273 void make_nbt_name_client(struct nbt_name *nbt, const char *name)
275 make_nbt_name(nbt, name, NBT_NAME_CLIENT);
278 /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
280 void make_nbt_name_server(struct nbt_name *nbt, const char *name)
282 make_nbt_name(nbt, name, NBT_NAME_SERVER);