getaddrinfo.c revision d736db6dc53e615e3f2d66d1ddbe28473694d107
/*-
* Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
* The Berkeley Software Design Inc. software License Agreement specifies
* the terms and conditions for redistribution.
*
* BSDI $Id: getaddrinfo.c,v 1.14 2000/03/10 23:11:27 explorer Exp $
*/
#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
{
char *proto;
int, int);
return (EAI_NONAME);
return (EAI_BADFLAGS);
return (EAI_SYSTEM);
}
switch (family) {
case AF_UNSPEC:
switch (hints->ai_socktype) {
}
break;
case AF_INET:
case AF_INET6:
switch (hints->ai_socktype) {
case 0: 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
*/
if (hostname &&
if (servname &&
#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.
*/
if (servname) {
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[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx00")];
/* Convert to a V4 mapped address */
goto inet6_addr;
}
goto common;
return (EAI_NONAME);
return (EAI_MEMORY);
if (flags & AI_CANONNAME) {
sizeof(nbuf));
}
goto done;
} else if ((flags & AI_NUMERICHOST) != 0){
return (EAI_NONAME);
}
}
for (i = 0; i < FOUND_MAX; i++) {
break;
return(err);
}
return (EAI_NODATA);
done:
return (0);
}
static char *
char *s;
const char *d;
return (NULL);
*s++ = '\0';
*stringp = s;
return (string);
}
return (string);
}
static void
int family;
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;
}
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 *
while (oai) {
/* grab one off the old list */
/* put it on the front of the new list */
}
return (nai);
}