packetgen 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/packetgen/header.rb +2 -0
- data/lib/packetgen/header/icmp.rb +97 -0
- data/lib/packetgen/header/icmpv6.rb +33 -0
- data/lib/packetgen/header/ipv6.rb +1 -1
- data/lib/packetgen/structfu.rb +4 -0
- data/lib/packetgen/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89bdf542e873de9589a6c7e70b3823ccc306ad02
|
4
|
+
data.tar.gz: 82fd50fd41475f7471954dde56174853e24dfd1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b64e2a6f45b6a35c085c819567f9c08ccb2a62502c74861634408b1e1f69fc4c6ac706ad5223f57095fa2f670b2b4d46b920a5f44ecb7fa49381386a7865b736
|
7
|
+
data.tar.gz: 733c4d48b547aef34bef53c196da7f7d29558286f9791542c316f22e939c2a8c8f75a8b9d372b518b0588723b48c5a84606943b271c41d81d84bd4a985714cb7
|
data/lib/packetgen/header.rb
CHANGED
@@ -16,6 +16,8 @@ require_relative 'header/header_class_methods'
|
|
16
16
|
require_relative 'header/header_methods'
|
17
17
|
require_relative 'header/eth'
|
18
18
|
require_relative 'header/ip'
|
19
|
+
require_relative 'header/icmp'
|
19
20
|
require_relative 'header/arp'
|
20
21
|
require_relative 'header/ipv6'
|
22
|
+
require_relative 'header/icmpv6'
|
21
23
|
require_relative 'header/udp'
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module PacketGen
|
2
|
+
module Header
|
3
|
+
|
4
|
+
# ICMP header class
|
5
|
+
# @author Sylvain Daubert
|
6
|
+
class ICMP < Struct.new(:type, :code, :sum, :body)
|
7
|
+
include StructFu
|
8
|
+
include HeaderMethods
|
9
|
+
extend HeaderClassMethods
|
10
|
+
|
11
|
+
# ICMP internet protocol number
|
12
|
+
IP_PROTOCOL = 1
|
13
|
+
|
14
|
+
# @param [Hash] options
|
15
|
+
# @option options [Integer] :type
|
16
|
+
# @option options [Integer] :code
|
17
|
+
# @option options [Integer] :sum
|
18
|
+
# @option options [String] :body
|
19
|
+
def initialize(options={})
|
20
|
+
super Int8.new(options[:type]),
|
21
|
+
Int8.new(options[:code]),
|
22
|
+
Int16.new(options[:sum]),
|
23
|
+
StructFu::String.new.read(options[:body])
|
24
|
+
end
|
25
|
+
|
26
|
+
# Read a ICMP header from a string
|
27
|
+
# @param [String] str binary string
|
28
|
+
# @return [self]
|
29
|
+
def read(str)
|
30
|
+
return self if str.nil?
|
31
|
+
raise ParseError, 'string too short for Eth' if str.size < self.sz
|
32
|
+
force_binary str
|
33
|
+
self[:type].read str[0, 1]
|
34
|
+
self[:code].read str[1, 1]
|
35
|
+
self[:sum].read str[2, 2]
|
36
|
+
self[:body].read str[4..-1]
|
37
|
+
end
|
38
|
+
|
39
|
+
# Compute checksum and set +sum+ field
|
40
|
+
# @return [Integer]
|
41
|
+
def calc_sum
|
42
|
+
sum = (type << 8) | code
|
43
|
+
|
44
|
+
payload = body.to_s
|
45
|
+
payload << "\x00" unless payload.size % 2 == 0
|
46
|
+
payload.unpack('n*').each { |x| sum += x }
|
47
|
+
|
48
|
+
while sum > 0xffff do
|
49
|
+
sum = (sum & 0xffff) + (sum >> 16)
|
50
|
+
end
|
51
|
+
sum = ~sum & 0xffff
|
52
|
+
self[:sum].value = (sum == 0) ? 0xffff : sum
|
53
|
+
end
|
54
|
+
|
55
|
+
# Getter for type attribute
|
56
|
+
# @return [Integer]
|
57
|
+
def type
|
58
|
+
self[:type].to_i
|
59
|
+
end
|
60
|
+
|
61
|
+
# Setter for type attribute
|
62
|
+
# @param [Integer] type
|
63
|
+
# @return [Integer]
|
64
|
+
def type=(type)
|
65
|
+
self[:type].value = type
|
66
|
+
end
|
67
|
+
|
68
|
+
# Getter for code attribute
|
69
|
+
# @return [Integer]
|
70
|
+
def code
|
71
|
+
self[:code].to_i
|
72
|
+
end
|
73
|
+
|
74
|
+
# Setter for code attribute
|
75
|
+
# @param [Integer] code
|
76
|
+
# @return [Integer]
|
77
|
+
def code=(code)
|
78
|
+
self[:code].value = code
|
79
|
+
end
|
80
|
+
|
81
|
+
# Getter for sum attribute
|
82
|
+
# @return [Integer]
|
83
|
+
def sum
|
84
|
+
self[:sum].to_i
|
85
|
+
end
|
86
|
+
|
87
|
+
# Setter for sum attribute
|
88
|
+
# @param [Integer] sum
|
89
|
+
# @return [Integer]
|
90
|
+
def sum=(sum)
|
91
|
+
self[:sum].value = sum
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
IP.bind_header ICMP, proto: ICMP::IP_PROTOCOL
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module PacketGen
|
2
|
+
module Header
|
3
|
+
|
4
|
+
# ICMPv6 header class
|
5
|
+
# @author Sylvain Daubert
|
6
|
+
class ICMPv6 < ICMP
|
7
|
+
|
8
|
+
# ICMPv6 internet protocol number
|
9
|
+
IP_PROTOCOL = 58
|
10
|
+
|
11
|
+
# Compute checksum and set +sum+ field
|
12
|
+
# @return [Integer]
|
13
|
+
def calc_sum
|
14
|
+
sum = ip_header(self).pseudo_header_sum
|
15
|
+
sum += self.sz
|
16
|
+
sum += IP_PROTOCOL
|
17
|
+
sum +=(type << 8) | code
|
18
|
+
|
19
|
+
payload = body.to_s
|
20
|
+
payload << "\x00" unless payload.size % 2 == 0
|
21
|
+
payload.unpack('n*').each { |x| sum += x }
|
22
|
+
|
23
|
+
while sum > 0xffff do
|
24
|
+
sum = (sum & 0xffff) + (sum >> 16)
|
25
|
+
end
|
26
|
+
sum = ~sum & 0xffff
|
27
|
+
self[:sum].value = (sum == 0) ? 0xffff : sum
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
IPv6.bind_header ICMPv6, next: ICMPv6::IP_PROTOCOL
|
32
|
+
end
|
33
|
+
end
|
data/lib/packetgen/structfu.rb
CHANGED
data/lib/packetgen/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: packetgen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sylvain Daubert
|
@@ -114,6 +114,8 @@ files:
|
|
114
114
|
- lib/packetgen/header/eth.rb
|
115
115
|
- lib/packetgen/header/header_class_methods.rb
|
116
116
|
- lib/packetgen/header/header_methods.rb
|
117
|
+
- lib/packetgen/header/icmp.rb
|
118
|
+
- lib/packetgen/header/icmpv6.rb
|
117
119
|
- lib/packetgen/header/ip.rb
|
118
120
|
- lib/packetgen/header/ipv6.rb
|
119
121
|
- lib/packetgen/header/udp.rb
|