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