* ext/socket/ancdata.c (inspect_timespec_as_abstime): new function to

show struct timespec.
  (ancillary_inspect): use it for SCM_TIMESTAMPNS on GNU/Linux.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22571 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-02-23 11:41:38 +00:00
Родитель 23e286f3fb
Коммит 605cd65904
3 изменённых файлов: 54 добавлений и 2 удалений

Просмотреть файл

@ -1,3 +1,9 @@
Mon Feb 23 20:39:21 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/ancdata.c (inspect_timespec_as_abstime): new function to
show struct timespec.
(ancillary_inspect): use it for SCM_TIMESTAMPNS on GNU/Linux.
Mon Feb 23 20:30:06 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/ancdata.c (inspect_bintime_as_abstime): new function to

Просмотреть файл

@ -700,7 +700,7 @@ anc_inspect_ipv6_pktinfo(int level, int type, VALUE data, VALUE ret)
}
#endif
#if defined(SCM_TIMESTAMP)
#if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
static int
inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret)
{
@ -722,7 +722,27 @@ inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret)
}
#endif
#if defined(SCM_BINTIME)
#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 */
static int
inspect_bintime_as_abstime(int level, int optname, VALUE data, VALUE ret)
{
@ -843,6 +863,9 @@ ancillary_inspect(VALUE self)
# if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
case SCM_TIMESTAMP: inspected = inspect_timeval_as_abstime(level, type, data, ret); break;
# endif
# if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
case SCM_TIMESTAMPNS: inspected = inspect_timespec_as_abstime(level, type, data, ret); break;
# endif
# if defined(SCM_BINTIME) /* FreeBSD */
case SCM_BINTIME: inspected = inspect_bintime_as_abstime(level, type, data, ret); break;
# endif

Просмотреть файл

@ -291,6 +291,29 @@ class TestSocket < Test::Unit::TestCase
assert_match(pat, stamp.inspect)
end
def test_timestampns
return if /linux/ !~ RUBY_PLATFORM || !defined?(Socket::SO_TIMESTAMPNS)
t1 = Time.now.strftime("%Y-%m-%d")
stamp = nil
Addrinfo.udp("127.0.0.1", 0).bind {|s1|
Addrinfo.udp("127.0.0.1", 0).bind {|s2|
begin
s1.setsockopt(:SOCKET, :TIMESTAMPNS, true)
rescue Errno::ENOPROTOOPT
# SO_TIMESTAMPNS is available since Linux 2.6.22
return
end
s2.send "a", 0, s1.local_address
msg, addr, rflags, stamp = s1.recvmsg
assert_equal("a", msg)
assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMPNS))
}
}
t2 = Time.now.strftime("%Y-%m-%d")
pat = Regexp.union([t1, t2].uniq)
assert_match(pat, stamp.inspect)
end
def test_bintime
return if /freebsd/ !~ RUBY_PLATFORM
t1 = Time.now.strftime("%Y-%m-%d")