packetgen 3.3.2 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +37 -21
  3. data/lib/packetgen/capture.rb +2 -2
  4. data/lib/packetgen/config.rb +0 -1
  5. data/lib/packetgen/deprecation.rb +7 -7
  6. data/lib/packetgen/header/arp.rb +13 -13
  7. data/lib/packetgen/header/asn1_base.rb +1 -1
  8. data/lib/packetgen/header/base.rb +17 -18
  9. data/lib/packetgen/header/bootp.rb +32 -34
  10. data/lib/packetgen/header/dhcp/option.rb +19 -19
  11. data/lib/packetgen/header/dhcp/options.rb +1 -1
  12. data/lib/packetgen/header/dhcp.rb +3 -3
  13. data/lib/packetgen/header/dhcpv6/duid.rb +16 -16
  14. data/lib/packetgen/header/dhcpv6/option.rb +53 -53
  15. data/lib/packetgen/header/dhcpv6/options.rb +1 -1
  16. data/lib/packetgen/header/dhcpv6/relay.rb +5 -5
  17. data/lib/packetgen/header/dhcpv6.rb +6 -6
  18. data/lib/packetgen/header/dns/name.rb +14 -10
  19. data/lib/packetgen/header/dns/opt.rb +2 -2
  20. data/lib/packetgen/header/dns/option.rb +11 -11
  21. data/lib/packetgen/header/dns/qdsection.rb +1 -1
  22. data/lib/packetgen/header/dns/question.rb +6 -8
  23. data/lib/packetgen/header/dns/rr.rb +56 -43
  24. data/lib/packetgen/header/dns/rrsection.rb +4 -4
  25. data/lib/packetgen/header/dns.rb +27 -30
  26. data/lib/packetgen/header/dot11/control.rb +11 -11
  27. data/lib/packetgen/header/dot11/data.rb +20 -20
  28. data/lib/packetgen/header/dot11/element.rb +4 -4
  29. data/lib/packetgen/header/dot11/management.rb +8 -8
  30. data/lib/packetgen/header/dot11/sub_mngt.rb +39 -53
  31. data/lib/packetgen/header/dot11.rb +88 -93
  32. data/lib/packetgen/header/dot1q.rb +10 -12
  33. data/lib/packetgen/header/dot1x.rb +9 -9
  34. data/lib/packetgen/header/eap/fast.rb +4 -4
  35. data/lib/packetgen/header/eap/md5.rb +6 -6
  36. data/lib/packetgen/header/eap/tls.rb +13 -15
  37. data/lib/packetgen/header/eap/ttls.rb +13 -15
  38. data/lib/packetgen/header/eap.rb +22 -22
  39. data/lib/packetgen/header/eth.rb +18 -18
  40. data/lib/packetgen/header/gre.rb +8 -10
  41. data/lib/packetgen/header/http/headers.rb +2 -2
  42. data/lib/packetgen/header/http/request.rb +17 -16
  43. data/lib/packetgen/header/http/response.rb +18 -17
  44. data/lib/packetgen/header/http/verbs.rb +1 -3
  45. data/lib/packetgen/header/icmp.rb +8 -8
  46. data/lib/packetgen/header/icmpv6.rb +3 -3
  47. data/lib/packetgen/header/igmp.rb +8 -8
  48. data/lib/packetgen/header/igmpv3/group_record.rb +12 -12
  49. data/lib/packetgen/header/igmpv3/mq.rb +16 -18
  50. data/lib/packetgen/header/igmpv3/mr.rb +4 -4
  51. data/lib/packetgen/header/igmpv3.rb +7 -7
  52. data/lib/packetgen/header/ip/addr.rb +13 -13
  53. data/lib/packetgen/header/ip/option.rb +31 -33
  54. data/lib/packetgen/header/ip/options.rb +1 -1
  55. data/lib/packetgen/header/ip.rb +37 -72
  56. data/lib/packetgen/header/ipv6/addr.rb +14 -14
  57. data/lib/packetgen/header/ipv6/extension.rb +8 -8
  58. data/lib/packetgen/header/ipv6/hop_by_hop.rb +9 -9
  59. data/lib/packetgen/header/ipv6.rb +20 -22
  60. data/lib/packetgen/header/llc.rb +17 -17
  61. data/lib/packetgen/header/mdns.rb +1 -1
  62. data/lib/packetgen/header/mld.rb +6 -6
  63. data/lib/packetgen/header/mldv2/mcast_address_record.rb +11 -11
  64. data/lib/packetgen/header/mldv2/mlq.rb +21 -23
  65. data/lib/packetgen/header/mldv2/mlr.rb +8 -8
  66. data/lib/packetgen/header/ospfv2/db_description.rb +11 -12
  67. data/lib/packetgen/header/ospfv2/hello.rb +11 -11
  68. data/lib/packetgen/header/ospfv2/ls_ack.rb +1 -1
  69. data/lib/packetgen/header/ospfv2/ls_request.rb +9 -9
  70. data/lib/packetgen/header/ospfv2/ls_update.rb +3 -3
  71. data/lib/packetgen/header/ospfv2/lsa.rb +54 -58
  72. data/lib/packetgen/header/ospfv2/lsa_header.rb +9 -9
  73. data/lib/packetgen/header/ospfv2.rb +27 -29
  74. data/lib/packetgen/header/ospfv3/db_description.rb +13 -14
  75. data/lib/packetgen/header/ospfv3/hello.rb +12 -12
  76. data/lib/packetgen/header/ospfv3/ipv6_prefix.rb +17 -19
  77. data/lib/packetgen/header/ospfv3/ls_ack.rb +2 -2
  78. data/lib/packetgen/header/ospfv3/ls_request.rb +9 -9
  79. data/lib/packetgen/header/ospfv3/ls_update.rb +4 -4
  80. data/lib/packetgen/header/ospfv3/lsa.rb +48 -51
  81. data/lib/packetgen/header/ospfv3/lsa_header.rb +9 -9
  82. data/lib/packetgen/header/ospfv3.rb +25 -27
  83. data/lib/packetgen/header/sctp/chunk.rb +44 -41
  84. data/lib/packetgen/header/sctp/error.rb +52 -52
  85. data/lib/packetgen/header/sctp/parameter.rb +38 -38
  86. data/lib/packetgen/header/sctp.rb +5 -5
  87. data/lib/packetgen/header/snmp.rb +2 -2
  88. data/lib/packetgen/header/tcp/option.rb +45 -39
  89. data/lib/packetgen/header/tcp/options.rb +2 -2
  90. data/lib/packetgen/header/tcp.rb +55 -44
  91. data/lib/packetgen/header/tftp.rb +16 -16
  92. data/lib/packetgen/header/udp.rb +8 -8
  93. data/lib/packetgen/header.rb +9 -10
  94. data/lib/packetgen/headerable.rb +13 -3
  95. data/lib/packetgen/inspect.rb +2 -2
  96. data/lib/packetgen/packet.rb +54 -37
  97. data/lib/packetgen/pcap.rb +15 -4
  98. data/lib/packetgen/pcapng/block.rb +18 -17
  99. data/lib/packetgen/pcapng/epb.rb +13 -15
  100. data/lib/packetgen/pcapng/file.rb +3 -97
  101. data/lib/packetgen/pcapng/idb.rb +9 -11
  102. data/lib/packetgen/pcapng/shb.rb +13 -15
  103. data/lib/packetgen/pcapng/spb.rb +8 -10
  104. data/lib/packetgen/pcapng/unknown_block.rb +6 -17
  105. data/lib/packetgen/pcapng.rb +4 -4
  106. data/lib/packetgen/pcaprub_wrapper.rb +17 -1
  107. data/lib/packetgen/proto.rb +1 -1
  108. data/lib/packetgen/unknown_packet.rb +2 -2
  109. data/lib/packetgen/utils/arp_spoofer.rb +18 -19
  110. data/lib/packetgen/utils.rb +2 -2
  111. data/lib/packetgen/version.rb +1 -1
  112. data/lib/packetgen.rb +4 -3
  113. metadata +34 -29
  114. data/lib/packetgen/types/abstract_tlv.rb +0 -278
  115. data/lib/packetgen/types/array.rb +0 -287
  116. data/lib/packetgen/types/cstring.rb +0 -109
  117. data/lib/packetgen/types/enum.rb +0 -171
  118. data/lib/packetgen/types/fieldable.rb +0 -66
  119. data/lib/packetgen/types/fields.rb +0 -622
  120. data/lib/packetgen/types/int.rb +0 -473
  121. data/lib/packetgen/types/int_string.rb +0 -102
  122. data/lib/packetgen/types/length_from.rb +0 -54
  123. data/lib/packetgen/types/oui.rb +0 -52
  124. data/lib/packetgen/types/string.rb +0 -97
  125. data/lib/packetgen/types/tlv.rb +0 -161
  126. data/lib/packetgen/types.rb +0 -26
@@ -1,473 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- # This file is part of PacketGen
5
- # See https://github.com/lemontree55/packetgen for more informations
6
- # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
7
- # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
8
- # This program is published under MIT license.
9
-
10
- module PacketGen
11
- module Types
12
- # Base integer class to handle binary integers
13
- # @abstract
14
- # @author Sylvain Daubert
15
- # @since 3.3.1 support native endianess
16
- class Int
17
- include Fieldable
18
-
19
- # Integer value
20
- # @return [Integer]
21
- attr_accessor :value
22
- # Integer endianness
23
- # @return [:little,:big,:native]
24
- # @since 3.3.1 add :native
25
- attr_accessor :endian
26
- # Integer size, in bytes
27
- # @return [Integer]
28
- attr_accessor :width
29
- # Integer default value
30
- # @return [Integer]
31
- attr_accessor :default
32
-
33
- # @param [Integer,nil] value
34
- # @param [:little,:big,nil] endian
35
- # @param [Integer,nil] width
36
- # @param [Integer] default
37
- def initialize(value=nil, endian=nil, width=nil, default=0)
38
- @value = value
39
- @endian = endian
40
- @width = width
41
- @default = default
42
- end
43
-
44
- # @abstract
45
- # Read an Int from a binary string or an integer
46
- # @param [Integer, #to_s] value
47
- # @return [self]
48
- # @raise [ParseError] when reading +#to_s+ objects with abstract Int class.
49
- def read(value)
50
- @value = if value.is_a?(Integer)
51
- value.to_i
52
- elsif defined? @packstr
53
- value.to_s.unpack1(@packstr[@endian])
54
- else
55
- raise ParseError, 'Int#read is abstract and cannot read'
56
- end
57
- self
58
- end
59
-
60
- # @abstract
61
- # @return [::String]
62
- # @raise [ParseError] This is an abstrat method and must be redefined
63
- def to_s
64
- raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr
65
-
66
- [to_i].pack(@packstr[@endian])
67
- end
68
-
69
- # Convert Int to Integer
70
- # @return [Integer]
71
- def to_i
72
- @value || @default
73
- end
74
- alias to_human to_i
75
- alias from_human value=
76
-
77
- # Convert Int to Float
78
- # @return [Float]
79
- def to_f
80
- to_i.to_f
81
- end
82
-
83
- # Give size in bytes of self
84
- # @return [Integer]
85
- def sz
86
- width
87
- end
88
-
89
- # Format Int type when inspecting header or packet
90
- # @return [String]
91
- def format_inspect
92
- format_str % [to_i.to_s, to_i]
93
- end
94
-
95
- # Return the number of bits used to encode this Int
96
- # @return [Integer]
97
- # @since 3.2.1
98
- def nbits
99
- width * 8
100
- end
101
-
102
- private
103
-
104
- def format_str
105
- "%-16s (0x%0#{width * 2}x)"
106
- end
107
- end
108
-
109
- # One byte unsigned integer
110
- # @author Sylvain Daubert
111
- class Int8 < Int
112
- # @param [Integer,nil] value
113
- def initialize(value=nil)
114
- super(value, nil, 1)
115
- @packstr = { nil => 'C' }
116
- end
117
- end
118
-
119
- # One byte signed integer
120
- # @author Sylvain Daubert
121
- # @since 2.8.2
122
- class SInt8 < Int
123
- # @param [Integer,nil] value
124
- def initialize(value=nil)
125
- super(value, nil, 1)
126
- @packstr = { nil => 'c' }
127
- end
128
- end
129
-
130
- # 2-byte unsigned integer
131
- # @author Sylvain Daubert
132
- class Int16 < Int
133
- # @param [Integer,nil] value
134
- # @param [:big, :little, :native] endian
135
- def initialize(value=nil, endian=:big)
136
- super(value, endian, 2)
137
- @packstr = { big: 'n', little: 'v', native: 'S' }
138
- end
139
- end
140
-
141
- # big endian 2-byte unsigned integer
142
- # @author Sylvain Daubert
143
- class Int16be < Int16
144
- undef endian=
145
- end
146
-
147
- # little endian 2-byte unsigned integer
148
- # @author Sylvain Daubert
149
- class Int16le < Int16
150
- # @param [Integer,nil] value
151
- undef endian=
152
-
153
- # @param [Integer, nil] value
154
- def initialize(value=nil)
155
- super(value, :little)
156
- end
157
- end
158
-
159
- # native endian 2-byte unsigned integer
160
- # @author Sylvain Daubert
161
- # @since 3.3.1
162
- class Int16n < Int16
163
- # @param [Integer,nil] value
164
- undef endian=
165
-
166
- # @param [Integer, nil] value
167
- def initialize(value=nil)
168
- super(value, :native)
169
- end
170
- end
171
-
172
- # 2-byte signed integer
173
- # @author Sylvain Daubert
174
- # @since 2.8.2
175
- class SInt16 < Int16
176
- # @param [Integer,nil] value
177
- # @param [:big, :little] endian
178
- def initialize(value=nil, endian=:big)
179
- super
180
- @packstr = { big: 's>', little: 's<', native: 's' }
181
- end
182
- end
183
-
184
- # big endian 2-byte signed integer
185
- # @author Sylvain Daubert
186
- # @since 2.8.2
187
- class SInt16be < SInt16
188
- undef endian=
189
- end
190
-
191
- # little endian 2-byte signed integer
192
- # @author Sylvain Daubert
193
- # @since 2.8.2
194
- class SInt16le < SInt16
195
- # @param [Integer,nil] value
196
- undef endian=
197
-
198
- # @param [Integer, nil] value
199
- def initialize(value=nil)
200
- super(value, :little)
201
- end
202
- end
203
-
204
- # native endian 2-byte signed integer
205
- # @author Sylvain Daubert
206
- # @since 3.3.1
207
- class SInt16n < SInt16
208
- # @param [Integer,nil] value
209
- undef endian=
210
-
211
- # @param [Integer, nil] value
212
- def initialize(value=nil)
213
- super(value, :native)
214
- end
215
- end
216
-
217
- # 3-byte unsigned integer
218
- # @author Sylvain Daubert
219
- # @since 2.1.4
220
- class Int24 < Int
221
- # @param [Integer,nil] value
222
- # @param [:big, :little, :native] endian
223
- def initialize(value=nil, endian=:big)
224
- if endian == :native
225
- endian = if [1].pack('S').unpack1('n') == 1
226
- :big
227
- else
228
- :little
229
- end
230
- end
231
- super(value, endian, 3)
232
- end
233
-
234
- # Read an 3-byte Int from a binary string or an integer
235
- # @param [Integer, String] value
236
- # @return [self]
237
- def read(value)
238
- return self if value.nil?
239
-
240
- @value = if value.is_a?(Integer)
241
- value.to_i
242
- else
243
- up8 = down16 = 0
244
- if @endian == :big
245
- up8, down16 = value.to_s.unpack('Cn')
246
- else
247
- down16, up8 = value.to_s.unpack('vC')
248
- end
249
- (up8 << 16) | down16
250
- end
251
- self
252
- end
253
-
254
- # @return [::String]
255
- def to_s
256
- up8 = to_i >> 16
257
- down16 = to_i & 0xffff
258
- if @endian == :big
259
- [up8, down16].pack('Cn')
260
- else
261
- [down16, up8].pack('vC')
262
- end
263
- end
264
- end
265
-
266
- # big endian 3-byte unsigned integer
267
- # @author Sylvain Daubert
268
- # @since 2.1.4
269
- class Int24be < Int24
270
- undef endian=
271
- end
272
-
273
- # little endian 3-byte unsigned integer
274
- # @author Sylvain Daubert
275
- # @since 2.1.4
276
- class Int24le < Int24
277
- # @param [Integer,nil] value
278
- undef endian=
279
-
280
- # @param [Integer, nil] value
281
- def initialize(value=nil)
282
- super(value, :little)
283
- end
284
- end
285
-
286
- # native endian 3-byte unsigned integer
287
- # @author Sylvain Daubert
288
- # @since 3.3.1
289
- class Int24n < Int24
290
- # @param [Integer,nil] value
291
- undef endian=
292
-
293
- # @param [Integer, nil] value
294
- def initialize(value=nil)
295
- super(value, :native)
296
- end
297
- end
298
-
299
- # 4-byte unsigned integer
300
- # @author Sylvain Daubert
301
- class Int32 < Int
302
- # @param [Integer,nil] value
303
- # @param [:big, :little, :native] endian
304
- def initialize(value=nil, endian=:big)
305
- super(value, endian, 4)
306
- @packstr = { big: 'N', little: 'V', native: 'L' }
307
- end
308
- end
309
-
310
- # big endian 4-byte unsigned integer
311
- # @author Sylvain Daubert
312
- class Int32be < Int32
313
- undef endian=
314
- end
315
-
316
- # little endian 4-byte unsigned integer
317
- # @author Sylvain Daubert
318
- class Int32le < Int32
319
- # @param [Integer,nil] value
320
- undef endian=
321
-
322
- # @param [Integer, nil] value
323
- def initialize(value=nil)
324
- super(value, :little)
325
- end
326
- end
327
-
328
- # native endian 4-byte unsigned integer
329
- # @author Sylvain Daubert
330
- # @since 3.3.1
331
- class Int32n < Int32
332
- # @param [Integer,nil] value
333
- undef endian=
334
-
335
- # @param [Integer, nil] value
336
- def initialize(value=nil)
337
- super(value, :native)
338
- end
339
- end
340
-
341
- # 4-byte unsigned integer
342
- # @author Sylvain Daubert
343
- # @since 2.8.2
344
- class SInt32 < Int32
345
- # @param [Integer,nil] value
346
- # @param [:big, :little] endian
347
- def initialize(value=nil, endian=:big)
348
- super
349
- @packstr = { big: 'l>', little: 'l<', native: 'l' }
350
- end
351
- end
352
-
353
- # big endian 4-byte unsigned integer
354
- # @author Sylvain Daubert
355
- # @since 2.8.2
356
- class SInt32be < SInt32
357
- undef endian=
358
- end
359
-
360
- # little endian 4-byte unsigned integer
361
- # @author Sylvain Daubert
362
- # @since 2.8.2
363
- class SInt32le < SInt32
364
- # @param [Integer,nil] value
365
- undef endian=
366
-
367
- # @param [Integer, nil] value
368
- def initialize(value=nil)
369
- super(value, :little)
370
- end
371
- end
372
-
373
- # native endian 4-byte unsigned integer
374
- # @author Sylvain Daubert
375
- # @since 3.3.1
376
- class SInt32n < SInt32
377
- # @param [Integer,nil] value
378
- undef endian=
379
-
380
- # @param [Integer, nil] value
381
- def initialize(value=nil)
382
- super(value, :native)
383
- end
384
- end
385
-
386
- # 8-byte unsigned integer
387
- # @author Sylvain Daubert
388
- class Int64 < Int
389
- # @param [Integer,nil] value
390
- # @param [:big, :little, :native] endian
391
- def initialize(value=nil, endian=:big)
392
- super(value, endian, 8)
393
- @packstr = { big: 'Q>', little: 'Q<', native: 'Q' }
394
- end
395
- end
396
-
397
- # big endian 8-byte unsigned integer
398
- # @author Sylvain Daubert
399
- class Int64be < Int64
400
- undef endian=
401
- end
402
-
403
- # little endian 8-byte unsigned integer
404
- # @author Sylvain Daubert
405
- class Int64le < Int64
406
- # @param [Integer,nil] value
407
- undef endian=
408
-
409
- # @param [Integer, nil] value
410
- def initialize(value=nil)
411
- super(value, :little)
412
- end
413
- end
414
-
415
- # native endian 8-byte unsigned integer
416
- # @author Sylvain Daubert
417
- # @since 3.3.1
418
- class Int64n < Int64
419
- # @param [Integer,nil] value
420
- undef endian=
421
-
422
- # @param [Integer, nil] value
423
- def initialize(value=nil)
424
- super(value, :native)
425
- end
426
- end
427
-
428
- # 8-byte unsigned integer
429
- # @author Sylvain Daubert
430
- # @since 2.8.2
431
- class SInt64 < Int64
432
- # @param [Integer,nil] value
433
- # @param [:big, :little, :native] endian
434
- def initialize(value=nil, endian=:big)
435
- super
436
- @packstr = { big: 'q>', little: 'q<', native: 'q' }
437
- end
438
- end
439
-
440
- # big endian 8-byte unsigned integer
441
- # @author Sylvain Daubert
442
- # @since 2.8.2
443
- class SInt64be < SInt64
444
- undef endian=
445
- end
446
-
447
- # little endian 8-byte unsigned integer
448
- # @author Sylvain Daubert
449
- # @since 2.8.2
450
- class SInt64le < SInt64
451
- # @param [Integer,nil] value
452
- undef endian=
453
-
454
- # @param [Integer, nil] value
455
- def initialize(value=nil)
456
- super(value, :little)
457
- end
458
- end
459
-
460
- # native endian 8-byte unsigned integer
461
- # @author Sylvain Daubert
462
- # @since 3.3.1
463
- class SInt64n < SInt64
464
- # @param [Integer,nil] value
465
- undef endian=
466
-
467
- # @param [Integer, nil] value
468
- def initialize(value=nil)
469
- super(value, :native)
470
- end
471
- end
472
- end
473
- end
@@ -1,102 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- # This file is part of PacketGen
5
- # See https://github.com/lemontree55/packetgen for more informations
6
- # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
7
- # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
8
- # This program is published under MIT license.
9
-
10
- module PacketGen
11
- module Types
12
- # Provides a class for creating strings preceeded by their length as a {Int}.
13
- # By default, a null string will have one byte length (length byte set to 0).
14
- # @author Sylvain Daubert
15
- class IntString
16
- include Fieldable
17
-
18
- # internal string
19
- # @return [String]
20
- attr_reader :string
21
-
22
- # @param [Class] len_type should be a {Int} subclass
23
- # @param [::String] string
24
- def initialize(len_type=Int8, string: '')
25
- @string = Types::String.new.read(string)
26
- @length = len_type.new
27
- calc_length
28
- end
29
-
30
- # @param [::String] str
31
- # @return [IntString] self
32
- def read(str)
33
- unless str[0, @length.width].size == @length.width
34
- raise ParseError,
35
- "String too short for type #{@length.class.to_s.gsub(/.*::/, '')}"
36
- end
37
- @length.read str[0, @length.width]
38
- @string.read str[@length.width, @length.to_i]
39
- self
40
- end
41
-
42
- # @param [Integer] len
43
- # @return [Integer]
44
- def length=(len)
45
- @length.read len
46
- len
47
- end
48
-
49
- # @return [Integer]
50
- def length
51
- @length.to_i
52
- end
53
-
54
- # @param [#to_s] str
55
- # @return [String]
56
- def string=(str)
57
- @length.value = str.to_s.size
58
- @string = str.to_s
59
- end
60
-
61
- # Get binary string
62
- # @return [::String]
63
- def to_s
64
- @length.to_s << @string.to_s
65
- end
66
-
67
- # Set from a human readable string
68
- # @param [String] str
69
- # @return [self]
70
- def from_human(str)
71
- @string.read str
72
- calc_length
73
- self
74
- end
75
-
76
- # Get human readable string
77
- # @return [::String]
78
- # @since 2.2.0
79
- def to_human
80
- @string
81
- end
82
-
83
- # Set length from internal string length
84
- # @return [Integer]
85
- def calc_length
86
- @length.read @string.length
87
- end
88
-
89
- # Give binary string length (including +length+ field)
90
- # @return [Integer]
91
- def sz
92
- to_s.size
93
- end
94
-
95
- # Say if IntString is empty
96
- # @return [Boolean]
97
- def empty?
98
- length.zero?
99
- end
100
- end
101
- end
102
- end
@@ -1,54 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- # This file is part of PacketGen
5
- # See https://github.com/lemontree55/packetgen for more informations
6
- # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
7
- # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
8
- # This program is published under MIT license.
9
-
10
- module PacketGen
11
- module Types
12
- # This module is a mixin adding +length_from+ capacity to a type.
13
- # +length_from+ capacity is the capacity, for a type, to gets its
14
- # length from another object.
15
- # @author Sylvain Daubert
16
- # @since 3.0.0
17
- module LengthFrom
18
- # Max value returned by {#sz_to_read}.
19
- MAX_SZ_TO_READ = 65_535
20
-
21
- # Initialize +length from+ capacity.
22
- # Should be call by extensed object's initialize.
23
- # @param [Hash] options
24
- # @option options [Types::Int,Proc] :length_from object or proc from which
25
- # takes length when reading
26
- # @return [void]
27
- def initialize_length_from(options)
28
- @length_from = options[:length_from]
29
- end
30
-
31
- # Return a substring from +str+ of length given in another object.
32
- # @param [#to_s] str
33
- # @return [String]
34
- def read_with_length_from(str)
35
- s = PacketGen.force_binary(str.to_s)
36
- s[0, sz_to_read]
37
- end
38
-
39
- # Size to read, from length_from
40
- # @return [Integer]
41
- def sz_to_read
42
- len = case @length_from
43
- when Types::Int
44
- @length_from.to_i
45
- when Proc
46
- @length_from.call
47
- else
48
- MAX_SZ_TO_READ
49
- end
50
- [0, len].max
51
- end
52
- end
53
- end
54
- end
@@ -1,52 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- # This file is part of PacketGen
5
- # See https://github.com/lemontree55/packetgen for more informations
6
- # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
7
- # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
8
- # This program is published under MIT license.
9
-
10
- module PacketGen
11
- module Types
12
- # OUI type, defined as a set of 3 bytes
13
- # oui = OUI.new
14
- # oui.from_human('00:01:02')
15
- # oui.to_human # => "00:01:02"
16
- # @author Sylvain Daubert
17
- class OUI < Types::Fields
18
- include Fieldable
19
-
20
- # @attribute b2
21
- # @return [Integer] left-most byte
22
- define_field :b2, Types::Int8
23
- # @attribute b1
24
- # @return [Integer] center byte
25
- define_field :b1, Types::Int8
26
- # @attribute b0
27
- # @return [Integer] right-most byte
28
- define_field :b0, Types::Int8
29
-
30
- # Read a human-readable string to populate object
31
- # @param [String] str
32
- # @return [OUI] self
33
- def from_human(str)
34
- return self if str.nil?
35
-
36
- bytes = str.split(':')
37
- raise ArgumentError, 'not a OUI' unless bytes.size == 3
38
-
39
- self[:b2].read(bytes[0].to_i(16))
40
- self[:b1].read(bytes[1].to_i(16))
41
- self[:b0].read(bytes[2].to_i(16))
42
- self
43
- end
44
-
45
- # Get OUI in human readable form (colon-separated bytes)
46
- # @return [String]
47
- def to_human
48
- fields.map { |m| '%02x' % self[m] }.join(':')
49
- end
50
- end
51
- end
52
- end