summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarco Ceresa <ceresa@gmail.com>2010-02-09 16:01:26 +0000
committerMarco Ceresa <ceresa@gmail.com>2010-02-09 16:01:26 +0000
commitb3b6f0529017568a8d9639159a4b503e361b5fe5 (patch)
tree859f785322348b8dcb5d98f5bc4090206a688cbf /lib
parent48045a5857123dc3e5067bb457e4fd2c337c98df (diff)
downloadipaddress-b3b6f0529017568a8d9639159a4b503e361b5fe5.tar.gz
Added more IPv6 and IPv4 methods
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddress/ipv4.rb51
-rw-r--r--lib/ipaddress/ipv6.rb112
2 files changed, 117 insertions, 46 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index 23dec91..b7b8a75 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -33,6 +33,8 @@ module IPAddress;
/^10./ => 16, # Class B, from 128.0.0.0 to 191.255.255.255
/^110/ => 24 # Class C, D and E, from 192.0.0.0 to 255.255.255.254
}
+
+ REGEXP = Regexp.new(/((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)/)
#
# Creates a new IPv4 address object.
@@ -607,6 +609,38 @@ module IPAddress;
end
#
+ # Docs here
+ # TODO
+ #
+ def a?
+ CLASSFUL.index(8) === bits
+ end
+
+ #
+ # Docs here
+ # TODO
+ #
+ def b?
+ CLASSFUL.index(16) === bits
+ end
+
+ #
+ # Docs here
+ # TODO
+ #
+ def c?
+ CLASSFUL.index(24) === bits
+ end
+
+ #
+ # Docs here
+ # TODO
+ #
+ def to_ipv6
+ "%.4x:%.4x" % [to_u32].pack("N").unpack("nn")
+ end
+
+ #
# Creates a new IPv4 object from an
# unsigned 32bits integer.
#
@@ -624,9 +658,9 @@ module IPAddress;
def self.parse_u32(u32, prefix=nil)
ip = [u32].pack("N").unpack("C4").join(".")
if prefix
- IPAddress::IPv4.new(ip+"/#{prefix}")
+ self.new(ip+"/#{prefix}")
else
- IPAddress::IPv4.new(ip)
+ self.new(ip)
end
end
@@ -639,6 +673,14 @@ module IPAddress;
end
#
+ # Docs here
+ # TODO
+ #
+ def self.extract(str)
+ self.new REGEXP.match(str).to_s
+ end
+
+ #
# Summarization (or aggregation) is the process when two or more
# networks are taken together to check if a supernet, including all
# and only these networks, exists. If it exists then this supernet
@@ -721,13 +763,10 @@ module IPAddress;
return self.summarize(*result)
end
end
-
-
#
# private methods
#
-
private
def bits_from_address(ip)
@@ -738,7 +777,6 @@ module IPAddress;
bits = bits_from_address(ip)
CLASSFUL.each {|reg,prefix| return prefix if bits =~ reg}
end
-
def subnet_even(subnets)
new_prefix = @prefix.to_i + Math::log2(subnets).ceil
@@ -755,7 +793,6 @@ module IPAddress;
networks[-2..-1] = IPAddress::IPv4.summarize(networks[-2],networks[-1])
return networks
end
-
end # class IPv4
end # module IPAddress
diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb
index 01e2f04..997a97e 100644
--- a/lib/ipaddress/ipv6.rb
+++ b/lib/ipaddress/ipv6.rb
@@ -36,12 +36,12 @@ module IPAddress;
raise ArgumentError, "Invalid IP #{ip.inspect}"
end
-# # Check the prefix
-# if netmask =~ /^\d{1,3}$/
-# @prefix = Prefix128.new(netmask.to_i)
-# else
-# @prefix = Prefix128.new(128)
-# end
+ # # Check the prefix
+ # if netmask =~ /^\d{1,3}$/
+ # @prefix = Prefix128.new(netmask.to_i)
+ # else
+ # @prefix = Prefix128.new(128)
+ # end
@prefix = Prefix128.new(netmask ? netmask : 128)
@@ -94,6 +94,10 @@ module IPAddress;
end
alias_method :to_u128, :to_i
+ def network?
+ to_u128 | @prefix.to_u128 == @prefix.to_u128
+ end
+
#
# Returns the 16-bits value specified by index
#
@@ -136,6 +140,10 @@ module IPAddress;
@prefix == 128 and @compressed == "::1"
end
+ def mapped?
+ false
+ end
+
#
# Returns the address portion of an IP in binary format,
# as a string containing a sequence of 0 and 1
@@ -147,7 +155,7 @@ module IPAddress;
def bits
data.unpack("B*").first
end
-
+
#
# Expands an IPv6 address in the canocical form
#
@@ -161,6 +169,10 @@ module IPAddress;
def self.compress(str)
self.new(str).compressed
end
+
+ def literal
+ @address.gsub(":","-") + ".ipv6-literal.net"
+ end
def self.groups(str)
l, r = if str =~ /^(.*)::(.*)$/
@@ -170,7 +182,7 @@ module IPAddress;
end
(l + Array.new(8-l.size-r.size, '0') + r).map {|i| i.hex}
end
-
+
def self.parse_data(str)
self.new(IN6FORMAT % str.unpack("n8"))
end
@@ -184,42 +196,64 @@ module IPAddress;
self.parse_u128(hex.hex, prefix)
end
- private
-
- def compress_address
- str = @groups.map{|i| i.to_s 16}.join ":"
- loop do
- break if str.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::')
- break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
- break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
- break if str.sub!(/\b0:0:0:0:0\b/, ':')
- break if str.sub!(/\b0:0:0:0\b/, ':')
- break if str.sub!(/\b0:0:0\b/, ':')
- break if str.sub!(/\b0:0\b/, ':')
- break
- end
- str.sub(/:{3,}/, '::')
- end
-
+ private
+
+ def compress_address
+ str = @groups.map{|i| i.to_s 16}.join ":"
+ loop do
+ break if str.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::')
+ break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0:0\b/, ':')
+ break if str.sub!(/\b0:0:0\b/, ':')
+ break if str.sub!(/\b0:0\b/, ':')
+ break
+ end
+ str.sub(/:{3,}/, '::')
+ end
+
end # class IPv6
-class IPAddress::IPv6::Unspecified < IPAddress::IPv6
- def initialize
- @address = ("0000:"*8).chop
- @groups = Array.new(8,0)
- @prefix = Prefix128.new(128)
+ class IPAddress::IPv6::Unspecified < IPAddress::IPv6
+ def initialize
+ @address = ("0000:"*8).chop
+ @groups = Array.new(8,0)
+ @prefix = Prefix128.new(128)
+ @compressed = compress_address
+ end # class IPv6::Unspecified
end
-end
-class IPAddress::IPv6::Loopback < IPAddress::IPv6
- def initialize
- @address = ("0000:"*7)+"0001"
- @groups = Array.new(7,0).push(1)
- @prefix = Prefix128.new(128)
- end
-end
+ class IPAddress::IPv6::Loopback < IPAddress::IPv6
+ def initialize
+ @address = ("0000:"*7)+"0001"
+ @groups = Array.new(7,0).push(1)
+ @prefix = Prefix128.new(128)
+ @compressed = compress_address
+ end
+ end # class IPv6::Loopback
+
+ class IPAddress::IPv6::Mapped < IPAddress::IPv6
+
+ attr_reader :ipv4
+
+ def initialize(str)
+ string, netmask = str.split("/")
+ @ipv4 = IPAddress::IPv4.extract(string)
+ super("::FFFF:#{@ipv4.to_ipv6}/#{netmask}")
+ end
-
+ def to_s
+ "::FFFF:#{@ipv4.address}/#@prefix"
+ end
+
+ def mapped?
+ true
+ end
+
+ end # class IPv6::Mapped
+
+
end # module IPAddress