Module: IP::Address::Util

Defined in:
lib/ip/util.rb

Overview

General utility functions used by other IP classes.

Class Method Summary collapse

Class Method Details

.long_netmask(short) ⇒ Object



108
109
110
111
# File 'lib/ip/util.rb', line 108

def long_netmask(short)
  warn "This function is deprecated, please use IP::Address::Util.long_netmask_ipv4 instead."
  return long_netmask_ipv4(short)
end

.long_netmask_ipv4(short) ⇒ Object

Given a CIDR-notation “short” netmask, returns a IP::Address object containing the equivalent “long” netmask.

ex: long_netmask(32) => IP::Address object of “255.255.255.255” long_netmask(28) => IP::Address object of “255.255.255.240”



124
125
126
127
# File 'lib/ip/util.rb', line 124

def long_netmask_ipv4(short)
  raw = (0xFFFFFFFF << (32 - short)) & 0xFFFFFFFF
  return IP::Address::Util.unpack(raw)
end

.pack(ip) ⇒ Object

Pack an object into a long integer used for calculation. Returns a ‘FixNum’ type. Can take both IP::Address::IPv4 and IP::Address::IPv6 objects.



12
13
14
# File 'lib/ip/util.rb', line 12

def pack(ip)
  return ip.pack
end

.raw_pack(array) ⇒ Object

This routine takes an array of integers which are intended to be an IP address, and joins them into a single integer used for easy calculation.

IPv4 and IPv6 objects are packed into the same size, a 128-bit integer. This is done for easier processing within the library.



27
28
29
30
31
32
# File 'lib/ip/util.rb', line 27

def raw_pack(array)
  ret = 0
  myip = array.reverse
  8.times { |x| ret = ret | myip[x] << 16*x }
  return ret
end

.raw_unpack(ip) ⇒ Object

Take a ‘FixNum’ and return it’s in-place octet representation. This is mostly a helper method for the unpack routines.



66
67
68
69
70
# File 'lib/ip/util.rb', line 66

def raw_unpack(ip)
  ret = []
  8.times { |x| ret.push((ip >> 16*x) & 0xFFFF) }
  return ret
end

.short_netmask(ip) ⇒ Object

Returns a short subnet mask - works for all IP::Address objects.

ex: short_netmask(IP::Address::IPv4.new(“255.255.255.255”)) => 32 short_netmask(IP::Address::IPv6.new(“2001:0DB8:0000:CD30:0000:0000:0000:0000”)) => 60



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ip/util.rb', line 82

def short_netmask(ip)
  #
  # This method handles 128-bit integers better for both types of
  # addresses, even though it is a bit slower.
  #
  # TODO: there really is probably a better way to do this.
  #

  s = ip.pack.to_s(2)

  pad = 0

  case ip.class.object_id
  when IP::Address::IPv6.object_id
    pad = 128
  when IP::Address::IPv4.object_id
    pad = 32
  end

  s = ("0" * (pad - s.length)) + s

  return s.rindex("1") + 1
end

.string_to_ip(s) ⇒ Object

This takes a string which supposedly contains an IP address, and tries to figure out if it is a IPv6 or IPv4 address. Returns a constructed object of the proper type.



137
138
139
140
# File 'lib/ip/util.rb', line 137

def string_to_ip(s)
  return IP::Address::IPv6.new(s) if s.match(/:/)
  return IP::Address::IPv4.new(s)
end

.unpack(ip) ⇒ Object

Take an ‘Integer’ type and return an IP::Address object.

This routine will ‘guess’ at what version of IP addressing you want, returning the oldest type possible (IPv4 addresses will return IP::Address::IPv4 objects).

In almost all cases, you’ll want to use the type-specific routines, which merely involve passing an Integer to the constructor for each class.



47
48
49
50
51
52
53
54
55
56
# File 'lib/ip/util.rb', line 47

def unpack(ip)
  ret = raw_unpack(ip)

  # any IPv6 address should meet this criteria.
  if ret[2..8].any? { |x| x > 0 }
    return IP::Address::IPv6.new(ip)
  end

  return IP::Address::IPv4.new(ip)
end