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