Module: IpMethods

Included in:
SimpleIP
Defined in:
lib/ip_methods.rb,
lib/ip_methods/version.rb

Overview

-*- coding: UTF-8 -*-

Defined Under Namespace

Modules: Version Classes: SimpleIP

Constant Summary collapse

IPV4_PATTERN =
/^(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}$/
IPV4_BIT_WIDTH =
32
IPV4_BIT_OCTET =
8

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_bin(ip_bin) ⇒ Object



9
10
11
# File 'lib/ip_methods.rb', line 9

def self.from_bin(ip_bin)
  "#{ip_bin >> 24}.#{(ip_bin >> 16) & 255}.#{(ip_bin >> 8) & 255}.#{ip_bin & 255}"
end

.ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ip_methods.rb', line 13

def self.ip?(ip)
  ip =~ IPV4_PATTERN
end

.ip_and_mask?(address) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/ip_methods.rb', line 21

def self.ip_and_mask?(address)
  parts = address.to_s.split('/')
  parts.size == 2 && ip?(parts[0]) && mask?(parts[1])
end

.mask?(mask) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ip_methods.rb', line 17

def self.mask?(mask)
  mask.to_s =~ /^\d+$/ && (0..32).include?(mask.to_i)
end

.to_bin(ip) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ip_methods.rb', line 26

def self.to_bin(ip)
  ip_bin = 0
  shift = IPV4_BIT_WIDTH - IPV4_BIT_OCTET
  ip.split('.').each do |part|
    ip_bin += (part.to_i << shift)
    shift  -= IPV4_BIT_OCTET
  end
  ip_bin
end

Instance Method Details

#belongs_to?(range) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/ip_methods.rb', line 84

def belongs_to?(range)
  range = SimpleIP.new(range) unless range.is_a? IpMethods
  range.mask <= self.mask && SimpleIP.new(self.address, range.mask).network == range.network
end

#broadcastObject



48
49
50
# File 'lib/ip_methods.rb', line 48

def broadcast
  IpMethods.from_bin(broadcast_bin)
end

#broadcast_binObject



80
81
82
# File 'lib/ip_methods.rb', line 80

def broadcast_bin
  mask_bin & network_bin | umask_bin;
end

#default_gatewayObject



44
45
46
# File 'lib/ip_methods.rb', line 44

def default_gateway
  IpMethods.from_bin(network_bin+1)
end

#generate_ipsObject



52
53
54
55
56
# File 'lib/ip_methods.rb', line 52

def generate_ips
  ips = []
  (network_bin+1..broadcast_bin-1).each {|ip_bin| ips << IpMethods.from_bin(ip_bin)}
  ips
end

#mask_binObject



62
63
64
65
66
67
# File 'lib/ip_methods.rb', line 62

def mask_bin
  umask = 0
  range = 32 - mask
  (0..mask-1).each {|i| umask += 1 << i}
  umask << range
end

#net_maskObject



40
41
42
# File 'lib/ip_methods.rb', line 40

def net_mask
  IpMethods.from_bin(mask_bin)
end

#networkObject



36
37
38
# File 'lib/ip_methods.rb', line 36

def network
  IpMethods.from_bin(network_bin)
end

#network_binObject



76
77
78
# File 'lib/ip_methods.rb', line 76

def network_bin
  self.to_bin & mask_bin
end

#to_binObject



58
59
60
# File 'lib/ip_methods.rb', line 58

def to_bin
  IpMethods.to_bin(address)
end

#umask_binObject



69
70
71
72
73
74
# File 'lib/ip_methods.rb', line 69

def umask_bin
  umask = 0
  range = 32 - mask
  (0..range-1).each {|i| umask += 1 << i}
  umask
end