Use global iconv convenience rather than global loadparm.
[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 #include "libcli/resolve/resolve.h"
34
35 struct nbtlist_state {
36         struct nbt_name name;
37         struct nbt_name_socket *nbtsock;
38         int num_queries;
39         struct nbt_name_request **queries;
40         struct nbt_name_query *io_queries;
41         const char *reply_addr;
42         struct interface *ifaces;
43 };
44
45 /*
46   handle events during nbtlist name resolution
47 */
48 static void nbtlist_handler(struct nbt_name_request *req)
49 {
50         struct composite_context *c = talloc_get_type(req->async.private_data,
51                                                       struct composite_context);
52         struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
53         struct nbt_name_query *q;
54         int i;
55
56         for (i=0;i<state->num_queries;i++) {
57                 if (req == state->queries[i]) break;
58         }
59
60         if (i == state->num_queries) {
61                 /* not for us?! */
62                 composite_error(c, NT_STATUS_INTERNAL_ERROR);
63                 return;
64         }
65
66         q = &state->io_queries[i];
67
68         c->status = nbt_name_query_recv(req, state, q);
69
70         /* free the network resource directly */
71         talloc_free(state->nbtsock);
72         if (!composite_is_ok(c)) return;
73
74         if (state->io_queries[i].out.num_addrs < 1) {
75                 composite_error(c, NT_STATUS_UNEXPECTED_NETWORK_ERROR);
76                 return;
77         }
78
79         /* favor a local address if possible */
80         state->reply_addr = NULL;
81         for (i=0;i<q->out.num_addrs;i++) {
82                 if (iface_is_local(state->ifaces, q->out.reply_addrs[i])) {
83                         state->reply_addr = talloc_steal(state, 
84                                                          q->out.reply_addrs[i]);
85                         break;
86                 }
87         }
88
89         if (state->reply_addr == NULL) {
90                 state->reply_addr = talloc_steal(state, 
91                                                  q->out.reply_addrs[0]);
92         }
93
94         composite_done(c);
95 }
96
97 /*
98   nbtlist name resolution method - async send
99  */
100 struct composite_context *resolve_name_nbtlist_send(TALLOC_CTX *mem_ctx,
101                                                     struct event_context *event_ctx,
102                                                     struct nbt_name *name, 
103                                                     const char **address_list,
104                                                     struct interface *ifaces,
105                                                     uint16_t nbt_port,
106                                                     int nbt_timeout,
107                                                     bool broadcast,
108                                                     bool wins_lookup)
109 {
110         struct composite_context *c;
111         struct nbtlist_state *state;
112         int i;
113
114         c = composite_create(mem_ctx, event_ctx);
115         if (c == NULL) return NULL;
116
117         if (composite_nomem(c->event_ctx, c)) return c;
118
119         state = talloc(c, struct nbtlist_state);
120         if (composite_nomem(state, c)) return c;
121         c->private_data = state;
122
123         c->status = nbt_name_dup(state, name, &state->name);
124         if (!composite_is_ok(c)) return c;
125
126         state->name.name = strupper_talloc(state, state->name.name);
127         if (composite_nomem(state->name.name, c)) return c;
128         if (state->name.scope) {
129                 state->name.scope = strupper_talloc(state, state->name.scope);
130                 if (composite_nomem(state->name.scope, c)) return c;
131         }
132
133         state->ifaces = talloc_reference(state, ifaces);
134
135         /*
136          * we can't push long names on the wire,
137          * so bail out here to give a useful error message
138          */
139         if (strlen(state->name.name) > 15) {
140                 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
141                 return c;
142         }
143
144         state->nbtsock = nbt_name_socket_init(state, event_ctx, 
145                                               global_iconv_convenience);
146         if (composite_nomem(state->nbtsock, c)) return c;
147
148         /* count the address_list size */
149         for (i=0;address_list[i];i++) /* noop */ ;
150
151         state->num_queries = i;
152         state->io_queries = talloc_array(state, struct nbt_name_query, state->num_queries);
153         if (composite_nomem(state->io_queries, c)) return c;
154
155         state->queries = talloc_array(state, struct nbt_name_request *, state->num_queries);
156         if (composite_nomem(state->queries, c)) return c;
157
158         for (i=0;i<state->num_queries;i++) {
159                 state->io_queries[i].in.name        = state->name;
160                 state->io_queries[i].in.dest_addr   = talloc_strdup(state->io_queries, address_list[i]);
161                 state->io_queries[i].in.dest_port   = nbt_port;
162                 if (composite_nomem(state->io_queries[i].in.dest_addr, c)) return c;
163
164                 state->io_queries[i].in.broadcast   = broadcast;
165                 state->io_queries[i].in.wins_lookup = wins_lookup;
166                 state->io_queries[i].in.timeout     = nbt_timeout;
167                 state->io_queries[i].in.retries     = 2;
168
169                 state->queries[i] = nbt_name_query_send(state->nbtsock, &state->io_queries[i]);
170                 if (composite_nomem(state->queries[i], c)) return c;
171
172                 state->queries[i]->async.fn      = nbtlist_handler;
173                 state->queries[i]->async.private_data = c;
174         }
175
176         return c;
177 }
178
179 /*
180   nbt list of addresses name resolution method - recv side
181  */
182 NTSTATUS resolve_name_nbtlist_recv(struct composite_context *c, 
183                                    TALLOC_CTX *mem_ctx, const char **reply_addr)
184 {
185         NTSTATUS status;
186
187         status = composite_wait(c);
188
189         if (NT_STATUS_IS_OK(status)) {
190                 struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
191                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
192         }
193
194         talloc_free(c);
195         return status;
196 }
197
198 /*
199   nbt list of addresses name resolution method - sync call
200  */
201 NTSTATUS resolve_name_nbtlist(struct nbt_name *name, 
202                               TALLOC_CTX *mem_ctx,
203                               const char **address_list,
204                               struct interface *ifaces, 
205                               uint16_t nbt_port,
206                               int nbt_timeout,
207                               bool broadcast, bool wins_lookup,
208                               const char **reply_addr)
209 {
210         struct composite_context *c = resolve_name_nbtlist_send(mem_ctx, NULL, 
211                                                                 name, address_list, 
212                                                                 ifaces, nbt_port,
213                                                                 nbt_timeout,
214                                                                 broadcast, wins_lookup);
215         return resolve_name_nbtlist_recv(c, mem_ctx, reply_addr);
216 }
217