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