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