r5197: moved events code to lib/events/ (suggestion from metze)
[jelmer/samba4-debian.git] / source / 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/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27
28 struct resolve_state {
29         struct nbt_name name;
30         const char **methods;
31         struct composite_context *req;
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)(struct nbt_name *, struct event_context *);
41         NTSTATUS (*recv_fn)(struct composite_context *, TALLOC_CTX *, const char **);
42 } 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(methods);i++) {
57                 if (strcasecmp(name, methods[i].name) == 0) {
58                         return &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 *req)
68 {
69         struct composite_context *c = req->async.private;
70         struct resolve_state *state = talloc_get_type(c->private, struct resolve_state);
71         const struct resolve_method *method = find_method(state->methods[0]);
72
73         c->status = method->recv_fn(req, state, &state->reply_addr);
74         
75         if (!NT_STATUS_IS_OK(c->status)) {
76                 state->methods++;
77                 state->req = setup_next_method(c);
78                 if (state->req != NULL) {
79                         return;
80                 }
81         }
82
83         if (!NT_STATUS_IS_OK(c->status)) {
84                 c->state = SMBCLI_REQUEST_ERROR;
85         } else {
86                 c->state = SMBCLI_REQUEST_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, struct resolve_state);
97         struct composite_context *req = NULL;
98
99         do {
100                 const struct resolve_method *method = find_method(state->methods[0]);
101                 if (method) {
102                         req = method->send_fn(&state->name, c->event_ctx);
103                 }
104                 if (req == NULL) state->methods++;
105         } while (!req && state->methods[0]);
106
107         if (req) {
108                 req->async.fn = resolve_handler;
109                 req->async.private = c;
110         }
111
112         return req;
113 }
114
115 /*
116   general name resolution - async send
117  */
118 struct composite_context *resolve_name_send(struct nbt_name *name, struct event_context *event_ctx)
119 {
120         struct composite_context *c;
121         struct resolve_state *state;
122         NTSTATUS status;
123
124         c = talloc_zero(NULL, struct composite_context);
125         if (c == NULL) goto failed;
126
127         state = talloc(c, struct resolve_state);
128         if (state == NULL) goto failed;
129
130         status = nbt_name_dup(state, name, &state->name);
131         if (!NT_STATUS_IS_OK(status)) goto failed;
132
133         state->methods = lp_name_resolve_order();
134         if (state->methods == NULL) {
135                 return NULL;
136         }
137
138         c->state = SMBCLI_REQUEST_SEND;
139         c->private = state;
140         if (event_ctx == NULL) {
141                 c->event_ctx = event_context_init(c);
142                 if (c->event_ctx == NULL) goto failed;
143         } else {
144                 c->event_ctx = talloc_reference(c, event_ctx);
145         }
146
147         state->req = setup_next_method(c);
148         if (state->req == NULL) goto failed;
149         
150         return c;
151
152 failed:
153         talloc_free(c);
154         return NULL;
155 }
156
157 /*
158   general name resolution method - recv side
159  */
160 NTSTATUS resolve_name_recv(struct composite_context *c, 
161                            TALLOC_CTX *mem_ctx, const char **reply_addr)
162 {
163         NTSTATUS status;
164
165         status = composite_wait(c);
166
167         if (NT_STATUS_IS_OK(status)) {
168                 struct resolve_state *state = talloc_get_type(c->private, struct resolve_state);
169                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
170         }
171
172         talloc_free(c);
173         return status;
174 }
175
176 /*
177   general name resolution - sync call
178  */
179 NTSTATUS resolve_name(struct nbt_name *name, TALLOC_CTX *mem_ctx, const char **reply_addr)
180 {
181         struct composite_context *c = resolve_name_send(name, NULL);
182         return resolve_name_recv(c, mem_ctx, reply_addr);
183 }