Class: PacketGen::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/packetgen/config.rb

Overview

Config class to provide config object to pgconsole

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iface = nil) ⇒ Config

Create a configuration object. If iface is not set, attempt to find it automatically or default to the first available loopback interface.

Parameters:

  • iface (String, nil) (defaults to: nil)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/packetgen/config.rb', line 28

def initialize(iface=nil)
  if iface.nil?
    begin
      iface = Pcap.lookupdev
    rescue PCAPRUB::BindingError
      iface = NetworkInterface.interfaces.select { |iface| iface =~ /lo/ }.first
    end
  end
  @iface = iface

  addresses = NetworkInterface.addresses(iface)
  @hwaddr = case RbConfig::CONFIG['target_os']
            when /darwin/
              addresses[Socket::AF_LINK][0]['addr'] if addresses[Socket::AF_LINK]
            else
              addresses[Socket::AF_PACKET][0]['addr'] if addresses[Socket::AF_PACKET]
            end
  @ipaddr = addresses[Socket::AF_INET][0]['addr'] if addresses[Socket::AF_INET]
  @ip6addr = addresses[Socket::AF_INET6][0]['addr'] if addresses[Socket::AF_INET6]
end

Instance Attribute Details

#hwaddrString (readonly)

MAC address of default interface

Returns:

  • (String)


16
17
18
# File 'lib/packetgen/config.rb', line 16

def hwaddr
  @hwaddr
end

#ifaceString (readonly)

Default network interface

Returns:

  • (String)


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

def iface
  @iface
end

#ip6addrString (readonly)

IPv6 address of default interface

Returns:

  • (String)


22
23
24
# File 'lib/packetgen/config.rb', line 22

def ip6addr
  @ip6addr
end

#ipaddrString (readonly)

IP address of default interface

Returns:

  • (String)


19
20
21
# File 'lib/packetgen/config.rb', line 19

def ipaddr
  @ipaddr
end