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