From 23e286f3fb1b7570e107b9a028c1fb03bacc5cca Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 23 Feb 2009 11:32:43 +0000 Subject: [PATCH] * ext/socket/ancdata.c (inspect_bintime_as_abstime): new function to show struct bintime. (ancillary_inspect): use it for SCM_BINTIME on FreeBSD. * ext/socket/mkconstants.rb: define SCM_BINTIME. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22570 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++ ext/socket/ancdata.c | 51 ++++++++++++++++++++++++++++++++++++++ ext/socket/mkconstants.rb | 1 + test/socket/test_socket.rb | 18 ++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/ChangeLog b/ChangeLog index 5a1817c8d9..a80980c8e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Feb 23 20:30:06 2009 Tanaka Akira + + * ext/socket/ancdata.c (inspect_bintime_as_abstime): new function to + show struct bintime. + (ancillary_inspect): use it for SCM_BINTIME on FreeBSD. + + * ext/socket/mkconstants.rb: define SCM_BINTIME. + Mon Feb 23 20:18:49 2009 Tanaka Akira * ext/socket/ancdata.c (inspect_timeval_as_abstime): use time_t diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c index 793c7ed5df..f902a67f56 100644 --- a/ext/socket/ancdata.c +++ b/ext/socket/ancdata.c @@ -722,6 +722,54 @@ inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret) } #endif +#if defined(SCM_BINTIME) +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 + /* * call-seq: * ancillarydata.inspect => string @@ -795,6 +843,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_BINTIME) /* FreeBSD */ + case SCM_BINTIME: inspected = inspect_bintime_as_abstime(level, type, data, ret); break; +# endif # if defined(SCM_RIGHTS) /* 4.4BSD */ case SCM_RIGHTS: inspected = anc_inspect_socket_rights(level, type, data, ret); break; # endif diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb index e2efc49295..fc39faf8e0 100644 --- a/ext/socket/mkconstants.rb +++ b/ext/socket/mkconstants.rb @@ -668,6 +668,7 @@ SCM_CREDS SCM_TIMESTAMP SCM_TIMESTAMPNS SCM_UCRED +SCM_BINTIME LOCAL_PEERCRED LOCAL_CREDS diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index f411a14b7d..1d4d4fa828 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -291,4 +291,22 @@ class TestSocket < Test::Unit::TestCase assert_match(pat, stamp.inspect) end + def test_bintime + return if /freebsd/ !~ RUBY_PLATFORM + 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| + s1.setsockopt(:SOCKET, :BINTIME, true) + s2.send "a", 0, s1.local_address + msg, addr, rflags, stamp = s1.recvmsg + assert_equal("a", msg) + assert(stamp.cmsg_is?(:SOCKET, :BINTIME)) + } + } + t2 = Time.now.strftime("%Y-%m-%d") + pat = Regexp.union([t1, t2].uniq) + assert_match(pat, stamp.inspect) + end + end if defined?(Socket)