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"
28 send a nbt name refresh request
30 struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock,
31 struct nbt_name_refresh *io)
33 struct nbt_name_request *req;
34 struct nbt_name_packet *packet;
35 struct nbt_peer_socket dest;
37 packet = talloc_zero(nbtsock, struct nbt_name_packet);
38 if (packet == NULL) return NULL;
42 packet->operation = NBT_OPCODE_REFRESH;
43 if (io->in.broadcast) {
44 packet->operation |= NBT_FLAG_BROADCAST;
47 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
48 if (packet->questions == NULL) goto failed;
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;
54 packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
55 if (packet->additional == NULL) goto failed;
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);
69 dest.port = lp_nbt_port();
70 dest.addr = io->in.dest_addr;
71 req = nbt_name_request_send(nbtsock, &dest, packet,
72 io->in.timeout, io->in.retries, False);
73 if (req == NULL) goto failed;
84 wait for a refresh reply
86 NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
87 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
90 struct nbt_name_packet *packet;
92 status = nbt_name_request_recv(req);
93 if (!NT_STATUS_IS_OK(status) ||
94 req->num_replies == 0) {
99 packet = req->replies[0].packet;
100 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr);
102 if (packet->ancount != 1 ||
103 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
104 packet->answers[0].rr_class != NBT_QCLASS_IP) {
106 return NT_STATUS_INVALID_NETWORK_RESPONSE;
109 io->out.rcode = packet->operation & NBT_RCODE;
110 io->out.name = packet->answers[0].name;
111 if (packet->answers[0].rdata.netbios.length < 6) {
113 return NT_STATUS_INVALID_NETWORK_RESPONSE;
115 io->out.reply_addr = talloc_steal(mem_ctx,
116 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
117 talloc_steal(mem_ctx, io->out.name.name);
118 talloc_steal(mem_ctx, io->out.name.scope);
126 synchronous name refresh request
128 NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
129 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
131 struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
132 return nbt_name_refresh_recv(req, mem_ctx, io);
138 a wins name refresh with multiple WINS servers and multiple
139 addresses to refresh. Try each WINS server in turn, until we get a
140 reply for each address
142 struct refresh_wins_state {
143 struct nbt_name_socket *nbtsock;
144 struct nbt_name_refresh *io;
145 const char **wins_servers;
146 const char **addresses;
148 struct nbt_name_request *req;
153 state handler for WINS multi-homed multi-server name refresh
155 static void name_refresh_wins_handler(struct nbt_name_request *req)
157 struct composite_context *c = talloc_get_type(req->async.private,
158 struct composite_context);
159 struct refresh_wins_state *state = talloc_get_type(c->private_data,
160 struct refresh_wins_state);
163 status = nbt_name_refresh_recv(state->req, state, state->io);
164 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
165 /* the refresh timed out - try the next WINS server */
166 state->wins_servers++;
167 state->address_idx = 0;
168 if (state->wins_servers[0] == NULL) {
169 c->state = COMPOSITE_STATE_ERROR;
173 state->io->in.dest_addr = state->wins_servers[0];
174 state->io->in.address = state->addresses[0];
175 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
176 if (state->req == NULL) {
177 c->state = COMPOSITE_STATE_ERROR;
178 c->status = NT_STATUS_NO_MEMORY;
180 state->req->async.fn = name_refresh_wins_handler;
181 state->req->async.private = c;
183 } else if (!NT_STATUS_IS_OK(status)) {
184 c->state = COMPOSITE_STATE_ERROR;
187 if (state->io->out.rcode == 0 &&
188 state->addresses[state->address_idx+1] != NULL) {
189 /* refresh our next address */
190 state->io->in.address = state->addresses[++(state->address_idx)];
191 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
192 if (state->req == NULL) {
193 c->state = COMPOSITE_STATE_ERROR;
194 c->status = NT_STATUS_NO_MEMORY;
196 state->req->async.fn = name_refresh_wins_handler;
197 state->req->async.private = c;
200 c->state = COMPOSITE_STATE_DONE;
201 c->status = NT_STATUS_OK;
206 if (c->state >= COMPOSITE_STATE_DONE &&
213 the async send call for a multi-server WINS refresh
215 struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
216 struct nbt_name_refresh_wins *io)
218 struct composite_context *c;
219 struct refresh_wins_state *state;
221 c = talloc_zero(nbtsock, struct composite_context);
222 if (c == NULL) goto failed;
224 state = talloc(c, struct refresh_wins_state);
225 if (state == NULL) goto failed;
227 state->io = talloc(state, struct nbt_name_refresh);
228 if (state->io == NULL) goto failed;
230 state->wins_servers = str_list_copy(state, io->in.wins_servers);
231 if (state->wins_servers == NULL ||
232 state->wins_servers[0] == NULL) goto failed;
234 state->addresses = str_list_copy(state, io->in.addresses);
235 if (state->addresses == NULL ||
236 state->addresses[0] == NULL) goto failed;
238 state->io->in.name = io->in.name;
239 state->io->in.dest_addr = state->wins_servers[0];
240 state->io->in.address = io->in.addresses[0];
241 state->io->in.nb_flags = io->in.nb_flags;
242 state->io->in.broadcast = False;
243 state->io->in.ttl = io->in.ttl;
244 state->io->in.timeout = 2;
245 state->io->in.retries = 2;
247 state->nbtsock = nbtsock;
248 state->address_idx = 0;
250 state->req = nbt_name_refresh_send(nbtsock, state->io);
251 if (state->req == NULL) goto failed;
253 state->req->async.fn = name_refresh_wins_handler;
254 state->req->async.private = c;
256 c->private_data = state;
257 c->state = COMPOSITE_STATE_IN_PROGRESS;
258 c->event_ctx = nbtsock->event_ctx;
268 multi-homed WINS name refresh - recv side
270 NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
271 struct nbt_name_refresh_wins *io)
274 status = composite_wait(c);
275 if (NT_STATUS_IS_OK(status)) {
276 struct refresh_wins_state *state =
277 talloc_get_type(c->private_data, struct refresh_wins_state);
278 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
279 io->out.rcode = state->io->out.rcode;
286 multi-homed WINS refresh - sync interface
288 NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
290 struct nbt_name_refresh_wins *io)
292 struct composite_context *c = nbt_name_refresh_wins_send(nbtsock, io);
293 return nbt_name_refresh_wins_recv(c, mem_ctx, io);