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