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