packetgen 3.0.1 → 3.0.2

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: 0d0ea9ae9a56d494ae6bde328655e61bdfc62c23ee413fe4d13d40e785e8dfb0
4
- data.tar.gz: 2563927ccdc6961ed36d64f9e19ab52cf556cccd65bb66b391eb75510b50283f
3
+ metadata.gz: be94839db4cb9f9a818fdf5b948f34185198d7f8445545d13763cf57f313eb3a
4
+ data.tar.gz: 0cf02398bb76d3bb9083f6bbcb1e873b81c686e822ac3e63790a0b5f5d5155b7
5
5
  SHA512:
6
- metadata.gz: 9c1057ab719194fef7f977b27770ea31489807bc05d20570c33746e12abdc9777e7f50c8311860152ec5c38cae30b6a6c5ef2865019e19aee8138a9c6e477db8
7
- data.tar.gz: c705889994c6df09519de2b76fd5417d7cfc32b92cdf1c68d4d5efd3c3208f42c16bddbdc8256db7815ad87657d4dcf21226779281a42aa8935bb3f7a1d9aceb
6
+ metadata.gz: c72f91e241484140503a997aca5aa930cb8d3e357103b0af617231d7c49fa791b49537916fd68bb5c8d77b901c203500babf18cfe5c2df40900bfc2a30feb90b
7
+ data.tar.gz: d76180f3c3089218d67a0840d4f94c802c94bd8990cc45b91062414bba175973cc6f97287c1be99ccd87c063d7ebb17f210f15cc49fdd9eb649ec3465b29ea8e
@@ -17,23 +17,7 @@ module PacketGen
17
17
  # @author Sylvain Daubert
18
18
  # @since 2.0.0
19
19
  class ASN1Base < RASN1::Model
20
- # Reference on packet which owns this header
21
- # @return [Packet,nil]
22
- attr_reader :packet
23
-
24
- # Give protocol name for this class
25
- # @return [String]
26
- # @since 2.0.0
27
- def self.protocol_name
28
- return @protocol_name if defined? @protocol_name
29
-
30
- classname = to_s
31
- @protocol_name = if classname.start_with?('PacketGen::Header')
32
- classname.sub(/.*Header::/, '')
33
- else
34
- classname.sub(/.*::/, '')
35
- end
36
- end
20
+ include Headerable
37
21
 
38
22
  # Define some methods from given ASN.1 fields to mimic {Base} attributes
39
23
  # @param [Array<Symbol>] attributes
@@ -46,46 +30,6 @@ module PacketGen
46
30
  end
47
31
  end
48
32
 
49
- # Return header protocol name
50
- # @return [String]
51
- def protocol_name
52
- self.class.protocol_name
53
- end
54
-
55
- # return header method name
56
- # @return [String]
57
- # @since 2.0.0
58
- def method_name
59
- return @method_name if defined? @method_name
60
-
61
- @method_name = protocol_name.downcase.sub(/::/, '_')
62
- end
63
-
64
- # Called by {Packet#parse} when guessing first header to check if header
65
- # is correct
66
- # @return [true]
67
- def parse?
68
- true
69
- end
70
-
71
- # @api private
72
- # @since 2.1.4
73
- # Set packet to which this header belongs
74
- # @param [Packet] packet
75
- # @return [Packet] packet
76
- def packet=(packet)
77
- @packet = packet
78
- added_to_packet(packet)
79
- @packet
80
- end
81
-
82
- # @abstract This method is called when a header is added to a packet.
83
- # This base method does nothing but may be overriden by subclasses.
84
- # @param [Packet] packet packet to which self is added
85
- # @return [void]
86
- # @since 2.1.4
87
- def added_to_packet(packet) end
88
-
89
33
  alias parse parse!
90
34
  alias to_s to_der
91
35
 
@@ -15,6 +15,8 @@ module PacketGen
15
15
  # * +#reply!+, which inverts needed fields to forge a response.
16
16
  # @author Sylvain Daubert
17
17
  class Base < Types::Fields
18
+ include Headerable
19
+
18
20
  # @api private
19
21
  # Simple class to handle a header association
20
22
  class Binding < Struct.new(:key, :value)
@@ -133,10 +135,6 @@ module PacketGen
133
135
  end
134
136
  end
135
137
 
136
- # Reference on packet which owns this header
137
- # @return [Packet,nil]
138
- attr_reader :packet
139
-
140
138
  # @private
141
139
  # On inheritage, create +@known_header+ class variable
142
140
  # @param [Class] klass
@@ -195,19 +193,6 @@ module PacketGen
195
193
  end
196
194
  end
197
195
 
198
- # Give protocol name for this class
199
- # @return [String]
200
- # @since 2.0.0
201
- def self.protocol_name
202
- return @protocol_name if defined? @protocol_name
203
-
204
- classname = to_s
205
- @protocol_name = if classname.start_with?('PacketGen::Header')
206
- classname.sub(/.*Header::/, '')
207
- else
208
- classname.sub(/.*::/, '')
209
- end
210
- end
211
196
 
212
197
  # Helper method to calculate length of +hdr+ and set its +length+ field.
213
198
  # To be used by +#calc_length+ in Base subclasses.
@@ -236,47 +221,6 @@ module PacketGen
236
221
  super
237
222
  end
238
223
 
239
- # Return header protocol name
240
- # @return [String]
241
- def protocol_name
242
- self.class.protocol_name
243
- end
244
-
245
- # return header method name
246
- # @return [String]
247
- # @since 2.0.0
248
- # @since 2.8.6 permit multiple nesting levels
249
- def method_name
250
- return @method_name if defined? @method_name
251
-
252
- @method_name = protocol_name.downcase.gsub(/::/, '_')
253
- end
254
-
255
- # @abstract Should be redefined by subclasses. This method should check invariant
256
- # fields from header.
257
- # Call by {Packet#parse} when guessing first header to check if header is correct
258
- # @return [Boolean]
259
- def parse?
260
- true
261
- end
262
-
263
- # @api private
264
- # Set packet to which this header belongs
265
- # @param [Packet] packet
266
- # @return [Packet] packet
267
- # @since 2.1.4
268
- def packet=(packet)
269
- @packet = packet
270
- added_to_packet(packet)
271
- @packet
272
- end
273
-
274
- # @abstract This method is called when a header is added to a packet.
275
- # This base method does nothing but may be overriden by subclasses.
276
- # @param [Packet] packet packet to which self is added
277
- # @return [void]
278
- # @since 2.1.4
279
- def added_to_packet(packet) end
280
224
 
281
225
  # @api private
282
226
  # Get +header+ id in {Packet#headers} array
@@ -33,7 +33,7 @@ module PacketGen
33
33
  when 'end', 255
34
34
  End.new
35
35
  else
36
- obj_klass = self.class.class_eval { @klass }
36
+ obj_klass = self.class.set_of_klass
37
37
  obj_klass.new(hsh)
38
38
  end
39
39
  end
@@ -10,7 +10,7 @@ module PacketGen
10
10
  class DNS
11
11
  # DNS Question
12
12
  # @author Sylvain Daubert
13
- class Question < Base
13
+ class Question < Types::Fields
14
14
  # Ressource Record types
15
15
  TYPES = {
16
16
  'A' => 1,
@@ -48,7 +48,7 @@ module PacketGen
48
48
 
49
49
  def real_type(opt)
50
50
  klasses = self.class.option_classes
51
- klasses[opt.kind].nil? ? OPtion : klasses[opt.kind]
51
+ klasses[opt.kind].nil? ? Option : klasses[opt.kind]
52
52
  end
53
53
  end
54
54
  end
@@ -0,0 +1,93 @@
1
+ # This file is part of PacketGen
2
+ # See https://github.com/sdaubert/packetgen for more informations
3
+ # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
4
+ # This program is published under MIT license.
5
+
6
+ # frozen_string_literal: true
7
+
8
+ module PacketGen
9
+ # This mixin module defines minimal API for a class to act as a header
10
+ # in {Packet}.
11
+ # @author Sylvain Daubert
12
+ # @since 3.0.2
13
+ module Headerable
14
+ # This modules handles class methods for {Headerable headerable classes}.
15
+ module ClassMethods
16
+ # Give protocol name for this class
17
+ # @return [String]
18
+ def protocol_name
19
+ return @protocol_name if defined? @protocol_name
20
+
21
+ classname = to_s
22
+ @protocol_name = if classname.start_with?('PacketGen::Header')
23
+ classname.sub(/.*Header::/, '')
24
+ else
25
+ classname.sub(/.*::/, '')
26
+ end
27
+ end
28
+ end
29
+
30
+ # @api private
31
+ # Extend +klass+ with {ClassMethods}.
32
+ # @param [Class] klass
33
+ # @return [void]
34
+ def self.included(klass)
35
+ klass.extend ClassMethods
36
+ end
37
+
38
+ # Return header protocol name
39
+ # @return [String]
40
+ def protocol_name
41
+ self.class.protocol_name
42
+ end
43
+
44
+ # return header method name
45
+ # @return [String]
46
+ def method_name
47
+ return @method_name if defined? @method_name
48
+
49
+ @method_name = protocol_name.downcase.gsub(/::/, '_')
50
+ end
51
+
52
+ # @abstract Should be redefined by subclasses. This method should check invariant
53
+ # fields from header.
54
+ # Called by {Packet#parse} when guessing first header to check if header is correct
55
+ # @return [Boolean]
56
+ def parse?
57
+ true
58
+ end
59
+
60
+ # Reference on packet which owns this header
61
+ # @return [Packet,nil]
62
+ def packet
63
+ @packet ||= nil
64
+ end
65
+
66
+ # @api private
67
+ # Set packet to which this header belongs
68
+ # @param [Packet] packet
69
+ # @return [Packet] packet
70
+ def packet=(packet)
71
+ @packet = packet
72
+ added_to_packet(packet)
73
+ @packet
74
+ end
75
+
76
+ # @abstract This method is called when a header is added to a packet.
77
+ # This base method does nothing but may be overriden by subclasses.
78
+ # @param [Packet] packet packet to which self is added
79
+ # @return [void]
80
+ def added_to_packet(packet) end
81
+
82
+ # @abstract This method MUST be redefined by subclasses.
83
+ # Populate headerable object from a binary string.
84
+ # @param [String] str
85
+ # @return [self]
86
+ # @raise [NotImplementedError]
87
+ def read(str)
88
+ super
89
+ rescue NoMethodError
90
+ raise NotImplementedError, "#{self.class} should implement #read"
91
+ end
92
+ end
93
+ end
@@ -483,4 +483,5 @@ module PacketGen
483
483
  end
484
484
  end
485
485
 
486
+ require_relative 'headerable'
486
487
  require_relative 'header'
@@ -18,11 +18,11 @@ module PacketGen
18
18
  # by {.set_of}.
19
19
  #
20
20
  # == #real_type
21
- # Subclasses should define private method +#real_type+ is {.set_of} type
21
+ # Subclasses should define private method +#real_type+ if {.set_of} type
22
22
  # may be subclassed. This method should return real class to use. It
23
23
  # takes an only argument, which is of type given by {.set_of}.
24
24
  #
25
- # Default behaviour of this method is to argument's class.
25
+ # Default behaviour of this method is to return argument's class.
26
26
  #
27
27
  # @author Sylvain Daubert
28
28
  class Array
@@ -201,11 +201,15 @@ module PacketGen
201
201
 
202
202
  private
203
203
 
204
- def record_from_hash(obj)
204
+ def record_from_hash(hsh)
205
205
  obj_klass = self.class.set_of_klass
206
- return obj_klass.new(obj) if obj_klass
206
+ unless obj_klass
207
+ raise NotImplementedError, 'class should define #record_from_hash or declare type of elements in set with .set_of'
208
+ end
207
209
 
208
- raise NotImplementedError, 'class should define #record_from_hash or declare type of elements in set with .set_of'
210
+ obj = obj_klass.new(hsh) if obj_klass
211
+ klass = real_type(obj)
212
+ klass == obj_klass ? obj : klass.new(hsh)
209
213
  end
210
214
 
211
215
  def real_type(obj)
@@ -10,5 +10,5 @@
10
10
  # @author Sylvain Daubert
11
11
  module PacketGen
12
12
  # PacketGen version
13
- VERSION = '3.0.1'
13
+ VERSION = '3.0.2'
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.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Daubert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-31 00:00:00.000000000 Z
11
+ date: 2018-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interfacez
@@ -234,6 +234,7 @@ files:
234
234
  - lib/packetgen/header/tcp/options.rb
235
235
  - lib/packetgen/header/tftp.rb
236
236
  - lib/packetgen/header/udp.rb
237
+ - lib/packetgen/headerable.rb
237
238
  - lib/packetgen/inspect.rb
238
239
  - lib/packetgen/packet.rb
239
240
  - lib/packetgen/pcapng.rb