Merge branch 'master' of ssh://git.samba.org/data/git/samba
[bbaumbach/samba-autobuild/.git] / source4 / libcli / resolve / resolve.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    general name resolution interface
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Jelmer Vernooij 2007
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
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 "system/network.h"
29 #include "lib/socket/socket.h"
30 #include "../lib/util/dlinklist.h"
31
32 struct resolve_state {
33         struct resolve_context *ctx;
34         struct resolve_method *method;
35         struct nbt_name name;
36         struct composite_context *creq;
37         struct socket_address **addrs;
38 };
39
40 static struct composite_context *setup_next_method(struct composite_context *c);
41
42
43 struct resolve_context {
44         struct resolve_method {
45                 resolve_name_send_fn send_fn;
46                 resolve_name_recv_fn recv_fn;
47                 void *privdata;
48                 struct resolve_method *prev, *next;
49         } *methods;
50 };
51
52 /**
53  * Initialize a resolve context
54  */
55 struct resolve_context *resolve_context_init(TALLOC_CTX *mem_ctx)
56 {
57         return talloc_zero(mem_ctx, struct resolve_context);
58 }
59
60 /**
61  * Add a resolve method
62  */
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)
65 {
66         struct resolve_method *method = talloc_zero(ctx, struct resolve_method);
67
68         if (method == NULL)
69                 return false;
70
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 *);
75         return true;
76 }
77
78 /**
79   handle completion of one name resolve method
80 */
81 static void resolve_handler(struct composite_context *creq)
82 {
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;
86
87         c->status = method->recv_fn(creq, state, &state->addrs);
88         
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) {
93                         return;
94                 }
95         }
96
97         if (!NT_STATUS_IS_OK(c->status)) {
98                 c->state = COMPOSITE_STATE_ERROR;
99         } else {
100                 c->state = COMPOSITE_STATE_DONE;
101         }
102         if (c->async.fn) {
103                 c->async.fn(c);
104         }
105 }
106
107
108 static struct composite_context *setup_next_method(struct composite_context *c)
109 {
110         struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
111         struct composite_context *creq = NULL;
112
113         do {
114                 if (state->method) {
115                         creq = state->method->send_fn(c, c->event_ctx, state->method->privdata, &state->name);
116                 }
117                 if (creq == NULL && state->method) state->method = state->method->next;
118
119         } while (!creq && state->method);
120
121         if (creq) {
122                 creq->async.fn = resolve_handler;
123                 creq->async.private_data = c;
124         }
125
126         return creq;
127 }
128
129 /*
130   general name resolution - async send
131  */
132 struct composite_context *resolve_name_all_send(struct resolve_context *ctx,
133                                                 struct nbt_name *name,
134                                                 struct event_context *event_ctx)
135 {
136         struct composite_context *c;
137         struct resolve_state *state;
138
139         if (ctx == NULL || event_ctx == NULL) {
140                 return NULL;
141         }
142
143         c = composite_create(ctx, event_ctx);
144         if (c == NULL) return NULL;
145
146         if (composite_nomem(c->event_ctx, c)) return c;
147
148         state = talloc(c, struct resolve_state);
149         if (composite_nomem(state, c)) return c;
150         c->private_data = state;
151
152         c->status = nbt_name_dup(state, name, &state->name);
153         if (!composite_is_ok(c)) return c;
154         
155         state->ctx = talloc_reference(state, ctx);
156         if (composite_nomem(state->ctx, c)) return c;
157
158         if (is_ipaddress(state->name.name) || 
159             strcasecmp(state->name.name, "localhost") == 0) {
160                 struct in_addr ip = interpret_addr2(state->name.name);
161
162                 state->addrs = talloc_array(state, struct socket_address *, 2);
163                 if (composite_nomem(state->addrs, c)) return c;
164                 state->addrs[0] = socket_address_from_strings(state->addrs, "ipv4",
165                                                               inet_ntoa(ip), 0);
166                 if (composite_nomem(state->addrs[0], c)) return c;
167                 state->addrs[1] = NULL;
168                 composite_done(c);
169                 return c;
170         }
171
172         state->method = ctx->methods;
173         if (state->method == NULL) {
174                 composite_error(c, NT_STATUS_BAD_NETWORK_NAME);
175                 return c;
176         }
177         state->creq = setup_next_method(c);
178         if (composite_nomem(state->creq, c)) return c;
179         
180         return c;
181 }
182
183 /*
184   general name resolution method - recv side
185  */
186 NTSTATUS resolve_name_all_recv(struct composite_context *c,
187                                TALLOC_CTX *mem_ctx,
188                                struct socket_address ***addrs)
189 {
190         NTSTATUS status;
191
192         status = composite_wait(c);
193
194         if (NT_STATUS_IS_OK(status)) {
195                 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
196                 *addrs = talloc_steal(mem_ctx, state->addrs);
197         }
198
199         talloc_free(c);
200         return status;
201 }
202
203 /*
204   general name resolution - sync call
205  */
206 NTSTATUS resolve_all_name(struct resolve_context *ctx,
207                       struct nbt_name *name,
208                       TALLOC_CTX *mem_ctx,
209                       struct socket_address ***addrs,
210                       struct event_context *ev)
211 {
212         struct composite_context *c = resolve_name_all_send(ctx, name, ev);
213         return resolve_name_all_recv(c, mem_ctx, addrs);
214 }
215
216 struct composite_context *resolve_name_send(struct resolve_context *ctx,
217                                             struct nbt_name *name,
218                                             struct event_context *event_ctx)
219 {
220         return resolve_name_all_send(ctx, name, event_ctx);
221 }
222
223 NTSTATUS resolve_name_recv(struct composite_context *c,
224                            TALLOC_CTX *mem_ctx,
225                            const char **reply_addr)
226 {
227         NTSTATUS status;
228         struct socket_address **addrs = NULL;
229
230         status = resolve_name_all_recv(c, mem_ctx, &addrs);
231
232         if (NT_STATUS_IS_OK(status)) {
233                 *reply_addr = talloc_steal(mem_ctx, addrs[0]->addr);
234                 talloc_free(addrs);
235         }
236
237         return status;
238 }
239
240 /*
241   general name resolution - sync call
242  */
243 NTSTATUS resolve_name(struct resolve_context *ctx,
244                           struct nbt_name *name,
245                           TALLOC_CTX *mem_ctx,
246                           const char **reply_addr,
247                           struct event_context *ev)
248 {
249         struct composite_context *c = resolve_name_send(ctx, name, ev);
250         return resolve_name_recv(c, mem_ctx, reply_addr);
251 }
252
253 /* Initialise a struct nbt_name with a NULL scope */
254
255 void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
256 {
257         nbt->name = name;
258         nbt->scope = NULL;
259         nbt->type = type;
260 }
261
262 /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
263
264 void make_nbt_name_client(struct nbt_name *nbt, const char *name)
265 {
266         make_nbt_name(nbt, name, NBT_NAME_CLIENT);
267 }
268
269 /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
270
271 void make_nbt_name_server(struct nbt_name *nbt, const char *name)
272 {
273         make_nbt_name(nbt, name, NBT_NAME_SERVER);
274 }
275
276