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