diff options
| author | itojun <> | 1999-12-30 08:54:20 +0000 |
|---|---|---|
| committer | itojun <> | 1999-12-30 08:54:20 +0000 |
| commit | d9056b0e3240e176c6cfdfc13ac1cf384b0a04fd (patch) | |
| tree | 356686aebb42f6ae34e6f80d29a8d44b44ff2879 /src/lib/libc/net/getnameinfo.c | |
| parent | c90229eaa6df37dd29c45c1aa632cb37ca8cfa06 (diff) | |
| download | openbsd-d9056b0e3240e176c6cfdfc13ac1cf384b0a04fd.tar.gz openbsd-d9056b0e3240e176c6cfdfc13ac1cf384b0a04fd.tar.bz2 openbsd-d9056b0e3240e176c6cfdfc13ac1cf384b0a04fd.zip | |
replace NRL get{addr,name}info with KAME get{addr,name}info.
removed functionality:
new code will not return AF_LOCAL addrinfo struct.
added funtionality:
SOCK_RAW is permitted as ai_socktype (no servname allowed).
draft-ietf-ipngwg-scopedaddr-format-00.txt
Diffstat (limited to 'src/lib/libc/net/getnameinfo.c')
| -rw-r--r-- | src/lib/libc/net/getnameinfo.c | 446 |
1 files changed, 233 insertions, 213 deletions
diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c index 62c4b9d928..787beec9c6 100644 --- a/src/lib/libc/net/getnameinfo.c +++ b/src/lib/libc/net/getnameinfo.c | |||
| @@ -1,7 +1,9 @@ | |||
| 1 | /* $OpenBSD: getnameinfo.c,v 1.5 1999/12/30 08:54:20 itojun Exp $ */ | ||
| 2 | |||
| 1 | /* | 3 | /* |
| 2 | * %%% copyright-cmetz-96-bsd | 4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
| 3 | * Copyright (c) 1996-1999, Craig Metz, All rights reserved. | 5 | * All rights reserved. |
| 4 | * | 6 | * |
| 5 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
| 7 | * are met: | 9 | * are met: |
| @@ -10,18 +12,14 @@ | |||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software | 15 | * 3. Neither the name of the project nor the names of its contributors |
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by Craig Metz and | ||
| 16 | * by other contributors. | ||
| 17 | * 4. Neither the name of the author nor the names of contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | 16 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. | 17 | * without specific prior written permission. |
| 20 | * | 18 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| @@ -29,236 +27,258 @@ | |||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
| 32 | * | ||
| 33 | */ | 30 | */ |
| 34 | 31 | ||
| 35 | /* getnameinfo() v1.38 */ | 32 | /* |
| 33 | * Issues to be discussed: | ||
| 34 | * - Thread safe-ness must be checked | ||
| 35 | * - Return values. There seems to be no standard for return value (RFC2553) | ||
| 36 | * but INRIA implementation returns EAI_xxx defined for getaddrinfo(). | ||
| 37 | * - RFC2553 says that we should raise error on short buffer. X/Open says | ||
| 38 | * we need to truncate the result. We obey RFC2553 (and X/Open should be | ||
| 39 | * modified). | ||
| 40 | */ | ||
| 41 | |||
| 42 | #if 0 | ||
| 43 | #ifdef HAVE_CONFIG_H | ||
| 44 | #include "config.h" | ||
| 45 | #endif | ||
| 46 | #else | ||
| 47 | #define HAVE_SA_LEN | ||
| 48 | #define INET6 | ||
| 49 | #endif | ||
| 36 | 50 | ||
| 37 | #include <sys/types.h> | 51 | #include <sys/types.h> |
| 38 | #include <sys/socket.h> | 52 | #include <sys/socket.h> |
| 53 | #include <net/if.h> | ||
| 39 | #include <netinet/in.h> | 54 | #include <netinet/in.h> |
| 40 | #include <sys/un.h> | ||
| 41 | #include <sys/utsname.h> | ||
| 42 | #include <netdb.h> | ||
| 43 | #include <arpa/inet.h> | 55 | #include <arpa/inet.h> |
| 44 | #include <errno.h> | 56 | #include <arpa/nameser.h> |
| 45 | #include <string.h> | 57 | #include <netdb.h> |
| 46 | #include <resolv.h> | 58 | #include <resolv.h> |
| 59 | #include <string.h> | ||
| 60 | #include <stddef.h> | ||
| 47 | 61 | ||
| 48 | #ifndef min | 62 | #if 0 |
| 49 | #define min(x,y) (((x) > (y)) ? (y) : (x)) | 63 | #ifndef HAVE_PORTABLE_PROTOTYPE |
| 50 | #endif /* min */ | 64 | #include "cdecl_ext.h" |
| 51 | 65 | #endif | |
| 52 | static int | ||
| 53 | netdb_lookup_name(int family, void *addr, int addrlen, char *name, | ||
| 54 | int namelen, int flags) | ||
| 55 | { | ||
| 56 | struct hostent *hostent; | ||
| 57 | char *c, *c2; | ||
| 58 | int i; | ||
| 59 | |||
| 60 | if (!(hostent = gethostbyaddr(addr, addrlen, family))) { | ||
| 61 | switch (h_errno) { | ||
| 62 | case NETDB_INTERNAL: | ||
| 63 | return(EAI_SYSTEM); | ||
| 64 | case HOST_NOT_FOUND: | ||
| 65 | return(1); | ||
| 66 | case TRY_AGAIN: | ||
| 67 | return(EAI_AGAIN); | ||
| 68 | case NO_RECOVERY: | ||
| 69 | return(EAI_FAIL); | ||
| 70 | case NO_DATA: | ||
| 71 | return(1); | ||
| 72 | default: | ||
| 73 | return(EAI_FAIL); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | endhostent(); | ||
| 78 | |||
| 79 | c = hostent->h_name; | ||
| 80 | if ((flags & NI_NOFQDN) && (_res.options & RES_INIT) && _res.defdname[0] && | ||
| 81 | (c2 = strstr(c + 1, _res.defdname)) && (*(--c2) == '.')) { | ||
| 82 | *c2 = 0; | ||
| 83 | i = min(c2 - c, namelen); | ||
| 84 | strlcpy(name, c, i); | ||
| 85 | } else | ||
| 86 | strlcpy(name, c, namelen); | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | int | ||
| 91 | getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, | ||
| 92 | size_t hostlen, char *serv, size_t servlen, int flags) | ||
| 93 | { | ||
| 94 | int rval; | ||
| 95 | int saved_errno; | ||
| 96 | |||
| 97 | if (sa == NULL || addrlen != sa->sa_len) | ||
| 98 | return EAI_FAIL; | ||
| 99 | saved_errno = errno; | ||
| 100 | 66 | ||
| 101 | if (host && hostlen > 0) { | 67 | #ifndef HAVE_ADDRINFO |
| 102 | switch (sa->sa_family) { | 68 | #include "addrinfo.h" |
| 103 | case AF_INET6: | 69 | #endif |
| 104 | { | 70 | #endif |
| 105 | struct sockaddr_in6 *sin6 = (void *)sa; | ||
| 106 | 71 | ||
| 107 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { | 72 | #define SUCCESS 0 |
| 108 | if (flags & NI_NUMERICHOST) | 73 | #define ANY 0 |
| 109 | goto inet6_noname; | 74 | #define YES 1 |
| 110 | strlcpy(host, "*", hostlen); | 75 | #define NO 0 |
| 111 | break; | ||
| 112 | } | ||
| 113 | 76 | ||
| 114 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { | 77 | static struct afd { |
| 115 | struct sockaddr_in sin; | 78 | int a_af; |
| 79 | int a_addrlen; | ||
| 80 | int a_socklen; | ||
| 81 | int a_off; | ||
| 82 | } afdl [] = { | ||
| 83 | #ifdef INET6 | ||
| 84 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), | ||
| 85 | offsetof(struct sockaddr_in6, sin6_addr)}, | ||
| 86 | #endif | ||
| 87 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), | ||
| 88 | offsetof(struct sockaddr_in, sin_addr)}, | ||
| 89 | {0, 0, 0}, | ||
| 90 | }; | ||
| 116 | 91 | ||
| 117 | memset(&sin, 0, sizeof(struct sockaddr_in)); | 92 | struct sockinet { |
| 118 | sin.sin_len = sizeof(struct sockaddr_in); | 93 | u_char si_len; |
| 119 | sin.sin_family = AF_INET; | 94 | u_char si_family; |
| 120 | sin.sin_port = sin6->sin6_port; | 95 | u_short si_port; |
| 121 | sin.sin_addr.s_addr = | 96 | }; |
| 122 | ((u_int32_t *)&sin6->sin6_addr)[3]; | ||
| 123 | if (!(rval = getnameinfo((struct sockaddr *)&sin, | ||
| 124 | sizeof(struct sockaddr_in), host, hostlen, | ||
| 125 | serv, servlen, flags | NI_NAMEREQD))) | ||
| 126 | goto ret; | ||
| 127 | if (rval != EAI_NONAME) | ||
| 128 | goto ret; | ||
| 129 | goto inet6_noname; | ||
| 130 | } | ||
| 131 | 97 | ||
| 132 | if (flags & NI_NUMERICHOST) | 98 | #define ENI_NOSOCKET 0 |
| 133 | goto inet6_noname; | 99 | #define ENI_NOSERVNAME 1 |
| 134 | if ((rval = netdb_lookup_name(AF_INET6, | 100 | #define ENI_NOHOSTNAME 2 |
| 135 | &sin6->sin6_addr, sizeof(struct in6_addr), | 101 | #define ENI_MEMORY 3 |
| 136 | host, hostlen, flags)) < 0) | 102 | #define ENI_SYSTEM 4 |
| 137 | goto ret; | 103 | #define ENI_FAMILY 5 |
| 104 | #define ENI_SALEN 6 | ||
| 138 | 105 | ||
| 139 | if (!rval) | 106 | int |
| 140 | break; | 107 | getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) |
| 141 | inet6_noname: | 108 | const struct sockaddr *sa; |
| 142 | if (flags & NI_NAMEREQD) { | 109 | size_t salen; |
| 143 | rval = EAI_NONAME; | 110 | char *host; |
| 144 | goto ret; | 111 | size_t hostlen; |
| 145 | } | 112 | char *serv; |
| 146 | if (!inet_ntop(AF_INET6, &sin6->sin6_addr, host, hostlen)) { | 113 | size_t servlen; |
| 147 | rval = EAI_NONAME; | 114 | int flags; |
| 148 | goto ret; | 115 | { |
| 149 | } | 116 | struct afd *afd; |
| 150 | break; | 117 | struct servent *sp; |
| 151 | } | 118 | struct hostent *hp; |
| 152 | case AF_INET: | 119 | u_short port; |
| 153 | { | 120 | int family, i; |
| 154 | struct sockaddr_in *sin = (void *)sa; | 121 | char *addr, *p; |
| 122 | u_int32_t v4a; | ||
| 123 | int h_error; | ||
| 124 | char numserv[512]; | ||
| 125 | char numaddr[512]; | ||
| 155 | 126 | ||
| 156 | if (flags & NI_NUMERICHOST) | 127 | if (sa == NULL) |
| 157 | goto inet_noname; | 128 | return ENI_NOSOCKET; |
| 158 | 129 | ||
| 159 | if (sin->sin_addr.s_addr == 0) { | 130 | #ifdef HAVE_SA_LEN /*XXX*/ |
| 160 | strlcpy(host, "*", hostlen); | 131 | if (sa->sa_len != salen) |
| 161 | break; | 132 | return ENI_SALEN; |
| 162 | } | 133 | #endif |
| 134 | |||
| 135 | family = sa->sa_family; | ||
| 136 | for (i = 0; afdl[i].a_af; i++) | ||
| 137 | if (afdl[i].a_af == family) { | ||
| 138 | afd = &afdl[i]; | ||
| 139 | goto found; | ||
| 140 | } | ||
| 141 | return ENI_FAMILY; | ||
| 142 | |||
| 143 | found: | ||
| 144 | if (salen != afd->a_socklen) | ||
| 145 | return ENI_SALEN; | ||
| 146 | |||
| 147 | port = ((struct sockinet *)sa)->si_port; /* network byte order */ | ||
| 148 | addr = (char *)sa + afd->a_off; | ||
| 163 | 149 | ||
| 164 | if ((rval = netdb_lookup_name(AF_INET, | 150 | if (serv == NULL || servlen == 0) { |
| 165 | &sin->sin_addr, sizeof(struct in_addr), | 151 | /* |
| 166 | host, hostlen, flags)) < 0) | 152 | * do nothing in this case. |
| 167 | goto ret; | 153 | * in case you are wondering if "&&" is more correct than |
| 154 | * "||" here: RFC2553 says that serv == NULL OR servlen == 0 | ||
| 155 | * means that the caller does not want the result. | ||
| 156 | */ | ||
| 157 | } else { | ||
| 158 | if (flags & NI_NUMERICSERV) | ||
| 159 | sp = NULL; | ||
| 160 | else { | ||
| 161 | sp = getservbyport(port, | ||
| 162 | (flags & NI_DGRAM) ? "udp" : "tcp"); | ||
| 163 | } | ||
| 164 | if (sp) { | ||
| 165 | if (strlen(sp->s_name) > servlen) | ||
| 166 | return ENI_MEMORY; | ||
| 167 | strcpy(serv, sp->s_name); | ||
| 168 | } else { | ||
| 169 | snprintf(numserv, sizeof(numserv), "%d", ntohs(port)); | ||
| 170 | if (strlen(numserv) > servlen) | ||
| 171 | return ENI_MEMORY; | ||
| 172 | strcpy(serv, numserv); | ||
| 173 | } | ||
| 174 | } | ||
| 168 | 175 | ||
| 169 | if (!rval) | 176 | switch (sa->sa_family) { |
| 170 | break; | 177 | case AF_INET: |
| 171 | inet_noname: | 178 | v4a = (u_int32_t) |
| 172 | if (flags & NI_NAMEREQD) { | 179 | ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr); |
| 173 | rval = EAI_NONAME; | 180 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) |
| 174 | goto ret; | 181 | flags |= NI_NUMERICHOST; |
| 175 | } | 182 | v4a >>= IN_CLASSA_NSHIFT; |
| 176 | if (!inet_ntop(AF_INET, &sin->sin_addr, host, hostlen)) { | 183 | if (v4a == 0 || v4a == IN_LOOPBACKNET) |
| 177 | rval = EAI_NONAME; | 184 | flags |= NI_NUMERICHOST; |
| 178 | goto ret; | 185 | break; |
| 179 | } | 186 | #ifdef INET6 |
| 187 | case AF_INET6: | ||
| 188 | { | ||
| 189 | struct sockaddr_in6 *sin6; | ||
| 190 | sin6 = (struct sockaddr_in6 *)sa; | ||
| 191 | switch (sin6->sin6_addr.s6_addr[0]) { | ||
| 192 | case 0x00: | ||
| 193 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | ||
| 194 | ; | ||
| 195 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | ||
| 196 | ; | ||
| 197 | else | ||
| 198 | flags |= NI_NUMERICHOST; | ||
| 180 | break; | 199 | break; |
| 181 | } | 200 | default: |
| 182 | case AF_LOCAL: | 201 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 183 | if (!(flags & NI_NUMERICHOST)) { | 202 | flags |= NI_NUMERICHOST; |
| 184 | struct utsname utsname; | ||
| 185 | |||
| 186 | if (!uname(&utsname)) { | ||
| 187 | strlcpy(host, utsname.nodename, hostlen); | ||
| 188 | break; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | if (flags & NI_NAMEREQD) { | ||
| 193 | rval = EAI_NONAME; | ||
| 194 | goto ret; | ||
| 195 | } | 203 | } |
| 196 | 204 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | |
| 197 | strlcpy(host, "localhost", hostlen); | 205 | flags |= NI_NUMERICHOST; |
| 198 | break; | 206 | break; |
| 199 | default: | ||
| 200 | rval = EAI_FAMILY; | ||
| 201 | goto ret; | ||
| 202 | } | 207 | } |
| 208 | } | ||
| 209 | break; | ||
| 210 | #endif | ||
| 203 | } | 211 | } |
| 212 | if (host == NULL || hostlen == 0) { | ||
| 213 | /* | ||
| 214 | * do nothing in this case. | ||
| 215 | * in case you are wondering if "&&" is more correct than | ||
| 216 | * "||" here: RFC2553 says that host == NULL OR hostlen == 0 | ||
| 217 | * means that the caller does not want the result. | ||
| 218 | */ | ||
| 219 | } else if (flags & NI_NUMERICHOST) { | ||
| 220 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | ||
| 221 | if (flags & NI_NAMEREQD) | ||
| 222 | return ENI_NOHOSTNAME; | ||
| 223 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | ||
| 224 | == NULL) | ||
| 225 | return ENI_SYSTEM; | ||
| 226 | if (strlen(numaddr) > hostlen) | ||
| 227 | return ENI_MEMORY; | ||
| 228 | strcpy(host, numaddr); | ||
| 229 | #if defined(INET6) && defined(NI_WITHSCOPEID) | ||
| 230 | if (afd->a_af == AF_INET6 && | ||
| 231 | (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) || | ||
| 232 | IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) && | ||
| 233 | ((struct sockaddr_in6 *)sa)->sin6_scope_id) { | ||
| 234 | #ifndef ALWAYS_WITHSCOPE | ||
| 235 | if (flags & NI_WITHSCOPEID) | ||
| 236 | #endif /* !ALWAYS_WITHSCOPE */ | ||
| 237 | { | ||
| 238 | char *ep = strchr(host, '\0'); | ||
| 239 | unsigned int ifindex = | ||
| 240 | ((struct sockaddr_in6 *)sa)->sin6_scope_id; | ||
| 204 | 241 | ||
| 205 | if (serv && servlen > 0) { | 242 | *ep = SCOPE_DELIMITER; |
| 206 | switch (sa->sa_family) { | 243 | if ((if_indextoname(ifindex, ep + 1)) == NULL) |
| 207 | case AF_INET: | 244 | /* XXX what should we do? */ |
| 208 | { | 245 | strncpy(ep + 1, "???", 3); |
| 209 | struct sockaddr_in *sin = (void *)sa; | ||
| 210 | struct servent *s; | ||
| 211 | |||
| 212 | if ((flags & NI_NUMERICSERV) == 0) { | ||
| 213 | s = getservbyport(sin->sin_port, | ||
| 214 | (flags & NI_DGRAM) ? "udp" : "tcp"); | ||
| 215 | if (s) { | ||
| 216 | strlcpy(serv, s->s_name, servlen); | ||
| 217 | break; | ||
| 218 | } | ||
| 219 | if (sin->sin_port == 0) { | ||
| 220 | strlcpy(serv, "*", servlen); | ||
| 221 | break; | ||
| 222 | } | ||
| 223 | } | 246 | } |
| 224 | snprintf(serv, servlen, "%d", ntohs(sin->sin_port)); | 247 | } |
| 225 | break; | 248 | #endif /* INET6 */ |
| 226 | } | 249 | } else { |
| 227 | case AF_INET6: | 250 | #ifdef USE_GETIPNODEBY |
| 228 | { | 251 | hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error); |
| 229 | struct sockaddr_in6 *sin6 = (void *)sa; | 252 | #else |
| 230 | struct servent *s; | 253 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); |
| 231 | 254 | h_error = h_errno; | |
| 232 | if ((flags & NI_NUMERICSERV) == 0) { | 255 | #endif |
| 233 | 256 | ||
| 234 | s = getservbyport(sin6->sin6_port, | 257 | if (hp) { |
| 235 | (flags & NI_DGRAM) ? "udp" : "tcp"); | 258 | if (flags & NI_NOFQDN) { |
| 236 | if (s) { | 259 | p = strchr(hp->h_name, '.'); |
| 237 | strlcpy(serv, s->s_name, servlen); | 260 | if (p) *p = '\0'; |
| 238 | break; | ||
| 239 | } | ||
| 240 | if (sin6->sin6_port == 0) { | ||
| 241 | strlcpy(serv, "*", servlen); | ||
| 242 | break; | ||
| 243 | } | ||
| 244 | } | 261 | } |
| 245 | snprintf(serv, servlen, "%d", ntohs(sin6->sin6_port)); | 262 | if (strlen(hp->h_name) > hostlen) { |
| 246 | break; | 263 | #ifdef USE_GETIPNODEBY |
| 247 | } | 264 | freehostent(hp); |
| 248 | case AF_LOCAL: | 265 | #endif |
| 249 | { | 266 | return ENI_MEMORY; |
| 250 | struct sockaddr_un *sun = (void *)sa; | 267 | } |
| 251 | 268 | strcpy(host, hp->h_name); | |
| 252 | strlcpy(serv, sun->sun_path, servlen); | 269 | #ifdef USE_GETIPNODEBY |
| 253 | break; | 270 | freehostent(hp); |
| 254 | } | 271 | #endif |
| 272 | } else { | ||
| 273 | if (flags & NI_NAMEREQD) | ||
| 274 | return ENI_NOHOSTNAME; | ||
| 275 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | ||
| 276 | == NULL) | ||
| 277 | return ENI_NOHOSTNAME; | ||
| 278 | if (strlen(numaddr) > hostlen) | ||
| 279 | return ENI_MEMORY; | ||
| 280 | strcpy(host, numaddr); | ||
| 255 | } | 281 | } |
| 256 | } | 282 | } |
| 257 | rval = 0; | 283 | return SUCCESS; |
| 258 | |||
| 259 | ret: | ||
| 260 | if (rval == 1) | ||
| 261 | rval = EAI_FAIL; | ||
| 262 | errno = saved_errno; | ||
| 263 | return (rval); | ||
| 264 | } | 284 | } |
