r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[samba.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24   TODO: we should lower the timeout, and add retries for each name
25 */
26
27 #include "includes.h"
28 #include "libcli/composite/composite.h"
29 #include "system/network.h"
30 #include "netif/netif.h"
31 #include "librpc/gen_ndr/ndr_nbt.h"
32
33 struct nbtlist_state {
34         struct nbt_name name;
35         struct nbt_name_socket *nbtsock;
36         int num_queries;
37         struct nbt_name_request **queries;
38         struct nbt_name_query *io_queries;
39         const char *reply_addr;
40 };
41
42 /*
43   handle events during nbtlist name resolution
44 */
45 static void nbtlist_handler(struct nbt_name_request *req)
46 {
47         struct composite_context *c = talloc_get_type(req->async.private, 
48                                                       struct composite_context);
49         struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
50         int i;
51
52         for (i=0;i<state->num_queries;i++) {
53                 if (req == state->queries[i]) break;
54         }
55
56         if (i == state->num_queries) {
57                 /* not for us?! */
58                 c->status = NT_STATUS_INTERNAL_ERROR;
59                 c->state = COMPOSITE_STATE_ERROR;               
60                 goto done;
61         }
62
63         c->status = nbt_name_query_recv(req, state, &state->io_queries[i]);
64         if (!NT_STATUS_IS_OK(c->status)) {
65                 c->state = COMPOSITE_STATE_ERROR;
66
67         } else {
68                 if (state->io_queries[i].out.num_addrs < 1) {
69                         c->state = COMPOSITE_STATE_ERROR;
70                         c->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
71
72                 } else {
73                         struct nbt_name_query *q = &state->io_queries[i];
74                         c->state = COMPOSITE_STATE_DONE;
75                         /* favor a local address if possible */
76                         state->reply_addr = NULL;
77
78                         for (i=0;i<q->out.num_addrs;i++) {
79                                 if (iface_is_local(q->out.reply_addrs[i])) {
80                                         state->reply_addr = talloc_steal(state, 
81                                                                          q->out.reply_addrs[i]);
82                                         break;
83                                 }
84                         }
85
86                         if (state->reply_addr == NULL) {
87                                 state->reply_addr = talloc_steal(state, 
88                                                                  q->out.reply_addrs[0]);
89                         }
90                 }
91         }
92
93 done:
94         talloc_free(state->nbtsock);
95         if (c->async.fn) {
96                 c->async.fn(c);
97         }
98 }
99
100 /*
101   nbtlist name resolution method - async send
102  */
103 struct composite_context *resolve_name_nbtlist_send(struct nbt_name *name, 
104                                                    struct event_context *event_ctx,
105                                                    const char **address_list,
106                                                    BOOL broadcast,
107                                                    BOOL wins_lookup)
108 {
109         struct composite_context *c;
110         struct nbtlist_state *state;
111         int i;
112         NTSTATUS status;
113
114         c = talloc_zero(NULL, struct composite_context);
115         if (c == NULL) goto failed;
116
117         state = talloc(c, struct nbtlist_state);
118         if (state == NULL) goto failed;
119
120         status = nbt_name_dup(state, name, &state->name);
121         if (!NT_STATUS_IS_OK(status)) goto failed;
122
123         state->name.name = strupper_talloc(state, state->name.name);
124         if (state->name.name == NULL) goto failed;
125         if (state->name.scope) {
126                 state->name.scope = strupper_talloc(state, state->name.scope);
127                 if (state->name.scope == NULL) goto failed;
128         }
129
130         state->nbtsock = nbt_name_socket_init(state, event_ctx);
131         if (state->nbtsock == NULL) goto failed;
132
133         /* count the address_list size */
134         for (i=0;address_list[i];i++) /* noop */ ;
135
136         state->num_queries = i;
137         state->io_queries = talloc_array(state, struct nbt_name_query, state->num_queries);
138         if (!state->io_queries) goto failed;
139
140         state->queries = talloc_array(state, struct nbt_name_request *, state->num_queries);
141         if (!state->queries) goto failed;
142
143         for (i=0;i<state->num_queries;i++) {
144                 state->io_queries[i].in.name        = state->name;
145                 state->io_queries[i].in.dest_addr   = talloc_strdup(state->io_queries, address_list[i]);
146                 if (!state->io_queries[i].in.dest_addr) goto failed;
147
148                 state->io_queries[i].in.broadcast   = broadcast;
149                 state->io_queries[i].in.wins_lookup = wins_lookup;
150                 state->io_queries[i].in.timeout     = lp_parm_int(-1, "nbt", "timeout", 1);
151                 state->io_queries[i].in.retries     = 2;
152
153                 state->queries[i] = nbt_name_query_send(state->nbtsock, &state->io_queries[i]);
154                 if (!state->queries[i]) goto failed;
155
156                 state->queries[i]->async.fn      = nbtlist_handler;
157                 state->queries[i]->async.private = c;
158         }
159
160         c->state = COMPOSITE_STATE_IN_PROGRESS;
161         c->private_data = state;
162         c->event_ctx = talloc_reference(c, state->nbtsock->event_ctx);
163
164         return c;
165
166 failed:
167         talloc_free(c);
168         return NULL;
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(name, NULL, address_list, 
200                                                                broadcast, wins_lookup);
201         return resolve_name_nbtlist_recv(c, mem_ctx, reply_addr);
202 }
203