r12861: Cope when we are not supplied the messaging context. This is just
[jelmer/samba4-debian.git] / source / 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
32 struct lookup_state {
33         struct composite_context *resolve_ctx;
34         struct nbt_name hostname;
35 };
36
37
38 /**
39  * Sends asynchronous Lookup request
40  *
41  * @param io arguments and result of the call
42  */
43
44 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
45                                              struct libnet_Lookup *io)
46 {
47         struct composite_context *c;
48         struct lookup_state *s;
49         const char** methods;
50
51         if (!io) return NULL;
52
53         /* allocate context and state structures */
54         c = talloc_zero(NULL, struct composite_context);
55         if (c == NULL) goto failed;
56
57         s = talloc_zero(c, struct lookup_state);
58         if (s == NULL) goto failed;
59         
60         /* prepare event context */
61         c->event_ctx = event_context_find(c);
62         if (c->event_ctx == NULL) goto failed;
63
64         /* parameters */
65         s->hostname.name   = talloc_strdup(s, io->in.hostname);
66         s->hostname.type   = io->in.type;
67         s->hostname.scope  = NULL;
68
69         /* name resolution methods */
70         if (io->in.methods) {
71                 methods = io->in.methods;
72         } else {
73                 methods = (const char**)ctx->name_res_methods;
74         }
75
76         c->private_data = s;
77         c->state        = COMPOSITE_STATE_IN_PROGRESS;
78
79         /* send resolve request */
80         s->resolve_ctx = resolve_name_send(&s->hostname, c->event_ctx, methods);
81
82         return c;
83
84 failed:
85         talloc_free(c);
86         return NULL;
87 }
88
89
90 /**
91  * Waits for and receives results of asynchronous Lookup call
92  *
93  * @param c composite context returned by asynchronous Lookup call
94  * @param mem_ctx memory context of the call
95  * @param io pointer to results (and arguments) of the call
96  * @return nt status code of execution
97  */
98
99 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
100                             struct libnet_Lookup *io)
101 {
102         NTSTATUS status;
103         struct lookup_state *s;
104         const char *address;
105
106         s = talloc_get_type(c->private_data, struct lookup_state);
107
108         status = resolve_name_recv(s->resolve_ctx, mem_ctx, &address);
109         if (NT_STATUS_IS_OK(status)) {
110                 io->out.address = str_list_make(mem_ctx, address, NULL);
111                 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
112         }
113
114         return status;
115 }
116
117
118 /**
119  * Synchronous version of Lookup call
120  *
121  * @param mem_ctx memory context for the call
122  * @param io arguments and results of the call
123  * @return nt status code of execution
124  */
125
126 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
127                        struct libnet_Lookup *io)
128 {
129         struct composite_context *c = libnet_Lookup_send(ctx, io);
130         return libnet_Lookup_recv(c, mem_ctx, io);
131 }
132
133
134 /*
135  * Shortcut functions to find common types of name
136  * (and skip nbt name type argument)
137  */
138
139
140 /**
141  * Sends asynchronous LookupHost request
142  */
143 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
144                                                  struct libnet_Lookup *io)
145 {
146         io->in.type = NBT_NAME_SERVER;
147         return libnet_Lookup_send(ctx, io);
148 }
149
150
151
152 /**
153  * Synchronous version of LookupHost call
154  */
155 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
156                            struct libnet_Lookup *io)
157 {
158         struct composite_context *c = libnet_LookupHost_send(ctx, io);
159         return libnet_Lookup_recv(c, mem_ctx, io);
160 }
161
162
163 /**
164  * Sends asynchronous LookupPdc request
165  */
166 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
167                                                 TALLOC_CTX *mem_ctx,
168                                                 struct libnet_LookupDCs *io)
169 {
170         struct messaging_context *msg_ctx = messaging_init(mem_ctx, random() % 0x10000000, ctx->event_ctx);
171         struct composite_context *c;
172         c = finddcs_send(mem_ctx,
173                          io->in.domain_name, 
174                          NBT_NAME_PDC,
175                          NULL, ctx->name_res_methods,
176                          ctx->event_ctx, msg_ctx);
177         return c;
178 }
179
180 /**
181  * Waits for and receives results of asynchronous Lookup call
182  *
183  * @param c composite context returned by asynchronous Lookup call
184  * @param mem_ctx memory context of the call
185  * @param io pointer to results (and arguments) of the call
186  * @return nt status code of execution
187  */
188
189 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
190                                struct libnet_LookupDCs *io)
191 {
192         NTSTATUS status;
193         status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
194         if (!NT_STATUS_IS_OK(status)) {
195                 return status;
196         }
197         return status;
198 }
199
200 /**
201  * Synchronous version of LookupPdc
202  */
203 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
204                           struct libnet_LookupDCs *io)
205 {
206         struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
207         return libnet_LookupDCs_recv(c, mem_ctx, io);
208 }