Skip to content

Instantly share code, notes, and snippets.

@picatz
Created November 16, 2017 01:35
Show Gist options
  • Save picatz/22e8678b9e39966b45b8bd14055af043 to your computer and use it in GitHub Desktop.
Save picatz/22e8678b9e39966b45b8bd14055af043 to your computer and use it in GitHub Desktop.
Using pcap_open_live
# previous code ...
err = LibPcap::PCAP_ERRBUF_SIZE.dup
cap = LibPcap.pcap_open_live("en0", 65535, 0, 1, pointerof(err))
raise Exception.new("An error happened: #{String.new(err)}") if cap.null?
loop do
result = LibPcap.pcap_next_ex(cap, out pkt_header, out pkt_data)
case result
when 1 # success
puts pkt_data.to_slice(pkt_header.value.caplen).hexdump
when 0 # timeout
next
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment