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"
27 #include "system/network.h"
30 send a nbt name refresh request
32 struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock,
33 struct nbt_name_refresh *io)
35 struct nbt_name_request *req;
36 struct nbt_name_packet *packet;
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 req = nbt_name_request_send(nbtsock, io->in.dest_addr, lp_nbt_port(), packet,
71 io->in.timeout, io->in.retries, False);
72 if (req == NULL) goto failed;
83 wait for a refresh reply
85 NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
86 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
89 struct nbt_name_packet *packet;
91 status = nbt_name_request_recv(req);
92 if (!NT_STATUS_IS_OK(status) ||
93 req->num_replies == 0) {
98 packet = req->replies[0].packet;
99 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
101 if (packet->ancount != 1 ||
102 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
103 packet->answers[0].rr_class != NBT_QCLASS_IP) {
105 return NT_STATUS_INVALID_NETWORK_RESPONSE;
108 io->out.rcode = packet->operation & NBT_RCODE;
109 io->out.name = packet->answers[0].name;
110 if (packet->answers[0].rdata.netbios.length < 6) {
112 return NT_STATUS_INVALID_NETWORK_RESPONSE;
114 io->out.reply_addr = talloc_steal(mem_ctx,
115 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
116 talloc_steal(mem_ctx, io->out.name.name);
117 talloc_steal(mem_ctx, io->out.name.scope);
125 synchronous name refresh request
127 NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
128 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
130 struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
131 return nbt_name_refresh_recv(req, mem_ctx, io);
137 a wins name refresh with multiple WINS servers and multiple
138 addresses to refresh. Try each WINS server in turn, until we get a
139 reply for each address
141 struct refresh_wins_state {
142 struct nbt_name_socket *nbtsock;
143 struct nbt_name_refresh *io;
144 const char **wins_servers;
145 const char **addresses;
147 struct nbt_name_request *req;
152 state handler for WINS multi-homed multi-server name refresh
154 static void name_refresh_wins_handler(struct nbt_name_request *req)
156 struct composite_context *c = talloc_get_type(req->async.private,
157 struct composite_context);
158 struct refresh_wins_state *state = talloc_get_type(c->private,
159 struct refresh_wins_state);
162 status = nbt_name_refresh_recv(state->req, state, state->io);
163 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
164 /* the refresh timed out - try the next WINS server */
165 state->wins_servers++;
166 state->address_idx = 0;
167 if (state->wins_servers[0] == NULL) {
168 c->state = SMBCLI_REQUEST_ERROR;
172 state->io->in.dest_addr = state->wins_servers[0];
173 state->io->in.address = state->addresses[0];
174 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
175 if (state->req == NULL) {
176 c->state = SMBCLI_REQUEST_ERROR;
177 c->status = NT_STATUS_NO_MEMORY;
179 state->req->async.fn = name_refresh_wins_handler;
180 state->req->async.private = c;
182 } else if (!NT_STATUS_IS_OK(status)) {
183 c->state = SMBCLI_REQUEST_ERROR;
186 if (state->io->out.rcode == 0 &&
187 state->addresses[state->address_idx+1] != NULL) {
188 /* refresh our next address */
189 state->io->in.address = state->addresses[++(state->address_idx)];
190 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
191 if (state->req == NULL) {
192 c->state = SMBCLI_REQUEST_ERROR;
193 c->status = NT_STATUS_NO_MEMORY;
195 state->req->async.fn = name_refresh_wins_handler;
196 state->req->async.private = c;
199 c->state = SMBCLI_REQUEST_DONE;
200 c->status = NT_STATUS_OK;
205 if (c->state >= SMBCLI_REQUEST_DONE &&
212 the async send call for a multi-server WINS refresh
214 struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
215 struct nbt_name_refresh_wins *io)
217 struct composite_context *c;
218 struct refresh_wins_state *state;
220 c = talloc_zero(nbtsock, struct composite_context);
221 if (c == NULL) goto failed;
223 state = talloc(c, struct refresh_wins_state);
224 if (state == NULL) goto failed;
226 state->io = talloc(state, struct nbt_name_refresh);
227 if (state->io == NULL) goto failed;
229 state->wins_servers = str_list_copy(state, io->in.wins_servers);
230 if (state->wins_servers == NULL ||
231 state->wins_servers[0] == NULL) goto failed;
233 state->addresses = str_list_copy(state, io->in.addresses);
234 if (state->addresses == NULL ||
235 state->addresses[0] == NULL) goto failed;
237 state->io->in.name = io->in.name;
238 state->io->in.dest_addr = state->wins_servers[0];
239 state->io->in.address = io->in.addresses[0];
240 state->io->in.nb_flags = io->in.nb_flags;
241 state->io->in.broadcast = False;
242 state->io->in.ttl = io->in.ttl;
243 state->io->in.timeout = 2;
244 state->io->in.retries = 2;
246 state->nbtsock = nbtsock;
247 state->address_idx = 0;
249 state->req = nbt_name_refresh_send(nbtsock, state->io);
250 if (state->req == NULL) goto failed;
252 state->req->async.fn = name_refresh_wins_handler;
253 state->req->async.private = c;
256 c->state = SMBCLI_REQUEST_SEND;
257 c->event_ctx = nbtsock->event_ctx;
267 multi-homed WINS name refresh - recv side
269 NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
270 struct nbt_name_refresh_wins *io)
273 status = composite_wait(c);
274 if (NT_STATUS_IS_OK(status)) {
275 struct refresh_wins_state *state =
276 talloc_get_type(c->private, struct refresh_wins_state);
277 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
278 io->out.rcode = state->io->out.rcode;
285 multi-homed WINS refresh - sync interface
287 NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
289 struct nbt_name_refresh_wins *io)
291 struct composite_context *c = nbt_name_refresh_wins_send(nbtsock, io);
292 return nbt_name_refresh_wins_recv(c, mem_ctx, io);