Class: LifxDash::Capturer

Inherits:
Object
  • Object
show all
Defined in:
lib/lifx_dash/capturer.rb

Constant Summary collapse

PACKET_FILTER =

Berkeley Packet filter for Amazon Dash buttons

- most dash buttons broadcast a DHCP request from 0.0.0.0
- for more details see https://github.com/ide/dash-button/issues/36
"udp and src port 68 and dst port 67 and udp[247:4] == 0x63350103 and src host 0.0.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network_iface_id) ⇒ Capturer

Returns a new instance of Capturer


15
16
17
# File 'lib/lifx_dash/capturer.rb', line 15

def initialize(network_iface_id)
  @iface = network_iface_id
end

Instance Attribute Details

#ifaceObject (readonly)

Returns the value of attribute iface


13
14
15
# File 'lib/lifx_dash/capturer.rb', line 13

def iface
  @iface
end

Instance Method Details

#listen(&block) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lifx_dash/capturer.rb', line 19

def listen(&block)
  # examine packets on the stream
  capturer.stream.each do |packet|
    if PacketFu::IPPacket.can_parse?(packet)
      pkt = PacketFu::IPPacket.parse(packet)
      # only consider the first (early ip ids) even packet sent
      # since dhcp often sends 2 packets in a quick burst
      if pkt && pkt.ip_id < 5 && (pkt.ip_id % 2) == 0
        puts pkt.inspect
        mac = PacketFu::EthHeader.str2mac(pkt.eth_src)
        block.call(pkt, mac) if block
      end
    end
  end
end