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