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"
34 struct composite_context *resolve_ctx;
35 struct nbt_name hostname;
40 * Sends asynchronous Lookup request
42 * @param io arguments and result of the call
45 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
46 struct libnet_Lookup *io)
48 struct composite_context *c;
49 struct lookup_state *s;
54 /* allocate context and state structures */
55 c = talloc_zero(NULL, struct composite_context);
56 if (c == NULL) goto failed;
58 s = talloc_zero(c, struct lookup_state);
59 if (s == NULL) goto failed;
61 /* prepare event context */
62 c->event_ctx = event_context_find(c);
63 if (c->event_ctx == NULL) goto failed;
66 s->hostname.name = talloc_strdup(s, io->in.hostname);
67 s->hostname.type = io->in.type;
68 s->hostname.scope = NULL;
70 /* name resolution methods */
72 methods = io->in.methods;
74 methods = (const char**)ctx->name_res_methods;
78 c->state = COMPOSITE_STATE_IN_PROGRESS;
80 /* send resolve request */
81 s->resolve_ctx = resolve_name_send(&s->hostname, c->event_ctx, methods);
92 * Waits for and receives results of asynchronous Lookup call
94 * @param c composite context returned by asynchronous Lookup call
95 * @param mem_ctx memory context of the call
96 * @param io pointer to results (and arguments) of the call
97 * @return nt status code of execution
100 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
101 struct libnet_Lookup *io)
104 struct lookup_state *s;
107 s = talloc_get_type(c->private_data, struct lookup_state);
109 status = resolve_name_recv(s->resolve_ctx, mem_ctx, &address);
110 if (NT_STATUS_IS_OK(status)) {
111 io->out.address = str_list_make(mem_ctx, address, NULL);
112 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
120 * Synchronous version of Lookup call
122 * @param mem_ctx memory context for the call
123 * @param io arguments and results of the call
124 * @return nt status code of execution
127 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
128 struct libnet_Lookup *io)
130 struct composite_context *c = libnet_Lookup_send(ctx, io);
131 return libnet_Lookup_recv(c, mem_ctx, io);
136 * Shortcut functions to find common types of name
137 * (and skip nbt name type argument)
142 * Sends asynchronous LookupHost request
144 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
145 struct libnet_Lookup *io)
147 io->in.type = NBT_NAME_SERVER;
148 return libnet_Lookup_send(ctx, io);
154 * Synchronous version of LookupHost call
156 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
157 struct libnet_Lookup *io)
159 struct composite_context *c = libnet_LookupHost_send(ctx, io);
160 return libnet_Lookup_recv(c, mem_ctx, io);
165 * Sends asynchronous LookupPdc request
167 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
169 struct libnet_LookupDCs *io)
171 struct messaging_context *msg_ctx = messaging_client_init(mem_ctx, ctx->event_ctx);
172 struct composite_context *c;
173 c = finddcs_send(mem_ctx,
176 NULL, ctx->name_res_methods,
177 ctx->event_ctx, msg_ctx);
182 * Waits for and receives results of asynchronous Lookup call
184 * @param c composite context returned by asynchronous Lookup call
185 * @param mem_ctx memory context of the call
186 * @param io pointer to results (and arguments) of the call
187 * @return nt status code of execution
190 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
191 struct libnet_LookupDCs *io)
194 status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
195 if (!NT_STATUS_IS_OK(status)) {
202 * Synchronous version of LookupPdc
204 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
205 struct libnet_LookupDCs *io)
207 struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
208 return libnet_LookupDCs_recv(c, mem_ctx, io);