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/raw/libcliraw.h"
26 #include "libcli/composite/composite.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;
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 req = nbt_name_request_send(nbtsock, io->in.dest_addr, lp_nbt_port(), packet,
78 io->in.timeout, io->in.retries, False);
79 if (req == NULL) goto failed;
90 wait for a registration reply
92 NTSTATUS nbt_name_register_recv(struct nbt_name_request *req,
93 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
96 struct nbt_name_packet *packet;
98 status = nbt_name_request_recv(req);
99 if (!NT_STATUS_IS_OK(status) ||
100 req->num_replies == 0) {
105 packet = req->replies[0].packet;
106 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].reply_addr);
108 if (packet->ancount != 1 ||
109 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
110 packet->answers[0].rr_class != NBT_QCLASS_IP) {
112 return NT_STATUS_INVALID_NETWORK_RESPONSE;
115 io->out.rcode = packet->operation & NBT_RCODE;
116 io->out.name = packet->answers[0].name;
117 if (packet->answers[0].rdata.netbios.length < 6) {
119 return NT_STATUS_INVALID_NETWORK_RESPONSE;
121 io->out.reply_addr = talloc_steal(mem_ctx,
122 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
123 talloc_steal(mem_ctx, io->out.name.name);
124 talloc_steal(mem_ctx, io->out.name.scope);
132 synchronous name registration request
134 NTSTATUS nbt_name_register(struct nbt_name_socket *nbtsock,
135 TALLOC_CTX *mem_ctx, struct nbt_name_register *io)
137 struct nbt_name_request *req = nbt_name_register_send(nbtsock, io);
138 return nbt_name_register_recv(req, mem_ctx, io);
143 a 4 step broadcast registration. 3 lots of name registration requests, followed by
144 a name registration demand
146 struct register_bcast_state {
147 struct nbt_name_socket *nbtsock;
148 struct nbt_name_register *io;
149 struct nbt_name_request *req;
154 state handler for 4 stage name registration
156 static void name_register_bcast_handler(struct nbt_name_request *req)
158 struct composite_context *c = talloc_get_type(req->async.private, struct composite_context);
159 struct register_bcast_state *state = talloc_get_type(c->private, struct register_bcast_state);
162 status = nbt_name_register_recv(state->req, state, state->io);
163 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
164 if (state->io->in.register_demand == True) {
166 c->state = SMBCLI_REQUEST_DONE;
167 c->status = NT_STATUS_OK;
171 /* the registration timed out - good, send the demand */
172 state->io->in.register_demand = True;
173 state->io->in.retries = 0;
174 state->req = nbt_name_register_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_register_bcast_handler;
180 state->req->async.private = c;
182 } else if (!NT_STATUS_IS_OK(status)) {
183 c->state = SMBCLI_REQUEST_ERROR;
186 c->state = SMBCLI_REQUEST_ERROR;
187 c->status = NT_STATUS_CONFLICTING_ADDRESSES;
188 DEBUG(3,("Name registration conflict from %s for %s<%02x> with ip %s - rcode %d\n",
189 state->io->out.reply_from,
190 state->io->out.name.name,
191 state->io->out.name.type,
192 state->io->out.reply_addr,
193 state->io->out.rcode));
197 if (c->state >= SMBCLI_REQUEST_DONE &&
204 the async send call for a 4 stage name registration
206 struct composite_context *nbt_name_register_bcast_send(struct nbt_name_socket *nbtsock,
207 struct nbt_name_register_bcast *io)
209 struct composite_context *c;
210 struct register_bcast_state *state;
212 c = talloc_zero(nbtsock, struct composite_context);
213 if (c == NULL) goto failed;
215 state = talloc(c, struct register_bcast_state);
216 if (state == NULL) goto failed;
218 state->io = talloc(state, struct nbt_name_register);
219 if (state->io == NULL) goto failed;
221 state->io->in.name = io->in.name;
222 state->io->in.dest_addr = io->in.dest_addr;
223 state->io->in.address = io->in.address;
224 state->io->in.nb_flags = io->in.nb_flags;
225 state->io->in.register_demand = False;
226 state->io->in.broadcast = True;
227 state->io->in.multi_homed = False;
228 state->io->in.ttl = io->in.ttl;
229 state->io->in.timeout = 1;
230 state->io->in.retries = 2;
232 state->nbtsock = nbtsock;
234 state->req = nbt_name_register_send(nbtsock, state->io);
235 if (state->req == NULL) goto failed;
237 state->req->async.fn = name_register_bcast_handler;
238 state->req->async.private = c;
241 c->state = SMBCLI_REQUEST_SEND;
242 c->event_ctx = nbtsock->event_ctx;
252 broadcast 4 part name register - recv
254 NTSTATUS nbt_name_register_bcast_recv(struct composite_context *c)
257 status = composite_wait(c);
263 broadcast 4 part name register - sync interface
265 NTSTATUS nbt_name_register_bcast(struct nbt_name_socket *nbtsock,
266 struct nbt_name_register_bcast *io)
268 struct composite_context *c = nbt_name_register_bcast_send(nbtsock, io);
269 return nbt_name_register_bcast_recv(c);
274 a wins name register with multiple WINS servers and multiple
275 addresses to register. Try each WINS server in turn, until we get a
276 reply for each address
278 struct register_wins_state {
279 struct nbt_name_socket *nbtsock;
280 struct nbt_name_register *io;
281 const char **wins_servers;
282 const char **addresses;
284 struct nbt_name_request *req;
289 state handler for WINS multi-homed multi-server name register
291 static void name_register_wins_handler(struct nbt_name_request *req)
293 struct composite_context *c = talloc_get_type(req->async.private,
294 struct composite_context);
295 struct register_wins_state *state = talloc_get_type(c->private,
296 struct register_wins_state);
299 status = nbt_name_register_recv(state->req, state, state->io);
300 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
301 /* the register timed out - try the next WINS server */
302 state->wins_servers++;
303 state->address_idx = 0;
304 if (state->wins_servers[0] == NULL) {
305 c->state = SMBCLI_REQUEST_ERROR;
309 state->io->in.dest_addr = state->wins_servers[0];
310 state->io->in.address = state->addresses[0];
311 state->req = nbt_name_register_send(state->nbtsock, state->io);
312 if (state->req == NULL) {
313 c->state = SMBCLI_REQUEST_ERROR;
314 c->status = NT_STATUS_NO_MEMORY;
316 state->req->async.fn = name_register_wins_handler;
317 state->req->async.private = c;
319 } else if (!NT_STATUS_IS_OK(status)) {
320 c->state = SMBCLI_REQUEST_ERROR;
323 if (state->io->out.rcode == 0 &&
324 state->addresses[state->address_idx+1] != NULL) {
325 /* register our next address */
326 state->io->in.address = state->addresses[++(state->address_idx)];
327 state->req = nbt_name_register_send(state->nbtsock, state->io);
328 if (state->req == NULL) {
329 c->state = SMBCLI_REQUEST_ERROR;
330 c->status = NT_STATUS_NO_MEMORY;
332 state->req->async.fn = name_register_wins_handler;
333 state->req->async.private = c;
336 c->state = SMBCLI_REQUEST_DONE;
337 c->status = NT_STATUS_OK;
342 if (c->state >= SMBCLI_REQUEST_DONE &&
349 the async send call for a multi-server WINS register
351 struct composite_context *nbt_name_register_wins_send(struct nbt_name_socket *nbtsock,
352 struct nbt_name_register_wins *io)
354 struct composite_context *c;
355 struct register_wins_state *state;
357 c = talloc_zero(nbtsock, struct composite_context);
358 if (c == NULL) goto failed;
360 state = talloc(c, struct register_wins_state);
361 if (state == NULL) goto failed;
363 state->io = talloc(state, struct nbt_name_register);
364 if (state->io == NULL) goto failed;
366 state->wins_servers = str_list_copy(state, io->in.wins_servers);
367 if (state->wins_servers == NULL ||
368 state->wins_servers[0] == NULL) goto failed;
370 state->addresses = str_list_copy(state, io->in.addresses);
371 if (state->addresses == NULL ||
372 state->addresses[0] == NULL) goto failed;
374 state->io->in.name = io->in.name;
375 state->io->in.dest_addr = state->wins_servers[0];
376 state->io->in.address = io->in.addresses[0];
377 state->io->in.nb_flags = io->in.nb_flags;
378 state->io->in.broadcast = False;
379 state->io->in.register_demand = False;
380 state->io->in.multi_homed = (io->in.nb_flags & NBT_NM_GROUP)?False:True;
381 state->io->in.ttl = io->in.ttl;
382 state->io->in.timeout = 3;
383 state->io->in.retries = 2;
385 state->nbtsock = nbtsock;
386 state->address_idx = 0;
388 state->req = nbt_name_register_send(nbtsock, state->io);
389 if (state->req == NULL) goto failed;
391 state->req->async.fn = name_register_wins_handler;
392 state->req->async.private = c;
395 c->state = SMBCLI_REQUEST_SEND;
396 c->event_ctx = nbtsock->event_ctx;
406 multi-homed WINS name register - recv side
408 NTSTATUS nbt_name_register_wins_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
409 struct nbt_name_register_wins *io)
412 status = composite_wait(c);
413 if (NT_STATUS_IS_OK(status)) {
414 struct register_wins_state *state =
415 talloc_get_type(c->private, struct register_wins_state);
416 io->out.wins_server = talloc_steal(mem_ctx, state->wins_servers[0]);
417 io->out.rcode = state->io->out.rcode;
424 multi-homed WINS register - sync interface
426 NTSTATUS nbt_name_register_wins(struct nbt_name_socket *nbtsock,
428 struct nbt_name_register_wins *io)
430 struct composite_context *c = nbt_name_register_wins_send(nbtsock, io);
431 return nbt_name_register_wins_recv(c, mem_ctx, io);