Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-trivial
[bbaumbach/samba-autobuild/.git] / source4 / libcli / wrepl / winsrepl.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    low level WINS replication client code
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/util/dlinklist.h"
25 #include "lib/socket/socket.h"
26 #include "libcli/wrepl/winsrepl.h"
27 #include "librpc/gen_ndr/ndr_winsrepl.h"
28 #include "lib/stream/packet.h"
29 #include "libcli/composite/composite.h"
30 #include "system/network.h"
31 #include "lib/socket/netif.h"
32 #include "param/param.h"
33 #include "libcli/resolve/resolve.h"
34
35 static struct wrepl_request *wrepl_request_finished(struct wrepl_request *req, NTSTATUS status);
36
37 /*
38   mark all pending requests as dead - called when a socket error happens
39 */
40 static void wrepl_socket_dead(struct wrepl_socket *wrepl_socket, NTSTATUS status)
41 {
42         wrepl_socket->dead = true;
43
44         if (wrepl_socket->packet) {
45                 packet_recv_disable(wrepl_socket->packet);
46                 packet_set_fde(wrepl_socket->packet, NULL);
47                 packet_set_socket(wrepl_socket->packet, NULL);
48         }
49
50         if (wrepl_socket->event.fde) {
51                 talloc_free(wrepl_socket->event.fde);
52                 wrepl_socket->event.fde = NULL;
53         }
54
55         if (wrepl_socket->sock) {
56                 talloc_free(wrepl_socket->sock);
57                 wrepl_socket->sock = NULL;
58         }
59
60         if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
61                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
62         }
63         while (wrepl_socket->recv_queue) {
64                 struct wrepl_request *req = wrepl_socket->recv_queue;
65                 DLIST_REMOVE(wrepl_socket->recv_queue, req);
66                 wrepl_request_finished(req, status);
67         }
68
69         talloc_set_destructor(wrepl_socket, NULL);
70         if (wrepl_socket->free_skipped) {
71                 talloc_free(wrepl_socket);
72         }
73 }
74
75 static void wrepl_request_timeout_handler(struct event_context *ev, struct timed_event *te,
76                                           struct timeval t, void *ptr)
77 {
78         struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
79         wrepl_socket_dead(req->wrepl_socket, NT_STATUS_IO_TIMEOUT);
80 }
81
82 /*
83   handle recv events 
84 */
85 static NTSTATUS wrepl_finish_recv(void *private, DATA_BLOB packet_blob_in)
86 {
87         struct wrepl_socket *wrepl_socket = talloc_get_type(private, struct wrepl_socket);
88         struct wrepl_request *req = wrepl_socket->recv_queue;
89         DATA_BLOB blob;
90         enum ndr_err_code ndr_err;
91
92         if (!req) {
93                 DEBUG(1,("Received unexpected WINS packet of length %u!\n", 
94                          (unsigned)packet_blob_in.length));
95                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
96         }
97
98         req->packet = talloc(req, struct wrepl_packet);
99         NT_STATUS_HAVE_NO_MEMORY(req->packet);
100
101         blob.data = packet_blob_in.data + 4;
102         blob.length = packet_blob_in.length - 4;
103         
104         /* we have a full request - parse it */
105         ndr_err = ndr_pull_struct_blob(&blob, req->packet, wrepl_socket->iconv_convenience, req->packet,
106                                        (ndr_pull_flags_fn_t)ndr_pull_wrepl_packet);
107         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
108                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
109                 wrepl_request_finished(req, status);
110                 return NT_STATUS_OK;
111         }
112
113         if (DEBUGLVL(10)) {
114                 DEBUG(10,("Received WINS packet of length %u\n", 
115                           (unsigned)packet_blob_in.length));
116                 NDR_PRINT_DEBUG(wrepl_packet, req->packet);
117         }
118
119         wrepl_request_finished(req, NT_STATUS_OK);
120         return NT_STATUS_OK;
121 }
122
123 /*
124   handler for winrepl events
125 */
126 static void wrepl_handler(struct event_context *ev, struct fd_event *fde, 
127                           uint16_t flags, void *private)
128 {
129         struct wrepl_socket *wrepl_socket = talloc_get_type(private, 
130                                                             struct wrepl_socket);
131         if (flags & EVENT_FD_READ) {
132                 packet_recv(wrepl_socket->packet);
133                 return;
134         }
135         if (flags & EVENT_FD_WRITE) {
136                 packet_queue_run(wrepl_socket->packet);
137         }
138 }
139
140 static void wrepl_error(void *private, NTSTATUS status)
141 {
142         struct wrepl_socket *wrepl_socket = talloc_get_type(private, 
143                                                             struct wrepl_socket);
144         wrepl_socket_dead(wrepl_socket, status);
145 }
146
147
148 /*
149   destroy a wrepl_socket destructor
150 */
151 static int wrepl_socket_destructor(struct wrepl_socket *sock)
152 {
153         if (sock->dead) {
154                 sock->free_skipped = true;
155                 return -1;
156         }
157         wrepl_socket_dead(sock, NT_STATUS_LOCAL_DISCONNECT);
158         return 0;
159 }
160
161 /*
162   initialise a wrepl_socket. The event_ctx is optional, if provided then
163   operations will use that event context
164 */
165 struct wrepl_socket *wrepl_socket_init(TALLOC_CTX *mem_ctx, 
166                                        struct event_context *event_ctx,
167                                        struct smb_iconv_convenience *iconv_convenience)
168 {
169         struct wrepl_socket *wrepl_socket;
170         NTSTATUS status;
171
172         wrepl_socket = talloc_zero(mem_ctx, struct wrepl_socket);
173         if (!wrepl_socket) return NULL;
174
175         if (event_ctx == NULL) {
176                 wrepl_socket->event.ctx = event_context_init(wrepl_socket);
177         } else {
178                 wrepl_socket->event.ctx = talloc_reference(wrepl_socket, event_ctx);
179         }
180         if (!wrepl_socket->event.ctx) goto failed;
181
182         wrepl_socket->iconv_convenience = iconv_convenience;
183
184         status = socket_create("ip", SOCKET_TYPE_STREAM, &wrepl_socket->sock, 0);
185         if (!NT_STATUS_IS_OK(status)) goto failed;
186
187         talloc_steal(wrepl_socket, wrepl_socket->sock);
188
189         wrepl_socket->request_timeout   = WREPL_SOCKET_REQUEST_TIMEOUT;
190
191         talloc_set_destructor(wrepl_socket, wrepl_socket_destructor);
192
193         return wrepl_socket;
194
195 failed:
196         talloc_free(wrepl_socket);
197         return NULL;
198 }
199
200 /*
201   initialise a wrepl_socket from an already existing connection
202 */
203 struct wrepl_socket *wrepl_socket_merge(TALLOC_CTX *mem_ctx, 
204                                         struct event_context *event_ctx,
205                                         struct socket_context *sock,
206                                         struct packet_context *pack)
207 {
208         struct wrepl_socket *wrepl_socket;
209
210         wrepl_socket = talloc_zero(mem_ctx, struct wrepl_socket);
211         if (wrepl_socket == NULL) goto failed;
212
213         wrepl_socket->event.ctx = talloc_reference(wrepl_socket, event_ctx);
214         if (wrepl_socket->event.ctx == NULL) goto failed;
215
216         wrepl_socket->sock = sock;
217         talloc_steal(wrepl_socket, wrepl_socket->sock);
218
219
220         wrepl_socket->request_timeout   = WREPL_SOCKET_REQUEST_TIMEOUT;
221
222         wrepl_socket->event.fde = event_add_fd(wrepl_socket->event.ctx, wrepl_socket,
223                                                socket_get_fd(wrepl_socket->sock), 
224                                                EVENT_FD_READ,
225                                                wrepl_handler, wrepl_socket);
226         if (wrepl_socket->event.fde == NULL) {
227                 goto failed;
228         }
229
230         wrepl_socket->packet = pack;
231         talloc_steal(wrepl_socket, wrepl_socket->packet);
232         packet_set_private(wrepl_socket->packet, wrepl_socket);
233         packet_set_socket(wrepl_socket->packet, wrepl_socket->sock);
234         packet_set_callback(wrepl_socket->packet, wrepl_finish_recv);
235         packet_set_full_request(wrepl_socket->packet, packet_full_request_u32);
236         packet_set_error_handler(wrepl_socket->packet, wrepl_error);
237         packet_set_event_context(wrepl_socket->packet, wrepl_socket->event.ctx);
238         packet_set_fde(wrepl_socket->packet, wrepl_socket->event.fde);
239         packet_set_serialise(wrepl_socket->packet);
240
241         talloc_set_destructor(wrepl_socket, wrepl_socket_destructor);
242         
243         return wrepl_socket;
244
245 failed:
246         talloc_free(wrepl_socket);
247         return NULL;
248 }
249
250 /*
251   destroy a wrepl_request
252 */
253 static int wrepl_request_destructor(struct wrepl_request *req)
254 {
255         if (req->state == WREPL_REQUEST_RECV) {
256                 DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
257         }
258         req->state = WREPL_REQUEST_ERROR;
259         return 0;
260 }
261
262 /*
263   wait for a request to complete
264 */
265 static NTSTATUS wrepl_request_wait(struct wrepl_request *req)
266 {
267         NT_STATUS_HAVE_NO_MEMORY(req);
268         while (req->state < WREPL_REQUEST_DONE) {
269                 event_loop_once(req->wrepl_socket->event.ctx);
270         }
271         return req->status;
272 }
273
274 struct wrepl_connect_state {
275         struct composite_context *result;
276         struct wrepl_socket *wrepl_socket;
277         struct composite_context *creq;
278 };
279
280 /*
281   handler for winrepl connection completion
282 */
283 static void wrepl_connect_handler(struct composite_context *creq)
284 {
285         struct wrepl_connect_state *state = talloc_get_type(creq->async.private_data, 
286                                             struct wrepl_connect_state);
287         struct wrepl_socket *wrepl_socket = state->wrepl_socket;
288         struct composite_context *result = state->result;
289
290         result->status = socket_connect_recv(state->creq);
291         if (!composite_is_ok(result)) return;
292
293         wrepl_socket->event.fde = event_add_fd(wrepl_socket->event.ctx, wrepl_socket, 
294                                                socket_get_fd(wrepl_socket->sock), 
295                                                EVENT_FD_READ,
296                                                wrepl_handler, wrepl_socket);
297         if (composite_nomem(wrepl_socket->event.fde, result)) return;
298
299         /* setup the stream -> packet parser */
300         wrepl_socket->packet = packet_init(wrepl_socket);
301         if (composite_nomem(wrepl_socket->packet, result)) return;
302         packet_set_private(wrepl_socket->packet, wrepl_socket);
303         packet_set_socket(wrepl_socket->packet, wrepl_socket->sock);
304         packet_set_callback(wrepl_socket->packet, wrepl_finish_recv);
305         packet_set_full_request(wrepl_socket->packet, packet_full_request_u32);
306         packet_set_error_handler(wrepl_socket->packet, wrepl_error);
307         packet_set_event_context(wrepl_socket->packet, wrepl_socket->event.ctx);
308         packet_set_fde(wrepl_socket->packet, wrepl_socket->event.fde);
309         packet_set_serialise(wrepl_socket->packet);
310
311         composite_done(result);
312 }
313
314 const char *wrepl_best_ip(struct loadparm_context *lp_ctx, const char *peer_ip)
315 {
316         struct interface *ifaces;
317         load_interfaces(lp_ctx, lp_interfaces(lp_ctx), &ifaces);
318         return iface_best_ip(ifaces, peer_ip);
319 }
320
321
322 /*
323   connect a wrepl_socket to a WINS server
324 */
325 struct composite_context *wrepl_connect_send(struct wrepl_socket *wrepl_socket,
326                                              struct resolve_context *resolve_ctx,
327                                              const char *our_ip, const char *peer_ip)
328 {
329         struct composite_context *result;
330         struct wrepl_connect_state *state;
331         struct socket_address *peer, *us;
332
333         result = talloc_zero(wrepl_socket, struct composite_context);
334         if (!result) return NULL;
335
336         result->state           = COMPOSITE_STATE_IN_PROGRESS;
337         result->event_ctx       = wrepl_socket->event.ctx;
338
339         state = talloc_zero(result, struct wrepl_connect_state);
340         if (composite_nomem(state, result)) return result;
341         result->private_data    = state;
342         state->result           = result;
343         state->wrepl_socket     = wrepl_socket;
344
345         us = socket_address_from_strings(state, wrepl_socket->sock->backend_name, 
346                                          our_ip, 0);
347         if (composite_nomem(us, result)) return result;
348
349         peer = socket_address_from_strings(state, wrepl_socket->sock->backend_name, 
350                                            peer_ip, WINS_REPLICATION_PORT);
351         if (composite_nomem(peer, result)) return result;
352
353         state->creq = socket_connect_send(wrepl_socket->sock, us, peer,
354                                           0, resolve_ctx,
355                                           wrepl_socket->event.ctx);
356         composite_continue(result, state->creq, wrepl_connect_handler, state);
357         return result;
358 }
359
360 /*
361   connect a wrepl_socket to a WINS server - recv side
362 */
363 NTSTATUS wrepl_connect_recv(struct composite_context *result)
364 {
365         struct wrepl_connect_state *state = talloc_get_type(result->private_data,
366                                             struct wrepl_connect_state);
367         struct wrepl_socket *wrepl_socket = state->wrepl_socket;
368         NTSTATUS status = composite_wait(result);
369
370         if (!NT_STATUS_IS_OK(status)) {
371                 wrepl_socket_dead(wrepl_socket, status);
372         }
373
374         talloc_free(result);
375         return status;
376 }
377
378 /*
379   connect a wrepl_socket to a WINS server - sync API
380 */
381 NTSTATUS wrepl_connect(struct wrepl_socket *wrepl_socket, struct resolve_context *resolve_ctx,
382                        const char *our_ip, const char *peer_ip)
383 {
384         struct composite_context *c_req = wrepl_connect_send(wrepl_socket, resolve_ctx, our_ip, peer_ip);
385         return wrepl_connect_recv(c_req);
386 }
387
388 /* 
389    callback from wrepl_request_trigger() 
390 */
391 static void wrepl_request_trigger_handler(struct event_context *ev, struct timed_event *te,
392                                           struct timeval t, void *ptr)
393 {
394         struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
395         if (req->async.fn) {
396                 req->async.fn(req);
397         }
398 }
399
400 /*
401   trigger an immediate event on a wrepl_request
402   the return value should only be used in wrepl_request_send()
403   this is the only place where req->trigger is true
404 */
405 static struct wrepl_request *wrepl_request_finished(struct wrepl_request *req, NTSTATUS status)
406 {
407         struct timed_event *te;
408
409         if (req->state == WREPL_REQUEST_RECV) {
410                 DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
411         }
412
413         if (!NT_STATUS_IS_OK(status)) {
414                 req->state      = WREPL_REQUEST_ERROR;
415         } else {
416                 req->state      = WREPL_REQUEST_DONE;
417         }
418
419         req->status     = status;
420
421         if (req->trigger) {
422                 req->trigger = false;
423                 /* a zero timeout means immediate */
424                 te = event_add_timed(req->wrepl_socket->event.ctx,
425                                      req, timeval_zero(),
426                                      wrepl_request_trigger_handler, req);
427                 if (!te) {
428                         talloc_free(req);
429                         return NULL;
430                 }
431                 return req;
432         }
433
434         if (req->async.fn) {
435                 req->async.fn(req);
436         }
437         return NULL;
438 }
439
440 struct wrepl_send_ctrl_state {
441         struct wrepl_send_ctrl ctrl;
442         struct wrepl_request *req;
443         struct wrepl_socket *wrepl_sock;
444 };
445
446 static int wrepl_send_ctrl_destructor(struct wrepl_send_ctrl_state *s)
447 {
448         struct wrepl_request *req = s->wrepl_sock->recv_queue;
449
450         /* check if the request is still in WREPL_STATE_RECV,
451          * we need this here because the caller has may called 
452          * talloc_free(req) and wrepl_send_ctrl_state isn't
453          * a talloc child of the request, so our s->req pointer
454          * is maybe invalid!
455          */
456         for (; req; req = req->next) {
457                 if (req == s->req) break;
458         }
459         if (!req) return 0;
460
461         /* here, we need to make sure the async request handler is called
462          * later in the next event_loop and now now
463          */
464         req->trigger = true;
465         wrepl_request_finished(req, NT_STATUS_OK);
466
467         if (s->ctrl.disconnect_after_send) {
468                 wrepl_socket_dead(s->wrepl_sock, NT_STATUS_LOCAL_DISCONNECT);
469         }
470
471         return 0;
472 }
473
474 /*
475   send a generic wins replication request
476 */
477 struct wrepl_request *wrepl_request_send(struct wrepl_socket *wrepl_socket,
478                                          struct wrepl_packet *packet,
479                                          struct wrepl_send_ctrl *ctrl)
480 {
481         struct wrepl_request *req;
482         struct wrepl_wrap wrap;
483         DATA_BLOB blob;
484         NTSTATUS status;
485         enum ndr_err_code ndr_err;
486
487         req = talloc_zero(wrepl_socket, struct wrepl_request);
488         if (!req) return NULL;
489         req->wrepl_socket = wrepl_socket;
490         req->state        = WREPL_REQUEST_RECV;
491         req->trigger      = true;
492
493         DLIST_ADD_END(wrepl_socket->recv_queue, req, struct wrepl_request *);
494         talloc_set_destructor(req, wrepl_request_destructor);
495
496         if (wrepl_socket->dead) {
497                 return wrepl_request_finished(req, NT_STATUS_INVALID_CONNECTION);
498         }
499
500         wrap.packet = *packet;
501         ndr_err = ndr_push_struct_blob(&blob, req, wrepl_socket->iconv_convenience, &wrap, 
502                                        (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
503         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
504                 status = ndr_map_error2ntstatus(ndr_err);
505                 return wrepl_request_finished(req, status);
506         }
507
508         if (DEBUGLVL(10)) {
509                 DEBUG(10,("Sending WINS packet of length %u\n", 
510                           (unsigned)blob.length));
511                 NDR_PRINT_DEBUG(wrepl_packet, &wrap.packet);
512         }
513
514         if (wrepl_socket->request_timeout > 0) {
515                 req->te = event_add_timed(wrepl_socket->event.ctx, req, 
516                                           timeval_current_ofs(wrepl_socket->request_timeout, 0), 
517                                           wrepl_request_timeout_handler, req);
518                 if (!req->te) return wrepl_request_finished(req, NT_STATUS_NO_MEMORY);
519         }
520
521         if (ctrl && (ctrl->send_only || ctrl->disconnect_after_send)) {
522                 struct wrepl_send_ctrl_state *s = talloc(blob.data, struct wrepl_send_ctrl_state);
523                 if (!s) return wrepl_request_finished(req, NT_STATUS_NO_MEMORY);
524                 s->ctrl         = *ctrl;
525                 s->req          = req;
526                 s->wrepl_sock   = wrepl_socket;
527                 talloc_set_destructor(s, wrepl_send_ctrl_destructor);
528         }
529
530         status = packet_send(wrepl_socket->packet, blob);
531         if (!NT_STATUS_IS_OK(status)) {
532                 return wrepl_request_finished(req, status);
533         }
534
535         req->trigger = false;
536         return req;
537 }
538
539 /*
540   receive a generic WINS replication reply
541 */
542 NTSTATUS wrepl_request_recv(struct wrepl_request *req,
543                             TALLOC_CTX *mem_ctx,
544                             struct wrepl_packet **packet)
545 {
546         NTSTATUS status = wrepl_request_wait(req);
547         if (NT_STATUS_IS_OK(status) && packet) {
548                 *packet = talloc_steal(mem_ctx, req->packet);
549         }
550         talloc_free(req);
551         return status;
552 }
553
554 /*
555   a full WINS replication request/response
556 */
557 NTSTATUS wrepl_request(struct wrepl_socket *wrepl_socket,
558                        TALLOC_CTX *mem_ctx,
559                        struct wrepl_packet *req_packet,
560                        struct wrepl_packet **reply_packet)
561 {
562         struct wrepl_request *req = wrepl_request_send(wrepl_socket, req_packet, NULL);
563         return wrepl_request_recv(req, mem_ctx, reply_packet);
564 }
565
566
567 /*
568   setup an association - send
569 */
570 struct wrepl_request *wrepl_associate_send(struct wrepl_socket *wrepl_socket,
571                                            struct wrepl_associate *io)
572 {
573         struct wrepl_packet *packet;
574         struct wrepl_request *req;
575
576         packet = talloc_zero(wrepl_socket, struct wrepl_packet);
577         if (packet == NULL) return NULL;
578
579         packet->opcode                      = WREPL_OPCODE_BITS;
580         packet->mess_type                   = WREPL_START_ASSOCIATION;
581         packet->message.start.minor_version = 2;
582         packet->message.start.major_version = 5;
583
584         /*
585          * nt4 uses 41 bytes for the start_association call
586          * so do it the same and as we don't know th emeanings of this bytes
587          * we just send zeros and nt4, w2k and w2k3 seems to be happy with this
588          *
589          * if we don't do this nt4 uses an old version of the wins replication protocol
590          * and that would break nt4 <-> samba replication
591          */
592         packet->padding = data_blob_talloc(packet, NULL, 21);
593         if (packet->padding.data == NULL) {
594                 talloc_free(packet);
595                 return NULL;
596         }
597         memset(packet->padding.data, 0, packet->padding.length);
598
599         req = wrepl_request_send(wrepl_socket, packet, NULL);
600
601         talloc_free(packet);
602
603         return req;     
604 }
605
606 /*
607   setup an association - recv
608 */
609 NTSTATUS wrepl_associate_recv(struct wrepl_request *req,
610                               struct wrepl_associate *io)
611 {
612         struct wrepl_packet *packet=NULL;
613         NTSTATUS status;
614         status = wrepl_request_recv(req, req->wrepl_socket, &packet);
615         NT_STATUS_NOT_OK_RETURN(status);
616         if (packet->mess_type != WREPL_START_ASSOCIATION_REPLY) {
617                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
618         }
619         if (NT_STATUS_IS_OK(status)) {
620                 io->out.assoc_ctx = packet->message.start_reply.assoc_ctx;
621         }
622         talloc_free(packet);
623         return status;
624 }
625
626 /*
627   setup an association - sync api
628 */
629 NTSTATUS wrepl_associate(struct wrepl_socket *wrepl_socket,
630                          struct wrepl_associate *io)
631 {
632         struct wrepl_request *req = wrepl_associate_send(wrepl_socket, io);
633         return wrepl_associate_recv(req, io);
634 }
635
636
637 /*
638   stop an association - send
639 */
640 struct wrepl_request *wrepl_associate_stop_send(struct wrepl_socket *wrepl_socket,
641                                                 struct wrepl_associate_stop *io)
642 {
643         struct wrepl_packet *packet;
644         struct wrepl_request *req;
645         struct wrepl_send_ctrl ctrl;
646
647         packet = talloc_zero(wrepl_socket, struct wrepl_packet);
648         if (packet == NULL) return NULL;
649
650         packet->opcode                  = WREPL_OPCODE_BITS;
651         packet->assoc_ctx               = io->in.assoc_ctx;
652         packet->mess_type               = WREPL_STOP_ASSOCIATION;
653         packet->message.stop.reason     = io->in.reason;
654
655         ZERO_STRUCT(ctrl);
656         if (io->in.reason == 0) {
657                 ctrl.send_only                  = true;
658                 ctrl.disconnect_after_send      = true;
659         }
660
661         req = wrepl_request_send(wrepl_socket, packet, &ctrl);
662
663         talloc_free(packet);
664
665         return req;     
666 }
667
668 /*
669   stop an association - recv
670 */
671 NTSTATUS wrepl_associate_stop_recv(struct wrepl_request *req,
672                                    struct wrepl_associate_stop *io)
673 {
674         struct wrepl_packet *packet=NULL;
675         NTSTATUS status;
676         status = wrepl_request_recv(req, req->wrepl_socket, &packet);
677         NT_STATUS_NOT_OK_RETURN(status);
678         talloc_free(packet);
679         return status;
680 }
681
682 /*
683   setup an association - sync api
684 */
685 NTSTATUS wrepl_associate_stop(struct wrepl_socket *wrepl_socket,
686                               struct wrepl_associate_stop *io)
687 {
688         struct wrepl_request *req = wrepl_associate_stop_send(wrepl_socket, io);
689         return wrepl_associate_stop_recv(req, io);
690 }
691
692 /*
693   fetch the partner tables - send
694 */
695 struct wrepl_request *wrepl_pull_table_send(struct wrepl_socket *wrepl_socket,
696                                             struct wrepl_pull_table *io)
697 {
698         struct wrepl_packet *packet;
699         struct wrepl_request *req;
700
701         packet = talloc_zero(wrepl_socket, struct wrepl_packet);
702         if (packet == NULL) return NULL;
703
704         packet->opcode                      = WREPL_OPCODE_BITS;
705         packet->assoc_ctx                   = io->in.assoc_ctx;
706         packet->mess_type                   = WREPL_REPLICATION;
707         packet->message.replication.command = WREPL_REPL_TABLE_QUERY;
708
709         req = wrepl_request_send(wrepl_socket, packet, NULL);
710
711         talloc_free(packet);
712
713         return req;     
714 }
715
716
717 /*
718   fetch the partner tables - recv
719 */
720 NTSTATUS wrepl_pull_table_recv(struct wrepl_request *req,
721                                TALLOC_CTX *mem_ctx,
722                                struct wrepl_pull_table *io)
723 {
724         struct wrepl_packet *packet=NULL;
725         NTSTATUS status;
726         struct wrepl_table *table;
727         int i;
728
729         status = wrepl_request_recv(req, req->wrepl_socket, &packet);
730         NT_STATUS_NOT_OK_RETURN(status);
731         if (packet->mess_type != WREPL_REPLICATION) {
732                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
733         } else if (packet->message.replication.command != WREPL_REPL_TABLE_REPLY) {
734                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
735         }
736         if (!NT_STATUS_IS_OK(status)) goto failed;
737
738         table = &packet->message.replication.info.table;
739         io->out.num_partners = table->partner_count;
740         io->out.partners = talloc_steal(mem_ctx, table->partners);
741         for (i=0;i<io->out.num_partners;i++) {
742                 talloc_steal(io->out.partners, io->out.partners[i].address);
743         }
744
745 failed:
746         talloc_free(packet);
747         return status;
748 }
749
750
751 /*
752   fetch the partner table - sync api
753 */
754 NTSTATUS wrepl_pull_table(struct wrepl_socket *wrepl_socket,
755                           TALLOC_CTX *mem_ctx,
756                           struct wrepl_pull_table *io)
757 {
758         struct wrepl_request *req = wrepl_pull_table_send(wrepl_socket, io);
759         return wrepl_pull_table_recv(req, mem_ctx, io);
760 }
761
762
763 /*
764   fetch the names for a WINS partner - send
765 */
766 struct wrepl_request *wrepl_pull_names_send(struct wrepl_socket *wrepl_socket,
767                                             struct wrepl_pull_names *io)
768 {
769         struct wrepl_packet *packet;
770         struct wrepl_request *req;
771
772         packet = talloc_zero(wrepl_socket, struct wrepl_packet);
773         if (packet == NULL) return NULL;
774
775         packet->opcode                         = WREPL_OPCODE_BITS;
776         packet->assoc_ctx                      = io->in.assoc_ctx;
777         packet->mess_type                      = WREPL_REPLICATION;
778         packet->message.replication.command    = WREPL_REPL_SEND_REQUEST;
779         packet->message.replication.info.owner = io->in.partner;
780
781         req = wrepl_request_send(wrepl_socket, packet, NULL);
782
783         talloc_free(packet);
784
785         return req;     
786 }
787
788 /*
789   fetch the names for a WINS partner - recv
790 */
791 NTSTATUS wrepl_pull_names_recv(struct wrepl_request *req,
792                                TALLOC_CTX *mem_ctx,
793                                struct wrepl_pull_names *io)
794 {
795         struct wrepl_packet *packet=NULL;
796         NTSTATUS status;
797         int i;
798
799         status = wrepl_request_recv(req, req->wrepl_socket, &packet);
800         NT_STATUS_NOT_OK_RETURN(status);
801         if (packet->mess_type != WREPL_REPLICATION ||
802             packet->message.replication.command != WREPL_REPL_SEND_REPLY) {
803                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
804         }
805         if (!NT_STATUS_IS_OK(status)) goto failed;
806
807         io->out.num_names = packet->message.replication.info.reply.num_names;
808
809         io->out.names = talloc_array(packet, struct wrepl_name, io->out.num_names);
810         if (io->out.names == NULL) goto nomem;
811
812         /* convert the list of names and addresses to a sane format */
813         for (i=0;i<io->out.num_names;i++) {
814                 struct wrepl_wins_name *wname = &packet->message.replication.info.reply.names[i];
815                 struct wrepl_name *name = &io->out.names[i];
816
817                 name->name      = *wname->name;
818                 talloc_steal(io->out.names, wname->name);
819                 name->type      = WREPL_NAME_TYPE(wname->flags);
820                 name->state     = WREPL_NAME_STATE(wname->flags);
821                 name->node      = WREPL_NAME_NODE(wname->flags);
822                 name->is_static = WREPL_NAME_IS_STATIC(wname->flags);
823                 name->raw_flags = wname->flags;
824                 name->version_id= wname->id;
825                 name->owner     = talloc_strdup(io->out.names, io->in.partner.address);
826                 if (name->owner == NULL) goto nomem;
827
828                 /* trying to save 1 or 2 bytes on the wire isn't a good idea */
829                 if (wname->flags & 2) {
830                         int j;
831
832                         name->num_addresses = wname->addresses.addresses.num_ips;
833                         name->addresses = talloc_array(io->out.names, 
834                                                        struct wrepl_address, 
835                                                        name->num_addresses);
836                         if (name->addresses == NULL) goto nomem;
837                         for (j=0;j<name->num_addresses;j++) {
838                                 name->addresses[j].owner = 
839                                         talloc_steal(name->addresses, 
840                                                      wname->addresses.addresses.ips[j].owner);
841                                 name->addresses[j].address = 
842                                         talloc_steal(name->addresses, 
843                                                      wname->addresses.addresses.ips[j].ip);
844                         }
845                 } else {
846                         name->num_addresses = 1;
847                         name->addresses = talloc(io->out.names, struct wrepl_address);
848                         if (name->addresses == NULL) goto nomem;
849                         name->addresses[0].owner = talloc_strdup(name->addresses,io->in.partner.address);
850                         if (name->addresses[0].owner == NULL) goto nomem;
851                         name->addresses[0].address = talloc_steal(name->addresses,
852                                                                   wname->addresses.ip);
853                 }
854         }
855
856         talloc_steal(mem_ctx, io->out.names);
857         talloc_free(packet);
858         return NT_STATUS_OK;
859 nomem:
860         status = NT_STATUS_NO_MEMORY;
861 failed:
862         talloc_free(packet);
863         return status;
864 }
865
866
867
868 /*
869   fetch the names for a WINS partner - sync api
870 */
871 NTSTATUS wrepl_pull_names(struct wrepl_socket *wrepl_socket,
872                           TALLOC_CTX *mem_ctx,
873                           struct wrepl_pull_names *io)
874 {
875         struct wrepl_request *req = wrepl_pull_names_send(wrepl_socket, io);
876         return wrepl_pull_names_recv(req, mem_ctx, io);
877 }