Skip to content

Commit 0d932d2

Browse files
authored
Shortcut Base.in for mismatching ip versions to avoid iterating the entire net (#22)
Closes #21.
1 parent 92a9364 commit 0d932d2

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/IPNets.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ Base.show(io::IO, net::T) where T <: IPNet = print(io, T, "(\"", net, "\")")
126126

127127
Base.in(ip::IPv4, network::IPv4Net) = ip.host & network.netmask == network.netaddr
128128
Base.in(ip::IPv6, network::IPv6Net) = ip.host & network.netmask == network.netaddr
129+
Base.in(ip::Any, network::IPv4Net) = false
130+
Base.in(ip::Any, network::IPv6Net) = false
129131

130132
# IP Networks are ordered first by starting network address
131133
# and then by network mask. That is, smaller IP nets (with higher

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,19 @@ using IPNets, Test, Sockets
137137
@test !is_global(ip"::1")
138138
@test !is_private(ip"2606:4700:4700::1111")
139139
@test is_global(ip"2606:4700:4700::1111")
140+
141+
###############
142+
## Both nets ##
143+
###############
144+
145+
ip6net1 = IPv6Net("2605:dead:beef:ff00::/64")
146+
ip6net2 = IPv6Net("::ffff:0:0/96") # IPv4 addresses encoded as IPv6
147+
ip4net = IPv4Net("192.168.0.0/16")
148+
149+
@test !(ip"192.168.1.200" in ip6net1)
150+
@test !(ip"192.168.1.200" in ip6net2) # *
151+
@test !(ip"2606:4700:4700::1111" in ip4net)
152+
@test !(ip"::ffff:c0a8:01c8" in ip4net) # *
153+
# * If IPv4-mapped addresses are considered equivalent, these tests should
154+
# be true, but that requires more code.
140155
end

0 commit comments

Comments
 (0)