Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ def ipv4_compat
if !ipv4?
raise InvalidAddressError, "not an IPv4 address: #{@addr}"
end
return self.clone.set(@addr, Socket::AF_INET6)
clone = self.clone.set(@addr, Socket::AF_INET6)
clone.instance_variable_set(:@mask_addr, @mask_addr | 0xffffffffffffffffffffffff00000000)
clone
end

# Returns a new ipaddr built by converting the IPv6 address into a
Expand Down
7 changes: 7 additions & 0 deletions test/test_ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ def test_ipv4_compat
}
assert_equal("::192.168.1.2", b.to_s)
assert_equal(Socket::AF_INET6, b.family)
assert_equal(128, b.prefix)

a = IPAddr.new("192.168.0.0/16")
b = a.ipv4_compat
assert_equal("::192.168.0.0", b.to_s)
assert_equal(Socket::AF_INET6, b.family)
assert_equal(112, b.prefix)
end

def test_ipv4_mapped
Expand Down