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