Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.2
      1 /*	$NetBSD: getnameinfo.c,v 1.2 1999/07/04 00:43:43 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 (RFC2133)
     36  *   but INRIA implementation returns EAI_xxx defined for getaddrinfo().
     37  */
     38 
     39 #include <sys/types.h>
     40 #include <sys/socket.h>
     41 #include <netinet/in.h>
     42 #include <arpa/inet.h>
     43 #include <arpa/nameser.h>
     44 #include <netdb.h>
     45 #include <resolv.h>
     46 #include <string.h>
     47 #include <stddef.h>
     48 
     49 #define SUCCESS 0
     50 #define ANY 0
     51 #define YES 1
     52 #define NO  0
     53 
     54 static struct afd {
     55 	int a_af;
     56 	int a_addrlen;
     57 	int a_socklen;
     58 	int a_off;
     59 } afdl [] = {
     60 #ifdef INET6
     61 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     62 		offsetof(struct sockaddr_in6, sin6_addr)},
     63 #endif
     64 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     65 		offsetof(struct sockaddr_in, sin_addr)},
     66 	{0, 0, 0},
     67 };
     68 
     69 struct sockinet {
     70 	u_char	si_len;
     71 	u_char	si_family;
     72 	u_short	si_port;
     73 };
     74 
     75 #define ENI_NOSOCKET 	0
     76 #define ENI_NOSERVNAME	1
     77 #define ENI_NOHOSTNAME	2
     78 #define ENI_MEMORY	3
     79 #define ENI_SYSTEM	4
     80 #define ENI_FAMILY	5
     81 #define ENI_SALEN	6
     82 
     83 int
     84 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
     85 	const struct sockaddr *sa;
     86 	size_t salen;
     87 	char *host;
     88 	size_t hostlen;
     89 	char *serv;
     90 	size_t servlen;
     91 	int flags;
     92 {
     93 	struct afd *afd;
     94 	struct servent *sp;
     95 	struct hostent *hp;
     96 	u_short port;
     97 	int family, len, i;
     98 	char *addr, *p;
     99 	u_long v4a;
    100 	u_char pfx;
    101 	int h_error;
    102 	char numserv[512];
    103 	char numaddr[512];
    104 
    105 	if (sa == NULL)
    106 		return ENI_NOSOCKET;
    107 
    108 	len = sa->sa_len;
    109 	if (len != salen) return ENI_SALEN;
    110 
    111 	family = sa->sa_family;
    112 	for (i = 0; afdl[i].a_af; i++)
    113 		if (afdl[i].a_af == family) {
    114 			afd = &afdl[i];
    115 			goto found;
    116 		}
    117 	return ENI_FAMILY;
    118 
    119  found:
    120 	if (len != afd->a_socklen) return ENI_SALEN;
    121 
    122 	port = ((struct sockinet *)sa)->si_port; /* network byte order */
    123 	addr = (char *)sa + afd->a_off;
    124 
    125 	if (serv == NULL || servlen == 0) {
    126 		/* what we should do? */
    127 	} else if (flags & NI_NUMERICSERV) {
    128 		snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    129 		if (strlen(numserv) > servlen)
    130 			return ENI_MEMORY;
    131 		strcpy(serv, numserv);
    132 	} else {
    133 		sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
    134 		if (sp) {
    135 			if (strlen(sp->s_name) > servlen)
    136 				return ENI_MEMORY;
    137 			strcpy(serv, sp->s_name);
    138 		} else
    139 			return ENI_NOSERVNAME;
    140 	}
    141 
    142 	switch (sa->sa_family) {
    143 	case AF_INET:
    144 		v4a = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
    145 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    146 			flags |= NI_NUMERICHOST;
    147 		v4a >>= IN_CLASSA_NSHIFT;
    148 		if (v4a == 0 || v4a == IN_LOOPBACKNET)
    149 			flags |= NI_NUMERICHOST;
    150 		break;
    151 #ifdef INET6
    152 	case AF_INET6:
    153 		pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0];
    154 		if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
    155 			flags |= NI_NUMERICHOST;
    156 		break;
    157 #endif
    158 	}
    159 	if (host == NULL || hostlen == 0) {
    160 		/* what should we do? */
    161 	} else if (flags & NI_NUMERICHOST) {
    162 		if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    163 		    == NULL)
    164 			return ENI_SYSTEM;
    165 		if (strlen(numaddr) > hostlen)
    166 			return ENI_MEMORY;
    167 		strcpy(host, numaddr);
    168 	} else {
    169 #ifdef USE_GETIPNODEBY
    170 		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
    171 #else
    172 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    173 		h_error = h_errno;
    174 #endif
    175 
    176 		if (hp) {
    177 			if (flags & NI_NOFQDN) {
    178 				p = strchr(hp->h_name, '.');
    179 				if (p) *p = '\0';
    180 			}
    181 			if (strlen(hp->h_name) > hostlen) {
    182 #ifdef USE_GETIPNODEBY
    183 				freehostent(hp);
    184 #endif
    185 				return ENI_MEMORY;
    186 			}
    187 			strcpy(host, hp->h_name);
    188 #ifdef USE_GETIPNODEBY
    189 			freehostent(hp);
    190 #endif
    191 		} else {
    192 			if (flags & NI_NAMEREQD)
    193 				return ENI_NOHOSTNAME;
    194 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    195 			    == NULL)
    196 				return ENI_NOHOSTNAME;
    197 			if (strlen(numaddr) > hostlen)
    198 				return ENI_MEMORY;
    199 			strcpy(host, numaddr);
    200 		}
    201 	}
    202 	return SUCCESS;
    203 }
    204