1 /* $FreeBSD$ */ 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) NI_WITHSCOPEID when called with global address, 42 * and sin6_scope_id filled 43 */ 44 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <net/if.h> 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 50 #include <arpa/nameser.h> 51 #include <netdb.h> 52 #include <resolv.h> 53 #include <string.h> 54 #include <stddef.h> 55 #include <errno.h> 56 57 #define SUCCESS 0 58 #define ANY 0 59 #define YES 1 60 #define NO 0 61 62 static struct afd { 63 int a_af; 64 int a_addrlen; 65 int a_socklen; 66 int a_off; 67 } afdl [] = { 68 #ifdef INET6 69 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), 70 offsetof(struct sockaddr_in6, sin6_addr)}, 71 #endif 72 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), 73 offsetof(struct sockaddr_in, sin_addr)}, 74 {0, 0, 0}, 75 }; 76 77 struct sockinet { 78 u_char si_len; 79 u_char si_family; 80 u_short si_port; 81 }; 82 83 #ifdef INET6 84 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *, 85 size_t, int)); 86 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int)); 87 #endif 88 89 /* 2553bis: use EAI_xx for getnameinfo */ 90 #define ENI_NOSOCKET EAI_FAIL /*XXX*/ 91 #define ENI_NOSERVNAME EAI_NONAME 92 #define ENI_NOHOSTNAME EAI_NONAME 93 #define ENI_MEMORY EAI_MEMORY 94 #define ENI_SYSTEM EAI_SYSTEM 95 #define ENI_FAMILY EAI_FAMILY 96 #define ENI_SALEN EAI_FAMILY 97 98 int 99 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) 100 const struct sockaddr *sa; 101 size_t salen; 102 char *host; 103 size_t hostlen; 104 char *serv; 105 size_t servlen; 106 int flags; 107 { 108 struct afd *afd; 109 struct servent *sp; 110 struct hostent *hp; 111 u_short port; 112 int family, i; 113 const char *addr; 114 u_int32_t v4a; 115 int h_error; 116 char numserv[512]; 117 char numaddr[512]; 118 119 if (sa == NULL) 120 return ENI_NOSOCKET; 121 122 if (sa->sa_len != salen) 123 return ENI_SALEN; 124 125 family = sa->sa_family; 126 for (i = 0; afdl[i].a_af; i++) 127 if (afdl[i].a_af == family) { 128 afd = &afdl[i]; 129 goto found; 130 } 131 return ENI_FAMILY; 132 133 found: 134 if (salen != afd->a_socklen) 135 return ENI_SALEN; 136 137 /* network byte order */ 138 port = ((const struct sockinet *)sa)->si_port; 139 addr = (const char *)sa + afd->a_off; 140 141 if (serv == NULL || servlen == 0) { 142 /* 143 * do nothing in this case. 144 * in case you are wondering if "&&" is more correct than 145 * "||" here: RFC2553 says that serv == NULL OR servlen == 0 146 * means that the caller does not want the result. 147 */ 148 } else { 149 if (flags & NI_NUMERICSERV) 150 sp = NULL; 151 else { 152 sp = getservbyport(port, 153 (flags & NI_DGRAM) ? "udp" : "tcp"); 154 } 155 if (sp) { 156 if (strlen(sp->s_name) + 1 > servlen) 157 return ENI_MEMORY; 158 strcpy(serv, sp->s_name); 159 } else { 160 snprintf(numserv, sizeof(numserv), "%d", ntohs(port)); 161 if (strlen(numserv) + 1 > servlen) 162 return ENI_MEMORY; 163 strcpy(serv, numserv); 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: RFC2553 says that host == NULL OR hostlen == 0 208 * 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 ENI_NOHOSTNAME; 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 ENI_SYSTEM; 233 numaddrlen = strlen(numaddr); 234 if (numaddrlen + 1 > hostlen) /* don't forget terminator */ 235 return ENI_MEMORY; 236 strcpy(host, numaddr); 237 break; 238 } 239 } else { 240 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error); 241 242 if (hp) { 243 #if 0 244 /* 245 * commented out, since "for local host" is not 246 * implemented here - see RFC2553 p30 247 */ 248 if (flags & NI_NOFQDN) { 249 char *p; 250 p = strchr(hp->h_name, '.'); 251 if (p) 252 *p = '\0'; 253 } 254 #endif 255 if (strlen(hp->h_name) + 1 > hostlen) { 256 freehostent(hp); 257 return ENI_MEMORY; 258 } 259 strcpy(host, hp->h_name); 260 freehostent(hp); 261 } else { 262 if (flags & NI_NAMEREQD) 263 return ENI_NOHOSTNAME; 264 switch(afd->a_af) { 265 #ifdef INET6 266 case AF_INET6: 267 { 268 int error; 269 270 if ((error = ip6_parsenumeric(sa, addr, host, 271 hostlen, 272 flags)) != 0) 273 return(error); 274 break; 275 } 276 #endif 277 default: 278 if (inet_ntop(afd->a_af, addr, host, 279 hostlen) == NULL) 280 return ENI_SYSTEM; 281 break; 282 } 283 } 284 } 285 return SUCCESS; 286 } 287 288 #ifdef INET6 289 static int 290 ip6_parsenumeric(sa, addr, host, hostlen, flags) 291 const struct sockaddr *sa; 292 const char *addr; 293 char *host; 294 size_t hostlen; 295 int flags; 296 { 297 int numaddrlen; 298 char numaddr[512]; 299 300 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) 301 == NULL) 302 return ENI_SYSTEM; 303 304 numaddrlen = strlen(numaddr); 305 if (numaddrlen + 1 > hostlen) /* don't forget terminator */ 306 return ENI_MEMORY; 307 strcpy(host, numaddr); 308 309 #ifdef NI_WITHSCOPEID 310 if ( 311 #ifdef DONT_OPAQUE_SCOPEID 312 (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) || 313 IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) && 314 #endif 315 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) { 316 #ifndef ALWAYS_WITHSCOPE 317 if (flags & NI_WITHSCOPEID) 318 #endif /* !ALWAYS_WITHSCOPE */ 319 { 320 char scopebuf[MAXHOSTNAMELEN]; 321 int scopelen; 322 323 /* ip6_sa2str never fails */ 324 scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa, 325 scopebuf, sizeof(scopebuf), 326 flags); 327 if (scopelen + 1 + numaddrlen + 1 > hostlen) 328 return ENI_MEMORY; 329 /* 330 * construct <numeric-addr><delim><scopeid> 331 */ 332 memcpy(host + numaddrlen + 1, scopebuf, 333 scopelen); 334 host[numaddrlen] = SCOPE_DELIMITER; 335 host[numaddrlen + 1 + scopelen] = '\0'; 336 } 337 } 338 #endif /* NI_WITHSCOPEID */ 339 340 return 0; 341 } 342 343 /* ARGSUSED */ 344 static int 345 ip6_sa2str(sa6, buf, bufsiz, flags) 346 const struct sockaddr_in6 *sa6; 347 char *buf; 348 size_t bufsiz; 349 int flags; 350 { 351 unsigned int ifindex = (unsigned int)sa6->sin6_scope_id; 352 const struct in6_addr *a6 = &sa6->sin6_addr; 353 354 #ifdef NI_NUMERICSCOPE 355 if (flags & NI_NUMERICSCOPE) { 356 return(snprintf(buf, bufsiz, "%d", sa6->sin6_scope_id)); 357 } 358 #endif 359 360 /* if_indextoname() does not take buffer size. not a good api... */ 361 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && 362 bufsiz >= IF_NAMESIZE) { 363 char *p = if_indextoname(ifindex, buf); 364 if (p) { 365 return(strlen(p)); 366 } 367 } 368 369 /* last resort */ 370 return(snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id)); 371 } 372 #endif /* INET6 */ 373