forked from postmodern/pcap-ffi
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ARP packet layout, changed print_bytes example to require pcap_…
…ffi instead of pcap
- Loading branch information
dakrone
committed
Jun 4, 2009
1 parent
326b7da
commit ec65005
Showing
2 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'rubygems' | ||
require 'pcap' | ||
require 'pcap_ffi' | ||
|
||
include FFI | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require 'pcap_ffi/packets/typedefs' | ||
require 'pcap_ffi/mac_addr' | ||
require 'pcap_ffi/in_addr' | ||
require 'pcap_ffi/packet' | ||
|
||
require 'ffi' | ||
|
||
module FFI | ||
module PCap | ||
module Packets | ||
# Note that ARP only supports Ethernet, ipv4 right now | ||
class Arp < FFI::Struct | ||
|
||
include Packet | ||
|
||
HW_ETHERNET = 0x0001 | ||
PROTCOL_IP = 0x0002 | ||
OP_REQUEST = 0x0001 | ||
OP_REPLY = 0x0002 | ||
|
||
layout :h_type, :uint16, | ||
:p_type, :uint16, | ||
:h_len, :uint8, | ||
:p_len, :uint8, | ||
:operation, :uint16, | ||
:sender_mac, MACAddr, | ||
:sender_ip, InAddr, | ||
:dest_mac, MACAddr, | ||
:dest_ip, InAddr | ||
|
||
# | ||
# Returns ARP hardware type | ||
# | ||
def hardware_type | ||
self[:h_type].to_endian(:big) | ||
end | ||
|
||
# | ||
# Returns ARP protocol type | ||
# | ||
def protocol_type | ||
self[:p_type].to_endian(:big) | ||
end | ||
|
||
# | ||
# Returns true if ARP request hardware type is Ethernet | ||
# | ||
def hardware_ethernet? | ||
self.hardware_type == HW_ETHERNET | ||
end | ||
|
||
# | ||
# Returns true if ARP request protocol type is IP | ||
# | ||
def protocol_ip? | ||
self.protocol_type == PROTOCOL_IP | ||
end | ||
|
||
# | ||
# Returns the ARP operation | ||
# | ||
def operation | ||
self[:operation].to_endian(:big) | ||
end | ||
|
||
# | ||
# Returns true if it was an ARP request operation | ||
# | ||
def request? | ||
self.operation == OP_REQUEST | ||
end | ||
|
||
# | ||
# Returns true if it was an ARP reply operation | ||
# | ||
def reply? | ||
self.operation == OP_REPLY | ||
end | ||
|
||
# | ||
# Returns the source MAC address. | ||
# | ||
def src_mac | ||
self[:sender_mac] | ||
end | ||
|
||
# | ||
# Returns the destination MAC address. | ||
# | ||
def dest_mac | ||
self[:dest_mac] | ||
end | ||
|
||
# | ||
# Returns the source IP address. | ||
# | ||
def src_ip | ||
self[:sender_ip] | ||
end | ||
|
||
# | ||
# Returns the destination IP address. | ||
# | ||
def dest_ip | ||
self[:dest_ip] | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |