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
@@ -36,18 +36,18 @@ module PacketGen
36
36
  # @!attribute link_type
37
37
  # 16-bit link type
38
38
  # @return [Integer]
39
- define_field_before :block_len2, :link_type, Types::Int16, default: 1
39
+ define_attr_before :block_len2, :link_type, BinStruct::Int16, default: 1
40
40
  # @!attribute reserved
41
41
  # 16-bit reserved field
42
42
  # @return [Integer]
43
- define_field_before :block_len2, :reserved, Types::Int16, default: 0
43
+ define_attr_before :block_len2, :reserved, BinStruct::Int16, default: 0
44
44
  # @!attribute snaplen
45
45
  # 32-bit snap length
46
46
  # @return [Integer]
47
- define_field_before :block_len2, :snaplen, Types::Int32, default: 0
47
+ define_attr_before :block_len2, :snaplen, BinStruct::Int32, default: 0
48
48
  # @!attribute options
49
- # @return [Types::String]
50
- define_field_before :block_len2, :options, Types::String
49
+ # @return [BinStruct::String]
50
+ define_attr_before :block_len2, :options, BinStruct::String
51
51
 
52
52
  # @param [Hash] options
53
53
  # @option options [:little, :big] :endian set block endianness
@@ -61,10 +61,8 @@ module PacketGen
61
61
  # @option options [Integer] :block_len2 block total length
62
62
  def initialize(options={})
63
63
  super
64
- endianness(options[:endian] || :little)
65
64
  @packets = []
66
65
  @options_decoded = false
67
- recalc_block_len
68
66
  self.type = options[:type] || PcapNG::IDB_TYPE.to_i
69
67
  end
70
68
 
@@ -76,9 +74,9 @@ module PacketGen
76
74
  return self if io.eof?
77
75
 
78
76
  %i[type block_len link_type reserved snaplen].each do |attr|
79
- self[attr].read io.read(self[attr].sz)
77
+ self[attr].read(io.read(self[attr].sz))
80
78
  end
81
- self[:options].read io.read(self.block_len - MIN_SIZE)
79
+ self[:options].read(io.read(self.block_len - MIN_SIZE))
82
80
  read_blocklen2_and_check(io)
83
81
 
84
82
  self
@@ -107,7 +105,7 @@ module PacketGen
107
105
  # Return the object as a String
108
106
  # @return [String]
109
107
  def to_s
110
- pad_field :options
108
+ pad_field(:options)
111
109
  recalc_block_len
112
110
  super << @packets.map(&:to_s).join
113
111
  end
@@ -119,7 +117,7 @@ module PacketGen
119
117
  @options_decoded = true
120
118
  return @ts_resol = 1E-6 if tsresol.nil?
121
119
 
122
- @ts_resol = if (tsresol & 0x80).zero?
120
+ @ts_resol = if tsresol.nobits?(0x80)
123
121
  10**-tsresol
124
122
  else
125
123
  2**-(tsresol & 0x7f)
@@ -45,23 +45,23 @@ module PacketGen
45
45
  # @!attribute magic
46
46
  # 32-bit magic number
47
47
  # @return [Integer]
48
- define_field_before :block_len2, :magic, Types::Int32, default: MAGIC_INT32
48
+ define_attr_before :block_len2, :magic, BinStruct::Int32, default: MAGIC_INT32
49
49
  # @!attribute ver_major
50
50
  # 16-bit major version number
51
51
  # @return [Integer]
52
- define_field_before :block_len2, :ver_major, Types::Int16, default: 1
52
+ define_attr_before :block_len2, :ver_major, BinStruct::Int16, default: 1
53
53
  # @!attribute ver_major
54
54
  # 16-bit minor version number
55
55
  # @return [Integer]
56
- define_field_before :block_len2, :ver_minor, Types::Int16, default: 0
56
+ define_attr_before :block_len2, :ver_minor, BinStruct::Int16, default: 0
57
57
  # @!attribute section_len
58
58
  # 64-bit section length
59
59
  # @return [Integer]
60
- define_field_before :block_len2, :section_len, Types::Int64,
61
- default: SECTION_LEN_UNDEFINED
60
+ define_attr_before :block_len2, :section_len, BinStruct::Int64,
61
+ default: SECTION_LEN_UNDEFINED
62
62
  # @!attribute options
63
- # @return [Types::String]
64
- define_field_before :block_len2, :options, Types::String
63
+ # @return [BinStruct::String]
64
+ define_attr_before :block_len2, :options, BinStruct::String
65
65
 
66
66
  # @param [Hash] options
67
67
  # @option options [:little, :big] :endian set block endianness
@@ -81,8 +81,6 @@ module PacketGen
81
81
  super
82
82
  @interfaces = []
83
83
  @unknown_blocks = []
84
- endianness(options[:endian] || :little)
85
- recalc_block_len
86
84
  self.type = options[:type] || PcapNG::SHB_TYPE.to_i
87
85
  end
88
86
 
@@ -93,9 +91,9 @@ module PacketGen
93
91
  io = to_io(str_or_io)
94
92
  return self if io.eof?
95
93
 
96
- self[:type].read check_shb(io)
94
+ self[:type].read(check_shb(io))
97
95
  %i[block_len magic ver_major ver_minor section_len].each do |attr|
98
- self[attr].read io.read(self[attr].sz)
96
+ self[attr].read(io.read(self[attr].sz))
99
97
  end
100
98
  handle_magic_and_check(self[:magic].to_s)
101
99
 
@@ -120,7 +118,7 @@ module PacketGen
120
118
  body = @interfaces.map(&:to_s).join
121
119
  self.section_len = body.size unless self.section_len == SECTION_LEN_UNDEFINED
122
120
 
123
- pad_field :options
121
+ pad_field(:options)
124
122
  recalc_block_len
125
123
  super + body
126
124
  end
@@ -135,12 +133,12 @@ module PacketGen
135
133
  def force_endianness(endian)
136
134
  @endian = endian
137
135
  %i[type block_len magic block_len2].each do |attr|
138
- self[attr] = Types::Int32.new(0, endian).read(self[attr].to_s)
136
+ self[attr] = BinStruct::Int32.new(value: 0, endian: endian).read(self[attr].to_s)
139
137
  end
140
138
  %i[ver_major ver_minor].each do |attr|
141
- self[attr] = Types::Int16.new(0, endian).read(self[attr].to_s)
139
+ self[attr] = BinStruct::Int16.new(value: 0, endian: endian).read(self[attr].to_s)
142
140
  end
143
- self[:section_len] = Types::Int64.new(0, endian).read(self[:section_len].to_s)
141
+ self[:section_len] = BinStruct::Int64.new(value: 0, endian: endian).read(self[:section_len].to_s)
144
142
  end
145
143
 
146
144
  # Check io contains a SHB
@@ -29,10 +29,10 @@ module PacketGen
29
29
  # @!attribute orig_len
30
30
  # 32-bit original length
31
31
  # @return [Integer]
32
- define_field_before :block_len2, :orig_len, Types::Int32, default: 0
32
+ define_attr_before :block_len2, :orig_len, BinStruct::Int32, default: 0
33
33
  # @!attribute data
34
- # @return [Types::String]
35
- define_field_before :block_len2, :data, Types::String
34
+ # @return [BinStruct::String]
35
+ define_attr_before :block_len2, :data, BinStruct::String
36
36
 
37
37
  # @param [Hash] options
38
38
  # @option options [:little, :big] :endian set block endianness
@@ -45,8 +45,6 @@ module PacketGen
45
45
  # @option options [Integer] :block_len2 block total length
46
46
  def initialize(options={})
47
47
  super
48
- endianness(options[:endian] || :little)
49
- recalc_block_len
50
48
  self.type = options[:type] || PcapNG::SPB_TYPE.to_i
51
49
  end
52
50
 
@@ -64,11 +62,11 @@ module PacketGen
64
62
  io = to_io(str_or_io)
65
63
  return self if io.eof?
66
64
 
67
- self[:type].read io.read(4)
68
- self[:block_len].read io.read(4)
69
- self[:orig_len].read io.read(4)
65
+ self[:type].read(io.read(4))
66
+ self[:block_len].read(io.read(4))
67
+ self[:orig_len].read(io.read(4))
70
68
  data_len = compute_data_len
71
- self[:data].read io.read(data_len)
69
+ self[:data].read(io.read(data_len))
72
70
  remove_padding(io, data_len)
73
71
  read_blocklen2_and_check(io)
74
72
 
@@ -79,7 +77,7 @@ module PacketGen
79
77
  # Return the object as a String
80
78
  # @return [String]
81
79
  def to_s
82
- pad_field :data
80
+ pad_field(:data)
83
81
  recalc_block_len
84
82
  super
85
83
  end
@@ -20,19 +20,8 @@ module PacketGen
20
20
  attr_accessor :section
21
21
 
22
22
  # @!attribute body
23
- # @return [Types::String]
24
- define_field_before :block_len2, :body, Types::String
25
-
26
- # @option options [:little, :big] :endian set block endianness
27
- # @option options [Integer] :type
28
- # @option options [Integer] :block_len block total length
29
- # @option options [::String] :body
30
- # @option options [Integer] :block_len2 block total length
31
- def initialize(options={})
32
- super
33
- endianness(options[:endian] || :little)
34
- recalc_block_len
35
- end
23
+ # @return [BinStruct::String]
24
+ define_attr_before :block_len2, :body, BinStruct::String
36
25
 
37
26
  # Has this block options?
38
27
  # @return [false]
@@ -48,9 +37,9 @@ module PacketGen
48
37
  io = to_io(str_or_io)
49
38
  return self if io.eof?
50
39
 
51
- self[:type].read io.read(4)
52
- self[:block_len].read io.read(4)
53
- self[:body].read io.read(self[:block_len].to_i - MIN_SIZE)
40
+ self[:type].read(io.read(4))
41
+ self[:block_len].read(io.read(4))
42
+ self[:body].read(io.read(self[:block_len].to_i - MIN_SIZE))
54
43
  read_blocklen2_and_check(io)
55
44
 
56
45
  self
@@ -59,7 +48,7 @@ module PacketGen
59
48
  # Return the object as a String
60
49
  # @return [String]
61
50
  def to_s
62
- pad_field :body
51
+ pad_field(:body)
63
52
  recalc_block_len
64
53
  super
65
54
  end
@@ -14,13 +14,13 @@ module PacketGen
14
14
  # @author Sylvain Daubert
15
15
  module PcapNG
16
16
  # Section Header Block type number
17
- SHB_TYPE = Types::Int32.new(0x0A0D0D0A, :little).freeze
17
+ SHB_TYPE = BinStruct::Int32.new(value: 0x0A0D0D0A, endian: :little).freeze
18
18
  # Interface Description Block type number
19
- IDB_TYPE = Types::Int32.new(1, :little).freeze
19
+ IDB_TYPE = BinStruct::Int32.new(value: 1, endian: :little).freeze
20
20
  # Simple Packet Block type number
21
- SPB_TYPE = Types::Int32.new(3, :little).freeze
21
+ SPB_TYPE = BinStruct::Int32.new(value: 3, endian: :little).freeze
22
22
  # Enhanced Packet Block type number
23
- EPB_TYPE = Types::Int32.new(6, :little).freeze
23
+ EPB_TYPE = BinStruct::Int32.new(value: 6, endian: :little).freeze
24
24
 
25
25
  # IEEE 802.3 Ethernet (10Mb, 100Mb, 1000Mb, and up)
26
26
  LINKTYPE_ETHERNET = 1
@@ -10,6 +10,7 @@ require 'pcaprub'
10
10
  module PacketGen
11
11
  # Wrapper around PCAPRUB
12
12
  # @author Sylvain Daubert
13
+ # @author LemonTree55
13
14
  # @api private
14
15
  # @since 3.1.4
15
16
  module PCAPRUBWrapper
@@ -35,8 +36,9 @@ module PacketGen
35
36
  pcap.setpromisc(promisc)
36
37
  pcap.settimeout(TIMEOUT)
37
38
  # Monitor MUST be set before pcap is activated
38
- pcap.setmonitor monitor unless monitor.nil?
39
+ pcap.setmonitor(monitor) unless monitor.nil?
39
40
  pcap.activate
41
+ pcap
40
42
  end
41
43
 
42
44
  # Capture packets from a network interface
@@ -74,5 +76,19 @@ module PacketGen
74
76
  def self.read_pcap(filename:, &block)
75
77
  PCAPRUB::Pcap.open_offline(filename).each_packet(&block)
76
78
  end
79
+
80
+ # Write binary packets to a PCAP file
81
+ # @param [String] filename
82
+ # @param [Array<String>] packets
83
+ # @return [void]
84
+ # @since 4.0.0
85
+ # @author LemonTree55
86
+ def self.write_pcap(filename:, packets:)
87
+ PCAPRUB::Pcap.dump_open(filename) do |pcap|
88
+ packets.each do |packet|
89
+ pcap.dump(packet)
90
+ end
91
+ end
92
+ end
77
93
  end
78
94
  end
@@ -10,7 +10,7 @@
10
10
  require 'socket'
11
11
 
12
12
  module PacketGen
13
- # Module handling some helper methods for protocols
13
+ # Module handling some helper methods for well_known protocols
14
14
  # @author Sylvain Daubert
15
15
  # @since 2.1.2
16
16
  module Proto
@@ -30,7 +30,7 @@ module PacketGen
30
30
  end
31
31
 
32
32
  # Get packet body
33
- # @return [Types]
33
+ # @return [String]
34
34
  def body
35
35
  @binary_str
36
36
  end
@@ -66,7 +66,7 @@ module PacketGen
66
66
  str << Inspect.inspect_body(body)
67
67
  end
68
68
 
69
- # equality if {#to_s} is equal
69
+ # equality if {#to_s} are equal
70
70
  # @return [Boolean]
71
71
  def ==(other)
72
72
  to_s == other.to_s
@@ -22,15 +22,15 @@ module PacketGen
22
22
  # @author Sylvain Daubert
23
23
  # @since 2.1.3
24
24
  class ARPSpoofer
25
- # @param [Integer,Float,nil] timeout spoof will happen for this amount
25
+ # @param [Numeric,nil] timeout spoof will happen for this amount
26
26
  # of time
27
- # @param [Integer,Float] interval time between 2 ARP packets
27
+ # @param [Numeric] interval time between 2 ARP packets
28
28
  # @param [String,nil] iface network interface on which do spoofing.
29
29
  # Defaults to {PacketGen.default_iface}
30
30
  def initialize(timeout: nil, interval: 1.0, iface: nil)
31
31
  @timeout = timeout
32
32
  @timeout = @timeout.to_f if @timeout
33
- @interval = interval
33
+ @interval = interval.to_f
34
34
  @iface = iface || PacketGen.default_iface
35
35
  @targets = {}
36
36
  @arp_packets = {}
@@ -56,19 +56,19 @@ module PacketGen
56
56
  # @param [String] target_ip target IP address
57
57
  # @return [void]
58
58
  def remove(target_ip)
59
- @targets.delete target_ip
60
- @arp_packets.delete target_ip
59
+ @targets.delete(target_ip)
60
+ @arp_packets.delete(target_ip)
61
61
  end
62
62
 
63
63
  # Get registered targets (all targets, registered with {#add} and {#start})
64
- # @return [Array<String>] list of target IP addresses
64
+ # @return [Array<String>] list of registeres IP addresses
65
65
  def registered_targets
66
66
  @targets.keys
67
67
  end
68
68
 
69
69
  # Get active targets (registered with {#start}, or all after using
70
70
  # {#start_all})
71
- # @return [Array<String>] list of target IP addresses
71
+ # @return [Array<String>] list of active IP addresses
72
72
  def active_targets
73
73
  @arp_packets.keys
74
74
  end
@@ -83,23 +83,23 @@ module PacketGen
83
83
  # an ARP request is made to get it.
84
84
  # @return [void]
85
85
  def start(target_ip, spoofed_ip, options={})
86
- add target_ip, spoofed_ip, options
87
- activate target_ip
86
+ add(target_ip, spoofed_ip, options)
87
+ activate(target_ip)
88
88
  end
89
89
 
90
90
  # Stop spoofing on given target
91
91
  # @param [String] target_ip target IP address
92
92
  # @return [void]
93
93
  def stop(target_ip)
94
- deactivate target_ip
95
- remove target_ip
94
+ deactivate(target_ip)
95
+ remove(target_ip)
96
96
  end
97
97
 
98
98
  # Start spoofing on all targets added with {#add}.
99
99
  # @return [void]
100
100
  def start_all
101
101
  @targets.each_key do |target_ip|
102
- activate target_ip
102
+ activate(target_ip)
103
103
  end
104
104
  end
105
105
 
@@ -107,7 +107,7 @@ module PacketGen
107
107
  # @return [void]
108
108
  def stop_all
109
109
  @targets.each_key do |target_ip|
110
- deactivate target_ip
110
+ deactivate(target_ip)
111
111
  end
112
112
  end
113
113
 
@@ -115,9 +115,7 @@ module PacketGen
115
115
  # @param [String] target_ip target IP address
116
116
  # @return [Boolean]
117
117
  def active?(target_ip)
118
- # rubocop:disable Style/ReturnNilInPredicateMethodDefinition
119
- return unless @targets.key?(target_ip)
120
- # rubocop:enable Style/ReturnNilInPredicateMethodDefinition
118
+ return false unless @targets.key?(target_ip)
121
119
 
122
120
  @targets[target_ip][:active]
123
121
  end
@@ -126,6 +124,7 @@ module PacketGen
126
124
  # was set on {#initialize}.
127
125
  def wait
128
126
  @spoof_thread.join
127
+ @spoof_thread = nil
129
128
  end
130
129
 
131
130
  private
@@ -143,7 +142,7 @@ module PacketGen
143
142
  # Create spoof thread
144
143
  def create_spoof_thread
145
144
  @spoof_thread = Thread.new(@queue, @timeout, @interval) do |queue, timeout, interval|
146
- while timeout.nil? || timeout > 0.0
145
+ while timeout.nil? || timeout.positive?
147
146
  packets = queue.pop unless queue.empty?
148
147
  send_packets_on_wire(packets) unless packets.empty?
149
148
  timeout -= interval if timeout
@@ -161,9 +160,9 @@ module PacketGen
161
160
  # @param [String] target_ip
162
161
  # @return [void]
163
162
  def deactivate(target_ip)
164
- @arp_packets.delete target_ip
163
+ @arp_packets.delete(target_ip)
165
164
  if @arp_packets.empty?
166
- @spoof_thread.kill
165
+ @spoof_thread&.kill
167
166
  @spoof_thread = nil
168
167
  else
169
168
  @queue << @arp_packets.values
@@ -31,9 +31,9 @@ module PacketGen
31
31
  # @private
32
32
  IP_PATH = '/usr/bin/ip'
33
33
  # @private
34
- ARP_LINE_RE = /\((\d+\.\d+\.\d+\.\d+)\) at (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})(?: \[ether\])? on (\w+)/.freeze
34
+ ARP_LINE_RE = /\((\d+\.\d+\.\d+\.\d+)\) at (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})(?: \[ether\])? on (\w+)/
35
35
  # @private
36
- IP_LINE_RE = /^(\d+\.\d+\.\d+\.\d+) dev (\w+) lladdr (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})/.freeze
36
+ IP_LINE_RE = /^(\d+\.\d+\.\d+\.\d+) dev (\w+) lladdr (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})/
37
37
 
38
38
  # Get local ARP cache
39
39
  # @return [Hash] key: IP address, value: array containing MAC address and
@@ -11,5 +11,5 @@
11
11
  # @author Sylvain Daubert
12
12
  module PacketGen
13
13
  # PacketGen version
14
- VERSION = '3.3.2'
14
+ VERSION = '4.0.0'
15
15
  end
data/lib/packetgen.rb CHANGED
@@ -8,6 +8,7 @@
8
8
  # This program is published under MIT license.
9
9
 
10
10
  require 'packetgen/version'
11
+ require 'bin_struct'
11
12
  require 'interfacez'
12
13
 
13
14
  # PacketGen is a network packet generator and analyzor.
@@ -100,12 +101,13 @@ module PacketGen
100
101
  def self.default_iface
101
102
  return @default_iface if defined? @default_iface
102
103
 
103
- @default_iface = Interfacez.raw_interface_addresses.each do |iface|
104
+ Interfacez.raw_interface_addresses.each do |iface|
104
105
  next unless iface.broadaddr
105
106
  next unless Interfacez.ipv4_address_of(iface.name)
106
107
  next unless Interfacez.ipv6_address_of(iface.name)
107
108
 
108
- break iface.name
109
+ @default_iface = iface.name
110
+ break
109
111
  end
110
112
  end
111
113
 
@@ -133,7 +135,6 @@ end
133
135
 
134
136
  require 'packetgen/deprecation'
135
137
  require 'packetgen/inspect'
136
- require 'packetgen/types'
137
138
  require 'packetgen/pcapng'
138
139
  require 'packetgen/pcap'
139
140
  require 'packetgen/packet'
metadata CHANGED
@@ -1,15 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packetgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.2
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LemonTree55
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-07-13 00:00:00.000000000 Z
10
+ date: 2024-12-28 00:00:00.000000000 Z
12
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: bin_struct
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 0.3.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.3.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: digest-crc
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
13
40
  - !ruby/object:Gem::Dependency
14
41
  name: interfacez
15
42
  requirement: !ruby/object:Gem::Requirement
@@ -44,20 +71,14 @@ dependencies:
44
71
  requirements:
45
72
  - - "~>"
46
73
  - !ruby/object:Gem::Version
47
- version: '0.13'
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: 0.13.1
74
+ version: '0.14'
51
75
  type: :runtime
52
76
  prerelease: false
53
77
  version_requirements: !ruby/object:Gem::Requirement
54
78
  requirements:
55
79
  - - "~>"
56
80
  - !ruby/object:Gem::Version
57
- version: '0.13'
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: 0.13.1
81
+ version: '0.14'
61
82
  description: |
62
83
  PacketGen is a network packet manipulation library. It allows reading, parsing
63
84
  and sending network packets with fun.
@@ -183,24 +204,10 @@ files:
183
204
  - lib/packetgen/pcapng/unknown_block.rb
184
205
  - lib/packetgen/pcaprub_wrapper.rb
185
206
  - lib/packetgen/proto.rb
186
- - lib/packetgen/types.rb
187
- - lib/packetgen/types/abstract_tlv.rb
188
- - lib/packetgen/types/array.rb
189
- - lib/packetgen/types/cstring.rb
190
- - lib/packetgen/types/enum.rb
191
- - lib/packetgen/types/fieldable.rb
192
- - lib/packetgen/types/fields.rb
193
- - lib/packetgen/types/int.rb
194
- - lib/packetgen/types/int_string.rb
195
- - lib/packetgen/types/length_from.rb
196
- - lib/packetgen/types/oui.rb
197
- - lib/packetgen/types/string.rb
198
- - lib/packetgen/types/tlv.rb
199
207
  - lib/packetgen/unknown_packet.rb
200
208
  - lib/packetgen/utils.rb
201
209
  - lib/packetgen/utils/arp_spoofer.rb
202
210
  - lib/packetgen/version.rb
203
- homepage:
204
211
  licenses:
205
212
  - MIT
206
213
  metadata:
@@ -209,7 +216,6 @@ metadata:
209
216
  bug_tracker_uri: https://github.com/lemontree55/packetgen/issues
210
217
  documentation_uri: https://www.rubydoc.info/gems/packetgen
211
218
  wiki_uri: https://github.com/lemontree55/packetgen/wiki
212
- post_install_message:
213
219
  rdoc_options:
214
220
  - "--title"
215
221
  - PacketGen - network packet dissector
@@ -223,15 +229,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
223
229
  requirements:
224
230
  - - ">="
225
231
  - !ruby/object:Gem::Version
226
- version: 2.7.0
232
+ version: 3.0.0
227
233
  required_rubygems_version: !ruby/object:Gem::Requirement
228
234
  requirements:
229
235
  - - ">="
230
236
  - !ruby/object:Gem::Version
231
237
  version: '0'
232
238
  requirements: []
233
- rubygems_version: 3.3.15
234
- signing_key:
239
+ rubygems_version: 3.6.2
235
240
  specification_version: 4
236
241
  summary: Network packet generator and dissector
237
242
  test_files: []