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