File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -477,6 +477,20 @@ def netmask
477477 _to_string ( @mask_addr )
478478 end
479479
480+ # Returns the wildcard mask in string format e.g. 0.0.255.255
481+ def wildcard_mask
482+ case @family
483+ when Socket ::AF_INET
484+ mask = IN4MASK ^ @mask_addr
485+ when Socket ::AF_INET6
486+ mask = IN6MASK ^ @mask_addr
487+ else
488+ raise AddressFamilyError , "unsupported address family"
489+ end
490+
491+ _to_string ( mask )
492+ end
493+
480494 # Returns the IPv6 zone identifier, if present.
481495 # Raises InvalidAddressError if not an IPv6 address.
482496 def zone_id
Original file line number Diff line number Diff line change @@ -271,6 +271,29 @@ def test_netmask
271271 assert_equal ( a . netmask , "255.255.255.0" )
272272 end
273273
274+ def test_wildcard_mask
275+ a = IPAddr . new ( "192.168.1.2/1" )
276+ assert_equal ( a . wildcard_mask , "127.255.255.255" )
277+
278+ a = IPAddr . new ( "192.168.1.2/8" )
279+ assert_equal ( a . wildcard_mask , "0.255.255.255" )
280+
281+ a = IPAddr . new ( "192.168.1.2/16" )
282+ assert_equal ( a . wildcard_mask , "0.0.255.255" )
283+
284+ a = IPAddr . new ( "192.168.1.2/24" )
285+ assert_equal ( a . wildcard_mask , "0.0.0.255" )
286+
287+ a = IPAddr . new ( "192.168.1.2/32" )
288+ assert_equal ( a . wildcard_mask , "0.0.0.0" )
289+
290+ a = IPAddr . new ( "3ffe:505:2::/48" )
291+ assert_equal ( a . wildcard_mask , "0000:0000:0000:ffff:ffff:ffff:ffff:ffff" )
292+
293+ a = IPAddr . new ( "3ffe:505:2::/128" )
294+ assert_equal ( a . wildcard_mask , "0000:0000:0000:0000:0000:0000:0000:0000" )
295+ end
296+
274297 def test_zone_id
275298 a = IPAddr . new ( "192.168.1.2" )
276299 assert_raise ( IPAddr ::InvalidAddressError ) { a . zone_id = '%ab0' }
You can’t perform that action at this time.
0 commit comments