2 Unix SMB/CIFS implementation.
4 send out a name refresh request
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "libcli/nbt/libnbt.h"
25 #include "libcli/composite/composite.h"
26 #include "lib/socket/socket.h"
29 send a nbt name refresh request
31 struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock,
32 struct nbt_name_refresh *io)
34 struct nbt_name_request *req;
35 struct nbt_name_packet *packet;
36 struct socket_address *dest;
38 packet = talloc_zero(nbtsock, struct nbt_name_packet);
39 if (packet == NULL) return NULL;
43 packet->operation = NBT_OPCODE_REFRESH;
44 if (io->in.broadcast) {
45 packet->operation |= NBT_FLAG_BROADCAST;
48 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
49 if (packet->questions == NULL) goto failed;
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;
55 packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
56 if (packet->additional == NULL) goto failed;
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);
70 dest = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name,
71 io->in.dest_addr, lp_nbt_port());
72 if (dest == NULL) goto failed;
73 req = nbt_name_request_send(nbtsock, dest, packet,
74 io->in.timeout, io->in.retries, False);
75 if (req == NULL) goto failed;
86 wait for a refresh reply
88 NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
89 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
92 struct nbt_name_packet *packet;
94 status = nbt_name_request_recv(req);
95 if (!NT_STATUS_IS_OK(status) ||
96 req->num_replies == 0) {
101 packet = req->replies[0].packet;
102 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
104 if (packet->ancount != 1 ||
105 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
106 packet->answers[0].rr_class != NBT_QCLASS_IP) {
108 return NT_STATUS_INVALID_NETWORK_RESPONSE;
111 io->out.rcode = packet->operation & NBT_RCODE;
112 io->out.name = packet->answers[0].name;
113 if (packet->answers[0].rdata.netbios.length < 6) {
115 return NT_STATUS_INVALID_NETWORK_RESPONSE;
117 io->out.reply_addr = talloc_steal(mem_ctx,
118 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
119 talloc_steal(mem_ctx, io->out.name.name);
120 talloc_steal(mem_ctx, io->out.name.scope);
128 synchronous name refresh request
130 NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
131 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
133 struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
134 return nbt_name_refresh_recv(req, mem_ctx, io);
140 a wins name refresh with multiple WINS servers and multiple
141 addresses to refresh. Try each WINS server in turn, until we get a
142 reply for each address
144 struct refresh_wins_state {
145 struct nbt_name_socket *nbtsock;
146 struct nbt_name_refresh *io;
147 const char **wins_servers;
148 const char **addresses;
150 struct nbt_name_request *req;
155 state handler for WINS multi-homed multi-server name refresh
157 static void name_refresh_wins_handler(struct nbt_name_request *req)
159 struct composite_context *c = talloc_get_type(req->async.private,
160 struct composite_context);
161 struct refresh_wins_state *state = talloc_get_type(c->private_data,
162 struct refresh_wins_state);
165 status = nbt_name_refresh_recv(state->req, state, state->io);
166 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
167 /* the refresh timed out - try the next WINS server */
168 state->wins_servers++;
169 state->address_idx = 0;
170 if (state->wins_servers[0] == NULL) {
171 c->state = COMPOSITE_STATE_ERROR;
175 state->io->in.dest_addr = state->wins_servers[0];
176 state->io->in.address = state->addresses[0];
177 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
178 if (state->req == NULL) {
179 c->state = COMPOSITE_STATE_ERROR;
180 c->status = NT_STATUS_NO_MEMORY;
182 state->req->async.fn = name_refresh_wins_handler;
183 state->req->async.private = c;
185 } else if (!NT_STATUS_IS_OK(status)) {
186 c->state = COMPOSITE_STATE_ERROR;
189 if (state->io->out.rcode == 0 &&
190 state->addresses[state->address_idx+1] != NULL) {
191 /* refresh our next address */
192 state->io->in.address = state->addresses[++(state->address_idx)];
193 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
194 if (state->req == NULL) {
195 c->state = COMPOSITE_STATE_ERROR;
196 c->status = NT_STATUS_NO_MEMORY;
198 state->req->async.fn = name_refresh_wins_handler;
199 state->req->async.private = c;
202 c->state = COMPOSITE_STATE_DONE;
203 c->status = NT_STATUS_OK;
208 if (c->state >= COMPOSITE_STATE_DONE &&
215 the async send call for a multi-server WINS refresh
217 struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
218 struct nbt_name_refresh_wins *io)
220 struct composite_context *c;
221 struct refresh_wins_state *state;
223 c = talloc_zero(nbtsock, struct composite_context);
224 if (c == NULL) goto failed;
226 state = talloc(c, struct refresh_wins_state);
227 if (state == NULL) goto failed;
229 state->io = talloc(state, struct nbt_name_refresh);
230 if (state->io == NULL) goto failed;
232 state->wins_servers = str_list_copy(state, io->in.wins_servers);
233 if (state->wins_servers == NULL ||
234 state->wins_servers[0] == NULL) goto failed;
236 state->addresses = str_list_copy(state, io->in.addresses);
237 if (state->addresses == NULL ||
238 state->addresses[0] == NULL) goto failed;
240 state->io->in.name = io->in.name;
241 state->io->in.dest_addr = state->wins_servers[0];
242 state->io->in.address = io->in.addresses[0];
243 state->io->in.nb_flags = io->in.nb_flags;
244 state->io->in.broadcast = False;
245 state->io->in.ttl = io->in.ttl;
246 state->io->in.timeout = 2;
247 state->io->in.retries = 2;
249 state->nbtsock = nbtsock;
250 state->address_idx = 0;
252 state->req = nbt_name_refresh_send(nbtsock, state->io);
253 if (state->req == NULL) goto failed;
255 state->req->async.fn = name_refresh_wins_handler;
256 state->req->async.private = c;
258 c->private_data = state;
259 c->state = COMPOSITE_STATE_IN_PROGRESS;
260 c->event_ctx = nbtsock->event_ctx;
270 multi-homed WINS name refresh - recv side
272 NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
273 struct nbt_name_refresh_wins *io)
276 status = composite_wait(c);
277 if (NT_STATUS_IS_OK(status)) {
278 struct refresh_wins_state *state =
279 talloc_get_type(c->private_data, struct refresh_wins_state);
280 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
281 io->out.rcode = state->io->out.rcode;
288 multi-homed WINS refresh - sync interface
290 NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
292 struct nbt_name_refresh_wins *io)
294 struct composite_context *c = nbt_name_refresh_wins_send(nbtsock, io);
295 return nbt_name_refresh_wins_recv(c, mem_ctx, io);