/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <netdb.h>
#include <resolv.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <unistd.h>
#include "addrinfo.h"
#define SUCCESS 0
#define ANY 0
#define NO 0
static const char in_addrany[] = { 0, 0, 0, 0 };
static const char in6_addrany[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static const char in6_loopback[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};
struct sockinet {
};
static struct afd {
int a_af;
int a_addrlen;
int a_socklen;
int a_off;
const char *a_addrany;
const char *a_loopback;
} afdl [] = {
#ifdef INET6
#define N_INET6 0
sizeof(struct sockaddr_in6),
#else
#define N_INET 0
#endif
sizeof(struct sockaddr_in),
};
#ifdef INET6
#else
#endif
int);
struct addrinfo *, int);
struct addrinfo *, int);
static int str_isnumber(const char *);
static char *ai_errlist[] = {
"Success",
"Address family for hostname not supported", /* EAI_ADDRFAMILY */
"Temporary failure in name resolution", /* EAI_AGAIN */
"Invalid value for ai_flags", /* EAI_BADFLAGS */
"Non-recoverable failure in name resolution", /* EAI_FAIL */
"ai_family not supported", /* EAI_FAMILY */
"Memory allocation failure", /* EAI_MEMORY */
"No address associated with hostname", /* EAI_NODATA */
"hostname nor servname provided, or not known",/* EAI_NONAME */
"servname not supported for ai_socktype", /* EAI_SERVICE */
"ai_socktype not supported", /* EAI_SOCKTYPE */
"System error returned in errno", /* EAI_SYSTEM */
"Invalid value for hints", /* EAI_BADHINTS */
"Resolved protocol is unknown", /* EAI_PROTOCOL */
"Unknown error", /* EAI_MAX */
};
} else {\
error = EAI_MEMORY;\
goto free;\
}\
}
#ifdef HAVE_SA_LEN
#else
#endif
char *p;\
== NULL) {\
error = EAI_MEMORY;\
goto free;\
}\
}
char *
int ecode;
{
return ai_errlist[ecode];
}
void
{
do {
if (ai->ai_canonname)
/* no need to free(ai->ai_addr) */
}
static int
str_isnumber(p)
const char *p;
{
char *q = (char *)p;
while (*q) {
if (! isdigit(*q))
return NO;
q++;
}
return YES;
}
int
{
int i, error = 0;
/* initialize file static vars */
pai->ai_addrlen = 0;
return EAI_NONAME;
if (hints) {
/* error check for hints */
case PF_UNSPEC:
case PF_INET:
#ifdef INET6
case PF_INET6:
#endif
break;
default:
}
switch (pai->ai_socktype) {
case ANY:
switch (pai->ai_protocol) {
case ANY:
break;
case IPPROTO_UDP:
break;
case IPPROTO_TCP:
break;
default:
break;
}
break;
case SOCK_RAW:
break;
case SOCK_DGRAM:
break;
case SOCK_STREAM:
break;
default:
break;
}
}
/*
* service port
*/
if (servname) {
if (str_isnumber(servname)) {
/* caller accept *ANY* socktype */
}
} else {
char *proto;
switch (pai->ai_socktype) {
case ANY:
break;
case SOCK_DGRAM:
proto = "udp";
break;
case SOCK_STREAM:
proto = "tcp";
break;
default:
break;
}
} else
}
}
}
/*
* hostname == NULL.
* passive socket -> anyaddr (0.0.0.0 or ::)
* non-passive socket -> localhost (127.0.0.1 or ::1)
*/
int s;
continue;
}
/*
* filter out AFs that are not supported by the kernel
* XXX errno?
*/
if (s < 0)
continue;
close(s);
/* xxx meaningless?
* GET_CANONNAME(cur->ai_next, "anyaddr");
*/
} else {
port);
/* xxx meaningless?
* GET_CANONNAME(cur->ai_next, "localhost");
*/
}
}
if (top)
goto good;
else
}
/* hostname as numeric name */
case AF_INET:
break;
#ifdef INET6
case AF_INET6:
break;
#endif
}
goto good;
}
/*
* if AI_CANONNAME and if reverse lookup
* fail, return ai anyway to pacify
* calling application.
*
* XXX getaddrinfo() is a name->address
* translation function, and it looks strange
* that we do addr->name translation here.
*/
goto good;
} else
}
}
/* hostname as alphabetical name */
if (error == 0) {
if (top) {
good:
return SUCCESS;
} else
}
free:
if (top)
bad:
return error;
}
static int
const char *addr;
char *numaddr;
int port0;
{
int error = 0;
} else
return SUCCESS;
free:
if (cur)
/* bad: */
return error;
}
static int
const char *hostname;
int af;
int port0;
{
int retry;
int s;
ekeep = 0;
error = 0;
retry = 0;
/*
* filter out AFs that are not supported by the kernel
* XXX errno?
*/
if (s < 0)
continue;
close(s);
} else {
continue;
}
/* It is WRONG, we need getipnodebyname(). */
switch (error) {
case EAI_AGAIN:
if (++retry < 3)
goto again;
/* FALL THROUGH*/
default:
if (ekeep == 0)
break;
}
if (*res) {
/* make chain of addrs */
;
if (!cur)
return EAI_FAIL;
}
}
/* if we got something, it's okay */
if (*res0)
return 0;
}
static int
const char *hostname;
int af;
int port0;
{
char *ap;
#ifdef HAVE_GETHOSTBYNAME2
goto bad;
}
#else
goto bad;
}
#endif
switch (h_error) {
case HOST_NOT_FOUND:
case NO_DATA:
error = EAI_NODATA;
break;
case TRY_AGAIN:
break;
case NO_RECOVERY:
case NETDB_INTERNAL:
default:
break;
}
goto bad;
}
switch (af) {
#ifdef INET6
case AF_INET6:
break;
#endif
#ifndef INET6
default: /* AF_UNSPEC */
#endif
case AF_INET:
break;
#ifdef INET6
default: /* AF_UNSPEC */
sizeof(struct in_addr);
} else
break;
#endif
}
}
}
return SUCCESS;
free:
if (top)
bad:
return error;
}