diff options
Diffstat (limited to 'src/lib/libc/net/getnameinfo.c')
-rw-r--r-- | src/lib/libc/net/getnameinfo.c | 356 |
1 files changed, 356 insertions, 0 deletions
diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c new file mode 100644 index 0000000000..830a94279e --- /dev/null +++ b/src/lib/libc/net/getnameinfo.c | |||
@@ -0,0 +1,356 @@ | |||
1 | /* $OpenBSD: getnameinfo.c,v 1.30 2005/03/25 13:24:12 otto Exp $ */ | ||
2 | /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | /* | ||
34 | * Issues to be discussed: | ||
35 | * - Thread safe-ness must be checked | ||
36 | * - RFC2553 says that we should raise error on short buffer. X/Open says | ||
37 | * we need to truncate the result. We obey RFC2553 (and X/Open should be | ||
38 | * modified). ipngwg rough consensus seems to follow RFC2553. | ||
39 | * - What is "local" in NI_FQDN? | ||
40 | * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. | ||
41 | * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if | ||
42 | * sin6_scope_id is filled - standardization status? | ||
43 | * XXX breaks backward compat for code that expects no scopeid. | ||
44 | * beware on merge. | ||
45 | */ | ||
46 | |||
47 | #ifndef INET6 | ||
48 | #define INET6 | ||
49 | #endif | ||
50 | |||
51 | #include <sys/types.h> | ||
52 | #include <sys/socket.h> | ||
53 | #include <net/if.h> | ||
54 | #include <netinet/in.h> | ||
55 | #include <arpa/inet.h> | ||
56 | #include <arpa/nameser.h> | ||
57 | #include <netdb.h> | ||
58 | #include <resolv.h> | ||
59 | #include <string.h> | ||
60 | #include <stddef.h> | ||
61 | |||
62 | #include "thread_private.h" | ||
63 | |||
64 | static const struct afd { | ||
65 | int a_af; | ||
66 | int a_addrlen; | ||
67 | int a_socklen; | ||
68 | int a_off; | ||
69 | } afdl [] = { | ||
70 | #ifdef INET6 | ||
71 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), | ||
72 | offsetof(struct sockaddr_in6, sin6_addr)}, | ||
73 | #endif | ||
74 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), | ||
75 | offsetof(struct sockaddr_in, sin_addr)}, | ||
76 | {0, 0, 0}, | ||
77 | }; | ||
78 | |||
79 | struct sockinet { | ||
80 | u_char si_len; | ||
81 | u_char si_family; | ||
82 | u_short si_port; | ||
83 | }; | ||
84 | |||
85 | #ifdef INET6 | ||
86 | static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, | ||
87 | size_t, int); | ||
88 | static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); | ||
89 | #endif | ||
90 | |||
91 | /* | ||
92 | * this mutex is also used by get_port in getaddrinfo.c | ||
93 | */ | ||
94 | void *__THREAD_NAME(serv_mutex); | ||
95 | |||
96 | int | ||
97 | getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, | ||
98 | size_t hostlen, char *serv, size_t servlen, int flags) | ||
99 | { | ||
100 | const struct afd *afd; | ||
101 | struct servent *sp; | ||
102 | struct hostent *hp; | ||
103 | u_short port; | ||
104 | int family, i; | ||
105 | const char *addr; | ||
106 | u_int32_t v4a; | ||
107 | char numserv[512]; | ||
108 | char numaddr[512]; | ||
109 | |||
110 | if (sa == NULL) | ||
111 | return EAI_FAIL; | ||
112 | |||
113 | if (sa->sa_len != salen) | ||
114 | return EAI_FAIL; | ||
115 | |||
116 | family = sa->sa_family; | ||
117 | for (i = 0; afdl[i].a_af; i++) | ||
118 | if (afdl[i].a_af == family) { | ||
119 | afd = &afdl[i]; | ||
120 | goto found; | ||
121 | } | ||
122 | return EAI_FAMILY; | ||
123 | |||
124 | found: | ||
125 | if (salen != afd->a_socklen) | ||
126 | return EAI_FAIL; | ||
127 | |||
128 | /* network byte order */ | ||
129 | port = ((const struct sockinet *)sa)->si_port; | ||
130 | addr = (const char *)sa + afd->a_off; | ||
131 | |||
132 | if (serv == NULL || servlen == 0) { | ||
133 | /* | ||
134 | * do nothing in this case. | ||
135 | * in case you are wondering if "&&" is more correct than | ||
136 | * "||" here: rfc2553bis-03 says that serv == NULL OR | ||
137 | * servlen == 0 means that the caller does not want the result. | ||
138 | */ | ||
139 | } else { | ||
140 | if (flags & NI_NUMERICSERV) | ||
141 | sp = NULL; | ||
142 | else { | ||
143 | _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); | ||
144 | sp = getservbyport(port, | ||
145 | (flags & NI_DGRAM) ? "udp" : "tcp"); | ||
146 | _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); | ||
147 | } | ||
148 | if (sp) { | ||
149 | if (strlen(sp->s_name) + 1 > servlen) | ||
150 | return EAI_MEMORY; | ||
151 | strlcpy(serv, sp->s_name, servlen); | ||
152 | } else { | ||
153 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | ||
154 | if (strlen(numserv) + 1 > servlen) | ||
155 | return EAI_MEMORY; | ||
156 | strlcpy(serv, numserv, servlen); | ||
157 | } | ||
158 | } | ||
159 | |||
160 | switch (sa->sa_family) { | ||
161 | case AF_INET: | ||
162 | v4a = (u_int32_t) | ||
163 | ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr); | ||
164 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) | ||
165 | flags |= NI_NUMERICHOST; | ||
166 | v4a >>= IN_CLASSA_NSHIFT; | ||
167 | if (v4a == 0) | ||
168 | flags |= NI_NUMERICHOST; | ||
169 | break; | ||
170 | #ifdef INET6 | ||
171 | case AF_INET6: | ||
172 | { | ||
173 | const struct sockaddr_in6 *sin6; | ||
174 | sin6 = (const struct sockaddr_in6 *)sa; | ||
175 | switch (sin6->sin6_addr.s6_addr[0]) { | ||
176 | case 0x00: | ||
177 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | ||
178 | ; | ||
179 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | ||
180 | ; | ||
181 | else | ||
182 | flags |= NI_NUMERICHOST; | ||
183 | break; | ||
184 | default: | ||
185 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | ||
186 | flags |= NI_NUMERICHOST; | ||
187 | } | ||
188 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | ||
189 | flags |= NI_NUMERICHOST; | ||
190 | break; | ||
191 | } | ||
192 | } | ||
193 | break; | ||
194 | #endif | ||
195 | } | ||
196 | if (host == NULL || hostlen == 0) { | ||
197 | /* | ||
198 | * do nothing in this case. | ||
199 | * in case you are wondering if "&&" is more correct than | ||
200 | * "||" here: rfc2553bis-03 says that host == NULL or | ||
201 | * hostlen == 0 means that the caller does not want the result. | ||
202 | */ | ||
203 | } else if (flags & NI_NUMERICHOST) { | ||
204 | int numaddrlen; | ||
205 | |||
206 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | ||
207 | if (flags & NI_NAMEREQD) | ||
208 | return EAI_NONAME; | ||
209 | |||
210 | switch(afd->a_af) { | ||
211 | #ifdef INET6 | ||
212 | case AF_INET6: | ||
213 | { | ||
214 | int error; | ||
215 | |||
216 | if ((error = ip6_parsenumeric(sa, addr, host, | ||
217 | hostlen, flags)) != 0) | ||
218 | return(error); | ||
219 | break; | ||
220 | } | ||
221 | #endif | ||
222 | default: | ||
223 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | ||
224 | == NULL) | ||
225 | return EAI_SYSTEM; | ||
226 | numaddrlen = strlen(numaddr); | ||
227 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | ||
228 | return EAI_MEMORY; | ||
229 | strlcpy(host, numaddr, hostlen); | ||
230 | break; | ||
231 | } | ||
232 | } else { | ||
233 | _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); | ||
234 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); | ||
235 | _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); | ||
236 | |||
237 | if (hp) { | ||
238 | #if 0 | ||
239 | /* | ||
240 | * commented out, since "for local host" is not | ||
241 | * implemented here - see RFC2553 p30 | ||
242 | */ | ||
243 | if (flags & NI_NOFQDN) { | ||
244 | char *p; | ||
245 | p = strchr(hp->h_name, '.'); | ||
246 | if (p) | ||
247 | *p = '\0'; | ||
248 | } | ||
249 | #endif | ||
250 | if (strlen(hp->h_name) + 1 > hostlen) { | ||
251 | return EAI_MEMORY; | ||
252 | } | ||
253 | strlcpy(host, hp->h_name, hostlen); | ||
254 | } else { | ||
255 | if (flags & NI_NAMEREQD) | ||
256 | return EAI_NONAME; | ||
257 | switch(afd->a_af) { | ||
258 | #ifdef INET6 | ||
259 | case AF_INET6: | ||
260 | { | ||
261 | int error; | ||
262 | |||
263 | if ((error = ip6_parsenumeric(sa, addr, host, | ||
264 | hostlen, | ||
265 | flags)) != 0) | ||
266 | return(error); | ||
267 | break; | ||
268 | } | ||
269 | #endif | ||
270 | default: | ||
271 | if (inet_ntop(afd->a_af, addr, host, | ||
272 | hostlen) == NULL) | ||
273 | return EAI_SYSTEM; | ||
274 | break; | ||
275 | } | ||
276 | } | ||
277 | } | ||
278 | return(0); | ||
279 | } | ||
280 | |||
281 | #ifdef INET6 | ||
282 | static int | ||
283 | ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host, | ||
284 | size_t hostlen, int flags) | ||
285 | { | ||
286 | int numaddrlen; | ||
287 | char numaddr[512]; | ||
288 | |||
289 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) | ||
290 | return EAI_SYSTEM; | ||
291 | |||
292 | numaddrlen = strlen(numaddr); | ||
293 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | ||
294 | return EAI_MEMORY; | ||
295 | strlcpy(host, numaddr, hostlen); | ||
296 | |||
297 | if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { | ||
298 | char zonebuf[MAXHOSTNAMELEN]; | ||
299 | int zonelen; | ||
300 | |||
301 | zonelen = ip6_sa2str( | ||
302 | (const struct sockaddr_in6 *)(const void *)sa, | ||
303 | zonebuf, sizeof(zonebuf), flags); | ||
304 | if (zonelen < 0) | ||
305 | return EAI_MEMORY; | ||
306 | if (zonelen + 1 + numaddrlen + 1 > hostlen) | ||
307 | return EAI_MEMORY; | ||
308 | |||
309 | /* construct <numeric-addr><delim><zoneid> */ | ||
310 | memcpy(host + numaddrlen + 1, zonebuf, | ||
311 | (size_t)zonelen); | ||
312 | host[numaddrlen] = SCOPE_DELIMITER; | ||
313 | host[numaddrlen + 1 + zonelen] = '\0'; | ||
314 | } | ||
315 | |||
316 | return 0; | ||
317 | } | ||
318 | |||
319 | /* ARGSUSED */ | ||
320 | static int | ||
321 | ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags) | ||
322 | { | ||
323 | unsigned int ifindex; | ||
324 | const struct in6_addr *a6; | ||
325 | int n; | ||
326 | |||
327 | ifindex = (unsigned int)sa6->sin6_scope_id; | ||
328 | a6 = &sa6->sin6_addr; | ||
329 | |||
330 | #ifdef notdef | ||
331 | if ((flags & NI_NUMERICSCOPE) != 0) { | ||
332 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | ||
333 | if (n < 0 || n >= bufsiz) | ||
334 | return -1; | ||
335 | else | ||
336 | return n; | ||
337 | } | ||
338 | #endif | ||
339 | |||
340 | /* if_indextoname() does not take buffer size. not a good api... */ | ||
341 | if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && | ||
342 | bufsiz >= IF_NAMESIZE) { | ||
343 | char *p = if_indextoname(ifindex, buf); | ||
344 | if (p) { | ||
345 | return(strlen(p)); | ||
346 | } | ||
347 | } | ||
348 | |||
349 | /* last resort */ | ||
350 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | ||
351 | if (n < 0 || n >= bufsiz) | ||
352 | return -1; | ||
353 | else | ||
354 | return n; | ||
355 | } | ||
356 | #endif /* INET6 */ | ||