packetgen 3.1.3 → 3.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/bin/pgconsole +1 -0
  4. data/lib/packetgen.rb +33 -4
  5. data/lib/packetgen/capture.rb +51 -28
  6. data/lib/packetgen/config.rb +17 -11
  7. data/lib/packetgen/deprecation.rb +33 -7
  8. data/lib/packetgen/header.rb +2 -9
  9. data/lib/packetgen/header/arp.rb +2 -2
  10. data/lib/packetgen/header/asn1_base.rb +2 -2
  11. data/lib/packetgen/header/base.rb +70 -74
  12. data/lib/packetgen/header/bootp.rb +3 -3
  13. data/lib/packetgen/header/dhcp.rb +2 -2
  14. data/lib/packetgen/header/dhcp/option.rb +2 -2
  15. data/lib/packetgen/header/dhcp/options.rb +2 -2
  16. data/lib/packetgen/header/dhcpv6.rb +12 -12
  17. data/lib/packetgen/header/dhcpv6/duid.rb +11 -5
  18. data/lib/packetgen/header/dhcpv6/option.rb +8 -16
  19. data/lib/packetgen/header/dhcpv6/options.rb +2 -2
  20. data/lib/packetgen/header/dhcpv6/relay.rb +2 -2
  21. data/lib/packetgen/header/dns.rb +9 -9
  22. data/lib/packetgen/header/dns/name.rb +20 -9
  23. data/lib/packetgen/header/dns/opt.rb +2 -2
  24. data/lib/packetgen/header/dns/option.rb +2 -2
  25. data/lib/packetgen/header/dns/qdsection.rb +3 -3
  26. data/lib/packetgen/header/dns/question.rb +37 -35
  27. data/lib/packetgen/header/dns/rr.rb +3 -3
  28. data/lib/packetgen/header/dns/rrsection.rb +2 -2
  29. data/lib/packetgen/header/dot11.rb +30 -51
  30. data/lib/packetgen/header/dot11/control.rb +5 -5
  31. data/lib/packetgen/header/dot11/data.rb +11 -7
  32. data/lib/packetgen/header/dot11/element.rb +16 -16
  33. data/lib/packetgen/header/dot11/management.rb +2 -2
  34. data/lib/packetgen/header/dot11/sub_mngt.rb +2 -12
  35. data/lib/packetgen/header/dot1q.rb +2 -2
  36. data/lib/packetgen/header/dot1x.rb +7 -20
  37. data/lib/packetgen/header/eap.rb +30 -33
  38. data/lib/packetgen/header/eap/fast.rb +2 -2
  39. data/lib/packetgen/header/eap/md5.rb +2 -2
  40. data/lib/packetgen/header/eap/tls.rb +2 -2
  41. data/lib/packetgen/header/eap/ttls.rb +2 -2
  42. data/lib/packetgen/header/eth.rb +13 -11
  43. data/lib/packetgen/header/gre.rb +2 -2
  44. data/lib/packetgen/header/http.rb +2 -0
  45. data/lib/packetgen/header/http/headers.rb +6 -4
  46. data/lib/packetgen/header/http/request.rb +36 -21
  47. data/lib/packetgen/header/http/response.rb +7 -7
  48. data/lib/packetgen/header/http/verbs.rb +3 -3
  49. data/lib/packetgen/header/icmp.rb +2 -2
  50. data/lib/packetgen/header/icmpv6.rb +2 -2
  51. data/lib/packetgen/header/igmp.rb +4 -4
  52. data/lib/packetgen/header/igmpv3.rb +3 -3
  53. data/lib/packetgen/header/igmpv3/group_record.rb +8 -6
  54. data/lib/packetgen/header/igmpv3/mq.rb +2 -2
  55. data/lib/packetgen/header/igmpv3/mr.rb +2 -2
  56. data/lib/packetgen/header/ip.rb +30 -31
  57. data/lib/packetgen/header/ip/addr.rb +10 -3
  58. data/lib/packetgen/header/ip/option.rb +8 -10
  59. data/lib/packetgen/header/ip/options.rb +3 -5
  60. data/lib/packetgen/header/ipv6.rb +2 -2
  61. data/lib/packetgen/header/ipv6/addr.rb +9 -2
  62. data/lib/packetgen/header/ipv6/extension.rb +2 -2
  63. data/lib/packetgen/header/ipv6/hop_by_hop.rb +3 -3
  64. data/lib/packetgen/header/llc.rb +2 -2
  65. data/lib/packetgen/header/mdns.rb +2 -2
  66. data/lib/packetgen/header/mld.rb +2 -2
  67. data/lib/packetgen/header/mldv2.rb +2 -2
  68. data/lib/packetgen/header/mldv2/mcast_address_record.rb +4 -2
  69. data/lib/packetgen/header/mldv2/mlq.rb +2 -2
  70. data/lib/packetgen/header/mldv2/mlr.rb +2 -2
  71. data/lib/packetgen/header/ospfv2.rb +9 -9
  72. data/lib/packetgen/header/ospfv2/db_description.rb +2 -2
  73. data/lib/packetgen/header/ospfv2/hello.rb +2 -2
  74. data/lib/packetgen/header/ospfv2/ls_ack.rb +2 -2
  75. data/lib/packetgen/header/ospfv2/ls_request.rb +4 -2
  76. data/lib/packetgen/header/ospfv2/ls_update.rb +2 -2
  77. data/lib/packetgen/header/ospfv2/lsa.rb +9 -5
  78. data/lib/packetgen/header/ospfv2/lsa_header.rb +8 -7
  79. data/lib/packetgen/header/ospfv3.rb +2 -2
  80. data/lib/packetgen/header/ospfv3/db_description.rb +2 -2
  81. data/lib/packetgen/header/ospfv3/hello.rb +2 -2
  82. data/lib/packetgen/header/ospfv3/ipv6_prefix.rb +4 -2
  83. data/lib/packetgen/header/ospfv3/ls_ack.rb +2 -2
  84. data/lib/packetgen/header/ospfv3/ls_request.rb +4 -2
  85. data/lib/packetgen/header/ospfv3/ls_update.rb +2 -2
  86. data/lib/packetgen/header/ospfv3/lsa.rb +5 -5
  87. data/lib/packetgen/header/ospfv3/lsa_header.rb +9 -8
  88. data/lib/packetgen/header/snmp.rb +33 -29
  89. data/lib/packetgen/header/tcp.rb +4 -23
  90. data/lib/packetgen/header/tcp/option.rb +11 -11
  91. data/lib/packetgen/header/tcp/options.rb +2 -2
  92. data/lib/packetgen/header/tftp.rb +6 -6
  93. data/lib/packetgen/header/udp.rb +3 -3
  94. data/lib/packetgen/headerable.rb +5 -4
  95. data/lib/packetgen/inject.rb +23 -0
  96. data/lib/packetgen/inspect.rb +23 -20
  97. data/lib/packetgen/packet.rb +82 -53
  98. data/lib/packetgen/pcap.rb +29 -0
  99. data/lib/packetgen/pcapng.rb +13 -13
  100. data/lib/packetgen/pcapng/block.rb +26 -13
  101. data/lib/packetgen/pcapng/epb.rb +25 -22
  102. data/lib/packetgen/pcapng/file.rb +260 -138
  103. data/lib/packetgen/pcapng/idb.rb +36 -38
  104. data/lib/packetgen/pcapng/shb.rb +51 -53
  105. data/lib/packetgen/pcapng/spb.rb +19 -19
  106. data/lib/packetgen/pcapng/unknown_block.rb +5 -13
  107. data/lib/packetgen/pcaprub_wrapper.rb +81 -0
  108. data/lib/packetgen/proto.rb +2 -2
  109. data/lib/packetgen/types.rb +3 -0
  110. data/lib/packetgen/types/abstract_tlv.rb +27 -7
  111. data/lib/packetgen/types/array.rb +22 -15
  112. data/lib/packetgen/types/cstring.rb +57 -20
  113. data/lib/packetgen/types/enum.rb +7 -2
  114. data/lib/packetgen/types/fieldable.rb +65 -0
  115. data/lib/packetgen/types/fields.rb +182 -117
  116. data/lib/packetgen/types/int.rb +18 -6
  117. data/lib/packetgen/types/int_string.rb +10 -2
  118. data/lib/packetgen/types/length_from.rb +20 -12
  119. data/lib/packetgen/types/oui.rb +4 -2
  120. data/lib/packetgen/types/string.rb +59 -8
  121. data/lib/packetgen/types/tlv.rb +4 -2
  122. data/lib/packetgen/utils.rb +4 -4
  123. data/lib/packetgen/utils/arp_spoofer.rb +2 -2
  124. data/lib/packetgen/version.rb +3 -3
  125. metadata +39 -61
  126. data/.gitignore +0 -13
  127. data/.rubocop.yml +0 -30
  128. data/.travis.yml +0 -19
  129. data/Gemfile +0 -4
  130. data/Rakefile +0 -21
  131. data/packetgen.gemspec +0 -36
@@ -1,10 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is part of PacketGen
2
4
  # See https://github.com/sdaubert/packetgen for more informations
3
5
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
4
6
  # This program is published under MIT license.
5
7
 
6
- # frozen_string_literal: true
7
-
8
8
  module PacketGen
9
9
  module Header
10
10
  class DNS
@@ -98,7 +98,7 @@ module PacketGen
98
98
  def human_rrclass
99
99
  if self[:name].dns.is_a? MDNS
100
100
  str = self.class::CLASSES.key(self.rrclass & 0x7fff) || '0x%04x' % (self.rrclass & 0x7fff)
101
- str += ' CACHE-FLUSH' if self.rrclass & 0x8000 > 0
101
+ str += ' CACHE-FLUSH' if (self.rrclass & 0x8000).positive?
102
102
  str
103
103
  else
104
104
  self.class::CLASSES.key(self.rrclass) || '0x%04x' % self.rrclass
@@ -1,10 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is part of PacketGen
2
4
  # See https://github.com/sdaubert/packetgen for more informations
3
5
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
4
6
  # This program is published under MIT license.
5
7
 
6
- # frozen_string_literal: true
7
-
8
8
  module PacketGen
9
9
  module Header
10
10
  class DNS
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  # This file is part of PacketGen
3
5
  # See https://github.com/sdaubert/packetgen for more informations
4
6
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
5
7
  # This program is published under MIT license.
6
8
 
7
- # frozen_string_literal: true
8
-
9
9
  require 'zlib'
10
10
 
11
11
  module PacketGen
@@ -28,7 +28,7 @@ module PacketGen
28
28
  define_field :dlt, Types::Int32le
29
29
  # @!attribute ppi_fields
30
30
  # @return [Type::String] concatenation of PPI fields
31
- define_field :ppi_fields, Types::String
31
+ define_field :ppi_fields, Types::String, builder: ->(h, t) { t.new(length_from: -> { h.length - 8 }) }
32
32
  # @!attribute body
33
33
  # @return [Type::String]
34
34
  define_field :body, Types::String
@@ -36,21 +36,6 @@ module PacketGen
36
36
  # @return [Boolean] align flag from {#flags} attribute
37
37
  define_bit_fields_on :flags, :reserved, 7, :align
38
38
 
39
- # @param [String] str
40
- # @return [PPI] self
41
- def read(str)
42
- return self if str.nil?
43
-
44
- force_binary str
45
- self[:version].read str[0, 1]
46
- self[:flags].read str[1, 1]
47
- self[:length].read str[2, 2]
48
- self[:dlt].read str[4, 4]
49
- self[:ppi_fields].read str[8, length - 8]
50
- self[:body].read str[length, str.size]
51
- self
52
- end
53
-
54
39
  # Check version field
55
40
  # @see [Base#parse?]
56
41
  def parse?
@@ -68,9 +53,7 @@ module PacketGen
68
53
  # @param [String] iface interface name
69
54
  # @return [void]
70
55
  def to_w(iface)
71
- pcap = PCAPRUB::Pcap.open_live(iface, PCAP_SNAPLEN, PCAP_PROMISC, PCAP_TIMEOUT)
72
- pcap.inject self.to_s
73
- pcap.close
56
+ Inject.inject(iface: iface, data: self)
74
57
  end
75
58
  end
76
59
  self.add_class PPI
@@ -93,26 +76,11 @@ module PacketGen
93
76
  define_field :present_flags, Types::Int32le
94
77
  # @!attribute radio_fields
95
78
  # @return [Type::String] concatenation of RadioTap fields
96
- define_field :radio_fields, Types::String
79
+ define_field :radio_fields, Types::String, builder: ->(h, t) { t.new(length_from: -> { h.length - 8 }) }
97
80
  # @!attribute body
98
81
  # @return [Type::String]
99
82
  define_field :body, Types::String
100
83
 
101
- # @param [String] str
102
- # @return [RadioTap] self
103
- def read(str)
104
- return self if str.nil?
105
-
106
- force_binary str
107
- self[:version].read str[0, 1]
108
- self[:pad].read str[1, 1]
109
- self[:length].read str[2, 2]
110
- self[:present_flags].read str[4, 4]
111
- self[:radio_fields].read str[8, length - 8]
112
- self[:body].read str[length, str.size]
113
- self
114
- end
115
-
116
84
  # Check version field
117
85
  # @see [Base#parse?]
118
86
  def parse?
@@ -130,10 +98,7 @@ module PacketGen
130
98
  # @param [String] iface interface name
131
99
  # @return [void]
132
100
  def to_w(iface)
133
- pcap = PCAPRUB::Pcap.open_live(iface, PCAP_SNAPLEN, PCAP_PROMISC,
134
- PCAP_TIMEOUT)
135
- pcap.inject self.to_s
136
- pcap.close
101
+ Inject.inject(iface: iface, data: self)
137
102
  end
138
103
  end
139
104
  self.add_class RadioTap
@@ -315,7 +280,7 @@ module PacketGen
315
280
  def read(str)
316
281
  fcs = Dot11.fcs?
317
282
 
318
- if self.class == Dot11
283
+ if self.instance_of? Dot11
319
284
  return self if str.nil?
320
285
 
321
286
  force_binary str
@@ -358,7 +323,7 @@ module PacketGen
358
323
 
359
324
  # @return [String]
360
325
  def inspect
361
- str = if self.class == Dot11
326
+ str = if self.instance_of? Dot11
362
327
  Inspect.dashed_line("#{self.class} #{human_type}", 1)
363
328
  elsif self.respond_to? :human_subtype
364
329
  Inspect.dashed_line("#{self.class} #{human_subtype}", 1)
@@ -379,10 +344,7 @@ module PacketGen
379
344
  # @param [String] iface interface name
380
345
  # @return [void]
381
346
  def to_w(iface)
382
- pcap = PCAPRUB::Pcap.open_live(iface, PCAP_SNAPLEN, PCAP_PROMISC,
383
- PCAP_TIMEOUT)
384
- pcap.inject self.to_s
385
- pcap.close
347
+ Inject.inject(iface: iface, data: self)
386
348
  end
387
349
 
388
350
  # Callback called when a Dot11 header is added to a packet
@@ -398,27 +360,44 @@ module PacketGen
398
360
 
399
361
  private
400
362
 
401
- def define_applicable_fields
363
+ def remove_from_applicable_fields(fields)
364
+ fields = [fields] unless fields.is_a? Array
365
+ @applicable_fields -= fields
366
+ end
367
+
368
+ def handle_mac4
402
369
  if to_ds? && from_ds?
403
370
  @applicable_fields[6, 0] = :mac4 unless @applicable_fields.include? :mac4
404
371
  else
405
- @applicable_fields -= %i[mac4]
372
+ remove_from_applicable_fields :mac4
406
373
  end
374
+ end
375
+
376
+ def handle_ht_ctrl
407
377
  if order?
408
378
  unless @applicable_fields.include? :ht_ctrl
409
379
  idx = @applicable_fields.index(:body)
410
380
  @applicable_fields[idx, 0] = :ht_ctrl
411
381
  end
412
382
  else
413
- @applicable_fields -= %i[ht_ctrl]
383
+ remove_from_applicable_fields %i[ht_ctrl]
414
384
  end
385
+ end
386
+
387
+ def handle_fcs
415
388
  if Dot11.fcs?
416
389
  @applicable_fields << :fcs unless @applicable_fields.include? :fcs
417
390
  else
418
- @applicable_fields -= %i[fcs]
391
+ remove_from_applicable_fields :fcs
419
392
  end
420
393
  end
421
394
 
395
+ def define_applicable_fields
396
+ handle_mac4
397
+ handle_ht_ctrl
398
+ handle_fcs
399
+ end
400
+
422
401
  def private_read(str, fcs)
423
402
  self[:frame_ctrl].read str[0, 2]
424
403
  define_applicable_fields
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  # This file is part of PacketGen
3
5
  # See https://github.com/sdaubert/packetgen for more informations
4
6
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
5
7
  # This program is published under MIT license.
6
8
 
7
- # frozen_string_literal: true
8
-
9
9
  module PacketGen
10
10
  module Header
11
11
  class Dot11
@@ -25,9 +25,9 @@ module PacketGen
25
25
  class Control < Dot11
26
26
  # Control subtypes
27
27
  SUBTYPES = {
28
- 7 => 'Wrapper',
29
- 8 => 'Block Ack Request',
30
- 9 => 'Block Ack',
28
+ 7 => 'Wrapper',
29
+ 8 => 'Block Ack Request',
30
+ 9 => 'Block Ack',
31
31
  10 => 'PS-Poll',
32
32
  11 => 'RTS',
33
33
  12 => 'CTS',
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  # This file is part of PacketGen
3
5
  # See https://github.com/sdaubert/packetgen for more informations
4
6
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
5
7
  # This program is published under MIT license.
6
8
 
7
- # frozen_string_literal: true
8
-
9
9
  module PacketGen
10
10
  module Header
11
11
  class Dot11
@@ -44,22 +44,22 @@ module PacketGen
44
44
  case ds
45
45
  when 0
46
46
  # MAC1: RA/DA, MAC2: TA/SA
47
- self[:mac1], self[:mac2] = self[:mac2], self[:mac1]
47
+ invert_mac :mac1, :mac2
48
48
  when 1
49
49
  # MAC1: RA/BSSID, MAC2: TA/SA, MAC3: DA
50
- self[:mac2], self[:mac1] = self[:mac1], self[:mac2]
50
+ invert_mac :mac1, :mac2
51
51
  self.to_ds = false
52
52
  self.from_ds = true
53
53
  when 2
54
54
  # MAC1: RA/DA, MAC2: BSSID, MAC3: SA or BSSID
55
- self[:mac1], self[:mac2] = self[:mac2], self[:mac1]
55
+ invert_mac :mac1, :mac2
56
56
  self.to_ds = true
57
57
  self.from_ds = false
58
58
  when 3
59
59
  # MAC1: RA, MAC2: TA
60
- self[:mac1], self[:mac2] = self[:mac2], self[:mac1]
60
+ invert_mac :mac1, :mac2
61
61
  # MAC3: DA, MAC4: SA
62
- self[:mac4], self[:mac3] = self[:mac3], self[:mac4]
62
+ invert_mac :mac3, :mac4
63
63
  end
64
64
  self
65
65
  end
@@ -135,6 +135,10 @@ module PacketGen
135
135
  @applicable_fields -= %i[qos_ctrl]
136
136
  end
137
137
  end
138
+
139
+ def invert_mac(mac1, mac2)
140
+ self[mac1], self[mac2] = self[mac2], self[mac1]
141
+ end
138
142
  end
139
143
  end
140
144
  end
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  # This file is part of PacketGen
3
5
  # See https://github.com/sdaubert/packetgen for more informations
4
6
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
5
7
  # This program is published under MIT license.
6
8
 
7
- # frozen_string_literal: true
8
-
9
9
  module PacketGen
10
10
  module Header
11
11
  class Dot11
@@ -22,20 +22,20 @@ module PacketGen
22
22
  class Element
23
23
  # Known element types
24
24
  TYPES = {
25
- 0 => 'SSID',
26
- 1 => 'Rates',
27
- 2 => 'FHset',
28
- 3 => 'DSset',
29
- 4 => 'CFset',
30
- 5 => 'TIM',
31
- 6 => 'IBSSset',
32
- 16 => 'challenge',
33
- 42 => 'ERPinfo',
34
- 46 => 'QoS Cap.',
35
- 47 => 'ERPinfo',
36
- 48 => 'RSNinfo',
37
- 50 => 'ESRates',
38
- 68 => 'reserved',
25
+ 0 => 'SSID',
26
+ 1 => 'Rates',
27
+ 2 => 'FHset',
28
+ 3 => 'DSset',
29
+ 4 => 'CFset',
30
+ 5 => 'TIM',
31
+ 6 => 'IBSSset',
32
+ 16 => 'challenge',
33
+ 42 => 'ERPinfo',
34
+ 46 => 'QoS Cap.',
35
+ 47 => 'ERPinfo',
36
+ 48 => 'RSNinfo',
37
+ 50 => 'ESRates',
38
+ 68 => 'reserved',
39
39
  221 => 'vendor'
40
40
  }.freeze
41
41
  end
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  # This file is part of PacketGen
3
5
  # See https://github.com/sdaubert/packetgen for more informations
4
6
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
5
7
  # This program is published under MIT license.
6
8
 
7
- # frozen_string_literal: true
8
-
9
9
  module PacketGen
10
10
  module Header
11
11
  class Dot11
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  # This file is part of PacketGen
3
5
  # See https://github.com/sdaubert/packetgen for more informations
4
6
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
5
7
  # This program is published under MIT license.
6
8
 
7
- # frozen_string_literal: true
8
-
9
9
  module PacketGen
10
10
  module Header
11
11
  class Dot11
@@ -21,16 +21,6 @@ module PacketGen
21
21
  # @return [Array<Element>]
22
22
  define_field :elements, ArrayOfElements
23
23
 
24
- # @return [String]
25
- #def inspect
26
- # str = super
27
- # str << Inspect.dashed_line('Dot11 Elements', 2)
28
- # @elements.each do |el|
29
- # str << Inspect.shift_level(2) << el.to_human << "\n"
30
- # end
31
- # str
32
- #end
33
-
34
24
  # Add an {Element} to header
35
25
  # @param [Integer,String] type element type
36
26
  # @param [Object] value element value
@@ -1,10 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is part of PacketGen
2
4
  # See https://github.com/sdaubert/packetgen for more informations
3
5
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
4
6
  # This program is published under MIT license.
5
7
 
6
- # frozen_string_literal: true
7
-
8
8
  module PacketGen
9
9
  module Header
10
10
  # IEEE 802.1Q VLAN tagging
@@ -1,10 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is part of PacketGen
2
4
  # See https://github.com/sdaubert/packetgen for more informations
3
5
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
4
6
  # This program is published under MIT license.
5
7
 
6
- # frozen_string_literal: true
7
-
8
8
  module PacketGen
9
9
  module Header
10
10
  # IEEE 802.1X / EAPOL
@@ -27,10 +27,10 @@ module PacketGen
27
27
 
28
28
  # IEEE 802.1X packet types
29
29
  TYPES = {
30
- 'EAP Packet' => 0,
31
- 'Start' => 1,
32
- 'Logoff' => 2,
33
- 'Key' => 3,
30
+ 'EAP Packet' => 0,
31
+ 'Start' => 1,
32
+ 'Logoff' => 2,
33
+ 'Key' => 3,
34
34
  'Encap-ASF-Alert' => 4
35
35
  }.freeze
36
36
 
@@ -45,20 +45,7 @@ module PacketGen
45
45
  define_field :length, Types::Int16
46
46
  # @!attribute body
47
47
  # @return [Types::String,Header::Base]
48
- define_field :body, Types::String
49
-
50
- # Populate object from string
51
- # @param [String] str
52
- # @return [self]
53
- def read(str)
54
- return self if str.nil?
55
-
56
- self[:version].read(str[0, 1])
57
- self[:type].read(str[1, 1])
58
- self[:length].read(str[2, 2])
59
- self[:body].read(str[4, self.length])
60
- self
61
- end
48
+ define_field :body, Types::String, builder: ->(h, t) { t.new(length_from: h[:length]) }
62
49
 
63
50
  # Get human readable type
64
51
  # @return [String]
@@ -1,10 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is part of PacketGen
2
4
  # See https://github.com/sdaubert/packetgen for more informations
3
5
  # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
4
6
  # This program is published under MIT license.
5
7
 
6
- # frozen_string_literal: true
7
-
8
8
  module PacketGen
9
9
  module Header
10
10
  # Extensible Authentication Protocol (EAP),
@@ -58,25 +58,25 @@ module PacketGen
58
58
  class EAP < Base
59
59
  # EAP known codes
60
60
  CODES = {
61
- 'Request' => 1,
62
- 'Response' => 2,
63
- 'Success' => 3,
64
- 'Failure' => 4
61
+ 'Request' => 1,
62
+ 'Response' => 2,
63
+ 'Success' => 3,
64
+ 'Failure' => 4
65
65
  }.freeze
66
66
 
67
67
  # EAP known request/response types
68
68
  TYPES = {
69
- 'Identity' => 1,
70
- 'Notification' => 2,
71
- 'Nak' => 3,
72
- 'MD5-Challenge' => 4,
73
- 'One Time Password' => 5,
69
+ 'Identity' => 1,
70
+ 'Notification' => 2,
71
+ 'Nak' => 3,
72
+ 'MD5-Challenge' => 4,
73
+ 'One Time Password' => 5,
74
74
  'Generic Token Card' => 6,
75
- 'EAP-TLS' => 13,
76
- 'EAP-TTLS' => 21,
77
- 'EAP-FAST' => 43,
78
- 'Expanded Types' => 254,
79
- 'Experimental Use' => 255
75
+ 'EAP-TLS' => 13,
76
+ 'EAP-TTLS' => 21,
77
+ 'EAP-FAST' => 43,
78
+ 'Expanded Types' => 254,
79
+ 'Experimental Use' => 255
80
80
  }.freeze
81
81
 
82
82
  # @!attribute code
@@ -95,7 +95,8 @@ module PacketGen
95
95
  # This field is present only for Request or Response packets,
96
96
  # with type different from Expanded Types (254).
97
97
  # @return [Integer] 8-bit request or response type
98
- define_field :type, Types::Int8Enum, enum: TYPES,
98
+ define_field :type, Types::Int8Enum,
99
+ enum: TYPES,
99
100
  optional: ->(eap) { eap.type? }
100
101
 
101
102
  # @!attribute vendor_id
@@ -131,22 +132,18 @@ module PacketGen
131
132
  # may be determined
132
133
  def read(str)
133
134
  super str
134
- return self unless self.class == EAP
135
-
136
- if type?
137
- obj = case self.type
138
- when 4
139
- EAP::MD5.new.read(str)
140
- when 13
141
- EAP::TLS.new.read(str)
142
- when 21
143
- EAP::TTLS.new.read(str)
144
- when 43
145
- EAP::FAST.new.read(str)
146
- else
147
- self
148
- end
149
- obj
135
+ return self unless self.instance_of?(EAP)
136
+ return self unless type?
137
+
138
+ case self.type
139
+ when 4
140
+ EAP::MD5.new.read(str)
141
+ when 13
142
+ EAP::TLS.new.read(str)
143
+ when 21
144
+ EAP::TTLS.new.read(str)
145
+ when 43
146
+ EAP::FAST.new.read(str)
150
147
  else
151
148
  self
152
149
  end