1 #include <stdlib.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <netdb.h>
5 #include <string.h>
6 #include <pthread.h>
7 #include <unistd.h>
8 #include <endian.h>
9 #include <errno.h>
10 #include "lookup.h"
11
getaddrinfo(const char * restrict host,const char * restrict serv,const struct addrinfo * restrict hint,struct addrinfo ** restrict res)12 int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res)
13 {
14 struct service ports[MAXSERVS];
15 struct address addrs[MAXADDRS];
16 char canon[256], *outcanon;
17 int nservs, naddrs, nais, canon_len, i, j, k;
18 int family = AF_UNSPEC, flags = 0, proto = 0, socktype = 0;
19 struct aibuf *out;
20
21 if (!host && !serv) return EAI_NONAME;
22
23 if (hint) {
24 family = hint->ai_family;
25 flags = hint->ai_flags;
26 proto = hint->ai_protocol;
27 socktype = hint->ai_socktype;
28
29 const int mask = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST |
30 AI_V4MAPPED | AI_ALL | AI_ADDRCONFIG | AI_NUMERICSERV;
31 if ((flags & mask) != flags)
32 return EAI_BADFLAGS;
33
34 switch (family) {
35 case AF_INET:
36 case AF_INET6:
37 case AF_UNSPEC:
38 break;
39 default:
40 return EAI_FAMILY;
41 }
42 }
43
44 if (flags & AI_ADDRCONFIG) {
45 /* Define the "an address is configured" condition for address
46 * families via ability to create a socket for the family plus
47 * routability of the loopback address for the family. */
48 static const struct sockaddr_in lo4 = {
49 .sin_family = AF_INET, .sin_port = 65535,
50 .sin_addr.s_addr = __BYTE_ORDER == __BIG_ENDIAN
51 ? 0x7f000001 : 0x0100007f
52 };
53 static const struct sockaddr_in6 lo6 = {
54 .sin6_family = AF_INET6, .sin6_port = 65535,
55 .sin6_addr = IN6ADDR_LOOPBACK_INIT
56 };
57 int tf[2] = { AF_INET, AF_INET6 };
58 const void *ta[2] = { &lo4, &lo6 };
59 socklen_t tl[2] = { sizeof lo4, sizeof lo6 };
60 for (i=0; i<2; i++) {
61 if (family==tf[1-i]) continue;
62 int s = socket(tf[i], SOCK_CLOEXEC|SOCK_DGRAM,
63 IPPROTO_UDP);
64 if (s>=0) {
65 int cs;
66 pthread_setcancelstate(
67 PTHREAD_CANCEL_DISABLE, &cs);
68 int r = connect(s, ta[i], tl[i]);
69 int saved_errno = errno;
70 pthread_setcancelstate(cs, 0);
71 close(s);
72 if (!r) continue;
73 errno = saved_errno;
74 }
75 switch (errno) {
76 case EADDRNOTAVAIL:
77 case EAFNOSUPPORT:
78 case EHOSTUNREACH:
79 case ENETDOWN:
80 case ENETUNREACH:
81 break;
82 default:
83 return EAI_SYSTEM;
84 }
85 if (family == tf[i]) return EAI_NONAME;
86 family = tf[1-i];
87 }
88 }
89
90 nservs = __lookup_serv(ports, serv, proto, socktype, flags);
91 if (nservs < 0) return nservs;
92
93 naddrs = __lookup_name(addrs, canon, host, family, flags);
94 if (naddrs < 0) return naddrs;
95
96 nais = nservs * naddrs;
97 canon_len = strlen(canon);
98 out = calloc(1, nais * sizeof(*out) + canon_len + 1);
99 if (!out) return EAI_MEMORY;
100
101 if (canon_len) {
102 outcanon = (void *)&out[nais];
103 memcpy(outcanon, canon, canon_len+1);
104 } else {
105 outcanon = 0;
106 }
107
108 for (k=i=0; i<naddrs; i++) for (j=0; j<nservs; j++, k++) {
109 out[k].slot = k;
110 out[k].ai = (struct addrinfo){
111 .ai_family = addrs[i].family,
112 .ai_socktype = ports[j].socktype,
113 .ai_protocol = ports[j].proto,
114 .ai_addrlen = addrs[i].family == AF_INET
115 ? sizeof(struct sockaddr_in)
116 : sizeof(struct sockaddr_in6),
117 .ai_addr = (void *)&out[k].sa,
118 .ai_canonname = outcanon };
119 if (k) out[k-1].ai.ai_next = &out[k].ai;
120 switch (addrs[i].family) {
121 case AF_INET:
122 out[k].sa.sin.sin_family = AF_INET;
123 out[k].sa.sin.sin_port = htons(ports[j].port);
124 memcpy(&out[k].sa.sin.sin_addr, &addrs[i].addr, 4);
125 break;
126 case AF_INET6:
127 out[k].sa.sin6.sin6_family = AF_INET6;
128 out[k].sa.sin6.sin6_port = htons(ports[j].port);
129 out[k].sa.sin6.sin6_scope_id = addrs[i].scopeid;
130 memcpy(&out[k].sa.sin6.sin6_addr, &addrs[i].addr, 16);
131 break;
132 }
133 }
134 out[0].ref = nais;
135 *res = &out->ai;
136 return 0;
137 }
138