Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.15
      1 /*	$NetBSD: getnameinfo.c,v 1.15 2000/04/24 10:40:25 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     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 #include <sys/cdefs.h>
     43 #if defined(LIBC_SCCS) && !defined(lint)
     44 __RCSID("$NetBSD: getnameinfo.c,v 1.15 2000/04/24 10:40:25 itojun Exp $");
     45 #endif /* LIBC_SCCS and not lint */
     46 
     47 #include "namespace.h"
     48 #include <sys/types.h>
     49 #include <sys/socket.h>
     50 #include <net/if.h>
     51 #include <netinet/in.h>
     52 #include <arpa/inet.h>
     53 #include <arpa/nameser.h>
     54 #include <netdb.h>
     55 #include <resolv.h>
     56 #include <string.h>
     57 #include <stddef.h>
     58 
     59 #ifdef __weak_alias
     60 __weak_alias(getnameinfo,_getnameinfo)
     61 #endif
     62 
     63 #define SUCCESS 0
     64 #define ANY 0
     65 #define YES 1
     66 #define NO  0
     67 
     68 static struct afd {
     69 	int a_af;
     70 	int a_addrlen;
     71 	int a_socklen;
     72 	int a_off;
     73 } afdl [] = {
     74 #ifdef INET6
     75 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     76 		offsetof(struct sockaddr_in6, sin6_addr)},
     77 #endif
     78 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     79 		offsetof(struct sockaddr_in, sin_addr)},
     80 	{0, 0, 0},
     81 };
     82 
     83 struct sockinet {
     84 	u_char	si_len;
     85 	u_char	si_family;
     86 	u_short	si_port;
     87 };
     88 
     89 #ifdef INET6
     90 static char *ip6_sa2str __P((struct sockaddr_in6 *, char *, int));
     91 #endif
     92 
     93 #define ENI_NOSOCKET 	0
     94 #define ENI_NOSERVNAME	1
     95 #define ENI_NOHOSTNAME	2
     96 #define ENI_MEMORY	3
     97 #define ENI_SYSTEM	4
     98 #define ENI_FAMILY	5
     99 #define ENI_SALEN	6
    100 
    101 int
    102 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
    103 	const struct sockaddr *sa;
    104 	size_t salen;
    105 	char *host;
    106 	size_t hostlen;
    107 	char *serv;
    108 	size_t servlen;
    109 	int flags;
    110 {
    111 	struct afd *afd;
    112 	struct servent *sp;
    113 	struct hostent *hp;
    114 	u_short port;
    115 	int family, i;
    116 	char *addr, *p;
    117 	u_int32_t v4a;
    118 	int h_error;
    119 	char numserv[512];
    120 	char numaddr[512];
    121 
    122 	if (sa == NULL)
    123 		return ENI_NOSOCKET;
    124 
    125 #ifdef BSD4_4
    126 	if (sa->sa_len != salen)
    127 		return ENI_SALEN;
    128 #endif
    129 
    130 	family = sa->sa_family;
    131 	for (i = 0; afdl[i].a_af; i++)
    132 		if (afdl[i].a_af == family) {
    133 			afd = &afdl[i];
    134 			goto found;
    135 		}
    136 	return ENI_FAMILY;
    137 
    138  found:
    139 	if (salen != afd->a_socklen)
    140 		return ENI_SALEN;
    141 
    142 	port = ((struct sockinet *)sa)->si_port; /* network byte order */
    143 	addr = (char *)sa + afd->a_off;
    144 
    145 	if (serv == NULL || servlen == 0) {
    146 		/*
    147 		 * do nothing in this case.
    148 		 * in case you are wondering if "&&" is more correct than
    149 		 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
    150 		 * means that the caller does not want the result.
    151 		 */
    152 	} else {
    153 		if (flags & NI_NUMERICSERV)
    154 			sp = NULL;
    155 		else {
    156 			sp = getservbyport(port,
    157 				(flags & NI_DGRAM) ? "udp" : "tcp");
    158 		}
    159 		if (sp) {
    160 			if (strlen(sp->s_name) > servlen)
    161 				return ENI_MEMORY;
    162 			strcpy(serv, sp->s_name);
    163 		} else {
    164 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    165 			if (strlen(numserv) > servlen)
    166 				return ENI_MEMORY;
    167 			strcpy(serv, numserv);
    168 		}
    169 	}
    170 
    171 	switch (sa->sa_family) {
    172 	case AF_INET:
    173 		v4a = (u_int32_t)
    174 			ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
    175 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    176 			flags |= NI_NUMERICHOST;
    177 		v4a >>= IN_CLASSA_NSHIFT;
    178 		if (v4a == 0)
    179 			flags |= NI_NUMERICHOST;
    180 		break;
    181 #ifdef INET6
    182 	case AF_INET6:
    183 	    {
    184 		struct sockaddr_in6 *sin6;
    185 		sin6 = (struct sockaddr_in6 *)sa;
    186 		switch (sin6->sin6_addr.s6_addr[0]) {
    187 		case 0x00:
    188 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    189 				;
    190 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    191 				;
    192 			else
    193 				flags |= NI_NUMERICHOST;
    194 			break;
    195 		default:
    196 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    197 				flags |= NI_NUMERICHOST;
    198 			}
    199 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    200 				flags |= NI_NUMERICHOST;
    201 			break;
    202 		}
    203 	    }
    204 		break;
    205 #endif
    206 	}
    207 	if (host == NULL || hostlen == 0) {
    208 		/*
    209 		 * do nothing in this case.
    210 		 * in case you are wondering if "&&" is more correct than
    211 		 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
    212 		 * means that the caller does not want the result.
    213 		 */
    214 	} else if (flags & NI_NUMERICHOST) {
    215 		int numaddrlen;
    216 
    217 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    218 		if (flags & NI_NAMEREQD)
    219 			return ENI_NOHOSTNAME;
    220 		if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    221 		    == NULL)
    222 			return ENI_SYSTEM;
    223 		numaddrlen = strlen(numaddr);
    224 		if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    225 			return ENI_MEMORY;
    226 		strcpy(host, numaddr);
    227 #if defined(INET6) && defined(NI_WITHSCOPEID)
    228 		if (afd->a_af == AF_INET6 &&
    229 		    (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
    230 		     IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
    231 		    ((struct sockaddr_in6 *)sa)->sin6_scope_id) {
    232 #ifndef ALWAYS_WITHSCOPE
    233 			if (flags & NI_WITHSCOPEID)
    234 #endif /* !ALWAYS_WITHSCOPE */
    235 			{
    236 				char scopebuf[MAXHOSTNAMELEN], *s;
    237 				int scopelen;
    238 
    239 				if ((s = ip6_sa2str((struct sockaddr_in6 *)sa,
    240 						    scopebuf, 0)) == NULL)
    241 					/* XXX what should we do? */
    242 					strcpy(scopebuf, "0");
    243 
    244 				scopelen = strlen(scopebuf);
    245 				if (scopelen + 1 + numaddrlen + 1 > hostlen)
    246 					return ENI_MEMORY;
    247 
    248 #if 0
    249 				/*
    250 				 * construct <scopeid><delim><numeric-addr>
    251 				 */
    252 				/*
    253 				 * Shift the host string to allocate
    254 				 * space for the scope ID part.
    255 				 */
    256 				memmove(host + scopelen + 1, host,
    257 					numaddrlen);
    258 				/* copy the scope ID and the delimiter */
    259 				memcpy(host, scopebuf, scopelen);
    260 				host[scopelen] = SCOPE_DELIMITER;
    261 				host[scopelen + 1 + numaddrlen] = '\0';
    262 #else
    263 				/*
    264 				 * construct <numeric-addr><delim><scopeid>
    265 				 */
    266 				memcpy(host + numaddrlen + 1, scopebuf,
    267 				    scopelen);
    268 				host[numaddrlen] = SCOPE_DELIMITER;
    269 				host[numaddrlen + 1 + scopelen] = '\0';
    270 #endif
    271 			}
    272 		}
    273 #endif /* INET6 */
    274 	} else {
    275 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    276 		h_error = h_errno;
    277 
    278 		if (hp) {
    279 			if (flags & NI_NOFQDN) {
    280 				p = strchr(hp->h_name, '.');
    281 				if (p) *p = '\0';
    282 			}
    283 			if (strlen(hp->h_name) > hostlen) {
    284 				return ENI_MEMORY;
    285 			}
    286 			strcpy(host, hp->h_name);
    287 		} else {
    288 			if (flags & NI_NAMEREQD)
    289 				return ENI_NOHOSTNAME;
    290 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    291 			    == NULL)
    292 				return ENI_NOHOSTNAME;
    293 			if (strlen(numaddr) > hostlen)
    294 				return ENI_MEMORY;
    295 			strcpy(host, numaddr);
    296 		}
    297 	}
    298 	return SUCCESS;
    299 }
    300 
    301 #ifdef INET6
    302 /* ARGSUSED */
    303 static char *
    304 ip6_sa2str(sa6, buf, flags)
    305 	struct sockaddr_in6 *sa6;
    306 	char *buf;
    307 	int flags;
    308 {
    309 	unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
    310 	struct in6_addr *a6 = &sa6->sin6_addr;
    311 
    312 #ifdef notyet
    313 	if (flags & NI_NUMERICSCOPE) {
    314 		sprintf(buf, "%d", sa6->sin6_scope_id);
    315 		return(buf);
    316 	}
    317 #endif
    318 
    319 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6))
    320 		return(if_indextoname(ifindex, buf));
    321 
    322 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
    323 		return(NULL);	/* XXX */
    324 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
    325 		return(NULL);	/* XXX */
    326 
    327 	return(NULL);		/* XXX */
    328 }
    329 #endif
    330