File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -464,6 +464,20 @@ def netmask
464464 _to_string ( @mask_addr )
465465 end
466466
467+ # Returns the wildcard mask in string format e.g. 0.0.255.255
468+ def wildcard_mask
469+ case @family
470+ when Socket ::AF_INET
471+ mask = IN4MASK ^ @mask_addr
472+ when Socket ::AF_INET6
473+ mask = IN6MASK ^ @mask_addr
474+ else
475+ raise AddressFamilyError , "unsupported address family"
476+ end
477+
478+ _to_string ( mask )
479+ end
480+
467481 # Returns the IPv6 zone identifier, if present.
468482 # Raises InvalidAddressError if not an IPv6 address.
469483 def zone_id
Original file line number Diff line number Diff line change @@ -244,6 +244,29 @@ def test_netmask
244244 assert_equal ( a . netmask , "255.255.255.0" )
245245 end
246246
247+ def test_wildcard_mask
248+ a = IPAddr . new ( "192.168.1.2/1" )
249+ assert_equal ( a . wildcard_mask , "127.255.255.255" )
250+
251+ a = IPAddr . new ( "192.168.1.2/8" )
252+ assert_equal ( a . wildcard_mask , "0.255.255.255" )
253+
254+ a = IPAddr . new ( "192.168.1.2/16" )
255+ assert_equal ( a . wildcard_mask , "0.0.255.255" )
256+
257+ a = IPAddr . new ( "192.168.1.2/24" )
258+ assert_equal ( a . wildcard_mask , "0.0.0.255" )
259+
260+ a = IPAddr . new ( "192.168.1.2/32" )
261+ assert_equal ( a . wildcard_mask , "0.0.0.0" )
262+
263+ a = IPAddr . new ( "3ffe:505:2::/48" )
264+ assert_equal ( a . wildcard_mask , "0000:0000:0000:ffff:ffff:ffff:ffff:ffff" )
265+
266+ a = IPAddr . new ( "3ffe:505:2::/128" )
267+ assert_equal ( a . wildcard_mask , "0000:0000:0000:0000:0000:0000:0000:0000" )
268+ end
269+
247270 def test_zone_id
248271 a = IPAddr . new ( "192.168.1.2" )
249272 assert_raise ( IPAddr ::InvalidAddressError ) { a . zone_id = '%ab0' }
You can’t perform that action at this time.
0 commit comments