2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 a composite function for name resolving
26 #include "lib/events/events.h"
27 #include "libnet/libnet.h"
28 #include "libcli/composite/composite.h"
29 #include "lib/messaging/messaging.h"
30 #include "lib/messaging/irpc.h"
31 #include "libcli/resolve/resolve.h"
32 #include "libcli/finddcs.h"
35 struct nbt_name hostname;
40 static void continue_name_resolved(struct composite_context *ctx);
44 * Sends asynchronous Lookup request
46 * @param io arguments and result of the call
49 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
50 struct libnet_Lookup *io)
52 struct composite_context *c;
53 struct lookup_state *s;
54 struct composite_context *cresolve_req;
57 /* allocate context and state structures */
58 c = talloc_zero(NULL, struct composite_context);
59 if (c == NULL) return NULL;
61 s = talloc_zero(c, struct lookup_state);
63 composite_error(c, NT_STATUS_NO_MEMORY);
67 /* prepare event context */
68 c->event_ctx = event_context_find(c);
69 if (c->event_ctx == NULL) {
70 composite_error(c, NT_STATUS_NO_MEMORY);
74 if (io == NULL || io->in.hostname == NULL) {
75 composite_error(c, NT_STATUS_INVALID_PARAMETER);
80 s->hostname.name = talloc_strdup(s, io->in.hostname);
81 s->hostname.type = io->in.type;
82 s->hostname.scope = NULL;
84 /* name resolution methods */
86 methods = io->in.methods;
88 methods = (const char**)ctx->name_res_methods;
92 c->state = COMPOSITE_STATE_IN_PROGRESS;
94 /* send resolve request */
95 cresolve_req = resolve_name_send(&s->hostname, c->event_ctx, methods);
97 composite_continue(c, cresolve_req, continue_name_resolved, c);
103 static void continue_name_resolved(struct composite_context *ctx)
105 struct composite_context *c;
106 struct lookup_state *s;
108 c = talloc_get_type(ctx->async.private_data, struct composite_context);
109 s = talloc_get_type(c->private_data, struct lookup_state);
111 c->status = resolve_name_recv(ctx, s, &s->address);
118 * Waits for and receives results of asynchronous Lookup call
120 * @param c composite context returned by asynchronous Lookup call
121 * @param mem_ctx memory context of the call
122 * @param io pointer to results (and arguments) of the call
123 * @return nt status code of execution
126 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
127 struct libnet_Lookup *io)
130 struct lookup_state *s;
132 status = composite_wait(c);
133 if (NT_STATUS_IS_OK(status)) {
134 s = talloc_get_type(c->private_data, struct lookup_state);
136 io->out.address = str_list_make(mem_ctx, s->address, NULL);
137 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
146 * Synchronous version of Lookup call
148 * @param mem_ctx memory context for the call
149 * @param io arguments and results of the call
150 * @return nt status code of execution
153 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
154 struct libnet_Lookup *io)
156 struct composite_context *c = libnet_Lookup_send(ctx, io);
157 return libnet_Lookup_recv(c, mem_ctx, io);
162 * Shortcut functions to find common types of name
163 * (and skip nbt name type argument)
168 * Sends asynchronous LookupHost request
170 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
171 struct libnet_Lookup *io)
173 io->in.type = NBT_NAME_SERVER;
174 return libnet_Lookup_send(ctx, io);
180 * Synchronous version of LookupHost call
182 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
183 struct libnet_Lookup *io)
185 struct composite_context *c = libnet_LookupHost_send(ctx, io);
186 return libnet_Lookup_recv(c, mem_ctx, io);
191 * Sends asynchronous LookupDCs request
193 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
195 struct libnet_LookupDCs *io)
197 struct composite_context *c;
198 struct messaging_context *msg_ctx = messaging_client_init(mem_ctx, ctx->event_ctx);
200 c = finddcs_send(mem_ctx, io->in.domain_name, io->in.name_type,
201 NULL, ctx->name_res_methods, ctx->event_ctx, msg_ctx);
206 * Waits for and receives results of asynchronous Lookup call
208 * @param c composite context returned by asynchronous Lookup call
209 * @param mem_ctx memory context of the call
210 * @param io pointer to results (and arguments) of the call
211 * @return nt status code of execution
214 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
215 struct libnet_LookupDCs *io)
218 status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
219 if (!NT_STATUS_IS_OK(status)) {
226 * Synchronous version of LookupDCs
228 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
229 struct libnet_LookupDCs *io)
231 struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
232 return libnet_LookupDCs_recv(c, mem_ctx, io);