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/nbt/nbt_proto.h"
25 #include "libcli/composite/composite.h"
26 #include "lib/socket/socket.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "param/param.h"
31 send a nbt name registration request
33 struct nbt_name_request
*nbt_name_register_send(struct nbt_name_socket
*nbtsock
,
34 struct nbt_name_register
*io
)
36 struct nbt_name_request
*req
;
37 struct nbt_name_packet
*packet
;
38 struct socket_address
*dest
;
40 packet
= talloc_zero(nbtsock
, struct nbt_name_packet
);
41 if (packet
== NULL
) return NULL
;
45 if (io
->in
.multi_homed
) {
46 packet
->operation
= NBT_OPCODE_MULTI_HOME_REG
;
48 packet
->operation
= NBT_OPCODE_REGISTER
;
50 if (io
->in
.broadcast
) {
51 packet
->operation
|= NBT_FLAG_BROADCAST
;
53 if (io
->in
.register_demand
) {
54 packet
->operation
|= NBT_FLAG_RECURSION_DESIRED
;
57 packet
->questions
= talloc_array(packet
, struct nbt_name_question
, 1);
58 if (packet
->questions
== NULL
) goto failed
;
60 packet
->questions
[0].name
= io
->in
.name
;
61 packet
->questions
[0].question_type
= NBT_QTYPE_NETBIOS
;
62 packet
->questions
[0].question_class
= NBT_QCLASS_IP
;
64 packet
->additional
= talloc_array(packet
, struct nbt_res_rec
, 1);
65 if (packet
->additional
== NULL
) goto failed
;
67 packet
->additional
[0].name
= io
->in
.name
;
68 packet
->additional
[0].rr_type
= NBT_QTYPE_NETBIOS
;
69 packet
->additional
[0].rr_class
= NBT_QCLASS_IP
;
70 packet
->additional
[0].ttl
= io
->in
.ttl
;
71 packet
->additional
[0].rdata
.netbios
.length
= 6;
72 packet
->additional
[0].rdata
.netbios
.addresses
= talloc_array(packet
->additional
,
73 struct nbt_rdata_address
, 1);
74 if (packet
->additional
[0].rdata
.netbios
.addresses
== NULL
) goto failed
;
75 packet
->additional
[0].rdata
.netbios
.addresses
[0].nb_flags
= io
->in
.nb_flags
;
76 packet
->additional
[0].rdata
.netbios
.addresses
[0].ipaddr
=
77 talloc_strdup(packet
->additional
, io
->in
.address
);
78 if (packet
->additional
[0].rdata
.netbios
.addresses
[0].ipaddr
== NULL
) goto failed
;
80 dest
= socket_address_from_strings(packet
, nbtsock
->sock
->backend_name
,
81 io
->in
.dest_addr
, io
->in
.dest_port
);
82 if (dest
== NULL
) goto failed
;
83 req
= nbt_name_request_send(nbtsock
, dest
, packet
,
84 io
->in
.timeout
, io
->in
.retries
, false);
85 if (req
== NULL
) goto failed
;
96 wait for a registration reply
98 _PUBLIC_ NTSTATUS
nbt_name_register_recv(struct nbt_name_request
*req
,
99 TALLOC_CTX
*mem_ctx
, struct nbt_name_register
*io
)
102 struct nbt_name_packet
*packet
;
104 status
= nbt_name_request_recv(req
);
105 if (!NT_STATUS_IS_OK(status
) ||
106 req
->num_replies
== 0) {
111 packet
= req
->replies
[0].packet
;
112 io
->out
.reply_from
= talloc_steal(mem_ctx
, req
->replies
[0].dest
->addr
);
114 if (packet
->ancount
!= 1 ||
115 packet
->answers
[0].rr_type
!= NBT_QTYPE_NETBIOS
||
116 packet
->answers
[0].rr_class
!= NBT_QCLASS_IP
) {
118 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
121 io
->out
.rcode
= packet
->operation
& NBT_RCODE
;
122 io
->out
.name
= packet
->answers
[0].name
;
123 if (packet
->answers
[0].rdata
.netbios
.length
< 6) {
125 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
127 io
->out
.reply_addr
= talloc_steal(mem_ctx
,
128 packet
->answers
[0].rdata
.netbios
.addresses
[0].ipaddr
);
129 talloc_steal(mem_ctx
, io
->out
.name
.name
);
130 talloc_steal(mem_ctx
, io
->out
.name
.scope
);
138 synchronous name registration request
140 _PUBLIC_ NTSTATUS
nbt_name_register(struct nbt_name_socket
*nbtsock
,
141 TALLOC_CTX
*mem_ctx
, struct nbt_name_register
*io
)
143 struct nbt_name_request
*req
= nbt_name_register_send(nbtsock
, io
);
144 return nbt_name_register_recv(req
, mem_ctx
, io
);
149 a 4 step broadcast registration. 3 lots of name registration requests, followed by
150 a name registration demand
152 struct register_bcast_state
{
153 struct nbt_name_socket
*nbtsock
;
154 struct nbt_name_register
*io
;
155 struct nbt_name_request
*req
;
160 state handler for 4 stage name registration
162 static void name_register_bcast_handler(struct nbt_name_request
*req
)
164 struct composite_context
*c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
165 struct register_bcast_state
*state
= talloc_get_type(c
->private_data
, struct register_bcast_state
);
168 status
= nbt_name_register_recv(state
->req
, state
, state
->io
);
169 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
170 if (state
->io
->in
.register_demand
== true) {
172 c
->state
= COMPOSITE_STATE_DONE
;
173 c
->status
= NT_STATUS_OK
;
177 /* the registration timed out - good, send the demand */
178 state
->io
->in
.register_demand
= true;
179 state
->io
->in
.retries
= 0;
180 state
->req
= nbt_name_register_send(state
->nbtsock
, state
->io
);
181 if (state
->req
== NULL
) {
182 c
->state
= COMPOSITE_STATE_ERROR
;
183 c
->status
= NT_STATUS_NO_MEMORY
;
185 state
->req
->async
.fn
= name_register_bcast_handler
;
186 state
->req
->async
.private_data
= c
;
188 } else if (!NT_STATUS_IS_OK(status
)) {
189 c
->state
= COMPOSITE_STATE_ERROR
;
192 c
->state
= COMPOSITE_STATE_ERROR
;
193 c
->status
= NT_STATUS_CONFLICTING_ADDRESSES
;
194 DEBUG(3,("Name registration conflict from %s for %s with ip %s - rcode %d\n",
195 state
->io
->out
.reply_from
,
196 nbt_name_string(state
, &state
->io
->out
.name
),
197 state
->io
->out
.reply_addr
,
198 state
->io
->out
.rcode
));
202 if (c
->state
>= COMPOSITE_STATE_DONE
&&
209 the async send call for a 4 stage name registration
211 _PUBLIC_
struct composite_context
*nbt_name_register_bcast_send(struct nbt_name_socket
*nbtsock
,
212 struct nbt_name_register_bcast
*io
)
214 struct composite_context
*c
;
215 struct register_bcast_state
*state
;
217 c
= talloc_zero(nbtsock
, struct composite_context
);
218 if (c
== NULL
) goto failed
;
220 state
= talloc(c
, struct register_bcast_state
);
221 if (state
== NULL
) goto failed
;
223 state
->io
= talloc(state
, struct nbt_name_register
);
224 if (state
->io
== NULL
) goto failed
;
226 state
->io
->in
.name
= io
->in
.name
;
227 state
->io
->in
.dest_addr
= io
->in
.dest_addr
;
228 state
->io
->in
.dest_port
= io
->in
.dest_port
;
229 state
->io
->in
.address
= io
->in
.address
;
230 state
->io
->in
.nb_flags
= io
->in
.nb_flags
;
231 state
->io
->in
.register_demand
= false;
232 state
->io
->in
.broadcast
= true;
233 state
->io
->in
.multi_homed
= false;
234 state
->io
->in
.ttl
= io
->in
.ttl
;
235 state
->io
->in
.timeout
= 1;
236 state
->io
->in
.retries
= 2;
238 state
->nbtsock
= nbtsock
;
240 state
->req
= nbt_name_register_send(nbtsock
, state
->io
);
241 if (state
->req
== NULL
) goto failed
;
243 state
->req
->async
.fn
= name_register_bcast_handler
;
244 state
->req
->async
.private_data
= c
;
246 c
->private_data
= state
;
247 c
->state
= COMPOSITE_STATE_IN_PROGRESS
;
248 c
->event_ctx
= nbtsock
->event_ctx
;
258 broadcast 4 part name register - recv
260 _PUBLIC_ NTSTATUS
nbt_name_register_bcast_recv(struct composite_context
*c
)
263 status
= composite_wait(c
);
269 broadcast 4 part name register - sync interface
271 NTSTATUS
nbt_name_register_bcast(struct nbt_name_socket
*nbtsock
,
272 struct nbt_name_register_bcast
*io
)
274 struct composite_context
*c
= nbt_name_register_bcast_send(nbtsock
, io
);
275 return nbt_name_register_bcast_recv(c
);
280 a wins name register with multiple WINS servers and multiple
281 addresses to register. Try each WINS server in turn, until we get a
282 reply for each address
284 struct register_wins_state
{
285 struct nbt_name_socket
*nbtsock
;
286 struct nbt_name_register
*io
;
287 const char **wins_servers
;
289 const char **addresses
;
291 struct nbt_name_request
*req
;
296 state handler for WINS multi-homed multi-server name register
298 static void name_register_wins_handler(struct nbt_name_request
*req
)
300 struct composite_context
*c
= talloc_get_type(req
->async
.private_data
,
301 struct composite_context
);
302 struct register_wins_state
*state
= talloc_get_type(c
->private_data
,
303 struct register_wins_state
);
306 status
= nbt_name_register_recv(state
->req
, state
, state
->io
);
307 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
308 /* the register timed out - try the next WINS server */
309 state
->wins_servers
++;
310 state
->address_idx
= 0;
311 if (state
->wins_servers
[0] == NULL
) {
312 c
->state
= COMPOSITE_STATE_ERROR
;
316 state
->io
->in
.dest_addr
= state
->wins_servers
[0];
317 state
->io
->in
.dest_port
= state
->wins_port
;
318 state
->io
->in
.address
= state
->addresses
[0];
319 state
->req
= nbt_name_register_send(state
->nbtsock
, state
->io
);
320 if (state
->req
== NULL
) {
321 c
->state
= COMPOSITE_STATE_ERROR
;
322 c
->status
= NT_STATUS_NO_MEMORY
;
324 state
->req
->async
.fn
= name_register_wins_handler
;
325 state
->req
->async
.private_data
= c
;
327 } else if (!NT_STATUS_IS_OK(status
)) {
328 c
->state
= COMPOSITE_STATE_ERROR
;
331 if (state
->io
->out
.rcode
== 0 &&
332 state
->addresses
[state
->address_idx
+1] != NULL
) {
333 /* register our next address */
334 state
->io
->in
.address
= state
->addresses
[++(state
->address_idx
)];
335 state
->req
= nbt_name_register_send(state
->nbtsock
, state
->io
);
336 if (state
->req
== NULL
) {
337 c
->state
= COMPOSITE_STATE_ERROR
;
338 c
->status
= NT_STATUS_NO_MEMORY
;
340 state
->req
->async
.fn
= name_register_wins_handler
;
341 state
->req
->async
.private_data
= c
;
344 c
->state
= COMPOSITE_STATE_DONE
;
345 c
->status
= NT_STATUS_OK
;
350 if (c
->state
>= COMPOSITE_STATE_DONE
&&
357 the async send call for a multi-server WINS register
359 _PUBLIC_
struct composite_context
*nbt_name_register_wins_send(struct nbt_name_socket
*nbtsock
,
360 struct nbt_name_register_wins
*io
)
362 struct composite_context
*c
;
363 struct register_wins_state
*state
;
365 c
= talloc_zero(nbtsock
, struct composite_context
);
366 if (c
== NULL
) goto failed
;
368 state
= talloc(c
, struct register_wins_state
);
369 if (state
== NULL
) goto failed
;
371 state
->io
= talloc(state
, struct nbt_name_register
);
372 if (state
->io
== NULL
) goto failed
;
374 state
->wins_port
= io
->in
.wins_port
;
375 state
->wins_servers
= str_list_copy(state
, io
->in
.wins_servers
);
376 if (state
->wins_servers
== NULL
||
377 state
->wins_servers
[0] == NULL
) goto failed
;
379 state
->addresses
= str_list_copy(state
, io
->in
.addresses
);
380 if (state
->addresses
== NULL
||
381 state
->addresses
[0] == NULL
) goto failed
;
383 state
->io
->in
.name
= io
->in
.name
;
384 state
->io
->in
.dest_addr
= state
->wins_servers
[0];
385 state
->io
->in
.dest_port
= state
->wins_port
;
386 state
->io
->in
.address
= io
->in
.addresses
[0];
387 state
->io
->in
.nb_flags
= io
->in
.nb_flags
;
388 state
->io
->in
.broadcast
= false;
389 state
->io
->in
.register_demand
= false;
390 state
->io
->in
.multi_homed
= (io
->in
.nb_flags
& NBT_NM_GROUP
)?false:true;
391 state
->io
->in
.ttl
= io
->in
.ttl
;
392 state
->io
->in
.timeout
= 3;
393 state
->io
->in
.retries
= 2;
395 state
->nbtsock
= nbtsock
;
396 state
->address_idx
= 0;
398 state
->req
= nbt_name_register_send(nbtsock
, state
->io
);
399 if (state
->req
== NULL
) goto failed
;
401 state
->req
->async
.fn
= name_register_wins_handler
;
402 state
->req
->async
.private_data
= c
;
404 c
->private_data
= state
;
405 c
->state
= COMPOSITE_STATE_IN_PROGRESS
;
406 c
->event_ctx
= nbtsock
->event_ctx
;
416 multi-homed WINS name register - recv side
418 _PUBLIC_ NTSTATUS
nbt_name_register_wins_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
419 struct nbt_name_register_wins
*io
)
422 status
= composite_wait(c
);
423 if (NT_STATUS_IS_OK(status
)) {
424 struct register_wins_state
*state
=
425 talloc_get_type(c
->private_data
, struct register_wins_state
);
426 io
->out
.wins_server
= talloc_steal(mem_ctx
, state
->wins_servers
[0]);
427 io
->out
.rcode
= state
->io
->out
.rcode
;
434 multi-homed WINS register - sync interface
436 _PUBLIC_ NTSTATUS
nbt_name_register_wins(struct nbt_name_socket
*nbtsock
,
438 struct nbt_name_register_wins
*io
)
440 struct composite_context
*c
= nbt_name_register_wins_send(nbtsock
, io
);
441 return nbt_name_register_wins_recv(c
, mem_ctx
, io
);