4 #include <netinet/in.h>
5 #include <sys/socket.h>
10 #include "stdio_impl.h"
12 static int is_valid(const char *host)
14 const unsigned char *s;
15 if (strlen(host)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
16 for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
21 static int have_af(int family)
23 struct sockaddr_in6 sin6 = { .sin6_family = family };
24 socklen_t sl = family == AF_INET
25 ? sizeof(struct sockaddr_in)
26 : sizeof(struct sockaddr_in6);
27 int sock = socket(family, SOCK_STREAM, 0);
28 int have = !bind(sock, (void *)&sin6, sl);
38 struct sockaddr_in sin;
39 struct sockaddr_in6 sin6;
47 /* Extra slots needed for storing canonical name */
48 #define EXTRA ((256+sizeof(struct aibuf)-1)/sizeof(struct aibuf))
50 int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)
52 int flags = hint ? hint->ai_flags : 0;
53 int family = hint ? hint->ai_family : AF_UNSPEC;
54 int type = hint ? hint->ai_socktype : 0;
55 int proto = hint ? hint->ai_protocol : 0;
56 unsigned long port = 0;
59 unsigned char reply[1024];
64 unsigned char _buf[64];
69 if (host && strlen(host)>255) return EAI_NONAME;
70 if (serv && strlen(serv)>32) return EAI_SERVICE;
73 proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
75 type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
78 port = strtoul(serv, &z, 0);
79 if (!*z && port > 65535) return EAI_SERVICE;
81 if (flags & AI_NUMERICSERV) return EAI_SERVICE;
83 //f = fopen("/etc/services", "rb");
90 if (family == AF_UNSPEC) family = AF_INET;
91 buf = calloc(sizeof *buf, 1+EXTRA);
92 if (!buf) return EAI_MEMORY;
93 buf->ai.ai_protocol = proto;
94 buf->ai.ai_socktype = type;
95 buf->ai.ai_addr = (void *)&buf->sa;
96 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
97 buf->ai.ai_family = family;
98 buf->sa.sin.sin_family = family;
99 buf->sa.sin.sin_port = port;
100 if (!(flags & AI_PASSIVE)) {
101 if (family == AF_INET) {
102 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127;
103 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1;
104 } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1;
110 /* Try as a numeric address */
111 if (__ipparse(&sa, family, host) >= 0) {
112 buf = calloc(sizeof *buf, 1+EXTRA);
113 if (!buf) return EAI_MEMORY;
114 family = sa.sin.sin_family;
115 buf->ai.ai_protocol = proto;
116 buf->ai.ai_socktype = type;
117 buf->ai.ai_addr = (void *)&buf->sa;
118 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
119 buf->ai.ai_family = family;
120 buf->ai.ai_canonname = (char *)host;
122 buf->sa.sin.sin_port = port;
127 if (flags & AI_NUMERICHOST) return EAI_NONAME;
129 f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
130 if (f) while (fgets(line, sizeof line, f)) {
132 size_t l = strlen(host);
134 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
135 for(p=line+1; (p=strstr(p, host)) &&
136 (!isspace(p[-1]) || !isspace(p[l])); p++);
140 /* Isolate IP address to parse */
141 for (p=line; *p && !isspace(*p); p++);
143 if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
145 /* Allocate and fill result buffer */
146 buf = calloc(sizeof *buf, 1+EXTRA);
147 if (!buf) return EAI_MEMORY;
148 family = sa.sin.sin_family;
149 buf->ai.ai_protocol = proto;
150 buf->ai.ai_socktype = type;
151 buf->ai.ai_addr = (void *)&buf->sa;
152 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
153 buf->ai.ai_family = family;
155 buf->sa.sin.sin_port = port;
157 /* Extract first name as canonical name */
158 for (; *p && isspace(*p); p++);
159 buf->ai.ai_canonname = (void *)(buf+1);
160 snprintf(buf->ai.ai_canonname, 256, "%s", p);
161 for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
163 if (!is_valid(buf->ai.ai_canonname))
164 buf->ai.ai_canonname = 0;
169 if (f) __fclose_ca(f);
172 f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
173 if (f) while (fgets(line, sizeof line, f)) {
174 if (!isspace(line[10]) || (strncmp(line, "search", 6)
175 && strncmp(line, "domain", 6))) continue;
177 if (f) __fclose_ca(f);
180 /* Perform one or more DNS queries for host */
181 memset(reply, 0, sizeof reply);
182 result = __dns_query(reply, host, family, 0);
183 if (result < 0) return result;
185 cnt = __dns_count_addrs(reply, result);
186 if (cnt <= 0) return EAI_NONAME;
188 buf = calloc(sizeof *buf, cnt+EXTRA);
189 if (!buf) return EAI_MEMORY;
192 if (family != AF_INET6) {
193 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
194 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
196 if (family != AF_INET) {
197 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
198 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
201 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
202 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
203 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
204 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
207 if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) < 0)
208 strcpy((void *)&buf[cnt], host);
210 for (i=0; i<cnt; i++) {
211 buf[i].ai.ai_protocol = proto;
212 buf[i].ai.ai_socktype = type;
213 buf[i].ai.ai_addr = (void *)&buf[i].sa;
214 buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
215 ? sizeof sa.sin6 : sizeof sa.sin;
216 buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
217 buf[i].sa.sin.sin_port = port;
218 buf[i].ai.ai_next = &buf[i+1].ai;
219 buf[i].ai.ai_canonname = (void *)&buf[cnt];
221 buf[cnt-1].ai.ai_next = 0;