Class: FFI::PCap::Handler

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pcap/handler.rb

Constant Summary collapse

SNAPLEN =

Default snaplen

65535

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pcap, options = {}, &block) ⇒ Handler

Returns a new instance of Handler.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pcap/handler.rb', line 25

def initialize(pcap,options={},&block)
  @pcap = pcap
  @closed = false

  # Default is to infinitely loop over packets.
  @count = (options[:count] || -1)

  if options[:direction]
    self.direction = options[:direction]
  end

  @callback_wrapper = Proc.new do |user,header,bytes|
    if @callback
      @callback.call(user,PacketHeader.new(header),bytes)
    end
  end

  callback(&block)

  trap('SIGINT') { self.close }
end

Instance Attribute Details

#countObject

Number of packets to sniff



23
24
25
# File 'lib/pcap/handler.rb', line 23

def count
  @count
end

#pcapObject (readonly)

Pointer to the pcap opaque type



20
21
22
# File 'lib/pcap/handler.rb', line 20

def pcap
  @pcap
end

Instance Method Details

#callback(&block) ⇒ Object



51
52
53
54
# File 'lib/pcap/handler.rb', line 51

def callback(&block)
  @callback = block
  return @callback
end

#closeObject



153
154
155
156
157
158
# File 'lib/pcap/handler.rb', line 153

def close
  unless @closed
    @closed = true
    PCap.pcap_close(@pcap)
  end
end

#closed?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/pcap/handler.rb', line 149

def closed?
  @closed == true
end


47
48
49
# File 'lib/pcap/handler.rb', line 47

def datalink
  DataLink.new(PCap.pcap_datalink(@pcap))
end

#direction=(dir) ⇒ Object



56
57
58
59
60
# File 'lib/pcap/handler.rb', line 56

def direction=(dir)
  directions = PCap.enum_type(:pcap_direction)

  return PCap.pcap_setdirection(@pcap,directions[:"pcap_d_#{dir}"])
end

#dispatch(data = nil, &block) ⇒ Object



96
97
98
99
100
# File 'lib/pcap/handler.rb', line 96

def dispatch(data=nil,&block)
  callback(&block) if block

  return PCap.pcap_dispatch(@pcap,@count,@callback_wrapper,data)
end

#errorObject



141
142
143
# File 'lib/pcap/handler.rb', line 141

def error
  PCap.pcap_geterr(@pcap)
end

#inspectObject



164
165
166
# File 'lib/pcap/handler.rb', line 164

def inspect
  "#<#{self.class}: 0x#{@pcap.address.to_s(16)}>"
end

#loop(data = nil, &block) ⇒ Object Also known as: each



88
89
90
91
92
# File 'lib/pcap/handler.rb', line 88

def loop(data=nil,&block)
  callback(&block) if block

  PCap.pcap_loop(@pcap,@count,@callback_wrapper,data)
end

#nextObject



102
103
104
105
106
107
108
# File 'lib/pcap/handler.rb', line 102

def next
  header = PacketHeader.new
  data = PCap.pcap_next(@pcap,header)

  return [nil, nil] if data.null?
  return [header, data]
end

#next_extraObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pcap/handler.rb', line 110

def next_extra
  header_ptr = MemoryPointer.new(:pointer)
  data_ptr = MemoryPointer.new(:pointer)

  case PCap.pcap_next_ex(@pcap,header_ptr,data_ptr)
  when -1
    raise(ReadError,"an error occurred while reading the packet",caller)
  when -2
    raise(ReadError,"the 'savefile' contains no more packets",caller)
  end

  return [header_ptr.get_pointer(0), data_ptr.get_pointer(0)]
end

#non_blocking=(mode) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pcap/handler.rb', line 62

def non_blocking=(mode)
  errbuf = ErrorBuffer.new
  mode = if mode
    1
  else
    0
  end

  if PCap.pcap_setnonblock(@pcap,mode,errbuf) == -1
    raise(RuntimeError,errbuf.to_s,caller)
  end

  return mode == 1
end

#non_blocking?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
# File 'lib/pcap/handler.rb', line 77

def non_blocking?
  errbuf = ErrorBuffer.new
  mode = PCap.pcap_getnonblock(@pcap,errbuf)

  if mode == -1
    raise(RuntimeError,errbuf.to_s,caller)
  end

  return mode == 1
end

#open_dump(path) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/pcap/handler.rb', line 124

def open_dump(path)
  dump_ptr = PCap.pcap_dump_open(@pcap,File.expand_path(path))

  if dump_ptr.null?
    raise(RuntimeError,error,caller)
  end

  return Dumper.new(dump_ptr)
end

#statsObject



134
135
136
137
138
139
# File 'lib/pcap/handler.rb', line 134

def stats
  stats = Stat.new

  PCap.pcap_stats(@pcap,stats)
  return stats
end

#stopObject



145
146
147
# File 'lib/pcap/handler.rb', line 145

def stop
  PCap.pcap_breakloop(@pcap)
end

#to_ptrObject



160
161
162
# File 'lib/pcap/handler.rb', line 160

def to_ptr
  @pcap
end