Skip to content

Commit

Permalink
Added ARP packet layout, changed print_bytes example to require pcap_…
Browse files Browse the repository at this point in the history
…ffi instead of pcap
  • Loading branch information
dakrone committed Jun 4, 2009
1 parent 326b7da commit ec65005
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/print_bytes.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'pcap'
require 'pcap_ffi'

include FFI

Expand Down
111 changes: 111 additions & 0 deletions lib/pcap_ffi/packets/arp.rb
@@ -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

0 comments on commit ec65005

Please sign in to comment.