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/raw/libcliraw.h"
26 #include "libcli/composite/composite.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;
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 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;
82 wait for a refresh reply
84 NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
85 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
88 struct nbt_name_packet *packet;
90 status = nbt_name_request_recv(req);
91 if (!NT_STATUS_IS_OK(status) ||
92 req->num_replies == 0) {
97 packet = req->replies[0].packet;
98 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
100 if (packet->ancount != 1 ||
101 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
102 packet->answers[0].rr_class != NBT_QCLASS_IP) {
104 return NT_STATUS_INVALID_NETWORK_RESPONSE;
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) {
111 return NT_STATUS_INVALID_NETWORK_RESPONSE;
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);
124 synchronous name refresh request
126 NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
127 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
129 struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
130 return nbt_name_refresh_recv(req, mem_ctx, io);
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
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;
146 struct nbt_name_request *req;
151 state handler for WINS multi-homed multi-server name refresh
153 static void name_refresh_wins_handler(struct nbt_name_request *req)
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);
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;
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;
178 state->req->async.fn = name_refresh_wins_handler;
179 state->req->async.private = c;
181 } else if (!NT_STATUS_IS_OK(status)) {
182 c->state = SMBCLI_REQUEST_ERROR;
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;
194 state->req->async.fn = name_refresh_wins_handler;
195 state->req->async.private = c;
198 c->state = SMBCLI_REQUEST_DONE;
199 c->status = NT_STATUS_OK;
204 if (c->state >= SMBCLI_REQUEST_DONE &&
211 the async send call for a multi-server WINS refresh
213 struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
214 struct nbt_name_refresh_wins *io)
216 struct composite_context *c;
217 struct refresh_wins_state *state;
219 c = talloc_zero(nbtsock, struct composite_context);
220 if (c == NULL) goto failed;
222 state = talloc(c, struct refresh_wins_state);
223 if (state == NULL) goto failed;
225 state->io = talloc(state, struct nbt_name_refresh);
226 if (state->io == NULL) goto failed;
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;
232 state->addresses = str_list_copy(state, io->in.addresses);
233 if (state->addresses == NULL ||
234 state->addresses[0] == NULL) goto failed;
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;
245 state->nbtsock = nbtsock;
246 state->address_idx = 0;
248 state->req = nbt_name_refresh_send(nbtsock, state->io);
249 if (state->req == NULL) goto failed;
251 state->req->async.fn = name_refresh_wins_handler;
252 state->req->async.private = c;
255 c->state = SMBCLI_REQUEST_SEND;
256 c->event_ctx = nbtsock->event_ctx;
266 multi-homed WINS name refresh - recv side
268 NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
269 struct nbt_name_refresh_wins *io)
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;
284 multi-homed WINS refresh - sync interface
286 NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
288 struct nbt_name_refresh_wins *io)
290 struct composite_context *c = nbt_name_refresh_wins_send(nbtsock, io);
291 return nbt_name_refresh_wins_recv(c, mem_ctx, io);