Skip to content

Commit

Permalink
Merge pull request #83 from icy-arctic-fox/link-local
Browse files Browse the repository at this point in the history
Add detection of link-local addresses
  • Loading branch information
francisluong committed Sep 1, 2017
2 parents f16c587 + 548f007 commit a172928
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/ipaddress/ipv4.rb
Expand Up @@ -650,6 +650,20 @@ def loopback?
[self.class.new("127.0.0.0/8")].any? {|i| i.include? self}
end

#
# Checks if an IPv4 address objects belongs
# to a link-local network RFC3927
#
# Example:
#
# ip = IPAddress "169.254.0.1"
# ip.link_local?
# #=> true
#
def link_local?
[self.class.new("169.254.0.0/16")].any? {|i| i.include? self}
end

#
# Returns the IP address in in-addr.arpa format
# for DNS lookups
Expand Down
28 changes: 28 additions & 0 deletions lib/ipaddress/ipv6.rb
Expand Up @@ -423,6 +423,34 @@ def loopback?
@prefix == 128 and @compressed == "::1"
end

#
# Checks if an IPv6 address objects belongs
# to a link-local network RFC4291
#
# Example:
#
# ip = IPAddress "fe80::1"
# ip.link_local?
# #=> true
#
def link_local?
[self.class.new("fe80::/64")].any? {|i| i.include? self}
end

#
# Checks if an IPv6 address objects belongs
# to a unique-local network RFC4193
#
# Example:
#
# ip = IPAddress "fc00::1"
# ip.unique_local?
# #=> true
#
def unique_local?
[self.class.new("fc00::/7")].any? {|i| i.include? self}
end

#
# Returns true if the address is a mapped address
#
Expand Down
25 changes: 25 additions & 0 deletions test/ipaddress/ipv4_test.rb
Expand Up @@ -74,6 +74,22 @@ def setup
"10.32.0.1" => ["10.32.0.253", 253],
"192.0.0.0" => ["192.1.255.255", 131072]
}

@link_local = [
"169.254.0.0",
"169.254.255.255",
"169.254.12.34",
"169.254.0.0/16",
"169.254.0.0/17"]

@not_link_local = [
"127.0.0.1",
"127.0.1.1",
"192.168.0.100",
"169.255.0.0",
"169.254.0.0/15",
"0.0.0.0",
"255.255.255.255"]

end

Expand Down Expand Up @@ -310,6 +326,15 @@ def test_method_private?
assert_equal false, @klass.new("192.0.0.2/24").private?
end

def test_method_link_local?
@link_local.each do |addr|
assert_equal true, @klass.new(addr).link_local?
end
@not_link_local.each do |addr|
assert_equal false, @klass.new(addr).link_local?
end
end

def test_method_octet
assert_equal 172, @ip[0]
assert_equal 16, @ip[1]
Expand Down
49 changes: 49 additions & 0 deletions test/ipaddress/ipv6_test.rb
Expand Up @@ -42,6 +42,37 @@ def setup
@network = @klass.new "2001:db8:8:800::/64"
@arr = [8193,3512,0,0,8,2048,8204,16762]
@hex = "20010db80000000000080800200c417a"

@link_local = [
"fe80::",
"fe80::1",
"fe80::208:74ff:feda:625c",
"fe80::/64",
"fe80::/65"]

@not_link_local = [
"::",
"::1",
"ff80:03:02:01::",
"2001:db8::8:800:200c:417a",
"fe80::/63"]

@unique_local = [
"fc00::/7",
"fc00::/8",
"fd00::/8",
"fd12:3456:789a:1::1",
"fd12:3456:789a:1::/64",
"fc00::1"]

@not_unique_local = [
"fc00::/6",
"::",
"::1",
"fe80::",
"fe80::1",
"fe80::/64"]

end

def test_attribute_address
Expand Down Expand Up @@ -215,6 +246,24 @@ def test_method_loopback?
assert_equal false, @ip.loopback?
end

def test_method_link_local?
@link_local.each do |addr|
assert_equal true, @klass.new(addr).link_local?
end
@not_link_local.each do |addr|
assert_equal false, @klass.new(addr).link_local?
end
end

def test_method_unique_local?
@unique_local.each do |addr|
assert_equal true, @klass.new(addr).unique_local?
end
@not_unique_local.each do |addr|
assert_equal false, @klass.new(addr).unique_local?
end
end

def test_method_network
@networks.each do |addr,net|
ip = @klass.new addr
Expand Down

0 comments on commit a172928

Please sign in to comment.