Skip to content

Commit

Permalink
Cleaned up style and some rough code.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Jul 24, 2012
1 parent 2b98718 commit f692da3
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 73 deletions.
14 changes: 9 additions & 5 deletions lib/ffi/pcap/bpf_program.rb
@@ -1,4 +1,5 @@
require 'ffi/pcap/bpf_instruction'
require 'ffi/pcap/data_link'

module FFI
module PCap
Expand All @@ -18,7 +19,8 @@ class BPFProgram < FFI::Struct

def instructions
i = 0
sz = BPFInstruction.size()
sz = BPFInstruction.size

Array.new(self.bf_len) do
ins = BPFInstruction.new( self[:bf_insn] + i )
i += sz
Expand All @@ -29,12 +31,12 @@ def instructions
def free!
unless @closed
@freed = true
FFI::PCap.pcap_freecode(self)
PCap.pcap_freecode(self)
end
end

def freed?
return @freed == true
@freed == true
end

#
Expand Down Expand Up @@ -70,8 +72,10 @@ def self.compile(expr, opts={})
slen = (opts[:snaplen] || DEFAULT_SNAPLEN)
optimize = (opts[:optimize] || 1)
mask = (opts[:netmask] || 0)
code = BPFProgram.new()
r = FFI::PCap.pcap_compile_nopcap(slen, dl.value, code, expr, optimize, mask)

code = new()
r = PCap.pcap_compile_nopcap(slen, dl.value, code, expr, optimize, mask)

raise(LibError, "pcap_compile_nopcap(): unspecified error") if r < 0
return code
end
Expand Down
11 changes: 4 additions & 7 deletions lib/ffi/pcap/capture_wrapper.rb
Expand Up @@ -40,11 +40,7 @@ class CaptureWrapper < CommonWrapper
# retain a reference to them elsewhere.
#
def initialize(pcap, opts={}, &block)
@handler = if opts.has_key?(opts[:handler])
opts[:handler]
else
CopyHandler
end
@handler = opts.fetch(handler,CopyHandler)

trap('INT') do
stop()
Expand All @@ -56,6 +52,7 @@ def initialize(pcap, opts={}, &block)
trap('TERM') do
stop()
close()

raise(SignalException,'TERM',caller)
end

Expand Down Expand Up @@ -164,6 +161,7 @@ def dispatch(opts={}, &block)
h = opts[:handler]

ret = PCap.pcap_dispatch(_pcap, cnt, _wrap_callback(h, block),nil)

if ret == -1
raise(ReadError,"pcap_dispatch(): #{geterr}",caller)
elsif ret -2
Expand Down Expand Up @@ -212,7 +210,7 @@ def next
hdr_p = MemoryPointer.new(:pointer)
buf_p = MemoryPointer.new(:pointer)

case FFI::PCap.pcap_next_ex(_pcap, hdr_p, buf_p)
case PCap.pcap_next_ex(_pcap, hdr_p, buf_p)
when -1 # error
raise(ReadError,"pcap_next_ex(): #{geterr}",caller)
when 0 # live capture read timeout expired
Expand Down Expand Up @@ -327,6 +325,5 @@ def _wrap_callback(h, block)
attach_function :pcap_breakloop, [:pcap_t], :void
attach_function :pcap_setfilter, [:pcap_t, BPFProgram], :int
attach_function :pcap_fileno, [:pcap_t], :int

end
end
18 changes: 9 additions & 9 deletions lib/ffi/pcap/common_wrapper.rb
Expand Up @@ -11,9 +11,9 @@ class CommonWrapper
attr_accessor :pcap

def initialize(pcap, opts={})
@pcap = pcap
@closed = false
@errbuf ||= ErrorBuffer.create
@pcap = pcap
@closed = false
@errbuf ||= ErrorBuffer.new

yield self if block_given?
end
Expand All @@ -22,16 +22,16 @@ def initialize(pcap, opts={})
# Returns the DataLink for the pcap device.
#
def datalink
@datalink ||= DataLink.new(FFI::PCap.pcap_datalink(_pcap))
@datalink ||= DataLink.new(PCap.pcap_datalink(_pcap))
end

#
# Returns an array of supported DataLinks for the pcap device.
#
def supported_datalinks
dlt_lst = FFI::MemoryPointer.new(:pointer)
dlt_lst = MemoryPointer.new(:pointer)

if (cnt=FFI::PCap.pcap_list_datalinks(_pcap, dlt_lst)) < 0
if (cnt = PCap.pcap_list_datalinks(_pcap, dlt_lst)) < 0
raise(LibError, "pcap_list_datalinks(): #{geterr}",caller)
end

Expand Down Expand Up @@ -82,7 +82,7 @@ def to_ptr
# @return [Integer]
# Snapshot length for the pcap interface.
def snaplen
FFI::PCap.pcap_snapshot(_pcap)
PCap.pcap_snapshot(_pcap)
end

#
Expand Down Expand Up @@ -130,7 +130,7 @@ def compile(expression, opts={})
# message from libpcap.
#
def open_dump(path)
dp = FFI::PCap.pcap_dump_open(_pcap, File.expand_path(path))
dp = PCap.pcap_dump_open(_pcap, File.expand_path(path))

if dp.null?
raise(LibError,"pcap_dump_open(): #{geterr}",caller)
Expand All @@ -144,7 +144,7 @@ def open_dump(path)
# The error text pertaining to the last pcap library error.
#
def geterr
FFI::PCap.pcap_geterr(_pcap)
PCap.pcap_geterr(_pcap)
end

alias error geterr
Expand Down
4 changes: 2 additions & 2 deletions lib/ffi/pcap/copy_handler.rb
Expand Up @@ -21,7 +21,7 @@ module PCap
#
class CopyHandler
def receive_pcap(pcap, pkt)
return [pcap, pkt.copy]
[pcap, pkt.copy]
end
end

Expand All @@ -31,7 +31,7 @@ def receive_pcap(pcap, pkt)
#
class Handler
def receive_pcap(*args)
return args
args
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/ffi/pcap/crt.rb
Expand Up @@ -5,8 +5,6 @@ module CRT

ffi_lib FFI::Library::LIBC

typedef :ulong, :size_t # not all platforms have this set for FFI

attach_function :free, [:pointer], :void
attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer
end
Expand Down
16 changes: 8 additions & 8 deletions lib/ffi/pcap/data_link.rb
Expand Up @@ -121,11 +121,12 @@ def self.describe(l)
# value fails or if the arg parameter is an invalid type.
#
def initialize(arg)
if (arg.kind_of?(String) || arg.kind_of?(Symbol))
case arg
when String, Symbol
unless (@value = self.class.name_to_val(arg.to_s))
raise(UnsupportedDataLinkError, "Invalid DataLink: #{arg.to_s}")
end
elsif arg.kind_of?(Numeric)
when Numeric
@value = arg
else
raise(UnsupportedDataLinkError,"Invalid DataLink: #{arg.inspect}",caller)
Expand All @@ -141,15 +142,15 @@ def initialize(arg)
def ==(other)
case other
when DataLink
return (self.value == other.value)
self.value == other.value
when Numeric
return (self.value == other)
self.value == other
when Symbol
return (@value == self.class.name_to_val(other.to_s))
@value == self.class.name_to_val(other.to_s)
when String
return (@value == self.class.name_to_val(other))
@value == self.class.name_to_val(other)
else
return false
false
end
end

Expand Down Expand Up @@ -193,6 +194,5 @@ def inspect
attach_function :pcap_datalink_name_to_val, [:string], :int
attach_function :pcap_datalink_val_to_name, [:int], :string
attach_function :pcap_datalink_val_to_description, [:int], :string

end
end
4 changes: 2 additions & 2 deletions lib/ffi/pcap/dead.rb
@@ -1,4 +1,5 @@
require 'ffi/pcap/common_wrapper'
require 'ffi/pcap/data_link'

module FFI
module PCap
Expand Down Expand Up @@ -33,7 +34,7 @@ def initialize(opts={}, &block)

@datalink = dl.kind_of?(DataLink) ? dl : DataLink.new(dl)
@snaplen = opts[:snaplen] || DEFAULT_SNAPLEN
@pcap = FFI::PCap.pcap_open_dead(@datalink.value, @snaplen)
@pcap = PCap.pcap_open_dead(@datalink.value, @snaplen)

if @pcap.null?
raise(LibError,"pcap_open_dead(): returned a null pointer",caller)
Expand All @@ -45,6 +46,5 @@ def initialize(opts={}, &block)
end

attach_function :pcap_open_dead, [:int, :int], :pcap_t

end
end
10 changes: 4 additions & 6 deletions lib/ffi/pcap/dumper.rb
Expand Up @@ -4,7 +4,7 @@ module PCap
# See `pcap_dumper_t` in `pcap.h`.
#
# A `pcap_dumper`, {Dumper} is handled opaquely so that it can
# be implemented differently on different platforms. In {FFI::PCap}, we
# be implemented differently on different platforms. In {PCap}, we
# simply wrap the `pcap_dumper_t` pointer with a ruby interface.
#
class Dumper
Expand All @@ -18,10 +18,9 @@ def _write(header, bytes)
end

def write(*args)
if args.first.is_a? Packet
write_pkt(*args)
else
_write(*args)
case args.first
when Packet then write_pkt(*args)
else _write(*args)
end
end

Expand Down Expand Up @@ -50,6 +49,5 @@ def close
attach_function :pcap_dump_flush, [:pcap_dumper_t], :int
attach_function :pcap_dump_close, [:pcap_dumper_t], :void
attach_function :pcap_dump, [:pointer, PacketHeader, :pointer], :void

end
end
4 changes: 2 additions & 2 deletions lib/ffi/pcap/live.rb
Expand Up @@ -73,8 +73,8 @@ def initialize(opts=nil)
@timeout = opts[:timeout] || DEFAULT_TO_MS
@direction = (opts[:direction] || opts[:dir])

@errbuf = ErrorBuffer.create()
@pcap = PCap.pcap_open_live(@device, @snaplen, @promisc, @timeout, @errbuf)
@errbuf = ErrorBuffer.new
@pcap = PCap.pcap_open_live(@device, @snaplen, @promisc, @timeout, @errbuf)

if @pcap.null?
raise(LibError, "pcap_open_live(): #{@errbuf}",caller)
Expand Down
7 changes: 3 additions & 4 deletions lib/ffi/pcap/offline.rb
Expand Up @@ -31,9 +31,9 @@ class Offline < CaptureWrapper
# message from libpcap.
#
def initialize(path, opts={}, &block)
@path = path
@errbuf = ErrorBuffer.create()
@pcap = PCap.pcap_open_offline(File.expand_path(@path), @errbuf)
@path = path
@errbuf = ErrorBuffer.new
@pcap = PCap.pcap_open_offline(File.expand_path(@path), @errbuf)

if @pcap.null?
raise(LibError,"pcap_open_offline(): #{@errbuf}",caller)
Expand All @@ -56,6 +56,5 @@ def file_version
attach_function :pcap_is_swapped, [:pcap_t], :int
attach_function :pcap_major_version, [:pcap_t], :int
attach_function :pcap_minor_version, [:pcap_t], :int

end
end
11 changes: 6 additions & 5 deletions lib/ffi/pcap/packet.rb
@@ -1,10 +1,11 @@
require 'ffi/pcap/packet_header'

module FFI
module PCap
class Packet

attr_reader :body_ptr, :header


# Unmarshall a marshalled {Packet}
def self._load(s)
time, body = Marshal.load(s)
Expand Down Expand Up @@ -122,13 +123,13 @@ def set_body(data, opts={})
cl = (opts[:caplen] || opts[:captured] || data.size)
l = (opts[:length] || opts[:len] || cl)

clen = (cl < data.size) ? cl : data.size
len = (l < clen) ? clen : l
clen = [cl, data.size].min
len = [l, clen].max

@header ||= PacketHeader.new
@header.caplen = len || @header.caplen
@header.len = len || @header.caplen
@body_ptr = FFI::MemoryPointer.from_string(data)
@body_ptr = MemoryPointer.from_string(data)
return self
end

Expand Down Expand Up @@ -158,7 +159,7 @@ def time
# Sets the pcap timestamp.
#
def time=(t)
@header.ts.time=(t)
@header.ts.time = t
end

def caplen
Expand Down

0 comments on commit f692da3

Please sign in to comment.