1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/socket/raddrinfo.c (addrinfo_ipv4_private_p): new method.

(addrinfo_ipv4_loopback_p): ditto.
  (addrinfo_ipv4_multicast_p): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21928 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-02-01 12:28:50 +00:00
parent d99eeabea6
commit f1137963e3
3 changed files with 84 additions and 1 deletions

View file

@ -1,3 +1,9 @@
Sun Feb 1 21:27:55 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/raddrinfo.c (addrinfo_ipv4_private_p): new method.
(addrinfo_ipv4_loopback_p): ditto.
(addrinfo_ipv4_multicast_p): ditto.
Sun Feb 1 16:10:06 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Sun Feb 1 16:10:06 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/optparse.rb (Switch#summarize): strips an equal sign from * lib/optparse.rb (Switch#summarize): strips an equal sign from

View file

@ -1623,6 +1623,60 @@ addrinfo_ip_port(VALUE self)
return INT2NUM(port); return INT2NUM(port);
} }
static int
extract_in_addr(VALUE self, uint32_t *addrp)
{
rb_addrinfo_t *rai = get_addrinfo(self);
int family = ai_get_afamily(rai);
if (family != AF_INET) return 0;
*addrp = ntohl(((struct sockaddr_in *)&rai->addr)->sin_addr.s_addr);
return 1;
}
/*
* Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
* It returns false otherwise.
*/
static VALUE
addrinfo_ipv4_private_p(VALUE self)
{
uint32_t a;
if (!extract_in_addr(self, &a)) return Qfalse;
if ((a & 0xff000000) == 0x0a000000 || /* 10.0.0.0/8 */
(a & 0xfff00000) == 0xac100000 || /* 172.16.0.0/12 */
(a & 0xffff0000) == 0xc0a80000) /* 192.168.0.0/16 */
return Qtrue;
return Qfalse;
}
/*
* Returns true for IPv4 loopback address (127.0.0.0/8).
* It returns false otherwise.
*/
static VALUE
addrinfo_ipv4_loopback_p(VALUE self)
{
uint32_t a;
if (!extract_in_addr(self, &a)) return Qfalse;
if ((a & 0xff000000) == 0x7f000000) /* 127.0.0.0/8 */
return Qtrue;
return Qfalse;
}
/*
* Returns true for IPv4 multicast address (224.0.0.0/4).
* It returns false otherwise.
*/
static VALUE
addrinfo_ipv4_multicast_p(VALUE self)
{
uint32_t a;
if (!extract_in_addr(self, &a)) return Qfalse;
if ((a & 0xf0000000) == 0xe0000000) /* 224.0.0.0/4 */
return Qtrue;
return Qfalse;
}
#ifdef AF_INET6 #ifdef AF_INET6
static struct in6_addr * static struct in6_addr *
@ -2036,6 +2090,10 @@ Init_addrinfo(void)
rb_define_method(rb_cAddrInfo, "ip_address", addrinfo_ip_address, 0); rb_define_method(rb_cAddrInfo, "ip_address", addrinfo_ip_address, 0);
rb_define_method(rb_cAddrInfo, "ip_port", addrinfo_ip_port, 0); rb_define_method(rb_cAddrInfo, "ip_port", addrinfo_ip_port, 0);
rb_define_method(rb_cAddrInfo, "ipv4_private?", addrinfo_ipv4_private_p, 0);
rb_define_method(rb_cAddrInfo, "ipv4_loopback?", addrinfo_ipv4_loopback_p, 0);
rb_define_method(rb_cAddrInfo, "ipv4_multicast?", addrinfo_ipv4_multicast_p, 0);
#ifdef AF_INET6 #ifdef AF_INET6
rb_define_method(rb_cAddrInfo, "ipv6_unspecified?", addrinfo_ipv6_unspecified_p, 0); rb_define_method(rb_cAddrInfo, "ipv6_unspecified?", addrinfo_ipv6_unspecified_p, 0);
rb_define_method(rb_cAddrInfo, "ipv6_loopback?", addrinfo_ipv6_loopback_p, 0); rb_define_method(rb_cAddrInfo, "ipv6_loopback?", addrinfo_ipv6_loopback_p, 0);

View file

@ -63,6 +63,25 @@ class TestSocketAddrInfo < Test::Unit::TestCase
assert(!ipv4_ai.unix?) assert(!ipv4_ai.unix?)
end end
def test_ipv4_address_predicates
list = [
[:ipv4_private?, "10.0.0.0", "10.255.255.255",
"172.16.0.0", "172.31.255.255",
"192.168.0.0", "192.168.255.255"],
[:ipv4_loopback?, "127.0.0.1", "127.0.0.0", "127.255.255.255"],
[:ipv4_multicast?, "224.0.0.0", "224.255.255.255"]
]
list.each {|meth, *addrs|
addrs.each {|addr|
assert(AddrInfo.ip(addr).send(meth), "AddrInfo.ip(#{addr.inspect}).#{meth}")
list.each {|meth2,|
next if meth == meth2
assert(!AddrInfo.ip(addr).send(meth2), "!AddrInfo.ip(#{addr.inspect}).#{meth2}")
}
}
}
end
def test_basicsocket_send def test_basicsocket_send
s1 = Socket.new(:INET, :DGRAM, 0) s1 = Socket.new(:INET, :DGRAM, 0)
s1.bind(Socket.sockaddr_in(0, "127.0.0.1")) s1.bind(Socket.sockaddr_in(0, "127.0.0.1"))
@ -326,7 +345,7 @@ class TestSocketAddrInfo < Test::Unit::TestCase
assert_equal(ai1.canonname, ai2.canonname) assert_equal(ai1.canonname, ai2.canonname)
end end
def test_ipv6_predicates def test_ipv6_address_predicates
list = [ list = [
[:ipv6_unspecified?, "::"], [:ipv6_unspecified?, "::"],
[:ipv6_loopback?, "::1"], [:ipv6_loopback?, "::1"],