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