234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉/*
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * All rights reserved.
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 *
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * Redistribution and use in source and binary forms, with or without
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * modification, are permitted provided that the following conditions
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * are met:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * 1. Redistributions of source code must retain the above copyright
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * notice, this list of conditions and the following disclaimer.
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * 2. Redistributions in binary form must reproduce the above copyright
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * notice, this list of conditions and the following disclaimer in the
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * documentation and/or other materials provided with the distribution.
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * 3. Neither the name of the project nor the names of its contributors
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * may be used to endorse or promote products derived from this software
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * without specific prior written permission.
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 *
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * SUCH DAMAGE.
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 */
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉/*
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * Issues to be discussed:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * - Thread safe-ness must be checked
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * - Return values. There seems to be no standard for return value (RFC2553)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 */
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <sys/types.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <sys/socket.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <netinet/in.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <arpa/inet.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <arpa/nameser.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <netdb.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <resolv.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <string.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include <stddef.h>
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#include "addrinfo.h"
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define SUCCESS 0
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ANY 0
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define YES 1
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define NO 0
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉static struct afd {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int a_af;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int a_addrlen;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int a_socklen;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int a_off;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉} afdl [] = {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#ifdef INET6
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 offsetof(struct sockaddr_in6, sin6_addr)},
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#endif
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 offsetof(struct sockaddr_in, sin_addr)},
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 {0, 0, 0},
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉};
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉struct sockinet {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 u_char si_len;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 u_char si_family;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 u_short si_port;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉};
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_NOSOCKET 0
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_NOSERVNAME 1
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_NOHOSTNAME 2
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_MEMORY 3
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_SYSTEM 4
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_FAMILY 5
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#define ENI_SALEN 6
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉int
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 const struct sockaddr *sa;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 size_t salen;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 char *host;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 size_t hostlen;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 char *serv;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 size_t servlen;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int flags;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉{
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 struct afd *afd;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 struct servent *sp;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 struct hostent *hp;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 u_short port;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int family, len, i;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 char *addr, *p;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 u_long v4a;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 int h_error;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 char numserv[512];
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 char numaddr[512];
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (sa == NULL)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_NOSOCKET;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#ifdef HAVE_SA_LEN
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 len = sa->sa_len;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (len != salen) return ENI_SALEN;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#else
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 len = salen;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#endif
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 family = sa->sa_family;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 for (i = 0; afdl[i].a_af; i++)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (afdl[i].a_af == family) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 afd = &afdl[i];
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 goto found;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_FAMILY;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 found:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (len != afd->a_socklen) return ENI_SALEN;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 port = ((struct sockinet *)sa)->si_port; /* network byte order */
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 addr = (char *)sa + afd->a_off;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (serv == NULL || servlen == 0) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 /* what we should do? */
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 } else if (flags & NI_NUMERICSERV) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (strlen(numserv) > servlen)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_MEMORY;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 strcpy(serv, numserv);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 } else {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (sp) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (strlen(sp->s_name) > servlen)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_MEMORY;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 strcpy(serv, sp->s_name);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 } else
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_NOSERVNAME;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 switch (sa->sa_family) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 case AF_INET:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 v4a = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 flags |= NI_NUMERICHOST;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 v4a >>= IN_CLASSA_NSHIFT;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (v4a == 0 || v4a == IN_LOOPBACKNET)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 flags |= NI_NUMERICHOST;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 break;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#ifdef INET6
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 case AF_INET6:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 struct sockaddr_in6 *sin6;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 sin6 = (struct sockaddr_in6 *)sa;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 switch (sin6->sin6_addr.s6_addr[0]) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 case 0x00:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 ;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 ;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 else
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 flags |= NI_NUMERICHOST;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 break;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 default:
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 flags |= NI_NUMERICHOST;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 flags |= NI_NUMERICHOST;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 break;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 break;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#endif
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (host == NULL || hostlen == 0) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 /* what should we do? */
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 } else if (flags & NI_NUMERICHOST) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 /* NUMERICHOST and NAMEREQD conflicts with each other */
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (flags & NI_NAMEREQD)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_NOHOSTNAME;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 == NULL)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_SYSTEM;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (strlen(numaddr) > hostlen)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_MEMORY;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 strcpy(host, numaddr);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 } else {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#ifdef USE_GETIPNODEBY
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#else
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 h_error = h_errno;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#endif
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (hp) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (flags & NI_NOFQDN) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 p = strchr(hp->h_name, '.');
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (p) *p = '\0';
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (strlen(hp->h_name) > hostlen) {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#ifdef USE_GETIPNODEBY
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 freehostent(hp);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#endif
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_MEMORY;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 strcpy(host, hp->h_name);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#ifdef USE_GETIPNODEBY
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 freehostent(hp);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉#endif
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 } else {
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (flags & NI_NAMEREQD)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_NOHOSTNAME;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 == NULL)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_NOHOSTNAME;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 if (strlen(numaddr) > hostlen)
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return ENI_MEMORY;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 strcpy(host, numaddr);
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 }
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉 return SUCCESS;
234ef071da767c70518b887c5a913687b05b19e9Tatuya JINMEI 神明達哉}