Blame lib/lwres/getnameinfo.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
 *
Packit Service ae04f2
 * See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
 * information regarding copyright ownership.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
/*! \file */
Packit Service ae04f2
Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
Packit Service ae04f2
 * All rights reserved.
Packit Service ae04f2
 *
Packit Service ae04f2
 * Redistribution and use in source and binary forms, with or without
Packit Service ae04f2
 * modification, are permitted provided that the following conditions
Packit Service ae04f2
 * are met:
Packit Service ae04f2
 * 1. Redistributions of source code must retain the above copyright
Packit Service ae04f2
 *    notice, this list of conditions and the following disclaimer.
Packit Service ae04f2
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service ae04f2
 *    notice, this list of conditions and the following disclaimer in the
Packit Service ae04f2
 *    documentation and/or other materials provided with the distribution.
Packit Service ae04f2
 * 3. Neither the name of the project nor the names of its contributors
Packit Service ae04f2
 *    may be used to endorse or promote products derived from this software
Packit Service ae04f2
 *    without specific prior written permission.
Packit Service ae04f2
 *
Packit Service ae04f2
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
Packit Service ae04f2
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service ae04f2
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service ae04f2
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
Packit Service ae04f2
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service ae04f2
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service ae04f2
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service ae04f2
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service ae04f2
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service ae04f2
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service ae04f2
 * SUCH DAMAGE.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
/*
Packit Service ae04f2
 * XXX
Packit Service ae04f2
 * Issues to be discussed:
Packit Service ae04f2
 * - Return values.  There seems to be no standard for return value (RFC2553)
Packit Service ae04f2
 *   but INRIA implementation returns EAI_xxx defined for getaddrinfo().
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
/**
Packit Service ae04f2
 *    This function is equivalent to the getnameinfo(3) function defined in
Packit Service ae04f2
 *    RFC2133. lwres_getnameinfo() returns the hostname for the struct
Packit Service ae04f2
 *    sockaddr sa which is salen bytes long. The hostname is of length
Packit Service ae04f2
 *    hostlen and is returned via *host. The maximum length of the hostname
Packit Service ae04f2
 *    is 1025 bytes: #NI_MAXHOST.
Packit Service ae04f2
 *
Packit Service ae04f2
 *    The name of the service associated with the port number in sa is
Packit Service ae04f2
 *    returned in *serv. It is servlen bytes long. The maximum length of the
Packit Service ae04f2
 *    service name is #NI_MAXSERV - 32 bytes.
Packit Service ae04f2
 *
Packit Service ae04f2
 *    The flags argument sets the following bits:
Packit Service ae04f2
 *
Packit Service ae04f2
 * \li   #NI_NOFQDN:
Packit Service ae04f2
 *           A fully qualified domain name is not required for local hosts.
Packit Service ae04f2
 *           The local part of the fully qualified domain name is returned
Packit Service ae04f2
 *           instead.
Packit Service ae04f2
 *
Packit Service ae04f2
 * \li   #NI_NUMERICHOST
Packit Service ae04f2
 *           Return the address in numeric form, as if calling inet_ntop(),
Packit Service ae04f2
 *           instead of a host name.
Packit Service ae04f2
 *
Packit Service ae04f2
 * \li   #NI_NAMEREQD
Packit Service ae04f2
 *           A name is required. If the hostname cannot be found in the DNS
Packit Service ae04f2
 *           and this flag is set, a non-zero error code is returned. If the
Packit Service ae04f2
 *           hostname is not found and the flag is not set, the address is
Packit Service ae04f2
 *           returned in numeric form.
Packit Service ae04f2
 *
Packit Service ae04f2
 * \li   #NI_NUMERICSERV
Packit Service ae04f2
 *           The service name is returned as a digit string representing the
Packit Service ae04f2
 *           port number.
Packit Service ae04f2
 *
Packit Service ae04f2
 * \li   #NI_DGRAM
Packit Service ae04f2
 *           Specifies that the service being looked up is a datagram
Packit Service ae04f2
 *           service, and causes getservbyport() to be called with a second
Packit Service ae04f2
 *           argument of "udp" instead of its default of "tcp". This is
Packit Service ae04f2
 *           required for the few ports (512-514) that have different
Packit Service ae04f2
 *           services for UDP and TCP.
Packit Service ae04f2
 *
Packit Service ae04f2
 * \section getnameinfo_return Return Values
Packit Service ae04f2
 *
Packit Service ae04f2
 *    lwres_getnameinfo() returns 0 on success or a non-zero error code if
Packit Service ae04f2
 *    an error occurs.
Packit Service ae04f2
 *
Packit Service ae04f2
 * \section getname_see See Also
Packit Service ae04f2
 *
Packit Service ae04f2
 *    RFC2133, getservbyport(),
Packit Service ae04f2
 *    lwres_getnamebyaddr(). lwres_net_ntop().
Packit Service ae04f2
 *
Packit Service ae04f2
 * \section getnameinfo_bugs Bugs
Packit Service ae04f2
 *
Packit Service ae04f2
 *    RFC2133 fails to define what the nonzero return values of
Packit Service ae04f2
 *    getnameinfo() are.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
#include <config.h>
Packit Service ae04f2
Packit Service ae04f2
#include <inttypes.h>
Packit Service ae04f2
#include <stdio.h>
Packit Service ae04f2
#include <string.h>
Packit Service ae04f2
Packit Service ae04f2
#include <lwres/lwres.h>
Packit Service ae04f2
#include <lwres/net.h>
Packit Service ae04f2
#include <lwres/netdb.h>
Packit Service ae04f2
#include "print_p.h"
Packit Service ae04f2
Packit Service ae04f2
#include "assert_p.h"
Packit Service ae04f2
#include "unreachable_p.h"
Packit Service ae04f2
Packit Service ae04f2
#define SUCCESS 0
Packit Service ae04f2
Packit Service ae04f2
/*% afd structure definition */
Packit Service ae04f2
static struct afd {
Packit Service ae04f2
	int a_af;
Packit Service ae04f2
	size_t a_addrlen;
Packit Service ae04f2
	size_t a_socklen;
Packit Service ae04f2
} afdl [] = {
Packit Service ae04f2
	/*!
Packit Service ae04f2
	 * First entry is linked last...
Packit Service ae04f2
	 */
Packit Service ae04f2
	{ AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
Packit Service ae04f2
	{ AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
Packit Service ae04f2
	{0, 0, 0},
Packit Service ae04f2
};
Packit Service ae04f2
Packit Service ae04f2
#define ENI_NOSERVNAME	1
Packit Service ae04f2
#define ENI_NOHOSTNAME	2
Packit Service ae04f2
#define ENI_MEMORY	3
Packit Service ae04f2
#define ENI_SYSTEM	4
Packit Service ae04f2
#define ENI_FAMILY	5
Packit Service ae04f2
#define ENI_SALEN	6
Packit Service ae04f2
#define ENI_NOSOCKET 	7
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * The test against 0 is there to keep the Solaris compiler
Packit Service ae04f2
 * from complaining about "end-of-loop code not reached".
Packit Service ae04f2
 */
Packit Service ae04f2
#define ERR(code) \
Packit Service ae04f2
	do { result = (code);			\
Packit Service ae04f2
		if (result != 0) goto cleanup;	\
Packit Service ae04f2
	} while (0)
Packit Service ae04f2
Packit Service ae04f2
/*% lightweight resolver socket address structure to hostname and service name */
Packit Service ae04f2
int
Packit Service ae04f2
lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
Packit Service ae04f2
		  size_t hostlen, char *serv, size_t servlen, int flags)
Packit Service ae04f2
{
Packit Service ae04f2
	struct afd *afd = NULL;
Packit Service ae04f2
	struct servent *sp;
Packit Service ae04f2
	unsigned short port;
Packit Service ae04f2
#ifdef LWRES_PLATFORM_HAVESALEN
Packit Service ae04f2
	size_t len;
Packit Service ae04f2
#endif
Packit Service ae04f2
	int family, i;
Packit Service ae04f2
	const void *addr;
Packit Service ae04f2
#if 0
Packit Service ae04f2
	unsigned long v4a;
Packit Service ae04f2
	unsigned char pfx;
Packit Service ae04f2
#endif
Packit Service ae04f2
	char numserv[sizeof("65000")];
Packit Service ae04f2
	char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
Packit Service ae04f2
		    + 1 + sizeof("4294967295")];
Packit Service ae04f2
	const char *proto;
Packit Service ae04f2
	uint32_t lwf = 0;
Packit Service ae04f2
	lwres_context_t *lwrctx = NULL;
Packit Service ae04f2
	lwres_gnbaresponse_t *by = NULL;
Packit Service ae04f2
	int result = SUCCESS;
Packit Service ae04f2
	int n;
Packit Service ae04f2
Packit Service ae04f2
	if (sa == NULL)
Packit Service ae04f2
		ERR(ENI_NOSOCKET);
Packit Service ae04f2
Packit Service ae04f2
#ifdef LWRES_PLATFORM_HAVESALEN
Packit Service ae04f2
	len = sa->sa_len;
Packit Service ae04f2
	if (len != salen)
Packit Service ae04f2
		ERR(ENI_SALEN);
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	family = sa->sa_family;
Packit Service ae04f2
	for (i = 0; afdl[i].a_af; i++)
Packit Service ae04f2
		if (afdl[i].a_af == family) {
Packit Service ae04f2
			afd = &afdl[i];
Packit Service ae04f2
			goto found;
Packit Service ae04f2
		}
Packit Service ae04f2
	ERR(ENI_FAMILY);
Packit Service ae04f2
Packit Service ae04f2
 found:
Packit Service ae04f2
	if (salen != afd->a_socklen)
Packit Service ae04f2
		ERR(ENI_SALEN);
Packit Service ae04f2
Packit Service ae04f2
	switch (family) {
Packit Service ae04f2
	case AF_INET:
Packit Service ae04f2
		port = ((const struct sockaddr_in *)sa)->sin_port;
Packit Service ae04f2
		addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
Packit Service ae04f2
		break;
Packit Service ae04f2
Packit Service ae04f2
	case AF_INET6:
Packit Service ae04f2
		port = ((const struct sockaddr_in6 *)sa)->sin6_port;
Packit Service ae04f2
		addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
Packit Service ae04f2
		break;
Packit Service ae04f2
Packit Service ae04f2
	default:
Packit Service ae04f2
		port = 0;
Packit Service ae04f2
		addr = NULL;
Packit Service ae04f2
		POST(port); POST(addr);
Packit Service ae04f2
		INSIST(0);
Packit Service ae04f2
	}
Packit Service ae04f2
	proto = (flags & NI_DGRAM) ? "udp" : "tcp";
Packit Service ae04f2
Packit Service ae04f2
	if (serv == NULL || servlen == 0U) {
Packit Service ae04f2
		/*
Packit Service ae04f2
		 * Caller does not want service.
Packit Service ae04f2
		 */
Packit Service ae04f2
	} else if ((flags & NI_NUMERICSERV) != 0 ||
Packit Service ae04f2
		   (sp = getservbyport(port, proto)) == NULL) {
Packit Service ae04f2
		snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
Packit Service ae04f2
		if ((strlen(numserv) + 1) > servlen)
Packit Service ae04f2
			ERR(ENI_MEMORY);
Packit Service ae04f2
		strcpy(serv, numserv);
Packit Service ae04f2
	} else {
Packit Service ae04f2
		if ((strlen(sp->s_name) + 1) > servlen)
Packit Service ae04f2
			ERR(ENI_MEMORY);
Packit Service ae04f2
		strcpy(serv, sp->s_name);
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
#if 0
Packit Service ae04f2
	switch (sa->sa_family) {
Packit Service ae04f2
	case AF_INET:
Packit Service ae04f2
		v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Packit Service ae04f2
		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
Packit Service ae04f2
			flags |= NI_NUMERICHOST;
Packit Service ae04f2
		v4a >>= IN_CLASSA_NSHIFT;
Packit Service ae04f2
		if (v4a == 0 || v4a == IN_LOOPBACKNET)
Packit Service ae04f2
			flags |= NI_NUMERICHOST;
Packit Service ae04f2
		break;
Packit Service ae04f2
Packit Service ae04f2
	case AF_INET6:
Packit Service ae04f2
		pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
Packit Service ae04f2
		if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
Packit Service ae04f2
			flags |= NI_NUMERICHOST;
Packit Service ae04f2
		break;
Packit Service ae04f2
	}
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	if (host == NULL || hostlen == 0U) {
Packit Service ae04f2
		/*
Packit Service ae04f2
		 * What should we do?
Packit Service ae04f2
		 */
Packit Service ae04f2
	} else if (flags & NI_NUMERICHOST) {
Packit Service ae04f2
		if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
Packit Service ae04f2
		    == NULL)
Packit Service ae04f2
			ERR(ENI_SYSTEM);
Packit Service ae04f2
#if defined(LWRES_HAVE_SIN6_SCOPE_ID)
Packit Service ae04f2
		if (afd->a_af == AF_INET6 &&
Packit Service ae04f2
		    ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
Packit Service ae04f2
			char *p = numaddr + strlen(numaddr);
Packit Service ae04f2
			const char *stringscope = NULL;
Packit Service ae04f2
#if 0
Packit Service ae04f2
			if ((flags & NI_NUMERICSCOPE) == 0) {
Packit Service ae04f2
				/*
Packit Service ae04f2
				 * Vendors may want to add support for
Packit Service ae04f2
				 * non-numeric scope identifier.
Packit Service ae04f2
				 */
Packit Service ae04f2
				stringscope = foo;
Packit Service ae04f2
			}
Packit Service ae04f2
#endif
Packit Service ae04f2
			if (stringscope == NULL) {
Packit Service ae04f2
				snprintf(p, sizeof(numaddr) - (p - numaddr),
Packit Service ae04f2
				    "%%%u",
Packit Service ae04f2
				    ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
Packit Service ae04f2
			} else {
Packit Service ae04f2
				snprintf(p, sizeof(numaddr) - (p - numaddr),
Packit Service ae04f2
				    "%%%s", stringscope);
Packit Service ae04f2
			}
Packit Service ae04f2
		}
Packit Service ae04f2
#endif
Packit Service ae04f2
		if (strlen(numaddr) + 1 > hostlen)
Packit Service ae04f2
			ERR(ENI_MEMORY);
Packit Service ae04f2
		strcpy(host, numaddr);
Packit Service ae04f2
	} else {
Packit Service ae04f2
		switch (family) {
Packit Service ae04f2
		case AF_INET:
Packit Service ae04f2
			lwf = LWRES_ADDRTYPE_V4;
Packit Service ae04f2
			break;
Packit Service ae04f2
		case AF_INET6:
Packit Service ae04f2
			lwf = LWRES_ADDRTYPE_V6;
Packit Service ae04f2
			break;
Packit Service ae04f2
		default:
Packit Service ae04f2
			INSIST(0);
Packit Service ae04f2
			ISC_UNREACHABLE();
Packit Service ae04f2
		}
Packit Service ae04f2
Packit Service ae04f2
		n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
Packit Service ae04f2
		if (n == 0) {
Packit Service ae04f2
			(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
Packit Service ae04f2
Packit Service ae04f2
			n = lwres_getnamebyaddr(lwrctx, lwf,
Packit Service ae04f2
						(uint16_t)afd->a_addrlen,
Packit Service ae04f2
						addr, &by;;
Packit Service ae04f2
		}
Packit Service ae04f2
		if (n == 0) {
Packit Service ae04f2
			if (flags & NI_NOFQDN) {
Packit Service ae04f2
				char *p = strchr(by->realname, '.');
Packit Service ae04f2
				if (p)
Packit Service ae04f2
					*p = '\0';
Packit Service ae04f2
			}
Packit Service ae04f2
			if ((strlen(by->realname) + 1) > hostlen)
Packit Service ae04f2
				ERR(ENI_MEMORY);
Packit Service ae04f2
			strcpy(host, by->realname);
Packit Service ae04f2
		} else {
Packit Service ae04f2
			if (flags & NI_NAMEREQD)
Packit Service ae04f2
				ERR(ENI_NOHOSTNAME);
Packit Service ae04f2
			if (lwres_net_ntop(afd->a_af, addr, numaddr,
Packit Service ae04f2
					   sizeof(numaddr))
Packit Service ae04f2
			    == NULL)
Packit Service ae04f2
				ERR(ENI_NOHOSTNAME);
Packit Service ae04f2
			if ((strlen(numaddr) + 1) > hostlen)
Packit Service ae04f2
				ERR(ENI_MEMORY);
Packit Service ae04f2
			strcpy(host, numaddr);
Packit Service ae04f2
		}
Packit Service ae04f2
	}
Packit Service ae04f2
	result = SUCCESS;
Packit Service ae04f2
 cleanup:
Packit Service ae04f2
	if (by != NULL)
Packit Service ae04f2
		lwres_gnbaresponse_free(lwrctx, &by;;
Packit Service ae04f2
	if (lwrctx != NULL) {
Packit Service ae04f2
		lwres_conf_clear(lwrctx);
Packit Service ae04f2
		lwres_context_destroy(&lwrctx);
Packit Service ae04f2
	}
Packit Service ae04f2
	return (result);
Packit Service ae04f2
}