diff options
Diffstat (limited to 'src/lib/libc/net/getnameinfo.c')
-rw-r--r-- | src/lib/libc/net/getnameinfo.c | 351 |
1 files changed, 0 insertions, 351 deletions
diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c deleted file mode 100644 index 7041a4ee48..0000000000 --- a/src/lib/libc/net/getnameinfo.c +++ /dev/null | |||
@@ -1,351 +0,0 @@ | |||
1 | /* $OpenBSD: getnameinfo.c,v 1.33 2007/02/15 04:25:35 ray 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 | void *__THREAD_NAME(serv_mutex); | ||
92 | |||
93 | int | ||
94 | getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, | ||
95 | size_t hostlen, char *serv, size_t servlen, int flags) | ||
96 | { | ||
97 | const struct afd *afd; | ||
98 | struct hostent *hp; | ||
99 | u_short port; | ||
100 | int family, i; | ||
101 | const char *addr; | ||
102 | u_int32_t v4a; | ||
103 | char numserv[512]; | ||
104 | char numaddr[512]; | ||
105 | |||
106 | if (sa == NULL) | ||
107 | return EAI_FAIL; | ||
108 | |||
109 | family = sa->sa_family; | ||
110 | for (i = 0; afdl[i].a_af; i++) | ||
111 | if (afdl[i].a_af == family) { | ||
112 | afd = &afdl[i]; | ||
113 | goto found; | ||
114 | } | ||
115 | return EAI_FAMILY; | ||
116 | |||
117 | found: | ||
118 | if (salen != afd->a_socklen) | ||
119 | return EAI_FAIL; | ||
120 | |||
121 | /* network byte order */ | ||
122 | port = ((const struct sockinet *)sa)->si_port; | ||
123 | addr = (const char *)sa + afd->a_off; | ||
124 | |||
125 | if (serv == NULL || servlen == 0) { | ||
126 | /* | ||
127 | * do nothing in this case. | ||
128 | * in case you are wondering if "&&" is more correct than | ||
129 | * "||" here: rfc2553bis-03 says that serv == NULL OR | ||
130 | * servlen == 0 means that the caller does not want the result. | ||
131 | */ | ||
132 | } else if (!(flags & NI_NUMERICSERV)) { | ||
133 | struct servent sp; | ||
134 | struct servent_data sd; | ||
135 | |||
136 | (void)memset(&sd, 0, sizeof(sd)); | ||
137 | if (getservbyport_r(port, | ||
138 | (flags & NI_DGRAM) ? "udp" : "tcp", &sp, &sd) == -1) | ||
139 | goto numeric; | ||
140 | |||
141 | if (strlen(sp.s_name) + 1 > servlen) { | ||
142 | endservent_r(&sd); | ||
143 | return EAI_MEMORY; | ||
144 | } | ||
145 | strlcpy(serv, sp.s_name, servlen); | ||
146 | endservent_r(&sd); | ||
147 | } else { | ||
148 | numeric: | ||
149 | i = snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | ||
150 | if (i < 0 || i >= servlen || i >= sizeof(numserv)) | ||
151 | return EAI_MEMORY; | ||
152 | strlcpy(serv, numserv, servlen); | ||
153 | } | ||
154 | |||
155 | switch (sa->sa_family) { | ||
156 | case AF_INET: | ||
157 | v4a = (u_int32_t) | ||
158 | ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr); | ||
159 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) | ||
160 | flags |= NI_NUMERICHOST; | ||
161 | v4a >>= IN_CLASSA_NSHIFT; | ||
162 | if (v4a == 0) | ||
163 | flags |= NI_NUMERICHOST; | ||
164 | break; | ||
165 | #ifdef INET6 | ||
166 | case AF_INET6: | ||
167 | { | ||
168 | const struct sockaddr_in6 *sin6; | ||
169 | sin6 = (const struct sockaddr_in6 *)sa; | ||
170 | switch (sin6->sin6_addr.s6_addr[0]) { | ||
171 | case 0x00: | ||
172 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | ||
173 | ; | ||
174 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | ||
175 | ; | ||
176 | else | ||
177 | flags |= NI_NUMERICHOST; | ||
178 | break; | ||
179 | default: | ||
180 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | ||
181 | flags |= NI_NUMERICHOST; | ||
182 | } | ||
183 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | ||
184 | flags |= NI_NUMERICHOST; | ||
185 | break; | ||
186 | } | ||
187 | } | ||
188 | break; | ||
189 | #endif | ||
190 | } | ||
191 | if (host == NULL || hostlen == 0) { | ||
192 | /* | ||
193 | * do nothing in this case. | ||
194 | * in case you are wondering if "&&" is more correct than | ||
195 | * "||" here: rfc2553bis-03 says that host == NULL or | ||
196 | * hostlen == 0 means that the caller does not want the result. | ||
197 | */ | ||
198 | } else if (flags & NI_NUMERICHOST) { | ||
199 | int numaddrlen; | ||
200 | |||
201 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | ||
202 | if (flags & NI_NAMEREQD) | ||
203 | return EAI_NONAME; | ||
204 | |||
205 | switch(afd->a_af) { | ||
206 | #ifdef INET6 | ||
207 | case AF_INET6: | ||
208 | { | ||
209 | int error; | ||
210 | |||
211 | if ((error = ip6_parsenumeric(sa, addr, host, | ||
212 | hostlen, flags)) != 0) | ||
213 | return(error); | ||
214 | break; | ||
215 | } | ||
216 | #endif | ||
217 | default: | ||
218 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | ||
219 | == NULL) | ||
220 | return EAI_SYSTEM; | ||
221 | numaddrlen = strlen(numaddr); | ||
222 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | ||
223 | return EAI_MEMORY; | ||
224 | strlcpy(host, numaddr, hostlen); | ||
225 | break; | ||
226 | } | ||
227 | } else { | ||
228 | _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); | ||
229 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); | ||
230 | _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); | ||
231 | |||
232 | if (hp) { | ||
233 | #if 0 | ||
234 | /* | ||
235 | * commented out, since "for local host" is not | ||
236 | * implemented here - see RFC2553 p30 | ||
237 | */ | ||
238 | if (flags & NI_NOFQDN) { | ||
239 | char *p; | ||
240 | p = strchr(hp->h_name, '.'); | ||
241 | if (p) | ||
242 | *p = '\0'; | ||
243 | } | ||
244 | #endif | ||
245 | if (strlen(hp->h_name) + 1 > hostlen) { | ||
246 | return EAI_MEMORY; | ||
247 | } | ||
248 | strlcpy(host, hp->h_name, hostlen); | ||
249 | } else { | ||
250 | if (flags & NI_NAMEREQD) | ||
251 | return EAI_NONAME; | ||
252 | switch(afd->a_af) { | ||
253 | #ifdef INET6 | ||
254 | case AF_INET6: | ||
255 | { | ||
256 | int error; | ||
257 | |||
258 | if ((error = ip6_parsenumeric(sa, addr, host, | ||
259 | hostlen, | ||
260 | flags)) != 0) | ||
261 | return(error); | ||
262 | break; | ||
263 | } | ||
264 | #endif | ||
265 | default: | ||
266 | if (inet_ntop(afd->a_af, addr, host, | ||
267 | hostlen) == NULL) | ||
268 | return EAI_SYSTEM; | ||
269 | break; | ||
270 | } | ||
271 | } | ||
272 | } | ||
273 | return(0); | ||
274 | } | ||
275 | |||
276 | #ifdef INET6 | ||
277 | static int | ||
278 | ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host, | ||
279 | size_t hostlen, int flags) | ||
280 | { | ||
281 | int numaddrlen; | ||
282 | char numaddr[512]; | ||
283 | |||
284 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) | ||
285 | return EAI_SYSTEM; | ||
286 | |||
287 | numaddrlen = strlen(numaddr); | ||
288 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | ||
289 | return EAI_MEMORY; | ||
290 | strlcpy(host, numaddr, hostlen); | ||
291 | |||
292 | if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { | ||
293 | char zonebuf[MAXHOSTNAMELEN]; | ||
294 | int zonelen; | ||
295 | |||
296 | zonelen = ip6_sa2str( | ||
297 | (const struct sockaddr_in6 *)(const void *)sa, | ||
298 | zonebuf, sizeof(zonebuf), flags); | ||
299 | if (zonelen < 0) | ||
300 | return EAI_MEMORY; | ||
301 | if (zonelen + 1 + numaddrlen + 1 > hostlen) | ||
302 | return EAI_MEMORY; | ||
303 | |||
304 | /* construct <numeric-addr><delim><zoneid> */ | ||
305 | memcpy(host + numaddrlen + 1, zonebuf, | ||
306 | (size_t)zonelen); | ||
307 | host[numaddrlen] = SCOPE_DELIMITER; | ||
308 | host[numaddrlen + 1 + zonelen] = '\0'; | ||
309 | } | ||
310 | |||
311 | return 0; | ||
312 | } | ||
313 | |||
314 | /* ARGSUSED */ | ||
315 | static int | ||
316 | ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags) | ||
317 | { | ||
318 | unsigned int ifindex; | ||
319 | const struct in6_addr *a6; | ||
320 | int n; | ||
321 | |||
322 | ifindex = (unsigned int)sa6->sin6_scope_id; | ||
323 | a6 = &sa6->sin6_addr; | ||
324 | |||
325 | #ifdef notdef | ||
326 | if ((flags & NI_NUMERICSCOPE) != 0) { | ||
327 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | ||
328 | if (n < 0 || n >= bufsiz) | ||
329 | return -1; | ||
330 | else | ||
331 | return n; | ||
332 | } | ||
333 | #endif | ||
334 | |||
335 | /* if_indextoname() does not take buffer size. not a good api... */ | ||
336 | if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) || | ||
337 | IN6_IS_ADDR_MC_INTFACELOCAL(a6)) && bufsiz >= IF_NAMESIZE) { | ||
338 | char *p = if_indextoname(ifindex, buf); | ||
339 | if (p) { | ||
340 | return(strlen(p)); | ||
341 | } | ||
342 | } | ||
343 | |||
344 | /* last resort */ | ||
345 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | ||
346 | if (n < 0 || n >= bufsiz) | ||
347 | return -1; | ||
348 | else | ||
349 | return n; | ||
350 | } | ||
351 | #endif /* INET6 */ | ||