1 #include <sys/socket.h>
2 #include <netinet/in.h>
10 #include "stdio_impl.h"
14 static int is_valid_hostname(const char *host)
16 const unsigned char *s;
17 if (strnlen(host, 256)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
18 for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
22 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
26 if (flags & AI_PASSIVE) {
27 if (family != AF_INET6)
28 buf[cnt++] = (struct address){ .family = AF_INET };
29 if (family != AF_INET)
30 buf[cnt++] = (struct address){ .family = AF_INET6 };
32 if (family != AF_INET6)
33 buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
34 if (family != AF_INET)
35 buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
40 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
44 if (family != AF_INET6 && inet_aton(name, &a4)>0) {
45 memcpy(&buf[0].addr, &a4, sizeof a4);
46 buf[0].family = AF_INET;
49 if (family != AF_INET && inet_pton(AF_INET6, name, &a6)>0) {
50 memcpy(&buf[0].addr, &a6, sizeof a6);
51 buf[0].family = AF_INET6;
57 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
60 size_t l = strlen(name);
62 unsigned char _buf[1032];
63 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
65 while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
68 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
69 for(p=line+1; (p=strstr(p, name)) &&
70 (!isspace(p[-1]) || !isspace(p[l])); p++);
73 /* Isolate IP address to parse */
74 for (p=line; *p && !isspace(*p); p++);
76 if (name_from_numeric(buf+cnt, line, family))
79 /* Extract first name as canonical name */
80 for (; *p && isspace(*p); p++);
81 for (z=p; *z && !isspace(*z); z++);
83 if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
89 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
91 unsigned char reply[1024] = { 0 }, *p = reply;
95 /* Perform one or more DNS queries for host */
96 int result = __dns_query(reply, name, family, 0);
97 if (result < 0) return result;
99 for (i=0; i<result; i++) {
100 if (family != AF_INET6) {
101 int j = __dns_get_rr(&buf[cnt].addr, sizeof *buf, 4, MAXADDRS-cnt, p, RR_A, 0);
102 while (j--) buf[cnt++].family = AF_INET;
104 if (family != AF_INET) {
105 int j = __dns_get_rr(&buf[cnt].addr, sizeof *buf, 16, MAXADDRS-cnt, p, RR_AAAA, 0);
106 while (j--) buf[cnt++].family = AF_INET6;
110 __dns_get_rr(tmp, 0, 256, 1, reply, RR_CNAME, 1);
111 if (is_valid_hostname(tmp)) strcpy(canon, tmp);
115 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
122 if ((l = strnlen(name, 256))-1 > 254)
124 memcpy(canon, name, l+1);
127 /* Procedurally, a request for v6 addresses with the v4-mapped
128 * flag set is like a request for unspecified family, followed
129 * by filtering of the results. */
130 if (flags & AI_V4MAPPED) {
131 if (family == AF_INET6) family = AF_UNSPEC;
132 else flags -= AI_V4MAPPED;
135 /* Try each backend until there's at least one result. */
136 cnt = name_from_null(buf, name, family, flags);
137 if (cnt<=0) cnt = name_from_numeric(buf, name, family);
138 if (cnt<=0 && !(flags & AI_NUMERICHOST)) {
139 cnt = name_from_hosts(buf, canon, name, family);
140 if (cnt<=0) cnt = name_from_dns(buf, canon, name, family);
142 if (cnt<=0) return cnt ? cnt : EAI_NONAME;
144 /* Filter/transform results for v4-mapped lookup, if requested. */
145 if (flags & AI_V4MAPPED) {
146 if (!(flags & AI_ALL)) {
147 /* If any v6 results exist, remove v4 results. */
148 for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
150 for (j=0; i<cnt; i++) {
151 if (buf[i].family == AF_INET6)
157 /* Translate any remaining v4 results to v6 */
158 for (i=0; i<cnt; i++) {
159 if (buf[i].family != AF_INET) continue;
160 memcpy(buf[i].addr+12, buf[i].addr, 4);
161 memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
163 buf[i].family = AF_INET6;