s4:libcli/wrepl: convert wrepl_associate_stop_send to tevent_req
[bbaumbach/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 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 "lib/util/tevent_ntstatus.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 tevent_context *ev, struct tevent_timer *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, DATA_BLOB packet_blob_in)
86 {
87         struct wrepl_socket *wrepl_socket = talloc_get_type(private_data, 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, wrepl_socket->iconv_convenience, 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 tevent_context *ev, struct tevent_fd *fde, 
127                           uint16_t flags, void *private_data)
128 {
129         struct wrepl_socket *wrepl_socket = talloc_get_type(private_data,
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_data, NTSTATUS status)
141 {
142         struct wrepl_socket *wrepl_socket = talloc_get_type(private_data,
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 tevent_context *event_ctx,
167                                        struct smb_iconv_convenience *iconv_convenience)
168 {
169         struct wrepl_socket *wrepl_socket;
170         NTSTATUS status;
171
172         wrepl_socket = talloc_zero(mem_ctx, struct wrepl_socket);
173         if (!wrepl_socket) return NULL;
174
175         wrepl_socket->event.ctx = event_ctx;
176         if (!wrepl_socket->event.ctx) goto failed;
177
178         wrepl_socket->iconv_convenience = iconv_convenience;
179
180         status = socket_create("ip", SOCKET_TYPE_STREAM, &wrepl_socket->sock, 0);
181         if (!NT_STATUS_IS_OK(status)) goto failed;
182
183         talloc_steal(wrepl_socket, wrepl_socket->sock);
184
185         wrepl_socket->request_timeout   = WREPL_SOCKET_REQUEST_TIMEOUT;
186
187         talloc_set_destructor(wrepl_socket, wrepl_socket_destructor);
188
189         return wrepl_socket;
190
191 failed:
192         talloc_free(wrepl_socket);
193         return NULL;
194 }
195
196 /*
197   initialise a wrepl_socket from an already existing connection
198 */
199 struct wrepl_socket *wrepl_socket_merge(TALLOC_CTX *mem_ctx, 
200                                         struct tevent_context *event_ctx,
201                                         struct socket_context *sock,
202                                         struct packet_context *pack)
203 {
204         struct wrepl_socket *wrepl_socket;
205
206         wrepl_socket = talloc_zero(mem_ctx, struct wrepl_socket);
207         if (wrepl_socket == NULL) goto failed;
208
209         wrepl_socket->event.ctx = event_ctx;
210         if (wrepl_socket->event.ctx == NULL) goto failed;
211
212         wrepl_socket->sock = sock;
213         talloc_steal(wrepl_socket, wrepl_socket->sock);
214
215
216         wrepl_socket->request_timeout   = WREPL_SOCKET_REQUEST_TIMEOUT;
217
218         wrepl_socket->event.fde = event_add_fd(wrepl_socket->event.ctx, wrepl_socket,
219                                                socket_get_fd(wrepl_socket->sock), 
220                                                EVENT_FD_READ,
221                                                wrepl_handler, wrepl_socket);
222         if (wrepl_socket->event.fde == NULL) {
223                 goto failed;
224         }
225
226         wrepl_socket->packet = pack;
227         talloc_steal(wrepl_socket, wrepl_socket->packet);
228         packet_set_private(wrepl_socket->packet, wrepl_socket);
229         packet_set_socket(wrepl_socket->packet, wrepl_socket->sock);
230         packet_set_callback(wrepl_socket->packet, wrepl_finish_recv);
231         packet_set_full_request(wrepl_socket->packet, packet_full_request_u32);
232         packet_set_error_handler(wrepl_socket->packet, wrepl_error);
233         packet_set_event_context(wrepl_socket->packet, wrepl_socket->event.ctx);
234         packet_set_fde(wrepl_socket->packet, wrepl_socket->event.fde);
235         packet_set_serialise(wrepl_socket->packet);
236
237         talloc_set_destructor(wrepl_socket, wrepl_socket_destructor);
238         
239         return wrepl_socket;
240
241 failed:
242         talloc_free(wrepl_socket);
243         return NULL;
244 }
245
246 /*
247   destroy a wrepl_request
248 */
249 static int wrepl_request_destructor(struct wrepl_request *req)
250 {
251         if (req->state == WREPL_REQUEST_RECV) {
252                 DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
253         }
254         req->state = WREPL_REQUEST_ERROR;
255         return 0;
256 }
257
258 /*
259   wait for a request to complete
260 */
261 static NTSTATUS wrepl_request_wait(struct wrepl_request *req)
262 {
263         NT_STATUS_HAVE_NO_MEMORY(req);
264         while (req->state < WREPL_REQUEST_DONE) {
265                 event_loop_once(req->wrepl_socket->event.ctx);
266         }
267         return req->status;
268 }
269
270 const char *wrepl_best_ip(struct loadparm_context *lp_ctx, const char *peer_ip)
271 {
272         struct interface *ifaces;
273         load_interfaces(lp_ctx, lp_interfaces(lp_ctx), &ifaces);
274         return iface_best_ip(ifaces, peer_ip);
275 }
276
277 struct wrepl_connect_state {
278         struct wrepl_socket *wrepl_socket;
279 };
280
281 static void wrepl_connect_handler(struct composite_context *creq);
282
283 struct tevent_req *wrepl_connect_send(TALLOC_CTX *mem_ctx,
284                                       struct tevent_context *ev,
285                                       struct wrepl_socket *wrepl_socket,
286                                       const char *our_ip, const char *peer_ip)
287 {
288         struct tevent_req *req;
289         struct wrepl_connect_state *state;
290         struct composite_context *subreq;
291         struct socket_address *peer, *us;
292
293         req = tevent_req_create(mem_ctx, &state,
294                                 struct wrepl_connect_state);
295         if (req == NULL) {
296                 return NULL;
297         }
298
299         state->wrepl_socket     = wrepl_socket;
300
301         us = socket_address_from_strings(state, wrepl_socket->sock->backend_name,
302                                          our_ip, 0);
303         if (tevent_req_nomem(us, req)) {
304                 return tevent_req_post(req, ev);
305         }
306
307         peer = socket_address_from_strings(state, wrepl_socket->sock->backend_name,
308                                            peer_ip, WINS_REPLICATION_PORT);
309         if (tevent_req_nomem(peer, req)) {
310                 return tevent_req_post(req, ev);
311         }
312
313         subreq = socket_connect_send(wrepl_socket->sock, us, peer,
314                                      0, wrepl_socket->event.ctx);
315         if (tevent_req_nomem(subreq, req)) {
316                 return tevent_req_post(req, ev);
317         }
318
319         subreq->async.fn = wrepl_connect_handler;
320         subreq->async.private_data = req;
321
322         return req;
323 }
324
325 static void wrepl_connect_handler(struct composite_context *subreq)
326 {
327         struct tevent_req *req = talloc_get_type_abort(subreq->async.private_data,
328                                  struct tevent_req);
329         struct wrepl_connect_state *state = tevent_req_data(req,
330                                             struct wrepl_connect_state);
331         struct wrepl_socket *wrepl_socket = state->wrepl_socket;
332         NTSTATUS status;
333
334         status = socket_connect_recv(subreq);
335         if (!NT_STATUS_IS_OK(status)) {
336                 tevent_req_nterror(req, status);
337                 return;
338         }
339
340         wrepl_socket->event.fde = event_add_fd(wrepl_socket->event.ctx, wrepl_socket, 
341                                                socket_get_fd(wrepl_socket->sock), 
342                                                EVENT_FD_READ,
343                                                wrepl_handler, wrepl_socket);
344         if (tevent_req_nomem(wrepl_socket->event.fde, req)) {
345                 return;
346         }
347
348         /* setup the stream -> packet parser */
349         wrepl_socket->packet = packet_init(wrepl_socket);
350         if (tevent_req_nomem(wrepl_socket->packet, req)) {
351                 return;
352         }
353         packet_set_private(wrepl_socket->packet, wrepl_socket);
354         packet_set_socket(wrepl_socket->packet, wrepl_socket->sock);
355         packet_set_callback(wrepl_socket->packet, wrepl_finish_recv);
356         packet_set_full_request(wrepl_socket->packet, packet_full_request_u32);
357         packet_set_error_handler(wrepl_socket->packet, wrepl_error);
358         packet_set_event_context(wrepl_socket->packet, wrepl_socket->event.ctx);
359         packet_set_fde(wrepl_socket->packet, wrepl_socket->event.fde);
360         packet_set_serialise(wrepl_socket->packet);
361
362         tevent_req_done(req);
363 }
364
365 /*
366   connect a wrepl_socket to a WINS server - recv side
367 */
368 NTSTATUS wrepl_connect_recv(struct tevent_req *req)
369 {
370         struct wrepl_connect_state *state = tevent_req_data(req,
371                                             struct wrepl_connect_state);
372         struct wrepl_socket *wrepl_socket = state->wrepl_socket;
373         NTSTATUS status;
374
375         if (tevent_req_is_nterror(req, &status)) {
376                 wrepl_socket_dead(wrepl_socket, status);
377                 tevent_req_received(req);
378                 return status;
379         }
380
381         tevent_req_received(req);
382         return NT_STATUS_OK;
383 }
384
385 /*
386   connect a wrepl_socket to a WINS server - sync API
387 */
388 NTSTATUS wrepl_connect(struct wrepl_socket *wrepl_socket,
389                        const char *our_ip, const char *peer_ip)
390 {
391         struct tevent_req *subreq;
392         bool ok;
393         NTSTATUS status;
394
395         subreq = wrepl_connect_send(wrepl_socket, wrepl_socket->event.ctx,
396                                     wrepl_socket, our_ip, peer_ip);
397         NT_STATUS_HAVE_NO_MEMORY(subreq);
398
399         ok = tevent_req_poll(subreq, wrepl_socket->event.ctx);
400         if (!ok) {
401                 TALLOC_FREE(subreq);
402                 return NT_STATUS_INTERNAL_ERROR;
403         }
404
405         status = wrepl_connect_recv(subreq);
406         TALLOC_FREE(subreq);
407         NT_STATUS_NOT_OK_RETURN(status);
408
409         return NT_STATUS_OK;
410 }
411
412 /* 
413    callback from wrepl_request_trigger() 
414 */
415 static void wrepl_request_trigger_handler(struct tevent_context *ev, struct tevent_timer *te,
416                                           struct timeval t, void *ptr)
417 {
418         struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
419         if (req->async.fn) {
420                 req->async.fn(req);
421         }
422 }
423
424 /*
425   trigger an immediate event on a wrepl_request
426   the return value should only be used in wrepl_request_send()
427   this is the only place where req->trigger is true
428 */
429 static struct wrepl_request *wrepl_request_finished(struct wrepl_request *req, NTSTATUS status)
430 {
431         struct tevent_timer *te;
432
433         if (req->state == WREPL_REQUEST_RECV) {
434                 DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
435         }
436
437         if (!NT_STATUS_IS_OK(status)) {
438                 req->state      = WREPL_REQUEST_ERROR;
439         } else {
440                 req->state      = WREPL_REQUEST_DONE;
441         }
442
443         req->status     = status;
444
445         if (req->trigger) {
446                 req->trigger = false;
447                 /* a zero timeout means immediate */
448                 te = event_add_timed(req->wrepl_socket->event.ctx,
449                                      req, timeval_zero(),
450                                      wrepl_request_trigger_handler, req);
451                 if (!te) {
452                         talloc_free(req);
453                         return NULL;
454                 }
455                 return req;
456         }
457
458         if (req->async.fn) {
459                 req->async.fn(req);
460         }
461         return NULL;
462 }
463
464 struct wrepl_send_ctrl_state {
465         struct wrepl_send_ctrl ctrl;
466         struct wrepl_request *req;
467         struct wrepl_socket *wrepl_sock;
468 };
469
470 static int wrepl_send_ctrl_destructor(struct wrepl_send_ctrl_state *s)
471 {
472         struct wrepl_request *req = s->wrepl_sock->recv_queue;
473
474         /* check if the request is still in WREPL_STATE_RECV,
475          * we need this here because the caller has may called 
476          * talloc_free(req) and wrepl_send_ctrl_state isn't
477          * a talloc child of the request, so our s->req pointer
478          * is maybe invalid!
479          */
480         for (; req; req = req->next) {
481                 if (req == s->req) break;
482         }
483         if (!req) return 0;
484
485         /* here, we need to make sure the async request handler is called
486          * later in the next event_loop and now now
487          */
488         req->trigger = true;
489         wrepl_request_finished(req, NT_STATUS_OK);
490
491         if (s->ctrl.disconnect_after_send) {
492                 wrepl_socket_dead(s->wrepl_sock, NT_STATUS_LOCAL_DISCONNECT);
493         }
494
495         return 0;
496 }
497
498 /*
499   send a generic wins replication request
500 */
501 struct wrepl_request *wrepl_request_send(struct wrepl_socket *wrepl_socket,
502                                          struct wrepl_packet *packet,
503                                          struct wrepl_send_ctrl *ctrl)
504 {
505         struct wrepl_request *req;
506         struct wrepl_wrap wrap;
507         DATA_BLOB blob;
508         NTSTATUS status;
509         enum ndr_err_code ndr_err;
510
511         req = talloc_zero(wrepl_socket, struct wrepl_request);
512         if (!req) return NULL;
513         req->wrepl_socket = wrepl_socket;
514         req->state        = WREPL_REQUEST_RECV;
515         req->trigger      = true;
516
517         DLIST_ADD_END(wrepl_socket->recv_queue, req, struct wrepl_request *);
518         talloc_set_destructor(req, wrepl_request_destructor);
519
520         if (wrepl_socket->dead) {
521                 return wrepl_request_finished(req, NT_STATUS_INVALID_CONNECTION);
522         }
523
524         wrap.packet = *packet;
525         ndr_err = ndr_push_struct_blob(&blob, req, wrepl_socket->iconv_convenience, &wrap, 
526                                        (ndr_push_flags_fn_t)ndr_push_wrepl_wrap);
527         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
528                 status = ndr_map_error2ntstatus(ndr_err);
529                 return wrepl_request_finished(req, status);
530         }
531
532         if (DEBUGLVL(10)) {
533                 DEBUG(10,("Sending WINS packet of length %u\n", 
534                           (unsigned)blob.length));
535                 NDR_PRINT_DEBUG(wrepl_packet, &wrap.packet);
536         }
537
538         if (wrepl_socket->request_timeout > 0) {
539                 req->te = event_add_timed(wrepl_socket->event.ctx, req, 
540                                           timeval_current_ofs(wrepl_socket->request_timeout, 0), 
541                                           wrepl_request_timeout_handler, req);
542                 if (!req->te) return wrepl_request_finished(req, NT_STATUS_NO_MEMORY);
543         }
544
545         if (ctrl && (ctrl->send_only || ctrl->disconnect_after_send)) {
546                 struct wrepl_send_ctrl_state *s = talloc(blob.data, struct wrepl_send_ctrl_state);
547                 if (!s) return wrepl_request_finished(req, NT_STATUS_NO_MEMORY);
548                 s->ctrl         = *ctrl;
549                 s->req          = req;
550                 s->wrepl_sock   = wrepl_socket;
551                 talloc_set_destructor(s, wrepl_send_ctrl_destructor);
552         }
553
554         status = packet_send(wrepl_socket->packet, blob);
555         if (!NT_STATUS_IS_OK(status)) {
556                 return wrepl_request_finished(req, status);
557         }
558
559         req->trigger = false;
560         return req;
561 }
562
563 /*
564   receive a generic WINS replication reply
565 */
566 NTSTATUS wrepl_request_recv(struct wrepl_request *req,
567                             TALLOC_CTX *mem_ctx,
568                             struct wrepl_packet **packet)
569 {
570         NTSTATUS status = wrepl_request_wait(req);
571         if (NT_STATUS_IS_OK(status) && packet) {
572                 *packet = talloc_steal(mem_ctx, req->packet);
573         }
574         talloc_free(req);
575         return status;
576 }
577
578 /*
579   a full WINS replication request/response
580 */
581 NTSTATUS wrepl_request(struct wrepl_socket *wrepl_socket,
582                        TALLOC_CTX *mem_ctx,
583                        struct wrepl_packet *req_packet,
584                        struct wrepl_packet **reply_packet)
585 {
586         struct wrepl_request *req = wrepl_request_send(wrepl_socket, req_packet, NULL);
587         return wrepl_request_recv(req, mem_ctx, reply_packet);
588 }
589
590
591 struct wrepl_associate_state {
592         struct wrepl_packet packet;
593         uint32_t assoc_ctx;
594         uint16_t major_version;
595 };
596
597 static void wrepl_associate_done(struct wrepl_request *subreq);
598
599 struct tevent_req *wrepl_associate_send(TALLOC_CTX *mem_ctx,
600                                         struct tevent_context *ev,
601                                         struct wrepl_socket *wrepl_socket,
602                                         const struct wrepl_associate *io)
603 {
604         struct tevent_req *req;
605         struct wrepl_associate_state *state;
606         struct wrepl_request *subreq;
607
608         if (wrepl_socket->event.ctx != ev) {
609                 /* TODO: remove wrepl_socket->event.ctx !!! */
610                 smb_panic("wrepl_associate_send event context mismatch!");
611                 return NULL;
612         }
613
614         req = tevent_req_create(mem_ctx, &state,
615                                 struct wrepl_associate_state);
616         if (req == NULL) {
617                 return NULL;
618         };
619
620         state->packet.opcode                            = WREPL_OPCODE_BITS;
621         state->packet.mess_type                         = WREPL_START_ASSOCIATION;
622         state->packet.message.start.minor_version       = 2;
623         state->packet.message.start.major_version       = 5;
624
625         /*
626          * nt4 uses 41 bytes for the start_association call
627          * so do it the same and as we don't know th emeanings of this bytes
628          * we just send zeros and nt4, w2k and w2k3 seems to be happy with this
629          *
630          * if we don't do this nt4 uses an old version of the wins replication protocol
631          * and that would break nt4 <-> samba replication
632          */
633         state->packet.padding   = data_blob_talloc(state, NULL, 21);
634         if (tevent_req_nomem(state->packet.padding.data, req)) {
635                 return tevent_req_post(req, ev);
636         }
637         memset(state->packet.padding.data, 0, state->packet.padding.length);
638
639         subreq = wrepl_request_send(wrepl_socket, &state->packet, NULL);
640         if (tevent_req_nomem(subreq, req)) {
641                 return tevent_req_post(req, ev);
642         }
643         subreq->async.fn = wrepl_associate_done;
644         subreq->async.private_data = req;
645
646         return req;
647 }
648
649 static void wrepl_associate_done(struct wrepl_request *subreq)
650 {
651         struct tevent_req *req = talloc_get_type_abort(subreq->async.private_data,
652                                  struct tevent_req);
653         struct wrepl_associate_state *state = tevent_req_data(req,
654                                               struct wrepl_associate_state);
655         NTSTATUS status;
656         struct wrepl_packet *packet;
657
658         status = wrepl_request_recv(subreq, state, &packet);
659         if (!NT_STATUS_IS_OK(status)) {
660                 tevent_req_nterror(req, status);
661                 return;
662         }
663
664         if (packet->mess_type != WREPL_START_ASSOCIATION_REPLY) {
665                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
666                 return;
667         }
668
669         state->assoc_ctx = packet->message.start_reply.assoc_ctx;
670         state->major_version = packet->message.start_reply.major_version;
671
672         tevent_req_done(req);
673 }
674
675 /*
676   setup an association - recv
677 */
678 NTSTATUS wrepl_associate_recv(struct tevent_req *req,
679                               struct wrepl_associate *io)
680 {
681         struct wrepl_associate_state *state = tevent_req_data(req,
682                                               struct wrepl_associate_state);
683         NTSTATUS status;
684
685         if (tevent_req_is_nterror(req, &status)) {
686                 tevent_req_received(req);
687                 return status;
688         }
689
690         io->out.assoc_ctx = state->assoc_ctx;
691         io->out.major_version = state->major_version;
692
693         tevent_req_received(req);
694         return NT_STATUS_OK;
695 }
696
697 /*
698   setup an association - sync api
699 */
700 NTSTATUS wrepl_associate(struct wrepl_socket *wrepl_socket,
701                          struct wrepl_associate *io)
702 {
703         struct tevent_req *subreq;
704         bool ok;
705         NTSTATUS status;
706
707         subreq = wrepl_associate_send(wrepl_socket, wrepl_socket->event.ctx,
708                                       wrepl_socket, io);
709         NT_STATUS_HAVE_NO_MEMORY(subreq);
710
711         ok = tevent_req_poll(subreq, wrepl_socket->event.ctx);
712         if (!ok) {
713                 TALLOC_FREE(subreq);
714                 return NT_STATUS_INTERNAL_ERROR;
715         }
716
717         status = wrepl_associate_recv(subreq, io);
718         TALLOC_FREE(subreq);
719         NT_STATUS_NOT_OK_RETURN(status);
720
721         return NT_STATUS_OK;
722 }
723
724 struct wrepl_associate_stop_state {
725         struct wrepl_packet packet;
726         struct wrepl_send_ctrl ctrl;
727 };
728
729 static void wrepl_associate_stop_done(struct wrepl_request *subreq);
730
731 struct tevent_req *wrepl_associate_stop_send(TALLOC_CTX *mem_ctx,
732                                              struct tevent_context *ev,
733                                              struct wrepl_socket *wrepl_socket,
734                                              const struct wrepl_associate_stop *io)
735 {
736         struct tevent_req *req;
737         struct wrepl_associate_stop_state *state;
738         struct wrepl_request *subreq;
739
740         if (wrepl_socket->event.ctx != ev) {
741                 /* TODO: remove wrepl_socket->event.ctx !!! */
742                 smb_panic("wrepl_associate_stop_send event context mismatch!");
743                 return NULL;
744         }
745
746         req = tevent_req_create(mem_ctx, &state,
747                                 struct wrepl_associate_stop_state);
748         if (req == NULL) {
749                 return NULL;
750         };
751
752         state->packet.opcode                    = WREPL_OPCODE_BITS;
753         state->packet.assoc_ctx                 = io->in.assoc_ctx;
754         state->packet.mess_type                 = WREPL_STOP_ASSOCIATION;
755         state->packet.message.stop.reason       = io->in.reason;
756
757         if (io->in.reason == 0) {
758                 state->ctrl.send_only                   = true;
759                 state->ctrl.disconnect_after_send       = true;
760         }
761
762         subreq = wrepl_request_send(wrepl_socket, &state->packet, &state->ctrl);
763         if (tevent_req_nomem(subreq, req)) {
764                 return tevent_req_post(req, ev);
765         }
766         subreq->async.fn = wrepl_associate_stop_done;
767         subreq->async.private_data = req;
768
769         return req;
770 }
771
772 static void wrepl_associate_stop_done(struct wrepl_request *subreq)
773 {
774         struct tevent_req *req = talloc_get_type_abort(subreq->async.private_data,
775                                  struct tevent_req);
776         struct wrepl_associate_stop_state *state = tevent_req_data(req,
777                                                    struct wrepl_associate_stop_state);
778         NTSTATUS status;
779
780         /* currently we don't care about a possible response */
781         status = wrepl_request_recv(subreq, state, NULL);
782         if (!NT_STATUS_IS_OK(status)) {
783                 tevent_req_nterror(req, status);
784                 return;
785         }
786
787         tevent_req_done(req);
788 }
789
790 /*
791   stop an association - recv
792 */
793 NTSTATUS wrepl_associate_stop_recv(struct tevent_req *req,
794                                    struct wrepl_associate_stop *io)
795 {
796         NTSTATUS status;
797
798         if (tevent_req_is_nterror(req, &status)) {
799                 tevent_req_received(req);
800                 return status;
801         }
802
803         tevent_req_received(req);
804         return NT_STATUS_OK;
805 }
806
807 /*
808   setup an association - sync api
809 */
810 NTSTATUS wrepl_associate_stop(struct wrepl_socket *wrepl_socket,
811                               struct wrepl_associate_stop *io)
812 {
813         struct tevent_req *subreq;
814         bool ok;
815         NTSTATUS status;
816
817         subreq = wrepl_associate_stop_send(wrepl_socket, wrepl_socket->event.ctx,
818                                            wrepl_socket, io);
819         NT_STATUS_HAVE_NO_MEMORY(subreq);
820
821         ok = tevent_req_poll(subreq, wrepl_socket->event.ctx);
822         if (!ok) {
823                 TALLOC_FREE(subreq);
824                 return NT_STATUS_INTERNAL_ERROR;
825         }
826
827         status = wrepl_associate_stop_recv(subreq, io);
828         TALLOC_FREE(subreq);
829         NT_STATUS_NOT_OK_RETURN(status);
830
831         return NT_STATUS_OK;
832 }
833
834 struct wrepl_pull_table_state {
835         struct wrepl_packet packet;
836         uint32_t num_partners;
837         struct wrepl_wins_owner *partners;
838 };
839
840 static void wrepl_pull_table_done(struct wrepl_request *subreq);
841
842 struct tevent_req *wrepl_pull_table_send(TALLOC_CTX *mem_ctx,
843                                          struct tevent_context *ev,
844                                          struct wrepl_socket *wrepl_socket,
845                                          const struct wrepl_pull_table *io)
846 {
847         struct tevent_req *req;
848         struct wrepl_pull_table_state *state;
849         struct wrepl_request *subreq;
850
851         if (wrepl_socket->event.ctx != ev) {
852                 /* TODO: remove wrepl_socket->event.ctx !!! */
853                 smb_panic("wrepl_pull_table_send event context mismatch!");
854                 return NULL;
855         }
856
857         req = tevent_req_create(mem_ctx, &state,
858                                 struct wrepl_pull_table_state);
859         if (req == NULL) {
860                 return NULL;
861         };
862
863         state->packet.opcode                            = WREPL_OPCODE_BITS;
864         state->packet.assoc_ctx                         = io->in.assoc_ctx;
865         state->packet.mess_type                         = WREPL_REPLICATION;
866         state->packet.message.replication.command       = WREPL_REPL_TABLE_QUERY;
867
868         subreq = wrepl_request_send(wrepl_socket, &state->packet, NULL);
869         if (tevent_req_nomem(subreq, req)) {
870                 return tevent_req_post(req, ev);
871         }
872         subreq->async.fn = wrepl_pull_table_done;
873         subreq->async.private_data = req;
874
875         return req;
876 }
877
878 static void wrepl_pull_table_done(struct wrepl_request *subreq)
879 {
880         struct tevent_req *req = talloc_get_type_abort(subreq->async.private_data,
881                                  struct tevent_req);
882         struct wrepl_pull_table_state *state = tevent_req_data(req,
883                                                struct wrepl_pull_table_state);
884         NTSTATUS status;
885         struct wrepl_packet *packet;
886         struct wrepl_table *table;
887
888         status = wrepl_request_recv(subreq, state, &packet);
889         if (!NT_STATUS_IS_OK(status)) {
890                 tevent_req_nterror(req, status);
891                 return;
892         }
893
894         if (packet->mess_type != WREPL_REPLICATION) {
895                 tevent_req_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
896                 return;
897         }
898
899         if (packet->message.replication.command != WREPL_REPL_TABLE_REPLY) {
900                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
901                 return;
902         }
903
904         table = &packet->message.replication.info.table;
905
906         state->num_partners = table->partner_count;
907         state->partners = talloc_move(state, &table->partners);
908
909         tevent_req_done(req);
910 }
911
912 /*
913   fetch the partner tables - recv
914 */
915 NTSTATUS wrepl_pull_table_recv(struct tevent_req *req,
916                                TALLOC_CTX *mem_ctx,
917                                struct wrepl_pull_table *io)
918 {
919         struct wrepl_pull_table_state *state = tevent_req_data(req,
920                                                struct wrepl_pull_table_state);
921         NTSTATUS status;
922
923         if (tevent_req_is_nterror(req, &status)) {
924                 tevent_req_received(req);
925                 return status;
926         }
927
928         io->out.num_partners = state->num_partners;
929         io->out.partners = talloc_move(mem_ctx, &state->partners);
930
931         tevent_req_received(req);
932         return NT_STATUS_OK;
933 }
934
935 /*
936   fetch the partner table - sync api
937 */
938 NTSTATUS wrepl_pull_table(struct wrepl_socket *wrepl_socket,
939                           TALLOC_CTX *mem_ctx,
940                           struct wrepl_pull_table *io)
941 {
942         struct tevent_req *subreq;
943         bool ok;
944         NTSTATUS status;
945
946         subreq = wrepl_pull_table_send(mem_ctx, wrepl_socket->event.ctx,
947                                        wrepl_socket, io);
948         NT_STATUS_HAVE_NO_MEMORY(subreq);
949
950         ok = tevent_req_poll(subreq, wrepl_socket->event.ctx);
951         if (!ok) {
952                 TALLOC_FREE(subreq);
953                 return NT_STATUS_INTERNAL_ERROR;
954         }
955
956         status = wrepl_pull_table_recv(subreq, mem_ctx, io);
957         TALLOC_FREE(subreq);
958         NT_STATUS_NOT_OK_RETURN(status);
959
960         return NT_STATUS_OK;
961 }
962
963
964 struct wrepl_pull_names_state {
965         struct {
966                 const struct wrepl_pull_names *io;
967         } caller;
968         struct wrepl_packet packet;
969         uint32_t num_names;
970         struct wrepl_name *names;
971 };
972
973 static void wrepl_pull_names_done(struct wrepl_request *subreq);
974
975 struct tevent_req *wrepl_pull_names_send(TALLOC_CTX *mem_ctx,
976                                          struct tevent_context *ev,
977                                          struct wrepl_socket *wrepl_socket,
978                                          const struct wrepl_pull_names *io)
979 {
980         struct tevent_req *req;
981         struct wrepl_pull_names_state *state;
982         struct wrepl_request *subreq;
983
984         if (wrepl_socket->event.ctx != ev) {
985                 /* TODO: remove wrepl_socket->event.ctx !!! */
986                 smb_panic("wrepl_pull_names_send event context mismatch!");
987                 return NULL;
988         }
989
990         req = tevent_req_create(mem_ctx, &state,
991                                 struct wrepl_pull_names_state);
992         if (req == NULL) {
993                 return NULL;
994         };
995         state->caller.io = io;
996
997         state->packet.opcode                            = WREPL_OPCODE_BITS;
998         state->packet.assoc_ctx                         = io->in.assoc_ctx;
999         state->packet.mess_type                         = WREPL_REPLICATION;
1000         state->packet.message.replication.command       = WREPL_REPL_SEND_REQUEST;
1001         state->packet.message.replication.info.owner    = io->in.partner;
1002
1003         subreq = wrepl_request_send(wrepl_socket, &state->packet, NULL);
1004         if (tevent_req_nomem(subreq, req)) {
1005                 return tevent_req_post(req, ev);
1006         }
1007         subreq->async.fn = wrepl_pull_names_done;
1008         subreq->async.private_data = req;
1009
1010         return req;
1011 }
1012
1013 static void wrepl_pull_names_done(struct wrepl_request *subreq)
1014 {
1015         struct tevent_req *req = talloc_get_type_abort(subreq->async.private_data,
1016                                  struct tevent_req);
1017         struct wrepl_pull_names_state *state = tevent_req_data(req,
1018                                                struct wrepl_pull_names_state);
1019         NTSTATUS status;
1020         struct wrepl_packet *packet;
1021         uint32_t i;
1022
1023         status = wrepl_request_recv(subreq, state, &packet);
1024         if (!NT_STATUS_IS_OK(status)) {
1025                 tevent_req_nterror(req, status);
1026                 return;
1027         }
1028
1029         if (packet->mess_type != WREPL_REPLICATION) {
1030                 tevent_req_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
1031                 return;
1032         }
1033
1034         if (packet->message.replication.command != WREPL_REPL_SEND_REPLY) {
1035                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1036                 return;
1037         }
1038
1039         state->num_names = packet->message.replication.info.reply.num_names;
1040
1041         state->names = talloc_array(state, struct wrepl_name, state->num_names);
1042         if (tevent_req_nomem(state->names, req)) {
1043                 return;
1044         }
1045
1046         /* convert the list of names and addresses to a sane format */
1047         for (i=0; i < state->num_names; i++) {
1048                 struct wrepl_wins_name *wname = &packet->message.replication.info.reply.names[i];
1049                 struct wrepl_name *name = &state->names[i];
1050
1051                 name->name      = *wname->name;
1052                 talloc_steal(state->names, wname->name);
1053                 name->type      = WREPL_NAME_TYPE(wname->flags);
1054                 name->state     = WREPL_NAME_STATE(wname->flags);
1055                 name->node      = WREPL_NAME_NODE(wname->flags);
1056                 name->is_static = WREPL_NAME_IS_STATIC(wname->flags);
1057                 name->raw_flags = wname->flags;
1058                 name->version_id= wname->id;
1059                 name->owner     = talloc_strdup(state->names,
1060                                                 state->caller.io->in.partner.address);
1061                 if (tevent_req_nomem(name->owner, req)) {
1062                         return;
1063                 }
1064
1065                 /* trying to save 1 or 2 bytes on the wire isn't a good idea */
1066                 if (wname->flags & 2) {
1067                         uint32_t j;
1068
1069                         name->num_addresses = wname->addresses.addresses.num_ips;
1070                         name->addresses = talloc_array(state->names,
1071                                                        struct wrepl_address,
1072                                                        name->num_addresses);
1073                         if (tevent_req_nomem(name->addresses, req)) {
1074                                 return;
1075                         }
1076
1077                         for (j=0;j<name->num_addresses;j++) {
1078                                 name->addresses[j].owner =
1079                                         talloc_move(name->addresses,
1080                                                     &wname->addresses.addresses.ips[j].owner);
1081                                 name->addresses[j].address = 
1082                                         talloc_move(name->addresses,
1083                                                     &wname->addresses.addresses.ips[j].ip);
1084                         }
1085                 } else {
1086                         name->num_addresses = 1;
1087                         name->addresses = talloc_array(state->names,
1088                                                        struct wrepl_address,
1089                                                        name->num_addresses);
1090                         if (tevent_req_nomem(name->addresses, req)) {
1091                                 return;
1092                         }
1093
1094                         name->addresses[0].owner = talloc_strdup(name->addresses, name->owner);
1095                         if (tevent_req_nomem(name->addresses[0].owner, req)) {
1096                                 return;
1097                         }
1098                         name->addresses[0].address = talloc_move(name->addresses,
1099                                                                  &wname->addresses.ip);
1100                 }
1101         }
1102
1103         tevent_req_done(req);
1104 }
1105
1106 /*
1107   fetch the names for a WINS partner - recv
1108 */
1109 NTSTATUS wrepl_pull_names_recv(struct tevent_req *req,
1110                                TALLOC_CTX *mem_ctx,
1111                                struct wrepl_pull_names *io)
1112 {
1113         struct wrepl_pull_names_state *state = tevent_req_data(req,
1114                                                struct wrepl_pull_names_state);
1115         NTSTATUS status;
1116
1117         if (tevent_req_is_nterror(req, &status)) {
1118                 tevent_req_received(req);
1119                 return status;
1120         }
1121
1122         io->out.num_names = state->num_names;
1123         io->out.names = talloc_move(mem_ctx, &state->names);
1124
1125         tevent_req_received(req);
1126         return NT_STATUS_OK;
1127 }
1128
1129
1130
1131 /*
1132   fetch the names for a WINS partner - sync api
1133 */
1134 NTSTATUS wrepl_pull_names(struct wrepl_socket *wrepl_socket,
1135                           TALLOC_CTX *mem_ctx,
1136                           struct wrepl_pull_names *io)
1137 {
1138         struct tevent_req *subreq;
1139         bool ok;
1140         NTSTATUS status;
1141
1142         subreq = wrepl_pull_names_send(mem_ctx, wrepl_socket->event.ctx,
1143                                        wrepl_socket, io);
1144         NT_STATUS_HAVE_NO_MEMORY(subreq);
1145
1146         ok = tevent_req_poll(subreq, wrepl_socket->event.ctx);
1147         if (!ok) {
1148                 TALLOC_FREE(subreq);
1149                 return NT_STATUS_INTERNAL_ERROR;
1150         }
1151
1152         status = wrepl_pull_names_recv(subreq, mem_ctx, io);
1153         TALLOC_FREE(subreq);
1154         NT_STATUS_NOT_OK_RETURN(status);
1155
1156         return NT_STATUS_OK;
1157 }