f8fe5df9a1bd39e6a10dd34bc23d8366676f0ffc
[jelmer/samba4-debian.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/raw/libcliraw.h"
29 #include "libcli/nbt/libnbt.h"
30 #include "libcli/composite/composite.h"
31
32 struct nbtlist_state {
33         struct nbt_name name;
34         struct nbt_name_socket *nbtsock;
35         int num_queries;
36         struct nbt_name_request **queries;
37         struct nbt_name_query *io_queries;
38         const char *reply_addr;
39 };
40
41 /*
42   handle events during nbtlist name resolution
43 */
44 static void nbtlist_handler(struct nbt_name_request *req)
45 {
46         struct composite_context *c = talloc_get_type(req->async.private, 
47                                                       struct composite_context);
48         struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
49         int i;
50
51         for (i=0;i<state->num_queries;i++) {
52                 if (req == state->queries[i]) break;
53         }
54
55         if (i == state->num_queries) {
56                 /* not for us?! */
57                 c->status = NT_STATUS_INTERNAL_ERROR;
58                 c->state = COMPOSITE_STATE_ERROR;               
59                 goto done;
60         }
61
62         c->status = nbt_name_query_recv(req, state, &state->io_queries[i]);
63         if (!NT_STATUS_IS_OK(c->status)) {
64                 c->state = COMPOSITE_STATE_ERROR;
65
66         } else {
67                 if (state->io_queries[i].out.num_addrs < 1) {
68                         c->state = COMPOSITE_STATE_ERROR;
69                         c->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
70
71                 } else {
72                         struct nbt_name_query *q = &state->io_queries[i];
73                         c->state = COMPOSITE_STATE_DONE;
74                         /* favor a local address if possible */
75                         state->reply_addr = NULL;
76
77                         for (i=0;i<q->out.num_addrs;i++) {
78                                 if (iface_is_local(q->out.reply_addrs[i])) {
79                                         state->reply_addr = talloc_steal(state, 
80                                                                          q->out.reply_addrs[i]);
81                                         break;
82                                 }
83                         }
84
85                         if (state->reply_addr == NULL) {
86                                 state->reply_addr = talloc_steal(state, 
87                                                                  q->out.reply_addrs[0]);
88                         }
89                 }
90         }
91
92 done:
93         talloc_free(state->nbtsock);
94         if (c->async.fn) {
95                 c->async.fn(c);
96         }
97 }
98
99 /*
100   nbtlist name resolution method - async send
101  */
102 struct composite_context *resolve_name_nbtlist_send(struct nbt_name *name, 
103                                                    struct event_context *event_ctx,
104                                                    const char **address_list,
105                                                    BOOL broadcast,
106                                                    BOOL wins_lookup)
107 {
108         struct composite_context *c;
109         struct nbtlist_state *state;
110         int i;
111         NTSTATUS status;
112
113         c = talloc_zero(NULL, struct composite_context);
114         if (c == NULL) goto failed;
115
116         state = talloc(c, struct nbtlist_state);
117         if (state == NULL) goto failed;
118
119         status = nbt_name_dup(state, name, &state->name);
120         if (!NT_STATUS_IS_OK(status)) goto failed;
121
122         state->name.name = strupper_talloc(state, state->name.name);
123         if (state->name.name == NULL) goto failed;
124         if (state->name.scope) {
125                 state->name.scope = strupper_talloc(state, state->name.scope);
126                 if (state->name.scope == NULL) goto failed;
127         }
128
129         state->nbtsock = nbt_name_socket_init(state, event_ctx);
130         if (state->nbtsock == NULL) goto failed;
131
132         /* count the address_list size */
133         for (i=0;address_list[i];i++) /* noop */ ;
134
135         state->num_queries = i;
136         state->io_queries = talloc_array(state, struct nbt_name_query, state->num_queries);
137         if (!state->io_queries) goto failed;
138
139         state->queries = talloc_array(state, struct nbt_name_request *, state->num_queries);
140         if (!state->queries) goto failed;
141
142         for (i=0;i<state->num_queries;i++) {
143                 state->io_queries[i].in.name        = state->name;
144                 state->io_queries[i].in.dest_addr   = talloc_strdup(state->io_queries, address_list[i]);
145                 if (!state->io_queries[i].in.dest_addr) goto failed;
146
147                 state->io_queries[i].in.broadcast   = broadcast;
148                 state->io_queries[i].in.wins_lookup = wins_lookup;
149                 state->io_queries[i].in.timeout     = lp_parm_int(-1, "nbt", "timeout", 1);
150                 state->io_queries[i].in.retries     = 2;
151
152                 state->queries[i] = nbt_name_query_send(state->nbtsock, &state->io_queries[i]);
153                 if (!state->queries[i]) goto failed;
154
155                 state->queries[i]->async.fn      = nbtlist_handler;
156                 state->queries[i]->async.private = c;
157         }
158
159         c->state = COMPOSITE_STATE_IN_PROGRESS;
160         c->private_data = state;
161         c->event_ctx = talloc_reference(c, state->nbtsock->event_ctx);
162
163         return c;
164
165 failed:
166         talloc_free(c);
167         return NULL;
168 }
169
170 /*
171   nbt list of addresses name resolution method - recv side
172  */
173 NTSTATUS resolve_name_nbtlist_recv(struct composite_context *c, 
174                                    TALLOC_CTX *mem_ctx, const char **reply_addr)
175 {
176         NTSTATUS status;
177
178         status = composite_wait(c);
179
180         if (NT_STATUS_IS_OK(status)) {
181                 struct nbtlist_state *state = talloc_get_type(c->private_data, struct nbtlist_state);
182                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
183         }
184
185         talloc_free(c);
186         return status;
187 }
188
189 /*
190   nbt list of addresses name resolution method - sync call
191  */
192 NTSTATUS resolve_name_nbtlist(struct nbt_name *name, 
193                               TALLOC_CTX *mem_ctx,
194                               const char **address_list,
195                               BOOL broadcast, BOOL wins_lookup,
196                               const char **reply_addr)
197 {
198         struct composite_context *c = resolve_name_nbtlist_send(name, NULL, address_list, 
199                                                                broadcast, wins_lookup);
200         return resolve_name_nbtlist_recv(c, mem_ctx, reply_addr);
201 }
202