OLD | NEW |
1 #include <netdb.h> | 1 #include <netdb.h> |
2 #include <limits.h> | 2 #include <limits.h> |
3 #include <string.h> | 3 #include <string.h> |
4 #include <stdio.h> | 4 #include <stdio.h> |
5 #include <sys/socket.h> | 5 #include <sys/socket.h> |
6 #include <netinet/in.h> | 6 #include <netinet/in.h> |
7 #include <arpa/inet.h> | 7 #include <arpa/inet.h> |
8 #include <net/if.h> | 8 #include <net/if.h> |
9 #include <ctype.h> | 9 #include <ctype.h> |
10 #include "lookup.h" | 10 #include "lookup.h" |
11 #include "stdio_impl.h" | 11 #include "stdio_impl.h" |
12 | 12 |
13 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, i
nt, const void *), void *); | 13 int __dns_parse(const unsigned char*, |
14 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned cha
r *, char *, int); | 14 int, |
15 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const
unsigned char*, unsigned char *, int); | 15 int (*)(void*, int, const void*, int, const void*), |
16 int __res_send(const unsigned char *, int, unsigned char *, int); | 16 void*); |
| 17 int __dn_expand(const unsigned char*, |
| 18 const unsigned char*, |
| 19 const unsigned char*, |
| 20 char*, |
| 21 int); |
| 22 int __res_mkquery(int, |
| 23 const char*, |
| 24 int, |
| 25 int, |
| 26 const unsigned char*, |
| 27 int, |
| 28 const unsigned char*, |
| 29 unsigned char*, |
| 30 int); |
| 31 int __res_send(const unsigned char*, int, unsigned char*, int); |
17 | 32 |
18 #define PTR_MAX (64 + sizeof ".in-addr.arpa") | 33 #define PTR_MAX (64 + sizeof ".in-addr.arpa") |
19 #define RR_PTR 12 | 34 #define RR_PTR 12 |
20 | 35 |
21 static char *itoa(char *p, unsigned x) { | 36 static char* itoa(char* p, unsigned x) { |
22 » p += 3*sizeof(int); | 37 p += 3 * sizeof(int); |
23 » *--p = 0; | 38 *--p = 0; |
24 » do { | 39 do { |
25 » » *--p = '0' + x % 10; | 40 *--p = '0' + x % 10; |
26 » » x /= 10; | 41 x /= 10; |
27 » } while (x); | 42 } while (x); |
28 » return p; | 43 return p; |
29 } | 44 } |
30 | 45 |
31 static void mkptr4(char *s, const unsigned char *ip) | 46 static void mkptr4(char* s, const unsigned char* ip) { |
32 { | 47 sprintf(s, "%d.%d.%d.%d.in-addr.arpa", ip[3], ip[2], ip[1], ip[0]); |
33 » sprintf(s, "%d.%d.%d.%d.in-addr.arpa", | 48 } |
34 » » ip[3], ip[2], ip[1], ip[0]); | 49 |
35 } | 50 static void mkptr6(char* s, const unsigned char* ip) { |
36 | 51 static const char xdigits[] = "0123456789abcdef"; |
37 static void mkptr6(char *s, const unsigned char *ip) | 52 int i; |
38 { | 53 for (i = 15; i >= 0; i--) { |
39 » static const char xdigits[] = "0123456789abcdef"; | 54 *s++ = xdigits[ip[i] & 15]; |
40 » int i; | 55 *s++ = '.'; |
41 » for (i=15; i>=0; i--) { | 56 *s++ = xdigits[ip[i] >> 4]; |
42 » » *s++ = xdigits[ip[i]&15]; *s++ = '.'; | 57 *s++ = '.'; |
43 » » *s++ = xdigits[ip[i]>>4]; *s++ = '.'; | 58 } |
44 » } | 59 strcpy(s, "ip6.arpa"); |
45 » strcpy(s, "ip6.arpa"); | 60 } |
46 } | 61 |
47 | 62 static void reverse_hosts(char* buf, |
48 static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, i
nt family) | 63 const unsigned char* a, |
49 { | 64 unsigned scopeid, |
50 » char line[512], *p, *z; | 65 int family) { |
51 » unsigned char _buf[1032], atmp[16]; | 66 char line[512], *p, *z; |
52 » struct address iplit; | 67 unsigned char _buf[1032], atmp[16]; |
53 » FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); | 68 struct address iplit; |
54 » if (!f) return; | 69 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); |
55 » if (family == AF_INET) { | 70 if (!f) |
56 » » memcpy(atmp+12, a, 4); | 71 return; |
57 » » memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); | 72 if (family == AF_INET) { |
58 » » a = atmp; | 73 memcpy(atmp + 12, a, 4); |
59 » } | 74 memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); |
60 » while (fgets(line, sizeof line, f)) { | 75 a = atmp; |
61 » » if ((p=strchr(line, '#'))) *p++='\n', *p=0; | 76 } |
62 | 77 while (fgets(line, sizeof line, f)) { |
63 » » for (p=line; *p && !isspace(*p); p++); | 78 if ((p = strchr(line, '#'))) |
64 » » *p++ = 0; | 79 *p++ = '\n', *p = 0; |
65 » » if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0) | 80 |
66 » » » continue; | 81 for (p = line; *p && !isspace(*p); p++) |
67 | 82 ; |
68 » » if (iplit.family == AF_INET) { | 83 *p++ = 0; |
69 » » » memcpy(iplit.addr+12, iplit.addr, 4); | 84 if (__lookup_ipliteral(&iplit, line, AF_UNSPEC) <= 0) |
70 » » » memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); | 85 continue; |
71 » » » iplit.scopeid = 0; | 86 |
72 » » } | 87 if (iplit.family == AF_INET) { |
73 | 88 memcpy(iplit.addr + 12, iplit.addr, 4); |
74 » » if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid) | 89 memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); |
75 » » » continue; | 90 iplit.scopeid = 0; |
76 » » » | 91 } |
77 » » for (; *p && isspace(*p); p++); | 92 |
78 » » for (z=p; *z && !isspace(*z); z++); | 93 if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid) |
79 » » *z = 0; | 94 continue; |
80 » » if (z-p < 256) { | 95 |
81 » » » memcpy(buf, p, z-p+1); | 96 for (; *p && isspace(*p); p++) |
82 » » » break; | 97 ; |
83 » » } | 98 for (z = p; *z && !isspace(*z); z++) |
84 » } | 99 ; |
85 » __fclose_ca(f); | 100 *z = 0; |
86 } | 101 if (z - p < 256) { |
87 | 102 memcpy(buf, p, z - p + 1); |
88 static void reverse_services(char *buf, int port, int dgram) | 103 break; |
89 { | 104 } |
90 » unsigned long svport; | 105 } |
91 » char line[128], *p, *z; | 106 __fclose_ca(f); |
92 » unsigned char _buf[1032]; | 107 } |
93 » FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); | 108 |
94 » if (!f) return; | 109 static void reverse_services(char* buf, int port, int dgram) { |
95 » while (fgets(line, sizeof line, f)) { | 110 unsigned long svport; |
96 » » if ((p=strchr(line, '#'))) *p++='\n', *p=0; | 111 char line[128], *p, *z; |
97 | 112 unsigned char _buf[1032]; |
98 » » for (p=line; *p && !isspace(*p); p++); | 113 FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); |
99 » » if (!*p) continue; | 114 if (!f) |
100 » » *p++ = 0; | 115 return; |
101 » » svport = strtoul(p, &z, 10); | 116 while (fgets(line, sizeof line, f)) { |
102 | 117 if ((p = strchr(line, '#'))) |
103 » » if (svport != port || z==p) continue; | 118 *p++ = '\n', *p = 0; |
104 » » if (dgram && strncmp(z, "/udp", 4)) continue; | 119 |
105 » » if (!dgram && strncmp(z, "/tcp", 4)) continue; | 120 for (p = line; *p && !isspace(*p); p++) |
106 » » if (p-line > 32) continue; | 121 ; |
107 | 122 if (!*p) |
108 » » memcpy(buf, line, p-line); | 123 continue; |
109 » » break; | 124 *p++ = 0; |
110 » } | 125 svport = strtoul(p, &z, 10); |
111 » __fclose_ca(f); | 126 |
112 } | 127 if (svport != port || z == p) |
113 | 128 continue; |
114 static int dns_parse_callback(void *c, int rr, const void *data, int len, const
void *packet) | 129 if (dgram && strncmp(z, "/udp", 4)) |
115 { | 130 continue; |
116 » if (rr != RR_PTR) return 0; | 131 if (!dgram && strncmp(z, "/tcp", 4)) |
117 » if (__dn_expand(packet, (const unsigned char *)packet + 512, | 132 continue; |
118 » data, c, 256) <= 0) | 133 if (p - line > 32) |
119 » » *(char *)c = 0; | 134 continue; |
120 » return 0; | 135 |
121 » | 136 memcpy(buf, line, p - line); |
122 } | 137 break; |
123 | 138 } |
124 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, | 139 __fclose_ca(f); |
125 » char *restrict node, socklen_t nodelen, | 140 } |
126 » char *restrict serv, socklen_t servlen, | 141 |
127 » int flags) | 142 static int dns_parse_callback(void* c, |
128 { | 143 int rr, |
129 » char ptr[PTR_MAX]; | 144 const void* data, |
130 » char buf[256], num[3*sizeof(int)+1]; | 145 int len, |
131 » int af = sa->sa_family; | 146 const void* packet) { |
132 » unsigned char *a; | 147 if (rr != RR_PTR) |
133 » unsigned scopeid; | 148 return 0; |
134 | 149 if (__dn_expand(packet, (const unsigned char*)packet + 512, data, c, 256) <= |
135 » switch (af) { | 150 0) |
136 » case AF_INET: | 151 *(char*)c = 0; |
137 » » a = (void *)&((struct sockaddr_in *)sa)->sin_addr; | 152 return 0; |
138 » » if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY; | 153 } |
139 » » mkptr4(ptr, a); | 154 |
140 » » scopeid = 0; | 155 int getnameinfo(const struct sockaddr* restrict sa, |
141 » » break; | 156 socklen_t sl, |
142 » case AF_INET6: | 157 char* restrict node, |
143 » » a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr; | 158 socklen_t nodelen, |
144 » » if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY; | 159 char* restrict serv, |
145 » » if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12)) | 160 socklen_t servlen, |
146 » » » mkptr6(ptr, a); | 161 int flags) { |
147 » » else | 162 char ptr[PTR_MAX]; |
148 » » » mkptr4(ptr, a+12); | 163 char buf[256], num[3 * sizeof(int) + 1]; |
149 » » scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id; | 164 int af = sa->sa_family; |
150 » » break; | 165 unsigned char* a; |
151 » default: | 166 unsigned scopeid; |
152 » » return EAI_FAMILY; | 167 |
153 » } | 168 switch (af) { |
154 | 169 case AF_INET: |
155 » if (node && nodelen) { | 170 a = (void*)&((struct sockaddr_in*)sa)->sin_addr; |
156 » » buf[0] = 0; | 171 if (sl < sizeof(struct sockaddr_in)) |
157 » » if (!(flags & NI_NUMERICHOST)) { | 172 return EAI_FAMILY; |
158 » » » reverse_hosts(buf, a, scopeid, af); | 173 mkptr4(ptr, a); |
159 » » } | 174 scopeid = 0; |
160 » » if (!*buf && !(flags & NI_NUMERICHOST)) { | 175 break; |
161 » » » unsigned char query[18+PTR_MAX], reply[512]; | 176 case AF_INET6: |
162 » » » int qlen = __res_mkquery(0, ptr, 1, RR_PTR, | 177 a = (void*)&((struct sockaddr_in6*)sa)->sin6_addr; |
163 » » » » 0, 0, 0, query, sizeof query); | 178 if (sl < sizeof(struct sockaddr_in6)) |
164 » » » int rlen = __res_send(query, qlen, reply, sizeof reply); | 179 return EAI_FAMILY; |
165 » » » buf[0] = 0; | 180 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12)) |
166 » » » if (rlen > 0) | 181 mkptr6(ptr, a); |
167 » » » » __dns_parse(reply, rlen, dns_parse_callback, buf
); | 182 else |
168 » » } | 183 mkptr4(ptr, a + 12); |
169 » » if (!*buf) { | 184 scopeid = ((struct sockaddr_in6*)sa)->sin6_scope_id; |
170 » » » if (flags & NI_NAMEREQD) return EAI_NONAME; | 185 break; |
171 » » » inet_ntop(af, a, buf, sizeof buf); | 186 default: |
172 » » » if (scopeid) { | 187 return EAI_FAMILY; |
173 » » » » char *p = 0, tmp[IF_NAMESIZE+1]; | 188 } |
174 » » » » if (!(flags & NI_NUMERICSCOPE) && | 189 |
175 » » » » (IN6_IS_ADDR_LINKLOCAL(a) || | 190 if (node && nodelen) { |
176 » » » » IN6_IS_ADDR_MC_LINKLOCAL(a))) | 191 buf[0] = 0; |
177 » » » » » p = if_indextoname(scopeid, tmp+1); | 192 if (!(flags & NI_NUMERICHOST)) { |
178 » » » » if (!p) | 193 reverse_hosts(buf, a, scopeid, af); |
179 » » » » » p = itoa(num, scopeid); | 194 } |
180 » » » » *--p = '%'; | 195 if (!*buf && !(flags & NI_NUMERICHOST)) { |
181 » » » » strcat(buf, p); | 196 unsigned char query[18 + PTR_MAX], reply[512]; |
182 » » » } | 197 int qlen = __res_mkquery(0, ptr, 1, RR_PTR, 0, 0, 0, query, sizeof query); |
183 » » } | 198 int rlen = __res_send(query, qlen, reply, sizeof reply); |
184 » » if (strlen(buf) >= nodelen) return EAI_OVERFLOW; | 199 buf[0] = 0; |
185 » » strcpy(node, buf); | 200 if (rlen > 0) |
186 » } | 201 __dns_parse(reply, rlen, dns_parse_callback, buf); |
187 | 202 } |
188 » if (serv && servlen) { | 203 if (!*buf) { |
189 » » char *p = buf; | 204 if (flags & NI_NAMEREQD) |
190 » » int port = ntohs(((struct sockaddr_in *)sa)->sin_port); | 205 return EAI_NONAME; |
191 » » buf[0] = 0; | 206 inet_ntop(af, a, buf, sizeof buf); |
192 » » if (!(flags & NI_NUMERICSERV)) | 207 if (scopeid) { |
193 » » » reverse_services(buf, port, flags & NI_DGRAM); | 208 char *p = 0, tmp[IF_NAMESIZE + 1]; |
194 » » if (!*p) | 209 if (!(flags & NI_NUMERICSCOPE) && |
195 » » » p = itoa(num, port); | 210 (IN6_IS_ADDR_LINKLOCAL(a) || IN6_IS_ADDR_MC_LINKLOCAL(a))) |
196 » » if (strlen(p) >= servlen) | 211 p = if_indextoname(scopeid, tmp + 1); |
197 » » » return EAI_OVERFLOW; | 212 if (!p) |
198 » » strcpy(serv, p); | 213 p = itoa(num, scopeid); |
199 » } | 214 *--p = '%'; |
200 | 215 strcat(buf, p); |
201 » return 0; | 216 } |
202 } | 217 } |
| 218 if (strlen(buf) >= nodelen) |
| 219 return EAI_OVERFLOW; |
| 220 strcpy(node, buf); |
| 221 } |
| 222 |
| 223 if (serv && servlen) { |
| 224 char* p = buf; |
| 225 int port = ntohs(((struct sockaddr_in*)sa)->sin_port); |
| 226 buf[0] = 0; |
| 227 if (!(flags & NI_NUMERICSERV)) |
| 228 reverse_services(buf, port, flags & NI_DGRAM); |
| 229 if (!*p) |
| 230 p = itoa(num, port); |
| 231 if (strlen(p) >= servlen) |
| 232 return EAI_OVERFLOW; |
| 233 strcpy(serv, p); |
| 234 } |
| 235 |
| 236 return 0; |
| 237 } |
OLD | NEW |