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