summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorbluemonk <ceresa@gmail.com>2010-08-24 16:43:50 +0200
committerbluemonk <ceresa@gmail.com>2010-08-24 16:43:50 +0200
commit192d5be9b393392d91c8e4b82f89d79c5150cd60 (patch)
treeedb483cb5907f4a9bcafab07ff166078e0e1ee1d /lib
parent8ab44b9a5c0eada1a84ec1ab77ce6ec66592039b (diff)
downloadipaddress-192d5be9b393392d91c8e4b82f89d79c5150cd60.tar.gz
lots of methods rewriting to improve performances
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddress/ipv4.rb24
-rw-r--r--lib/ipaddress/prefix.rb21
2 files changed, 29 insertions, 16 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index 8c5f0ca..8f80e06 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -93,7 +93,8 @@ module IPAddress;
# Array formed with the IP octets
@octets = @address.split(".").map{|i| i.to_i}
-
+ @u32 = (@octets[0]<< 24) + (@octets[1]<< 16) + (@octets[2]<< 8) + (@octets[3])
+
end # def initialize
#
@@ -231,7 +232,8 @@ module IPAddress;
# #=> 167772160
#
def to_u32
- data.unpack("N").first
+ #data.unpack("N").first
+ @u32
end
alias_method :to_i, :to_u32
@@ -255,7 +257,7 @@ module IPAddress;
# a.puts binary_data
#
def data
- @octets.pack("C4")
+ [@u32].pack("N")
end
#
@@ -316,7 +318,7 @@ module IPAddress;
# #=> true
#
def network?
- to_u32 | @prefix.to_u32 == @prefix.to_u32
+ @u32 | @prefix.to_u32 == @prefix.to_u32
end
#
@@ -507,7 +509,7 @@ module IPAddress;
# #=> 167772160
#
def network_u32
- to_u32 & @prefix.to_u32
+ @u32 & @prefix.to_u32
end
#
@@ -519,7 +521,7 @@ module IPAddress;
# #=> 167772167
#
def broadcast_u32
- [to_u32 | ~@prefix.to_u32].pack("N").unpack("N").first
+ network_u32 + 2**@prefix.host_prefix - 1
end
#
@@ -539,7 +541,7 @@ module IPAddress;
# #=> false
#
def include?(oth)
- @prefix <= oth.prefix and network_u32 == self.class.new(oth.address+"/#@prefix").network_u32
+ @prefix <= oth.prefix and network_u32 == (oth.to_u32 & @prefix.to_u32)
end
#
@@ -746,13 +748,9 @@ module IPAddress;
# ip.to_string
# #=> "10.0.0.0/8"
#
- def self.parse_u32(u32, prefix=nil)
+ def self.parse_u32(u32, prefix=32)
ip = [u32].pack("N").unpack("C4").join(".")
- if prefix
- self.new(ip+"/#{prefix}")
- else
- self.new(ip)
- end
+ self.new(ip+"/#{prefix}")
end
#
diff --git a/lib/ipaddress/prefix.rb b/lib/ipaddress/prefix.rb
index 6463709..2e915d8 100644
--- a/lib/ipaddress/prefix.rb
+++ b/lib/ipaddress/prefix.rb
@@ -83,6 +83,8 @@ module IPAddress
class Prefix32 < Prefix
+ IN4MASK = 0xffffffff
+
#
# Creates a new prefix object for 32 bits IPv4 addresses
#
@@ -91,12 +93,25 @@ module IPAddress
#
def initialize(num)
unless (1..32).include? num
- raise ArgumentError, "Prefix must be in range 1..128, got: #{num}"
+ raise ArgumentError, "Prefix must be in range 1..32, got: #{num}"
end
super(num)
end
#
+ # Returns the length of the host portion
+ # of a netmask.
+ #
+ # prefix = Prefix32.new 24
+ #
+ # prefix.host_prefix
+ # #=> 8
+ #
+ def host_prefix
+ 32 - @prefix
+ end
+
+ #
# Transforms the prefix into a string of bits
# representing the netmask
#
@@ -106,7 +121,7 @@ module IPAddress
# #=> "11111111111111111111111100000000"
#
def bits
- "1" * @prefix + "0" * (32 - @prefix)
+ to_u32.to_s(2)
end
#
@@ -145,7 +160,7 @@ module IPAddress
# #=> 4294967040
#
def to_u32
- [bits].pack("B*").unpack("N").first
+ (IN4MASK >> host_prefix) << host_prefix
end
#