2009-01-17 07:11:27 +03:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
ipsocket.c -
|
|
|
|
|
|
|
|
created at: Thu Mar 31 12:21:29 JST 1994
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "rubysocket.h"
|
|
|
|
|
|
|
|
struct inetsock_arg
|
|
|
|
{
|
|
|
|
VALUE sock;
|
|
|
|
struct {
|
|
|
|
VALUE host, serv;
|
|
|
|
struct addrinfo *res;
|
|
|
|
} remote, local;
|
|
|
|
int type;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
inetsock_cleanup(struct inetsock_arg *arg)
|
|
|
|
{
|
|
|
|
if (arg->remote.res) {
|
|
|
|
freeaddrinfo(arg->remote.res);
|
|
|
|
arg->remote.res = 0;
|
|
|
|
}
|
|
|
|
if (arg->local.res) {
|
|
|
|
freeaddrinfo(arg->local.res);
|
|
|
|
arg->local.res = 0;
|
|
|
|
}
|
|
|
|
if (arg->fd >= 0) {
|
|
|
|
close(arg->fd);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
init_inetsock_internal(struct inetsock_arg *arg)
|
|
|
|
{
|
|
|
|
int type = arg->type;
|
2013-06-28 07:16:07 +04:00
|
|
|
struct addrinfo *res, *lres;
|
2013-04-06 06:39:44 +04:00
|
|
|
int fd, status = 0, local = 0;
|
2009-01-17 07:11:27 +03:00
|
|
|
const char *syscall = 0;
|
|
|
|
|
2009-03-01 09:48:22 +03:00
|
|
|
arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv, SOCK_STREAM,
|
2009-01-17 07:11:27 +03:00
|
|
|
(type == INET_SERVER) ? AI_PASSIVE : 0);
|
|
|
|
/*
|
|
|
|
* Maybe also accept a local address
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (type != INET_SERVER && (!NIL_P(arg->local.host) || !NIL_P(arg->local.serv))) {
|
2009-03-01 09:48:22 +03:00
|
|
|
arg->local.res = rsock_addrinfo(arg->local.host, arg->local.serv, SOCK_STREAM, 0);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
arg->fd = fd = -1;
|
|
|
|
for (res = arg->remote.res; res; res = res->ai_next) {
|
2009-07-22 12:42:12 +04:00
|
|
|
#if !defined(INET6) && defined(AF_INET6)
|
|
|
|
if (res->ai_family == AF_INET6)
|
|
|
|
continue;
|
|
|
|
#endif
|
2013-06-28 07:16:07 +04:00
|
|
|
lres = NULL;
|
|
|
|
if (arg->local.res) {
|
|
|
|
for (lres = arg->local.res; lres; lres = lres->ai_next) {
|
|
|
|
if (lres->ai_family == res->ai_family)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!lres)
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-01 09:48:22 +03:00
|
|
|
status = rsock_socket(res->ai_family,res->ai_socktype,res->ai_protocol);
|
2009-01-17 07:11:27 +03:00
|
|
|
syscall = "socket(2)";
|
|
|
|
fd = status;
|
|
|
|
if (fd < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
arg->fd = fd;
|
|
|
|
if (type == INET_SERVER) {
|
|
|
|
#if !defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
status = 1;
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
2010-04-28 12:14:13 +04:00
|
|
|
(char*)&status, (socklen_t)sizeof(status));
|
2009-01-17 07:11:27 +03:00
|
|
|
#endif
|
|
|
|
status = bind(fd, res->ai_addr, res->ai_addrlen);
|
|
|
|
syscall = "bind(2)";
|
|
|
|
}
|
|
|
|
else {
|
2013-06-28 07:16:07 +04:00
|
|
|
if (lres) {
|
|
|
|
status = bind(fd, lres->ai_addr, lres->ai_addrlen);
|
2013-04-06 06:39:44 +04:00
|
|
|
local = status;
|
2009-01-17 07:11:27 +03:00
|
|
|
syscall = "bind(2)";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status >= 0) {
|
2009-03-01 09:48:22 +03:00
|
|
|
status = rsock_connect(fd, res->ai_addr, res->ai_addrlen,
|
|
|
|
(type == INET_SOCKS));
|
2009-01-17 07:11:27 +03:00
|
|
|
syscall = "connect(2)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
close(fd);
|
|
|
|
arg->fd = fd = -1;
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status < 0) {
|
2013-04-06 06:39:44 +04:00
|
|
|
VALUE host, port;
|
|
|
|
|
|
|
|
if (local < 0) {
|
|
|
|
host = arg->local.host;
|
|
|
|
port = arg->local.serv;
|
|
|
|
} else {
|
|
|
|
host = arg->remote.host;
|
|
|
|
port = arg->remote.serv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rsock_sys_fail_host_port(syscall, host, port);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
arg->fd = -1;
|
|
|
|
|
2010-04-09 18:53:19 +04:00
|
|
|
if (type == INET_SERVER) {
|
2011-08-12 03:20:15 +04:00
|
|
|
status = listen(fd, SOMAXCONN);
|
2010-04-09 18:53:19 +04:00
|
|
|
if (status < 0) {
|
|
|
|
close(fd);
|
2011-03-16 14:42:03 +03:00
|
|
|
rb_sys_fail("listen(2)");
|
2010-04-09 18:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-17 07:11:27 +03:00
|
|
|
|
|
|
|
/* create new instance */
|
2009-03-01 09:30:41 +03:00
|
|
|
return rsock_init_sock(arg->sock, fd);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 09:30:41 +03:00
|
|
|
rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv,
|
|
|
|
VALUE local_host, VALUE local_serv, int type)
|
2009-01-17 07:11:27 +03:00
|
|
|
{
|
|
|
|
struct inetsock_arg arg;
|
|
|
|
arg.sock = sock;
|
|
|
|
arg.remote.host = remote_host;
|
|
|
|
arg.remote.serv = remote_serv;
|
|
|
|
arg.remote.res = 0;
|
|
|
|
arg.local.host = local_host;
|
|
|
|
arg.local.serv = local_serv;
|
|
|
|
arg.local.res = 0;
|
|
|
|
arg.type = type;
|
|
|
|
arg.fd = -1;
|
|
|
|
return rb_ensure(init_inetsock_internal, (VALUE)&arg,
|
|
|
|
inetsock_cleanup, (VALUE)&arg);
|
|
|
|
}
|
|
|
|
|
2010-02-06 05:35:11 +03:00
|
|
|
static ID id_numeric, id_hostname;
|
|
|
|
|
|
|
|
int
|
|
|
|
rsock_revlookup_flag(VALUE revlookup, int *norevlookup)
|
|
|
|
{
|
2011-03-27 18:16:50 +04:00
|
|
|
#define return_norevlookup(x) {*norevlookup = (x); return 1;}
|
2010-02-06 05:35:11 +03:00
|
|
|
ID id;
|
|
|
|
|
|
|
|
switch (revlookup) {
|
|
|
|
case Qtrue: return_norevlookup(0);
|
|
|
|
case Qfalse: return_norevlookup(1);
|
|
|
|
case Qnil: break;
|
|
|
|
default:
|
|
|
|
Check_Type(revlookup, T_SYMBOL);
|
|
|
|
id = SYM2ID(revlookup);
|
|
|
|
if (id == id_numeric) return_norevlookup(1);
|
|
|
|
if (id == id_hostname) return_norevlookup(0);
|
|
|
|
rb_raise(rb_eArgError, "invalid reverse_lookup flag: :%s", rb_id2name(id));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#undef return_norevlookup
|
|
|
|
}
|
|
|
|
|
2009-01-17 07:11:27 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-04-22 12:04:13 +04:00
|
|
|
* ipsocket.addr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
|
2009-01-17 07:11:27 +03:00
|
|
|
*
|
|
|
|
* Returns the local address as an array which contains
|
2010-04-22 12:04:13 +04:00
|
|
|
* address_family, port, hostname and numeric_address.
|
2009-01-17 07:11:27 +03:00
|
|
|
*
|
2010-02-06 05:35:11 +03:00
|
|
|
* If +reverse_lookup+ is +true+ or +:hostname+,
|
2009-01-17 07:11:27 +03:00
|
|
|
* hostname is obtained from numeric_address using reverse lookup.
|
2010-02-06 05:35:11 +03:00
|
|
|
* Or if it is +false+, or +:numeric+,
|
2009-01-17 07:11:27 +03:00
|
|
|
* hostname is same as numeric_address.
|
2010-02-06 05:35:11 +03:00
|
|
|
* Or if it is +nil+ or ommitted, obeys to +ipsocket.do_not_reverse_lookup+.
|
|
|
|
* See +Socket.getaddrinfo+ also.
|
2009-01-17 07:11:27 +03:00
|
|
|
*
|
|
|
|
* TCPSocket.open("www.ruby-lang.org", 80) {|sock|
|
|
|
|
* p sock.addr #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
|
2010-02-06 05:35:11 +03:00
|
|
|
* p sock.addr(true) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
|
|
|
|
* p sock.addr(false) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
|
|
|
|
* p sock.addr(:hostname) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
|
|
|
|
* p sock.addr(:numeric) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
|
2009-01-17 07:11:27 +03:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
2010-02-06 05:35:11 +03:00
|
|
|
ip_addr(int argc, VALUE *argv, VALUE sock)
|
2009-01-17 07:11:27 +03:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
2013-02-24 21:51:17 +04:00
|
|
|
union_sockaddr addr;
|
2010-04-28 12:14:13 +04:00
|
|
|
socklen_t len = (socklen_t)sizeof addr;
|
2010-02-06 05:35:11 +03:00
|
|
|
int norevlookup;
|
2009-01-17 07:11:27 +03:00
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
|
2010-02-06 05:35:11 +03:00
|
|
|
if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup))
|
|
|
|
norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
|
2013-02-24 21:51:17 +04:00
|
|
|
if (getsockname(fptr->fd, &addr.addr, &len) < 0)
|
2009-01-17 07:11:27 +03:00
|
|
|
rb_sys_fail("getsockname(2)");
|
2013-02-24 21:51:17 +04:00
|
|
|
return rsock_ipaddr(&addr.addr, len, norevlookup);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-04-22 12:04:13 +04:00
|
|
|
* ipsocket.peeraddr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
|
2009-01-17 07:11:27 +03:00
|
|
|
*
|
|
|
|
* Returns the remote address as an array which contains
|
2010-04-22 12:04:13 +04:00
|
|
|
* address_family, port, hostname and numeric_address.
|
2009-11-04 15:02:37 +03:00
|
|
|
* It is defined for connection oriented socket such as TCPSocket.
|
2009-01-17 07:11:27 +03:00
|
|
|
*
|
2010-02-06 05:35:11 +03:00
|
|
|
* If +reverse_lookup+ is +true+ or +:hostname+,
|
|
|
|
* hostname is obtained from numeric_address using reverse lookup.
|
|
|
|
* Or if it is +false+, or +:numeric+,
|
|
|
|
* hostname is same as numeric_address.
|
|
|
|
* Or if it is +nil+ or ommitted, obeys to +ipsocket.do_not_reverse_lookup+.
|
|
|
|
* See +Socket.getaddrinfo+ also.
|
|
|
|
*
|
2009-01-17 07:11:27 +03:00
|
|
|
* TCPSocket.open("www.ruby-lang.org", 80) {|sock|
|
|
|
|
* p sock.peeraddr #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
|
2012-11-23 05:46:51 +04:00
|
|
|
* p sock.peeraddr(true) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
|
2010-02-06 05:35:11 +03:00
|
|
|
* p sock.peeraddr(false) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
|
|
|
|
* p sock.peeraddr(:hostname) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
|
|
|
|
* p sock.peeraddr(:numeric) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
|
2009-01-17 07:11:27 +03:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
2010-02-06 05:35:11 +03:00
|
|
|
ip_peeraddr(int argc, VALUE *argv, VALUE sock)
|
2009-01-17 07:11:27 +03:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
2013-02-24 21:51:17 +04:00
|
|
|
union_sockaddr addr;
|
2010-04-28 12:14:13 +04:00
|
|
|
socklen_t len = (socklen_t)sizeof addr;
|
2010-02-06 05:35:11 +03:00
|
|
|
int norevlookup;
|
2009-01-17 07:11:27 +03:00
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
|
2010-02-06 05:35:11 +03:00
|
|
|
if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup))
|
|
|
|
norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
|
2013-02-24 21:51:17 +04:00
|
|
|
if (getpeername(fptr->fd, &addr.addr, &len) < 0)
|
2009-01-17 07:11:27 +03:00
|
|
|
rb_sys_fail("getpeername(2)");
|
2013-02-24 21:51:17 +04:00
|
|
|
return rsock_ipaddr(&addr.addr, len, norevlookup);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ipsocket.recvfrom(maxlen) => [mesg, ipaddr]
|
|
|
|
* ipsocket.recvfrom(maxlen, flags) => [mesg, ipaddr]
|
|
|
|
*
|
|
|
|
* Receives a message and return the message as a string and
|
|
|
|
* an address which the message come from.
|
|
|
|
*
|
|
|
|
* _maxlen_ is the maximum number of bytes to receive.
|
|
|
|
*
|
|
|
|
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
|
|
|
|
*
|
|
|
|
* ipaddr is same as IPSocket#{peeraddr,addr}.
|
|
|
|
*
|
|
|
|
* u1 = UDPSocket.new
|
|
|
|
* u1.bind("127.0.0.1", 4913)
|
|
|
|
* u2 = UDPSocket.new
|
|
|
|
* u2.send "uuuu", 0, "127.0.0.1", 4913
|
|
|
|
* p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
|
2010-04-22 12:04:13 +04:00
|
|
|
*
|
2009-01-17 07:11:27 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ip_recvfrom(int argc, VALUE *argv, VALUE sock)
|
|
|
|
{
|
2009-03-01 09:30:41 +03:00
|
|
|
return rsock_s_recvfrom(sock, argc, argv, RECV_IP);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* IPSocket.getaddress(host) => ipaddress
|
|
|
|
*
|
2010-03-22 06:33:14 +03:00
|
|
|
* Lookups the IP address of _host_.
|
2009-01-17 07:11:27 +03:00
|
|
|
*
|
|
|
|
* IPSocket.getaddress("localhost") #=> "127.0.0.1"
|
|
|
|
* IPSocket.getaddress("ip6-localhost") #=> "::1"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ip_s_getaddress(VALUE obj, VALUE host)
|
|
|
|
{
|
2013-02-24 21:51:17 +04:00
|
|
|
union_sockaddr addr;
|
2009-03-01 09:48:22 +03:00
|
|
|
struct addrinfo *res = rsock_addrinfo(host, Qnil, SOCK_STREAM, 0);
|
2009-01-17 07:11:27 +03:00
|
|
|
|
|
|
|
/* just take the first one */
|
|
|
|
memcpy(&addr, res->ai_addr, res->ai_addrlen);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
2013-02-24 21:51:17 +04:00
|
|
|
return rsock_make_ipaddr(&addr.addr, res->ai_addrlen);
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-22 19:15:21 +03:00
|
|
|
rsock_init_ipsocket(void)
|
2009-01-17 07:11:27 +03:00
|
|
|
{
|
2011-08-16 03:08:39 +04:00
|
|
|
/*
|
|
|
|
* Document-class: IPSocket < BasicSocket
|
|
|
|
*
|
|
|
|
* IPSocket is the super class of TCPSocket and UDPSocket.
|
|
|
|
*/
|
2009-01-17 07:11:27 +03:00
|
|
|
rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
|
2010-02-06 05:35:11 +03:00
|
|
|
rb_define_method(rb_cIPSocket, "addr", ip_addr, -1);
|
|
|
|
rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
|
2009-01-17 07:11:27 +03:00
|
|
|
rb_define_method(rb_cIPSocket, "recvfrom", ip_recvfrom, -1);
|
|
|
|
rb_define_singleton_method(rb_cIPSocket, "getaddress", ip_s_getaddress, 1);
|
2009-02-11 05:48:08 +03:00
|
|
|
rb_undef_method(rb_cIPSocket, "getpeereid");
|
|
|
|
|
2010-02-06 05:35:11 +03:00
|
|
|
id_numeric = rb_intern_const("numeric");
|
|
|
|
id_hostname = rb_intern_const("hostname");
|
2009-01-17 07:11:27 +03:00
|
|
|
}
|