getaddrinfo.c revision 40f53fa8d9c6a4fc38c0014495e7a42b08f52481
/*
* Copyright (C) 1999, 2000 Internet Software Consortium.
*
* This code is derived from software contributed to Internet Software
* Consortium by Berkeley Software Design, Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
* BERKELEY SOFTWARE DESIGN, INC DISCLAIM ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR BERKELEY
* SOFTWARE DESIGN, INC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getaddrinfo.c,v 1.30 2000/08/01 01:32:13 tale Exp $ */
#include <config.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <resolv.h>
static struct addrinfo
#ifdef AF_LOCAL
#endif
int, int));
#define FOUND_IPV4 0x1
#define FOUND_IPV6 0x2
#define FOUND_MAX 2
int
{
const char *proto;
int, int);
return (EAI_NONAME);
return (EAI_BADFLAGS);
return (EAI_SYSTEM);
}
switch (family) {
case AF_UNSPEC:
switch (hints->ai_socktype) {
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
}
break;
case AF_INET:
case AF_INET6:
switch (hints->ai_socktype) {
case 0:
break;
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
case SOCK_RAW:
break;
default:
return (EAI_SOCKTYPE);
}
break;
#ifdef AF_LOCAL
case AF_LOCAL:
switch (hints->ai_socktype) {
case 0:
break;
case SOCK_STREAM:
break;
case SOCK_DGRAM:
break;
default:
return (EAI_SOCKTYPE);
}
break;
#endif
default:
return (EAI_FAMILY);
}
} else {
protocol = 0;
family = 0;
socktype = 0;
flags = 0;
}
#ifdef AF_LOCAL
/*
* First, deal with AF_LOCAL. If the family was not set,
* then assume AF_LOCAL if the first character of the
*/
#endif
/*
* Ok, only AF_INET and AF_INET6 left.
*/
/*
* First, look up the service name (port) if it was
* requested. If the socket type wasn't specified, then
* try and figure it out.
*/
char *e;
if (*e == '\0') {
if (socktype == 0)
return (EAI_SOCKTYPE);
return (EAI_SERVICE);
} else {
return (EAI_SERVICE);
if (socktype == 0) {
}
}
} else
port = 0;
/*
* Next, deal with just a service name, and no hostname.
* (we verified that one of them was non-null up above).
*/
return (EAI_MEMORY);
}
return (EAI_MEMORY);
}
}
return (0);
}
/*
* If the family isn't specified or AI_NUMERICHOST specified,
* check first to see if it is a numeric address.
* Though the gethostbyname2() routine
* will recognize numeric addresses, it will only recognize
* the format that it is being called for. Thus, a numeric
* AF_INET address will be treated by the AF_INET6 call as
* a domain name, and vice versa. Checking for both numerics
* here avoids that.
*/
char nbuf[NI_MAXHOST];
char ntmp[NI_MAXHOST];
#if defined(LWRES_HAVE_SIN6_SCOPE_ID)
char *p, *ep;
#endif
#if defined(LWRES_HAVE_SIN6_SCOPE_ID)
/* scope identifier portion */
ntmp[0] = '\0';
/*
* vendors may want to support non-numeric
* scopeid around here.
*/
if (p != NULL)
&ep, 10);
*p = '\0';
else {
ntmp[0] = '\0';
scopeid = 0;
}
} else
scopeid = 0;
#endif
!= 0)
{
/*
* Convert to a V4 mapped address.
*/
goto inet6_addr;
}
goto common;
} else if (ntmp[0] != 0 &&
{
return (EAI_NONAME);
goto common;
return (EAI_NONAME);
return (EAI_MEMORY);
if (flags & AI_CANONNAME) {
#if defined(LWRES_HAVE_SIN6_SCOPE_ID)
#endif
NULL, 0,
NI_NUMERICHOST) == 0) {
return (EAI_MEMORY);
} else {
/* XXX raise error? */
}
}
goto done;
} else if ((flags & AI_NUMERICHOST) != 0) {
return (EAI_NONAME);
}
}
for (i = 0; i < FOUND_MAX; i++) {
break;
if (err != 0)
return (err);
}
return (EAI_NODATA);
done:
return (0);
}
static char *
char *s;
const char *d;
return (NULL);
for (s = string; *s != '\0'; s++) {
sc = *s;
*s++ = '\0';
*stringp = s;
return (string);
}
}
return (string);
}
static void
int, int))
{
int found;
if (family) {
switch (family) {
case AF_INET:
break;
case AF_INET6:
break;
}
} else {
found = 0;
/*
* We ignore any unknown names.
*/
if ((found & FOUND_IPV6) == 0)
found |= FOUND_IPV6;
if ((found & FOUND_IPV4) == 0)
found |= FOUND_IPV4;
}
}
/*
* Add in anything that we didn't find.
*/
if ((found & FOUND_IPV4) == 0)
if ((found & FOUND_IPV6) == 0)
}
return;
}
/*
* The test against 0 is there to keep the Solaris compiler
* from complaining about "end-of-loop code not reached".
*/
} while (0)
static int
{
int result = 0;
if (lwres != 0)
}
LWRES_ADDRTYPE_V4, &by) == 0) {
}
if (flags & AI_CANONNAME) {
}
}
}
}
return(result);
}
static int
{
int result = 0;
if (lwres != 0)
}
LWRES_ADDRTYPE_V6, &by) == 0) {
}
if (flags & AI_CANONNAME) {
}
}
}
}
return (result);
}
void
if (ai->ai_canonname)
}
}
#ifdef AF_LOCAL
static int
struct sockaddr_un *sun;
if (socktype == 0)
return (EAI_SOCKTYPE);
return (EAI_MEMORY);
/*
* ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
* and ai->ai_next were initialized to zero.
*/
return (0);
}
#endif
/*
* Allocate an addrinfo structure, and a sockaddr structure
* of the specificed length. We initialize:
* ai_addrlen
* ai_family
* ai_addr
* ai_addr->sa_family
* ai_addr->sa_len (HAVE_SA_LEN)
* and everything else is initialized to zero.
*/
static struct addrinfo *
return (NULL);
return (NULL);
}
#ifdef HAVE_SA_LEN
#endif
return (ai);
}
static struct addrinfo *
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
return (NULL);
}
return (ai);
return (ai);
}
static struct addrinfo *
/*
* Grab one off the old list.
*/
/*
* Put it on the front of the new list.
*/
}
return (nai);
}