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