mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-01-27 09:37:24 +08:00
3c77dcff9e
* libc/include/sys/reent.h: Add _h_errno field. * libc/sys/linux/libc-symbols.h: Add default stubs for new libc macros now used in net directory. * libc/sys/linux/bits/libc-lock.h: Always define _IO_MTSAFE_IO. * libc/sys/linux/include/hesiod.h: Add needed macros for new code in net directory. * libc/sys/linux/include/netdb.h: Ditto. * libc/sys/linux/include/resolv.h: Ditto. * libc/sys/linux/include/stdint.h: Ditto. * libc/sys/linux/include/arpa/nameser.h: Ditto. * libc/sys/linux/include/net/ethernet.h: Ditto. * libc/sys/linux/include/net/if_ppp.h: Ditto. * libc/sys/linux/include/netinet/if_ether.h: Ditto. * libc/sys/linux/machine/i386/get_clockfreq.c: Remove static memmem. * libc/sys/linux/machine/i386/include/endian.h: Protect macros with flags to avoid duplicate definition. * libc/sys/linux/machine/i386/include/param.h: Ditto. * libc/sys/linux/net/Makefile.am: Modified to account for removed and added files. * libc/sys/linux/net/Makefile.in: Regenerated. * libc/sys/linux/net/base64.c: Replaced with glibc version of code. * libc/sys/linux/net/getaddrinfo.c: Ditto. * libc/sys/linux/net/getnameinfo.c: Ditto. * libc/sys/linux/net/getproto.c: Ditto. * libc/sys/linux/net/getservent.c: Ditto. * libc/sys/linux/net/inet_netof.c: Ditto. * libc/sys/linux/net/inet_lnaof.c: Ditto. * libc/sys/linux/net/ns_name.c: Ditto. * libc/sys/linux/net/ns_netint.c: Ditto. * libc/sys/linux/net/inet_ntoa.c: Ditto. * libc/sys/linux/net/ns_parse.c: Ditto. * libc/sys/linux/net/ns_print.c: Ditto. * libc/sys/linux/net/ns_ttl.c: Ditto. * libc/sys/linux/net/nsap_addr.c: Ditto. * libc/sys/linux/net/rcmd.c: Ditto. * libc/sys/linux/net/res_comp.c: Ditto. * libc/sys/linux/net/res_data.c: Ditto. * libc/sys/linux/net/res_debug.c: Ditto. * libc/sys/linux/net/res_init.c: Ditto. * libc/sys/linux/net/res_mkquery.c: Ditto. * libc/sys/linux/net/res_query.c: Ditto. * libc/sys/linux/net/res_send.c: Ditto. * libc/sys/linux/net/send.c: Ditto. * libc/sys/linux/stdlib/collate.c: Ditto. * libc/sys/linux/sys/ioctl.h: Ditto. * libc/sys/linux/sys/socket.h: Ditto. * libc/sys/linux/sys/unistd.h: Ditto. * libc/sys/linux/iconv/iconvconfig.c: Removed either because no longer used or because licensing includes advertising clause that cannot be ignored. * libc/sys/linux/iconv/strtab.c: Ditto. * libc/sys/linux/include/libc_private.h: Ditto. * libc/sys/linux/include/nsswitch.h: Ditto. * libc/sys/linux/include/net/if_atm.h: Ditto. * libc/sys/linux/include/net/if_media.h: Ditto. * libc/sys/linux/include/net/if_pppvar.h: Ditto. * libc/sys/linux/include/netinet/if_atm.h: Ditto. * libc/sys/linux/include/netinet/ip_flow.h: Ditto. * libc/sys/linux/intl/locale.alias: Ditto. * libc/sys/linux/net/addr2ascii.3: Ditto. * libc/sys/linux/net/bindresvport.c: Ditto. * libc/sys/linux/net/byteorder.3: Ditto. * libc/sys/linux/net/res_config.h: Ditto. * libc/sys/linux/net/ether_addr.c: Ditto. * libc/sys/linux/net/ethers.3: Ditto. * libc/sys/linux/net/getaddrinfo.3: Ditto. * libc/sys/linux/net/gethostbydns.c: Ditto. * libc/sys/linux/net/gethostbyht.c: Ditto. * libc/sys/linux/net/gethostbyname.3: Ditto. * libc/sys/linux/net/gethostbynis.c: Ditto. * libc/sys/linux/net/gethostnamadr.c: Ditto. * libc/sys/linux/net/getifaddrs.3: Ditto. * libc/sys/linux/net/getifaddrs.c: Ditto. * libc/sys/linux/net/getipnodebyname.3: Ditto. * libc/sys/linux/net/getnameinfo.3: Ditto. * libc/sys/linux/net/getnetbydns.c: Ditto. * libc/sys/linux/net/getnetbyht.c: Ditto. * libc/sys/linux/net/getnetbynis.c: Ditto. * libc/sys/linux/net/getnetent.3: Ditto. * libc/sys/linux/net/getnetnamadr.c: Ditto. * libc/sys/linux/net/getprotoent.3: Ditto. * libc/sys/linux/net/getprotoent.c: Ditto. * libc/sys/linux/net/getprotoname.c: Ditto. * libc/sys/linux/net/getservbyname.c: Ditto. * libc/sys/linux/net/getservbyport.c: Ditto. * libc/sys/linux/net/getservent.3: Ditto. * libc/sys/linux/net/herror.c: Ditto. * libc/sys/linux/net/hesiod.3: Ditto. * libc/sys/linux/net/hesiod.c: Ditto. * libc/sys/linux/net/if_indextoname.3: Ditto. * libc/sys/linux/net/inet.3: Ditto. * libc/sys/linux/net/inet6_option_space.3: Ditto. * libc/sys/linux/net/inet6_rthdr_space.3: Ditto. * libc/sys/linux/net/inet_makeaddr.c: Ditto. * libc/sys/linux/net/inet_net.3: Ditto. * libc/sys/linux/net/inet_network.c: Ditto. * libc/sys/linux/net/innetgr-stub.c: Ditto. * libc/sys/linux/net/ip6opt.c: Ditto. * libc/sys/linux/net/iso_addr.3: Ditto. * libc/sys/linux/net/iso_addr.c: Ditto. * libc/sys/linux/net/linkaddr.3: Ditto. * libc/sys/linux/net/linkaddr.c: Ditto. * libc/sys/linux/net/map_v4v6.c: Ditto. * libc/sys/linux/net/name6.c: Ditto. * libc/sys/linux/net/namespace.h: Ditto. * libc/sys/linux/net/ns.3: Ditto. * libc/sys/linux/net/ns_addr.c: Ditto. * libc/sys/linux/net/ns_ntoa.c: Ditto. * libc/sys/linux/net/nsdispatch.3: Ditto. * libc/sys/linux/net/nsdispatch.c: Ditto. * libc/sys/linux/net/nslexer.c: Ditto. * libc/sys/linux/net/nslexer.l: Ditto. * libc/sys/linux/net/nsparser.c: Ditto. * libc/sys/linux/net/nsparser.h: Ditto. * libc/sys/linux/net/nsparser.y: Ditto. * libc/sys/linux/net/rcmd.3: Ditto. * libc/sys/linux/net/rcmdsh.3: Ditto. * libc/sys/linux/net/res_mkupdate.c: Ditto. * libc/sys/linux/net/res_update.c: Ditto. * libc/sys/linux/net/resolver.3: Ditto. * libc/sys/linux/net/rthdr.c: Ditto. * libc/sys/linux/net/vars.c: Ditto. * libc/sys/linux/shlib-compat.h: New file. * libc/sys/linux/machine/i386/atomic.h: Ditto. * libc/sys/linux/net/XXX-lookup.c: Ditto. * libc/sys/linux/net/alias-lookup.c: Ditto. * libc/sys/linux/net/res_libc.c: Ditto. * libc/sys/linux/net/aliases.h: Ditto. * libc/sys/linux/net/check_pf.c: Ditto. * libc/sys/linux/net/databases.def: Ditto. * libc/sys/linux/net/digits_dots.c: Ditto. * libc/sys/linux/net/ether_aton.c: Ditto. * libc/sys/linux/net/ether_aton_r.c: Ditto. * libc/sys/linux/net/ether_hton.c: Ditto. * libc/sys/linux/net/ether_line.c: Ditto. * libc/sys/linux/net/ether_ntoa.c: Ditto. * libc/sys/linux/net/ether_ntoa_r.c: Ditto. * libc/sys/linux/net/ether_ntoh.c: Ditto. * libc/sys/linux/net/etherent.h: Ditto. * libc/sys/linux/net/ethers-lookup.c: Ditto. * libc/sys/linux/net/function.def: Ditto. * libc/sys/linux/net/getXXbyYY.c: Ditto. * libc/sys/linux/net/getXXbyYY_r.c: Ditto. * libc/sys/linux/net/getXXent.c: Ditto. * libc/sys/linux/net/getXXent_r.c: Ditto. * libc/sys/linux/net/getaliasent.c: Ditto. * libc/sys/linux/net/getaliasent_r.c: Ditto. * libc/sys/linux/net/getaliasname.c: Ditto. * libc/sys/linux/net/getaliasname_r.c: Ditto. * libc/sys/linux/net/gethstbyad.c: Ditto. * libc/sys/linux/net/gethstbyad_r.c: Ditto. * libc/sys/linux/net/gethstbynm.c: Ditto. * libc/sys/linux/net/gethstbynm2.c: Ditto. * libc/sys/linux/net/gethstbynm2_r.c: Ditto. * libc/sys/linux/net/gethstbynm_r.c: Ditto. * libc/sys/linux/net/gethstent.c: Ditto. * libc/sys/linux/net/gethstent_r.c: Ditto. * libc/sys/linux/net/getnetbyad.c: Ditto. * libc/sys/linux/net/getnetbyad_r.c: Ditto. * libc/sys/linux/net/getnetbynm.c: Ditto. * libc/sys/linux/net/getnetbynm_r.c: Ditto. * libc/sys/linux/net/getnetent.c: Ditto. * libc/sys/linux/net/getnetent_r.c: Ditto. * libc/sys/linux/net/getnetgrent.c: Ditto. * libc/sys/linux/net/getnetgrent_r.c: Ditto. * libc/sys/linux/net/getnssent.c: Ditto. * libc/sys/linux/net/getnssent_r.c: Ditto. * libc/sys/linux/net/getproto_r.c: Ditto. * libc/sys/linux/net/getprtent.c: Ditto. * libc/sys/linux/net/getprtent_r.c: Ditto. * libc/sys/linux/net/getprtname.c: Ditto. * libc/sys/linux/net/getprtname_r.c: Ditto. * libc/sys/linux/net/getrpcbyname.c: Ditto. * libc/sys/linux/net/getrpcbyname_r.c: Ditto. * libc/sys/linux/net/getrpcbynumber.c: Ditto. * libc/sys/linux/net/getrpcbynumber_r.c: Ditto. * libc/sys/linux/net/getrpcent.c: Ditto. * libc/sys/linux/net/getrpcent_r.c: Ditto. * libc/sys/linux/net/getservent_r.c: Ditto. * libc/sys/linux/net/getsrvbynm.c: Ditto. * libc/sys/linux/net/getsrvbynm_r.c: Ditto. * libc/sys/linux/net/getsrvbypt.c: Ditto. * libc/sys/linux/net/getsrvbypt_r.c: Ditto. * libc/sys/linux/net/grp-lookup.c: Ditto. * libc/sys/linux/net/herrno.c: Ditto. * libc/sys/linux/net/hosts-lookup.c: Ditto. * libc/sys/linux/net/ifaddrs.h: Ditto. * libc/sys/linux/net/ifreq.c: Ditto. * libc/sys/linux/net/ifreq.h: Ditto. * libc/sys/linux/net/in6_addr.c: Ditto. * libc/sys/linux/net/inet6_option.c: Ditto. * libc/sys/linux/net/inet_mkadr.c: Ditto. * libc/sys/linux/net/inet_net.c: Ditto. * libc/sys/linux/net/key-lookup.c: Ditto. * libc/sys/linux/net/local.h: Ditto. * libc/sys/linux/net/netgroup.h: Ditto. * libc/sys/linux/net/netgrp-lookup.c: Ditto. * libc/sys/linux/net/network-lookup.c: Ditto. * libc/sys/linux/net/not-cancel.h: Ditto. * libc/sys/linux/net/ns_samedomain.c: Ditto. * libc/sys/linux/net/nscd-types.h: Ditto. * libc/sys/linux/net/nss.h: Ditto. * libc/sys/linux/net/nsswitch.c: Ditto. * libc/sys/linux/net/nsswitch.h: Ditto. * libc/sys/linux/net/opensock.c: Ditto. * libc/sys/linux/net/proto-lookup.c: Ditto. * libc/sys/linux/net/pwd-lookup.c: Ditto. * libc/sys/linux/net/res_debug.h: Ditto. * libc/sys/linux/net/res_hconf.c: Ditto. * libc/sys/linux/net/res_hconf.h: Ditto. * libc/sys/linux/net/rexec.c: Ditto. * libc/sys/linux/net/rpc-lookup.c: Ditto. * libc/sys/linux/net/ruserpass.c: Ditto. * libc/sys/linux/net/service-lookup.c: Ditto. * libc/sys/linux/net/spwd-lookup.c: Ditto. * libc/sys/linux/net/nscd/nscd-client.h: Ditto. * libc/sys/linux/net/nscd/nscd_proto.h: Ditto.
457 lines
11 KiB
C
457 lines
11 KiB
C
/* The Inner Net License, Version 2.00
|
|
|
|
The author(s) grant permission for redistribution and use in source and
|
|
binary forms, with or without modification, of the software and documentation
|
|
provided that the following conditions are met:
|
|
|
|
0. If you receive a version of the software that is specifically labelled
|
|
as not being for redistribution (check the version message and/or README),
|
|
you are not permitted to redistribute that version of the software in any
|
|
way or form.
|
|
1. All terms of the all other applicable copyrights and licenses must be
|
|
followed.
|
|
2. Redistributions of source code must retain the authors' copyright
|
|
notice(s), this list of conditions, and the following disclaimer.
|
|
3. Redistributions in binary form must reproduce the authors' copyright
|
|
notice(s), this list of conditions, and the following disclaimer in the
|
|
documentation and/or other materials provided with the distribution.
|
|
4. [The copyright holder has authorized the removal of this clause.]
|
|
5. Neither the name(s) of the author(s) 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 ITS AUTHORS 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 AUTHORS 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.
|
|
|
|
If these license terms cause you a real problem, contact the author. */
|
|
|
|
/* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */
|
|
|
|
#include <alloca.h>
|
|
#include <errno.h>
|
|
#include <netdb.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <net/if.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/param.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <sys/un.h>
|
|
#include <sys/utsname.h>
|
|
#define _IO_MTSAFE_IO
|
|
#include <bits/libc-lock.h>
|
|
#include <libc-symbols.h>
|
|
#include "local.h"
|
|
|
|
#ifdef HAVE_LIBIDN
|
|
# include <libidn/idna.h>
|
|
extern int __idna_to_unicode_lzlz (const char *input, char **output,
|
|
int flags);
|
|
#endif
|
|
|
|
#ifndef min
|
|
# define min(x,y) (((x) > (y)) ? (y) : (x))
|
|
#endif /* min */
|
|
|
|
libc_freeres_ptr (static char *domain);
|
|
|
|
|
|
static char *
|
|
internal_function
|
|
nrl_domainname (void)
|
|
{
|
|
static int not_first;
|
|
|
|
if (! not_first)
|
|
{
|
|
__libc_lock_define_initialized (static, lock);
|
|
__libc_lock_lock (lock);
|
|
|
|
if (! not_first)
|
|
{
|
|
char *c;
|
|
struct hostent *h, th;
|
|
size_t tmpbuflen = 1024;
|
|
char *tmpbuf = alloca (tmpbuflen);
|
|
int herror;
|
|
|
|
not_first = 1;
|
|
|
|
while (__gethostbyname_r ("localhost", &th, tmpbuf, tmpbuflen, &h,
|
|
&herror))
|
|
{
|
|
if (herror == NETDB_INTERNAL && errno == ERANGE)
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen);
|
|
else
|
|
break;
|
|
}
|
|
|
|
if (h && (c = strchr (h->h_name, '.')))
|
|
domain = strdup (++c);
|
|
else
|
|
{
|
|
/* The name contains no domain information. Use the name
|
|
now to get more information. */
|
|
while (__gethostname (tmpbuf, tmpbuflen))
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen, 2 * tmpbuflen);
|
|
|
|
if ((c = strchr (tmpbuf, '.')))
|
|
domain = strdup (++c);
|
|
else
|
|
{
|
|
/* We need to preserve the hostname. */
|
|
const char *hstname = alloca (strlen (tmpbuf) + 1);
|
|
strcpy (hstname, tmpbuf);
|
|
|
|
while (__gethostbyname_r (hstname, &th, tmpbuf, tmpbuflen,
|
|
&h, &herror))
|
|
{
|
|
if (herror == NETDB_INTERNAL && errno == ERANGE)
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
|
|
2 * tmpbuflen);
|
|
else
|
|
break;
|
|
}
|
|
|
|
if (h && (c = strchr(h->h_name, '.')))
|
|
domain = strdup (++c);
|
|
else
|
|
{
|
|
struct in_addr in_addr;
|
|
|
|
in_addr.s_addr = htonl (INADDR_LOOPBACK);
|
|
|
|
while (__gethostbyaddr_r ((const char *) &in_addr,
|
|
sizeof (struct in_addr),
|
|
AF_INET, &th, tmpbuf,
|
|
tmpbuflen, &h, &herror))
|
|
{
|
|
if (herror == NETDB_INTERNAL && errno == ERANGE)
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
|
|
2 * tmpbuflen);
|
|
else
|
|
break;
|
|
}
|
|
|
|
if (h && (c = strchr (h->h_name, '.')))
|
|
domain = strdup (++c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
__libc_lock_unlock (lock);
|
|
}
|
|
|
|
return domain;
|
|
};
|
|
|
|
|
|
int
|
|
getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
|
|
socklen_t hostlen, char *serv, socklen_t servlen,
|
|
unsigned int flags)
|
|
{
|
|
int serrno = errno;
|
|
int tmpbuflen = 1024;
|
|
int herrno;
|
|
char *tmpbuf = alloca (tmpbuflen);
|
|
struct hostent th;
|
|
int ok = 0;
|
|
|
|
if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM
|
|
#ifdef HAVE_LIBIDN
|
|
|NI_IDN|NI_IDN_ALLOW_UNASSIGNED|NI_IDN_USE_STD3_ASCII_RULES
|
|
#endif
|
|
))
|
|
return EAI_BADFLAGS;
|
|
|
|
if (sa == NULL || addrlen < sizeof (sa_family_t))
|
|
return EAI_FAMILY;
|
|
|
|
switch (sa->sa_family)
|
|
{
|
|
case AF_LOCAL:
|
|
if (addrlen < (socklen_t) (((struct sockaddr_un *) NULL)->sun_path))
|
|
return EAI_FAMILY;
|
|
break;
|
|
case AF_INET:
|
|
if (addrlen < sizeof (struct sockaddr_in))
|
|
return EAI_FAMILY;
|
|
break;
|
|
case AF_INET6:
|
|
if (addrlen < sizeof (struct sockaddr_in6))
|
|
return EAI_FAMILY;
|
|
break;
|
|
default:
|
|
return EAI_FAMILY;
|
|
}
|
|
|
|
if (host != NULL && hostlen > 0)
|
|
switch (sa->sa_family)
|
|
{
|
|
case AF_INET:
|
|
case AF_INET6:
|
|
if (!(flags & NI_NUMERICHOST))
|
|
{
|
|
struct hostent *h = NULL;
|
|
if (h == NULL)
|
|
{
|
|
if (sa->sa_family == AF_INET6)
|
|
{
|
|
while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
|
|
sizeof(struct in6_addr),
|
|
AF_INET6, &th, tmpbuf, tmpbuflen,
|
|
&h, &herrno))
|
|
{
|
|
if (herrno == NETDB_INTERNAL)
|
|
{
|
|
if (errno == ERANGE)
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
|
|
2 * tmpbuflen);
|
|
else
|
|
{
|
|
h_errno = (herrno);
|
|
errno = (serrno);
|
|
return EAI_SYSTEM;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (__gethostbyaddr_r ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
|
|
sizeof(struct in_addr), AF_INET,
|
|
&th, tmpbuf, tmpbuflen,
|
|
&h, &herrno))
|
|
{
|
|
if (errno == ERANGE)
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
|
|
2 * tmpbuflen);
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (h)
|
|
{
|
|
char *c;
|
|
if ((flags & NI_NOFQDN)
|
|
&& (c = nrl_domainname ())
|
|
&& (c = strstr (h->h_name, c))
|
|
&& (c != h->h_name) && (*(--c) == '.'))
|
|
/* Terminate the string after the prefix. */
|
|
*c = '\0';
|
|
|
|
#ifdef HAVE_LIBIDN
|
|
/* If requested, convert from the IDN format. */
|
|
if (flags & NI_IDN)
|
|
{
|
|
int idn_flags = 0;
|
|
if (flags & NI_IDN_ALLOW_UNASSIGNED)
|
|
idn_flags |= IDNA_ALLOW_UNASSIGNED;
|
|
if (flags & NI_IDN_USE_STD3_ASCII_RULES)
|
|
idn_flags |= IDNA_USE_STD3_ASCII_RULES;
|
|
|
|
char *out;
|
|
int rc = __idna_to_unicode_lzlz (h->h_name, &out,
|
|
idn_flags);
|
|
if (rc != IDNA_SUCCESS)
|
|
{
|
|
if (rc == IDNA_MALLOC_ERROR)
|
|
return EAI_MEMORY;
|
|
if (rc == IDNA_DLOPEN_ERROR)
|
|
return EAI_SYSTEM;
|
|
return EAI_IDN_ENCODE;
|
|
}
|
|
|
|
if (out != h->h_name)
|
|
{
|
|
h->h_name = strdupa (out);
|
|
free (out);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
size_t len = strlen (h->h_name) + 1;
|
|
if (len > hostlen)
|
|
return EAI_OVERFLOW;
|
|
|
|
memcpy (host, h->h_name, len);
|
|
|
|
ok = 1;
|
|
}
|
|
}
|
|
|
|
if (!ok)
|
|
{
|
|
if (flags & NI_NAMEREQD)
|
|
{
|
|
__set_errno (serrno);
|
|
return EAI_NONAME;
|
|
}
|
|
else
|
|
{
|
|
const char *c;
|
|
if (sa->sa_family == AF_INET6)
|
|
{
|
|
const struct sockaddr_in6 *sin6p;
|
|
uint32_t scopeid;
|
|
|
|
sin6p = (const struct sockaddr_in6 *) sa;
|
|
|
|
c = inet_ntop (AF_INET6,
|
|
(const void *) &sin6p->sin6_addr, host, hostlen);
|
|
scopeid = sin6p->sin6_scope_id;
|
|
if (scopeid != 0)
|
|
{
|
|
/* Buffer is >= IFNAMSIZ+1. */
|
|
char scopebuf[IFNAMSIZ + 1];
|
|
char *scopeptr;
|
|
int ni_numericscope = 0;
|
|
size_t real_hostlen = strnlen (host, hostlen);
|
|
size_t scopelen = 0;
|
|
|
|
scopebuf[0] = SCOPE_DELIMITER;
|
|
scopebuf[1] = '\0';
|
|
scopeptr = &scopebuf[1];
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
|
|
|| IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr))
|
|
{
|
|
if (if_indextoname (scopeid, scopeptr) == NULL)
|
|
++ni_numericscope;
|
|
else
|
|
scopelen = strlen (scopebuf);
|
|
}
|
|
else
|
|
++ni_numericscope;
|
|
|
|
if (ni_numericscope)
|
|
scopelen = 1 + snprintf (scopeptr,
|
|
(scopebuf
|
|
+ sizeof scopebuf
|
|
- scopeptr),
|
|
"%u", scopeid);
|
|
|
|
if (real_hostlen + scopelen + 1 > hostlen)
|
|
/* XXX We should not fail here. Simply enlarge
|
|
the buffer or return with out of memory. */
|
|
return EAI_SYSTEM;
|
|
memcpy (host + real_hostlen, scopebuf, scopelen + 1);
|
|
}
|
|
}
|
|
else
|
|
c = inet_ntop (AF_INET,
|
|
(const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
|
|
host, hostlen);
|
|
if (c == NULL)
|
|
{
|
|
__set_errno (serrno);
|
|
return EAI_SYSTEM;
|
|
}
|
|
}
|
|
ok = 1;
|
|
}
|
|
break;
|
|
|
|
case AF_LOCAL:
|
|
if (!(flags & NI_NUMERICHOST))
|
|
{
|
|
struct utsname utsname;
|
|
|
|
if (!uname (&utsname))
|
|
{
|
|
strncpy (host, utsname.nodename, hostlen);
|
|
break;
|
|
};
|
|
};
|
|
|
|
if (flags & NI_NAMEREQD)
|
|
{
|
|
__set_errno (serrno);
|
|
return EAI_NONAME;
|
|
}
|
|
|
|
strncpy (host, "localhost", hostlen);
|
|
break;
|
|
|
|
default:
|
|
return EAI_FAMILY;
|
|
}
|
|
|
|
if (serv && (servlen > 0))
|
|
switch (sa->sa_family)
|
|
{
|
|
case AF_INET:
|
|
case AF_INET6:
|
|
if (!(flags & NI_NUMERICSERV))
|
|
{
|
|
struct servent *s, ts;
|
|
while (__getservbyport_r (((const struct sockaddr_in *) sa)->sin_port,
|
|
((flags & NI_DGRAM) ? "udp" : "tcp"),
|
|
&ts, tmpbuf, tmpbuflen, &s))
|
|
{
|
|
if (herrno == NETDB_INTERNAL)
|
|
{
|
|
if (errno == ERANGE)
|
|
tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
|
|
2 * tmpbuflen);
|
|
else
|
|
{
|
|
__set_errno (serrno);
|
|
return EAI_SYSTEM;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
if (s)
|
|
{
|
|
strncpy (serv, s->s_name, servlen);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (snprintf (serv, servlen, "%d",
|
|
ntohs (((const struct sockaddr_in *) sa)->sin_port))
|
|
+ 1 > servlen)
|
|
return EAI_OVERFLOW;
|
|
|
|
break;
|
|
|
|
case AF_LOCAL:
|
|
strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
|
|
break;
|
|
}
|
|
|
|
if (host && (hostlen > 0))
|
|
host[hostlen-1] = 0;
|
|
if (serv && (servlen > 0))
|
|
serv[servlen-1] = 0;
|
|
errno = serrno;
|
|
return 0;
|
|
}
|
|
libc_hidden_def (getnameinfo)
|