Class: EtherPing::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ether_ping/server.rb

Overview

Responder for ping utility using raw Ethernet sockets.

Instance Method Summary collapse

Constructor Details

#initialize(eth_device, ether_type, verbose = true) ⇒ Server

Returns a new instance of Server.



29
30
31
32
# File 'lib/ether_ping/server.rb', line 29

def initialize(eth_device, ether_type, verbose = true)
  @socket = Ethernet.raw_socket eth_device, ether_type
  @verbose = verbose
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ether_ping/server.rb', line 10

def run
  loop do
    packet = @socket.recv 65536
    # Respond afap: exchange the source and destination MAC addresses.
    packet[0, 6], packet[6, 6] = packet[6, 6], packet[0, 6]
    @socket.send packet, 0

    if @verbose
      # The MAC addresses were switched above, before responding.
      dest_mac = packet[6, 6].unpack('H*').first
      source_mac = packet[0, 6].unpack('H*').first
      ether_type = packet[12, 2].unpack('H*').first
        
      puts "Src: #{source_mac} Dst: #{dest_mac} Eth: #{ether_type} Data:\n"
      puts packet[14..-1].unpack('H*').first
    end
  end
end