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