Class: IP::Address::IPv4
- Inherits:
-
IP::Address
- Object
- IP::Address
- IP::Address::IPv4
- Defined in:
- lib/ip/address.rb
Overview
Support for IPv4
Instance Attribute Summary
Attributes inherited from IP::Address
Instance Method Summary collapse
-
#initialize(ip_address) ⇒ IPv4
constructor
Constructs an IP::Address::IPv4 object.
-
#pack ⇒ Object
Returns a 128-bit integer representing the address.
Methods inherited from IP::Address
Constructor Details
#initialize(ip_address) ⇒ IPv4
Constructs an IP::Address::IPv4 object.
This can take two types of input. Either a string that contains a dotted-quad formatted address, or an integer that contains the data. This integer is expected to be constructed in the way that IP::Address::Util.pack_ipv4 would generate such an integer.
This constructor will throw IP::AddressException on any parse errors.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ip/address.rb', line 57 def initialize(ip_address) if ip_address.kind_of? Integer # unpack to generate a string, and parse that. # overwrites 'ip_address' # horribly inefficient, but general. raw = IP::Address::Util.raw_unpack(ip_address)[0..1] octets = [] 2.times do |x| octets.push(raw[x] & 0x00FF) octets.push((raw[x] & 0xFF00) >> 8) end ip_address = octets.reverse.join(".") end if ! ip_address.kind_of? String raise IP::AddressException.new("Fed IP address '#{ip_address}' is not String or Fixnum") end @ip_address = ip_address # # Unbeknowest by me, to_i will not throw an exception if the string # can't be converted cleanly - it just truncates, similar to atoi() and perl's int(). # # Code below does a final sanity check. # octets = ip_address.split(/\./) octets_i = octets.collect { |x| x.to_i } 0.upto(octets.length - 1) do |octet| if octets[octet] != octets_i[octet].to_s raise IP::AddressException.new("Integer conversion failed") end end @octets = octets_i # I made a design decision to allow 0.0.0.0 here. if @octets.length != 4 or @octets.find_all { |x| x > 255 }.length > 0 raise IP::AddressException.new("IP address is improperly formed") end end |
Instance Method Details
#pack ⇒ Object
Returns a 128-bit integer representing the address.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ip/address.rb', line 107 def pack # this routine does relatively little. all it does is ensure # that the IP address is of a certain size and has certain numeric limits. myip = self.octets packval = [0] * 6 # # this ensures that the octets are 8 bit, and combines the octets in order to # form two 16-bit integers suitable for pushing into the last places in 'packval' # (0..3).step(2) { |x| packval.push(((myip[x] & 0xFF) << 8) | (myip[x+1] & 0xFF)) } return IP::Address::Util.raw_pack(packval) end |