ip_frag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/helper/*.dat
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'ip_frag'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 yafei Lee
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # IpFrag
2
+
3
+ Help you quickly create fragged packets, used by network test.
4
+
5
+
6
+ ## Installation
7
+
8
+ $ gem install ip_frag
9
+
10
+ ## Usage
11
+
12
+ $ ip_gen --size 200 --mtu 1480
13
+
14
+ It will generate **2** UDP packets fragged.
15
+
16
+ ## Contributing
17
+
18
+ 1. Fork it
19
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
20
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
21
+ 4. Push to the branch (`git push origin my-new-feature`)
22
+ 5. Create new Pull Request
data/bin/ipgen ADDED
@@ -0,0 +1,29 @@
1
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), '..', 'lib')
2
+ require 'ip_frag'
3
+ require 'optparse'
4
+
5
+ options = {
6
+ :mtu => 1480
7
+ }
8
+
9
+ OptionParser.new do |opts|
10
+ opts.on('-s size', '--size size', 'total size, should be bigger than mtu') do |v|
11
+ options[:size] = v.to_i
12
+ end
13
+
14
+ opts.on('-m mtu', '--mtu mtu', 'ip mtu, offen less than 1480') do |v|
15
+ options[:mtu] = v.to_i
16
+ end
17
+
18
+ opts.on('-h', '--help', 'Show this help and exit') do
19
+ puts opts
20
+ exit 0
21
+ end
22
+ end.parse!
23
+
24
+ if options[:size].nil?
25
+ $stderr.puts "less -s argument, please see help for more."
26
+ exit 1
27
+ end
28
+
29
+ IPFrag::Generator.new( options[:size], options[:mtu] ).write_dat
data/ip_frag.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ip_frag/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["yafei Lee"]
6
+ gem.email = ["lyfi2003@gmail.com"]
7
+ gem.description = %q{ip frag tool}
8
+ gem.summary = %q{ip frag tool}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ip_frag"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = IPFrag::VERSION
17
+ gem.add_dependency "DIY-pcap", ">= 0.3.7"
18
+ end
@@ -0,0 +1,131 @@
1
+ module IPFrag
2
+ class Generator
3
+ def initialize(size, mtu)
4
+ @size = size
5
+ @mtu = mtu
6
+ check_size_mtu
7
+ end
8
+
9
+ def split_to_ip_packet
10
+ ret = []
11
+
12
+ offset = 0
13
+ ret << make_ip_packet(packet_offset) do |p|
14
+ p.offset = make_offset(offset)
15
+ end
16
+ offset += packet_offset
17
+
18
+ (packet_count - 2).times do
19
+ ret << make_ip_packet(packet_offset) do |p|
20
+ p.offset = make_offset(offset)
21
+ end
22
+ offset += packet_offset
23
+ end
24
+
25
+ ret << make_ip_packet(packet_left) do |p|
26
+ p.offset = make_offset(offset, true)
27
+ end
28
+
29
+ ret
30
+ end
31
+
32
+ def split_to_ethernet_packet
33
+ ip_packets = split_to_ip_packet
34
+ ip_packets.map do |ip_packet|
35
+ make_ether_packet do |e|
36
+ e.payload = ip_packet
37
+ e.type
38
+ end
39
+ end
40
+ end
41
+
42
+ def write_dat(path = '.')
43
+ split_to_ethernet_packet.each_with_index do |p, i|
44
+ file_name = "#{i+1}.dat"
45
+ file = File.open( File.join(path, file_name), 'w') do |f|
46
+ p.write(f)
47
+ end
48
+ end
49
+ end
50
+
51
+ # Get total packet size, it depends on @size and @mtu
52
+ #
53
+ #
54
+ def packet_count
55
+ count, left = @size.divmod(packet_offset)
56
+ if left != 0
57
+ count += 1
58
+ end
59
+ count
60
+ end
61
+
62
+ def packet_offset
63
+ @mtu & (~7)
64
+ end
65
+
66
+ def packet_left
67
+ left = @size % packet_offset
68
+ left = packet_offset if left == 0
69
+ left
70
+ end
71
+
72
+ def make_ip_packet(size)
73
+ ip = Mu::Pcap::IPv4.new(src_ip, dst_ip)
74
+ ip.payload = contant(size)
75
+ ip.proto = Mu::Pcap::IP::IPPROTO_UDP
76
+ yield ip if block_given?
77
+ ip
78
+ end
79
+
80
+ def make_ether_packet
81
+ packet = Mu::Pcap::Ethernet.new(src_mac, dst_mac)
82
+ packet.type = Mu::Pcap::Ethernet::ETHERTYPE_IP
83
+ yield packet
84
+ packet
85
+ end
86
+
87
+ def make_offset(offset, last = false)
88
+ offset = offset >> 3
89
+
90
+ flag = 1
91
+ if last
92
+ flag = 0
93
+ end
94
+ ( flag << 13) | offset
95
+ end
96
+
97
+ def check_size_mtu
98
+
99
+ if @size <= @mtu
100
+ raise "Size#{@size} MUST be big than Mtu#{@mtu}."
101
+ end
102
+
103
+ if @mtu <= 0
104
+ raise "Mtu MUST be an integer big than ZERO"
105
+ end
106
+ end
107
+
108
+ def src_ip
109
+ @src_ip ||= '200.200.200.1'
110
+ end
111
+
112
+ def dst_ip
113
+ @dst_ip ||= '200.200.200.2'
114
+ end
115
+
116
+ def src_mac
117
+ @src_mac ||= '00:00:00:00:00:01'
118
+ end
119
+
120
+ def dst_mac
121
+ @dst_mac ||= '00:00:00:00:00:02'
122
+ end
123
+
124
+ attr_writer :src_ip, :dst_ip, :src_mac, :dst_mac
125
+
126
+ def contant(size)
127
+ 'a' * size
128
+ end
129
+ end
130
+
131
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ module IPFrag
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ip_frag.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "ip_frag/version"
2
+
3
+ require 'rubygems'
4
+ require 'diy-pcap'
5
+ require 'ip_frag/generator'
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe IPFrag do
4
+ it "show version" do
5
+ lambda { IPFrag::VERSION }.should_not raise_error
6
+ end
7
+
8
+ it "#check_size_mtu" do
9
+ lambda { IPFrag::Generator.new(100, 10) }.should_not raise_error
10
+ lambda { IPFrag::Generator.new(100, 0) }.should raise_error
11
+ lambda { IPFrag::Generator.new(10, 10) }.should raise_error
12
+ lambda { IPFrag::Generator.new(10, -1) }.should raise_error
13
+ end
14
+
15
+ it "#split" do
16
+ g = IPFrag::Generator.new(100, 8)
17
+ g.split_to_ip_packet.size.should == 13
18
+ g.split_to_ethernet_packet.size.should == 13
19
+ end
20
+
21
+ it "#write_dat" do
22
+ g = IPFrag::Generator.new(2000, 1460)
23
+ g.write_dat('helper')
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.join( File.dirname(__FILE__), '..', 'lib') unless $LOADED
2
+
3
+ $LOADED = true
4
+
5
+ require 'ip_frag'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip_frag
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - yafei Lee
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: DIY-pcap
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 29
29
+ segments:
30
+ - 0
31
+ - 3
32
+ - 7
33
+ version: 0.3.7
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: ip frag tool
37
+ email:
38
+ - lyfi2003@gmail.com
39
+ executables:
40
+ - ipgen
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE
49
+ - README.md
50
+ - bin/ipgen
51
+ - ip_frag.gemspec
52
+ - lib/ip_frag.rb
53
+ - lib/ip_frag/generator.rb
54
+ - lib/ip_frag/ip_wrapper.rb
55
+ - lib/ip_frag/version.rb
56
+ - spec/generator_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: ""
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.24
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: ip frag tool
91
+ test_files:
92
+ - spec/generator_spec.rb
93
+ - spec/spec_helper.rb