packetgen 3.3.3 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +38 -22
  3. data/lib/packetgen/capture.rb +2 -2
  4. data/lib/packetgen/config.rb +0 -1
  5. data/lib/packetgen/deprecation.rb +14 -8
  6. data/lib/packetgen/header/arp.rb +17 -18
  7. data/lib/packetgen/header/asn1_base.rb +2 -1
  8. data/lib/packetgen/header/base.rb +42 -40
  9. data/lib/packetgen/header/bootp.rb +35 -37
  10. data/lib/packetgen/header/dhcp/option.rb +21 -21
  11. data/lib/packetgen/header/dhcp/options.rb +3 -3
  12. data/lib/packetgen/header/dhcp.rb +8 -9
  13. data/lib/packetgen/header/dhcpv6/duid.rb +16 -16
  14. data/lib/packetgen/header/dhcpv6/option.rb +83 -61
  15. data/lib/packetgen/header/dhcpv6/options.rb +4 -4
  16. data/lib/packetgen/header/dhcpv6/relay.rb +6 -5
  17. data/lib/packetgen/header/dhcpv6.rb +17 -18
  18. data/lib/packetgen/header/dns/name.rb +21 -16
  19. data/lib/packetgen/header/dns/opt.rb +5 -2
  20. data/lib/packetgen/header/dns/option.rb +14 -14
  21. data/lib/packetgen/header/dns/qdsection.rb +3 -3
  22. data/lib/packetgen/header/dns/question.rb +7 -8
  23. data/lib/packetgen/header/dns/rr.rb +56 -43
  24. data/lib/packetgen/header/dns/rrsection.rb +6 -6
  25. data/lib/packetgen/header/dns.rb +103 -90
  26. data/lib/packetgen/header/dot11/control.rb +12 -12
  27. data/lib/packetgen/header/dot11/data.rb +25 -24
  28. data/lib/packetgen/header/dot11/element.rb +4 -4
  29. data/lib/packetgen/header/dot11/management.rb +21 -18
  30. data/lib/packetgen/header/dot11/sub_mngt.rb +40 -53
  31. data/lib/packetgen/header/dot11.rb +117 -122
  32. data/lib/packetgen/header/dot1q.rb +12 -13
  33. data/lib/packetgen/header/dot1x.rb +13 -13
  34. data/lib/packetgen/header/eap/fast.rb +4 -4
  35. data/lib/packetgen/header/eap/md5.rb +16 -8
  36. data/lib/packetgen/header/eap/tls.rb +18 -19
  37. data/lib/packetgen/header/eap/ttls.rb +22 -21
  38. data/lib/packetgen/header/eap.rb +73 -48
  39. data/lib/packetgen/header/eth.rb +41 -27
  40. data/lib/packetgen/header/gre.rb +33 -11
  41. data/lib/packetgen/header/http/headers.rb +7 -6
  42. data/lib/packetgen/header/http/request.rb +38 -29
  43. data/lib/packetgen/header/http/response.rb +35 -27
  44. data/lib/packetgen/header/http/verbs.rb +1 -3
  45. data/lib/packetgen/header/icmp.rb +14 -14
  46. data/lib/packetgen/header/icmpv6.rb +10 -9
  47. data/lib/packetgen/header/igmp.rb +26 -15
  48. data/lib/packetgen/header/igmpv3/group_record.rb +18 -13
  49. data/lib/packetgen/header/igmpv3/mq.rb +16 -18
  50. data/lib/packetgen/header/igmpv3/mr.rb +5 -5
  51. data/lib/packetgen/header/igmpv3.rb +12 -11
  52. data/lib/packetgen/header/ip/addr.rb +19 -15
  53. data/lib/packetgen/header/ip/option.rb +47 -36
  54. data/lib/packetgen/header/ip/options.rb +1 -1
  55. data/lib/packetgen/header/ip.rb +77 -95
  56. data/lib/packetgen/header/ipv6/addr.rb +28 -27
  57. data/lib/packetgen/header/ipv6/extension.rb +13 -11
  58. data/lib/packetgen/header/ipv6/hop_by_hop.rb +32 -13
  59. data/lib/packetgen/header/ipv6.rb +42 -35
  60. data/lib/packetgen/header/llc.rb +28 -21
  61. data/lib/packetgen/header/mdns.rb +10 -3
  62. data/lib/packetgen/header/mld.rb +15 -13
  63. data/lib/packetgen/header/mldv2/mcast_address_record.rb +17 -12
  64. data/lib/packetgen/header/mldv2/mlq.rb +22 -24
  65. data/lib/packetgen/header/mldv2/mlr.rb +8 -8
  66. data/lib/packetgen/header/mldv2.rb +1 -1
  67. data/lib/packetgen/header/ospfv2/db_description.rb +17 -18
  68. data/lib/packetgen/header/ospfv2/hello.rb +18 -17
  69. data/lib/packetgen/header/ospfv2/ls_ack.rb +6 -7
  70. data/lib/packetgen/header/ospfv2/ls_request.rb +14 -13
  71. data/lib/packetgen/header/ospfv2/ls_update.rb +9 -9
  72. data/lib/packetgen/header/ospfv2/lsa.rb +79 -60
  73. data/lib/packetgen/header/ospfv2/lsa_header.rb +12 -11
  74. data/lib/packetgen/header/ospfv2.rb +49 -46
  75. data/lib/packetgen/header/ospfv3/db_description.rb +20 -22
  76. data/lib/packetgen/header/ospfv3/hello.rb +17 -16
  77. data/lib/packetgen/header/ospfv3/ipv6_prefix.rb +22 -20
  78. data/lib/packetgen/header/ospfv3/ls_ack.rb +7 -8
  79. data/lib/packetgen/header/ospfv3/ls_request.rb +18 -18
  80. data/lib/packetgen/header/ospfv3/ls_update.rb +10 -10
  81. data/lib/packetgen/header/ospfv3/lsa.rb +62 -51
  82. data/lib/packetgen/header/ospfv3/lsa_header.rb +12 -11
  83. data/lib/packetgen/header/ospfv3.rb +54 -52
  84. data/lib/packetgen/header/sctp/chunk.rb +80 -56
  85. data/lib/packetgen/header/sctp/error.rb +174 -202
  86. data/lib/packetgen/header/sctp/padded32.rb +3 -3
  87. data/lib/packetgen/header/sctp/parameter.rb +89 -136
  88. data/lib/packetgen/header/sctp.rb +19 -8
  89. data/lib/packetgen/header/snmp.rb +108 -7
  90. data/lib/packetgen/header/tcp/option.rb +52 -39
  91. data/lib/packetgen/header/tcp/options.rb +13 -5
  92. data/lib/packetgen/header/tcp.rb +83 -65
  93. data/lib/packetgen/header/tftp.rb +31 -25
  94. data/lib/packetgen/header/udp.rb +21 -19
  95. data/lib/packetgen/header.rb +23 -18
  96. data/lib/packetgen/headerable.rb +21 -5
  97. data/lib/packetgen/inspect.rb +3 -8
  98. data/lib/packetgen/packet.rb +146 -71
  99. data/lib/packetgen/pcap.rb +15 -4
  100. data/lib/packetgen/pcapng/block.rb +20 -18
  101. data/lib/packetgen/pcapng/epb.rb +13 -15
  102. data/lib/packetgen/pcapng/file.rb +44 -111
  103. data/lib/packetgen/pcapng/idb.rb +11 -12
  104. data/lib/packetgen/pcapng/shb.rb +15 -16
  105. data/lib/packetgen/pcapng/spb.rb +9 -11
  106. data/lib/packetgen/pcapng/unknown_block.rb +6 -17
  107. data/lib/packetgen/pcapng.rb +6 -4
  108. data/lib/packetgen/pcaprub_wrapper.rb +17 -1
  109. data/lib/packetgen/proto.rb +5 -1
  110. data/lib/packetgen/unknown_packet.rb +5 -5
  111. data/lib/packetgen/utils/arp_spoofer.rb +18 -19
  112. data/lib/packetgen/utils.rb +4 -3
  113. data/lib/packetgen/version.rb +1 -1
  114. data/lib/packetgen.rb +12 -5
  115. metadata +29 -38
  116. data/lib/packetgen/types/abstract_tlv.rb +0 -278
  117. data/lib/packetgen/types/array.rb +0 -287
  118. data/lib/packetgen/types/cstring.rb +0 -109
  119. data/lib/packetgen/types/enum.rb +0 -171
  120. data/lib/packetgen/types/fieldable.rb +0 -66
  121. data/lib/packetgen/types/fields.rb +0 -622
  122. data/lib/packetgen/types/int.rb +0 -473
  123. data/lib/packetgen/types/int_string.rb +0 -102
  124. data/lib/packetgen/types/length_from.rb +0 -54
  125. data/lib/packetgen/types/oui.rb +0 -52
  126. data/lib/packetgen/types/string.rb +0 -97
  127. data/lib/packetgen/types/tlv.rb +0 -161
  128. data/lib/packetgen/types.rb +0 -26
@@ -11,10 +11,11 @@ module PacketGen
11
11
  class IPv6
12
12
  # @!parse
13
13
  # # Option for {HopByHop} IPv6 extension header.
14
- # # @since 3.1.0 subclass of {Types::AbstractTLV}
14
+ # # @since 2.4.0
15
+ # # @since 3.1.0 subclass of +BinStruct::AbstractTLV+
15
16
  # class Option <AbstractTLV; end
16
17
  # @private
17
- Option = Types::AbstractTLV.create
18
+ Option = BinStruct::AbstractTLV.create
18
19
 
19
20
  class Option
20
21
  # Known option types
@@ -23,6 +24,7 @@ module PacketGen
23
24
  5 => 'router_alert'
24
25
  }.freeze
25
26
 
27
+ # Get human-readable string
26
28
  # @return [String]
27
29
  def to_human
28
30
  case type
@@ -35,13 +37,16 @@ module PacketGen
35
37
  end
36
38
  Option.define_type_enum Option::TYPES.invert
37
39
 
38
- # Special option pad1, for {HopByHop} IPv6 extension header
40
+ # Special option pad1 (one-byte option), for {HopByHop} IPv6 extension header
39
41
  # @author Sylvain Daubert
40
- class Pad1 < Types::Fields
42
+ # @since 2.4.0
43
+ class Pad1 < BinStruct::Struct
41
44
  # @!attribute pad
42
- # @return [Integer]
43
- define_field :pad, Types::Int8, default: 0
45
+ # Pad1 option type
46
+ # @return [Integer]
47
+ define_attr :pad, BinStruct::Int8, default: 0
44
48
 
49
+ # Get human-readable string
45
50
  # @return [String]
46
51
  def to_human
47
52
  'pad1'
@@ -50,7 +55,8 @@ module PacketGen
50
55
 
51
56
  # Array of {Option}, for {HopByHop} IPv6 extension header
52
57
  # @author Sylvain Daubert
53
- class Options < Types::Array
58
+ # @since 2.4.0
59
+ class Options < BinStruct::Array
54
60
  set_of Option
55
61
 
56
62
  # Get options as a binary string. Add padding if needed.
@@ -96,18 +102,31 @@ module PacketGen
96
102
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97
103
  #
98
104
  # Hop-by-hop IPv6 extension header consists of:
99
- # * a {#next} header field ({Types::Int8}),
100
- # * a {#length} field ({Types::Int8}),
105
+ # * a {#next #next} header field (+BinStruct::Int8+),
106
+ # * a {#length #length} field (+BinStruct::Int8+),
101
107
  # * an {#options} field ({Options}),
102
- # * and a {#body}, containing next header.
108
+ # * and a {#body #body}, containing next header.
109
+ # @example
110
+ # pkt = PacketGen.gen('Eth').add('IPv6').add('IPv6::HopByHop').add('ICMPv6')
111
+ # pkt.ipv6_hopbyhop.options << { type: 'router_alert', value: "\x00\x00".b }
103
112
  # @author Sylvain Daubert
113
+ # @since 2.4.0
104
114
  class HopByHop < Extension
105
115
  # redefine options field
106
- remove_field :options
116
+ remove_attr :options
107
117
  # @!attribute options
108
- # Specific options of extension header
118
+ # Specific HopByHop options
109
119
  # @return [Options]
110
- define_field_before :body, :options, Options, builder: ->(h, t) { t.new(length_from: -> { h.real_length - 2 }) }
120
+ define_attr_before :body, :options, Options, builder: ->(h, t) { t.new(length_from: -> { h.real_length - 2 }) }
121
+
122
+ # Generate binary string. Add padding if needed in {#options}, and update {#length} accordingly.
123
+ # @return [String]
124
+ # @since 2.4.0
125
+ # @since 4.1.0 Set {Extension#length}.
126
+ def to_s
127
+ calc_length
128
+ super
129
+ end
111
130
  end
112
131
  end
113
132
 
@@ -37,26 +37,27 @@ module PacketGen
37
37
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38
38
  #
39
39
  # A IPv6 header consists of:
40
- # * a first 32-bit word ({#u32}, of {Types::Int32} type) composed of:
40
+ # * a first 32-bit word ({#u32}, of +BinStruct::Int32+ type) composed of:
41
41
  # * a 4-bit {#version} field,
42
42
  # * a 8-bit {#traffic_class} field,
43
43
  # * a 20-bit {#flow_label} field,
44
- # * a payload length field ({#length}, {Types::Int16} type}),
45
- # * a next header field ({#next}, {Types::Int8} type),
46
- # * a hop-limit field ({#hop}, +Int8+ type),
44
+ # * a payload length field ({#length}, +BinStruct::Int16+ type}),
45
+ # * a next header field ({#next}, +BinStruct::Int8+ type),
46
+ # * a hop-limit field ({#hop}, +BinStruct::Int8+ type),
47
47
  # * a source address field ({#src}, {IPv6::Addr} type),
48
- # * a destination address field ({#dst}, +IPv6::Addr+ type),
49
- # * and a {#body} ({Types::String} type).
48
+ # * a destination address field ({#dst}, {IPv6::Addr} type),
49
+ # * and a {#body} (+BinStruct::String+ type).
50
50
  #
51
- # == Create a IPv6 header
51
+ # @example Create a IPv6 header
52
52
  # # standalone
53
53
  # ipv6 = PacketGen::Header::IPv6.new
54
54
  # # in a packet
55
55
  # pkt = PacketGen.gen('IPv6')
56
56
  # # access to IPv6 header
57
- # pkt.ipv6 # => PacketGen::Header::IPv6
57
+ # pkt.ipv6.class # => PacketGen::Header::IPv6
58
58
  #
59
- # == IPv6 attributes
59
+ # @example IPv6 attributes
60
+ # ipv6 = PacketGen::Header::IPv6.new
60
61
  # ipv6.u32 = 0x60280001
61
62
  # # the same as
62
63
  # ipv6.version = 6
@@ -68,9 +69,9 @@ module PacketGen
68
69
  # ipv6.next = 6
69
70
  # ipv6.src = '::1'
70
71
  # ipv6.src # => "::1"
71
- # ipv6[:src] # => PacketGen::Header::IPv6::Addr
72
+ # ipv6[:src].class # => PacketGen::Header::IPv6::Addr
72
73
  # ipv6.dst = '2001:1234:5678:abcd::123'
73
- # ipv6.body.read 'this is a body'
74
+ # ipv6.body ='this is a body'
74
75
  #
75
76
  # == Add IPv6 extensions
76
77
  # In IPv6, optional extensions are encoded in separate headers that
@@ -84,6 +85,7 @@ module PacketGen
84
85
  # # Add another header
85
86
  # pkt.add('UDP')
86
87
  # @author Sylvain Daubert
88
+ # @author LemonTree55
87
89
  class IPv6 < Base; end
88
90
 
89
91
  require_relative 'ipv6/addr'
@@ -95,40 +97,42 @@ module PacketGen
95
97
  # @!attribute u32
96
98
  # First 32-bit word of IPv6 header
97
99
  # @return [Integer]
98
- define_field :u32, Types::Int32, default: 0x6000_0000
100
+ # @!attribute version
101
+ # 4-bit version attribute
102
+ # @return [Integer]
103
+ # @!attribute traffic_class
104
+ # 8-bit traffic_class attribute
105
+ # @return [Integer]
106
+ # @!attribute flow_label
107
+ # 20-bit flow_label attribute
108
+ # @return [Integer]
109
+ define_bit_attr :u32, default: 0x60000000, version: 4, traffic_class: 8, flow_label: 20
99
110
  # @!attribute length
100
111
  # 16-bit word of IPv6 payload length
101
112
  # @return [Integer]
102
- define_field :length, Types::Int16
113
+ define_attr :length, BinStruct::Int16
103
114
  # @!attribute next
104
115
  # 8-bit IPv6 next payload value
105
116
  # @return [Integer]
106
- define_field :next, Types::Int8
117
+ define_attr :next, BinStruct::Int8
107
118
  # @!attribute hop
108
119
  # 8-bit IPv6 hop limit
109
120
  # @return [Integer]
110
- define_field :hop, Types::Int8, default: 64
121
+ define_attr :hop, BinStruct::Int8, default: 64
111
122
  # @!attribute src
112
123
  # IPv6 source address
113
124
  # @return [Addr]
114
- define_field :src, Addr, default: '::1'
125
+ define_attr :src, Addr, default: '::1'
115
126
  # @!attribute dst
116
127
  # IPv6 destination address
117
128
  # @return [Addr]
118
- define_field :dst, Addr, default: '::1'
129
+ define_attr :dst, Addr, default: '::1'
119
130
  # @!attribute body
120
- # @return [Types::String,Header::Base]
121
- define_field :body, Types::String
122
-
123
- # @!attribute version
124
- # @return [Integer] 4-bit version attribute
125
- # @!attribute traffic_class
126
- # @return [Integer] 8-bit traffic_class attribute
127
- # @!attribute flow_label
128
- # @return [Integer] 20-bit flow_label attribute
129
- define_bit_fields_on :u32, :version, 4, :traffic_class, 8, :flow_label, 20
131
+ # IPv6 body
132
+ # @return [BinStruct::String,Headerable]
133
+ define_attr :body, BinStruct::String
130
134
 
131
- # Compute length and set +len+ field
135
+ # Compute length and set {#length} field
132
136
  # @return [Integer]
133
137
  def calc_length
134
138
  Base.calculate_and_set_length self, header_in_size: false
@@ -143,14 +147,14 @@ module PacketGen
143
147
  sum
144
148
  end
145
149
 
146
- # Send IPv6 packet on wire. All fields may be set (even {#version}).
150
+ # Send IPv6 packet on wire. All attributes may be set (even {#version}).
147
151
  # @param [String] _iface interface name (not used)
148
152
  # @return [void]
149
153
  # @since 3.0.0 no more limitations on +flow_label+, +length+ and +src+ fields.
150
154
  def to_w(_iface=nil)
151
155
  sock = Socket.new(Socket::AF_INET6, Socket::SOCK_RAW, Socket::IPPROTO_RAW)
152
156
  sockaddrin = Socket.sockaddr_in(0, dst)
153
- sock.send to_s, 0, sockaddrin
157
+ sock.send(to_s, 0, sockaddrin)
154
158
  sock.close
155
159
  end
156
160
 
@@ -170,7 +174,7 @@ module PacketGen
170
174
  end
171
175
 
172
176
  # Check version field
173
- # @see [Base#parse?]
177
+ # @see Base#parse?
174
178
  def parse?
175
179
  version == 6
176
180
  end
@@ -199,15 +203,18 @@ module PacketGen
199
203
  module Header
200
204
  class IPv6
201
205
  class << self
206
+ # @private
202
207
  alias old_bind bind
203
208
 
204
209
  # Bind a upper header to IPv6 and its defined extension headers.
205
210
  # @see Base.bind
211
+ # @author LemonTree55
206
212
  def bind(header_klass, args={})
207
- IPv6.old_bind header_klass, args
208
- [IPv6::HopByHop].each do |klass|
209
- klass.bind header_klass, args
210
- end
213
+ IPv6.old_bind(header_klass, args)
214
+ IPv6.constants
215
+ .map { |cname| IPv6.const_get(cname) }
216
+ .select { |klass| klass.is_a?(Class) && (klass < Extension) }
217
+ .each { |klass| klass.bind(header_klass, args) }
211
218
  end
212
219
  end
213
220
  end
@@ -12,25 +12,29 @@ module PacketGen
12
12
  # Logical-Link Control header
13
13
  #
14
14
  # A LLC header consists of:
15
- # * a {#dsap} ({Types::Int8}),
16
- # * a {#ssap} ({Types::Int8}),
17
- # * a {#control} ({Types::Int8}),
18
- # * and a {#body} (a {Types::String} or another {Base} class).
15
+ # * a {#dsap} (+BinStruct::Int8+),
16
+ # * a {#ssap} (+BinStruct::Int8+),
17
+ # * a {#control} (+BinStruct::Int8+),
18
+ # * and a {#body} (a +BinStruct::String+ or another {Headerable} class).
19
19
  # @author Sylvain Daubert
20
20
  # @since 1.4.0
21
21
  class LLC < Base
22
22
  # @!attribute dsap
23
- # @return [Integer] 8-bit dsap value
24
- define_field :dsap, Types::Int8
23
+ # 8-bit Destination Service Access Point value
24
+ # @return [Integer]
25
+ define_attr :dsap, BinStruct::Int8
25
26
  # @!attribute ssap
26
- # @return [Integer] 8-bit ssap value
27
- define_field :ssap, Types::Int8
27
+ # 8-bit Source Service Access Point value
28
+ # @return [Integer]
29
+ define_attr :ssap, BinStruct::Int8
28
30
  # @!attribute control
29
- # @return [Integer] 8-bit control value
30
- define_field :control, Types::Int8
31
+ # 8-bit control value
32
+ # @return [Integer]
33
+ define_attr :control, BinStruct::Int8
31
34
  # @!attribute body
32
- # @return [Types::String,Header::Base]
33
- define_field :body, Types::String
35
+ # LLC body
36
+ # @return [BinStruct::String,Headerable]
37
+ define_attr :body, BinStruct::String
34
38
  end
35
39
  self.add_class LLC
36
40
  Dot11::Data.bind LLC, type: 2, wep?: false
@@ -38,21 +42,24 @@ module PacketGen
38
42
  # Sub-Network Access Protocol
39
43
  #
40
44
  # A SNAP header consists of:
41
- # * a {#oui} ({Types::OUI}),
42
- # * a {#proto_id} ({Types::Int16}),
43
- # * and a {#body} (a {Types::String} or another {Base} class).
45
+ # * a {#oui} (+BinStruct::OUI+),
46
+ # * a {#proto_id} (+BinStruct::Int16+),
47
+ # * and a {#body} (a +BinStruct::String+ or another {Headerable} class).
44
48
  # @author Sylvain Daubert
45
49
  # @since 1.4.0
46
50
  class SNAP < Base
47
51
  # @!attribute oui
48
- # @return [Types::OUI]
49
- define_field :oui, Types::OUI
52
+ # If +00:00:00+, {#proto_id} is an EtherType. Else, {#proto_id} is specified by organization specified BY +OUI+.
53
+ # @return [BinStruct::OUI]
54
+ define_attr :oui, BinStruct::OUI
50
55
  # @!attribute proto_id
51
- # @return [Integer] 16-bit protocol id
52
- define_field :proto_id, Types::Int16
56
+ # 16-bit protocol id
57
+ # @return [Integer]
58
+ define_attr :proto_id, BinStruct::Int16
53
59
  # @!attribute body
54
- # @return [Types::String,Header::Base]
55
- define_field :body, Types::String
60
+ # SNAP header
61
+ # @return [BinStruct::String,Headerable]
62
+ define_attr :body, BinStruct::String
56
63
  end
57
64
  self.add_class SNAP
58
65
  LLC.bind SNAP, dsap: 170, ssap: 170, control: 3
@@ -21,12 +21,18 @@ module PacketGen
21
21
  # Fixup IP header according to RFC 6762:
22
22
  # * set ethernet multicast address to +01:00:5E:00:00:FB+ (for IPv4)
23
23
  # or +33:33:00:00:00:FB+ (for IPv6),
24
- # * set IPv4 address to 224.0.0.251 or IPv6 address to ff02::fb.
24
+ # * set IPv4 address to +224.0.0.251+ or IPv6 address to +ff02::fb+.
25
25
  # This method may be called as:
26
26
  # # first way
27
27
  # pkt.mdns.mdnsize
28
28
  # # second way
29
- # pkt.mdnsize
29
+ # pkt.
30
+ # @example
31
+ # pkt = PacketGen.gen('Eth').add('IP').add('UDP').add('MDNS')
32
+ # pkt.mdnsize
33
+ # pkt.eth.dst #=> "01:00:5e:00:00:fb"
34
+ # pkt.ip.dst #=> "224.0.0.251"
35
+ # @return [void]
30
36
  def mdnsize
31
37
  iph = ip_header(self)
32
38
  case iph
@@ -42,12 +48,13 @@ module PacketGen
42
48
  # @api private
43
49
  # @note This method is used internally by PacketGen and should not be
44
50
  # directly called
51
+ # Add +#mdnsize+ method to +packet+. This method calls {#mdnsize}.
45
52
  # @since 2.7.0 Set UDP sport according to bindings, only if sport is 0.
46
53
  # Needed by new bind API.
47
54
  def added_to_packet(packet)
48
55
  mdns_idx = packet.headers.size
49
56
  packet.instance_eval "def mdnsize() @headers[#{mdns_idx}].mdnsize; end" # def mdnsize() @headers[4].mdnsize; end
50
- return unless packet.is? 'UDP'
57
+ return unless packet.is?('UDP')
51
58
  return unless packet.udp.sport.zero?
52
59
 
53
60
  packet.udp.sport = UDP_PORT
@@ -8,7 +8,7 @@
8
8
 
9
9
  module PacketGen
10
10
  module Header
11
- # This class supports MLDv1 (RFC 2710).
11
+ # This class supports Multicast Listener Discovery for IPv6 (RFC 2710).
12
12
  #
13
13
  # From RFC 2710, a MLD header has the following format:
14
14
  # 0 1 2 3
@@ -26,47 +26,49 @@ module PacketGen
26
26
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27
27
  #
28
28
  # A MLD header consists of:
29
- # * a {#max_resp_delay} field ({Types::Int16} type),
30
- # * a {#reserved} field ({Types::Int16} type),
29
+ # * a {#max_resp_delay} field (+BinStruct::Int16+ type),
30
+ # * a {#reserved} field (+BinStruct::Int16+ type),
31
31
  # * a {#mcast_addr} field ({Header::IPv6::Addr} type),
32
32
  # * and a {#body} (unused for MLDv1).
33
33
  #
34
- # == Create a MLD header
34
+ # @example Create a MLD header
35
35
  # # standalone
36
36
  # mld = PacketGen::Header::MLD.new
37
37
  # # in a packet
38
38
  # pkt = PacketGen.gen('IPv6').add('ICMPv6').add('MLD')
39
39
  # # access to MLD header
40
- # pkt.mld # => PacketGen::Header::MLD
40
+ # pkt.mld.class # => PacketGen::Header::MLD
41
41
  #
42
- # == MLD attributes
43
- # pkt.icmpv6.type = 130 # ICMPv6 type 130 is MLD Multicast Listener Query
42
+ # @example MLD attributes
43
+ # pkt = PacketGen.gen('IPv6').add('ICMPv6').add('MLD')
44
+ # pkt.icmpv6.type = 130 # ICMPv6 type 130 is MLD Multicast Listener Query
44
45
  # pkt.mld.max_resp_delay = 20
45
- # pkt.mld.group_addr = '::'
46
+ # pkt.mld.mcast_addr = '::'
46
47
  # @author Sylvain Daubert
47
48
  # @since 2.4.0
48
49
  class MLD < Base
49
50
  # @!attribute max_resp_delay
50
51
  # 16-bit MLD Max Response Delay
51
52
  # @return [Integer]
52
- define_field :max_resp_delay, Types::Int16
53
+ define_attr :max_resp_delay, BinStruct::Int16
53
54
  alias max_resp_code max_resp_delay
54
55
  alias max_resp_code= max_resp_delay=
55
56
  # @!attribute reserved
56
57
  # 16-bit Reserved field
57
58
  # @return [Integer]
58
- define_field :reserved, Types::Int16
59
+ define_attr :reserved, BinStruct::Int16
59
60
  # @!attribute mcast_addr
60
61
  # IPv6 Multicast address
61
62
  # @return [IPv6::Addr]
62
- define_field :mcast_addr, IPv6::Addr, default: '::'
63
+ define_attr :mcast_addr, IPv6::Addr, default: '::'
63
64
  # @!attribute body
64
- # @return [String,Base]
65
- define_field :body, Types::String
65
+ # @return [String,Headerable]
66
+ define_attr :body, BinStruct::String
66
67
 
67
68
  # @api private
68
69
  # @note This method is used internally by PacketGen and should not be
69
70
  # directly called
71
+ # This method adds +#mldize+ method to +packet+. This method calls {#mldize}.
70
72
  def added_to_packet(packet)
71
73
  mld_idx = packet.headers.size
72
74
  packet.instance_eval "def mldize() @headers[#{mld_idx}].mldize; end" # def mldize() @headers[3].mldize; end
@@ -52,8 +52,8 @@ module PacketGen
52
52
  # | |
53
53
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54
54
  # @author Sylvain Daubert
55
- class McastAddressRecord < Types::Fields
56
- include Types::Fieldable
55
+ class McastAddressRecord < BinStruct::Struct
56
+ include BinStruct::Structable
57
57
 
58
58
  # Known record types
59
59
  RECORD_TYPES = IGMPv3::GroupRecord::RECORD_TYPES
@@ -61,34 +61,39 @@ module PacketGen
61
61
  # @!attribute type
62
62
  # 8-bit record type
63
63
  # @return [Integer]
64
- define_field :type, Types::Int8Enum, enum: RECORD_TYPES
64
+ define_attr :type, BinStruct::Int8Enum, enum: RECORD_TYPES
65
65
  # @!attribute aux_data_len
66
66
  # 8-bit length of of the Auxiliary Data field ({#aux_data}), in unit of
67
67
  # 32-bit words
68
68
  # @return [Integer]
69
- define_field :aux_data_len, Types::Int8, default: 0
69
+ define_attr :aux_data_len, BinStruct::Int8, default: 0
70
70
  # @!attribute number_of_sources
71
71
  # 16-bit Number of source addresses in {#source_addr}
72
72
  # @return [Integer]
73
- define_field :number_of_sources, Types::Int16, default: 0
73
+ define_attr :number_of_sources, BinStruct::Int16, default: 0
74
74
  # @!attribute multicast_addr
75
75
  # IP multicast address to which this Multicast Address Record pertains
76
76
  # @return [IPv6::Addr]
77
- define_field :multicast_addr, IPv6::Addr, default: '::'
77
+ define_attr :multicast_addr, IPv6::Addr, default: '::'
78
78
  # @!attribute source_addr
79
79
  # Array of source addresses
80
80
  # @return [IPv6::ArrayOfAddr]
81
- define_field :source_addr, IPv6::ArrayOfAddr,
82
- builder: ->(h, t) { t.new(counter: h[:number_of_sources]) }
81
+ define_attr :source_addr, IPv6::ArrayOfAddr,
82
+ builder: ->(h, t) { t.new(counter: h[:number_of_sources]) }
83
83
  # @!attribute aux_data
84
+ # Auxiliary data
84
85
  # @return [String]
85
- define_field :aux_data, Types::String,
86
- builder: ->(h, t) { t.new(length_from: -> { h[:aux_data_len].to_i * 4 }) }
86
+ define_attr :aux_data, BinStruct::String,
87
+ builder: ->(h, t) { t.new(length_from: -> { h[:aux_data_len].to_i * 4 }) }
87
88
 
89
+ # Get human-readable type
90
+ # @return [String]
88
91
  def human_type
89
92
  self[:type].to_human
90
93
  end
91
94
 
95
+ # Get human-readable description
96
+ # @return [String]
92
97
  def to_human
93
98
  "#{human_type}(ma:#{multicast_addr}|src:#{source_addr.to_human})"
94
99
  end
@@ -96,10 +101,10 @@ module PacketGen
96
101
 
97
102
  # Class to handle series of {McastAddressRecord}.
98
103
  # @author Sylvain Daubert
99
- class McastAddressRecords < Types::Array
104
+ class McastAddressRecords < BinStruct::Array
100
105
  set_of McastAddressRecord
101
106
 
102
- # Separator used in {#to_human}.
107
+ # Separator used in +#to_human+.
103
108
  HUMAN_SEPARATOR = ';'
104
109
  end
105
110
  end
@@ -57,54 +57,52 @@ module PacketGen
57
57
  # * *
58
58
  # | |
59
59
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60
- # +type+, +code+ and +checksum+ are fields from {ICMPv6} header.
60
+ # +type+, +code+ and +checksum+ are attributes from {ICMPv6} header.
61
61
  #
62
- # MLQ fields are:
63
- # * {#max_resp_code #max_resp_code} ({Types::Int16}),
64
- # * {#reserved #reserved} ({Types::Int16}),
62
+ # MLQ attributes are:
63
+ # * {#max_resp_code #max_resp_code} (+BinStruct::Int16+),
64
+ # * {#reserved #reserved} (+BinStruct::Int16+),
65
65
  # * {#mcast_addr #mcast_addr} ({IPv6::Addr}),
66
- # * {#flags} ({Types::Int8}), with sub-fields:
66
+ # * {#flags} (+BinStruct::Int8+), with sub-fields:
67
67
  # * a 4-bit {#flag_resv} field,
68
68
  # * a 1-bit {#flag_s} boolean,
69
69
  # * a 3-bit {#flag_qrv} field,
70
- # * {#qqic} ({Types::Int8}),
71
- # * {#number_of_sources} ({Types::Int16}),
70
+ # * {#qqic} (+BinStruct::Int8+),
71
+ # * {#number_of_sources} (+BinStruct::Int16+),
72
72
  # * and {#source_addr}, a {IPv6::ArrayOfAddr}.
73
73
  #
74
74
  # == Max Resp Delay
75
75
  # Max Resp Delay is the real delay value. Max Resp Code is the encoded
76
- # delay. So {#max_resp_delay} and {#max_resp_code} attributes reflect this
76
+ # delay. So {#max_resp_delay} and {#max_resp_code #max_resp_code} attributes reflect this
77
77
  # difference.
78
78
  # @author Sylvain Daubert
79
79
  class MLQ < MLD
80
80
  # @!attribute flags
81
81
  # 8-bit flags
82
82
  # @return [Integer]
83
- define_field_before :body, :flags, Types::Int8
83
+ # @!attribute flag_resv
84
+ # 4-bit reserved field in {#flags}
85
+ # @return [Integer]
86
+ # @!attribute flag_s
87
+ # S Flag (Suppress Router-Side Processing)
88
+ # @return [Integer]
89
+ # @!attribute flag_qrv
90
+ # 3-bit QRV (Querier's Robustness Variable)
91
+ # @return [Integer]
92
+ define_bit_attr_before :body, :flags, flag_resv: 4, flag_s: 1, flag_qrv: 3
84
93
  # @!attribute qqic
85
94
  # 8-bit QQIC
86
95
  # @return [Integer]
87
- define_field_before :body, :qqic, Types::Int8
96
+ define_attr_before :body, :qqic, BinStruct::Int8
88
97
  # @!attribute number_of_sources
89
98
  # 16-bit number of sources
90
99
  # @return [Integer]
91
- define_field_before :body, :number_of_sources, Types::Int16
100
+ define_attr_before :body, :number_of_sources, BinStruct::Int16
92
101
  # @!attribute source_addr
93
102
  # Array of IPv6 source addresses
94
103
  # @return [IPv6::ArrayOfAddr]
95
- define_field_before :body, :source_addr, IPv6::ArrayOfAddr,
96
- builder: ->(h, t) { t.new(counter: h[:number_of_sources]) }
97
-
98
- # @!attribute flag_resv
99
- # 4-bit reserved field in {#flags}
100
- # @return [Integer]
101
- # @!attribute flag_s
102
- # S Flag (Suppress Router-Side Processing)
103
- # @return [Boolean]
104
- # @!attribute flag_qrv
105
- # 3-bit QRV (Querier's Robustness Variable)
106
- # @return [Integer]
107
- define_bit_fields_on :flags, :flag_resv, 4, :flag_s, :flag_qrv, 3
104
+ define_attr_before :body, :source_addr, IPv6::ArrayOfAddr,
105
+ builder: ->(h, t) { t.new(counter: h[:number_of_sources]) }
108
106
 
109
107
  # Getter for +max_resp_code+ for MLDv2 packets. Use {MLDv2.decode}.
110
108
  # @return [Integer]
@@ -38,28 +38,28 @@ module PacketGen
38
38
  # . .
39
39
  # | |
40
40
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41
- # +type+, +code+ and +checksum+ are fields from {ICMPv6} header.
41
+ # +type+, +code+ and +checksum+ are attributes from {ICMPv6} header.
42
42
  #
43
- # MLR fields are:
44
- # * {#reserved} ({Types::Int16}),
45
- # * {#number_of_mar} (number of mcast address records, {Types::Int16}),
43
+ # MLR attributes are:
44
+ # * {#reserved} (+BinStruct::Int16+),
45
+ # * {#number_of_mar} (number of mcast address records, +BinStruct::Int16+),
46
46
  # * {#records} ({McastAddressRecords}).
47
47
  # @author Sylvain Daubert
48
48
  class MLR < Base
49
49
  # @!attribute reserved
50
50
  # 16-bit reserved field
51
51
  # @return [Integer]
52
- define_field :reserved, Types::Int16, default: 0
52
+ define_attr :reserved, BinStruct::Int16, default: 0
53
53
  # @!attribute number_of_mar
54
54
  # 16-bit Number of group records in {#records}
55
55
  # @return [Integer]
56
- define_field :number_of_mar, Types::Int16, default: 0
56
+ define_attr :number_of_mar, BinStruct::Int16, default: 0
57
57
 
58
58
  # @!attribute records
59
59
  # Array of Mcast Address Records
60
60
  # @return [McastAddressRecords]
61
- define_field :records, McastAddressRecords,
62
- builder: ->(h, t) { t.new(counter: h[:number_of_mar]) }
61
+ define_attr :records, McastAddressRecords,
62
+ builder: ->(h, t) { t.new(counter: h[:number_of_mar]) }
63
63
  end
64
64
  end
65
65
 
@@ -8,7 +8,7 @@
8
8
 
9
9
  module PacketGen
10
10
  module Header
11
- # This module contains all MLDv2 specific classes.
11
+ # This module contains all Multicast Listener Discovery Version 2 (MLDv2) specific classes.
12
12
  # @author Sylvain Daubert
13
13
  # @since 2.4.0
14
14
  module MLDv2