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