r13924: Split more prototypes out of include/proto.h + initial work on header
[samba.git] / source4 / libnet / libnet_lookup.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2005
5    
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.
10    
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.
15    
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.
19 */
20
21 /*
22   a composite function for name resolving
23 */
24
25 #include "includes.h"
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
33 struct lookup_state {
34         struct composite_context *resolve_ctx;
35         struct nbt_name hostname;
36 };
37
38
39 /**
40  * Sends asynchronous Lookup request
41  *
42  * @param io arguments and result of the call
43  */
44
45 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
46                                              struct libnet_Lookup *io)
47 {
48         struct composite_context *c;
49         struct lookup_state *s;
50         const char** methods;
51
52         if (!io) return NULL;
53
54         /* allocate context and state structures */
55         c = talloc_zero(NULL, struct composite_context);
56         if (c == NULL) goto failed;
57
58         s = talloc_zero(c, struct lookup_state);
59         if (s == NULL) goto failed;
60         
61         /* prepare event context */
62         c->event_ctx = event_context_find(c);
63         if (c->event_ctx == NULL) goto failed;
64
65         /* parameters */
66         s->hostname.name   = talloc_strdup(s, io->in.hostname);
67         s->hostname.type   = io->in.type;
68         s->hostname.scope  = NULL;
69
70         /* name resolution methods */
71         if (io->in.methods) {
72                 methods = io->in.methods;
73         } else {
74                 methods = (const char**)ctx->name_res_methods;
75         }
76
77         c->private_data = s;
78         c->state        = COMPOSITE_STATE_IN_PROGRESS;
79
80         /* send resolve request */
81         s->resolve_ctx = resolve_name_send(&s->hostname, c->event_ctx, methods);
82
83         return c;
84
85 failed:
86         talloc_free(c);
87         return NULL;
88 }
89
90
91 /**
92  * Waits for and receives results of asynchronous Lookup call
93  *
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
98  */
99
100 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
101                             struct libnet_Lookup *io)
102 {
103         NTSTATUS status;
104         struct lookup_state *s;
105         const char *address;
106
107         s = talloc_get_type(c->private_data, struct lookup_state);
108
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);
113         }
114
115         return status;
116 }
117
118
119 /**
120  * Synchronous version of Lookup call
121  *
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
125  */
126
127 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
128                        struct libnet_Lookup *io)
129 {
130         struct composite_context *c = libnet_Lookup_send(ctx, io);
131         return libnet_Lookup_recv(c, mem_ctx, io);
132 }
133
134
135 /*
136  * Shortcut functions to find common types of name
137  * (and skip nbt name type argument)
138  */
139
140
141 /**
142  * Sends asynchronous LookupHost request
143  */
144 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
145                                                  struct libnet_Lookup *io)
146 {
147         io->in.type = NBT_NAME_SERVER;
148         return libnet_Lookup_send(ctx, io);
149 }
150
151
152
153 /**
154  * Synchronous version of LookupHost call
155  */
156 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
157                            struct libnet_Lookup *io)
158 {
159         struct composite_context *c = libnet_LookupHost_send(ctx, io);
160         return libnet_Lookup_recv(c, mem_ctx, io);
161 }
162
163
164 /**
165  * Sends asynchronous LookupPdc request
166  */
167 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
168                                                 TALLOC_CTX *mem_ctx,
169                                                 struct libnet_LookupDCs *io)
170 {
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,
174                          io->in.domain_name, 
175                          NBT_NAME_PDC,
176                          NULL, ctx->name_res_methods,
177                          ctx->event_ctx, msg_ctx);
178         return c;
179 }
180
181 /**
182  * Waits for and receives results of asynchronous Lookup call
183  *
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
188  */
189
190 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
191                                struct libnet_LookupDCs *io)
192 {
193         NTSTATUS status;
194         status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
195         if (!NT_STATUS_IS_OK(status)) {
196                 return status;
197         }
198         return status;
199 }
200
201 /**
202  * Synchronous version of LookupPdc
203  */
204 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
205                           struct libnet_LookupDCs *io)
206 {
207         struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
208         return libnet_LookupDCs_recv(c, mem_ctx, io);
209 }