Class: Y2Network::IPAddress

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
src/lib/y2network/ip_address.rb

Overview

This class represents an IP address

The IPAddr from the Ruby standard library drops the host bits according to the netmask. The problem is that YaST uses a CIDR-like string, including the host bits, to set IPADDR in ifcfg-* files (see man 5 ifcfg for further details).

However, what we need is to be able to keep the host part

Examples:

::IPAddr from the standard library behavior

ip = IPAddr.new("192.168.122.1/24")
ip.to_s #=> "192.168.122.0/24"

Y2Network::IPAddress behavior

ip = IPAddress.new("192.168.122.1/24")
ip.to_s #=> "192.168.122.1/24"

IPAddress with no prefix

ip = IPAddress.new("192.168.122.1")
ip.to_s #=> "192.168.122.1"

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, prefix = nil) ⇒ IPAddress

Constructor


69
70
71
72
# File 'src/lib/y2network/ip_address.rb', line 69

def initialize(address, prefix = nil)
  @address = IPAddr.new(address)
  @prefix = prefix
end

Instance Attribute Details

#addressIPAddr


50
51
52
# File 'src/lib/y2network/ip_address.rb', line 50

def address
  @address
end

#prefixInteger


52
53
54
# File 'src/lib/y2network/ip_address.rb', line 52

def prefix
  @prefix
end

Class Method Details

.from_string(str) ⇒ Object


57
58
59
60
61
# File 'src/lib/y2network/ip_address.rb', line 57

def from_string(str)
  address, prefix = str.split("/")
  prefix = prefix.to_i if prefix
  new(address, prefix)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Determines whether two addresses are equivalent


97
98
99
# File 'src/lib/y2network/ip_address.rb', line 97

def ==(other)
  address == other.address && prefix == other.prefix
end

#netmask=(netmask) ⇒ Object

Sets the prefix from a netmask


82
83
84
# File 'src/lib/y2network/ip_address.rb', line 82

def netmask=(netmask)
  self.prefix = IPAddr.new("#{netmask}/#{netmask}").prefix
end

#prefix?Boolean

Determines whether a prefix is defined


106
107
108
# File 'src/lib/y2network/ip_address.rb', line 106

def prefix?
  !!@prefix
end

#to_sObject

Returns a string representation of the address


75
76
77
# File 'src/lib/y2network/ip_address.rb', line 75

def to_s
  prefix? ? "#{@address}/#{@prefix}" : @address.to_s
end