Class: EtherPing::Client
- Inherits:
-
Object
- Object
- EtherPing::Client
- Defined in:
- lib/ether_ping/client.rb
Overview
ether_ping implementation.
Instance Attribute Summary collapse
-
#dest_mac ⇒ Object
readonly
Returns the value of attribute dest_mac.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#source_mac ⇒ Object
readonly
Returns the value of attribute source_mac.
Instance Method Summary collapse
-
#initialize(eth_device, ether_type, destination_mac) ⇒ Client
constructor
A new instance of Client.
-
#ping(data, timeout = 1) ⇒ Object
Pings over raw Ethernet sockets.
Constructor Details
#initialize(eth_device, ether_type, destination_mac) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 |
# File 'lib/ether_ping/client.rb', line 8 def initialize(eth_device, ether_type, destination_mac) @socket = Ethernet.raw_socket eth_device, ether_type @source_mac = Ethernet::Devices.mac eth_device @dest_mac = [destination_mac].pack('H*')[0, 6] @ether_type = [ether_type].pack('n') end |
Instance Attribute Details
#dest_mac ⇒ Object (readonly)
Returns the value of attribute dest_mac.
17 18 19 |
# File 'lib/ether_ping/client.rb', line 17 def dest_mac @dest_mac end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
15 16 17 |
# File 'lib/ether_ping/client.rb', line 15 def socket @socket end |
#source_mac ⇒ Object (readonly)
Returns the value of attribute source_mac.
16 17 18 |
# File 'lib/ether_ping/client.rb', line 16 def source_mac @source_mac end |
Instance Method Details
#ping(data, timeout = 1) ⇒ Object
Pings over raw Ethernet sockets.
Returns a Number representing the ping latency (in seconds) if the ping response matches, an array of [expected, received] strings if it doesn’t match, and false if the ping times out.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ether_ping/client.rb', line 24 def ping(data, timeout = 1) if data.kind_of? Numeric data = "\0" * data end # Pad data to have at least 64 bytes. data += "\0" * (64 - data.length) if data.length < 64 ping_packet = [@dest_mac, @source_mac, @ether_type, data].join response = nil receive_ts = nil send_ts = nil begin Timeout.timeout timeout do send_ts = Time.now @socket.send ping_packet, 0 response = @socket.recv ping_packet.length * 2 receive_ts = Time.now end rescue Timeout::Error response = nil end return false unless response response_packet = [@source_mac, @dest_mac, @ether_type, data].join response == response_packet ? receive_ts - send_ts : [response, response_packet] end |