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