DIY-pcap 0.0.1

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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "diy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "DIY-pcap"
7
+ s.version = DIY::PCAP::VERSION
8
+ s.authors = ["yafei Lee"]
9
+ s.email = ["lyfi2003@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{DIY pcap send and recv}
12
+ s.description = %q{DIY pcap send and recv}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ s.add_dependency "ffi-pcap", ">=0.2.0"
19
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://ruby.taobao.org"
2
+
3
+ # Specify your gem's dependencies in DIY-pcap.gemspec
4
+ gem "ffi-pcap", ">=0.2.0"
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), '..', 'lib' )
2
+ require 'diy/pcap'
3
+ require 'diy/task'
4
+
5
+ if ARGV[0].nil?
6
+ puts "Usage: #{File.basename(__FILE__)} file"
7
+ exit 0
8
+ end
9
+
10
+ require File.join( Dir.pwd, ARGV[0] )
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), '..', 'lib' )
2
+ require 'diy/pcap'
3
+ require 'diy/task'
4
+
5
+ $SERVER = true
6
+
7
+ if ARGV[0].nil?
8
+ puts "Usage: #{File.basename(__FILE__)} file"
9
+ exit 0
10
+ end
11
+
12
+ require File.join( Dir.pwd, ARGV[0] )
@@ -0,0 +1,2 @@
1
+ require "diy/version"
2
+ require 'diy/pcap'
@@ -0,0 +1,110 @@
1
+ require 'ffi/pcap'
2
+ require 'logger'
3
+ require 'diy/version'
4
+ module DIY
5
+ class PCAP
6
+ def initialize
7
+ @timeout = 10
8
+ @dir = Dir.pwd
9
+ @pkt_stack = []
10
+ @device_name = FFI::PCap.dump_devices[0][0]
11
+ yield self
12
+ @driver = FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
13
+ #~ pause_for_user
14
+ run
15
+ end
16
+
17
+ attr_accessor :timeout, :dir, :device_name
18
+
19
+ def send(pkt_dir)
20
+ @pkt_stack << Packet.new( pkt_dir2pkt(pkt_dir), Packet::SEND)
21
+ end
22
+
23
+ def recv(pkt_dir)
24
+ @pkt_stack << Packet.new( pkt_dir2pkt(pkt_dir), Packet::RECV)
25
+ end
26
+
27
+ def pkt_dir2pkt(dir)
28
+ File.read( File.join( @dir, dir ) )
29
+ end
30
+
31
+ def run
32
+ if $SERVER
33
+ run_server
34
+ else
35
+ run_client
36
+ end
37
+ end
38
+
39
+ def pause_for_user
40
+ puts "Input ENTER for going..."
41
+ gets
42
+ end
43
+
44
+ def run_client
45
+ @pkt_stack.each do |pkt|
46
+ if pkt.to_outer?
47
+ send_pkt(pkt.pkt)
48
+ else
49
+ recv_pkt(pkt.pkt)
50
+ end
51
+ end
52
+ end
53
+
54
+ def run_server
55
+ @pkt_stack.each do |pkt|
56
+ if pkt.to_inner?
57
+ send_pkt(pkt.pkt)
58
+ else
59
+ recv_pkt(pkt.pkt)
60
+ end
61
+ end
62
+ end
63
+
64
+ def send_pkt(pkt)
65
+ sleep 1
66
+ logger.info("send pkt: [ #{Time.now} ]#{pkt[0..10].dump}...")
67
+ @driver.inject(pkt)
68
+ end
69
+
70
+ def recv_pkt(pkt)
71
+ logger.info("I hope pkt: #{pkt[0..10].dump}")
72
+ @driver.loop do |this, new_pkt|
73
+ #~ logger.info("recv pkt: [ #{new_pkt.time} ]: #{new_pkt.body[0..10].dump}..." )
74
+ if new_pkt.body == pkt
75
+ logger.info("recv pkt: [ #{new_pkt.time} ]: #{new_pkt.body[0..10].dump}..." )
76
+ logger.info "got the same pkt,stop"
77
+ return true
78
+ end
79
+ end
80
+ end
81
+
82
+ def logger
83
+ @@logger ||= Logger.new(STDOUT)
84
+ end
85
+
86
+ def logger=(logger)
87
+ @@logger = logger
88
+ end
89
+
90
+ end
91
+
92
+ class Packet
93
+ SEND = 1
94
+ RECV = 0
95
+ def initialize( pkt, pos )
96
+ @pkt = pkt
97
+ @pos = pos
98
+ end
99
+
100
+ def to_outer?
101
+ @pos == SEND
102
+ end
103
+
104
+ def to_inner?
105
+ @pos == RECV
106
+ end
107
+
108
+ attr_reader :pkt, :pos
109
+ end
110
+ end
@@ -0,0 +1,10 @@
1
+ require 'eventmachine'
2
+
3
+
4
+ module DIY
5
+ class Sender < EM::Connection
6
+ def notify_readable(*arg)
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ def pcap(&block)
2
+ DIY::PCAP.new(&block)
3
+ end
@@ -0,0 +1,5 @@
1
+ module DIY
2
+ class PCAP
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ pcap do |s|
2
+ s.dir = File.join( File.dirname(__FILE__), 'pcaps')
3
+ s.send("r1.dat")
4
+ s.recv("s1.dat")
5
+ s.recv("s2.dat")
6
+ s.recv("s3.dat")
7
+ s.send("r3.dat")
8
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), '..', 'lib' )
2
+ require 'rubygems'
3
+ require 'diy/pcap'
4
+
5
+ # client and server
6
+ DIY::PCAP.new do |s|
7
+ s.dir = File.join( File.dirname(__FILE__), 'pcaps')
8
+ s.send("r1.dat")
9
+ s.recv("s1.dat")
10
+ s.recv("s2.dat")
11
+ s.recv("s3.dat")
12
+ s.send("r3.dat")
13
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DIY-pcap
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - yafei Lee
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-08-30 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ffi-pcap
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 2
31
+ - 0
32
+ version: 0.2.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: DIY pcap send and recv
36
+ email:
37
+ - lyfi2003@gmail.com
38
+ executables:
39
+ - pcap
40
+ - rpcap
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - .rspec
48
+ - DIY-pcap.gemspec
49
+ - Gemfile
50
+ - Rakefile
51
+ - bin/pcap
52
+ - bin/rpcap
53
+ - lib/DIY-pcap.rb
54
+ - lib/diy/pcap.rb
55
+ - lib/diy/sender.rb
56
+ - lib/diy/task.rb
57
+ - lib/diy/version.rb
58
+ - simple/cmd-pcap.rb
59
+ - simple/diy-pcap.rb
60
+ - simple/pcaps/r1.dat
61
+ - simple/pcaps/r3-2.dat
62
+ - simple/pcaps/r3.dat
63
+ - simple/pcaps/s1.dat
64
+ - simple/pcaps/s2.dat
65
+ - simple/pcaps/s3.dat
66
+ - simple/pcaps/s4.dat
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: ""
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: DIY pcap send and recv
100
+ test_files: []
101
+