2009-02-02 05:51:02 +03:00
|
|
|
#include "rubysocket.h"
|
|
|
|
|
2009-02-22 20:46:51 +03:00
|
|
|
#include <time.h>
|
|
|
|
|
2014-01-28 18:37:34 +04:00
|
|
|
int rsock_cmsg_cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
static VALUE sym_wait_readable, sym_wait_writable;
|
2014-01-28 18:37:34 +04:00
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE rb_cAncillaryData;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
constant_to_sym(int constant, ID (*intern_const)(int))
|
|
|
|
{
|
|
|
|
ID name = intern_const(constant);
|
|
|
|
if (name) {
|
|
|
|
return ID2SYM(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2NUM(constant);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-02-09 19:29:16 +03:00
|
|
|
ip_cmsg_type_to_sym(int level, int cmsg_type)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
2009-03-01 09:30:41 +03:00
|
|
|
return constant_to_sym(cmsg_type, rsock_intern_scm_optname);
|
2009-02-02 05:51:02 +03:00
|
|
|
case IPPROTO_IP:
|
2009-03-01 09:30:41 +03:00
|
|
|
return constant_to_sym(cmsg_type, rsock_intern_ip_optname);
|
2009-02-02 11:12:10 +03:00
|
|
|
#ifdef IPPROTO_IPV6
|
2009-02-02 05:51:02 +03:00
|
|
|
case IPPROTO_IPV6:
|
2009-03-01 09:30:41 +03:00
|
|
|
return constant_to_sym(cmsg_type, rsock_intern_ipv6_optname);
|
2009-02-02 11:12:10 +03:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
case IPPROTO_TCP:
|
2009-03-01 09:30:41 +03:00
|
|
|
return constant_to_sym(cmsg_type, rsock_intern_tcp_optname);
|
2009-02-02 05:51:02 +03:00
|
|
|
case IPPROTO_UDP:
|
2009-03-01 09:30:41 +03:00
|
|
|
return constant_to_sym(cmsg_type, rsock_intern_udp_optname);
|
2009-02-02 05:51:02 +03:00
|
|
|
default:
|
|
|
|
return INT2NUM(cmsg_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-09 13:49:16 +03:00
|
|
|
* Socket::AncillaryData.new(family, cmsg_level, cmsg_type, cmsg_data) -> ancillarydata
|
|
|
|
*
|
|
|
|
* _family_ should be an integer, a string or a symbol.
|
|
|
|
* - Socket::AF_INET, "AF_INET", "INET", :AF_INET, :INET
|
|
|
|
* - Socket::AF_UNIX, "AF_UNIX", "UNIX", :AF_UNIX, :UNIX
|
|
|
|
* - etc.
|
2009-02-02 05:51:02 +03:00
|
|
|
*
|
|
|
|
* _cmsg_level_ should be an integer, a string or a symbol.
|
|
|
|
* - Socket::SOL_SOCKET, "SOL_SOCKET", "SOCKET", :SOL_SOCKET and :SOCKET
|
|
|
|
* - Socket::IPPROTO_IP, "IP" and :IP
|
|
|
|
* - Socket::IPPROTO_IPV6, "IPV6" and :IPV6
|
2009-02-09 15:49:24 +03:00
|
|
|
* - Socket::IPPROTO_TCP, "TCP" and :TCP
|
2009-02-02 05:51:02 +03:00
|
|
|
* - etc.
|
|
|
|
*
|
|
|
|
* _cmsg_type_ should be an integer, a string or a symbol.
|
2009-11-04 15:02:37 +03:00
|
|
|
* If a string/symbol is specified, it is interpreted depend on _cmsg_level_.
|
2009-02-02 05:51:02 +03:00
|
|
|
* - Socket::SCM_RIGHTS, "SCM_RIGHTS", "RIGHTS", :SCM_RIGHTS, :RIGHTS for SOL_SOCKET
|
|
|
|
* - Socket::IP_RECVTTL, "RECVTTL" and :RECVTTL for IPPROTO_IP
|
|
|
|
* - Socket::IPV6_PKTINFO, "PKTINFO" and :PKTINFO for IPPROTO_IPV6
|
|
|
|
* - etc.
|
|
|
|
*
|
|
|
|
* _cmsg_data_ should be a string.
|
|
|
|
*
|
2009-02-10 03:18:53 +03:00
|
|
|
* p Socket::AncillaryData.new(:INET, :TCP, :NODELAY, "")
|
|
|
|
* #=> #<Socket::AncillaryData: INET TCP NODELAY "">
|
2009-02-09 15:49:24 +03:00
|
|
|
*
|
2010-04-22 12:04:13 +04:00
|
|
|
* p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "")
|
2009-02-09 15:49:24 +03:00
|
|
|
* #=> #<Socket::AncillaryData: INET6 IPV6 PKTINFO "">
|
2009-02-02 05:51:02 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-09 13:49:16 +03:00
|
|
|
ancillary_initialize(VALUE self, VALUE vfamily, VALUE vlevel, VALUE vtype, VALUE data)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
2009-03-01 09:30:41 +03:00
|
|
|
int family = rsock_family_arg(vfamily);
|
|
|
|
int level = rsock_level_arg(family, vlevel);
|
|
|
|
int type = rsock_cmsg_type_arg(family, level, vtype);
|
2009-02-02 05:51:02 +03:00
|
|
|
StringValue(data);
|
2009-02-09 13:49:16 +03:00
|
|
|
rb_ivar_set(self, rb_intern("family"), INT2NUM(family));
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_ivar_set(self, rb_intern("level"), INT2NUM(level));
|
2009-02-09 13:49:16 +03:00
|
|
|
rb_ivar_set(self, rb_intern("type"), INT2NUM(type));
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_ivar_set(self, rb_intern("data"), data);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-02-09 13:49:16 +03:00
|
|
|
ancdata_new(int family, int level, int type, VALUE data)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
2012-10-20 10:57:51 +04:00
|
|
|
NEWOBJ_OF(obj, struct RObject, rb_cAncillaryData, T_OBJECT);
|
2009-02-02 05:51:02 +03:00
|
|
|
StringValue(data);
|
2009-02-09 13:49:16 +03:00
|
|
|
ancillary_initialize((VALUE)obj, INT2NUM(family), INT2NUM(level), INT2NUM(type), data);
|
2009-02-02 05:51:02 +03:00
|
|
|
return (VALUE)obj;
|
|
|
|
}
|
|
|
|
|
2009-02-09 13:49:16 +03:00
|
|
|
static int
|
|
|
|
ancillary_family(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE v = rb_attr_get(self, rb_intern("family"));
|
|
|
|
return NUM2INT(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.family => integer
|
|
|
|
*
|
|
|
|
* returns the socket family as an integer.
|
|
|
|
*
|
|
|
|
* p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").family
|
2009-02-09 15:49:24 +03:00
|
|
|
* #=> 10
|
2009-02-09 13:49:16 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_family_m(VALUE self)
|
|
|
|
{
|
|
|
|
return INT2NUM(ancillary_family(self));
|
|
|
|
}
|
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
static int
|
|
|
|
ancillary_level(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE v = rb_attr_get(self, rb_intern("level"));
|
|
|
|
return NUM2INT(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.level => integer
|
|
|
|
*
|
|
|
|
* returns the cmsg level as an integer.
|
|
|
|
*
|
2009-02-09 13:49:16 +03:00
|
|
|
* p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").level
|
2009-02-02 05:51:02 +03:00
|
|
|
* #=> 41
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_level_m(VALUE self)
|
|
|
|
{
|
|
|
|
return INT2NUM(ancillary_level(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ancillary_type(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE v = rb_attr_get(self, rb_intern("type"));
|
|
|
|
return NUM2INT(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.type => integer
|
|
|
|
*
|
|
|
|
* returns the cmsg type as an integer.
|
|
|
|
*
|
2009-02-09 13:49:16 +03:00
|
|
|
* p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").type
|
2009-02-02 05:51:02 +03:00
|
|
|
* #=> 2
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_type_m(VALUE self)
|
|
|
|
{
|
|
|
|
return INT2NUM(ancillary_type(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.data => string
|
|
|
|
*
|
|
|
|
* returns the cmsg data as a string.
|
|
|
|
*
|
2009-02-09 13:49:16 +03:00
|
|
|
* p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").data
|
2009-02-02 05:51:02 +03:00
|
|
|
* #=> ""
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_data(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE v = rb_attr_get(self, rb_intern("data"));
|
|
|
|
StringValue(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#ifdef SCM_RIGHTS
|
2009-02-25 17:19:24 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Socket::AncillaryData.unix_rights(io1, io2, ...) => ancillarydata
|
|
|
|
*
|
|
|
|
* Creates a new Socket::AncillaryData object which contains file descriptors as data.
|
|
|
|
*
|
|
|
|
* p Socket::AncillaryData.unix_rights(STDERR)
|
|
|
|
* #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 2>
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_s_unix_rights(int argc, VALUE *argv, VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE result, str, ary;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ary = rb_ary_new();
|
|
|
|
|
|
|
|
for (i = 0 ; i < argc; i++) {
|
|
|
|
VALUE obj = argv[i];
|
2012-05-23 11:13:21 +04:00
|
|
|
if (!RB_TYPE_P(obj, T_FILE)) {
|
2009-02-25 17:19:24 +03:00
|
|
|
rb_raise(rb_eTypeError, "IO expected");
|
|
|
|
}
|
|
|
|
rb_ary_push(ary, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
str = rb_str_buf_new(sizeof(int) * argc);
|
|
|
|
|
|
|
|
for (i = 0 ; i < argc; i++) {
|
2015-11-05 04:09:17 +03:00
|
|
|
VALUE obj = RARRAY_AREF(ary, i);
|
2009-02-25 17:19:24 +03:00
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
GetOpenFile(obj, fptr);
|
|
|
|
fd = fptr->fd;
|
|
|
|
rb_str_buf_cat(str, (char *)&fd, sizeof(int));
|
|
|
|
}
|
|
|
|
|
|
|
|
result = ancdata_new(AF_UNIX, SOL_SOCKET, SCM_RIGHTS, str);
|
|
|
|
rb_ivar_set(result, rb_intern("unix_rights"), ary);
|
|
|
|
return result;
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-25 17:19:24 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_s_unix_rights rb_f_notimplement
|
2009-02-25 17:19:24 +03:00
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#ifdef SCM_RIGHTS
|
2009-02-18 21:43:15 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-27 17:23:54 +03:00
|
|
|
* ancillarydata.unix_rights => array-of-IOs or nil
|
2009-02-18 21:43:15 +03:00
|
|
|
*
|
2009-02-27 17:23:54 +03:00
|
|
|
* returns the array of IO objects for SCM_RIGHTS control message in UNIX domain socket.
|
2009-02-18 21:43:15 +03:00
|
|
|
*
|
2010-04-22 12:04:13 +04:00
|
|
|
* The class of the IO objects in the array is IO or Socket.
|
2009-02-18 21:43:15 +03:00
|
|
|
*
|
2009-02-27 17:23:54 +03:00
|
|
|
* The array is attached to _ancillarydata_ when it is instantiated.
|
|
|
|
* For example, BasicSocket#recvmsg attach the array when
|
|
|
|
* receives a SCM_RIGHTS control message and :scm_rights=>true option is given.
|
|
|
|
*
|
|
|
|
* # recvmsg needs :scm_rights=>true for unix_rights
|
2009-02-18 21:43:15 +03:00
|
|
|
* s1, s2 = UNIXSocket.pair
|
2009-02-19 15:13:38 +03:00
|
|
|
* p s1 #=> #<UNIXSocket:fd 3>
|
2010-12-24 02:52:42 +03:00
|
|
|
* s1.sendmsg "stdin and a socket", 0, nil, Socket::AncillaryData.unix_rights(STDIN, s1)
|
2009-02-27 17:23:54 +03:00
|
|
|
* _, _, _, ctl = s2.recvmsg(:scm_rights=>true)
|
2010-12-24 02:52:42 +03:00
|
|
|
* p ctl #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 6 7>
|
2009-02-19 15:13:38 +03:00
|
|
|
* p ctl.unix_rights #=> [#<IO:fd 6>, #<Socket:fd 7>]
|
|
|
|
* p File.identical?(STDIN, ctl.unix_rights[0]) #=> true
|
|
|
|
* p File.identical?(s1, ctl.unix_rights[1]) #=> true
|
2009-02-18 21:43:15 +03:00
|
|
|
*
|
2009-02-27 17:23:54 +03:00
|
|
|
* # If :scm_rights=>true is not given, unix_rights returns nil
|
|
|
|
* s1, s2 = UNIXSocket.pair
|
2010-12-24 02:52:42 +03:00
|
|
|
* s1.sendmsg "stdin and a socket", 0, nil, Socket::AncillaryData.unix_rights(STDIN, s1)
|
2009-02-27 17:23:54 +03:00
|
|
|
* _, _, _, ctl = s2.recvmsg
|
|
|
|
* p ctl #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 6 7>
|
|
|
|
* p ctl.unix_rights #=> nil
|
|
|
|
*
|
2009-02-18 21:43:15 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-19 15:13:38 +03:00
|
|
|
ancillary_unix_rights(VALUE self)
|
2009-02-18 21:43:15 +03:00
|
|
|
{
|
2009-02-28 12:29:26 +03:00
|
|
|
int level, type;
|
|
|
|
|
|
|
|
level = ancillary_level(self);
|
|
|
|
type = ancillary_type(self);
|
|
|
|
|
|
|
|
if (level != SOL_SOCKET || type != SCM_RIGHTS)
|
|
|
|
rb_raise(rb_eTypeError, "SCM_RIGHTS ancillary data expected");
|
|
|
|
|
2010-05-21 13:10:23 +04:00
|
|
|
return rb_attr_get(self, rb_intern("unix_rights"));
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-25 17:24:10 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_unix_rights rb_f_notimplement
|
2009-02-25 17:24:10 +03:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(SCM_TIMESTAMP) || defined(SCM_TIMESTAMPNS) || defined(SCM_BINTIME)
|
2009-02-24 13:45:46 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.timestamp => time
|
|
|
|
*
|
|
|
|
* returns the timestamp as a time object.
|
|
|
|
*
|
|
|
|
* _ancillarydata_ should be one of following type:
|
2013-09-09 16:33:36 +04:00
|
|
|
* - SOL_SOCKET/SCM_TIMESTAMP (microsecond) GNU/Linux, FreeBSD, NetBSD, OpenBSD, Solaris, MacOS X
|
|
|
|
* - SOL_SOCKET/SCM_TIMESTAMPNS (nanosecond) GNU/Linux
|
2009-02-24 13:56:57 +03:00
|
|
|
* - SOL_SOCKET/SCM_BINTIME (2**(-64) second) FreeBSD
|
|
|
|
*
|
2009-02-24 13:45:46 +03:00
|
|
|
* Addrinfo.udp("127.0.0.1", 0).bind {|s1|
|
|
|
|
* Addrinfo.udp("127.0.0.1", 0).bind {|s2|
|
|
|
|
* s1.setsockopt(:SOCKET, :TIMESTAMP, true)
|
|
|
|
* s2.send "a", 0, s1.local_address
|
|
|
|
* ctl = s1.recvmsg.last
|
|
|
|
* p ctl #=> #<Socket::AncillaryData: INET SOCKET TIMESTAMP 2009-02-24 17:35:46.775581>
|
|
|
|
* t = ctl.timestamp
|
|
|
|
* p t #=> 2009-02-24 17:35:46 +0900
|
|
|
|
* p t.usec #=> 775581
|
|
|
|
* p t.nsec #=> 775581000
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_timestamp(VALUE self)
|
|
|
|
{
|
|
|
|
int level, type;
|
|
|
|
VALUE data;
|
|
|
|
VALUE result = Qnil;
|
|
|
|
|
|
|
|
level = ancillary_level(self);
|
|
|
|
type = ancillary_type(self);
|
|
|
|
data = ancillary_data(self);
|
|
|
|
|
2009-02-25 17:24:10 +03:00
|
|
|
# ifdef SCM_TIMESTAMP
|
2009-02-24 13:45:46 +03:00
|
|
|
if (level == SOL_SOCKET && type == SCM_TIMESTAMP &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct timeval)) {
|
|
|
|
struct timeval tv;
|
|
|
|
memcpy((char*)&tv, RSTRING_PTR(data), sizeof(tv));
|
|
|
|
result = rb_time_new(tv.tv_sec, tv.tv_usec);
|
|
|
|
}
|
2009-02-25 17:24:10 +03:00
|
|
|
# endif
|
2009-02-24 13:45:46 +03:00
|
|
|
|
2009-02-25 17:24:10 +03:00
|
|
|
# ifdef SCM_TIMESTAMPNS
|
2009-02-24 13:45:46 +03:00
|
|
|
if (level == SOL_SOCKET && type == SCM_TIMESTAMPNS &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct timespec)) {
|
|
|
|
struct timespec ts;
|
|
|
|
memcpy((char*)&ts, RSTRING_PTR(data), sizeof(ts));
|
|
|
|
result = rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
|
|
|
|
}
|
2009-02-25 17:24:10 +03:00
|
|
|
# endif
|
2009-02-24 13:45:46 +03:00
|
|
|
|
2009-07-01 16:11:53 +04:00
|
|
|
#define add(x,y) (rb_funcall((x), '+', 1, (y)))
|
|
|
|
#define mul(x,y) (rb_funcall((x), '*', 1, (y)))
|
|
|
|
#define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
|
|
|
|
|
2009-02-25 17:24:10 +03:00
|
|
|
# ifdef SCM_BINTIME
|
2009-02-24 13:56:57 +03:00
|
|
|
if (level == SOL_SOCKET && type == SCM_BINTIME &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct bintime)) {
|
|
|
|
struct bintime bt;
|
2009-07-01 16:11:53 +04:00
|
|
|
VALUE d, timev;
|
2009-02-24 13:56:57 +03:00
|
|
|
memcpy((char*)&bt, RSTRING_PTR(data), sizeof(bt));
|
2010-08-19 14:54:56 +04:00
|
|
|
d = ULL2NUM(0x100000000ULL);
|
2009-07-01 16:11:53 +04:00
|
|
|
d = mul(d,d);
|
|
|
|
timev = add(TIMET2NUM(bt.sec), quo(ULL2NUM(bt.frac), d));
|
|
|
|
result = rb_time_num_new(timev, Qnil);
|
2009-02-24 13:56:57 +03:00
|
|
|
}
|
2009-02-25 17:24:10 +03:00
|
|
|
# endif
|
2009-02-24 13:56:57 +03:00
|
|
|
|
2009-02-24 13:45:46 +03:00
|
|
|
if (result == Qnil)
|
|
|
|
rb_raise(rb_eTypeError, "timestamp ancillary data expected");
|
|
|
|
|
|
|
|
return result;
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-25 17:24:10 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_timestamp rb_f_notimplement
|
2009-02-25 17:24:10 +03:00
|
|
|
#endif
|
2009-02-24 13:45:46 +03:00
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-09 13:49:16 +03:00
|
|
|
* Socket::AncillaryData.int(family, cmsg_level, cmsg_type, integer) => ancillarydata
|
2009-02-02 05:51:02 +03:00
|
|
|
*
|
|
|
|
* Creates a new Socket::AncillaryData object which contains a int as data.
|
|
|
|
*
|
2010-04-22 12:04:13 +04:00
|
|
|
* The size and endian is dependent on the host.
|
2009-02-02 05:51:02 +03:00
|
|
|
*
|
2009-02-09 13:49:16 +03:00
|
|
|
* p Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, STDERR.fileno)
|
2009-02-10 03:18:53 +03:00
|
|
|
* #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 2>
|
2009-02-02 05:51:02 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-09 13:49:16 +03:00
|
|
|
ancillary_s_int(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE vtype, VALUE integer)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
2009-03-01 09:30:41 +03:00
|
|
|
int family = rsock_family_arg(vfamily);
|
|
|
|
int level = rsock_level_arg(family, vlevel);
|
|
|
|
int type = rsock_cmsg_type_arg(family, level, vtype);
|
2009-02-02 05:51:02 +03:00
|
|
|
int i = NUM2INT(integer);
|
2009-02-09 13:49:16 +03:00
|
|
|
return ancdata_new(family, level, type, rb_str_new((char*)&i, sizeof(i)));
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.int => integer
|
|
|
|
*
|
|
|
|
* Returns the data in _ancillarydata_ as an int.
|
|
|
|
*
|
2010-04-22 12:04:13 +04:00
|
|
|
* The size and endian is dependent on the host.
|
2009-02-02 05:51:02 +03:00
|
|
|
*
|
2009-02-09 13:49:16 +03:00
|
|
|
* ancdata = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, STDERR.fileno)
|
2009-02-09 15:49:24 +03:00
|
|
|
* p ancdata.int #=> 2
|
2009-02-02 05:51:02 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_int(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE data;
|
|
|
|
int i;
|
|
|
|
data = ancillary_data(self);
|
|
|
|
if (RSTRING_LEN(data) != sizeof(int))
|
|
|
|
rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld", (int)sizeof(int), (long)RSTRING_LEN(data));
|
|
|
|
memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
|
|
|
|
return INT2NUM(i);
|
|
|
|
}
|
|
|
|
|
2009-12-31 00:01:13 +03:00
|
|
|
#if defined(IPPROTO_IP) && defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
|
2009-02-11 06:56:13 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-11 10:44:53 +03:00
|
|
|
* Socket::AncillaryData.ip_pktinfo(addr, ifindex) => ancdata
|
2009-02-11 06:56:13 +03:00
|
|
|
* Socket::AncillaryData.ip_pktinfo(addr, ifindex, spec_dst) => ancdata
|
|
|
|
*
|
|
|
|
* Returns new ancillary data for IP_PKTINFO.
|
|
|
|
*
|
2009-02-11 10:44:53 +03:00
|
|
|
* If spec_dst is not given, addr is used.
|
|
|
|
*
|
2009-02-11 06:56:13 +03:00
|
|
|
* IP_PKTINFO is not standard.
|
|
|
|
*
|
|
|
|
* Supported platform: GNU/Linux
|
|
|
|
*
|
|
|
|
* addr = Addrinfo.ip("127.0.0.1")
|
|
|
|
* ifindex = 0
|
|
|
|
* spec_dst = Addrinfo.ip("127.0.0.1")
|
|
|
|
* p Socket::AncillaryData.ip_pktinfo(addr, ifindex, spec_dst)
|
|
|
|
* #=> #<Socket::AncillaryData: INET IP PKTINFO 127.0.0.1 ifindex:0 spec_dst:127.0.0.1>
|
|
|
|
*
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
2009-02-11 10:44:53 +03:00
|
|
|
ancillary_s_ip_pktinfo(int argc, VALUE *argv, VALUE self)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
2009-02-11 10:44:53 +03:00
|
|
|
VALUE v_addr, v_ifindex, v_spec_dst;
|
2009-02-02 05:51:02 +03:00
|
|
|
unsigned int ifindex;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
struct in_pktinfo pktinfo;
|
|
|
|
|
2009-02-11 10:44:53 +03:00
|
|
|
rb_scan_args(argc, argv, "21", &v_addr, &v_ifindex, &v_spec_dst);
|
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
SockAddrStringValue(v_addr);
|
|
|
|
ifindex = NUM2UINT(v_ifindex);
|
2009-02-11 10:44:53 +03:00
|
|
|
if (NIL_P(v_spec_dst))
|
|
|
|
v_spec_dst = v_addr;
|
|
|
|
else
|
|
|
|
SockAddrStringValue(v_spec_dst);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
memset(&pktinfo, 0, sizeof(pktinfo));
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
if (RSTRING_LEN(v_addr) != sizeof(sa))
|
|
|
|
rb_raise(rb_eArgError, "addr size different to AF_INET sockaddr");
|
|
|
|
memcpy(&sa, RSTRING_PTR(v_addr), sizeof(sa));
|
|
|
|
if (sa.sin_family != AF_INET)
|
|
|
|
rb_raise(rb_eArgError, "addr is not AF_INET sockaddr");
|
|
|
|
memcpy(&pktinfo.ipi_addr, &sa.sin_addr, sizeof(pktinfo.ipi_addr));
|
|
|
|
|
|
|
|
pktinfo.ipi_ifindex = ifindex;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
if (RSTRING_LEN(v_spec_dst) != sizeof(sa))
|
|
|
|
rb_raise(rb_eArgError, "spec_dat size different to AF_INET sockaddr");
|
|
|
|
memcpy(&sa, RSTRING_PTR(v_spec_dst), sizeof(sa));
|
|
|
|
if (sa.sin_family != AF_INET)
|
|
|
|
rb_raise(rb_eArgError, "spec_dst is not AF_INET sockaddr");
|
|
|
|
memcpy(&pktinfo.ipi_spec_dst, &sa.sin_addr, sizeof(pktinfo.ipi_spec_dst));
|
|
|
|
|
2009-02-09 13:49:16 +03:00
|
|
|
return ancdata_new(AF_INET, IPPROTO_IP, IP_PKTINFO, rb_str_new((char *)&pktinfo, sizeof(pktinfo)));
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_s_ip_pktinfo rb_f_notimplement
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
2009-12-31 00:01:13 +03:00
|
|
|
#if defined(IPPROTO_IP) && defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
|
2009-02-11 06:56:13 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancdata.ip_pktinfo => [addr, ifindex, spec_dst]
|
|
|
|
*
|
|
|
|
* Extracts addr, ifindex and spec_dst from IP_PKTINFO ancillary data.
|
|
|
|
*
|
|
|
|
* IP_PKTINFO is not standard.
|
|
|
|
*
|
|
|
|
* Supported platform: GNU/Linux
|
|
|
|
*
|
|
|
|
* addr = Addrinfo.ip("127.0.0.1")
|
|
|
|
* ifindex = 0
|
|
|
|
* spec_dest = Addrinfo.ip("127.0.0.1")
|
|
|
|
* ancdata = Socket::AncillaryData.ip_pktinfo(addr, ifindex, spec_dest)
|
|
|
|
* p ancdata.ip_pktinfo
|
|
|
|
* #=> [#<Addrinfo: 127.0.0.1>, 0, #<Addrinfo: 127.0.0.1>]
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
|
|
|
ancillary_ip_pktinfo(VALUE self)
|
|
|
|
{
|
|
|
|
int level, type;
|
|
|
|
VALUE data;
|
|
|
|
struct in_pktinfo pktinfo;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
VALUE v_spec_dst, v_addr;
|
|
|
|
|
|
|
|
level = ancillary_level(self);
|
|
|
|
type = ancillary_type(self);
|
|
|
|
data = ancillary_data(self);
|
|
|
|
|
|
|
|
if (level != IPPROTO_IP || type != IP_PKTINFO ||
|
|
|
|
RSTRING_LEN(data) != sizeof(struct in_pktinfo)) {
|
|
|
|
rb_raise(rb_eTypeError, "IP_PKTINFO ancillary data expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&pktinfo, RSTRING_PTR(data), sizeof(struct in_pktinfo));
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
memcpy(&sa.sin_addr, &pktinfo.ipi_addr, sizeof(sa.sin_addr));
|
2009-03-01 09:30:41 +03:00
|
|
|
v_addr = rsock_addrinfo_new((struct sockaddr *)&sa, sizeof(sa), PF_INET, 0, 0, Qnil, Qnil);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
memcpy(&sa.sin_addr, &pktinfo.ipi_spec_dst, sizeof(sa.sin_addr));
|
2009-03-01 09:30:41 +03:00
|
|
|
v_spec_dst = rsock_addrinfo_new((struct sockaddr *)&sa, sizeof(sa), PF_INET, 0, 0, Qnil, Qnil);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
return rb_ary_new3(3, v_addr, UINT2NUM(pktinfo.ipi_ifindex), v_spec_dst);
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_ip_pktinfo rb_f_notimplement
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
|
2009-02-11 06:56:13 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Socket::AncillaryData.ipv6_pktinfo(addr, ifindex) => ancdata
|
|
|
|
*
|
|
|
|
* Returns new ancillary data for IPV6_PKTINFO.
|
|
|
|
*
|
|
|
|
* IPV6_PKTINFO is defined by RFC 3542.
|
|
|
|
*
|
|
|
|
* addr = Addrinfo.ip("::1")
|
|
|
|
* ifindex = 0
|
|
|
|
* p Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
|
|
|
|
* #=> #<Socket::AncillaryData: INET6 IPV6 PKTINFO ::1 ifindex:0>
|
|
|
|
*
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
|
|
|
ancillary_s_ipv6_pktinfo(VALUE self, VALUE v_addr, VALUE v_ifindex)
|
|
|
|
{
|
|
|
|
unsigned int ifindex;
|
|
|
|
struct sockaddr_in6 sa;
|
|
|
|
struct in6_pktinfo pktinfo;
|
|
|
|
|
|
|
|
SockAddrStringValue(v_addr);
|
|
|
|
ifindex = NUM2UINT(v_ifindex);
|
|
|
|
|
|
|
|
memset(&pktinfo, 0, sizeof(pktinfo));
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
if (RSTRING_LEN(v_addr) != sizeof(sa))
|
|
|
|
rb_raise(rb_eArgError, "addr size different to AF_INET6 sockaddr");
|
|
|
|
memcpy(&sa, RSTRING_PTR(v_addr), sizeof(sa));
|
|
|
|
if (sa.sin6_family != AF_INET6)
|
|
|
|
rb_raise(rb_eArgError, "addr is not AF_INET6 sockaddr");
|
|
|
|
memcpy(&pktinfo.ipi6_addr, &sa.sin6_addr, sizeof(pktinfo.ipi6_addr));
|
|
|
|
|
|
|
|
pktinfo.ipi6_ifindex = ifindex;
|
|
|
|
|
2009-02-09 13:49:16 +03:00
|
|
|
return ancdata_new(AF_INET6, IPPROTO_IPV6, IPV6_PKTINFO, rb_str_new((char *)&pktinfo, sizeof(pktinfo)));
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_s_ipv6_pktinfo rb_f_notimplement
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
|
|
|
|
static void
|
|
|
|
extract_ipv6_pktinfo(VALUE self, struct in6_pktinfo *pktinfo_ptr, struct sockaddr_in6 *sa_ptr)
|
|
|
|
{
|
|
|
|
int level, type;
|
|
|
|
VALUE data;
|
|
|
|
|
|
|
|
level = ancillary_level(self);
|
|
|
|
type = ancillary_type(self);
|
|
|
|
data = ancillary_data(self);
|
|
|
|
|
|
|
|
if (level != IPPROTO_IPV6 || type != IPV6_PKTINFO ||
|
|
|
|
RSTRING_LEN(data) != sizeof(struct in6_pktinfo)) {
|
|
|
|
rb_raise(rb_eTypeError, "IPV6_PKTINFO ancillary data expected");
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(pktinfo_ptr, RSTRING_PTR(data), sizeof(*pktinfo_ptr));
|
|
|
|
|
2013-02-15 03:14:22 +04:00
|
|
|
INIT_SOCKADDR((struct sockaddr *)sa_ptr, AF_INET6, sizeof(*sa_ptr));
|
2009-02-02 05:51:02 +03:00
|
|
|
memcpy(&sa_ptr->sin6_addr, &pktinfo_ptr->ipi6_addr, sizeof(sa_ptr->sin6_addr));
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&sa_ptr->sin6_addr))
|
|
|
|
sa_ptr->sin6_scope_id = pktinfo_ptr->ipi6_ifindex;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
|
2009-02-11 06:56:13 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancdata.ipv6_pktinfo => [addr, ifindex]
|
|
|
|
*
|
|
|
|
* Extracts addr and ifindex from IPV6_PKTINFO ancillary data.
|
|
|
|
*
|
|
|
|
* IPV6_PKTINFO is defined by RFC 3542.
|
|
|
|
*
|
2010-04-22 12:04:13 +04:00
|
|
|
* addr = Addrinfo.ip("::1")
|
2009-02-11 06:56:13 +03:00
|
|
|
* ifindex = 0
|
|
|
|
* ancdata = Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
|
|
|
|
* p ancdata.ipv6_pktinfo #=> [#<Addrinfo: ::1>, 0]
|
|
|
|
*
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
|
|
|
ancillary_ipv6_pktinfo(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_pktinfo pktinfo;
|
|
|
|
struct sockaddr_in6 sa;
|
|
|
|
VALUE v_addr;
|
|
|
|
|
|
|
|
extract_ipv6_pktinfo(self, &pktinfo, &sa);
|
2010-04-28 12:14:13 +04:00
|
|
|
v_addr = rsock_addrinfo_new((struct sockaddr *)&sa, (socklen_t)sizeof(sa), PF_INET6, 0, 0, Qnil, Qnil);
|
2009-02-02 05:51:02 +03:00
|
|
|
return rb_ary_new3(2, v_addr, UINT2NUM(pktinfo.ipi6_ifindex));
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_ipv6_pktinfo rb_f_notimplement
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
|
2009-02-11 06:56:13 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancdata.ipv6_pktinfo_addr => addr
|
|
|
|
*
|
|
|
|
* Extracts addr from IPV6_PKTINFO ancillary data.
|
|
|
|
*
|
|
|
|
* IPV6_PKTINFO is defined by RFC 3542.
|
|
|
|
*
|
|
|
|
* addr = Addrinfo.ip("::1")
|
|
|
|
* ifindex = 0
|
|
|
|
* ancdata = Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
|
|
|
|
* p ancdata.ipv6_pktinfo_addr #=> #<Addrinfo: ::1>
|
|
|
|
*
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
|
|
|
ancillary_ipv6_pktinfo_addr(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_pktinfo pktinfo;
|
|
|
|
struct sockaddr_in6 sa;
|
|
|
|
extract_ipv6_pktinfo(self, &pktinfo, &sa);
|
2010-04-28 12:14:13 +04:00
|
|
|
return rsock_addrinfo_new((struct sockaddr *)&sa, (socklen_t)sizeof(sa), PF_INET6, 0, 0, Qnil, Qnil);
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_ipv6_pktinfo_addr rb_f_notimplement
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
|
2009-02-11 06:56:13 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancdata.ipv6_pktinfo_ifindex => addr
|
|
|
|
*
|
|
|
|
* Extracts ifindex from IPV6_PKTINFO ancillary data.
|
|
|
|
*
|
|
|
|
* IPV6_PKTINFO is defined by RFC 3542.
|
|
|
|
*
|
|
|
|
* addr = Addrinfo.ip("::1")
|
|
|
|
* ifindex = 0
|
|
|
|
* ancdata = Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
|
|
|
|
* p ancdata.ipv6_pktinfo_ifindex #=> 0
|
|
|
|
*
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
|
|
|
ancillary_ipv6_pktinfo_ifindex(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_pktinfo pktinfo;
|
|
|
|
struct sockaddr_in6 sa;
|
|
|
|
extract_ipv6_pktinfo(self, &pktinfo, &sa);
|
|
|
|
return UINT2NUM(pktinfo.ipi6_ifindex);
|
2009-04-18 18:10:06 +04:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
2009-04-18 18:10:06 +04:00
|
|
|
#define ancillary_ipv6_pktinfo_ifindex rb_f_notimplement
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SOL_SOCKET) && defined(SCM_RIGHTS) /* 4.4BSD */
|
|
|
|
static int
|
|
|
|
anc_inspect_socket_rights(int level, int type, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (level == SOL_SOCKET && type == SCM_RIGHTS &&
|
|
|
|
0 < RSTRING_LEN(data) && (RSTRING_LEN(data) % sizeof(int) == 0)) {
|
|
|
|
long off;
|
|
|
|
for (off = 0; off < RSTRING_LEN(data); off += sizeof(int)) {
|
|
|
|
int fd;
|
|
|
|
memcpy((char*)&fd, RSTRING_PTR(data)+off, sizeof(int));
|
|
|
|
rb_str_catf(ret, " %d", fd);
|
|
|
|
}
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-08 12:47:00 +03:00
|
|
|
#if defined(SCM_CREDENTIALS) /* GNU/Linux */
|
|
|
|
static int
|
|
|
|
anc_inspect_passcred_credentials(int level, int type, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (level == SOL_SOCKET && type == SCM_CREDENTIALS &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct ucred)) {
|
|
|
|
struct ucred cred;
|
|
|
|
memcpy(&cred, RSTRING_PTR(data), sizeof(struct ucred));
|
|
|
|
rb_str_catf(ret, " pid=%u uid=%u gid=%u", cred.pid, cred.uid, cred.gid);
|
2009-02-08 15:53:55 +03:00
|
|
|
rb_str_cat2(ret, " (ucred)");
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-08 12:47:00 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-08 12:47:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-08 15:53:55 +03:00
|
|
|
#if defined(SCM_CREDS)
|
2009-02-08 14:36:18 +03:00
|
|
|
#define INSPECT_SCM_CREDS
|
|
|
|
static int
|
|
|
|
anc_inspect_socket_creds(int level, int type, VALUE data, VALUE ret)
|
|
|
|
{
|
2009-02-08 15:53:55 +03:00
|
|
|
if (level != SOL_SOCKET && type != SCM_CREDS)
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-08 15:53:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FreeBSD has struct cmsgcred and struct sockcred.
|
|
|
|
* They use both SOL_SOCKET/SCM_CREDS in the ancillary message.
|
|
|
|
* They are not ambiguous from the view of the caller
|
|
|
|
* because struct sockcred is sent if and only if the caller sets LOCAL_CREDS socket option.
|
|
|
|
* But inspect method doesn't know it.
|
|
|
|
* So they are ambiguous from the view of inspect.
|
|
|
|
* This function distinguish them by the size of the ancillary message.
|
|
|
|
* This heuristics works well except when sc_ngroups == CMGROUP_MAX.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(HAVE_TYPE_STRUCT_CMSGCRED) /* FreeBSD */
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct cmsgcred)) {
|
|
|
|
struct cmsgcred cred;
|
|
|
|
memcpy(&cred, RSTRING_PTR(data), sizeof(struct cmsgcred));
|
|
|
|
rb_str_catf(ret, " pid=%u", cred.cmcred_pid);
|
|
|
|
rb_str_catf(ret, " uid=%u", cred.cmcred_uid);
|
|
|
|
rb_str_catf(ret, " euid=%u", cred.cmcred_euid);
|
|
|
|
rb_str_catf(ret, " gid=%u", cred.cmcred_gid);
|
|
|
|
if (cred.cmcred_ngroups) {
|
2009-02-10 06:57:23 +03:00
|
|
|
int i;
|
2009-02-10 14:57:07 +03:00
|
|
|
const char *sep = " groups=";
|
2009-02-08 15:53:55 +03:00
|
|
|
for (i = 0; i < cred.cmcred_ngroups; i++) {
|
|
|
|
rb_str_catf(ret, "%s%u", sep, cred.cmcred_groups[i]);
|
2009-02-08 14:36:18 +03:00
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 15:53:55 +03:00
|
|
|
rb_str_cat2(ret, " (cmsgcred)");
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-08 14:36:18 +03:00
|
|
|
}
|
2009-02-08 15:53:55 +03:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_TYPE_STRUCT_SOCKCRED) /* FreeBSD, NetBSD */
|
2009-03-02 15:27:33 +03:00
|
|
|
if ((size_t)RSTRING_LEN(data) >= SOCKCREDSIZE(0)) {
|
2009-02-08 15:53:55 +03:00
|
|
|
struct sockcred cred0, *cred;
|
|
|
|
memcpy(&cred0, RSTRING_PTR(data), SOCKCREDSIZE(0));
|
2009-03-02 15:27:33 +03:00
|
|
|
if ((size_t)RSTRING_LEN(data) == SOCKCREDSIZE(cred0.sc_ngroups)) {
|
2009-02-08 15:53:55 +03:00
|
|
|
cred = (struct sockcred *)ALLOCA_N(char, SOCKCREDSIZE(cred0.sc_ngroups));
|
|
|
|
memcpy(cred, RSTRING_PTR(data), SOCKCREDSIZE(cred0.sc_ngroups));
|
|
|
|
rb_str_catf(ret, " uid=%u", cred->sc_uid);
|
|
|
|
rb_str_catf(ret, " euid=%u", cred->sc_euid);
|
|
|
|
rb_str_catf(ret, " gid=%u", cred->sc_gid);
|
|
|
|
rb_str_catf(ret, " egid=%u", cred->sc_egid);
|
|
|
|
if (cred0.sc_ngroups) {
|
2009-02-10 06:57:23 +03:00
|
|
|
int i;
|
2009-02-10 14:57:07 +03:00
|
|
|
const char *sep = " groups=";
|
2009-02-08 15:53:55 +03:00
|
|
|
for (i = 0; i < cred0.sc_ngroups; i++) {
|
|
|
|
rb_str_catf(ret, "%s%u", sep, cred->sc_groups[i]);
|
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_str_cat2(ret, " (sockcred)");
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-08 15:53:55 +03:00
|
|
|
}
|
2009-02-08 14:36:18 +03:00
|
|
|
}
|
2009-02-08 15:53:55 +03:00
|
|
|
#endif
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-08 14:36:18 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
#if defined(IPPROTO_IP) && defined(IP_RECVDSTADDR) /* 4.4BSD */
|
|
|
|
static int
|
|
|
|
anc_inspect_ip_recvdstaddr(int level, int type, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (level == IPPROTO_IP && type == IP_RECVDSTADDR &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct in_addr)) {
|
2010-04-22 12:04:13 +04:00
|
|
|
struct in_addr addr;
|
2009-02-02 05:51:02 +03:00
|
|
|
char addrbuf[INET_ADDRSTRLEN];
|
|
|
|
memcpy(&addr, RSTRING_PTR(data), sizeof(addr));
|
2010-04-28 12:14:13 +04:00
|
|
|
if (inet_ntop(AF_INET, &addr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_str_cat2(ret, " invalid-address");
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " %s", addrbuf);
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-31 00:01:13 +03:00
|
|
|
#if defined(IPPROTO_IP) && defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
|
2009-02-02 05:51:02 +03:00
|
|
|
static int
|
|
|
|
anc_inspect_ip_pktinfo(int level, int type, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (level == IPPROTO_IP && type == IP_PKTINFO &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct in_pktinfo)) {
|
|
|
|
struct in_pktinfo pktinfo;
|
|
|
|
char buf[INET_ADDRSTRLEN > IFNAMSIZ ? INET_ADDRSTRLEN : IFNAMSIZ];
|
|
|
|
memcpy(&pktinfo, RSTRING_PTR(data), sizeof(pktinfo));
|
|
|
|
if (inet_ntop(AF_INET, &pktinfo.ipi_addr, buf, sizeof(buf)) == NULL)
|
2009-02-11 06:56:13 +03:00
|
|
|
rb_str_cat2(ret, " invalid-address");
|
2009-02-02 05:51:02 +03:00
|
|
|
else
|
2009-02-11 06:56:13 +03:00
|
|
|
rb_str_catf(ret, " %s", buf);
|
2009-02-02 05:51:02 +03:00
|
|
|
if (if_indextoname(pktinfo.ipi_ifindex, buf) == NULL)
|
|
|
|
rb_str_catf(ret, " ifindex:%d", pktinfo.ipi_ifindex);
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " %s", buf);
|
|
|
|
if (inet_ntop(AF_INET, &pktinfo.ipi_spec_dst, buf, sizeof(buf)) == NULL)
|
|
|
|
rb_str_cat2(ret, " spec_dst:invalid-address");
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " spec_dst:%s", buf);
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-03 01:58:03 +03:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) && defined(HAVE_TYPE_STRUCT_IN6_PKTINFO) /* IPv6 RFC3542 */
|
2009-02-02 05:51:02 +03:00
|
|
|
static int
|
|
|
|
anc_inspect_ipv6_pktinfo(int level, int type, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (level == IPPROTO_IPV6 && type == IPV6_PKTINFO &&
|
|
|
|
RSTRING_LEN(data) == sizeof(struct in6_pktinfo)) {
|
|
|
|
struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)RSTRING_PTR(data);
|
2010-04-22 12:04:13 +04:00
|
|
|
struct in6_addr addr;
|
2009-02-02 05:51:02 +03:00
|
|
|
unsigned int ifindex;
|
|
|
|
char addrbuf[INET6_ADDRSTRLEN], ifbuf[IFNAMSIZ];
|
|
|
|
memcpy(&addr, &pktinfo->ipi6_addr, sizeof(addr));
|
|
|
|
memcpy(&ifindex, &pktinfo->ipi6_ifindex, sizeof(ifindex));
|
2010-04-28 12:14:13 +04:00
|
|
|
if (inet_ntop(AF_INET6, &addr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_str_cat2(ret, " invalid-address");
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " %s", addrbuf);
|
|
|
|
if (if_indextoname(ifindex, ifbuf) == NULL)
|
|
|
|
rb_str_catf(ret, " ifindex:%d", ifindex);
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " %s", ifbuf);
|
2009-02-11 08:10:47 +03:00
|
|
|
return 1;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-02-11 08:10:47 +03:00
|
|
|
return 0;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-23 14:41:38 +03:00
|
|
|
#if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
|
2009-02-22 20:46:51 +03:00
|
|
|
static int
|
|
|
|
inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct timeval)) {
|
2009-02-23 14:21:45 +03:00
|
|
|
struct timeval tv;
|
|
|
|
time_t time;
|
2009-02-22 20:46:51 +03:00
|
|
|
struct tm tm;
|
|
|
|
char buf[32];
|
2009-02-23 14:21:45 +03:00
|
|
|
memcpy((char*)&tv, RSTRING_PTR(data), sizeof(tv));
|
|
|
|
time = tv.tv_sec;
|
|
|
|
tm = *localtime(&time);
|
2009-02-22 20:46:51 +03:00
|
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
|
2009-02-23 14:21:45 +03:00
|
|
|
rb_str_catf(ret, " %s.%06ld", buf, (long)tv.tv_usec);
|
2009-02-22 20:46:51 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-23 14:41:38 +03:00
|
|
|
#if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
|
|
|
|
static int
|
|
|
|
inspect_timespec_as_abstime(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct timespec)) {
|
|
|
|
struct timespec ts;
|
|
|
|
struct tm tm;
|
|
|
|
char buf[32];
|
|
|
|
memcpy((char*)&ts, RSTRING_PTR(data), sizeof(ts));
|
|
|
|
tm = *localtime(&ts.tv_sec);
|
|
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
|
|
|
|
rb_str_catf(ret, " %s.%09ld", buf, (long)ts.tv_nsec);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SCM_BINTIME) /* FreeBSD */
|
2009-02-23 14:32:43 +03:00
|
|
|
static int
|
|
|
|
inspect_bintime_as_abstime(int level, int optname, VALUE data, VALUE ret)
|
|
|
|
{
|
|
|
|
if (RSTRING_LEN(data) == sizeof(struct bintime)) {
|
|
|
|
struct bintime bt;
|
|
|
|
struct tm tm;
|
|
|
|
uint64_t frac_h, frac_l;
|
|
|
|
uint64_t scale_h, scale_l;
|
|
|
|
uint64_t tmp1, tmp2;
|
|
|
|
uint64_t res_h, res_l;
|
|
|
|
char buf[32];
|
|
|
|
memcpy((char*)&bt, RSTRING_PTR(data), sizeof(bt));
|
|
|
|
tm = *localtime(&bt.sec);
|
|
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
|
|
|
|
|
|
|
|
/* res_h = frac * 10**19 / 2**64 */
|
|
|
|
|
|
|
|
frac_h = bt.frac >> 32;
|
|
|
|
frac_l = bt.frac & 0xffffffff;
|
|
|
|
|
|
|
|
scale_h = 0x8ac72304; /* 0x8ac7230489e80000 == 10**19 */
|
|
|
|
scale_l = 0x89e80000;
|
|
|
|
|
|
|
|
res_h = frac_h * scale_h;
|
|
|
|
res_l = frac_l * scale_l;
|
|
|
|
|
|
|
|
tmp1 = frac_h * scale_l;
|
|
|
|
res_h += tmp1 >> 32;
|
|
|
|
tmp2 = res_l;
|
|
|
|
res_l += tmp1 & 0xffffffff;
|
|
|
|
if (res_l < tmp2) res_h++;
|
|
|
|
|
|
|
|
tmp1 = frac_l * scale_h;
|
|
|
|
res_h += tmp1 >> 32;
|
|
|
|
tmp2 = res_l;
|
|
|
|
res_l += tmp1 & 0xffffffff;
|
|
|
|
if (res_l < tmp2) res_h++;
|
|
|
|
|
|
|
|
rb_str_catf(ret, " %s.%019"PRIu64, buf, res_h);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.inspect => string
|
|
|
|
*
|
|
|
|
* returns a string which shows ancillarydata in human-readable form.
|
|
|
|
*
|
2009-02-10 03:18:53 +03:00
|
|
|
* p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").inspect
|
|
|
|
* #=> "#<Socket::AncillaryData: INET6 IPV6 PKTINFO \"\">"
|
2009-02-02 05:51:02 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE ret;
|
2009-02-09 15:49:24 +03:00
|
|
|
int family, level, type;
|
2009-02-02 05:51:02 +03:00
|
|
|
VALUE data;
|
2009-02-11 08:10:47 +03:00
|
|
|
ID family_id, level_id, type_id;
|
2009-02-02 05:51:02 +03:00
|
|
|
VALUE vtype;
|
2009-02-11 08:10:47 +03:00
|
|
|
int inspected;
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-02-09 15:49:24 +03:00
|
|
|
family = ancillary_family(self);
|
2009-02-02 05:51:02 +03:00
|
|
|
level = ancillary_level(self);
|
|
|
|
type = ancillary_type(self);
|
|
|
|
data = ancillary_data(self);
|
|
|
|
|
2009-02-11 08:10:47 +03:00
|
|
|
ret = rb_sprintf("#<%s:", rb_obj_classname(self));
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-03-01 09:30:41 +03:00
|
|
|
family_id = rsock_intern_family_noprefix(family);
|
2009-02-09 15:49:24 +03:00
|
|
|
if (family_id)
|
2009-02-11 08:10:47 +03:00
|
|
|
rb_str_catf(ret, " %s", rb_id2name(family_id));
|
2009-02-09 15:49:24 +03:00
|
|
|
else
|
2009-02-11 08:10:47 +03:00
|
|
|
rb_str_catf(ret, " family:%d", family);
|
2009-02-09 15:49:24 +03:00
|
|
|
|
2009-02-11 08:10:47 +03:00
|
|
|
if (level == SOL_SOCKET) {
|
|
|
|
rb_str_cat2(ret, " SOCKET");
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-03-01 09:30:41 +03:00
|
|
|
type_id = rsock_intern_scm_optname(type);
|
2009-02-11 08:10:47 +03:00
|
|
|
if (type_id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(type_id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " cmsg_type:%d", type);
|
|
|
|
}
|
|
|
|
else if (IS_IP_FAMILY(family)) {
|
2009-03-01 09:30:41 +03:00
|
|
|
level_id = rsock_intern_iplevel(level);
|
2009-02-11 08:10:47 +03:00
|
|
|
if (level_id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(level_id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " cmsg_level:%d", level);
|
|
|
|
|
|
|
|
vtype = ip_cmsg_type_to_sym(level, type);
|
|
|
|
if (SYMBOL_P(vtype))
|
2015-08-15 07:32:15 +03:00
|
|
|
rb_str_catf(ret, " %"PRIsVALUE, rb_sym2str(vtype));
|
2009-02-11 08:10:47 +03:00
|
|
|
else
|
|
|
|
rb_str_catf(ret, " cmsg_type:%d", type);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_catf(ret, " cmsg_level:%d", level);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_str_catf(ret, " cmsg_type:%d", type);
|
2009-02-11 08:10:47 +03:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-02-11 08:10:47 +03:00
|
|
|
inspected = 0;
|
|
|
|
|
|
|
|
if (level == SOL_SOCKET)
|
|
|
|
family = AF_UNSPEC;
|
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
switch (level) {
|
|
|
|
# if defined(SOL_SOCKET)
|
|
|
|
case SOL_SOCKET:
|
|
|
|
switch (type) {
|
2009-02-23 14:21:45 +03:00
|
|
|
# if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
|
2009-02-22 20:55:52 +03:00
|
|
|
case SCM_TIMESTAMP: inspected = inspect_timeval_as_abstime(level, type, data, ret); break;
|
2009-02-22 20:46:51 +03:00
|
|
|
# endif
|
2009-02-23 14:41:38 +03:00
|
|
|
# if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
|
|
|
|
case SCM_TIMESTAMPNS: inspected = inspect_timespec_as_abstime(level, type, data, ret); break;
|
|
|
|
# endif
|
2009-02-23 14:32:43 +03:00
|
|
|
# if defined(SCM_BINTIME) /* FreeBSD */
|
|
|
|
case SCM_BINTIME: inspected = inspect_bintime_as_abstime(level, type, data, ret); break;
|
|
|
|
# endif
|
2009-02-11 08:10:47 +03:00
|
|
|
# if defined(SCM_RIGHTS) /* 4.4BSD */
|
|
|
|
case SCM_RIGHTS: inspected = anc_inspect_socket_rights(level, type, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(SCM_CREDENTIALS) /* GNU/Linux */
|
|
|
|
case SCM_CREDENTIALS: inspected = anc_inspect_passcred_credentials(level, type, data, ret); break;
|
|
|
|
# endif
|
|
|
|
# if defined(INSPECT_SCM_CREDS) /* NetBSD */
|
|
|
|
case SCM_CREDS: inspected = anc_inspect_socket_creds(level, type, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-02 05:51:02 +03:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2009-02-11 08:10:47 +03:00
|
|
|
case AF_INET:
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
#endif
|
|
|
|
switch (level) {
|
|
|
|
# if defined(IPPROTO_IP)
|
|
|
|
case IPPROTO_IP:
|
|
|
|
switch (type) {
|
|
|
|
# if defined(IP_RECVDSTADDR) /* 4.4BSD */
|
|
|
|
case IP_RECVDSTADDR: inspected = anc_inspect_ip_recvdstaddr(level, type, data, ret); break;
|
|
|
|
# endif
|
2009-12-31 00:01:13 +03:00
|
|
|
# if defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
|
2009-02-11 08:10:47 +03:00
|
|
|
case IP_PKTINFO: inspected = anc_inspect_ip_pktinfo(level, type, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-02 05:51:02 +03:00
|
|
|
# endif
|
|
|
|
|
2009-02-11 08:10:47 +03:00
|
|
|
# if defined(IPPROTO_IPV6)
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
switch (type) {
|
2013-05-07 07:14:47 +04:00
|
|
|
# if defined(IPV6_PKTINFO) && defined(HAVE_TYPE_STRUCT_IN6_PKTINFO) /* RFC 3542 */
|
2009-02-11 08:10:47 +03:00
|
|
|
case IPV6_PKTINFO: inspected = anc_inspect_ipv6_pktinfo(level, type, data, ret); break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-02 05:51:02 +03:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
break;
|
2009-02-11 08:10:47 +03:00
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-02-11 08:10:47 +03:00
|
|
|
if (!inspected) {
|
2010-05-04 06:51:46 +04:00
|
|
|
rb_str_cat2(ret, " ");
|
2010-05-04 07:39:36 +04:00
|
|
|
rb_str_append(ret, rb_str_dump(data));
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_str_cat2(ret, ">");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ancillarydata.cmsg_is?(level, type) => true or false
|
|
|
|
*
|
|
|
|
* tests the level and type of _ancillarydata_.
|
|
|
|
*
|
2009-02-09 13:49:16 +03:00
|
|
|
* ancdata = Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "")
|
2009-02-02 05:51:02 +03:00
|
|
|
* ancdata.cmsg_is?(Socket::IPPROTO_IPV6, Socket::IPV6_PKTINFO) #=> true
|
|
|
|
* ancdata.cmsg_is?(:IPV6, :PKTINFO) #=> true
|
|
|
|
* ancdata.cmsg_is?(:IP, :PKTINFO) #=> false
|
|
|
|
* ancdata.cmsg_is?(:SOCKET, :RIGHTS) #=> false
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ancillary_cmsg_is_p(VALUE self, VALUE vlevel, VALUE vtype)
|
|
|
|
{
|
2009-02-09 16:56:43 +03:00
|
|
|
int family = ancillary_family(self);
|
2009-03-01 09:30:41 +03:00
|
|
|
int level = rsock_level_arg(family, vlevel);
|
|
|
|
int type = rsock_cmsg_type_arg(family, level, vtype);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
if (ancillary_level(self) == level &&
|
|
|
|
ancillary_type(self) == type)
|
|
|
|
return Qtrue;
|
|
|
|
else
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_SENDMSG)
|
|
|
|
struct sendmsg_args_struct {
|
|
|
|
int fd;
|
|
|
|
int flags;
|
2014-09-22 04:46:25 +04:00
|
|
|
const struct msghdr *msg;
|
2009-02-02 05:51:02 +03:00
|
|
|
};
|
|
|
|
|
2012-07-10 17:57:11 +04:00
|
|
|
static void *
|
2009-02-02 05:51:02 +03:00
|
|
|
nogvl_sendmsg_func(void *ptr)
|
|
|
|
{
|
|
|
|
struct sendmsg_args_struct *args = ptr;
|
2012-07-28 06:14:23 +04:00
|
|
|
return (void *)(VALUE)sendmsg(args->fd, args->msg, args->flags);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
rb_sendmsg(int fd, const struct msghdr *msg, int flags)
|
|
|
|
{
|
|
|
|
struct sendmsg_args_struct args;
|
|
|
|
args.fd = fd;
|
|
|
|
args.msg = msg;
|
|
|
|
args.flags = flags;
|
2012-07-10 17:57:11 +04:00
|
|
|
return (ssize_t)rb_thread_call_without_gvl(nogvl_sendmsg_func, &args, RUBY_UBF_IO, 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
bsock_sendmsg_internal(VALUE sock, VALUE data, VALUE vflags,
|
|
|
|
VALUE dest_sockaddr, VALUE controls, VALUE ex,
|
|
|
|
int nonblock)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
struct msghdr mh;
|
|
|
|
struct iovec iov;
|
2015-06-05 11:28:14 +03:00
|
|
|
int controls_num;
|
2015-06-05 03:18:30 +03:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2015-05-30 11:19:37 +03:00
|
|
|
VALUE controls_str = 0;
|
2013-05-01 08:43:00 +04:00
|
|
|
int family;
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
int flags;
|
|
|
|
ssize_t ss;
|
|
|
|
|
2009-02-09 16:56:43 +03:00
|
|
|
GetOpenFile(sock, fptr);
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2015-07-02 04:58:14 +03:00
|
|
|
family = rsock_getfamily(fptr);
|
2013-05-01 08:43:00 +04:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
StringValue(data);
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
|
|
|
|
if (!RB_TYPE_P(controls, T_ARRAY)) {
|
|
|
|
controls = rb_ary_new();
|
|
|
|
}
|
2015-06-05 11:28:14 +03:00
|
|
|
controls_num = RARRAY_LENINT(controls);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2015-06-05 11:28:14 +03:00
|
|
|
if (controls_num) {
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
int i;
|
2010-04-28 12:14:13 +04:00
|
|
|
size_t last_pad = 0;
|
2015-06-04 23:50:08 +03:00
|
|
|
const VALUE *controls_ptr = RARRAY_CONST_PTR(controls);
|
2012-10-22 04:47:50 +04:00
|
|
|
#if defined(__NetBSD__)
|
2009-02-17 13:40:20 +03:00
|
|
|
int last_level = 0;
|
|
|
|
int last_type = 0;
|
2012-10-22 04:47:50 +04:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
controls_str = rb_str_tmp_new(0);
|
|
|
|
for (i = 0; i < controls_num; i++) {
|
|
|
|
VALUE elt = controls_ptr[i], v;
|
|
|
|
VALUE vlevel, vtype;
|
|
|
|
int level, type;
|
|
|
|
VALUE cdata;
|
|
|
|
long oldlen;
|
2009-02-18 15:35:22 +03:00
|
|
|
struct cmsghdr cmh;
|
|
|
|
char *cmsg;
|
2009-02-02 05:51:02 +03:00
|
|
|
size_t cspace;
|
|
|
|
v = rb_check_convert_type(elt, T_ARRAY, "Array", "to_ary");
|
|
|
|
if (!NIL_P(v)) {
|
|
|
|
elt = v;
|
|
|
|
if (RARRAY_LEN(elt) != 3)
|
|
|
|
rb_raise(rb_eArgError, "an element of controls should be 3-elements array");
|
|
|
|
vlevel = rb_ary_entry(elt, 0);
|
|
|
|
vtype = rb_ary_entry(elt, 1);
|
|
|
|
cdata = rb_ary_entry(elt, 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vlevel = rb_funcall(elt, rb_intern("level"), 0);
|
|
|
|
vtype = rb_funcall(elt, rb_intern("type"), 0);
|
|
|
|
cdata = rb_funcall(elt, rb_intern("data"), 0);
|
|
|
|
}
|
2009-03-01 09:30:41 +03:00
|
|
|
level = rsock_level_arg(family, vlevel);
|
|
|
|
type = rsock_cmsg_type_arg(family, level, vtype);
|
2009-02-02 05:51:02 +03:00
|
|
|
StringValue(cdata);
|
|
|
|
oldlen = RSTRING_LEN(controls_str);
|
|
|
|
cspace = CMSG_SPACE(RSTRING_LEN(cdata));
|
|
|
|
rb_str_resize(controls_str, oldlen + cspace);
|
2009-02-18 15:35:22 +03:00
|
|
|
cmsg = RSTRING_PTR(controls_str)+oldlen;
|
|
|
|
memset((char *)cmsg, 0, cspace);
|
|
|
|
memset((char *)&cmh, 0, sizeof(cmh));
|
|
|
|
cmh.cmsg_level = level;
|
|
|
|
cmh.cmsg_type = type;
|
2010-04-28 12:14:13 +04:00
|
|
|
cmh.cmsg_len = (socklen_t)CMSG_LEN(RSTRING_LEN(cdata));
|
2009-02-18 15:35:22 +03:00
|
|
|
MEMCPY(cmsg, &cmh, char, sizeof(cmh));
|
|
|
|
MEMCPY(cmsg+((char*)CMSG_DATA(&cmh)-(char*)&cmh), RSTRING_PTR(cdata), char, RSTRING_LEN(cdata));
|
2012-10-22 04:47:50 +04:00
|
|
|
#if defined(__NetBSD__)
|
2009-02-18 15:35:22 +03:00
|
|
|
last_level = cmh.cmsg_level;
|
|
|
|
last_type = cmh.cmsg_type;
|
2012-10-22 04:47:50 +04:00
|
|
|
#endif
|
2009-02-18 15:35:22 +03:00
|
|
|
last_pad = cspace - cmh.cmsg_len;
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
if (last_pad) {
|
2009-02-17 13:40:20 +03:00
|
|
|
/*
|
|
|
|
* This code removes the last padding from msg_controllen.
|
2010-04-22 12:04:13 +04:00
|
|
|
*
|
2009-02-17 13:40:20 +03:00
|
|
|
* 4.3BSD-Reno reject the padding for SCM_RIGHTS. (There was no 64bit environments in those days?)
|
|
|
|
* RFC 2292 require the padding.
|
|
|
|
* RFC 3542 relaxes the condition - implementation must accept both as valid.
|
2010-04-22 12:04:13 +04:00
|
|
|
*
|
2009-02-17 13:40:20 +03:00
|
|
|
* Actual problems:
|
|
|
|
*
|
|
|
|
* - NetBSD 4.0.1
|
|
|
|
* SCM_RIGHTS with padding causes EINVAL
|
|
|
|
* IPV6_PKTINFO without padding causes "page fault trap"
|
2009-10-07 17:35:07 +04:00
|
|
|
* http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=40661
|
2010-04-22 12:04:13 +04:00
|
|
|
*
|
2009-02-17 13:40:20 +03:00
|
|
|
* - OpenBSD 4.4
|
|
|
|
* IPV6_PKTINFO without padding causes EINVAL
|
|
|
|
*
|
|
|
|
* Basically, msg_controllen should contains the padding.
|
|
|
|
* So the padding is removed only if a problem really exists.
|
|
|
|
*/
|
2009-02-17 17:26:34 +03:00
|
|
|
#if defined(__NetBSD__)
|
2009-02-17 13:40:20 +03:00
|
|
|
if (last_level == SOL_SOCKET && last_type == SCM_RIGHTS)
|
|
|
|
rb_str_set_len(controls_str, RSTRING_LEN(controls_str)-last_pad);
|
2009-02-17 17:26:34 +03:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
2015-06-04 23:50:08 +03:00
|
|
|
RB_GC_GUARD(controls);
|
2009-02-02 05:51:02 +03:00
|
|
|
#else
|
|
|
|
rb_raise(rb_eNotImpError, "control message for sendmsg is unimplemented");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = NIL_P(vflags) ? 0 : NUM2INT(vflags);
|
|
|
|
#ifdef MSG_DONTWAIT
|
|
|
|
if (nonblock)
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!NIL_P(dest_sockaddr))
|
|
|
|
SockAddrStringValue(dest_sockaddr);
|
|
|
|
|
2009-02-09 16:56:43 +03:00
|
|
|
rb_io_check_closed(fptr);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
retry:
|
|
|
|
memset(&mh, 0, sizeof(mh));
|
|
|
|
if (!NIL_P(dest_sockaddr)) {
|
|
|
|
mh.msg_name = RSTRING_PTR(dest_sockaddr);
|
2013-04-06 11:58:51 +04:00
|
|
|
mh.msg_namelen = RSTRING_SOCKLEN(dest_sockaddr);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
mh.msg_iovlen = 1;
|
|
|
|
mh.msg_iov = &iov;
|
|
|
|
iov.iov_base = RSTRING_PTR(data);
|
|
|
|
iov.iov_len = RSTRING_LEN(data);
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
if (controls_str) {
|
|
|
|
mh.msg_control = RSTRING_PTR(controls_str);
|
2013-04-06 11:58:51 +04:00
|
|
|
mh.msg_controllen = RSTRING_SOCKLEN(controls_str);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_io_check_closed(fptr);
|
2015-05-29 05:24:18 +03:00
|
|
|
if (nonblock && !MSG_DONTWAIT_RELIABLE)
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_io_set_nonblock(fptr);
|
|
|
|
|
|
|
|
ss = rb_sendmsg(fptr->fd, &mh, flags);
|
|
|
|
|
2009-02-21 18:57:52 +03:00
|
|
|
if (ss == -1) {
|
2014-02-21 04:55:13 +04:00
|
|
|
if (!nonblock && rb_io_wait_writable(fptr->fd)) {
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
goto retry;
|
|
|
|
}
|
2015-06-15 22:38:49 +03:00
|
|
|
if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
if (ex == Qfalse) {
|
2015-06-15 22:38:49 +03:00
|
|
|
return sym_wait_writable;
|
|
|
|
}
|
|
|
|
rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE,
|
|
|
|
"sendmsg(2) would block");
|
|
|
|
}
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_sys_fail("sendmsg(2)");
|
2009-02-21 18:57:52 +03:00
|
|
|
}
|
2015-05-30 11:19:37 +03:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
|
|
|
RB_GC_GUARD(controls_str);
|
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
return SSIZET2NUM(ss);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(HAVE_SENDMSG)
|
2010-03-21 13:50:52 +03:00
|
|
|
VALUE
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
rsock_bsock_sendmsg(VALUE sock, VALUE data, VALUE flags, VALUE dest_sockaddr,
|
|
|
|
VALUE controls)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
return bsock_sendmsg_internal(sock, data, flags, dest_sockaddr, controls,
|
|
|
|
Qtrue, 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
2009-04-18 18:10:06 +04:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(HAVE_SENDMSG)
|
2010-03-21 13:50:52 +03:00
|
|
|
VALUE
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
rsock_bsock_sendmsg_nonblock(VALUE sock, VALUE data, VALUE flags,
|
|
|
|
VALUE dest_sockaddr, VALUE controls, VALUE ex)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
socket: avoid arg parsing in bsock_sendmsg_internal
* ext/socket/ancdata.c (bsock_sendmsg_internal): avoid arg parsing
[ruby-core:71439] [Feature #11339]
(rsock_bsock_sendmsg): make private, adjust for above
(rsock_bsock_sendmsg_nonblock): ditto
* ext/socket/rubysocket.h: adjust prototypes
(rsock_opt_false_p): remove
* ext/socket/basicsocket.c (rsock_init_basicsocket):
define private methods
* ext/socket/lib/socket.rb (BasicSocket#sendmsg): new wrapper
(BasicSocket#sendmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
sendmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg_nonblock(msg, exception: false)
r.recv(1, 0, buf)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["sendmsg_nonblock",
[[1.875997293740511,
1.8452614955604076,
1.8449317328631878,
1.8418389447033405,
1.869386937469244],
[1.5175109766423702,
1.4987873211503029,
1.4989623799920082,
1.47918451577425,
1.5017359890043736]]]]
Elapsed time: 16.775453245 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
sendmsg_nonblock 1.842 1.479
Speedup ratio: compare with the result of `a' (greater is better)
name b
sendmsg_nonblock 1.245
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 04:05:30 +03:00
|
|
|
return bsock_sendmsg_internal(sock, data, flags, dest_sockaddr,
|
|
|
|
controls, ex, 1);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
2009-04-18 18:10:06 +04:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
#if defined(HAVE_RECVMSG)
|
|
|
|
struct recvmsg_args_struct {
|
|
|
|
int fd;
|
|
|
|
int flags;
|
2014-09-22 04:46:25 +04:00
|
|
|
struct msghdr *msg;
|
2009-02-02 05:51:02 +03:00
|
|
|
};
|
|
|
|
|
2011-11-06 09:51:43 +04:00
|
|
|
ssize_t
|
|
|
|
rsock_recvmsg(int socket, struct msghdr *message, int flags)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
2013-02-16 15:44:42 +04:00
|
|
|
ssize_t ret;
|
|
|
|
socklen_t len0;
|
2011-11-01 02:32:49 +04:00
|
|
|
#ifdef MSG_CMSG_CLOEXEC
|
|
|
|
/* MSG_CMSG_CLOEXEC is available since Linux 2.6.23. Linux 2.6.18 silently ignore it. */
|
|
|
|
flags |= MSG_CMSG_CLOEXEC;
|
|
|
|
#endif
|
2013-02-16 15:44:42 +04:00
|
|
|
len0 = message->msg_namelen;
|
|
|
|
ret = recvmsg(socket, message, flags);
|
|
|
|
if (ret != -1 && len0 < message->msg_namelen)
|
|
|
|
message->msg_namelen = len0;
|
|
|
|
return ret;
|
2011-11-06 09:51:43 +04:00
|
|
|
}
|
|
|
|
|
2012-07-10 17:57:11 +04:00
|
|
|
static void *
|
2011-11-06 09:51:43 +04:00
|
|
|
nogvl_recvmsg_func(void *ptr)
|
|
|
|
{
|
|
|
|
struct recvmsg_args_struct *args = ptr;
|
|
|
|
int flags = args->flags;
|
2012-07-10 17:57:11 +04:00
|
|
|
return (void *)rsock_recvmsg(args->fd, args->msg, flags);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
rb_recvmsg(int fd, struct msghdr *msg, int flags)
|
|
|
|
{
|
|
|
|
struct recvmsg_args_struct args;
|
|
|
|
args.fd = fd;
|
|
|
|
args.msg = msg;
|
|
|
|
args.flags = flags;
|
2012-07-10 17:57:11 +04:00
|
|
|
return (ssize_t)rb_thread_call_without_gvl(nogvl_recvmsg_func, &args, RUBY_UBF_IO, 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-27 17:23:54 +03:00
|
|
|
static void
|
2011-07-23 12:13:37 +04:00
|
|
|
discard_cmsg(struct cmsghdr *cmh, char *msg_end, int msg_peek_p)
|
2009-02-27 17:23:54 +03:00
|
|
|
{
|
2011-07-24 03:57:50 +04:00
|
|
|
# if !defined(FD_PASSING_WORK_WITH_RECVMSG_MSG_PEEK)
|
2011-11-04 11:19:23 +04:00
|
|
|
/*
|
2011-07-24 03:57:50 +04:00
|
|
|
* FreeBSD 8.2.0, NetBSD 5 and MacOS X Snow Leopard doesn't
|
|
|
|
* allocate fds by recvmsg with MSG_PEEK.
|
|
|
|
* [ruby-dev:44189]
|
2011-12-20 20:09:17 +04:00
|
|
|
* http://bugs.ruby-lang.org/issues/5075
|
2011-07-24 03:57:50 +04:00
|
|
|
*
|
|
|
|
* Linux 2.6.38 allocate fds by recvmsg with MSG_PEEK.
|
2011-07-23 12:13:37 +04:00
|
|
|
*/
|
|
|
|
if (msg_peek_p)
|
|
|
|
return;
|
|
|
|
# endif
|
2009-02-27 17:23:54 +03:00
|
|
|
if (cmh->cmsg_level == SOL_SOCKET && cmh->cmsg_type == SCM_RIGHTS) {
|
|
|
|
int *fdp = (int *)CMSG_DATA(cmh);
|
|
|
|
int *end = (int *)((char *)cmh + cmh->cmsg_len);
|
|
|
|
while ((char *)fdp + sizeof(int) <= (char *)end &&
|
|
|
|
(char *)fdp + sizeof(int) <= msg_end) {
|
2014-01-28 18:37:34 +04:00
|
|
|
rb_update_max_fd(*fdp);
|
2011-07-23 12:13:37 +04:00
|
|
|
close(*fdp);
|
2009-02-27 17:23:54 +03:00
|
|
|
fdp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-02 15:14:35 +03:00
|
|
|
#endif
|
2009-02-27 17:23:54 +03:00
|
|
|
|
2009-02-25 17:03:42 +03:00
|
|
|
void
|
2011-07-23 12:13:37 +04:00
|
|
|
rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p)
|
2009-02-25 17:03:42 +03:00
|
|
|
{
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-18 16:47:53 +03:00
|
|
|
struct cmsghdr *cmh;
|
2009-02-27 17:23:54 +03:00
|
|
|
char *msg_end;
|
2009-02-18 16:47:53 +03:00
|
|
|
|
|
|
|
if (mh->msg_controllen == 0)
|
|
|
|
return;
|
|
|
|
|
2009-02-27 17:23:54 +03:00
|
|
|
msg_end = (char *)mh->msg_control + mh->msg_controllen;
|
|
|
|
|
2009-02-18 16:47:53 +03:00
|
|
|
for (cmh = CMSG_FIRSTHDR(mh); cmh != NULL; cmh = CMSG_NXTHDR(mh, cmh)) {
|
2011-07-23 12:13:37 +04:00
|
|
|
discard_cmsg(cmh, msg_end, msg_peek_p);
|
2009-02-18 16:47:53 +03:00
|
|
|
}
|
2009-02-25 17:03:42 +03:00
|
|
|
#endif
|
2009-03-02 15:14:35 +03:00
|
|
|
}
|
2009-02-18 16:47:53 +03:00
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-18 21:43:15 +03:00
|
|
|
static void
|
2009-02-19 17:01:17 +03:00
|
|
|
make_io_for_unix_rights(VALUE ctl, struct cmsghdr *cmh, char *msg_end)
|
2009-02-18 21:43:15 +03:00
|
|
|
{
|
|
|
|
if (cmh->cmsg_level == SOL_SOCKET && cmh->cmsg_type == SCM_RIGHTS) {
|
2009-02-19 17:01:17 +03:00
|
|
|
int *fdp, *end;
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
rb_ivar_set(ctl, rb_intern("unix_rights"), ary);
|
|
|
|
fdp = (int *)CMSG_DATA(cmh);
|
|
|
|
end = (int *)((char *)cmh + cmh->cmsg_len);
|
|
|
|
while ((char *)fdp + sizeof(int) <= (char *)end &&
|
|
|
|
(char *)fdp + sizeof(int) <= msg_end) {
|
2009-02-18 21:43:15 +03:00
|
|
|
int fd = *fdp;
|
|
|
|
struct stat stbuf;
|
2009-02-19 17:01:17 +03:00
|
|
|
VALUE io;
|
2009-02-18 21:43:15 +03:00
|
|
|
if (fstat(fd, &stbuf) == -1)
|
|
|
|
rb_raise(rb_eSocket, "invalid fd in SCM_RIGHTS");
|
2014-01-28 18:37:34 +04:00
|
|
|
rb_update_max_fd(fd);
|
|
|
|
if (rsock_cmsg_cloexec_state < 0)
|
|
|
|
rsock_cmsg_cloexec_state = rsock_detect_cloexec(fd);
|
|
|
|
if (rsock_cmsg_cloexec_state == 0 || fd <= 2)
|
|
|
|
rb_maygvl_fd_fix_cloexec(fd);
|
2009-02-18 21:43:15 +03:00
|
|
|
if (S_ISSOCK(stbuf.st_mode))
|
2009-03-01 09:30:41 +03:00
|
|
|
io = rsock_init_sock(rb_obj_alloc(rb_cSocket), fd);
|
2009-02-18 21:43:15 +03:00
|
|
|
else
|
|
|
|
io = rb_io_fdopen(fd, O_RDWR, NULL);
|
2009-02-19 15:13:38 +03:00
|
|
|
ary = rb_attr_get(ctl, rb_intern("unix_rights"));
|
2009-02-18 21:43:15 +03:00
|
|
|
rb_ary_push(ary, io);
|
|
|
|
fdp++;
|
|
|
|
}
|
2009-02-19 17:01:17 +03:00
|
|
|
OBJ_FREEZE(ary);
|
2009-02-18 21:43:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
static VALUE
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
bsock_recvmsg_internal(VALUE sock,
|
|
|
|
VALUE vmaxdatlen, VALUE vflags, VALUE vmaxctllen,
|
|
|
|
VALUE scm_rights, VALUE ex, int nonblock)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int grow_buffer;
|
2009-02-18 15:27:13 +03:00
|
|
|
size_t maxdatlen;
|
2009-02-02 05:51:02 +03:00
|
|
|
int flags, orig_flags;
|
|
|
|
struct msghdr mh;
|
|
|
|
struct iovec iov;
|
2013-02-24 21:51:17 +04:00
|
|
|
union_sockaddr namebuf;
|
2015-06-24 02:40:53 +03:00
|
|
|
char *datbuf;
|
2009-02-02 05:51:02 +03:00
|
|
|
VALUE dat_str = Qnil;
|
|
|
|
VALUE ret;
|
|
|
|
ssize_t ss;
|
2013-05-01 08:43:00 +04:00
|
|
|
int request_scm_rights;
|
2013-05-09 15:48:29 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-18 15:27:13 +03:00
|
|
|
struct cmsghdr *cmh;
|
|
|
|
size_t maxctllen;
|
2009-02-18 15:48:11 +03:00
|
|
|
char *ctlbuf;
|
2009-02-18 15:27:13 +03:00
|
|
|
VALUE ctl_str = Qnil;
|
2009-02-18 21:43:15 +03:00
|
|
|
int family;
|
2009-02-21 19:49:44 +03:00
|
|
|
int gc_done = 0;
|
2009-02-18 15:27:13 +03:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2015-11-18 04:18:30 +03:00
|
|
|
maxdatlen = NIL_P(vmaxdatlen) ? 4096 : NUM2SIZET(vmaxdatlen);
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2015-11-17 08:06:18 +03:00
|
|
|
maxctllen = NIL_P(vmaxctllen) ? 4096 : NUM2SIZET(vmaxctllen);
|
2009-02-18 15:27:13 +03:00
|
|
|
#else
|
|
|
|
if (!NIL_P(vmaxctllen))
|
|
|
|
rb_raise(rb_eArgError, "control message not supported");
|
|
|
|
#endif
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
flags = NUM2INT(vflags);
|
2009-02-02 05:51:02 +03:00
|
|
|
#ifdef MSG_DONTWAIT
|
|
|
|
if (nonblock)
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
#endif
|
|
|
|
orig_flags = flags;
|
|
|
|
|
|
|
|
grow_buffer = NIL_P(vmaxdatlen) || NIL_P(vmaxctllen);
|
|
|
|
|
2009-02-27 17:23:54 +03:00
|
|
|
request_scm_rights = 0;
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
if (RTEST(scm_rights))
|
2009-02-27 17:23:54 +03:00
|
|
|
request_scm_rights = 1;
|
2013-05-09 15:48:29 +04:00
|
|
|
#if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
|
|
|
if (request_scm_rights)
|
|
|
|
rb_raise(rb_eNotImpError, "control message for recvmsg is unimplemented");
|
2013-05-01 08:43:00 +04:00
|
|
|
#endif
|
2009-02-27 17:23:54 +03:00
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
if (rb_io_read_pending(fptr)) {
|
2009-02-17 13:43:55 +03:00
|
|
|
rb_raise(rb_eIOError, "recvmsg for buffered IO");
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
if (grow_buffer) {
|
2010-04-28 12:14:13 +04:00
|
|
|
int socktype;
|
|
|
|
socklen_t optlen = (socklen_t)sizeof(socktype);
|
2009-02-02 05:51:02 +03:00
|
|
|
if (getsockopt(fptr->fd, SOL_SOCKET, SO_TYPE, (void*)&socktype, &optlen) == -1) {
|
|
|
|
rb_sys_fail("getsockopt(SO_TYPE)");
|
|
|
|
}
|
|
|
|
if (socktype == SOCK_STREAM)
|
|
|
|
grow_buffer = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retry:
|
2015-06-24 02:40:53 +03:00
|
|
|
if (NIL_P(dat_str))
|
|
|
|
dat_str = rb_str_tmp_new(maxdatlen);
|
|
|
|
else
|
|
|
|
rb_str_resize(dat_str, maxdatlen);
|
|
|
|
datbuf = RSTRING_PTR(dat_str);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2015-06-24 02:40:53 +03:00
|
|
|
if (NIL_P(ctl_str))
|
|
|
|
ctl_str = rb_str_tmp_new(maxctllen);
|
|
|
|
else
|
|
|
|
rb_str_resize(ctl_str, maxctllen);
|
|
|
|
ctlbuf = RSTRING_PTR(ctl_str);
|
2009-02-18 15:27:13 +03:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
memset(&mh, 0, sizeof(mh));
|
|
|
|
|
2009-02-10 17:46:51 +03:00
|
|
|
memset(&namebuf, 0, sizeof(namebuf));
|
2013-02-24 21:51:17 +04:00
|
|
|
mh.msg_name = &namebuf.addr;
|
2010-04-28 12:14:13 +04:00
|
|
|
mh.msg_namelen = (socklen_t)sizeof(namebuf);
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
mh.msg_iov = &iov;
|
|
|
|
mh.msg_iovlen = 1;
|
|
|
|
iov.iov_base = datbuf;
|
|
|
|
iov.iov_len = maxdatlen;
|
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
mh.msg_control = ctlbuf;
|
2010-04-28 12:14:13 +04:00
|
|
|
mh.msg_controllen = (socklen_t)maxctllen;
|
2009-02-02 05:51:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (grow_buffer)
|
|
|
|
flags |= MSG_PEEK;
|
|
|
|
|
|
|
|
rb_io_check_closed(fptr);
|
2015-05-29 05:24:18 +03:00
|
|
|
if (nonblock && !MSG_DONTWAIT_RELIABLE)
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_io_set_nonblock(fptr);
|
|
|
|
|
|
|
|
ss = rb_recvmsg(fptr->fd, &mh, flags);
|
|
|
|
|
2009-02-21 18:57:52 +03:00
|
|
|
if (ss == -1) {
|
2014-02-21 04:55:13 +04:00
|
|
|
if (!nonblock && rb_io_wait_readable(fptr->fd)) {
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
goto retry;
|
|
|
|
}
|
2015-06-15 22:38:49 +03:00
|
|
|
if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
if (ex == Qfalse) {
|
2015-06-15 22:38:49 +03:00
|
|
|
return sym_wait_readable;
|
|
|
|
}
|
2013-04-08 23:58:55 +04:00
|
|
|
rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvmsg(2) would block");
|
2015-06-15 22:38:49 +03:00
|
|
|
}
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-21 20:16:18 +03:00
|
|
|
if (!gc_done && (errno == EMFILE || errno == EMSGSIZE)) {
|
|
|
|
/*
|
|
|
|
* When SCM_RIGHTS hit the file descriptors limit:
|
|
|
|
* - Linux 2.6.18 causes success with MSG_CTRUNC
|
|
|
|
* - MacOS X 10.4 causes EMSGSIZE (and lost file descriptors?)
|
|
|
|
* - Solaris 11 causes EMFILE
|
|
|
|
*/
|
2009-02-21 19:49:44 +03:00
|
|
|
gc_and_retry:
|
|
|
|
rb_gc();
|
|
|
|
gc_done = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
2015-11-18 04:37:34 +03:00
|
|
|
#else
|
2015-11-18 20:53:20 +03:00
|
|
|
if (NIL_P(vmaxdatlen) && grow_buffer && errno == EMSGSIZE)
|
2015-11-18 04:37:34 +03:00
|
|
|
ss = (ssize_t)iov.iov_len;
|
|
|
|
else
|
2009-02-21 19:49:44 +03:00
|
|
|
#endif
|
2009-02-21 18:32:52 +03:00
|
|
|
rb_sys_fail("recvmsg(2)");
|
2009-02-21 18:57:52 +03:00
|
|
|
}
|
2009-02-21 18:32:52 +03:00
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
if (grow_buffer) {
|
|
|
|
int grown = 0;
|
2015-11-19 16:49:58 +03:00
|
|
|
if (NIL_P(vmaxdatlen) && ss != -1 && ss == (ssize_t)iov.iov_len) {
|
2009-02-21 20:04:22 +03:00
|
|
|
if (SIZE_MAX/2 < maxdatlen)
|
|
|
|
rb_raise(rb_eArgError, "max data length too big");
|
2009-02-02 05:51:02 +03:00
|
|
|
maxdatlen *= 2;
|
|
|
|
grown = 1;
|
|
|
|
}
|
2015-11-19 16:49:58 +03:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
if (NIL_P(vmaxctllen) && (mh.msg_flags & MSG_CTRUNC)) {
|
2009-02-21 19:49:44 +03:00
|
|
|
#define BIG_ENOUGH_SPACE 65536
|
|
|
|
if (BIG_ENOUGH_SPACE < maxctllen &&
|
2013-05-07 07:19:52 +04:00
|
|
|
(socklen_t)mh.msg_controllen < (socklen_t)(maxctllen - BIG_ENOUGH_SPACE)) {
|
2009-02-21 19:49:44 +03:00
|
|
|
/* there are big space bug truncated.
|
|
|
|
* file descriptors limit? */
|
2009-02-21 19:53:30 +03:00
|
|
|
if (!gc_done) {
|
2011-07-23 12:13:37 +04:00
|
|
|
rsock_discard_cmsg_resource(&mh, (flags & MSG_PEEK) != 0);
|
2009-02-21 19:49:44 +03:00
|
|
|
goto gc_and_retry;
|
2009-02-21 19:53:30 +03:00
|
|
|
}
|
2009-02-21 19:49:44 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-02-21 20:04:22 +03:00
|
|
|
if (SIZE_MAX/2 < maxctllen)
|
|
|
|
rb_raise(rb_eArgError, "max control message length too big");
|
2009-02-21 19:49:44 +03:00
|
|
|
maxctllen *= 2;
|
|
|
|
grown = 1;
|
|
|
|
}
|
|
|
|
#undef BIG_ENOUGH_SPACE
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (grown) {
|
2011-07-23 12:13:37 +04:00
|
|
|
rsock_discard_cmsg_resource(&mh, (flags & MSG_PEEK) != 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
grow_buffer = 0;
|
|
|
|
if (flags != orig_flags) {
|
2011-07-23 12:13:37 +04:00
|
|
|
rsock_discard_cmsg_resource(&mh, (flags & MSG_PEEK) != 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
flags = orig_flags;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NIL_P(dat_str))
|
|
|
|
dat_str = rb_tainted_str_new(datbuf, ss);
|
|
|
|
else {
|
|
|
|
rb_str_resize(dat_str, ss);
|
|
|
|
OBJ_TAINT(dat_str);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
rb_obj_reveal(dat_str, rb_cString);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = rb_ary_new3(3, dat_str,
|
2009-03-01 09:30:41 +03:00
|
|
|
rsock_io_socket_addrinfo(sock, mh.msg_name, mh.msg_namelen),
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2009-02-02 05:51:02 +03:00
|
|
|
INT2NUM(mh.msg_flags)
|
|
|
|
#else
|
|
|
|
Qnil
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2015-07-02 04:58:14 +03:00
|
|
|
family = rsock_getfamily(fptr);
|
2009-02-02 05:51:02 +03:00
|
|
|
if (mh.msg_controllen) {
|
2009-02-19 17:01:17 +03:00
|
|
|
char *msg_end = (char *)mh.msg_control + mh.msg_controllen;
|
2009-02-02 05:51:02 +03:00
|
|
|
for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) {
|
|
|
|
VALUE ctl;
|
2009-02-19 17:01:17 +03:00
|
|
|
char *ctl_end;
|
2009-02-02 05:51:02 +03:00
|
|
|
size_t clen;
|
|
|
|
if (cmh->cmsg_len == 0) {
|
2009-02-19 16:18:02 +03:00
|
|
|
rb_raise(rb_eTypeError, "invalid control message (cmsg_len == 0)");
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
2009-02-19 17:01:17 +03:00
|
|
|
ctl_end = (char*)cmh + cmh->cmsg_len;
|
|
|
|
clen = (ctl_end <= msg_end ? ctl_end : msg_end) - (char*)CMSG_DATA(cmh);
|
2009-02-09 16:56:43 +03:00
|
|
|
ctl = ancdata_new(family, cmh->cmsg_level, cmh->cmsg_type, rb_tainted_str_new((char*)CMSG_DATA(cmh), clen));
|
2009-02-27 17:23:54 +03:00
|
|
|
if (request_scm_rights)
|
|
|
|
make_io_for_unix_rights(ctl, cmh, msg_end);
|
|
|
|
else
|
2011-07-23 12:13:37 +04:00
|
|
|
discard_cmsg(cmh, msg_end, (flags & MSG_PEEK) != 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_ary_push(ret, ctl);
|
|
|
|
}
|
2015-05-09 01:31:02 +03:00
|
|
|
RB_GC_GUARD(ctl_str);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(HAVE_RECVMSG)
|
2010-03-21 13:50:52 +03:00
|
|
|
VALUE
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
rsock_bsock_recvmsg(VALUE sock, VALUE dlen, VALUE flags, VALUE clen,
|
|
|
|
VALUE scm_rights)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
VALUE ex = Qtrue;
|
|
|
|
return bsock_recvmsg_internal(sock, dlen, flags, clen, scm_rights, ex, 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
2009-04-18 18:10:06 +04:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
2009-04-18 18:10:06 +04:00
|
|
|
#if defined(HAVE_RECVMSG)
|
2010-03-21 13:50:52 +03:00
|
|
|
VALUE
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
rsock_bsock_recvmsg_nonblock(VALUE sock, VALUE dlen, VALUE flags, VALUE clen,
|
|
|
|
VALUE scm_rights, VALUE ex)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
socket (bsock_recvmsg_internal): avoid arg parsing
* ext/socket/ancdata.c (bsock_recvmsg_internal): avoid arg parsing
(rsock_bsock_recvmsg): adjust for above change
(rsock_bsock_recvmsg_nonblock): ditto
[ruby-core:71439] [Feature #11339]
* ext/socket/rubysocket.h: adjust prototypes for above
* ext/socket/basicsocket.c (rsock_init_basicsocket):
adjust private methods
* ext/socket/lib/socket.rb (BasicSocket#recvmsg): wrapper method
(BasicSocket#recvmsg_nonblock): ditto
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52550) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52550) [x86_64-linux]
-----------------------------------------------------------
recvmsg_nonblock
require 'socket'
nr = 1_000_000
i = 0
msg = '.'
buf = '.'
begin
r, w = UNIXSocket.pair(:SEQPACKET)
while i < nr
i += 1
w.sendmsg(msg)
r.recvmsg_nonblock(1, exception: false)
end
ensure
r.close
w.close
end
-----------------------------------------------------------
raw data:
[["recvmsg_nonblock",
[[3.721687912940979,
3.6072621569037437,
3.580637402832508,
3.614185404032469,
3.6029579415917397],
[2.4694008752703667,
2.4908322244882584,
2.5051278844475746,
2.5037173740565777,
2.548359278589487]]]]
Elapsed time: 30.646087052 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recvmsg_nonblock 3.581 2.469
Speedup ratio: compare with the result of `a' (greater is better)
name b
recvmsg_nonblock 1.450
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 03:58:23 +03:00
|
|
|
return bsock_recvmsg_internal(sock, dlen, flags, clen, scm_rights, ex, 1);
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|
2009-04-18 18:10:06 +04:00
|
|
|
#endif
|
2009-02-02 05:51:02 +03:00
|
|
|
|
|
|
|
void
|
2010-03-22 19:15:21 +03:00
|
|
|
rsock_init_ancdata(void)
|
2009-02-02 05:51:02 +03:00
|
|
|
{
|
2013-05-09 07:08:58 +04:00
|
|
|
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
|
2011-08-16 03:08:39 +04:00
|
|
|
/*
|
|
|
|
* Document-class: Socket::AncillaryData
|
|
|
|
*
|
|
|
|
* Socket::AncillaryData represents the ancillary data (control information)
|
|
|
|
* used by sendmsg and recvmsg system call. It contains socket #family,
|
|
|
|
* control message (cmsg) #level, cmsg #type and cmsg #data.
|
|
|
|
*/
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_cAncillaryData = rb_define_class_under(rb_cSocket, "AncillaryData", rb_cObject);
|
2009-02-09 13:49:16 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "initialize", ancillary_initialize, 4);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "inspect", ancillary_inspect, 0);
|
2009-02-09 13:49:16 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "family", ancillary_family_m, 0);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "level", ancillary_level_m, 0);
|
|
|
|
rb_define_method(rb_cAncillaryData, "type", ancillary_type_m, 0);
|
|
|
|
rb_define_method(rb_cAncillaryData, "data", ancillary_data, 0);
|
2009-02-25 17:19:24 +03:00
|
|
|
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "cmsg_is?", ancillary_cmsg_is_p, 2);
|
2009-02-25 17:19:24 +03:00
|
|
|
|
2009-02-09 13:49:16 +03:00
|
|
|
rb_define_singleton_method(rb_cAncillaryData, "int", ancillary_s_int, 4);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "int", ancillary_int, 0);
|
2009-02-25 17:19:24 +03:00
|
|
|
|
2009-02-25 17:24:10 +03:00
|
|
|
rb_define_singleton_method(rb_cAncillaryData, "unix_rights", ancillary_s_unix_rights, -1);
|
|
|
|
rb_define_method(rb_cAncillaryData, "unix_rights", ancillary_unix_rights, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cAncillaryData, "timestamp", ancillary_timestamp, 0);
|
|
|
|
|
2009-02-11 10:44:53 +03:00
|
|
|
rb_define_singleton_method(rb_cAncillaryData, "ip_pktinfo", ancillary_s_ip_pktinfo, -1);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "ip_pktinfo", ancillary_ip_pktinfo, 0);
|
2009-02-25 17:19:24 +03:00
|
|
|
|
2009-02-11 06:56:13 +03:00
|
|
|
rb_define_singleton_method(rb_cAncillaryData, "ipv6_pktinfo", ancillary_s_ipv6_pktinfo, 2);
|
2009-02-02 05:51:02 +03:00
|
|
|
rb_define_method(rb_cAncillaryData, "ipv6_pktinfo", ancillary_ipv6_pktinfo, 0);
|
|
|
|
rb_define_method(rb_cAncillaryData, "ipv6_pktinfo_addr", ancillary_ipv6_pktinfo_addr, 0);
|
|
|
|
rb_define_method(rb_cAncillaryData, "ipv6_pktinfo_ifindex", ancillary_ipv6_pktinfo_ifindex, 0);
|
|
|
|
#endif
|
2015-06-15 22:38:49 +03:00
|
|
|
#undef rb_intern
|
|
|
|
sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
|
|
|
|
sym_wait_writable = ID2SYM(rb_intern("wait_writable"));
|
2009-02-02 05:51:02 +03:00
|
|
|
}
|