blob: fc8948776d35ff7200c4139b8cf7885169e31cfd [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#define _GNU_SOURCE
2
3#include <sys/socket.h>
4#include <netdb.h>
5#include <string.h>
6#include <netinet/in.h>
7#include <errno.h>
Szabolcs Nagy57174442013-12-12 05:09:18 +00008#include <stdint.h>
Rich Felkere8f39ca2014-06-01 00:09:28 -04009#include "lookup.h"
Rich Felker0b44a032011-02-12 00:22:29 -050010
11int gethostbyname2_r(const char *name, int af,
12 struct hostent *h, char *buf, size_t buflen,
13 struct hostent **res, int *err)
14{
Rich Felkere8f39ca2014-06-01 00:09:28 -040015 struct address addrs[MAXADDRS];
16 char canon[256];
17 int i, cnt;
18 size_t align, need;
Rich Felker0b44a032011-02-12 00:22:29 -050019
Timo Teräsfe82bb92014-06-20 13:53:23 +030020 *res = 0;
Rich Felkere8f39ca2014-06-01 00:09:28 -040021 cnt = __lookup_name(addrs, canon, name, af, AI_CANONNAME);
22 if (cnt<0) switch (cnt) {
Rich Felker0b44a032011-02-12 00:22:29 -050023 case EAI_NONAME:
24 *err = HOST_NOT_FOUND;
Rich Felkere8f39ca2014-06-01 00:09:28 -040025 return ENOENT;
Rich Felker0b44a032011-02-12 00:22:29 -050026 case EAI_AGAIN:
27 *err = TRY_AGAIN;
Rich Felkere8f39ca2014-06-01 00:09:28 -040028 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -050029 default:
Rich Felkere8f39ca2014-06-01 00:09:28 -040030 case EAI_FAIL:
31 *err = NO_RECOVERY;
32 return EBADMSG;
Rich Felker0b44a032011-02-12 00:22:29 -050033 case EAI_MEMORY:
34 case EAI_SYSTEM:
Rich Felker0b44a032011-02-12 00:22:29 -050035 *err = NO_RECOVERY;
Rich Felker70b584b2013-02-02 01:31:10 -050036 return errno;
Rich Felker0b44a032011-02-12 00:22:29 -050037 }
38
39 h->h_addrtype = af;
40 h->h_length = af==AF_INET6 ? 16 : 4;
41
Rich Felkere8f39ca2014-06-01 00:09:28 -040042 /* Align buffer */
43 align = -(uintptr_t)buf & sizeof(char *)-1;
44
Rich Felker0b44a032011-02-12 00:22:29 -050045 need = 4*sizeof(char *);
Rich Felkere8f39ca2014-06-01 00:09:28 -040046 need += (cnt + 1) * (sizeof(char *) + h->h_length);
Rich Felker0b44a032011-02-12 00:22:29 -050047 need += strlen(name)+1;
48 need += strlen(canon)+1;
Rich Felkere8f39ca2014-06-01 00:09:28 -040049 need += align;
Rich Felker0b44a032011-02-12 00:22:29 -050050
Rich Felkere8f39ca2014-06-01 00:09:28 -040051 if (need > buflen) return ERANGE;
Rich Felker0b44a032011-02-12 00:22:29 -050052
Rich Felkere8f39ca2014-06-01 00:09:28 -040053 buf += align;
Rich Felker0b44a032011-02-12 00:22:29 -050054 h->h_aliases = (void *)buf;
55 buf += 3*sizeof(char *);
56 h->h_addr_list = (void *)buf;
Rich Felkere8f39ca2014-06-01 00:09:28 -040057 buf += (cnt+1)*sizeof(char *);
Rich Felker0b44a032011-02-12 00:22:29 -050058
Rich Felker4da0bc52016-06-27 17:11:30 -040059 for (i=0; i<cnt; i++) {
60 h->h_addr_list[i] = (void *)buf;
61 buf += h->h_length;
62 memcpy(h->h_addr_list[i], addrs[i].addr, h->h_length);
63 }
64 h->h_addr_list[i] = 0;
65
Rich Felker0b44a032011-02-12 00:22:29 -050066 h->h_name = h->h_aliases[0] = buf;
67 strcpy(h->h_name, canon);
68 buf += strlen(h->h_name)+1;
69
70 if (strcmp(h->h_name, name)) {
71 h->h_aliases[1] = buf;
72 strcpy(h->h_aliases[1], name);
73 buf += strlen(h->h_aliases[1])+1;
74 } else h->h_aliases[1] = 0;
75
76 h->h_aliases[2] = 0;
77
Rich Felker0b44a032011-02-12 00:22:29 -050078 *res = h;
Rich Felker0b44a032011-02-12 00:22:29 -050079 return 0;
80}