Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.11
      1 /*	$NetBSD: getnameinfo.c,v 1.11 2000/02/16 04:50:23 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/types.h>
     43 #include <sys/socket.h>
     44 #include <net/if.h>
     45 #include <netinet/in.h>
     46 #include <arpa/inet.h>
     47 #include <arpa/nameser.h>
     48 #include <netdb.h>
     49 #include <resolv.h>
     50 #include <string.h>
     51 #include <stddef.h>
     52 
     53 #define SUCCESS 0
     54 #define ANY 0
     55 #define YES 1
     56 #define NO  0
     57 
     58 static struct afd {
     59 	int a_af;
     60 	int a_addrlen;
     61 	int a_socklen;
     62 	int a_off;
     63 } afdl [] = {
     64 #ifdef INET6
     65 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     66 		offsetof(struct sockaddr_in6, sin6_addr)},
     67 #endif
     68 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     69 		offsetof(struct sockaddr_in, sin_addr)},
     70 	{0, 0, 0},
     71 };
     72 
     73 struct sockinet {
     74 	u_char	si_len;
     75 	u_char	si_family;
     76 	u_short	si_port;
     77 };
     78 
     79 #ifdef INET6
     80 static char *ip6_sa2str __P((struct sockaddr_in6 *, char *, int));
     81 #endif
     82 
     83 #define ENI_NOSOCKET 	0
     84 #define ENI_NOSERVNAME	1
     85 #define ENI_NOHOSTNAME	2
     86 #define ENI_MEMORY	3
     87 #define ENI_SYSTEM	4
     88 #define ENI_FAMILY	5
     89 #define ENI_SALEN	6
     90 
     91 int
     92 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
     93 	const struct sockaddr *sa;
     94 	size_t salen;
     95 	char *host;
     96 	size_t hostlen;
     97 	char *serv;
     98 	size_t servlen;
     99 	int flags;
    100 {
    101 	struct afd *afd;
    102 	struct servent *sp;
    103 	struct hostent *hp;
    104 	u_short port;
    105 	int family, i;
    106 	char *addr, *p;
    107 	u_int32_t v4a;
    108 	int h_error;
    109 	char numserv[512];
    110 	char numaddr[512];
    111 
    112 	if (sa == NULL)
    113 		return ENI_NOSOCKET;
    114 
    115 	if (sa->sa_len != salen)
    116 		return ENI_SALEN;
    117 
    118 	family = sa->sa_family;
    119 	for (i = 0; afdl[i].a_af; i++)
    120 		if (afdl[i].a_af == family) {
    121 			afd = &afdl[i];
    122 			goto found;
    123 		}
    124 	return ENI_FAMILY;
    125 
    126  found:
    127 	if (salen != afd->a_socklen)
    128 		return ENI_SALEN;
    129 
    130 	port = ((struct sockinet *)sa)->si_port; /* network byte order */
    131 	addr = (char *)sa + afd->a_off;
    132 
    133 	if (serv == NULL || servlen == 0) {
    134 		/*
    135 		 * do nothing in this case.
    136 		 * in case you are wondering if "&&" is more correct than
    137 		 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
    138 		 * means that the caller does not want the result.
    139 		 */
    140 	} else {
    141 		if (flags & NI_NUMERICSERV)
    142 			sp = NULL;
    143 		else {
    144 			sp = getservbyport(port,
    145 				(flags & NI_DGRAM) ? "udp" : "tcp");
    146 		}
    147 		if (sp) {
    148 			if (strlen(sp->s_name) > servlen)
    149 				return ENI_MEMORY;
    150 			strcpy(serv, sp->s_name);
    151 		} else {
    152 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    153 			if (strlen(numserv) > servlen)
    154 				return ENI_MEMORY;
    155 			strcpy(serv, numserv);
    156 		}
    157 	}
    158 
    159 	switch (sa->sa_family) {
    160 	case AF_INET:
    161 		v4a = (u_int32_t)
    162 			ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
    163 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    164 			flags |= NI_NUMERICHOST;
    165 		v4a >>= IN_CLASSA_NSHIFT;
    166 		if (v4a == 0)
    167 			flags |= NI_NUMERICHOST;
    168 		break;
    169 #ifdef INET6
    170 	case AF_INET6:
    171 	    {
    172 		struct sockaddr_in6 *sin6;
    173 		sin6 = (struct sockaddr_in6 *)sa;
    174 		switch (sin6->sin6_addr.s6_addr[0]) {
    175 		case 0x00:
    176 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    177 				;
    178 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    179 				;
    180 			else
    181 				flags |= NI_NUMERICHOST;
    182 			break;
    183 		default:
    184 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    185 				flags |= NI_NUMERICHOST;
    186 			}
    187 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    188 				flags |= NI_NUMERICHOST;
    189 			break;
    190 		}
    191 	    }
    192 		break;
    193 #endif
    194 	}
    195 	if (host == NULL || hostlen == 0) {
    196 		/*
    197 		 * do nothing in this case.
    198 		 * in case you are wondering if "&&" is more correct than
    199 		 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
    200 		 * means that the caller does not want the result.
    201 		 */
    202 	} else if (flags & NI_NUMERICHOST) {
    203 		int numaddrlen;
    204 
    205 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    206 		if (flags & NI_NAMEREQD)
    207 			return ENI_NOHOSTNAME;
    208 		if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    209 		    == NULL)
    210 			return ENI_SYSTEM;
    211 		numaddrlen = strlen(numaddr);
    212 		if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    213 			return ENI_MEMORY;
    214 		strcpy(host, numaddr);
    215 #if defined(INET6) && defined(NI_WITHSCOPEID)
    216 		if (afd->a_af == AF_INET6 &&
    217 		    (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
    218 		     IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
    219 		    ((struct sockaddr_in6 *)sa)->sin6_scope_id) {
    220 #ifndef ALWAYS_WITHSCOPE
    221 			if (flags & NI_WITHSCOPEID)
    222 #endif /* !ALWAYS_WITHSCOPE */
    223 			{
    224 				char scopebuf[MAXHOSTNAMELEN], *s;
    225 				int scopelen;
    226 
    227 				if ((s = ip6_sa2str((struct sockaddr_in6 *)sa,
    228 						    scopebuf, 0)) == NULL)
    229 					/* XXX what should we do? */
    230 					strcpy(scopebuf, "0");
    231 
    232 				scopelen = strlen(scopebuf);
    233 				if (scopelen + 1 + numaddrlen + 1 > hostlen)
    234 					return ENI_MEMORY;
    235 
    236 #if 1
    237 				/*
    238 				 * construct <scopeid><delim><numeric-addr>
    239 				 */
    240 				/*
    241 				 * Shift the host string to allocate
    242 				 * space for the scope ID part.
    243 				 */
    244 				memmove(host + scopelen + 1, host,
    245 					numaddrlen);
    246 				/* copy the scope ID and the delimiter */
    247 				memcpy(host, scopebuf, scopelen);
    248 				host[scopelen] = SCOPE_DELIMITER;
    249 				host[scopelen + 1 + numaddrlen] = '\0';
    250 #else
    251 				/*
    252 				 * construct <numeric-addr><delim><scopeid>
    253 				 */
    254 				memcpy(host + numaddrlen + 1, scopebuf,
    255 				    scopelen);
    256 				host[numaddrlen] = SCOPE_DELIMITER;
    257 				host[numaddrlen + 1 + scopelen] = '\0';
    258 #endif
    259 			}
    260 		}
    261 #endif /* INET6 */
    262 	} else {
    263 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    264 		h_error = h_errno;
    265 
    266 		if (hp) {
    267 			if (flags & NI_NOFQDN) {
    268 				p = strchr(hp->h_name, '.');
    269 				if (p) *p = '\0';
    270 			}
    271 			if (strlen(hp->h_name) > hostlen) {
    272 				return ENI_MEMORY;
    273 			}
    274 			strcpy(host, hp->h_name);
    275 		} else {
    276 			if (flags & NI_NAMEREQD)
    277 				return ENI_NOHOSTNAME;
    278 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    279 			    == NULL)
    280 				return ENI_NOHOSTNAME;
    281 			if (strlen(numaddr) > hostlen)
    282 				return ENI_MEMORY;
    283 			strcpy(host, numaddr);
    284 		}
    285 	}
    286 	return SUCCESS;
    287 }
    288 
    289 #ifdef INET6
    290 /* ARGSUSED */
    291 static char *
    292 ip6_sa2str(sa6, buf, flags)
    293 	struct sockaddr_in6 *sa6;
    294 	char *buf;
    295 	int flags;
    296 {
    297 	unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
    298 	struct in6_addr *a6 = &sa6->sin6_addr;
    299 
    300 #ifdef notyet
    301 	if (flags & NI_NUMERICSCOPE) {
    302 		sprintf(buf, "%d", sa6->sin6_scope_id);
    303 		return(buf);
    304 	}
    305 #endif
    306 
    307 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6))
    308 		return(if_indextoname(ifindex, buf));
    309 
    310 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
    311 		return(NULL);	/* XXX */
    312 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
    313 		return(NULL);	/* XXX */
    314 
    315 	return(NULL);		/* XXX */
    316 }
    317 #endif
    318