Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.1
      1 /*
      2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. Neither the name of the project nor the names of its contributors
     14  *    may be used to endorse or promote products derived from this software
     15  *    without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Issues to be discussed:
     32  * - Thread safe-ness must be checked
     33  * - Return values.  There seems to be no standard for return value (RFC2133)
     34  *   but INRIA implementation returns EAI_xxx defined for getaddrinfo().
     35  */
     36 
     37 #include <sys/types.h>
     38 #include <sys/socket.h>
     39 #include <netinet/in.h>
     40 #include <arpa/inet.h>
     41 #include <arpa/nameser.h>
     42 #include <netdb.h>
     43 #include <resolv.h>
     44 #include <string.h>
     45 #include <stddef.h>
     46 
     47 #define SUCCESS 0
     48 #define ANY 0
     49 #define YES 1
     50 #define NO  0
     51 
     52 static struct afd {
     53 	int a_af;
     54 	int a_addrlen;
     55 	int a_socklen;
     56 	int a_off;
     57 } afdl [] = {
     58 #ifdef INET6
     59 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     60 		offsetof(struct sockaddr_in6, sin6_addr)},
     61 #endif
     62 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     63 		offsetof(struct sockaddr_in, sin_addr)},
     64 	{0, 0, 0},
     65 };
     66 
     67 struct sockinet {
     68 	u_char	si_len;
     69 	u_char	si_family;
     70 	u_short	si_port;
     71 };
     72 
     73 #define ENI_NOSOCKET 	0
     74 #define ENI_NOSERVNAME	1
     75 #define ENI_NOHOSTNAME	2
     76 #define ENI_MEMORY	3
     77 #define ENI_SYSTEM	4
     78 #define ENI_FAMILY	5
     79 #define ENI_SALEN	6
     80 
     81 int
     82 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
     83 	const struct sockaddr *sa;
     84 	size_t salen;
     85 	char *host;
     86 	size_t hostlen;
     87 	char *serv;
     88 	size_t servlen;
     89 	int flags;
     90 {
     91 	struct afd *afd;
     92 	struct servent *sp;
     93 	struct hostent *hp;
     94 	u_short port;
     95 	int family, len, i;
     96 	char *addr, *p;
     97 	u_long v4a;
     98 	u_char pfx;
     99 	int h_error;
    100 	char numserv[512];
    101 	char numaddr[512];
    102 
    103 	if (sa == NULL)
    104 		return ENI_NOSOCKET;
    105 
    106 	len = sa->sa_len;
    107 	if (len != salen) return ENI_SALEN;
    108 
    109 	family = sa->sa_family;
    110 	for (i = 0; afdl[i].a_af; i++)
    111 		if (afdl[i].a_af == family) {
    112 			afd = &afdl[i];
    113 			goto found;
    114 		}
    115 	return ENI_FAMILY;
    116 
    117  found:
    118 	if (len != afd->a_socklen) return ENI_SALEN;
    119 
    120 	port = ((struct sockinet *)sa)->si_port; /* network byte order */
    121 	addr = (char *)sa + afd->a_off;
    122 
    123 	if (serv == NULL || servlen == 0) {
    124 		/* what we should do? */
    125 	} else if (flags & NI_NUMERICSERV) {
    126 		snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    127 		if (strlen(numserv) > servlen)
    128 			return ENI_MEMORY;
    129 		strcpy(serv, numserv);
    130 	} else {
    131 		sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
    132 		if (sp) {
    133 			if (strlen(sp->s_name) > servlen)
    134 				return ENI_MEMORY;
    135 			strcpy(serv, sp->s_name);
    136 		} else
    137 			return ENI_NOSERVNAME;
    138 	}
    139 
    140 	switch (sa->sa_family) {
    141 	case AF_INET:
    142 		v4a = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
    143 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    144 			flags |= NI_NUMERICHOST;
    145 		v4a >>= IN_CLASSA_NSHIFT;
    146 		if (v4a == 0 || v4a == IN_LOOPBACKNET)
    147 			flags |= NI_NUMERICHOST;
    148 		break;
    149 #ifdef INET6
    150 	case AF_INET6:
    151 		pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0];
    152 		if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
    153 			flags |= NI_NUMERICHOST;
    154 		break;
    155 #endif
    156 	}
    157 	if (host == NULL || hostlen == 0) {
    158 		/* what should we do? */
    159 	} else if (flags & NI_NUMERICHOST) {
    160 		if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    161 		    == NULL)
    162 			return ENI_SYSTEM;
    163 		if (strlen(numaddr) > hostlen)
    164 			return ENI_MEMORY;
    165 		strcpy(host, numaddr);
    166 	} else {
    167 #ifdef USE_GETIPNODEBY
    168 		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
    169 #else
    170 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    171 		h_error = h_errno;
    172 #endif
    173 
    174 		if (hp) {
    175 			if (flags & NI_NOFQDN) {
    176 				p = strchr(hp->h_name, '.');
    177 				if (p) *p = '\0';
    178 			}
    179 			if (strlen(hp->h_name) > hostlen) {
    180 #ifdef USE_GETIPNODEBY
    181 				freehostent(hp);
    182 #endif
    183 				return ENI_MEMORY;
    184 			}
    185 			strcpy(host, hp->h_name);
    186 #ifdef USE_GETIPNODEBY
    187 			freehostent(hp);
    188 #endif
    189 		} else {
    190 			if (flags & NI_NAMEREQD)
    191 				return ENI_NOHOSTNAME;
    192 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    193 			    == NULL)
    194 				return ENI_NOHOSTNAME;
    195 			if (strlen(numaddr) > hostlen)
    196 				return ENI_MEMORY;
    197 			strcpy(host, numaddr);
    198 		}
    199 	}
    200 	return SUCCESS;
    201 }
    202