#ifndef lint
#endif
/*
* Copyright (c) 2001 Japan Network Information Center. All rights reserved.
*
* By using this file, you agree to the terms and conditions set forth bellow.
*
* LICENSE TERMS AND CONDITIONS
*
* The following License Terms and Conditions apply, unless a different
* license is obtained from Japan Network Information Center ("JPNIC"),
* a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
* Chiyoda-ku, Tokyo 101-0047, Japan.
*
* 1. Use, Modification and Redistribution (including distribution of any
* under this License Terms and Conditions.
*
* 2. Redistribution of source code must retain the copyright notices as they
* appear in each source code file, this License Terms and Conditions.
*
* 3. Redistribution in binary form must reproduce the Copyright Notice,
* materials provided with the distribution. For the purposes of binary
* distribution the "Copyright Notice" refers to the following language:
* "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
*
* 4. The name of JPNIC may not be used to endorse or promote products
* derived from this Software without specific prior written approval of
* JPNIC.
*
* 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
* "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 JPNIC 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 DAMAGES.
*/
#include <config.h>
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <idn/logmacro.h>
#ifdef FOR_RUNIDN
/*
* This file is specially compiled for runidn.
* runidn replaces existing resolver functions dynamically with ones
* with IDN processing (encoding conversion and normalization).
* So entry names must be same as the system's one.
*/
#include "stub.h"
#else
/*
* For normal use. All the entry names are prefixed with "idn_resolver_".
* <idn/resolver.h> has bunch of #defines to substitute the standard
* name resolver functions with ones provided here.
*/
#include "resolver.h"
#endif
typedef union {
char *dummy_for_alignment;
} hostbuf_t;
typedef struct obj_lock {
void *key;
} obj_lock_t;
/*
* This variable is to prevent IDN processing occuring more than once for
* a single name resolution. This will happen if some resolver function
* is implemented using another function (e.g. gethostbyname() implemented
* using gethostbyname2()).
* No, using the static variable is not a correct thing to do for a multi-
* threading environment, but I don't think of a better solution..
*/
static int idn_isprocessing = 0;
static int obj_islocked(void *key);
static void obj_unlock(void *key);
int *errp);
static char *decode_name_dynamic(const char *name);
int *errp);
#ifdef HAVE_GETADDRINFO
#endif
#ifdef HAVE_FREEADDRINFO
#endif
/*
* Object locking facility.
*/
static int
/*
* Hash function for obj_*.
* 'key' is supposed to be an address.
*/
unsigned long v = (unsigned long)key;
return ((v >> 3) % OBJLOCKHASH_SIZE);
}
static int
{
/*
* Check if the object specified by 'key' is locked.
* Return 1 if so, 0 otherwise.
*/
return (1);
}
return (0);
}
static void
{
/*
* Lock an object specified by 'key'.
*/
obj_lock_hash[h] = olp;
}
}
static void
{
/*
* Unlock an object specified by 'key'.
*/
olp = obj_lock_hash[h];
else
return;
}
}
}
static struct hostent *
{
/*
* Copy "struct hostent" data referenced by 'hp' to 'newhp'.
* It's a deep-copy, meaning all the data referenced by 'hp' are
* also copied. They are copied into 'buf', whose length is 'buflen'.
* The domain names ('hp->h_name' and 'hp->h_aliases') are
* decoded from ACE to the local encoding before they are copied.
* If 'buf' is too small to hold all the data, NULL will be
* returned and '*errp' is set to NO_RECOVERY.
*/
int naliases = 0;
int naddrs = 0;
return (NULL);
/*
* Allocate aliase table in 'buf'.
*/
naliases++;
goto overflow;
}
/*
* Allocate address table in 'buf'.
*/
int i;
naddrs++;
goto overflow;
/*
* Copy the addresses.
*/
goto overflow;
for (i = 0; i < naddrs; i++) {
}
}
/*
* Decode the name in h_name.
*/
idn_result_t r;
idn_enable(1);
idn_nameinit(1);
switch (r) {
case idn_success:
break;
default:
/* Copy hp->h_name verbatim. */
break;
}
/* falllthrough */
case idn_buffer_overflow:
goto overflow;
}
}
/*
* Decode the names in h_aliases.
*/
int i;
for (i = 0; i < naliases; i++) {
idn_result_t r;
idn_enable(1);
idn_nameinit(1);
switch (r) {
case idn_success:
newaliases[i] = buf;
break;
default:
/* Copy hp->h_name verbatim. */
newaliases[i] = buf;
break;
}
/* falllthrough */
case idn_buffer_overflow:
goto overflow;
}
}
}
return (newhp);
*errp = NO_RECOVERY;
return (NULL);
}
static char *
idn_result_t r;
char *s;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success) {
}
if (s == NULL)
return (NULL);
else
}
static struct hostent *
/*
* Make a deep-copy of the data referenced by 'hp', and return
* a pointer to the copied data.
* All the data are dynamically allocated using malloc().
* The domain names ('hp->h_name' and 'hp->h_aliases') are
* decoded from ACE to the local encoding before they are copied.
* If malloc() fails, NULL will be returned and '*errp' is set to
* NO_RECOVERY.
*/
char **pp;
int naliases = 0;
int naddrs = 0;
int i;
return (NULL);
naliases++;
}
naddrs++;
}
alloc_size = sizeof(struct hostent) +
sizeof(char *) * (naliases + 1) +
sizeof(char *) * (naddrs + 1) +
return (hp);
}
goto alloc_fail;
}
for (i = 0; i < naliases; i++) {
goto alloc_fail;
}
}
char *p;
p = (char *)pp;
for (i = 0; i < naddrs; i++) {
newhp->h_addr_list[i] = p;
}
}
return (newhp);
*errp = NO_RECOVERY;
return (NULL);
}
static void
/*
* Free all the memory allocated by copy_decode_hostent_dynamic().
*/
}
}
#ifdef HAVE_GETNAMEINFO
static struct addrinfo *
return (NULL);
return (NULL);
return (newaip);
}
#endif
#ifdef HAVE_FREEADDRINFO
static void
}
}
#endif
#ifdef HAVE_GETHOSTBYNAME
struct hostent *
idn_result_t r;
if (idn_isprocessing)
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
&h_errno);
idn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETHOSTBYNAME2
struct hostent *
idn_result_t r;
if (idn_isprocessing)
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
&h_errno);
idn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETHOSTBYADDR
struct hostent *
if (idn_isprocessing)
TRACE(("gethostbyaddr()\n"));
idn_isprocessing = 1;
&h_errno);
idn_isprocessing = 0;
return (hp);
}
#endif
#ifdef GETHOST_R_GLIBC_FLAVOR
#ifdef HAVE_GETHOSTBYNAME_R
int
{
char *data;
idn_result_t r;
int n;
if (idn_isprocessing)
TRACE(("gethostbyname_r(name=%s,buflen=%d)\n",
} else {
*errp = NO_RECOVERY;
return (ENOMEM);
}
}
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
*errp = 0;
errp);
idn_isprocessing = 0;
if (*errp != 0)
n = EINVAL; /* XXX */
return (n);
}
#endif
#ifdef HAVE_GETHOSTBYNAME2_R
int
{
char *data;
idn_result_t r;
int n;
if (idn_isprocessing)
TRACE(("gethostbyname2_r(name=%s,buflen=%d)\n",
} else {
*errp = NO_RECOVERY;
return (ENOMEM);
}
}
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
errp);
idn_isprocessing = 0;
if (*errp != 0)
n = EINVAL; /* XXX */
return (n);
}
#endif
#ifdef HAVE_GETHOSTBYADDR_R
int
{
char *data;
int n;
if (idn_isprocessing) {
}
} else {
*errp = NO_RECOVERY;
return (ENOMEM);
}
}
idn_isprocessing = 1;
errp);
idn_isprocessing = 0;
if (*errp != 0)
n = EINVAL; /* XXX */
return (0);
}
#endif
#else /* GETHOST_R_GLIBC_FLAVOR */
#ifdef HAVE_GETHOSTBYNAME_R
struct hostent *
{
char *data;
idn_result_t r;
if (idn_isprocessing)
TRACE(("gethostbyname_r(name=%s,buflen=%d)\n",
} else {
*errp = NO_RECOVERY;
return (NULL);
}
}
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
errp);
idn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETHOSTBYADDR_R
struct hostent *
{
char *data;
if (idn_isprocessing) {
}
} else {
*errp = NO_RECOVERY;
return (NULL);
}
}
idn_isprocessing = 1;
errp);
idn_isprocessing = 0;
return (hp);
}
#endif
#endif /* GETHOST_R_GLIBC_FLAVOR */
#ifdef HAVE_GETIPNODEBYNAME
struct hostent *
idn_result_t r;
if (idn_isprocessing)
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
}
}
idn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_GETIPNODEBYADDR
struct hostent *
if (idn_isprocessing)
TRACE(("getipnodebyaddr()\n"));
idn_isprocessing = 1;
}
}
idn_isprocessing = 0;
return (hp);
}
#endif
#ifdef HAVE_FREEHOSTENT
void
if (obj_islocked(hp)) {
/*
* We allocated the data.
*/
obj_unlock(hp);
} else {
/*
* It was allocated the original getipnodeby*().
*/
}
}
#endif
#ifdef HAVE_GETADDRINFO
int
{
idn_result_t r;
int err;
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
if (r == idn_success)
else
}
idn_isprocessing = 0;
return (err);
}
#endif
#ifdef HAVE_FREEADDRINFO
void
if (obj_islocked(aip)) {
/*
* We allocated the data.
*/
} else {
/*
* It was allocated the original getaddrinfo().
*/
}
}
#endif
#ifdef HAVE_GETNAMEINFO
int
{
int code;
idn_result_t r;
}
idn_isprocessing = 1;
idn_enable(1);
idn_nameinit(1);
switch (r) {
case idn_success:
code = 0;
break;
case idn_buffer_overflow:
case idn_nomemory:
code = EAI_MEMORY;
break;
default:
break;
}
}
idn_isprocessing = 0;
return (code);
}
#endif