]> git.ipfire.org Git - people/ms/strongswan.git/blob - lib/liblwres/getipnode.c
- import of strongswan-2.7.0
[people/ms/strongswan.git] / lib / liblwres / getipnode.c
1 /*
2 * Copyright (C) 1999-2001 Internet Software Consortium.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 /* $Id: getipnode.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <lwres/lwres.h>
27 #include <lwres/net.h>
28 #include <lwres/netdb.h> /* XXX #include <netdb.h> */
29
30 #include "assert_p.h"
31
32 #ifndef INADDRSZ
33 #define INADDRSZ 4
34 #endif
35 #ifndef IN6ADDRSZ
36 #define IN6ADDRSZ 16
37 #endif
38
39 #ifdef LWRES_PLATFORM_NEEDIN6ADDRANY
40 LIBLWRES_EXTERNAL_DATA const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
41 #endif
42
43 #ifndef IN6_IS_ADDR_V4COMPAT
44 static const unsigned char in6addr_compat[12] = {
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
46 };
47 #define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \
48 ((x)->s6_addr[12] != 0 || \
49 (x)->s6_addr[13] != 0 || \
50 (x)->s6_addr[14] != 0 || \
51 ((x)->s6_addr[15] != 0 && \
52 (x)->s6_addr[15] != 1)))
53 #endif
54 #ifndef IN6_IS_ADDR_V4MAPPED
55 #define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12))
56 #endif
57
58 static const unsigned char in6addr_mapped[12] = {
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
60 };
61
62 /***
63 *** Forward declarations.
64 ***/
65
66 static int
67 scan_interfaces(int *, int *);
68
69 static struct hostent *
70 copyandmerge(struct hostent *, struct hostent *, int, int *);
71
72 static struct hostent *
73 hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src);
74
75 static struct hostent *
76 hostfromname(lwres_gabnresponse_t *name, int af);
77
78 /***
79 *** Public functions.
80 ***/
81
82 /*
83 * AI_V4MAPPED + AF_INET6
84 * If no IPv6 address then a query for IPv4 and map returned values.
85 *
86 * AI_ALL + AI_V4MAPPED + AF_INET6
87 * Return IPv6 and IPv4 mapped.
88 *
89 * AI_ADDRCONFIG
90 * Only return IPv6 / IPv4 address if there is an interface of that
91 * type active.
92 */
93
94 struct hostent *
95 lwres_getipnodebyname(const char *name, int af, int flags, int *error_num) {
96 int have_v4 = 1, have_v6 = 1;
97 struct in_addr in4;
98 struct in6_addr in6;
99 struct hostent he, *he1 = NULL, *he2 = NULL, *he3 = NULL;
100 int v4 = 0, v6 = 0;
101 int tmp_err;
102 lwres_context_t *lwrctx = NULL;
103 lwres_gabnresponse_t *by = NULL;
104 int n;
105
106 /*
107 * If we care about active interfaces then check.
108 */
109 if ((flags & AI_ADDRCONFIG) != 0)
110 if (scan_interfaces(&have_v4, &have_v6) == -1) {
111 *error_num = NO_RECOVERY;
112 return (NULL);
113 }
114
115 /* Check for literal address. */
116 if ((v4 = lwres_net_pton(AF_INET, name, &in4)) != 1)
117 v6 = lwres_net_pton(AF_INET6, name, &in6);
118
119 /*
120 * Impossible combination?
121 */
122 if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) ||
123 (af == AF_INET && v6 == 1) ||
124 (have_v4 == 0 && v4 == 1) ||
125 (have_v6 == 0 && v6 == 1) ||
126 (have_v4 == 0 && af == AF_INET) ||
127 (have_v6 == 0 && af == AF_INET6 &&
128 (((flags & AI_V4MAPPED) != 0 && have_v4) ||
129 (flags & AI_V4MAPPED) == 0))) {
130 *error_num = HOST_NOT_FOUND;
131 return (NULL);
132 }
133
134 /*
135 * Literal address?
136 */
137 if (v4 == 1 || v6 == 1) {
138 char *addr_list[2];
139 char *aliases[1];
140 union {
141 const char *const_name;
142 char *deconst_name;
143 } u;
144
145 u.const_name = name;
146 he.h_name = u.deconst_name;
147 he.h_addr_list = addr_list;
148 he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6;
149 he.h_addr_list[1] = NULL;
150 he.h_aliases = aliases;
151 he.h_aliases[0] = NULL;
152 he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ;
153 he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6;
154 return (copyandmerge(&he, NULL, af, error_num));
155 }
156
157 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
158 if (n != 0) {
159 *error_num = NO_RECOVERY;
160 goto cleanup;
161 }
162 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
163 tmp_err = NO_RECOVERY;
164 if (have_v6 && af == AF_INET6) {
165
166 n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V6, &by);
167 if (n == 0) {
168 he1 = hostfromname(by, AF_INET6);
169 lwres_gabnresponse_free(lwrctx, &by);
170 if (he1 == NULL) {
171 *error_num = NO_RECOVERY;
172 goto cleanup;
173 }
174 } else {
175 tmp_err = HOST_NOT_FOUND;
176 }
177 }
178
179 if (have_v4 &&
180 ((af == AF_INET) ||
181 (af == AF_INET6 && (flags & AI_V4MAPPED) != 0 &&
182 (he1 == NULL || (flags & AI_ALL) != 0)))) {
183 n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V4, &by);
184 if (n == 0) {
185 he2 = hostfromname(by, AF_INET);
186 lwres_gabnresponse_free(lwrctx, &by);
187 if (he2 == NULL) {
188 *error_num = NO_RECOVERY;
189 goto cleanup;
190 }
191 } else if (he1 == NULL) {
192 if (n == LWRES_R_NOTFOUND)
193 *error_num = HOST_NOT_FOUND;
194 else
195 *error_num = NO_RECOVERY;
196 goto cleanup;
197 }
198 } else
199 *error_num = tmp_err;
200
201 he3 = copyandmerge(he1, he2, af, error_num);
202
203 cleanup:
204 if (he1 != NULL)
205 lwres_freehostent(he1);
206 if (he2 != NULL)
207 lwres_freehostent(he2);
208 if (lwrctx != NULL) {
209 lwres_conf_clear(lwrctx);
210 lwres_context_destroy(&lwrctx);
211 }
212 return (he3);
213 }
214
215 struct hostent *
216 lwres_getipnodebyaddr(const void *src, size_t len, int af, int *error_num) {
217 struct hostent *he1, *he2;
218 lwres_context_t *lwrctx = NULL;
219 lwres_gnbaresponse_t *by = NULL;
220 lwres_result_t n;
221 union {
222 const void *konst;
223 struct in6_addr *in6;
224 } u;
225
226 /*
227 * Sanity checks.
228 */
229 if (src == NULL) {
230 *error_num = NO_RECOVERY;
231 return (NULL);
232 }
233
234 switch (af) {
235 case AF_INET:
236 if (len != INADDRSZ) {
237 *error_num = NO_RECOVERY;
238 return (NULL);
239 }
240 break;
241 case AF_INET6:
242 if (len != IN6ADDRSZ) {
243 *error_num = NO_RECOVERY;
244 return (NULL);
245 }
246 break;
247 default:
248 *error_num = NO_RECOVERY;
249 return (NULL);
250 }
251
252 /*
253 * The de-"const"-ing game is done because at least one
254 * vendor's system (RedHat 6.0) defines the IN6_IS_ADDR_*
255 * macros in such a way that they discard the const with
256 * internal casting, and gcc ends up complaining. Rather
257 * than replacing their own (possibly optimized) definitions
258 * with our own, cleanly discarding the const is the easiest
259 * thing to do.
260 */
261 u.konst = src;
262
263 /*
264 * Look up IPv4 and IPv4 mapped/compatible addresses.
265 */
266 if ((af == AF_INET6 && IN6_IS_ADDR_V4COMPAT(u.in6)) ||
267 (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED(u.in6)) ||
268 (af == AF_INET)) {
269 const unsigned char *cp = src;
270
271 if (af == AF_INET6)
272 cp += 12;
273 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
274 if (n == LWRES_R_SUCCESS)
275 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
276 if (n == LWRES_R_SUCCESS)
277 n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V4,
278 INADDRSZ, cp, &by);
279 if (n != LWRES_R_SUCCESS) {
280 lwres_conf_clear(lwrctx);
281 lwres_context_destroy(&lwrctx);
282 if (n == LWRES_R_NOTFOUND)
283 *error_num = HOST_NOT_FOUND;
284 else
285 *error_num = NO_RECOVERY;
286 return (NULL);
287 }
288 he1 = hostfromaddr(by, AF_INET, cp);
289 lwres_gnbaresponse_free(lwrctx, &by);
290 lwres_conf_clear(lwrctx);
291 lwres_context_destroy(&lwrctx);
292 if (af != AF_INET6)
293 return (he1);
294
295 /*
296 * Convert from AF_INET to AF_INET6.
297 */
298 he2 = copyandmerge(he1, NULL, af, error_num);
299 lwres_freehostent(he1);
300 if (he2 == NULL)
301 return (NULL);
302 /*
303 * Restore original address.
304 */
305 memcpy(he2->h_addr, src, len);
306 return (he2);
307 }
308
309 /*
310 * Lookup IPv6 address.
311 */
312 if (memcmp(src, &in6addr_any, IN6ADDRSZ) == 0) {
313 *error_num = HOST_NOT_FOUND;
314 return (NULL);
315 }
316
317 n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
318 if (n == LWRES_R_SUCCESS)
319 (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
320 if (n == LWRES_R_SUCCESS)
321 n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V6, IN6ADDRSZ,
322 src, &by);
323 if (n != 0) {
324 *error_num = HOST_NOT_FOUND;
325 return (NULL);
326 }
327 he1 = hostfromaddr(by, AF_INET6, src);
328 lwres_gnbaresponse_free(lwrctx, &by);
329 if (he1 == NULL)
330 *error_num = NO_RECOVERY;
331 lwres_context_destroy(&lwrctx);
332 return (he1);
333 }
334
335 void
336 lwres_freehostent(struct hostent *he) {
337 char **cpp;
338 int names = 1;
339 int addresses = 1;
340
341 free(he->h_name);
342
343 cpp = he->h_addr_list;
344 while (*cpp != NULL) {
345 free(*cpp);
346 *cpp = NULL;
347 cpp++;
348 addresses++;
349 }
350
351 cpp = he->h_aliases;
352 while (*cpp != NULL) {
353 free(*cpp);
354 cpp++;
355 names++;
356 }
357
358 free(he->h_aliases);
359 free(he->h_addr_list);
360 free(he);
361 }
362
363 /*
364 * Private
365 */
366
367 /*
368 * Scan the interface table and set have_v4 and have_v6 depending
369 * upon whether there are IPv4 and IPv6 interface addresses.
370 *
371 * Returns:
372 * 0 on success
373 * -1 on failure.
374 */
375
376 static int
377 scan_interfaces(int *have_v4, int *have_v6) {
378 #if 1
379 *have_v4 = *have_v6 = 1;
380 return (0);
381 #else
382 struct ifconf ifc;
383 struct ifreq ifreq;
384 struct in_addr in4;
385 struct in6_addr in6;
386 char *buf = NULL, *cp, *cplim;
387 static int bufsiz = 4095;
388 int s, cpsize, n;
389
390 /*
391 * Set to zero. Used as loop terminators below.
392 */
393 *have_v4 = *have_v6 = 0;
394
395 /*
396 * Get interface list from system.
397 */
398 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
399 goto err_ret;
400
401 /*
402 * Grow buffer until large enough to contain all interface
403 * descriptions.
404 */
405 for (;;) {
406 buf = malloc(bufsiz);
407 if (buf == NULL)
408 goto err_ret;
409 ifc.ifc_len = bufsiz;
410 ifc.ifc_buf = buf;
411 #ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
412 /*
413 * This is a fix for IRIX OS in which the call to ioctl with
414 * the flag SIOCGIFCONF may not return an entry for all the
415 * interfaces like most flavors of Unix.
416 */
417 if (emul_ioctl(&ifc) >= 0)
418 break;
419 #else
420 if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
421 /*
422 * Some OS's just return what will fit rather
423 * than set EINVAL if the buffer is too small
424 * to fit all the interfaces in. If
425 * ifc.ifc_len is too near to the end of the
426 * buffer we will grow it just in case and
427 * retry.
428 */
429 if (ifc.ifc_len + 2 * sizeof(ifreq) < bufsiz)
430 break;
431 }
432 #endif
433 if ((n == -1) && errno != EINVAL)
434 goto err_ret;
435
436 if (bufsiz > 1000000)
437 goto err_ret;
438
439 free(buf);
440 bufsiz += 4096;
441 }
442
443 /*
444 * Parse system's interface list.
445 */
446 cplim = buf + ifc.ifc_len; /* skip over if's with big ifr_addr's */
447 for (cp = buf;
448 (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
449 cp += cpsize) {
450 memcpy(&ifreq, cp, sizeof ifreq);
451 #ifdef LWRES_PLATFORM_HAVESALEN
452 #ifdef FIX_ZERO_SA_LEN
453 if (ifreq.ifr_addr.sa_len == 0)
454 ifreq.ifr_addr.sa_len = IN6ADDRSZ;
455 #endif
456 #ifdef HAVE_MINIMUM_IFREQ
457 cpsize = sizeof ifreq;
458 if (ifreq.ifr_addr.sa_len > sizeof (struct sockaddr))
459 cpsize += (int)ifreq.ifr_addr.sa_len -
460 (int)(sizeof(struct sockaddr));
461 #else
462 cpsize = sizeof ifreq.ifr_name + ifreq.ifr_addr.sa_len;
463 #endif /* HAVE_MINIMUM_IFREQ */
464 #elif defined SIOCGIFCONF_ADDR
465 cpsize = sizeof ifreq;
466 #else
467 cpsize = sizeof ifreq.ifr_name;
468 /* XXX maybe this should be a hard error? */
469 if (ioctl(s, SIOCGIFADDR, (char *)&ifreq) < 0)
470 continue;
471 #endif /* LWRES_PLATFORM_HAVESALEN */
472 switch (ifreq.ifr_addr.sa_family) {
473 case AF_INET:
474 if (*have_v4 == 0) {
475 memcpy(&in4,
476 &((struct sockaddr_in *)
477 &ifreq.ifr_addr)->sin_addr,
478 sizeof(in4));
479 if (in4.s_addr == INADDR_ANY)
480 break;
481 n = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
482 if (n < 0)
483 break;
484 if ((ifreq.ifr_flags & IFF_UP) == 0)
485 break;
486 *have_v4 = 1;
487 }
488 break;
489 case AF_INET6:
490 if (*have_v6 == 0) {
491 memcpy(&in6,
492 &((struct sockaddr_in6 *)
493 &ifreq.ifr_addr)->sin6_addr,
494 sizeof(in6));
495 if (memcmp(&in6, &in6addr_any,
496 sizeof(in6)) == 0)
497 break;
498 n = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
499 if (n < 0)
500 break;
501 if ((ifreq.ifr_flags & IFF_UP) == 0)
502 break;
503 *have_v6 = 1;
504 }
505 break;
506 }
507 }
508 if (buf != NULL)
509 free(buf);
510 close(s);
511 return (0);
512 err_ret:
513 if (buf != NULL)
514 free(buf);
515 if (s != -1)
516 close(s);
517 return (-1);
518 #endif
519 }
520
521 static struct hostent *
522 copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num)
523 {
524 struct hostent *he = NULL;
525 int addresses = 1; /* NULL terminator */
526 int names = 1; /* NULL terminator */
527 int len = 0;
528 char **cpp, **npp;
529
530 /*
531 * Work out array sizes.
532 */
533 if (he1 != NULL) {
534 cpp = he1->h_addr_list;
535 while (*cpp != NULL) {
536 addresses++;
537 cpp++;
538 }
539 cpp = he1->h_aliases;
540 while (*cpp != NULL) {
541 names++;
542 cpp++;
543 }
544 }
545
546 if (he2 != NULL) {
547 cpp = he2->h_addr_list;
548 while (*cpp != NULL) {
549 addresses++;
550 cpp++;
551 }
552 if (he1 == NULL) {
553 cpp = he2->h_aliases;
554 while (*cpp != NULL) {
555 names++;
556 cpp++;
557 }
558 }
559 }
560
561 if (addresses == 1) {
562 *error_num = NO_ADDRESS;
563 return (NULL);
564 }
565
566 he = malloc(sizeof *he);
567 if (he == NULL)
568 goto no_recovery;
569
570 he->h_addr_list = malloc(sizeof(char *) * (addresses));
571 if (he->h_addr_list == NULL)
572 goto cleanup0;
573 memset(he->h_addr_list, 0, sizeof(char *) * (addresses));
574
575 /*
576 * Copy addresses.
577 */
578 npp = he->h_addr_list;
579 if (he1 != NULL) {
580 cpp = he1->h_addr_list;
581 while (*cpp != NULL) {
582 *npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
583 if (*npp == NULL)
584 goto cleanup1;
585 /*
586 * Convert to mapped if required.
587 */
588 if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
589 memcpy(*npp, in6addr_mapped,
590 sizeof in6addr_mapped);
591 memcpy(*npp + sizeof in6addr_mapped, *cpp,
592 INADDRSZ);
593 } else {
594 memcpy(*npp, *cpp,
595 (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
596 }
597 cpp++;
598 npp++;
599 }
600 }
601
602 if (he2 != NULL) {
603 cpp = he2->h_addr_list;
604 while (*cpp != NULL) {
605 *npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
606 if (*npp == NULL)
607 goto cleanup1;
608 /*
609 * Convert to mapped if required.
610 */
611 if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
612 memcpy(*npp, in6addr_mapped,
613 sizeof in6addr_mapped);
614 memcpy(*npp + sizeof in6addr_mapped, *cpp,
615 INADDRSZ);
616 } else {
617 memcpy(*npp, *cpp,
618 (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
619 }
620 cpp++;
621 npp++;
622 }
623 }
624
625 he->h_aliases = malloc(sizeof(char *) * (names));
626 if (he->h_aliases == NULL)
627 goto cleanup1;
628 memset(he->h_aliases, 0, sizeof(char *) * (names));
629
630 /*
631 * Copy aliases.
632 */
633 npp = he->h_aliases;
634 cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
635 while (*cpp != NULL) {
636 len = strlen (*cpp) + 1;
637 *npp = malloc(len);
638 if (*npp == NULL)
639 goto cleanup2;
640 strcpy(*npp, *cpp);
641 npp++;
642 cpp++;
643 }
644
645 /*
646 * Copy hostname.
647 */
648 he->h_name = malloc(strlen((he1 != NULL) ?
649 he1->h_name : he2->h_name) + 1);
650 if (he->h_name == NULL)
651 goto cleanup2;
652 strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);
653
654 /*
655 * Set address type and length.
656 */
657 he->h_addrtype = af;
658 he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
659 return (he);
660
661 cleanup2:
662 cpp = he->h_aliases;
663 while (*cpp != NULL) {
664 free(*cpp);
665 cpp++;
666 }
667 free(he->h_aliases);
668
669 cleanup1:
670 cpp = he->h_addr_list;
671 while (*cpp != NULL) {
672 free(*cpp);
673 *cpp = NULL;
674 cpp++;
675 }
676 free(he->h_addr_list);
677
678 cleanup0:
679 free(he);
680
681 no_recovery:
682 *error_num = NO_RECOVERY;
683 return (NULL);
684 }
685
686 static struct hostent *
687 hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src) {
688 struct hostent *he;
689 int i;
690
691 he = malloc(sizeof *he);
692 if (he == NULL)
693 goto cleanup;
694 memset(he, 0, sizeof(*he));
695
696 /*
697 * Set family and length.
698 */
699 he->h_addrtype = af;
700 switch (af) {
701 case AF_INET:
702 he->h_length = INADDRSZ;
703 break;
704 case AF_INET6:
705 he->h_length = IN6ADDRSZ;
706 break;
707 default:
708 INSIST(0);
709 }
710
711 /*
712 * Copy name.
713 */
714 he->h_name = strdup(addr->realname);
715 if (he->h_name == NULL)
716 goto cleanup;
717
718 /*
719 * Copy aliases.
720 */
721 he->h_aliases = malloc(sizeof(char *) * (addr->naliases + 1));
722 if (he->h_aliases == NULL)
723 goto cleanup;
724 for (i = 0 ; i < addr->naliases; i++) {
725 he->h_aliases[i] = strdup(addr->aliases[i]);
726 if (he->h_aliases[i] == NULL)
727 goto cleanup;
728 }
729 he->h_aliases[i] = NULL;
730
731 /*
732 * Copy address.
733 */
734 he->h_addr_list = malloc(sizeof(char *) * 2);
735 if (he->h_addr_list == NULL)
736 goto cleanup;
737 he->h_addr_list[0] = malloc(he->h_length);
738 if (he->h_addr_list[0] == NULL)
739 goto cleanup;
740 memcpy(he->h_addr_list[0], src, he->h_length);
741 he->h_addr_list[1] = NULL;
742 return (he);
743
744 cleanup:
745 if (he != NULL && he->h_addr_list != NULL) {
746 for (i = 0; he->h_addr_list[i] != NULL; i++)
747 free(he->h_addr_list[i]);
748 free(he->h_addr_list);
749 }
750 if (he != NULL && he->h_aliases != NULL) {
751 for (i = 0; he->h_aliases[i] != NULL; i++)
752 free(he->h_aliases[i]);
753 free(he->h_aliases);
754 }
755 if (he != NULL && he->h_name != NULL)
756 free(he->h_name);
757 if (he != NULL)
758 free(he);
759 return (NULL);
760 }
761
762 static struct hostent *
763 hostfromname(lwres_gabnresponse_t *name, int af) {
764 struct hostent *he;
765 int i;
766 lwres_addr_t *addr;
767
768 he = malloc(sizeof *he);
769 if (he == NULL)
770 goto cleanup;
771 memset(he, 0, sizeof(*he));
772
773 /*
774 * Set family and length.
775 */
776 he->h_addrtype = af;
777 switch (af) {
778 case AF_INET:
779 he->h_length = INADDRSZ;
780 break;
781 case AF_INET6:
782 he->h_length = IN6ADDRSZ;
783 break;
784 default:
785 INSIST(0);
786 }
787
788 /*
789 * Copy name.
790 */
791 he->h_name = strdup(name->realname);
792 if (he->h_name == NULL)
793 goto cleanup;
794
795 /*
796 * Copy aliases.
797 */
798 he->h_aliases = malloc(sizeof(char *) * (name->naliases + 1));
799 for (i = 0 ; i < name->naliases; i++) {
800 he->h_aliases[i] = strdup(name->aliases[i]);
801 if (he->h_aliases[i] == NULL)
802 goto cleanup;
803 }
804 he->h_aliases[i] = NULL;
805
806 /*
807 * Copy addresses.
808 */
809 he->h_addr_list = malloc(sizeof(char *) * (name->naddrs + 1));
810 addr = LWRES_LIST_HEAD(name->addrs);
811 i = 0;
812 while (addr != NULL) {
813 he->h_addr_list[i] = malloc(he->h_length);
814 if (he->h_addr_list[i] == NULL)
815 goto cleanup;
816 memcpy(he->h_addr_list[i], addr->address, he->h_length);
817 addr = LWRES_LIST_NEXT(addr, link);
818 i++;
819 }
820 he->h_addr_list[i] = NULL;
821 return (he);
822
823 cleanup:
824 if (he != NULL && he->h_addr_list != NULL) {
825 for (i = 0; he->h_addr_list[i] != NULL; i++)
826 free(he->h_addr_list[i]);
827 free(he->h_addr_list);
828 }
829 if (he != NULL && he->h_aliases != NULL) {
830 for (i = 0; he->h_aliases[i] != NULL; i++)
831 free(he->h_aliases[i]);
832 free(he->h_aliases);
833 }
834 if (he != NULL && he->h_name != NULL)
835 free(he->h_name);
836 if (he != NULL)
837 free(he);
838 return (NULL);
839 }