2 Unix SMB/CIFS implementation.
4 low level WINS replication client code
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 "lib/events/events.h"
25 #include "dlinklist.h"
26 #include "lib/socket/socket.h"
27 #include "libcli/wrepl/winsrepl.h"
30 mark all pending requests as dead - called when a socket error happens
32 static void wrepl_socket_dead(struct wrepl_socket *wrepl_socket)
34 wrepl_socket->dead = True;
36 event_set_fd_flags(wrepl_socket->fde, 0);
38 while (wrepl_socket->send_queue) {
39 struct wrepl_request *req = wrepl_socket->send_queue;
40 DLIST_REMOVE(wrepl_socket->send_queue, req);
41 req->state = WREPL_REQUEST_ERROR;
42 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
47 while (wrepl_socket->recv_queue) {
48 struct wrepl_request *req = wrepl_socket->recv_queue;
49 DLIST_REMOVE(wrepl_socket->recv_queue, req);
50 req->state = WREPL_REQUEST_ERROR;
51 req->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
61 static void wrepl_handler_send(struct wrepl_socket *wrepl_socket)
63 while (wrepl_socket->send_queue) {
64 struct wrepl_request *req = wrepl_socket->send_queue;
68 status = socket_send(wrepl_socket->sock, &req->buffer, &nsent, 0);
69 if (NT_STATUS_IS_ERR(status)) {
70 wrepl_socket_dead(wrepl_socket);
73 if (!NT_STATUS_IS_OK(status) || nsent == 0) return;
75 req->buffer.data += nsent;
76 req->buffer.length -= nsent;
77 if (req->buffer.length != 0) {
81 DLIST_REMOVE(wrepl_socket->send_queue, req);
82 DLIST_ADD_END(wrepl_socket->recv_queue, req, struct wrepl_request *);
83 req->state = WREPL_REQUEST_RECV;
85 EVENT_FD_READABLE(wrepl_socket->fde);
88 EVENT_FD_NOT_WRITEABLE(wrepl_socket->fde);
95 static void wrepl_handler_recv(struct wrepl_socket *wrepl_socket)
98 struct wrepl_request *req = wrepl_socket->recv_queue;
102 EVENT_FD_NOT_READABLE(wrepl_socket->fde);
106 if (req->buffer.length == 0) {
107 req->buffer = data_blob_talloc(req, NULL, 4);
108 if (req->buffer.data == NULL) {
109 req->status = NT_STATUS_NO_MEMORY;
115 /* read in the packet length */
116 if (req->num_read < 4) {
119 req->status = socket_recv(wrepl_socket->sock,
120 req->buffer.data + req->num_read,
123 if (NT_STATUS_IS_ERR(req->status)) {
124 wrepl_socket_dead(wrepl_socket);
127 if (!NT_STATUS_IS_OK(req->status)) return;
129 req->num_read += nread;
130 if (req->num_read != 4) return;
132 req_length = RIVAL(req->buffer.data, 0) + 4;
134 req->buffer.data = talloc_realloc(req, req->buffer.data,
135 uint8_t, req_length);
136 if (req->buffer.data == NULL) {
137 req->status = NT_STATUS_NO_MEMORY;
140 req->buffer.length = req_length;
143 /* read in the body */
144 req->status = socket_recv(wrepl_socket->sock,
145 req->buffer.data + req->num_read,
146 req->buffer.length - req->num_read,
148 if (NT_STATUS_IS_ERR(req->status)) {
149 wrepl_socket_dead(wrepl_socket);
152 if (!NT_STATUS_IS_OK(req->status)) return;
154 req->num_read += nread;
155 if (req->num_read != req->buffer.length) return;
157 req->packet = talloc(req, struct wrepl_packet);
158 if (req->packet == NULL) {
159 req->status = NT_STATUS_NO_MEMORY;
163 blob.data = req->buffer.data + 4;
164 blob.length = req->buffer.length - 4;
166 /* we have a full request - parse it */
167 req->status = ndr_pull_struct_blob(&blob,
168 req->packet, req->packet,
169 (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
170 if (!NT_STATUS_IS_OK(req->status)) {
171 DEBUG(2,("Failed to parse incoming WINS packet - %s\n",
172 nt_errstr(req->status)));
173 DEBUG(10,("packet length %d\n", (int)req->buffer.length));
174 NDR_PRINT_DEBUG(wrepl_packet, req->packet);
179 DEBUG(10,("Received WINS packet of length %d\n", (int)req->buffer.length));
180 NDR_PRINT_DEBUG(wrepl_packet, req->packet);
183 DLIST_REMOVE(wrepl_socket->recv_queue, req);
184 req->state = WREPL_REQUEST_DONE;
191 if (req->state == WREPL_REQUEST_RECV) {
192 DLIST_REMOVE(wrepl_socket->recv_queue, req);
194 req->state = WREPL_REQUEST_ERROR;
202 handler for winrepl events
204 static void wrepl_handler(struct event_context *ev, struct fd_event *fde,
205 uint16_t flags, void *private)
207 struct wrepl_socket *wrepl_socket = talloc_get_type(private,
208 struct wrepl_socket);
209 if (flags & EVENT_FD_WRITE) {
210 wrepl_handler_send(wrepl_socket);
212 if (flags & EVENT_FD_READ) {
213 wrepl_handler_recv(wrepl_socket);
219 handler for winrepl connection completion
221 static void wrepl_connect_handler(struct event_context *ev, struct fd_event *fde,
222 uint16_t flags, void *private)
224 struct wrepl_socket *wrepl_socket = talloc_get_type(private,
225 struct wrepl_socket);
226 struct wrepl_request *req = wrepl_socket->recv_queue;
230 if (req == NULL) return;
232 req->status = socket_connect_complete(wrepl_socket->sock, 0);
233 if (NT_STATUS_IS_ERR(req->status)) goto failed;
235 if (!NT_STATUS_IS_OK(req->status)) return;
237 wrepl_socket->fde = event_add_fd(wrepl_socket->event_ctx, wrepl_socket,
238 socket_get_fd(wrepl_socket->sock),
240 wrepl_handler, wrepl_socket);
241 if (wrepl_socket->fde == NULL) {
242 req->status = NT_STATUS_NO_MEMORY;
247 DLIST_REMOVE(wrepl_socket->recv_queue, req);
248 if (!NT_STATUS_IS_OK(req->status)) {
249 req->state = WREPL_REQUEST_ERROR;
251 req->state = WREPL_REQUEST_DONE;
260 initialise a wrepl_socket. The event_ctx is optional, if provided then
261 operations will use that event context
263 struct wrepl_socket *wrepl_socket_init(TALLOC_CTX *mem_ctx,
264 struct event_context *event_ctx)
266 struct wrepl_socket *wrepl_socket;
269 wrepl_socket = talloc(mem_ctx, struct wrepl_socket);
270 if (wrepl_socket == NULL) goto failed;
272 if (event_ctx == NULL) {
273 wrepl_socket->event_ctx = event_context_init(wrepl_socket);
275 wrepl_socket->event_ctx = talloc_reference(wrepl_socket, event_ctx);
277 if (wrepl_socket->event_ctx == NULL) goto failed;
279 status = socket_create("ip", SOCKET_TYPE_STREAM, &wrepl_socket->sock, 0);
280 if (!NT_STATUS_IS_OK(status)) goto failed;
282 talloc_steal(wrepl_socket, wrepl_socket->sock);
284 wrepl_socket->send_queue = NULL;
285 wrepl_socket->recv_queue = NULL;
286 wrepl_socket->dead = False;
288 wrepl_socket->fde = event_add_fd(wrepl_socket->event_ctx, wrepl_socket,
289 socket_get_fd(wrepl_socket->sock),
291 wrepl_connect_handler, wrepl_socket);
293 set_blocking(socket_get_fd(wrepl_socket->sock), False);
298 talloc_free(wrepl_socket);
304 destroy a wrepl_request
306 static int wrepl_request_destructor(void *ptr)
308 struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
309 if (req->state == WREPL_REQUEST_SEND) {
310 DLIST_REMOVE(req->wrepl_socket->send_queue, req);
312 if (req->state == WREPL_REQUEST_RECV) {
313 DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
315 req->state = WREPL_REQUEST_ERROR;
320 wait for a request to complete
322 static NTSTATUS wrepl_request_wait(struct wrepl_request *req)
324 NT_STATUS_HAVE_NO_MEMORY(req);
325 while (req->state < WREPL_REQUEST_DONE) {
326 event_loop_once(req->wrepl_socket->event_ctx);
333 connect a wrepl_socket to a WINS server
335 struct wrepl_request *wrepl_connect_send(struct wrepl_socket *wrepl_socket,
338 struct wrepl_request *req;
341 req = talloc_zero(wrepl_socket, struct wrepl_request);
342 if (req == NULL) goto failed;
344 req->wrepl_socket = wrepl_socket;
345 req->state = WREPL_REQUEST_RECV;
347 DLIST_ADD(wrepl_socket->recv_queue, req);
349 talloc_set_destructor(req, wrepl_request_destructor);
351 status = socket_connect(wrepl_socket->sock, iface_best_ip(address), 0, address,
352 WINS_REPLICATION_PORT, 0);
353 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) goto failed;
363 connect a wrepl_socket to a WINS server - recv side
365 NTSTATUS wrepl_connect_recv(struct wrepl_request *req)
367 return wrepl_request_wait(req);
372 connect a wrepl_socket to a WINS server - sync API
374 NTSTATUS wrepl_connect(struct wrepl_socket *wrepl_socket, const char *address)
376 struct wrepl_request *req = wrepl_connect_send(wrepl_socket, address);
377 return wrepl_connect_recv(req);
381 callback from wrepl_request_trigger()
383 static void wrepl_request_trigger_handler(struct event_context *ev, struct timed_event *te,
384 struct timeval t, void *ptr)
386 struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
389 * the event is a child of req,
390 * and req will be free'ed by the callback fn
391 * but the events code wants to free the event itself
393 talloc_steal(ev, te);
399 trigger an immediate event on a wrepl_request
401 static void wrepl_request_trigger(struct wrepl_request *req)
403 /* a zero timeout means immediate */
404 event_add_timed(req->wrepl_socket->event_ctx,
406 wrepl_request_trigger_handler, req);
411 send a generic wins replication request
413 struct wrepl_request *wrepl_request_send(struct wrepl_socket *wrepl_socket,
414 struct wrepl_packet *packet)
416 struct wrepl_request *req;
417 struct wrepl_wrap wrap;
419 req = talloc_zero(wrepl_socket, struct wrepl_request);
420 if (req == NULL) goto failed;
422 if (wrepl_socket->dead) {
423 req->wrepl_socket = wrepl_socket;
424 req->state = WREPL_REQUEST_ERROR;
425 req->status = NT_STATUS_INVALID_CONNECTION;
426 wrepl_request_trigger(req);
430 req->wrepl_socket = wrepl_socket;
431 req->state = WREPL_REQUEST_SEND;
433 wrap.packet = *packet;
434 req->status = ndr_push_struct_blob(&req->buffer, req, &wrap,
435 (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
436 if (!NT_STATUS_IS_OK(req->status)) goto failed;
439 DEBUG(10,("Sending WINS packet of length %d\n", (int)req->buffer.length));
440 NDR_PRINT_DEBUG(wrepl_packet, &wrap.packet);
443 DLIST_ADD(wrepl_socket->send_queue, req);
445 talloc_set_destructor(req, wrepl_request_destructor);
447 EVENT_FD_WRITEABLE(wrepl_socket->fde);
457 receive a generic WINS replication reply
459 NTSTATUS wrepl_request_recv(struct wrepl_request *req,
461 struct wrepl_packet **packet)
463 NTSTATUS status = wrepl_request_wait(req);
464 if (NT_STATUS_IS_OK(status)) {
465 *packet = talloc_steal(mem_ctx, req->packet);
472 a full WINS replication request/response
474 NTSTATUS wrepl_request(struct wrepl_socket *wrepl_socket,
476 struct wrepl_packet *req_packet,
477 struct wrepl_packet **reply_packet)
479 struct wrepl_request *req = wrepl_request_send(wrepl_socket, req_packet);
480 return wrepl_request_recv(req, mem_ctx, reply_packet);
485 setup an association - send
487 struct wrepl_request *wrepl_associate_send(struct wrepl_socket *wrepl_socket,
488 struct wrepl_associate *io)
490 struct wrepl_packet *packet;
491 struct wrepl_request *req;
493 packet = talloc_zero(wrepl_socket, struct wrepl_packet);
494 if (packet == NULL) return NULL;
496 packet->opcode = WREPL_OPCODE_BITS;
497 packet->mess_type = WREPL_START_ASSOCIATION;
498 packet->message.start.minor_version = 2;
499 packet->message.start.major_version = 5;
501 req = wrepl_request_send(wrepl_socket, packet);
509 setup an association - recv
511 NTSTATUS wrepl_associate_recv(struct wrepl_request *req,
512 struct wrepl_associate *io)
514 struct wrepl_packet *packet=NULL;
516 status = wrepl_request_recv(req, req->wrepl_socket, &packet);
517 NT_STATUS_NOT_OK_RETURN(status);
518 if (packet->mess_type != WREPL_START_ASSOCIATION_REPLY) {
519 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
521 if (NT_STATUS_IS_OK(status)) {
522 io->out.assoc_ctx = packet->message.start_reply.assoc_ctx;
529 setup an association - sync api
531 NTSTATUS wrepl_associate(struct wrepl_socket *wrepl_socket,
532 struct wrepl_associate *io)
534 struct wrepl_request *req = wrepl_associate_send(wrepl_socket, io);
535 return wrepl_associate_recv(req, io);
540 fetch the partner tables - send
542 struct wrepl_request *wrepl_pull_table_send(struct wrepl_socket *wrepl_socket,
543 struct wrepl_pull_table *io)
545 struct wrepl_packet *packet;
546 struct wrepl_request *req;
548 packet = talloc_zero(wrepl_socket, struct wrepl_packet);
549 if (packet == NULL) return NULL;
551 packet->opcode = WREPL_OPCODE_BITS;
552 packet->assoc_ctx = io->in.assoc_ctx;
553 packet->mess_type = WREPL_REPLICATION;
554 packet->message.replication.command = WREPL_REPL_TABLE_QUERY;
556 req = wrepl_request_send(wrepl_socket, packet);
565 fetch the partner tables - recv
567 NTSTATUS wrepl_pull_table_recv(struct wrepl_request *req,
569 struct wrepl_pull_table *io)
571 struct wrepl_packet *packet=NULL;
573 struct wrepl_table *table;
576 status = wrepl_request_recv(req, req->wrepl_socket, &packet);
577 NT_STATUS_NOT_OK_RETURN(status);
578 if (packet->mess_type != WREPL_REPLICATION) {
579 status = NT_STATUS_NETWORK_ACCESS_DENIED;
580 } else if (packet->message.replication.command != WREPL_REPL_TABLE_REPLY) {
581 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
583 if (!NT_STATUS_IS_OK(status)) goto failed;
585 table = &packet->message.replication.info.table;
586 io->out.num_partners = table->partner_count;
587 io->out.partners = talloc_steal(mem_ctx, table->partners);
588 for (i=0;i<io->out.num_partners;i++) {
589 talloc_steal(io->out.partners, io->out.partners[i].address);
599 fetch the partner table - sync api
601 NTSTATUS wrepl_pull_table(struct wrepl_socket *wrepl_socket,
603 struct wrepl_pull_table *io)
605 struct wrepl_request *req = wrepl_pull_table_send(wrepl_socket, io);
606 return wrepl_pull_table_recv(req, mem_ctx, io);
611 fetch the names for a WINS partner - send
613 struct wrepl_request *wrepl_pull_names_send(struct wrepl_socket *wrepl_socket,
614 struct wrepl_pull_names *io)
616 struct wrepl_packet *packet;
617 struct wrepl_request *req;
619 packet = talloc_zero(wrepl_socket, struct wrepl_packet);
620 if (packet == NULL) return NULL;
622 packet->opcode = WREPL_OPCODE_BITS;
623 packet->assoc_ctx = io->in.assoc_ctx;
624 packet->mess_type = WREPL_REPLICATION;
625 packet->message.replication.command = WREPL_REPL_SEND_REQUEST;
626 packet->message.replication.info.owner = io->in.partner;
628 req = wrepl_request_send(wrepl_socket, packet);
637 extract a nbt_name from a WINS name buffer
639 static NTSTATUS wrepl_extract_name(struct nbt_name *name,
641 uint8_t *namebuf, uint32_t len)
645 /* oh wow, what a nasty bug in windows ... */
646 if (namebuf[0] == 0x1b && len >= 16) {
647 namebuf[0] = namebuf[15];
652 make_nbt_name_client(name, talloc_strndup(mem_ctx, namebuf, len));
656 s = talloc_strndup(mem_ctx, namebuf, 15);
657 trim_string(s, NULL, " ");
659 name->type = namebuf[15];
661 name->scope = talloc_strndup(mem_ctx, namebuf+17, len-17);
670 fetch the names for a WINS partner - recv
672 NTSTATUS wrepl_pull_names_recv(struct wrepl_request *req,
674 struct wrepl_pull_names *io)
676 struct wrepl_packet *packet=NULL;
680 status = wrepl_request_recv(req, req->wrepl_socket, &packet);
681 NT_STATUS_NOT_OK_RETURN(status);
682 if (packet->mess_type != WREPL_REPLICATION ||
683 packet->message.replication.command != WREPL_REPL_SEND_REPLY) {
684 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
686 if (!NT_STATUS_IS_OK(status)) goto failed;
688 io->out.num_names = packet->message.replication.info.reply.num_names;
690 status = NT_STATUS_NO_MEMORY;
692 io->out.names = talloc_array(packet, struct wrepl_name, io->out.num_names);
693 if (io->out.names == NULL) goto failed;
695 /* convert the list of names and addresses to a sane format */
696 for (i=0;i<io->out.num_names;i++) {
697 struct wrepl_wins_name *wname = &packet->message.replication.info.reply.names[i];
698 struct wrepl_name *name = &io->out.names[i];
699 status = wrepl_extract_name(&name->name, io->out.names,
700 wname->name, wname->name_len);
701 if (!NT_STATUS_IS_OK(status)) goto failed;
703 /* trying to save 1 or 2 bytes on the wire isn't a good idea */
704 if (wname->flags & 2) {
707 name->num_addresses = wname->addresses.addresses.num_ips;
708 name->addresses = talloc_array(io->out.names,
709 struct wrepl_address,
710 name->num_addresses);
711 if (name->addresses == NULL) goto failed;
712 for (j=0;j<name->num_addresses;j++) {
713 name->addresses[j].owner =
714 talloc_steal(name->addresses,
715 wname->addresses.addresses.ips[j].owner);
716 name->addresses[j].address =
717 talloc_steal(name->addresses,
718 wname->addresses.addresses.ips[j].ip);
721 name->num_addresses = 1;
722 name->addresses = talloc(io->out.names, struct wrepl_address);
723 if (name->addresses == NULL) goto failed;
724 name->addresses[0].owner = io->in.partner.address;
725 name->addresses[0].address = talloc_steal(name->addresses,
726 wname->addresses.ip);
730 talloc_steal(mem_ctx, io->out.names);
731 status = NT_STATUS_OK;
741 fetch the names for a WINS partner - sync api
743 NTSTATUS wrepl_pull_names(struct wrepl_socket *wrepl_socket,
745 struct wrepl_pull_names *io)
747 struct wrepl_request *req = wrepl_pull_names_send(wrepl_socket, io);
748 return wrepl_pull_names_recv(req, mem_ctx, io);