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