2 Unix SMB/CIFS implementation.
4 general name resolution interface
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Jelmer Vernooij 2007
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.
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.
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/>.
24 #include "libcli/composite/composite.h"
25 #include "libcli/resolve/resolve.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "system/network.h"
28 #include "lib/socket/socket.h"
29 #include "../lib/util/dlinklist.h"
30 #include "lib/tsocket/tsocket.h"
32 struct resolve_state
{
33 struct resolve_context
*ctx
;
34 struct resolve_method
*method
;
38 struct composite_context
*creq
;
39 struct socket_address
**addrs
;
43 static struct composite_context
*setup_next_method(struct composite_context
*c
);
46 struct resolve_context
{
47 struct resolve_method
{
48 resolve_name_send_fn send_fn
;
49 resolve_name_recv_fn recv_fn
;
51 struct resolve_method
*prev
, *next
;
56 * Initialize a resolve context
58 struct resolve_context
*resolve_context_init(TALLOC_CTX
*mem_ctx
)
60 return talloc_zero(mem_ctx
, struct resolve_context
);
64 * Add a resolve method
66 bool resolve_context_add_method(struct resolve_context
*ctx
, resolve_name_send_fn send_fn
,
67 resolve_name_recv_fn recv_fn
, void *userdata
)
69 struct resolve_method
*method
= talloc_zero(ctx
, struct resolve_method
);
74 method
->send_fn
= send_fn
;
75 method
->recv_fn
= recv_fn
;
76 method
->privdata
= userdata
;
77 DLIST_ADD_END(ctx
->methods
, method
, struct resolve_method
*);
82 handle completion of one name resolve method
84 static void resolve_handler(struct composite_context
*creq
)
86 struct composite_context
*c
= (struct composite_context
*)creq
->async
.private_data
;
87 struct resolve_state
*state
= talloc_get_type(c
->private_data
, struct resolve_state
);
88 const struct resolve_method
*method
= state
->method
;
90 c
->status
= method
->recv_fn(creq
, state
, &state
->addrs
, &state
->names
);
92 if (!NT_STATUS_IS_OK(c
->status
)) {
93 state
->method
= state
->method
->next
;
94 state
->creq
= setup_next_method(c
);
95 if (state
->creq
!= NULL
) {
100 if (!NT_STATUS_IS_OK(c
->status
)) {
101 c
->state
= COMPOSITE_STATE_ERROR
;
103 c
->state
= COMPOSITE_STATE_DONE
;
111 static struct composite_context
*setup_next_method(struct composite_context
*c
)
113 struct resolve_state
*state
= talloc_get_type(c
->private_data
, struct resolve_state
);
114 struct composite_context
*creq
= NULL
;
118 creq
= state
->method
->send_fn(c
, c
->event_ctx
,
119 state
->method
->privdata
,
124 if (creq
== NULL
&& state
->method
) state
->method
= state
->method
->next
;
126 } while (!creq
&& state
->method
);
129 creq
->async
.fn
= resolve_handler
;
130 creq
->async
.private_data
= c
;
137 general name resolution - async send
139 struct composite_context
*resolve_name_all_send(struct resolve_context
*ctx
,
143 struct nbt_name
*name
,
144 struct tevent_context
*event_ctx
)
146 struct composite_context
*c
;
147 struct resolve_state
*state
;
149 if (event_ctx
== NULL
) {
153 c
= composite_create(mem_ctx
, event_ctx
);
154 if (c
== NULL
) return NULL
;
156 if (composite_nomem(c
->event_ctx
, c
)) return c
;
158 state
= talloc(c
, struct resolve_state
);
159 if (composite_nomem(state
, c
)) return c
;
160 c
->private_data
= state
;
162 state
->flags
= flags
;
165 c
->status
= nbt_name_dup(state
, name
, &state
->name
);
166 if (!composite_is_ok(c
)) return c
;
168 state
->ctx
= talloc_reference(state
, ctx
);
169 if (composite_nomem(state
->ctx
, c
)) return c
;
171 if (is_ipaddress(state
->name
.name
) ||
172 strcasecmp(state
->name
.name
, "localhost") == 0) {
173 struct in_addr ip
= interpret_addr2(state
->name
.name
);
175 state
->addrs
= talloc_array(state
, struct socket_address
*, 2);
176 if (composite_nomem(state
->addrs
, c
)) return c
;
177 state
->addrs
[0] = socket_address_from_strings(state
->addrs
, "ipv4",
179 if (composite_nomem(state
->addrs
[0], c
)) return c
;
180 state
->addrs
[1] = NULL
;
181 state
->names
= talloc_array(state
, char *, 2);
182 if (composite_nomem(state
->names
, c
)) return c
;
183 state
->names
[0] = talloc_strdup(state
->names
, state
->name
.name
);
184 if (composite_nomem(state
->names
[0], c
)) return c
;
185 state
->names
[1] = NULL
;
190 state
->method
= ctx
->methods
;
191 if (state
->method
== NULL
) {
192 composite_error(c
, NT_STATUS_BAD_NETWORK_NAME
);
195 state
->creq
= setup_next_method(c
);
196 if (composite_nomem(state
->creq
, c
)) return c
;
202 general name resolution method - recv side
204 NTSTATUS
resolve_name_all_recv(struct composite_context
*c
,
206 struct socket_address
***addrs
,
211 status
= composite_wait(c
);
213 if (NT_STATUS_IS_OK(status
)) {
214 struct resolve_state
*state
= talloc_get_type(c
->private_data
, struct resolve_state
);
215 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
217 *names
= talloc_steal(mem_ctx
, state
->names
);
225 struct composite_context
*resolve_name_send(struct resolve_context
*ctx
,
227 struct nbt_name
*name
,
228 struct tevent_context
*event_ctx
)
230 return resolve_name_all_send(ctx
, mem_ctx
, 0, 0, name
, event_ctx
);
233 NTSTATUS
resolve_name_recv(struct composite_context
*c
,
235 const char **reply_addr
)
238 struct socket_address
**addrs
= NULL
;
240 status
= resolve_name_all_recv(c
, mem_ctx
, &addrs
, NULL
);
242 if (NT_STATUS_IS_OK(status
)) {
243 struct tsocket_address
*t_addr
= socket_address_to_tsocket_address(addrs
, addrs
[0]);
245 return NT_STATUS_NO_MEMORY
;
248 *reply_addr
= tsocket_address_inet_addr_string(t_addr
, mem_ctx
);
251 return NT_STATUS_NO_MEMORY
;
259 general name resolution - sync call
261 NTSTATUS
resolve_name(struct resolve_context
*ctx
,
262 struct nbt_name
*name
,
264 const char **reply_addr
,
265 struct tevent_context
*ev
)
267 struct composite_context
*c
= resolve_name_send(ctx
, mem_ctx
, name
, ev
);
268 return resolve_name_recv(c
, mem_ctx
, reply_addr
);
271 /* Initialise a struct nbt_name with a NULL scope */
273 void make_nbt_name(struct nbt_name
*nbt
, const char *name
, int type
)
280 /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
282 void make_nbt_name_client(struct nbt_name
*nbt
, const char *name
)
284 make_nbt_name(nbt
, name
, NBT_NAME_CLIENT
);
287 /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
289 void make_nbt_name_server(struct nbt_name
*nbt
, const char *name
)
291 make_nbt_name(nbt
, name
, NBT_NAME_SERVER
);