packetgen 3.3.0 → 3.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de85a6f18ac001823cdbd0802c8946ead811be724c9069266b78c493d99feb6b
4
- data.tar.gz: ef73ca367e87bb47ca4f37ee387c7a73d0ad0f19cebaca151443560f42508559
3
+ metadata.gz: cdbaefa45999efbdef944fd579a9657a4ce4cfbf247bab09a4a7692ac22f9126
4
+ data.tar.gz: 8bce62a68be0b6803eea0a1323009c0413c20e1b4628062f22bcb240909f93a5
5
5
  SHA512:
6
- metadata.gz: '01605812af7b7d53111bfdc0d4d095cb6f0df4975634db17700d7c15c6acc63ba82585e6c5a5d2d93c130d566f45bb62d9ff3709c27e0f7cf4ea0c20ba7c172f'
7
- data.tar.gz: e4a54c74e032ee12d744f94c657416591c0f056a005da4b104c017294d73819596a28ec59c71597f8d0f2398da79a8b7784809d5e30cd5da9d53fc655101e2f4
6
+ metadata.gz: 20246ef1684a643dc0f18ecb25558e78ae70197f141dfa059bbd5559806c87229436432816a7ca3c1008910f15c010842a08dca0de5e50499fad283566953f01
7
+ data.tar.gz: c8576c49901a2c49336b638f859d80446c0773701177e3aeb4d24324c264d00f58ec4fbe6a3c5a1dd71247385f0fcb421953e4b1865144119a9d07242c0d6f97
@@ -60,63 +60,101 @@ module PacketGen
60
60
  class AbstractTLV < Types::Fields
61
61
  include Fieldable
62
62
 
63
+ # @private
64
+ FIELD_TYPES = { 'T' => :type, 'L' => :length, 'V' => :value }.freeze
65
+
63
66
  class << self
64
67
  # @return [Hash]
65
68
  attr_accessor :aliases
69
+ # @deprecated
66
70
  attr_accessor :header_in_length
67
- end
68
- self.aliases = {}
69
-
70
- # Generate a TLV class
71
- # @param [Class] type_class Class to use for +type+
72
- # @param [Class] length_class Class to use for +length+
73
- # @param [Class] value_class Class to use for +value+
74
- # @param [Boolean] header_in_length if +true +, +type+ and +length+ fields are
75
- # included in length
76
- # @return [Class]
77
- # @since 3.1.4 Add +header_in_length+ parameter
78
- def self.create(type_class: Int8Enum, length_class: Int8, value_class: String,
79
- aliases: {}, header_in_length: false)
80
- raise Error, '.create cannot be called on a subclass of PacketGen::Types::AbstractTLV' unless self.equal? AbstractTLV
81
-
82
- klass = Class.new(self)
83
- klass.aliases = aliases
84
- klass.header_in_length = header_in_length
85
-
86
- if type_class < Enum
87
- klass.define_field :type, type_class, enum: {}
88
- else
89
- klass.define_field :type, type_class
71
+ # @private
72
+ attr_accessor :field_in_length
73
+
74
+ # Generate a TLV class
75
+ # @param [Class] type_class Class to use for +type+
76
+ # @param [Class] length_class Class to use for +length+
77
+ # @param [Class] value_class Class to use for +value+
78
+ # @param [Boolean] header_in_length if +true +, +type+ and +length+ fields are
79
+ # included in length. Deprecated, use +field_in_length+ instead.
80
+ # @param [String] field_order give field order. Each character in [T,L,V] MUST be present once, in the desired order.
81
+ # @param [String] field_in_length give fields to compute length on.
82
+ # @return [Class]
83
+ # @since 3.1.4 Add +header_in_length+ parameter
84
+ # @since 3.3.1 Add +field_order+ and +field_in_length' parameters. Deprecate +header_in_length+ parameter.
85
+ def create(type_class: Int8Enum, length_class: Int8, value_class: String,
86
+ aliases: {}, header_in_length: false, field_order: 'TLV', field_in_length: 'V')
87
+ Deprecation.deprecated_option(self, 'create', 'header_in_length', klass_method: true) if header_in_length
88
+ raise Error, '.create cannot be called on a subclass of PacketGen::Types::AbstractTLV' unless self.equal?(AbstractTLV)
89
+
90
+ klass = Class.new(self)
91
+ klass.aliases = aliases
92
+ klass.header_in_length = header_in_length
93
+ klass.field_in_length = field_in_length
94
+
95
+ check_field_in_length(field_in_length)
96
+ check_field_order(field_order)
97
+ generate_fields(klass, field_order, type_class, length_class, value_class)
98
+
99
+ aliases.each do |al, orig|
100
+ klass.instance_eval do
101
+ alias_method al, orig if klass.method_defined?(orig)
102
+ alias_method :"#{al}=", :"#{orig}=" if klass.method_defined?(:"#{orig}=")
103
+ end
104
+ end
105
+
106
+ klass
90
107
  end
91
- klass.define_field :length, length_class
92
- klass.define_field :value, value_class
93
108
 
94
- aliases.each do |al, orig|
95
- klass.instance_eval do
96
- alias_method al, orig if klass.method_defined?(orig)
97
- alias_method :"#{al}=", :"#{orig}=" if klass.method_defined?(:"#{orig}=")
98
- end
109
+ # @!attribute type
110
+ # @abstract Type attribute for real TLV class
111
+ # @return [Integer]
112
+ # @!attribute length
113
+ # @abstract Length attribute for real TLV class
114
+ # @return [Integer]
115
+ # @!attribute value
116
+ # @abstract Value attribute for real TLV class
117
+ # @return [Object]
118
+
119
+ # @abstract Should only be called on real TLV classes, created by {.create}.
120
+ # Set enum hash for {#type} field.
121
+ # @param [Hash] hsh enum hash
122
+ # @return [void]
123
+ def define_type_enum(hsh)
124
+ field_defs[:type][:enum].clear
125
+ field_defs[:type][:enum].merge!(hsh)
99
126
  end
100
127
 
101
- klass
102
- end
103
- # @!attribute type
104
- # @abstract Type attribute for real TLV class
105
- # @return [Integer]
106
- # @!attribute length
107
- # @abstract Length attribute for real TLV class
108
- # @return [Integer]
109
- # @!attribute value
110
- # @abstract Value attribute for real TLV class
111
- # @return [Object]
128
+ private
112
129
 
113
- # @abstract Should only be called on real TLV classes, created by {.create}.
114
- # Set enum hash for {#type} field.
115
- # @param [Hash] hsh enum hash
116
- # @return [void]
117
- def self.define_type_enum(hsh)
118
- field_defs[:type][:enum].clear
119
- field_defs[:type][:enum].merge!(hsh)
130
+ def check_field_in_length(field_in_length)
131
+ return if /^[TLV]{1,3}$/.match?(field_in_length)
132
+
133
+ raise 'field_in_length must only contain T, L and/or V characters'
134
+ end
135
+
136
+ def check_field_order(field_order)
137
+ return if field_order.match(/^[TLV]{3,3}$/) && (field_order[0] != field_order[1]) && (field_order[0] != field_order[2]) && (field_order[1] != field_order[2])
138
+
139
+ raise 'field_order must contain all three letters TLV, each once'
140
+ end
141
+
142
+ def generate_fields(klass, field_order, type_class, length_class, value_class)
143
+ field_order.each_char do |field_type|
144
+ case field_type
145
+ when 'T'
146
+ if type_class < Enum
147
+ klass.define_field(:type, type_class, enum: {})
148
+ else
149
+ klass.define_field(:type, type_class)
150
+ end
151
+ when 'L'
152
+ klass.define_field(:length, length_class)
153
+ when 'V'
154
+ klass.define_field(:value, value_class)
155
+ end
156
+ end
157
+ end
120
158
  end
121
159
 
122
160
  # @abstract Should only be called on real TLV classes, created by {.create}.
@@ -126,6 +164,7 @@ module PacketGen
126
164
  # @option options [Object] :value
127
165
  def initialize(options={})
128
166
  @header_in_length = self.class.header_in_length
167
+ @field_in_length = self.class.field_in_length
129
168
  self.class.aliases.each do |al, orig|
130
169
  options[orig] = options[al] if options.key?(al)
131
170
  end
@@ -141,11 +180,13 @@ module PacketGen
141
180
  # @return [Fields] self
142
181
  def read(str)
143
182
  idx = 0
144
- self[:type].read str[idx, self[:type].sz]
145
- idx += self[:type].sz
146
- self[:length].read str[idx, self[:length].sz]
147
- idx += self[:length].sz
148
- self[:value].read str[idx, real_length]
183
+ fields.each do |field_name|
184
+ field = self[field_name]
185
+ length = field_name == :value ? real_length : field.sz
186
+ field.read(str[idx, length])
187
+ idx += field.sz
188
+ end
189
+
149
190
  self
150
191
  end
151
192
 
@@ -154,9 +195,17 @@ module PacketGen
154
195
  # @param [::String,Integer] val
155
196
  # @return [::String,Integer]
156
197
  def value=(val)
157
- self[:value].from_human val
158
- self.length = self[:value].sz
159
- self.length += self[:type].sz + self[:length].sz if @header_in_length
198
+ self[:value].from_human(val)
199
+
200
+ fil = @field_in_length
201
+ fil = 'TLV' if @header_in_length
202
+
203
+ length = 0
204
+ fil.each_char do |field_type|
205
+ length += self[FIELD_TYPES[field_type]].sz
206
+ end
207
+ self.length = length
208
+
160
209
  val
161
210
  end
162
211
 
@@ -180,7 +229,10 @@ module PacketGen
180
229
  if @header_in_length
181
230
  self.length - self[:type].sz - self[:length].sz
182
231
  else
183
- self.length
232
+ length = self.length
233
+ length -= self[:type].sz if @field_in_length.include?('T')
234
+ length -= self[:length].sz if @field_in_length.include?('L')
235
+ length
184
236
  end
185
237
  end
186
238
  end
@@ -11,6 +11,7 @@ module PacketGen
11
11
  # Base integer class to handle binary integers
12
12
  # @abstract
13
13
  # @author Sylvain Daubert
14
+ # @since 3.3.1 support native endianess
14
15
  class Int
15
16
  include Fieldable
16
17
 
@@ -18,7 +19,8 @@ module PacketGen
18
19
  # @return [Integer]
19
20
  attr_accessor :value
20
21
  # Integer endianness
21
- # @return [:little,:big]
22
+ # @return [:little,:big,:native]
23
+ # @since 3.3.1 add :native
22
24
  attr_accessor :endian
23
25
  # Integer size, in bytes
24
26
  # @return [Integer]
@@ -128,10 +130,10 @@ module PacketGen
128
130
  # @author Sylvain Daubert
129
131
  class Int16 < Int
130
132
  # @param [Integer,nil] value
131
- # @param [:big, :little] endian
133
+ # @param [:big, :little, :native] endian
132
134
  def initialize(value=nil, endian=:big)
133
135
  super(value, endian, 2)
134
- @packstr = { big: 'n', little: 'v' }
136
+ @packstr = { big: 'n', little: 'v', native: 'S' }
135
137
  end
136
138
  end
137
139
 
@@ -153,6 +155,19 @@ module PacketGen
153
155
  end
154
156
  end
155
157
 
158
+ # native endian 2-byte unsigned integer
159
+ # @author Sylvain Daubert
160
+ # @since 3.3.1
161
+ class Int16n < Int16
162
+ # @param [Integer,nil] value
163
+ undef endian=
164
+
165
+ # @param [Integer, nil] value
166
+ def initialize(value=nil)
167
+ super(value, :native)
168
+ end
169
+ end
170
+
156
171
  # 2-byte signed integer
157
172
  # @author Sylvain Daubert
158
173
  # @since 2.8.2
@@ -161,7 +176,7 @@ module PacketGen
161
176
  # @param [:big, :little] endian
162
177
  def initialize(value=nil, endian=:big)
163
178
  super
164
- @packstr = { big: 's>', little: 's<' }
179
+ @packstr = { big: 's>', little: 's<', native: 's' }
165
180
  end
166
181
  end
167
182
 
@@ -185,13 +200,33 @@ module PacketGen
185
200
  end
186
201
  end
187
202
 
203
+ # native endian 2-byte signed integer
204
+ # @author Sylvain Daubert
205
+ # @since 3.3.1
206
+ class SInt16n < SInt16
207
+ # @param [Integer,nil] value
208
+ undef endian=
209
+
210
+ # @param [Integer, nil] value
211
+ def initialize(value=nil)
212
+ super(value, :native)
213
+ end
214
+ end
215
+
188
216
  # 3-byte unsigned integer
189
217
  # @author Sylvain Daubert
190
218
  # @since 2.1.4
191
219
  class Int24 < Int
192
220
  # @param [Integer,nil] value
193
- # @param [:big, :little] endian
221
+ # @param [:big, :little, :native] endian
194
222
  def initialize(value=nil, endian=:big)
223
+ if endian == :native
224
+ endian = if [1].pack('S').unpack1('n') == 1
225
+ :big
226
+ else
227
+ :little
228
+ end
229
+ end
195
230
  super(value, endian, 3)
196
231
  end
197
232
 
@@ -247,14 +282,27 @@ module PacketGen
247
282
  end
248
283
  end
249
284
 
285
+ # native endian 3-byte unsigned integer
286
+ # @author Sylvain Daubert
287
+ # @since 3.3.1
288
+ class Int24n < Int24
289
+ # @param [Integer,nil] value
290
+ undef endian=
291
+
292
+ # @param [Integer, nil] value
293
+ def initialize(value=nil)
294
+ super(value, :native)
295
+ end
296
+ end
297
+
250
298
  # 4-byte unsigned integer
251
299
  # @author Sylvain Daubert
252
300
  class Int32 < Int
253
301
  # @param [Integer,nil] value
254
- # @param [:big, :little] endian
302
+ # @param [:big, :little, :native] endian
255
303
  def initialize(value=nil, endian=:big)
256
304
  super(value, endian, 4)
257
- @packstr = { big: 'N', little: 'V' }
305
+ @packstr = { big: 'N', little: 'V', native: 'L' }
258
306
  end
259
307
  end
260
308
 
@@ -276,6 +324,19 @@ module PacketGen
276
324
  end
277
325
  end
278
326
 
327
+ # native endian 4-byte unsigned integer
328
+ # @author Sylvain Daubert
329
+ # @since 3.3.1
330
+ class Int32n < Int32
331
+ # @param [Integer,nil] value
332
+ undef endian=
333
+
334
+ # @param [Integer, nil] value
335
+ def initialize(value=nil)
336
+ super(value, :native)
337
+ end
338
+ end
339
+
279
340
  # 4-byte unsigned integer
280
341
  # @author Sylvain Daubert
281
342
  # @since 2.8.2
@@ -284,7 +345,7 @@ module PacketGen
284
345
  # @param [:big, :little] endian
285
346
  def initialize(value=nil, endian=:big)
286
347
  super
287
- @packstr = { big: 'l>', little: 'l<' }
348
+ @packstr = { big: 'l>', little: 'l<', native: 'l' }
288
349
  end
289
350
  end
290
351
 
@@ -308,14 +369,27 @@ module PacketGen
308
369
  end
309
370
  end
310
371
 
372
+ # native endian 4-byte unsigned integer
373
+ # @author Sylvain Daubert
374
+ # @since 3.3.1
375
+ class SInt32n < SInt32
376
+ # @param [Integer,nil] value
377
+ undef endian=
378
+
379
+ # @param [Integer, nil] value
380
+ def initialize(value=nil)
381
+ super(value, :native)
382
+ end
383
+ end
384
+
311
385
  # 8-byte unsigned integer
312
386
  # @author Sylvain Daubert
313
387
  class Int64 < Int
314
388
  # @param [Integer,nil] value
315
- # @param [:big, :little] endian
389
+ # @param [:big, :little, :native] endian
316
390
  def initialize(value=nil, endian=:big)
317
391
  super(value, endian, 8)
318
- @packstr = { big: 'Q>', little: 'Q<' }
392
+ @packstr = { big: 'Q>', little: 'Q<', native: 'Q' }
319
393
  end
320
394
  end
321
395
 
@@ -337,15 +411,28 @@ module PacketGen
337
411
  end
338
412
  end
339
413
 
414
+ # native endian 8-byte unsigned integer
415
+ # @author Sylvain Daubert
416
+ # @since 3.3.1
417
+ class Int64n < Int64
418
+ # @param [Integer,nil] value
419
+ undef endian=
420
+
421
+ # @param [Integer, nil] value
422
+ def initialize(value=nil)
423
+ super(value, :native)
424
+ end
425
+ end
426
+
340
427
  # 8-byte unsigned integer
341
428
  # @author Sylvain Daubert
342
429
  # @since 2.8.2
343
430
  class SInt64 < Int64
344
431
  # @param [Integer,nil] value
345
- # @param [:big, :little] endian
432
+ # @param [:big, :little, :native] endian
346
433
  def initialize(value=nil, endian=:big)
347
434
  super
348
- @packstr = { big: 'q>', little: 'q<' }
435
+ @packstr = { big: 'q>', little: 'q<', native: 'q' }
349
436
  end
350
437
  end
351
438
 
@@ -368,5 +455,18 @@ module PacketGen
368
455
  super(value, :little)
369
456
  end
370
457
  end
458
+
459
+ # native endian 8-byte unsigned integer
460
+ # @author Sylvain Daubert
461
+ # @since 3.3.1
462
+ class SInt64n < SInt64
463
+ # @param [Integer,nil] value
464
+ undef endian=
465
+
466
+ # @param [Integer, nil] value
467
+ def initialize(value=nil)
468
+ super(value, :native)
469
+ end
470
+ end
371
471
  end
372
472
  end
@@ -10,5 +10,5 @@
10
10
  # @author Sylvain Daubert
11
11
  module PacketGen
12
12
  # PacketGen version
13
- VERSION = '3.3.0'
13
+ VERSION = '3.3.1'
14
14
  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: 3.3.0
4
+ version: 3.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Daubert
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-11 00:00:00.000000000 Z
11
+ date: 2024-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interfacez
@@ -189,7 +189,7 @@ files:
189
189
  - lib/packetgen/utils.rb
190
190
  - lib/packetgen/utils/arp_spoofer.rb
191
191
  - lib/packetgen/version.rb
192
- homepage:
192
+ homepage:
193
193
  licenses:
194
194
  - MIT
195
195
  metadata:
@@ -198,7 +198,7 @@ metadata:
198
198
  bug_tracker_uri: https://github.com/sdaubert/packetgen/issues
199
199
  documentation_uri: https://www.rubydoc.info/gems/packetgen
200
200
  wiki_uri: https://github.com/sdaubert/packetgen/wiki
201
- post_install_message:
201
+ post_install_message:
202
202
  rdoc_options:
203
203
  - "--title"
204
204
  - PacketGen - network packet dissector
@@ -219,8 +219,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  - !ruby/object:Gem::Version
220
220
  version: '0'
221
221
  requirements: []
222
- rubygems_version: 3.2.5
223
- signing_key:
222
+ rubygems_version: 3.3.15
223
+ signing_key:
224
224
  specification_version: 4
225
225
  summary: Network packet generator and dissector
226
226
  test_files: []