Class: Ifconf::NetworkInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/ifconf/network_interface.rb

Overview

Represents a single parsed network interface with its flags, addresses, link layer, and statistics.

Constant Summary collapse

NAME_PATTERN =
/\A[a-zA-Z0-9.:_-]+\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ NetworkInterface

Returns a new instance of NetworkInterface.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ifconf/network_interface.rb', line 19

def initialize(**attrs)
  name = attrs.fetch(:name)
  raise InvalidInterfaceName, name unless name.is_a?(String) && name.match?(NAME_PATTERN)

  @name = name
  @flags = attrs.fetch(:flags)
  @link_layer = attrs.fetch(:link_layer)
  @ipv4_config = attrs.fetch(:ipv4_config)
  @ipv6_configs = attrs.fetch(:ipv6_configs).freeze
  @statistics = attrs.fetch(:statistics)
  freeze
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



6
7
8
# File 'lib/ifconf/network_interface.rb', line 6

def flags
  @flags
end

#ipv4_configObject (readonly)

Returns the value of attribute ipv4_config.



6
7
8
# File 'lib/ifconf/network_interface.rb', line 6

def ipv4_config
  @ipv4_config
end

#ipv6_configsObject (readonly)

Returns the value of attribute ipv6_configs.



6
7
8
# File 'lib/ifconf/network_interface.rb', line 6

def ipv6_configs
  @ipv6_configs
end

Returns the value of attribute link_layer.



6
7
8
# File 'lib/ifconf/network_interface.rb', line 6

def link_layer
  @link_layer
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ifconf/network_interface.rb', line 6

def name
  @name
end

#statisticsObject (readonly)

Returns the value of attribute statistics.



6
7
8
# File 'lib/ifconf/network_interface.rb', line 6

def statistics
  @statistics
end

Class Method Details

.build(name:, flags:, link_layer:, statistics:, **options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/ifconf/network_interface.rb', line 8

def self.build(name:, flags:, link_layer:, statistics:, **options)
  new(
    name: name,
    flags: flags,
    link_layer: link_layer,
    ipv4_config: options[:ipv4_config] || NullIpv4Config.new,
    ipv6_configs: options.fetch(:ipv6_configs, []),
    statistics: statistics,
  )
end

Instance Method Details

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



107
108
109
# File 'lib/ifconf/network_interface.rb', line 107

def ==(other)
  other.is_a?(NetworkInterface) && @name == other.name
end

#all_addressesObject



56
57
58
59
60
61
# File 'lib/ifconf/network_interface.rb', line 56

def all_addresses
  addrs = []
  addrs << @ipv4_config.address if @ipv4_config.present?
  addrs.concat(ipv6_addresses)
  addrs
end

#encapsulationObject



67
68
69
# File 'lib/ifconf/network_interface.rb', line 67

def encapsulation
  @link_layer.encapsulation
end

#error_rateObject



79
80
81
# File 'lib/ifconf/network_interface.rb', line 79

def error_rate
  @statistics.error_rate
end

#hashObject



113
114
115
# File 'lib/ifconf/network_interface.rb', line 113

def hash
  @name.hash
end

#healthy?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ifconf/network_interface.rb', line 75

def healthy?
  @statistics.healthy?
end

#inspectObject



103
104
105
# File 'lib/ifconf/network_interface.rb', line 103

def inspect
  "#<#{self.class} name=#{@name}>"
end

#ipv4_addressObject



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

def ipv4_address
  @ipv4_config.address
end

#ipv6_addressesObject



52
53
54
# File 'lib/ifconf/network_interface.rb', line 52

def ipv6_addresses
  @ipv6_configs.map(&:address)
end

#loopback?Boolean

Returns:

  • (Boolean)


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

def loopback?
  @flags.loopback?
end

#mac_addressObject



63
64
65
# File 'lib/ifconf/network_interface.rb', line 63

def mac_address
  @link_layer.hardware_addr
end

#mtuObject



71
72
73
# File 'lib/ifconf/network_interface.rb', line 71

def mtu
  @flags.mtu
end

#operational?Boolean

Returns:

  • (Boolean)


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

def operational?
  @flags.operational?
end

#owns_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/ifconf/network_interface.rb', line 87

def owns_ip?(ip)
  ip_str = ip.to_s
  return true if @ipv4_config.includes_ip?(ip_str)

  @ipv6_configs.any? { |c| c.includes_ip?(ip_str) }
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  @flags.running?
end

#subnet_sizeObject



94
95
96
97
# File 'lib/ifconf/network_interface.rb', line 94

def subnet_size
  count = @ipv4_config.host_count
  count.zero? ? nil : count
end

#to_sObject



99
100
101
# File 'lib/ifconf/network_interface.rb', line 99

def to_s
  @name
end

#total_trafficObject



83
84
85
# File 'lib/ifconf/network_interface.rb', line 83

def total_traffic
  @statistics.total_bytes
end

#up?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ifconf/network_interface.rb', line 32

def up?
  @flags.up?
end