r8076: Put name resolution methods into libnet_context. This allows libnet based
[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 "libcli/raw/libcliraw.h"
27 #include "lib/events/events.h"
28 #include "libnet/libnet.h"
29 #include "libcli/composite/composite.h"
30 #include "libcli/composite/monitor.h"
31 #include "libnet/composite.h"
32 #include "librpc/gen_ndr/ndr_nbt.h"
33
34
35 struct lookup_state {
36         struct composite_context *resolve_ctx;
37         struct nbt_name hostname;
38         const char **address;
39 };
40
41
42 /**
43  * Sends asynchronous Lookup request
44  *
45  * @param io arguments and result of the call
46  */
47
48 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
49                                              struct libnet_Lookup *io)
50 {
51         struct composite_context *c;
52         struct lookup_state *s;
53         const char** methods;
54
55         if (!io) return NULL;
56
57         /* allocate context and state structures */
58         c = talloc_zero(NULL, struct composite_context);
59         if (c == NULL) goto failed;
60
61         s = talloc_zero(c, struct lookup_state);
62         if (s == NULL) goto failed;
63         
64         /* prepare event context */
65         c->event_ctx = event_context_init(c);
66         if (c->event_ctx == NULL) goto failed;
67
68         /* parameters */
69         s->hostname.name   = talloc_strdup(s, io->in.hostname);
70         s->hostname.type   = io->in.type;
71         s->hostname.scope  = NULL;
72         s->address         = io->out.address;
73
74         /* name resolution methods */
75         if (io->in.methods) {
76                 methods = io->in.methods;
77         } else {
78                 methods = (const char**)ctx->name_res_methods;
79         }
80
81         c->private  = s;
82         c->state    = SMBCLI_REQUEST_SEND;
83
84         /* send resolve request */
85         s->resolve_ctx = resolve_name_send(&s->hostname, c->event_ctx, methods);
86
87         return c;
88
89 failed:
90         talloc_free(c);
91         return NULL;
92 }
93
94
95 /**
96  * Waits for and receives results of asynchronous Lookup call
97  *
98  * @param c composite context returned by asynchronous Lookup call
99  * @param mem_ctx memory context of the call
100  * @param io pointer to results (and arguments) of the call
101  * @return nt status code of execution
102  */
103
104 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
105                             struct libnet_Lookup *io)
106 {
107         NTSTATUS status;
108         struct lookup_state *s;
109
110         s = talloc_get_type(c->private, struct lookup_state);
111
112         status = resolve_name_recv(s->resolve_ctx, mem_ctx, s->address);
113         return status;
114 }
115
116
117 /**
118  * Synchronous version of Lookup call
119  *
120  * @param mem_ctx memory context for the call
121  * @param io arguments and results of the call
122  * @return nt status code of execution
123  */
124
125 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
126                        struct libnet_Lookup *io)
127 {
128         struct composite_context *c = libnet_Lookup_send(ctx, io);
129         return libnet_Lookup_recv(c, mem_ctx, io);
130 }
131
132
133 /*
134  * Shortcut functions to find common types of name
135  * (and skip nbt name type argument)
136  */
137
138
139 /**
140  * Sends asynchronous LookupHost request
141  */
142 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
143                                                  struct libnet_Lookup *io)
144 {
145         io->in.type = NBT_NAME_SERVER;
146         return libnet_Lookup_send(ctx, io);
147 }
148
149
150
151 /**
152  * Synchronous version of LookupHost call
153  */
154 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
155                            struct libnet_Lookup *io)
156 {
157         struct composite_context *c = libnet_LookupHost_send(ctx, io);
158         return libnet_Lookup_recv(c, mem_ctx, io);
159 }
160
161
162 /**
163  * Sends asynchronous LookupPdc request
164  */
165 struct composite_context* libnet_LookupPdc_send(struct libnet_context *ctx,
166                                                 struct libnet_Lookup *io)
167 {
168         io->in.type = NBT_NAME_PDC;
169         return libnet_Lookup_send(ctx, io);
170 }
171
172
173 /**
174  * Synchronous version of LookupPdc
175  */
176 NTSTATUS libnet_LookupPdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
177                           struct libnet_Lookup *io)
178 {
179         struct composite_context *c = libnet_LookupPdc_send(ctx, io);
180         return libnet_Lookup_recv(c, mem_ctx, io);
181 }