1999-08-13 01:37:52 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
1999-08-13 01:37:52 -04:00
|
|
|
* 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
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 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.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
1999-08-13 01:37:52 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issues to be discussed:
|
|
|
|
* - Thread safe-ness must be checked
|
|
|
|
* - Return values. There seems to be no standard for return value (RFC2133)
|
|
|
|
* but INRIA implementation returns EAI_xxx defined for getaddrinfo().
|
|
|
|
*/
|
|
|
|
|
2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/config.h"
|
2009-11-19 06:44:29 -05:00
|
|
|
#ifdef RUBY_EXTCONF_H
|
|
|
|
#include RUBY_EXTCONF_H
|
|
|
|
#endif
|
2007-07-22 01:33:54 -04:00
|
|
|
#include <stdio.h>
|
1999-08-13 01:37:52 -04:00
|
|
|
#include <sys/types.h>
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#ifndef _WIN32
|
2008-09-18 20:33:19 -04:00
|
|
|
#if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
|
2000-07-10 00:49:24 -04:00
|
|
|
# include <net/socket.h>
|
|
|
|
#else
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
1999-08-13 01:37:52 -04:00
|
|
|
#include <netinet/in.h>
|
2000-07-10 00:49:24 -04:00
|
|
|
#if defined(HAVE_ARPA_INET_H)
|
1999-08-13 01:37:52 -04:00
|
|
|
#include <arpa/inet.h>
|
2000-07-10 00:49:24 -04:00
|
|
|
#endif
|
1999-08-13 01:37:52 -04:00
|
|
|
#if defined(HAVE_ARPA_NAMESER_H)
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#endif
|
|
|
|
#include <netdb.h>
|
|
|
|
#if defined(HAVE_RESOLV_H)
|
|
|
|
#include <resolv.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#ifdef _WIN32
|
1999-08-13 01:37:52 -04:00
|
|
|
#include <winsock2.h>
|
2009-01-12 22:11:17 -05:00
|
|
|
#include <ws2tcpip.h>
|
1999-08-13 01:37:52 -04:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
1999-10-04 00:51:08 -04:00
|
|
|
#ifdef SOCKS5
|
|
|
|
#include <socks.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
#include "addrinfo.h"
|
|
|
|
#include "sockport.h"
|
|
|
|
|
|
|
|
#define SUCCESS 0
|
|
|
|
#define ANY 0
|
|
|
|
#define YES 1
|
|
|
|
#define NO 0
|
|
|
|
|
|
|
|
struct sockinet {
|
|
|
|
u_char si_len;
|
|
|
|
u_char si_family;
|
|
|
|
u_short si_port;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct afd {
|
|
|
|
int a_af;
|
|
|
|
int a_addrlen;
|
|
|
|
int a_socklen;
|
|
|
|
int a_off;
|
|
|
|
} afdl [] = {
|
|
|
|
#ifdef INET6
|
|
|
|
#define N_INET6 0
|
|
|
|
{PF_INET6, sizeof(struct in6_addr),
|
|
|
|
sizeof(struct sockaddr_in6),
|
|
|
|
offsetof(struct sockaddr_in6, sin6_addr)},
|
|
|
|
#define N_INET 1
|
|
|
|
#else
|
|
|
|
#define N_INET 0
|
|
|
|
#endif
|
|
|
|
{PF_INET, sizeof(struct in_addr),
|
|
|
|
sizeof(struct sockaddr_in),
|
|
|
|
offsetof(struct sockaddr_in, sin_addr)},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ENI_NOSOCKET 0
|
|
|
|
#define ENI_NOSERVNAME 1
|
|
|
|
#define ENI_NOHOSTNAME 2
|
|
|
|
#define ENI_MEMORY 3
|
|
|
|
#define ENI_SYSTEM 4
|
|
|
|
#define ENI_FAMILY 5
|
|
|
|
#define ENI_SALEN 6
|
|
|
|
|
|
|
|
#ifndef HAVE_INET_NTOP
|
|
|
|
static const char *
|
2006-06-20 14:02:17 -04:00
|
|
|
inet_ntop(int af, const void *addr, char *numaddr, size_t numaddr_len)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INET_NTOA
|
|
|
|
struct in_addr in;
|
|
|
|
memcpy(&in.s_addr, addr, sizeof(in.s_addr));
|
|
|
|
snprintf(numaddr, numaddr_len, "%s", inet_ntoa(in));
|
|
|
|
#else
|
|
|
|
unsigned long x = ntohl(*(unsigned long*)addr);
|
|
|
|
snprintf(numaddr, numaddr_len, "%d.%d.%d.%d",
|
|
|
|
(int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
|
|
|
|
(int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
|
|
|
|
#endif
|
|
|
|
return numaddr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2009-12-30 15:56:41 -05:00
|
|
|
getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct afd *afd;
|
|
|
|
struct hostent *hp;
|
|
|
|
u_short port;
|
|
|
|
int family, len, i;
|
|
|
|
char *addr, *p;
|
|
|
|
u_long v4a;
|
|
|
|
#ifdef INET6
|
|
|
|
u_char pfx;
|
|
|
|
#endif
|
|
|
|
int h_error;
|
|
|
|
char numserv[512];
|
|
|
|
char numaddr[512];
|
|
|
|
|
|
|
|
if (sa == NULL)
|
|
|
|
return ENI_NOSOCKET;
|
|
|
|
|
|
|
|
len = SA_LEN(sa);
|
|
|
|
if (len != salen) return ENI_SALEN;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
family = sa->sa_family;
|
|
|
|
for (i = 0; afdl[i].a_af; i++)
|
|
|
|
if (afdl[i].a_af == family) {
|
|
|
|
afd = &afdl[i];
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
return ENI_FAMILY;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
found:
|
|
|
|
if (len != afd->a_socklen) return ENI_SALEN;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
port = ((struct sockinet *)sa)->si_port; /* network byte order */
|
|
|
|
addr = (char *)sa + afd->a_off;
|
|
|
|
|
|
|
|
if (serv == NULL || servlen == 0) {
|
|
|
|
/* what we should do? */
|
|
|
|
} else if (flags & NI_NUMERICSERV) {
|
|
|
|
snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
|
2001-10-02 00:31:23 -04:00
|
|
|
if (strlen(numserv) + 1 > servlen)
|
1999-08-13 01:37:52 -04:00
|
|
|
return ENI_MEMORY;
|
|
|
|
strcpy(serv, numserv);
|
|
|
|
} else {
|
2000-07-10 00:49:24 -04:00
|
|
|
#if defined(HAVE_GETSERVBYPORT)
|
2009-01-06 05:18:44 -05:00
|
|
|
struct servent *sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
|
1999-08-13 01:37:52 -04:00
|
|
|
if (sp) {
|
2001-10-02 00:31:23 -04:00
|
|
|
if (strlen(sp->s_name) + 1 > servlen)
|
1999-08-13 01:37:52 -04:00
|
|
|
return ENI_MEMORY;
|
|
|
|
strcpy(serv, sp->s_name);
|
|
|
|
} else
|
|
|
|
return ENI_NOSERVNAME;
|
2000-07-10 00:49:24 -04:00
|
|
|
#else
|
|
|
|
return ENI_NOSERVNAME;
|
|
|
|
#endif
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
2001-10-02 00:31:23 -04:00
|
|
|
v4a = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
|
1999-08-13 01:37:52 -04:00
|
|
|
if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
|
|
|
|
flags |= NI_NUMERICHOST;
|
|
|
|
v4a >>= IN_CLASSA_NSHIFT;
|
2001-10-02 00:31:23 -04:00
|
|
|
if (v4a == 0)
|
2010-04-22 04:21:01 -04:00
|
|
|
flags |= NI_NUMERICHOST;
|
1999-08-13 01:37:52 -04:00
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
2003-09-02 01:12:39 -04:00
|
|
|
#ifdef HAVE_ADDR8
|
1999-08-13 01:37:52 -04:00
|
|
|
pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0];
|
2003-09-02 01:12:39 -04:00
|
|
|
#else
|
|
|
|
pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
|
|
|
|
#endif
|
1999-08-13 01:37:52 -04:00
|
|
|
if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
|
|
|
|
flags |= NI_NUMERICHOST;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (host == NULL || hostlen == 0) {
|
|
|
|
/* what should we do? */
|
|
|
|
} else if (flags & NI_NUMERICHOST) {
|
|
|
|
if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
|
|
|
|
== NULL)
|
|
|
|
return ENI_SYSTEM;
|
|
|
|
if (strlen(numaddr) > hostlen)
|
|
|
|
return ENI_MEMORY;
|
|
|
|
strcpy(host, numaddr);
|
|
|
|
} else {
|
|
|
|
#ifdef INET6
|
|
|
|
hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
|
|
|
|
#else
|
|
|
|
hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
|
|
|
|
h_error = h_errno;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (hp) {
|
|
|
|
if (flags & NI_NOFQDN) {
|
|
|
|
p = strchr(hp->h_name, '.');
|
|
|
|
if (p) *p = '\0';
|
|
|
|
}
|
2001-10-02 00:31:23 -04:00
|
|
|
if (strlen(hp->h_name) + 1 > hostlen) {
|
1999-08-13 01:37:52 -04:00
|
|
|
#ifdef INET6
|
|
|
|
freehostent(hp);
|
|
|
|
#endif
|
|
|
|
return ENI_MEMORY;
|
|
|
|
}
|
|
|
|
strcpy(host, hp->h_name);
|
|
|
|
#ifdef INET6
|
|
|
|
freehostent(hp);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
if (flags & NI_NAMEREQD)
|
|
|
|
return ENI_NOHOSTNAME;
|
|
|
|
if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
|
|
|
|
== NULL)
|
|
|
|
return ENI_NOHOSTNAME;
|
|
|
|
if (strlen(numaddr) > hostlen)
|
|
|
|
return ENI_MEMORY;
|
|
|
|
strcpy(host, numaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|