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