ahobson-pcap 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ require 'pcap'
2
+ require 'optparse'
3
+
4
+ def pcaplet_usage()
5
+ $stderr.print <<END
6
+ Usage: #{File.basename $0} [ -dnv ] [ -i interface | -r file ]
7
+ #{' ' * File.basename($0).length} [ -c count ] [ -s snaplen ] [ filter ]
8
+ Options:
9
+ -n do not convert address to name
10
+ -d debug mode
11
+ -v verbose mode
12
+ END
13
+ end
14
+
15
+ module Pcap
16
+ class Pcaplet
17
+ def usage(status, msg = nil)
18
+ $stderr.puts msg if msg
19
+ pcaplet_usage
20
+ exit(status)
21
+ end
22
+
23
+ def initialize(args = nil)
24
+ if args
25
+ ARGV[0,0] = args.split(/\s+/)
26
+ end
27
+ @device = nil
28
+ @rfile = nil
29
+ @count = -1
30
+ @snaplen = 68
31
+ opts = OptionParser.new do |opts|
32
+ opts.on('-d') {$DEBUG = true}
33
+ opts.on('-v') {$VERBOSE = true}
34
+ opts.on('-n') {Pcap.convert = false}
35
+ opts.on('-i IFACE') {|s| @device = s}
36
+ opts.on('-r FILE') {|s| @rfile = s}
37
+ opts.on('-c COUNT', OptionParser::DecimalInteger) {|i| @count = i}
38
+ opts.on('-s LEN', OptionParser::DecimalInteger) {|i| @snaplen = i}
39
+ end
40
+ begin
41
+ opts.parse!
42
+ rescue
43
+ usage(1)
44
+ end
45
+
46
+ @filter = ARGV.join(' ')
47
+
48
+ # check option consistency
49
+ usage(1) if @device && @rfile
50
+ if !@device and !@rfile
51
+ @device = Pcap.lookupdev
52
+ end
53
+
54
+ # open
55
+ begin
56
+ if @device
57
+ @capture = Capture.open_live(@device, @snaplen)
58
+ elsif @rfile
59
+ if @rfile !~ /\.gz$/
60
+ @capture = Capture.open_offline(@rfile)
61
+ else
62
+ $stdin = IO.popen("gzip -dc < #@rfile", 'r')
63
+ @capture = Capture.open_offline('-')
64
+ end
65
+ end
66
+ @capture.setfilter(@filter)
67
+ rescue PcapError, ArgumentError
68
+ $stdout.flush
69
+ $stderr.puts $!
70
+ exit(1)
71
+ end
72
+ end
73
+
74
+ attr('capture')
75
+
76
+ def add_filter(f)
77
+ if @filter == nil || @filter =~ /^\s*$/ # if empty
78
+ @filter = f
79
+ else
80
+ f = f.source if f.is_a? Filter
81
+ @filter = "( #{@filter} ) and ( #{f} )"
82
+ end
83
+ @capture.setfilter(@filter)
84
+ end
85
+
86
+ def each_packet(&block)
87
+ begin
88
+ duplicated = (RUBY_PLATFORM =~ /linux/ && @device == "lo")
89
+ unless duplicated
90
+ @capture.loop(@count, &block)
91
+ else
92
+ flip = true
93
+ @capture.loop(@count) do |pkt|
94
+ flip = (! flip)
95
+ next if flip
96
+ block.call pkt
97
+ end
98
+ end
99
+ rescue Interrupt
100
+ $stdout.flush
101
+ $stderr.puts("Interrupted.")
102
+ $stderr.puts $@.join("\n\t") if $DEBUG
103
+ ensure
104
+ # print statistics if live
105
+ if @device
106
+ stat = @capture.stats
107
+ if stat
108
+ $stderr.print("#{stat.recv} packets received by filter\n");
109
+ $stderr.print("#{stat.drop} packets dropped by kernel\n");
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ alias each each_packet
116
+
117
+ def close
118
+ @capture.close
119
+ end
120
+ end
121
+ end
122
+
123
+ Pcaplet = Pcap::Pcaplet
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ahobson-pcap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - Masaki Fukushima
8
+ - Andrew Hobson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-06 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Ruby interface to LBL Packet Capture library. This library also includes classes to access packet header fields.
18
+ email: fukusima@goto.info.waseda.ac.jp
19
+ executables: []
20
+
21
+ extensions:
22
+ - ext/extconf.rb
23
+ extra_rdoc_files:
24
+ - ChangeLog
25
+ - README
26
+ - README.ja
27
+ files:
28
+ - COPYING
29
+ - ChangeLog
30
+ - MANIFEST
31
+ - README
32
+ - README.ja
33
+ - Rakefile
34
+ - VERSION
35
+ - doc-ja/Capture.html
36
+ - doc-ja/Dumper.html
37
+ - doc-ja/Filter.html
38
+ - doc-ja/ICMPPacket.html
39
+ - doc-ja/IPAddress.html
40
+ - doc-ja/IPPacket.html
41
+ - doc-ja/Packet.html
42
+ - doc-ja/Pcap.html
43
+ - doc-ja/PcapError.html
44
+ - doc-ja/Pcaplet.html
45
+ - doc-ja/TCPPacket.html
46
+ - doc-ja/TruncatedPacket.html
47
+ - doc-ja/UDPPacket.html
48
+ - doc-ja/index.html
49
+ - doc/Capture.html
50
+ - doc/Dumper.html
51
+ - doc/Filter.html
52
+ - doc/ICMPPacket.html
53
+ - doc/IPAddress.html
54
+ - doc/IPPacket.html
55
+ - doc/Packet.html
56
+ - doc/Pcap.html
57
+ - doc/PcapError.html
58
+ - doc/Pcaplet.html
59
+ - doc/TCPPacket.html
60
+ - doc/TruncatedPacket.html
61
+ - doc/UDPPacket.html
62
+ - doc/index.html
63
+ - examples/httpdump.rb
64
+ - examples/tcpdump.rb
65
+ - examples/test.rb
66
+ - ext/Pcap.c
67
+ - ext/extconf.rb
68
+ - ext/icmp_packet.c
69
+ - ext/ip_packet.c
70
+ - ext/packet.c
71
+ - ext/ruby_pcap.h
72
+ - ext/tcp_packet.c
73
+ - ext/udp_packet.c
74
+ - lib/pcap_misc.rb
75
+ - lib/pcaplet.rb
76
+ has_rdoc: true
77
+ homepage: http://www.goto.info.waseda.ac.jp/~fukusima/ruby/pcap-e.html
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.2.0
99
+ signing_key:
100
+ specification_version: 2
101
+ summary: Interface to LBL Packet Capture library (libpcap)
102
+ test_files: []
103
+