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 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/>.
23 #include "libcli/nbt/libnbt.h"
24 #include "libcli/composite/composite.h"
25 #include "lib/socket/socket.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
29 send a nbt name registration request
31 struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock,
32 struct nbt_name_register *io)
34 struct nbt_name_request *req;
35 struct nbt_name_packet *packet;
36 struct socket_address *dest;
38 packet = talloc_zero(nbtsock, struct nbt_name_packet);
39 if (packet == NULL) return NULL;
43 if (io->in.multi_homed) {
44 packet->operation = NBT_OPCODE_MULTI_HOME_REG;
46 packet->operation = NBT_OPCODE_REGISTER;
48 if (io->in.broadcast) {
49 packet->operation |= NBT_FLAG_BROADCAST;
51 if (io->in.register_demand) {
52 packet->operation |= NBT_FLAG_RECURSION_DESIRED;
55 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
56 if (packet->questions == NULL) goto failed;
58 packet->questions[0].name = io->in.name;
59 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
60 packet->questions[0].question_class = NBT_QCLASS_IP;
62 packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
63 if (packet->additional == NULL) goto failed;
65 packet->additional[0].name = io->in.name;
66 packet->additional[0].rr_type = NBT_QTYPE_NETBIOS;
67 packet->additional[0].rr_class = NBT_QCLASS_IP;
68 packet->additional[0].ttl = io->in.ttl;
69 packet->additional[0].rdata.netbios.length = 6;
70 packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
71 struct nbt_rdata_address, 1);
72 if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
73 packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
74 packet->additional[0].rdata.netbios.addresses[0].ipaddr =
75 talloc_strdup(packet->additional, io->in.address);
76 if (packet->additional[0].rdata.netbios.addresses[0].ipaddr == NULL) goto failed;
78 dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
79 io->in.dest_addr, lp_nbt_port());
80 if (dest == NULL) goto failed;
81 req = nbt_name_request_send(nbtsock, dest, packet,
82 io->in.timeout, io->in.retries, False);
83 if (req == NULL) goto failed;
94 wait for a registration reply
96 NTSTATUS nbt_name_register_recv(struct nbt_name_request *req,
97 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
100 struct nbt_name_packet *packet;
102 status = nbt_name_request_recv(req);
103 if (!NT_STATUS_IS_OK(status) ||
104 req->num_replies == 0) {
109 packet = req->replies[0].packet;
110 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
112 if (packet->ancount != 1 ||
113 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
114 packet->answers[0].rr_class != NBT_QCLASS_IP) {
116 return NT_STATUS_INVALID_NETWORK_RESPONSE;
119 io->out.rcode = packet->operation & NBT_RCODE;
120 io->out.name = packet->answers[0].name;
121 if (packet->answers[0].rdata.netbios.length < 6) {
123 return NT_STATUS_INVALID_NETWORK_RESPONSE;
125 io->out.reply_addr = talloc_steal(mem_ctx,
126 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
127 talloc_steal(mem_ctx, io->out.name.name);
128 talloc_steal(mem_ctx, io->out.name.scope);
136 synchronous name registration request
138 NTSTATUS nbt_name_register(struct nbt_name_socket *nbtsock,
139 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
141 struct nbt_name_request *req = nbt_name_register_send(nbtsock, io);
142 return nbt_name_register_recv(req, mem_ctx, io);
147 a 4 step broadcast registration. 3 lots of name registration requests, followed by
148 a name registration demand
150 struct register_bcast_state {
151 struct nbt_name_socket *nbtsock;
152 struct nbt_name_register *io;
153 struct nbt_name_request *req;
158 state handler for 4 stage name registration
160 static void name_register_bcast_handler(struct nbt_name_request *req)
162 struct composite_context *c = talloc_get_type(req->async.private, struct composite_context);
163 struct register_bcast_state *state = talloc_get_type(c->private_data, struct register_bcast_state);
166 status = nbt_name_register_recv(state->req, state, state->io);
167 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
168 if (state->io->in.register_demand == True) {
170 c->state = COMPOSITE_STATE_DONE;
171 c->status = NT_STATUS_OK;
175 /* the registration timed out - good, send the demand */
176 state->io->in.register_demand = True;
177 state->io->in.retries = 0;
178 state->req = nbt_name_register_send(state->nbtsock, state->io);
179 if (state->req == NULL) {
180 c->state = COMPOSITE_STATE_ERROR;
181 c->status = NT_STATUS_NO_MEMORY;
183 state->req->async.fn = name_register_bcast_handler;
184 state->req->async.private = c;
186 } else if (!NT_STATUS_IS_OK(status)) {
187 c->state = COMPOSITE_STATE_ERROR;
190 c->state = COMPOSITE_STATE_ERROR;
191 c->status = NT_STATUS_CONFLICTING_ADDRESSES;
192 DEBUG(3,("Name registration conflict from %s for %s with ip %s - rcode %d\n",
193 state->io->out.reply_from,
194 nbt_name_string(state, &state->io->out.name),
195 state->io->out.reply_addr,
196 state->io->out.rcode));
200 if (c->state >= COMPOSITE_STATE_DONE &&
207 the async send call for a 4 stage name registration
209 struct composite_context *nbt_name_register_bcast_send(struct nbt_name_socket *nbtsock,
210 struct nbt_name_register_bcast *io)
212 struct composite_context *c;
213 struct register_bcast_state *state;
215 c = talloc_zero(nbtsock, struct composite_context);
216 if (c == NULL) goto failed;
218 state = talloc(c, struct register_bcast_state);
219 if (state == NULL) goto failed;
221 state->io = talloc(state, struct nbt_name_register);
222 if (state->io == NULL) goto failed;
224 state->io->in.name = io->in.name;
225 state->io->in.dest_addr = io->in.dest_addr;
226 state->io->in.address = io->in.address;
227 state->io->in.nb_flags = io->in.nb_flags;
228 state->io->in.register_demand = False;
229 state->io->in.broadcast = True;
230 state->io->in.multi_homed = False;
231 state->io->in.ttl = io->in.ttl;
232 state->io->in.timeout = 1;
233 state->io->in.retries = 2;
235 state->nbtsock = nbtsock;
237 state->req = nbt_name_register_send(nbtsock, state->io);
238 if (state->req == NULL) goto failed;
240 state->req->async.fn = name_register_bcast_handler;
241 state->req->async.private = c;
243 c->private_data = state;
244 c->state = COMPOSITE_STATE_IN_PROGRESS;
245 c->event_ctx = nbtsock->event_ctx;
255 broadcast 4 part name register - recv
257 NTSTATUS nbt_name_register_bcast_recv(struct composite_context *c)
260 status = composite_wait(c);
266 broadcast 4 part name register - sync interface
268 NTSTATUS nbt_name_register_bcast(struct nbt_name_socket *nbtsock,
269 struct nbt_name_register_bcast *io)
271 struct composite_context *c = nbt_name_register_bcast_send(nbtsock, io);
272 return nbt_name_register_bcast_recv(c);
277 a wins name register with multiple WINS servers and multiple
278 addresses to register. Try each WINS server in turn, until we get a
279 reply for each address
281 struct register_wins_state {
282 struct nbt_name_socket *nbtsock;
283 struct nbt_name_register *io;
284 const char **wins_servers;
285 const char **addresses;
287 struct nbt_name_request *req;
292 state handler for WINS multi-homed multi-server name register
294 static void name_register_wins_handler(struct nbt_name_request *req)
296 struct composite_context *c = talloc_get_type(req->async.private,
297 struct composite_context);
298 struct register_wins_state *state = talloc_get_type(c->private_data,
299 struct register_wins_state);
302 status = nbt_name_register_recv(state->req, state, state->io);
303 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
304 /* the register timed out - try the next WINS server */
305 state->wins_servers++;
306 state->address_idx = 0;
307 if (state->wins_servers[0] == NULL) {
308 c->state = COMPOSITE_STATE_ERROR;
312 state->io->in.dest_addr = state->wins_servers[0];
313 state->io->in.address = state->addresses[0];
314 state->req = nbt_name_register_send(state->nbtsock, state->io);
315 if (state->req == NULL) {
316 c->state = COMPOSITE_STATE_ERROR;
317 c->status = NT_STATUS_NO_MEMORY;
319 state->req->async.fn = name_register_wins_handler;
320 state->req->async.private = c;
322 } else if (!NT_STATUS_IS_OK(status)) {
323 c->state = COMPOSITE_STATE_ERROR;
326 if (state->io->out.rcode == 0 &&
327 state->addresses[state->address_idx+1] != NULL) {
328 /* register our next address */
329 state->io->in.address = state->addresses[++(state->address_idx)];
330 state->req = nbt_name_register_send(state->nbtsock, state->io);
331 if (state->req == NULL) {
332 c->state = COMPOSITE_STATE_ERROR;
333 c->status = NT_STATUS_NO_MEMORY;
335 state->req->async.fn = name_register_wins_handler;
336 state->req->async.private = c;
339 c->state = COMPOSITE_STATE_DONE;
340 c->status = NT_STATUS_OK;
345 if (c->state >= COMPOSITE_STATE_DONE &&
352 the async send call for a multi-server WINS register
354 struct composite_context *nbt_name_register_wins_send(struct nbt_name_socket *nbtsock,
355 struct nbt_name_register_wins *io)
357 struct composite_context *c;
358 struct register_wins_state *state;
360 c = talloc_zero(nbtsock, struct composite_context);
361 if (c == NULL) goto failed;
363 state = talloc(c, struct register_wins_state);
364 if (state == NULL) goto failed;
366 state->io = talloc(state, struct nbt_name_register);
367 if (state->io == NULL) goto failed;
369 state->wins_servers = str_list_copy(state, io->in.wins_servers);
370 if (state->wins_servers == NULL ||
371 state->wins_servers[0] == NULL) goto failed;
373 state->addresses = str_list_copy(state, io->in.addresses);
374 if (state->addresses == NULL ||
375 state->addresses[0] == NULL) goto failed;
377 state->io->in.name = io->in.name;
378 state->io->in.dest_addr = state->wins_servers[0];
379 state->io->in.address = io->in.addresses[0];
380 state->io->in.nb_flags = io->in.nb_flags;
381 state->io->in.broadcast = False;
382 state->io->in.register_demand = False;
383 state->io->in.multi_homed = (io->in.nb_flags & NBT_NM_GROUP)?False:True;
384 state->io->in.ttl = io->in.ttl;
385 state->io->in.timeout = 3;
386 state->io->in.retries = 2;
388 state->nbtsock = nbtsock;
389 state->address_idx = 0;
391 state->req = nbt_name_register_send(nbtsock, state->io);
392 if (state->req == NULL) goto failed;
394 state->req->async.fn = name_register_wins_handler;
395 state->req->async.private = c;
397 c->private_data = state;
398 c->state = COMPOSITE_STATE_IN_PROGRESS;
399 c->event_ctx = nbtsock->event_ctx;
409 multi-homed WINS name register - recv side
411 NTSTATUS nbt_name_register_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
412 struct nbt_name_register_wins *io)
415 status = composite_wait(c);
416 if (NT_STATUS_IS_OK(status)) {
417 struct register_wins_state *state =
418 talloc_get_type(c->private_data, struct register_wins_state);
419 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
420 io->out.rcode = state->io->out.rcode;
427 multi-homed WINS register - sync interface
429 NTSTATUS nbt_name_register_wins(struct nbt_name_socket *nbtsock,
431 struct nbt_name_register_wins *io)
433 struct composite_context *c = nbt_name_register_wins_send(nbtsock, io);
434 return nbt_name_register_wins_recv(c, mem_ctx, io);