2 Unix SMB/CIFS implementation.
4 send out a name registration 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 registration request
30 struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock,
31 struct nbt_name_register *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 if (io->in.multi_homed) {
43 packet->operation = NBT_OPCODE_MULTI_HOME_REG;
45 packet->operation = NBT_OPCODE_REGISTER;
47 if (io->in.broadcast) {
48 packet->operation |= NBT_FLAG_BROADCAST;
50 if (io->in.register_demand) {
51 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
54 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
55 if (packet->questions == NULL) goto failed;
57 packet->questions[0].name = io->in.name;
58 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
59 packet->questions[0].question_class = NBT_QCLASS_IP;
61 packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
62 if (packet->additional == NULL) goto failed;
64 packet->additional[0].name = io->in.name;
65 packet->additional[0].rr_type = NBT_QTYPE_NETBIOS;
66 packet->additional[0].rr_class = NBT_QCLASS_IP;
67 packet->additional[0].ttl = io->in.ttl;
68 packet->additional[0].rdata.netbios.length = 6;
69 packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
70 struct nbt_rdata_address, 1);
71 if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
72 packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
73 packet->additional[0].rdata.netbios.addresses[0].ipaddr =
74 talloc_strdup(packet->additional, io->in.address);
75 if (packet->additional[0].rdata.netbios.addresses[0].ipaddr == NULL) goto failed;
77 dest.port = lp_nbt_port();
78 dest.addr = io->in.dest_addr;
79 req = nbt_name_request_send(nbtsock, &dest, packet,
80 io->in.timeout, io->in.retries, False);
81 if (req == NULL) goto failed;
92 wait for a registration reply
94 NTSTATUS nbt_name_register_recv(struct nbt_name_request *req,
95 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
98 struct nbt_name_packet *packet;
100 status = nbt_name_request_recv(req);
101 if (!NT_STATUS_IS_OK(status) ||
102 req->num_replies == 0) {
107 packet = req->replies[0].packet;
108 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr);
110 if (packet->ancount != 1 ||
111 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
112 packet->answers[0].rr_class != NBT_QCLASS_IP) {
114 return NT_STATUS_INVALID_NETWORK_RESPONSE;
117 io->out.rcode = packet->operation & NBT_RCODE;
118 io->out.name = packet->answers[0].name;
119 if (packet->answers[0].rdata.netbios.length < 6) {
121 return NT_STATUS_INVALID_NETWORK_RESPONSE;
123 io->out.reply_addr = talloc_steal(mem_ctx,
124 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
125 talloc_steal(mem_ctx, io->out.name.name);
126 talloc_steal(mem_ctx, io->out.name.scope);
134 synchronous name registration request
136 NTSTATUS nbt_name_register(struct nbt_name_socket *nbtsock,
137 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
139 struct nbt_name_request *req = nbt_name_register_send(nbtsock, io);
140 return nbt_name_register_recv(req, mem_ctx, io);
145 a 4 step broadcast registration. 3 lots of name registration requests, followed by
146 a name registration demand
148 struct register_bcast_state {
149 struct nbt_name_socket *nbtsock;
150 struct nbt_name_register *io;
151 struct nbt_name_request *req;
156 state handler for 4 stage name registration
158 static void name_register_bcast_handler(struct nbt_name_request *req)
160 struct composite_context *c = talloc_get_type(req->async.private, struct composite_context);
161 struct register_bcast_state *state = talloc_get_type(c->private_data, struct register_bcast_state);
164 status = nbt_name_register_recv(state->req, state, state->io);
165 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
166 if (state->io->in.register_demand == True) {
168 c->state = COMPOSITE_STATE_DONE;
169 c->status = NT_STATUS_OK;
173 /* the registration timed out - good, send the demand */
174 state->io->in.register_demand = True;
175 state->io->in.retries = 0;
176 state->req = nbt_name_register_send(state->nbtsock, state->io);
177 if (state->req == NULL) {
178 c->state = COMPOSITE_STATE_ERROR;
179 c->status = NT_STATUS_NO_MEMORY;
181 state->req->async.fn = name_register_bcast_handler;
182 state->req->async.private = c;
184 } else if (!NT_STATUS_IS_OK(status)) {
185 c->state = COMPOSITE_STATE_ERROR;
188 c->state = COMPOSITE_STATE_ERROR;
189 c->status = NT_STATUS_CONFLICTING_ADDRESSES;
190 DEBUG(3,("Name registration conflict from %s for %s with ip %s - rcode %d\n",
191 state->io->out.reply_from,
192 nbt_name_string(state, &state->io->out.name),
193 state->io->out.reply_addr,
194 state->io->out.rcode));
198 if (c->state >= COMPOSITE_STATE_DONE &&
205 the async send call for a 4 stage name registration
207 struct composite_context *nbt_name_register_bcast_send(struct nbt_name_socket *nbtsock,
208 struct nbt_name_register_bcast *io)
210 struct composite_context *c;
211 struct register_bcast_state *state;
213 c = talloc_zero(nbtsock, struct composite_context);
214 if (c == NULL) goto failed;
216 state = talloc(c, struct register_bcast_state);
217 if (state == NULL) goto failed;
219 state->io = talloc(state, struct nbt_name_register);
220 if (state->io == NULL) goto failed;
222 state->io->in.name = io->in.name;
223 state->io->in.dest_addr = io->in.dest_addr;
224 state->io->in.address = io->in.address;
225 state->io->in.nb_flags = io->in.nb_flags;
226 state->io->in.register_demand = False;
227 state->io->in.broadcast = True;
228 state->io->in.multi_homed = False;
229 state->io->in.ttl = io->in.ttl;
230 state->io->in.timeout = 1;
231 state->io->in.retries = 2;
233 state->nbtsock = nbtsock;
235 state->req = nbt_name_register_send(nbtsock, state->io);
236 if (state->req == NULL) goto failed;
238 state->req->async.fn = name_register_bcast_handler;
239 state->req->async.private = c;
241 c->private_data = state;
242 c->state = COMPOSITE_STATE_IN_PROGRESS;
243 c->event_ctx = nbtsock->event_ctx;
253 broadcast 4 part name register - recv
255 NTSTATUS nbt_name_register_bcast_recv(struct composite_context *c)
258 status = composite_wait(c);
264 broadcast 4 part name register - sync interface
266 NTSTATUS nbt_name_register_bcast(struct nbt_name_socket *nbtsock,
267 struct nbt_name_register_bcast *io)
269 struct composite_context *c = nbt_name_register_bcast_send(nbtsock, io);
270 return nbt_name_register_bcast_recv(c);
275 a wins name register with multiple WINS servers and multiple
276 addresses to register. Try each WINS server in turn, until we get a
277 reply for each address
279 struct register_wins_state {
280 struct nbt_name_socket *nbtsock;
281 struct nbt_name_register *io;
282 const char **wins_servers;
283 const char **addresses;
285 struct nbt_name_request *req;
290 state handler for WINS multi-homed multi-server name register
292 static void name_register_wins_handler(struct nbt_name_request *req)
294 struct composite_context *c = talloc_get_type(req->async.private,
295 struct composite_context);
296 struct register_wins_state *state = talloc_get_type(c->private_data,
297 struct register_wins_state);
300 status = nbt_name_register_recv(state->req, state, state->io);
301 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
302 /* the register timed out - try the next WINS server */
303 state->wins_servers++;
304 state->address_idx = 0;
305 if (state->wins_servers[0] == NULL) {
306 c->state = COMPOSITE_STATE_ERROR;
310 state->io->in.dest_addr = state->wins_servers[0];
311 state->io->in.address = state->addresses[0];
312 state->req = nbt_name_register_send(state->nbtsock, state->io);
313 if (state->req == NULL) {
314 c->state = COMPOSITE_STATE_ERROR;
315 c->status = NT_STATUS_NO_MEMORY;
317 state->req->async.fn = name_register_wins_handler;
318 state->req->async.private = c;
320 } else if (!NT_STATUS_IS_OK(status)) {
321 c->state = COMPOSITE_STATE_ERROR;
324 if (state->io->out.rcode == 0 &&
325 state->addresses[state->address_idx+1] != NULL) {
326 /* register our next address */
327 state->io->in.address = state->addresses[++(state->address_idx)];
328 state->req = nbt_name_register_send(state->nbtsock, state->io);
329 if (state->req == NULL) {
330 c->state = COMPOSITE_STATE_ERROR;
331 c->status = NT_STATUS_NO_MEMORY;
333 state->req->async.fn = name_register_wins_handler;
334 state->req->async.private = c;
337 c->state = COMPOSITE_STATE_DONE;
338 c->status = NT_STATUS_OK;
343 if (c->state >= COMPOSITE_STATE_DONE &&
350 the async send call for a multi-server WINS register
352 struct composite_context *nbt_name_register_wins_send(struct nbt_name_socket *nbtsock,
353 struct nbt_name_register_wins *io)
355 struct composite_context *c;
356 struct register_wins_state *state;
358 c = talloc_zero(nbtsock, struct composite_context);
359 if (c == NULL) goto failed;
361 state = talloc(c, struct register_wins_state);
362 if (state == NULL) goto failed;
364 state->io = talloc(state, struct nbt_name_register);
365 if (state->io == NULL) goto failed;
367 state->wins_servers = str_list_copy(state, io->in.wins_servers);
368 if (state->wins_servers == NULL ||
369 state->wins_servers[0] == NULL) goto failed;
371 state->addresses = str_list_copy(state, io->in.addresses);
372 if (state->addresses == NULL ||
373 state->addresses[0] == NULL) goto failed;
375 state->io->in.name = io->in.name;
376 state->io->in.dest_addr = state->wins_servers[0];
377 state->io->in.address = io->in.addresses[0];
378 state->io->in.nb_flags = io->in.nb_flags;
379 state->io->in.broadcast = False;
380 state->io->in.register_demand = False;
381 state->io->in.multi_homed = (io->in.nb_flags & NBT_NM_GROUP)?False:True;
382 state->io->in.ttl = io->in.ttl;
383 state->io->in.timeout = 3;
384 state->io->in.retries = 2;
386 state->nbtsock = nbtsock;
387 state->address_idx = 0;
389 state->req = nbt_name_register_send(nbtsock, state->io);
390 if (state->req == NULL) goto failed;
392 state->req->async.fn = name_register_wins_handler;
393 state->req->async.private = c;
395 c->private_data = state;
396 c->state = COMPOSITE_STATE_IN_PROGRESS;
397 c->event_ctx = nbtsock->event_ctx;
407 multi-homed WINS name register - recv side
409 NTSTATUS nbt_name_register_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
410 struct nbt_name_register_wins *io)
413 status = composite_wait(c);
414 if (NT_STATUS_IS_OK(status)) {
415 struct register_wins_state *state =
416 talloc_get_type(c->private_data, struct register_wins_state);
417 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
418 io->out.rcode = state->io->out.rcode;
425 multi-homed WINS register - sync interface
427 NTSTATUS nbt_name_register_wins(struct nbt_name_socket *nbtsock,
429 struct nbt_name_register_wins *io)
431 struct composite_context *c = nbt_name_register_wins_send(nbtsock, io);
432 return nbt_name_register_wins_recv(c, mem_ctx, io);