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