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