r12608: Remove some unused #include lines.
[sfrench/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    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "libcli/composite/composite.h"
26
27 struct resolve_state {
28         struct nbt_name name;
29         const char **methods;
30         struct composite_context *creq;
31         const char *reply_addr;
32 };
33
34 static struct composite_context *setup_next_method(struct composite_context *c);
35
36 /* pointers to the resolver backends */
37 static const struct resolve_method {
38         const char *name;
39         struct composite_context *(*send_fn)(struct nbt_name *, struct event_context *);
40         NTSTATUS (*recv_fn)(struct composite_context *, TALLOC_CTX *, const char **);
41
42 } resolve_methods[] = {
43         { "bcast", resolve_name_bcast_send,  resolve_name_bcast_recv },
44         { "wins",  resolve_name_wins_send,   resolve_name_wins_recv },
45         { "host",  resolve_name_host_send,   resolve_name_host_recv }
46 };
47
48
49 /* 
50    find a matching backend
51 */
52 static const struct resolve_method *find_method(const char *name)
53 {
54         int i;
55         if (name == NULL) return NULL;
56         for (i=0;i<ARRAY_SIZE(resolve_methods);i++) {
57                 if (strcasecmp(name, resolve_methods[i].name) == 0) {
58                         return &resolve_methods[i];
59                 }
60         }
61         return NULL;
62 }
63
64 /*
65   handle completion of one name resolve method
66 */
67 static void resolve_handler(struct composite_context *creq)
68 {
69         struct composite_context *c = creq->async.private_data;
70         struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
71         const struct resolve_method *method = find_method(state->methods[0]);
72
73         c->status = method->recv_fn(creq, state, &state->reply_addr);
74         
75         if (!NT_STATUS_IS_OK(c->status)) {
76                 state->methods++;
77                 state->creq = setup_next_method(c);
78                 if (state->creq != NULL) {
79                         return;
80                 }
81         }
82
83         if (!NT_STATUS_IS_OK(c->status)) {
84                 c->state = COMPOSITE_STATE_ERROR;
85         } else {
86                 c->state = COMPOSITE_STATE_DONE;
87         }
88         if (c->async.fn) {
89                 c->async.fn(c);
90         }
91 }
92
93
94 static struct composite_context *setup_next_method(struct composite_context *c)
95 {
96         struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
97         struct composite_context *creq = NULL;
98
99         do {
100                 const struct resolve_method *method = find_method(state->methods[0]);
101                 if (method) {
102                         creq = method->send_fn(&state->name, c->event_ctx);
103                 }
104                 if (creq == NULL && state->methods[0]) state->methods++;
105
106         } while (!creq && state->methods[0]);
107
108         if (creq) {
109                 creq->async.fn = resolve_handler;
110                 creq->async.private_data = c;
111         }
112
113         return creq;
114 }
115
116 /*
117   general name resolution - async send
118  */
119 struct composite_context *resolve_name_send(struct nbt_name *name, struct event_context *event_ctx,
120                                             const char **methods)
121 {
122         struct composite_context *c;
123         struct resolve_state *state;
124         NTSTATUS status;
125
126         c = talloc_zero(NULL, struct composite_context);
127         if (c == NULL) goto failed;
128
129         state = talloc(c, struct resolve_state);
130         if (state == NULL) goto failed;
131
132         status = nbt_name_dup(state, name, &state->name);
133         if (!NT_STATUS_IS_OK(status)) goto failed;
134
135         if (methods == NULL) goto failed;
136         state->methods = str_list_copy(state, methods);
137         if (state->methods == NULL) goto failed;
138
139         c->state = COMPOSITE_STATE_IN_PROGRESS;
140         c->private_data = state;
141
142         if (event_ctx == NULL) {
143                 c->event_ctx = event_context_init(c);
144                 if (c->event_ctx == NULL) goto failed;
145
146         } else {
147                 c->event_ctx = talloc_reference(c, event_ctx);
148         }
149
150         if (is_ipaddress(state->name.name) || 
151             strcasecmp(state->name.name, "localhost") == 0) {
152                 struct ipv4_addr ip = interpret_addr2(state->name.name);
153                 state->reply_addr = talloc_strdup(state, sys_inet_ntoa(ip));
154                 if (!state->reply_addr) goto failed;
155                 composite_done(c);
156                 return c;
157         }
158
159         state->creq = setup_next_method(c);
160         if (state->creq == NULL) goto failed;
161         
162         return c;
163
164 failed:
165         talloc_free(c);
166         return NULL;
167 }
168
169 /*
170   general name resolution method - recv side
171  */
172 NTSTATUS resolve_name_recv(struct composite_context *c, 
173                            TALLOC_CTX *mem_ctx, const char **reply_addr)
174 {
175         NTSTATUS status;
176
177         status = composite_wait(c);
178
179         if (NT_STATUS_IS_OK(status)) {
180                 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
181                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
182         }
183
184         talloc_free(c);
185         return status;
186 }
187
188 /*
189   general name resolution - sync call
190  */
191 NTSTATUS resolve_name(struct nbt_name *name, TALLOC_CTX *mem_ctx, const char **reply_addr, 
192                       struct event_context *ev)
193 {
194         struct composite_context *c = resolve_name_send(name, ev, lp_name_resolve_order());
195         return resolve_name_recv(c, mem_ctx, reply_addr);
196 }
197
198 /* Initialise a struct nbt_name with a NULL scope */
199
200 void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
201 {
202         nbt->name = name;
203         nbt->scope = NULL;
204         nbt->type = type;
205 }
206
207 /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
208
209 void make_nbt_name_client(struct nbt_name *nbt, const char *name)
210 {
211         make_nbt_name(nbt, name, NBT_NAME_CLIENT);
212 }
213
214 /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
215
216 void make_nbt_name_server(struct nbt_name *nbt, const char *name)
217 {
218         make_nbt_name(nbt, name, NBT_NAME_SERVER);
219 }