Class: Y2Network::IPAddress

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Yast2::Equatable
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

Parameters:

  • address (String)

    IP address without the prefix

  • prefix (Integer) (defaults to: nil)

    IP prefix (number of bits). If not specified, 32 will be used for IPv4 and 128 for IPv6.



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

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

Instance Attribute Details

#addressIPAddr

Returns IP address.

Returns:

  • (IPAddr)

    IP address



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

def address
  @address
end

#prefixInteger

Returns Prefix.

Returns:

  • (Integer)

    Prefix



54
55
56
# File 'src/lib/y2network/ip_address.rb', line 54

def prefix
  @prefix
end

Class Method Details

.from_string(str) ⇒ Object



61
62
63
64
65
# File 'src/lib/y2network/ip_address.rb', line 61

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

Instance Method Details

#netmask=(netmask) ⇒ Object

Sets the prefix from a netmask

Parameters:

  • netmask (String)

    String representation of the netmask



86
87
88
# File 'src/lib/y2network/ip_address.rb', line 86

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

#prefix?Boolean

Determines whether a prefix is defined

Returns:

  • (Boolean)


100
101
102
# File 'src/lib/y2network/ip_address.rb', line 100

def prefix?
  !!@prefix
end

#to_sObject

Returns a string representation of the address



79
80
81
# File 'src/lib/y2network/ip_address.rb', line 79

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