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];
63 unsigned char _buf[1024];
68 if (host && strlen(host)>255) return EAI_NONAME;
69 if (serv && strlen(serv)>32) return EAI_SERVICE;
72 proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
74 type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
77 if (!*serv) return EAI_SERVICE;
78 port = strtoul(serv, &z, 10);
79 if (!*z && port > 65535) return EAI_SERVICE;
81 size_t servlen = strlen(serv);
84 if (flags & AI_NUMERICSERV) return EAI_SERVICE;
86 f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
87 if (!f) return EAI_SERVICE;
88 while (fgets(line, sizeof line, f)) {
89 if (strncmp(line, serv, servlen))
91 if (sscanf(line+servlen, "%lu/%3s", &port, protname) < 2)
93 if (strcmp(protname, proto==IPPROTO_UDP ? "udp" : "tcp"))
98 if (feof(f)) return EAI_SERVICE;
104 if (family == AF_UNSPEC) family = AF_INET;
105 buf = calloc(sizeof *buf, 1+EXTRA);
106 if (!buf) return EAI_MEMORY;
107 buf->ai.ai_protocol = proto;
108 buf->ai.ai_socktype = type;
109 buf->ai.ai_addr = (void *)&buf->sa;
110 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
111 buf->ai.ai_family = family;
112 buf->sa.sin.sin_family = family;
113 buf->sa.sin.sin_port = port;
114 if (!(flags & AI_PASSIVE)) {
115 if (family == AF_INET) {
116 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127;
117 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1;
118 } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1;
124 if (!*host) return EAI_NONAME;
126 /* Try as a numeric address */
127 if (__ipparse(&sa, family, host) >= 0) {
128 buf = calloc(sizeof *buf, 1+EXTRA);
129 if (!buf) return EAI_MEMORY;
130 family = sa.sin.sin_family;
131 buf->ai.ai_protocol = proto;
132 buf->ai.ai_socktype = type;
133 buf->ai.ai_addr = (void *)&buf->sa;
134 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
135 buf->ai.ai_family = family;
136 buf->ai.ai_canonname = (char *)host;
138 buf->sa.sin.sin_port = port;
143 if (flags & AI_NUMERICHOST) return EAI_NONAME;
145 f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
146 if (f) while (fgets(line, sizeof line, f)) {
148 size_t l = strlen(host);
150 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
151 for(p=line+1; (p=strstr(p, host)) &&
152 (!isspace(p[-1]) || !isspace(p[l])); p++);
156 /* Isolate IP address to parse */
157 for (p=line; *p && !isspace(*p); p++);
159 if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
161 /* Allocate and fill result buffer */
162 buf = calloc(sizeof *buf, 1+EXTRA);
163 if (!buf) return EAI_MEMORY;
164 family = sa.sin.sin_family;
165 buf->ai.ai_protocol = proto;
166 buf->ai.ai_socktype = type;
167 buf->ai.ai_addr = (void *)&buf->sa;
168 buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
169 buf->ai.ai_family = family;
171 buf->sa.sin.sin_port = port;
173 /* Extract first name as canonical name */
174 for (; *p && isspace(*p); p++);
175 buf->ai.ai_canonname = (void *)(buf+1);
176 snprintf(buf->ai.ai_canonname, 256, "%s", p);
177 for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
179 if (!is_valid(buf->ai.ai_canonname))
180 buf->ai.ai_canonname = 0;
185 if (f) __fclose_ca(f);
188 f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
189 if (f) while (fgets(line, sizeof line, f)) {
190 if (!isspace(line[10]) || (strncmp(line, "search", 6)
191 && strncmp(line, "domain", 6))) continue;
193 if (f) __fclose_ca(f);
196 /* Perform one or more DNS queries for host */
197 memset(reply, 0, sizeof reply);
198 result = __dns_query(reply, host, family, 0);
199 if (result < 0) return result;
201 cnt = __dns_count_addrs(reply, result);
202 if (cnt <= 0) return EAI_NONAME;
204 buf = calloc(sizeof *buf, cnt+EXTRA);
205 if (!buf) return EAI_MEMORY;
208 if (family != AF_INET6) {
209 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
210 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
212 if (family != AF_INET) {
213 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
214 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
217 j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
218 while (j--) buf[i++].sa.sin.sin_family = AF_INET;
219 j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
220 while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
223 if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) < 0)
224 strcpy((void *)&buf[cnt], host);
226 for (i=0; i<cnt; i++) {
227 buf[i].ai.ai_protocol = proto;
228 buf[i].ai.ai_socktype = type;
229 buf[i].ai.ai_addr = (void *)&buf[i].sa;
230 buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
231 ? sizeof sa.sin6 : sizeof sa.sin;
232 buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
233 buf[i].sa.sin.sin_port = port;
234 buf[i].ai.ai_next = &buf[i+1].ai;
235 buf[i].ai.ai_canonname = (void *)&buf[cnt];
237 buf[cnt-1].ai.ai_next = 0;