r10997: r11980@SERNOX (orig r10037): metze | 2005-09-05 14:21:40 +0200
[gd/samba-autobuild/.git] / source4 / libcli / nbt / namerefresh.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    send out a name refresh request
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 #include "includes.h"
24 #include "libcli/nbt/libnbt.h"
25 #include "libcli/composite/composite.h"
26
27 /*
28   send a nbt name refresh request
29 */
30 struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock,
31                                                struct nbt_name_refresh *io)
32 {
33         struct nbt_name_request *req;
34         struct nbt_name_packet *packet;
35         struct nbt_peer_socket dest;
36
37         packet = talloc_zero(nbtsock, struct nbt_name_packet);
38         if (packet == NULL) return NULL;
39
40         packet->qdcount = 1;
41         packet->arcount = 1;
42         packet->operation = NBT_OPCODE_REFRESH;
43         if (io->in.broadcast) {
44                 packet->operation |= NBT_FLAG_BROADCAST;
45         }
46
47         packet->questions = talloc_array(packet, struct nbt_name_question, 1);
48         if (packet->questions == NULL) goto failed;
49
50         packet->questions[0].name           = io->in.name;
51         packet->questions[0].question_type  = NBT_QTYPE_NETBIOS;
52         packet->questions[0].question_class = NBT_QCLASS_IP;
53
54         packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
55         if (packet->additional == NULL) goto failed;
56
57         packet->additional[0].name                   = io->in.name;
58         packet->additional[0].rr_type                = NBT_QTYPE_NETBIOS;
59         packet->additional[0].rr_class               = NBT_QCLASS_IP;
60         packet->additional[0].ttl                    = io->in.ttl;
61         packet->additional[0].rdata.netbios.length   = 6;
62         packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
63                                                                      struct nbt_rdata_address, 1);
64         if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
65         packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
66         packet->additional[0].rdata.netbios.addresses[0].ipaddr = 
67                 talloc_strdup(packet->additional, io->in.address);
68
69         dest.port = lp_nbt_port();
70         dest.addr = io->in.dest_addr;
71         req = nbt_name_request_send(nbtsock, &dest, packet,
72                                     io->in.timeout, io->in.retries, False);
73         if (req == NULL) goto failed;
74
75         talloc_free(packet);
76         return req;
77
78 failed:
79         talloc_free(packet);
80         return NULL;    
81 }
82
83 /*
84   wait for a refresh reply
85 */
86 NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req, 
87                                TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
88 {
89         NTSTATUS status;
90         struct nbt_name_packet *packet;
91
92         status = nbt_name_request_recv(req);
93         if (!NT_STATUS_IS_OK(status) ||
94             req->num_replies == 0) {
95                 talloc_free(req);
96                 return status;
97         }
98         
99         packet = req->replies[0].packet;
100         io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr);
101
102         if (packet->ancount != 1 ||
103             packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
104             packet->answers[0].rr_class != NBT_QCLASS_IP) {
105                 talloc_free(req);
106                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
107         }
108
109         io->out.rcode = packet->operation & NBT_RCODE;
110         io->out.name = packet->answers[0].name;
111         if (packet->answers[0].rdata.netbios.length < 6) {
112                 talloc_free(req);
113                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
114         }
115         io->out.reply_addr = talloc_steal(mem_ctx, 
116                                           packet->answers[0].rdata.netbios.addresses[0].ipaddr);
117         talloc_steal(mem_ctx, io->out.name.name);
118         talloc_steal(mem_ctx, io->out.name.scope);
119
120         talloc_free(req);
121
122         return NT_STATUS_OK;
123 }
124
125 /*
126   synchronous name refresh request
127 */
128 NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock, 
129                            TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
130 {
131         struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
132         return nbt_name_refresh_recv(req, mem_ctx, io);
133 }
134
135
136
137 /*
138   a wins name refresh with multiple WINS servers and multiple
139   addresses to refresh. Try each WINS server in turn, until we get a
140   reply for each address
141 */
142 struct refresh_wins_state {
143         struct nbt_name_socket *nbtsock;
144         struct nbt_name_refresh *io;
145         const char **wins_servers;
146         const char **addresses;
147         int address_idx;
148         struct nbt_name_request *req;
149 };
150
151
152 /*
153   state handler for WINS multi-homed multi-server name refresh
154 */
155 static void name_refresh_wins_handler(struct nbt_name_request *req)
156 {
157         struct composite_context *c = talloc_get_type(req->async.private, 
158                                                       struct composite_context);
159         struct refresh_wins_state *state = talloc_get_type(c->private_data, 
160                                                             struct refresh_wins_state);
161         NTSTATUS status;
162
163         status = nbt_name_refresh_recv(state->req, state, state->io);
164         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
165                 /* the refresh timed out - try the next WINS server */
166                 state->wins_servers++;
167                 state->address_idx = 0;
168                 if (state->wins_servers[0] == NULL) {
169                         c->state = COMPOSITE_STATE_ERROR;
170                         c->status = status;
171                         goto done;
172                 }
173                 state->io->in.dest_addr = state->wins_servers[0];
174                 state->io->in.address   = state->addresses[0];
175                 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
176                 if (state->req == NULL) {
177                         c->state = COMPOSITE_STATE_ERROR;
178                         c->status = NT_STATUS_NO_MEMORY;
179                 } else {
180                         state->req->async.fn      = name_refresh_wins_handler;
181                         state->req->async.private = c;
182                 }
183         } else if (!NT_STATUS_IS_OK(status)) {
184                 c->state = COMPOSITE_STATE_ERROR;
185                 c->status = status;
186         } else {
187                 if (state->io->out.rcode == 0 &&
188                     state->addresses[state->address_idx+1] != NULL) {
189                         /* refresh our next address */
190                         state->io->in.address = state->addresses[++(state->address_idx)];
191                         state->req = nbt_name_refresh_send(state->nbtsock, state->io);
192                         if (state->req == NULL) {
193                                 c->state = COMPOSITE_STATE_ERROR;
194                                 c->status = NT_STATUS_NO_MEMORY;
195                         } else {
196                                 state->req->async.fn      = name_refresh_wins_handler;
197                                 state->req->async.private = c;
198                         }
199                 } else {
200                         c->state = COMPOSITE_STATE_DONE;
201                         c->status = NT_STATUS_OK;
202                 }
203         }
204
205 done:
206         if (c->state >= COMPOSITE_STATE_DONE &&
207             c->async.fn) {
208                 c->async.fn(c);
209         }
210 }
211
212 /*
213   the async send call for a multi-server WINS refresh
214 */
215 struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
216                                                       struct nbt_name_refresh_wins *io)
217 {
218         struct composite_context *c;
219         struct refresh_wins_state *state;
220
221         c = talloc_zero(nbtsock, struct composite_context);
222         if (c == NULL) goto failed;
223
224         state = talloc(c, struct refresh_wins_state);
225         if (state == NULL) goto failed;
226
227         state->io = talloc(state, struct nbt_name_refresh);
228         if (state->io == NULL) goto failed;
229
230         state->wins_servers = str_list_copy(state, io->in.wins_servers);
231         if (state->wins_servers == NULL || 
232             state->wins_servers[0] == NULL) goto failed;
233
234         state->addresses = str_list_copy(state, io->in.addresses);
235         if (state->addresses == NULL || 
236             state->addresses[0] == NULL) goto failed;
237
238         state->io->in.name            = io->in.name;
239         state->io->in.dest_addr       = state->wins_servers[0];
240         state->io->in.address         = io->in.addresses[0];
241         state->io->in.nb_flags        = io->in.nb_flags;
242         state->io->in.broadcast       = False;
243         state->io->in.ttl             = io->in.ttl;
244         state->io->in.timeout         = 2;
245         state->io->in.retries         = 2;
246
247         state->nbtsock     = nbtsock;
248         state->address_idx = 0;
249
250         state->req = nbt_name_refresh_send(nbtsock, state->io);
251         if (state->req == NULL) goto failed;
252
253         state->req->async.fn      = name_refresh_wins_handler;
254         state->req->async.private = c;
255
256         c->private_data = state;
257         c->state        = COMPOSITE_STATE_IN_PROGRESS;
258         c->event_ctx    = nbtsock->event_ctx;
259
260         return c;
261
262 failed:
263         talloc_free(c);
264         return NULL;
265 }
266
267 /*
268   multi-homed WINS name refresh - recv side
269 */
270 NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
271                                      struct nbt_name_refresh_wins *io)
272 {
273         NTSTATUS status;
274         status = composite_wait(c);
275         if (NT_STATUS_IS_OK(status)) {
276                 struct refresh_wins_state *state = 
277                         talloc_get_type(c->private_data, struct refresh_wins_state);
278                 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
279                 io->out.rcode = state->io->out.rcode;
280         }
281         talloc_free(c);
282         return status;
283 }
284
285 /*
286   multi-homed WINS refresh - sync interface
287 */
288 NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
289                                 TALLOC_CTX *mem_ctx,
290                                 struct nbt_name_refresh_wins *io)
291 {
292         struct composite_context *c = nbt_name_refresh_wins_send(nbtsock, io);
293         return nbt_name_refresh_wins_recv(c, mem_ctx, io);
294 }