s4-nbt: move libcli/nbt up one level.
[Samba/gebeck_regimport.git] / libcli / nbt / namerefresh.c
blob37bf0fc8f121a4af0e75ae3b0c0105953a809c0a
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "../libcli/nbt/libnbt.h"
24 #include "../libcli/nbt/nbt_proto.h"
25 #include "libcli/composite/composite.h"
26 #include "lib/socket/socket.h"
27 #include "param/param.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;
37 struct socket_address *dest;
39 packet = talloc_zero(nbtsock, struct nbt_name_packet);
40 if (packet == NULL) return NULL;
42 packet->qdcount = 1;
43 packet->arcount = 1;
44 packet->operation = NBT_OPCODE_REFRESH;
45 if (io->in.broadcast) {
46 packet->operation |= NBT_FLAG_BROADCAST;
49 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
50 if (packet->questions == NULL) goto failed;
52 packet->questions[0].name = io->in.name;
53 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
54 packet->questions[0].question_class = NBT_QCLASS_IP;
56 packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
57 if (packet->additional == NULL) goto failed;
59 packet->additional[0].name = io->in.name;
60 packet->additional[0].rr_type = NBT_QTYPE_NETBIOS;
61 packet->additional[0].rr_class = NBT_QCLASS_IP;
62 packet->additional[0].ttl = io->in.ttl;
63 packet->additional[0].rdata.netbios.length = 6;
64 packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
65 struct nbt_rdata_address, 1);
66 if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
67 packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
68 packet->additional[0].rdata.netbios.addresses[0].ipaddr =
69 talloc_strdup(packet->additional, io->in.address);
71 dest = socket_address_from_strings(nbtsock,
72 nbtsock->sock->backend_name,
73 io->in.dest_addr, io->in.dest_port);
74 if (dest == NULL) goto failed;
75 req = nbt_name_request_send(nbtsock, dest, packet,
76 io->in.timeout, io->in.retries, false);
77 if (req == NULL) goto failed;
79 talloc_free(packet);
80 return req;
82 failed:
83 talloc_free(packet);
84 return NULL;
88 wait for a refresh reply
90 _PUBLIC_ NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
91 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
93 NTSTATUS status;
94 struct nbt_name_packet *packet;
96 status = nbt_name_request_recv(req);
97 if (!NT_STATUS_IS_OK(status) ||
98 req->num_replies == 0) {
99 talloc_free(req);
100 return status;
103 packet = req->replies[0].packet;
104 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
106 if (packet->ancount != 1 ||
107 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
108 packet->answers[0].rr_class != NBT_QCLASS_IP) {
109 talloc_free(req);
110 return NT_STATUS_INVALID_NETWORK_RESPONSE;
113 io->out.rcode = packet->operation & NBT_RCODE;
114 io->out.name = packet->answers[0].name;
115 if (packet->answers[0].rdata.netbios.length < 6) {
116 talloc_free(req);
117 return NT_STATUS_INVALID_NETWORK_RESPONSE;
119 io->out.reply_addr = talloc_steal(mem_ctx,
120 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
121 talloc_steal(mem_ctx, io->out.name.name);
122 talloc_steal(mem_ctx, io->out.name.scope);
124 talloc_free(req);
126 return NT_STATUS_OK;
130 synchronous name refresh request
132 _PUBLIC_ NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
133 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
135 struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
136 return nbt_name_refresh_recv(req, mem_ctx, io);
142 a wins name refresh with multiple WINS servers and multiple
143 addresses to refresh. Try each WINS server in turn, until we get a
144 reply for each address
146 struct refresh_wins_state {
147 struct nbt_name_socket *nbtsock;
148 struct nbt_name_refresh *io;
149 const char **wins_servers;
150 uint16_t wins_port;
151 const char **addresses;
152 int address_idx;
153 struct nbt_name_request *req;
158 state handler for WINS multi-homed multi-server name refresh
160 static void name_refresh_wins_handler(struct nbt_name_request *req)
162 struct composite_context *c = talloc_get_type(req->async.private_data,
163 struct composite_context);
164 struct refresh_wins_state *state = talloc_get_type(c->private_data,
165 struct refresh_wins_state);
166 NTSTATUS status;
168 status = nbt_name_refresh_recv(state->req, state, state->io);
169 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
170 /* the refresh timed out - try the next WINS server */
171 state->wins_servers++;
172 state->address_idx = 0;
173 if (state->wins_servers[0] == NULL) {
174 c->state = COMPOSITE_STATE_ERROR;
175 c->status = status;
176 goto done;
178 state->io->in.dest_addr = state->wins_servers[0];
179 state->io->in.dest_port = state->wins_port;
180 state->io->in.address = state->addresses[0];
181 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
182 if (state->req == NULL) {
183 c->state = COMPOSITE_STATE_ERROR;
184 c->status = NT_STATUS_NO_MEMORY;
185 } else {
186 state->req->async.fn = name_refresh_wins_handler;
187 state->req->async.private_data = c;
189 } else if (!NT_STATUS_IS_OK(status)) {
190 c->state = COMPOSITE_STATE_ERROR;
191 c->status = status;
192 } else {
193 if (state->io->out.rcode == 0 &&
194 state->addresses[state->address_idx+1] != NULL) {
195 /* refresh our next address */
196 state->io->in.address = state->addresses[++(state->address_idx)];
197 state->req = nbt_name_refresh_send(state->nbtsock, state->io);
198 if (state->req == NULL) {
199 c->state = COMPOSITE_STATE_ERROR;
200 c->status = NT_STATUS_NO_MEMORY;
201 } else {
202 state->req->async.fn = name_refresh_wins_handler;
203 state->req->async.private_data = c;
205 } else {
206 c->state = COMPOSITE_STATE_DONE;
207 c->status = NT_STATUS_OK;
211 done:
212 if (c->state >= COMPOSITE_STATE_DONE &&
213 c->async.fn) {
214 c->async.fn(c);
219 the async send call for a multi-server WINS refresh
221 _PUBLIC_ struct composite_context *nbt_name_refresh_wins_send(struct nbt_name_socket *nbtsock,
222 struct nbt_name_refresh_wins *io)
224 struct composite_context *c;
225 struct refresh_wins_state *state;
227 c = talloc_zero(nbtsock, struct composite_context);
228 if (c == NULL) goto failed;
230 state = talloc(c, struct refresh_wins_state);
231 if (state == NULL) goto failed;
233 state->io = talloc(state, struct nbt_name_refresh);
234 if (state->io == NULL) goto failed;
236 state->wins_port = io->in.wins_port;
237 state->wins_servers = str_list_copy(state, io->in.wins_servers);
238 if (state->wins_servers == NULL ||
239 state->wins_servers[0] == NULL) goto failed;
241 state->addresses = str_list_copy(state, io->in.addresses);
242 if (state->addresses == NULL ||
243 state->addresses[0] == NULL) goto failed;
245 state->io->in.name = io->in.name;
246 state->io->in.dest_addr = state->wins_servers[0];
247 state->io->in.dest_port = state->wins_port;
248 state->io->in.address = io->in.addresses[0];
249 state->io->in.nb_flags = io->in.nb_flags;
250 state->io->in.broadcast = false;
251 state->io->in.ttl = io->in.ttl;
252 state->io->in.timeout = 2;
253 state->io->in.retries = 2;
255 state->nbtsock = nbtsock;
256 state->address_idx = 0;
258 state->req = nbt_name_refresh_send(nbtsock, state->io);
259 if (state->req == NULL) goto failed;
261 state->req->async.fn = name_refresh_wins_handler;
262 state->req->async.private_data = c;
264 c->private_data = state;
265 c->state = COMPOSITE_STATE_IN_PROGRESS;
266 c->event_ctx = nbtsock->event_ctx;
268 return c;
270 failed:
271 talloc_free(c);
272 return NULL;
276 multi-homed WINS name refresh - recv side
278 _PUBLIC_ NTSTATUS nbt_name_refresh_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
279 struct nbt_name_refresh_wins *io)
281 NTSTATUS status;
282 status = composite_wait(c);
283 if (NT_STATUS_IS_OK(status)) {
284 struct refresh_wins_state *state =
285 talloc_get_type(c->private_data, struct refresh_wins_state);
286 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
287 io->out.rcode = state->io->out.rcode;
289 talloc_free(c);
290 return status;
294 multi-homed WINS refresh - sync interface
296 _PUBLIC_ NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
297 TALLOC_CTX *mem_ctx,
298 struct nbt_name_refresh_wins *io)
300 struct composite_context *c = nbt_name_refresh_wins_send(nbtsock, io);
301 return nbt_name_refresh_wins_recv(c, mem_ctx, io);