r23792: convert Samba4 to GPLv3
[bbaumbach/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
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         struct nbt_name_query *q;
51         int i;
52
53         for (i=0;i<state->num_queries;i++) {
54                 if (req == state->queries[i]) break;
55         }
56
57         if (i == state->num_queries) {
58                 /* not for us?! */
59                 composite_error(c, NT_STATUS_INTERNAL_ERROR);
60                 return;
61         }
62
63         q = &state->io_queries[i];
64
65         c->status = nbt_name_query_recv(req, state, q);
66
67         /* free the network resource directly */
68         talloc_free(state->nbtsock);
69         if (!composite_is_ok(c)) return;
70
71         if (state->io_queries[i].out.num_addrs < 1) {
72                 composite_error(c, NT_STATUS_UNEXPECTED_NETWORK_ERROR);
73                 return;
74         }
75
76         /* favor a local address if possible */
77         state->reply_addr = NULL;
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         composite_done(c);
92 }
93
94 /*
95   nbtlist name resolution method - async send
96  */
97 struct composite_context *resolve_name_nbtlist_send(TALLOC_CTX *mem_ctx,
98                                                     struct event_context *event_ctx,
99                                                     struct nbt_name *name, 
100                                                     const char **address_list,
101                                                     BOOL broadcast,
102                                                     BOOL wins_lookup)
103 {
104         struct composite_context *c;
105         struct nbtlist_state *state;
106         int i;
107
108         c = composite_create(event_ctx, event_ctx);
109         if (c == NULL) return NULL;
110
111         c->event_ctx = talloc_reference(c, event_ctx);
112         if (composite_nomem(c->event_ctx, c)) return c;
113
114         state = talloc(c, struct nbtlist_state);
115         if (composite_nomem(state, c)) return c;
116         c->private_data = state;
117
118         c->status = nbt_name_dup(state, name, &state->name);
119         if (!composite_is_ok(c)) return c;
120
121         state->name.name = strupper_talloc(state, state->name.name);
122         if (composite_nomem(state->name.name, c)) return c;
123         if (state->name.scope) {
124                 state->name.scope = strupper_talloc(state, state->name.scope);
125                 if (composite_nomem(state->name.scope, c)) return c;
126         }
127
128         /*
129          * we can't push long names on the wire,
130          * so bail out here to give a useful error message
131          */
132         if (strlen(state->name.name) > 15) {
133                 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
134                 return c;
135         }
136
137         state->nbtsock = nbt_name_socket_init(state, event_ctx);
138         if (composite_nomem(state->nbtsock, c)) return c;
139
140         /* count the address_list size */
141         for (i=0;address_list[i];i++) /* noop */ ;
142
143         state->num_queries = i;
144         state->io_queries = talloc_array(state, struct nbt_name_query, state->num_queries);
145         if (composite_nomem(state->io_queries, c)) return c;
146
147         state->queries = talloc_array(state, struct nbt_name_request *, state->num_queries);
148         if (composite_nomem(state->queries, c)) return c;
149
150         for (i=0;i<state->num_queries;i++) {
151                 state->io_queries[i].in.name        = state->name;
152                 state->io_queries[i].in.dest_addr   = talloc_strdup(state->io_queries, address_list[i]);
153                 if (composite_nomem(state->io_queries[i].in.dest_addr, c)) return c;
154
155                 state->io_queries[i].in.broadcast   = broadcast;
156                 state->io_queries[i].in.wins_lookup = wins_lookup;
157                 state->io_queries[i].in.timeout     = lp_parm_int(-1, "nbt", "timeout", 1);
158                 state->io_queries[i].in.retries     = 2;
159
160                 state->queries[i] = nbt_name_query_send(state->nbtsock, &state->io_queries[i]);
161                 if (composite_nomem(state->queries[i], c)) return c;
162
163                 state->queries[i]->async.fn      = nbtlist_handler;
164                 state->queries[i]->async.private = c;
165         }
166
167         return c;
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(mem_ctx, NULL, name, address_list, 
199                                                                 broadcast, wins_lookup);
200         return resolve_name_nbtlist_recv(c, mem_ctx, reply_addr);
201 }
202