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