packetgen 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,24 @@ module PacketGen
2
2
  module Types
3
3
 
4
4
  # Class to handle Type-Length-Value attributes
5
+ #
6
+ # TLV fields handles three subfields:
7
+ # * a tag/type field (defaults: {Int8} type),
8
+ # * a length field (defaults: {Int8} type),
9
+ # * a value field (defaults: {String} type).
10
+ #
11
+ # {#initialize} supports options to change tag and length type. By example, to
12
+ # declare a TLV using {Int16}:
13
+ # define_field :tlv, PacketGen::Types::TLV, builder: ->(obj) { PacketGen::Types::TLV.new(t: PacketGen::Types::Int16, l: PacketGen::Types::Int16) }
14
+ #
15
+ # == Subclasses
16
+ # A subclass may defined a constant hash TYPES. This hash defines human readable
17
+ # types versus their integer values. Hash keys are integer values, and hash values
18
+ # are types as strings.
19
+ #
20
+ # If TYPES is defined, a subclass may:
21
+ # * print human readable type using {#human_type},
22
+ # * set type as String with {#type=}.
5
23
  # @author Sylvain Daubert
6
24
  class TLV < Fields
7
25
  # @!attribute type
@@ -26,16 +44,75 @@ module PacketGen
26
44
  def initialize(options={})
27
45
  super
28
46
  self[:type] = options[:t].new(type) if options[:t]
29
- if options[:l]
30
- self[:length] = options[:l].new(length)
31
- self[:value] = String.new('', length_from: self[:length])
47
+ self[:length] = options[:l].new(length) if options[:l]
48
+ self[:value] = String.new('', length_from: self[:length])
49
+ self.type = options[:type] if options[:type]
50
+ self.value = options[:value] if options[:value]
51
+ self.length = options[:length] if options[:length]
52
+ end
53
+
54
+ # @private
55
+ alias old_type= type=
56
+
57
+ # Set type
58
+ # @param [::String,Integer] val
59
+ # @return [Integer]
60
+ # @raise [TypeError] class does not define TYPES
61
+ # @raise [ArgumentError] unknown string type
62
+ def type=(val)
63
+ case val
64
+ when Integer
65
+ self.old_type = val
66
+ else
67
+ unless has_human_types?
68
+ raise TypeError, "no human types defined for #{self.class}"
69
+ end
70
+ new_val = self.class::TYPES[val.to_s]
71
+ raise ArgumentError, "unknown #{val.to_s} type" if new_val.nil?
72
+ self.old_type = new_val
32
73
  end
33
74
  end
34
75
 
35
- # @return [String]
36
- def to_human
37
- "type=#{type},len=#{length},value=#{value.inspect}"
76
+ # Set +value+ and +length+ fields
77
+ # @param [::String] str
78
+ # @return [::String]
79
+ def value=(str)
80
+ self.length = str.length
81
+ self[:value].read str
82
+ str
83
+ end
84
+
85
+ # Return human readable type, if TYPES is defined
86
+ # @return [String]
87
+ def human_type
88
+ if has_human_types?
89
+ htype = self.class::TYPES[type]
90
+ htype = type if htype.nil?
91
+ htype.to_s
92
+ else
93
+ type.to_s
38
94
  end
95
+ end
96
+
97
+ # @return [String]
98
+ def to_human
99
+ name = self.class.to_s.gsub(/.*::/, '')
100
+ @typestr ||= if has_human_types?
101
+ types = self.class::TYPES.values
102
+ "%-#{types.max { |a,b| a.length <=> b.length }.size}s"
103
+ else
104
+ "%s"
105
+ end
106
+ @lenstr ||= "%-#{(2**(self[:length].width*8) - 1).to_s.size}u"
107
+ "#{name} type:#@typestr length:#@lenstr value:#{value.inspect}" % [human_type,
108
+ length]
109
+ end
110
+
111
+ private
112
+
113
+ def has_human_types?
114
+ self.class.const_defined? :TYPES
115
+ end
39
116
  end
40
117
  end
41
118
  end
@@ -8,5 +8,5 @@
8
8
  # @author Sylvain Daubert
9
9
  module PacketGen
10
10
  # PacketGen version
11
- VERSION = "1.3.0"
11
+ VERSION = "1.4.0"
12
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packetgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Daubert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-27 00:00:00.000000000 Z
11
+ date: 2017-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pcaprub
@@ -120,12 +120,21 @@ files:
120
120
  - lib/packetgen/header/dns/question.rb
121
121
  - lib/packetgen/header/dns/rr.rb
122
122
  - lib/packetgen/header/dns/rrsection.rb
123
+ - lib/packetgen/header/dot11.rb
124
+ - lib/packetgen/header/dot11/control.rb
125
+ - lib/packetgen/header/dot11/data.rb
126
+ - lib/packetgen/header/dot11/element.rb
127
+ - lib/packetgen/header/dot11/management.rb
128
+ - lib/packetgen/header/dot11/sub_mngt.rb
129
+ - lib/packetgen/header/dot1q.rb
130
+ - lib/packetgen/header/dot1x.rb
123
131
  - lib/packetgen/header/esp.rb
124
132
  - lib/packetgen/header/eth.rb
125
133
  - lib/packetgen/header/icmp.rb
126
134
  - lib/packetgen/header/icmpv6.rb
127
135
  - lib/packetgen/header/ip.rb
128
136
  - lib/packetgen/header/ipv6.rb
137
+ - lib/packetgen/header/llc.rb
129
138
  - lib/packetgen/header/tcp.rb
130
139
  - lib/packetgen/header/tcp/option.rb
131
140
  - lib/packetgen/header/tcp/options.rb
@@ -145,6 +154,7 @@ files:
145
154
  - lib/packetgen/types/fields.rb
146
155
  - lib/packetgen/types/int.rb
147
156
  - lib/packetgen/types/int_string.rb
157
+ - lib/packetgen/types/oui.rb
148
158
  - lib/packetgen/types/string.rb
149
159
  - lib/packetgen/types/tlv.rb
150
160
  - lib/packetgen/version.rb