r25430: Add the loadparm context to all parametric options.
[ira/wip.git] / source4 / libcli / resolve / nbtlist.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    nbt list of addresses name resolution module
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23   TODO: we should lower the timeout, and add retries for each name
24 */
25
26 #include "includes.h"
27 #include "libcli/composite/composite.h"
28 #include "system/network.h"
29 #include "lib/socket/netif.h"
30 #include "librpc/gen_ndr/ndr_nbt.h"
31 #include "libcli/nbt/libnbt.h"
32 #include "param/param.h"
33
34 struct nbtlist_state {
35         struct nbt_name name;
36         struct nbt_name_socket *nbtsock;
37         int num_queries;
38         struct nbt_name_request **queries;
39         struct nbt_name_query *io_queries;
40         const char *reply_addr;
41 };
42
43 /*
44   handle events during nbtlist name resolution
45 */
46 static void nbtlist_handler(struct nbt_name_request *req)
47 {
48         struct composite_context *c = talloc_get_type(req->async.private, 
49                                                       struct composite_context);
50         struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
51         struct nbt_name_query *q;
52         int i;
53
54         for (i=0;i<state->num_queries;i++) {
55                 if (req == state->queries[i]) break;
56         }
57
58         if (i == state->num_queries) {
59                 /* not for us?! */
60                 composite_error(c, NT_STATUS_INTERNAL_ERROR);
61                 return;
62         }
63
64         q = &state->io_queries[i];
65
66         c->status = nbt_name_query_recv(req, state, q);
67
68         /* free the network resource directly */
69         talloc_free(state->nbtsock);
70         if (!composite_is_ok(c)) return;
71
72         if (state->io_queries[i].out.num_addrs < 1) {
73                 composite_error(c, NT_STATUS_UNEXPECTED_NETWORK_ERROR);
74                 return;
75         }
76
77         /* favor a local address if possible */
78         state->reply_addr = NULL;
79         for (i=0;i<q->out.num_addrs;i++) {
80                 if (iface_is_local(q->out.reply_addrs[i])) {
81                         state->reply_addr = talloc_steal(state, 
82                                                          q->out.reply_addrs[i]);
83                         break;
84                 }
85         }
86
87         if (state->reply_addr == NULL) {
88                 state->reply_addr = talloc_steal(state, 
89                                                  q->out.reply_addrs[0]);
90         }
91
92         composite_done(c);
93 }
94
95 /*
96   nbtlist name resolution method - async send
97  */
98 struct composite_context *resolve_name_nbtlist_send(TALLOC_CTX *mem_ctx,
99                                                     struct event_context *event_ctx,
100                                                     struct nbt_name *name, 
101                                                     const char **address_list,
102                                                     BOOL broadcast,
103                                                     BOOL wins_lookup)
104 {
105         struct composite_context *c;
106         struct nbtlist_state *state;
107         int i;
108
109         c = composite_create(event_ctx, event_ctx);
110         if (c == NULL) return NULL;
111
112         c->event_ctx = talloc_reference(c, event_ctx);
113         if (composite_nomem(c->event_ctx, c)) return c;
114
115         state = talloc(c, struct nbtlist_state);
116         if (composite_nomem(state, c)) return c;
117         c->private_data = state;
118
119         c->status = nbt_name_dup(state, name, &state->name);
120         if (!composite_is_ok(c)) return c;
121
122         state->name.name = strupper_talloc(state, state->name.name);
123         if (composite_nomem(state->name.name, c)) return c;
124         if (state->name.scope) {
125                 state->name.scope = strupper_talloc(state, state->name.scope);
126                 if (composite_nomem(state->name.scope, c)) return c;
127         }
128
129         /*
130          * we can't push long names on the wire,
131          * so bail out here to give a useful error message
132          */
133         if (strlen(state->name.name) > 15) {
134                 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
135                 return c;
136         }
137
138         state->nbtsock = nbt_name_socket_init(state, event_ctx);
139         if (composite_nomem(state->nbtsock, c)) return c;
140
141         /* count the address_list size */
142         for (i=0;address_list[i];i++) /* noop */ ;
143
144         state->num_queries = i;
145         state->io_queries = talloc_array(state, struct nbt_name_query, state->num_queries);
146         if (composite_nomem(state->io_queries, c)) return c;
147
148         state->queries = talloc_array(state, struct nbt_name_request *, state->num_queries);
149         if (composite_nomem(state->queries, c)) return c;
150
151         for (i=0;i<state->num_queries;i++) {
152                 state->io_queries[i].in.name        = state->name;
153                 state->io_queries[i].in.dest_addr   = talloc_strdup(state->io_queries, address_list[i]);
154                 if (composite_nomem(state->io_queries[i].in.dest_addr, c)) return c;
155
156                 state->io_queries[i].in.broadcast   = broadcast;
157                 state->io_queries[i].in.wins_lookup = wins_lookup;
158                 state->io_queries[i].in.timeout     = lp_parm_int(global_loadparm, NULL, "nbt", "timeout", 1);
159                 state->io_queries[i].in.retries     = 2;
160
161                 state->queries[i] = nbt_name_query_send(state->nbtsock, &state->io_queries[i]);
162                 if (composite_nomem(state->queries[i], c)) return c;
163
164                 state->queries[i]->async.fn      = nbtlist_handler;
165                 state->queries[i]->async.private = c;
166         }
167
168         return c;
169 }
170
171 /*
172   nbt list of addresses name resolution method - recv side
173  */
174 NTSTATUS resolve_name_nbtlist_recv(struct composite_context *c, 
175                                    TALLOC_CTX *mem_ctx, const char **reply_addr)
176 {
177         NTSTATUS status;
178
179         status = composite_wait(c);
180
181         if (NT_STATUS_IS_OK(status)) {
182                 struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
183                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
184         }
185
186         talloc_free(c);
187         return status;
188 }
189
190 /*
191   nbt list of addresses name resolution method - sync call
192  */
193 NTSTATUS resolve_name_nbtlist(struct nbt_name *name, 
194                               TALLOC_CTX *mem_ctx,
195                               const char **address_list,
196                               BOOL broadcast, BOOL wins_lookup,
197                               const char **reply_addr)
198 {
199         struct composite_context *c = resolve_name_nbtlist_send(mem_ctx, NULL, name, address_list, 
200                                                                 broadcast, wins_lookup);
201         return resolve_name_nbtlist_recv(c, mem_ctx, reply_addr);
202 }
203