Class: PcapParser::Linktype::Ethernet

Inherits:
Object
  • Object
show all
Defined in:
lib/pcap_parser/linktype/ethernet.rb

Overview

Constant Summary collapse

LENGTH =

Ethernet packet length in bytes

14

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Ethernet

Returns a new instance of Ethernet


9
10
11
# File 'lib/pcap_parser/linktype/ethernet.rb', line 9

def initialize(stream)
  @stream = stream
end

Instance Method Details

#ether_typeObject

Ether type class reference.

Returns:

  • (Object)

    see lib/pcap_parser/ethertype.rb. For example Ethernet::IPv4

Raises:


56
57
58
59
60
61
# File 'lib/pcap_parser/linktype/ethernet.rb', line 56

def ether_type
  ether = @stream.little_endian? ? @ether_raw.reverse : @ether_raw
  ethertype = ether.map.with_index { |x, i| x<<(i<<3) }.inject :+
  raise EtherTypeNotSupported if ETHER_TYPE[ethertype].nil?
  ethertype
end

#mac2str(mac) ⇒ String

Convert raw array of chars to human readable mac address.

Parameters:

  • mac (Array)

Returns:

  • (String)

49
50
51
# File 'lib/pcap_parser/linktype/ethernet.rb', line 49

def mac2str(mac)
  mac.map { |o| "%02x" % o }.join ?:
end

#mac_destString

Human readable destination mac address.

Returns:

  • (String)

    mac address like 00:22:33:aa:bb:cc


36
37
38
# File 'lib/pcap_parser/linktype/ethernet.rb', line 36

def mac_dest
  mac2str @dest_mac
end

#mac_dest_rawArray

Destination mac address

Returns:

  • (Array)

    Char array


24
25
26
# File 'lib/pcap_parser/linktype/ethernet.rb', line 24

def mac_dest_raw
  @dest_mac
end

#mac_srcString

Human readable source mac address.

Returns:

  • (String)

    mac address like 00:22:33:aa:bb:cc


42
43
44
# File 'lib/pcap_parser/linktype/ethernet.rb', line 42

def mac_src
  mac2str @src_mac
end

#mac_src_rawArray

Source mac address

Returns:

  • (Array)

    Char array


30
31
32
# File 'lib/pcap_parser/linktype/ethernet.rb', line 30

def mac_src_raw
  @src_mac
end

#readLinktype::Ethernet

Read whole packet structure.

Returns:


15
16
17
18
19
20
# File 'lib/pcap_parser/linktype/ethernet.rb', line 15

def read
  @dest_mac = @stream.read_char(6)
  @src_mac = @stream.read_char(6)
  @ether_raw = @stream.read_char(2)
  self
end