packetgen 3.1.3 → 3.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/packetgen.rb +24 -2
- data/lib/packetgen/capture.rb +30 -24
- data/lib/packetgen/config.rb +2 -2
- data/lib/packetgen/deprecation.rb +33 -7
- data/lib/packetgen/header.rb +2 -9
- data/lib/packetgen/header/arp.rb +2 -2
- data/lib/packetgen/header/asn1_base.rb +2 -2
- data/lib/packetgen/header/base.rb +2 -4
- data/lib/packetgen/header/bootp.rb +3 -3
- data/lib/packetgen/header/dhcp.rb +2 -2
- data/lib/packetgen/header/dhcp/option.rb +2 -2
- data/lib/packetgen/header/dhcp/options.rb +2 -2
- data/lib/packetgen/header/dhcpv6.rb +12 -12
- data/lib/packetgen/header/dhcpv6/duid.rb +8 -4
- data/lib/packetgen/header/dhcpv6/option.rb +4 -4
- data/lib/packetgen/header/dhcpv6/options.rb +2 -2
- data/lib/packetgen/header/dhcpv6/relay.rb +2 -2
- data/lib/packetgen/header/dns.rb +9 -9
- data/lib/packetgen/header/dns/name.rb +2 -2
- data/lib/packetgen/header/dns/opt.rb +2 -2
- data/lib/packetgen/header/dns/option.rb +2 -2
- data/lib/packetgen/header/dns/qdsection.rb +2 -2
- data/lib/packetgen/header/dns/question.rb +35 -35
- data/lib/packetgen/header/dns/rr.rb +3 -3
- data/lib/packetgen/header/dns/rrsection.rb +2 -2
- data/lib/packetgen/header/dot11.rb +5 -13
- data/lib/packetgen/header/dot11/control.rb +5 -5
- data/lib/packetgen/header/dot11/data.rb +2 -2
- data/lib/packetgen/header/dot11/element.rb +16 -16
- data/lib/packetgen/header/dot11/management.rb +2 -2
- data/lib/packetgen/header/dot11/sub_mngt.rb +2 -12
- data/lib/packetgen/header/dot1q.rb +2 -2
- data/lib/packetgen/header/dot1x.rb +6 -6
- data/lib/packetgen/header/eap.rb +16 -16
- data/lib/packetgen/header/eap/fast.rb +2 -2
- data/lib/packetgen/header/eap/md5.rb +2 -2
- data/lib/packetgen/header/eap/tls.rb +2 -2
- data/lib/packetgen/header/eap/ttls.rb +2 -2
- data/lib/packetgen/header/eth.rb +8 -5
- data/lib/packetgen/header/gre.rb +2 -2
- data/lib/packetgen/header/http.rb +2 -0
- data/lib/packetgen/header/http/headers.rb +2 -2
- data/lib/packetgen/header/http/request.rb +5 -5
- data/lib/packetgen/header/http/response.rb +6 -6
- data/lib/packetgen/header/http/verbs.rb +2 -2
- data/lib/packetgen/header/icmp.rb +2 -2
- data/lib/packetgen/header/icmpv6.rb +2 -2
- data/lib/packetgen/header/igmp.rb +4 -4
- data/lib/packetgen/header/igmpv3.rb +3 -3
- data/lib/packetgen/header/igmpv3/group_record.rb +6 -6
- data/lib/packetgen/header/igmpv3/mq.rb +2 -2
- data/lib/packetgen/header/igmpv3/mr.rb +2 -2
- data/lib/packetgen/header/ip.rb +3 -5
- data/lib/packetgen/header/ip/addr.rb +7 -2
- data/lib/packetgen/header/ip/option.rb +4 -6
- data/lib/packetgen/header/ip/options.rb +3 -5
- data/lib/packetgen/header/ipv6.rb +2 -2
- data/lib/packetgen/header/ipv6/addr.rb +7 -2
- data/lib/packetgen/header/ipv6/extension.rb +2 -2
- data/lib/packetgen/header/ipv6/hop_by_hop.rb +3 -3
- data/lib/packetgen/header/llc.rb +2 -2
- data/lib/packetgen/header/mdns.rb +2 -2
- data/lib/packetgen/header/mld.rb +2 -2
- data/lib/packetgen/header/mldv2.rb +2 -2
- data/lib/packetgen/header/mldv2/mcast_address_record.rb +2 -2
- data/lib/packetgen/header/mldv2/mlq.rb +2 -2
- data/lib/packetgen/header/mldv2/mlr.rb +2 -2
- data/lib/packetgen/header/ospfv2.rb +9 -9
- data/lib/packetgen/header/ospfv2/db_description.rb +2 -2
- data/lib/packetgen/header/ospfv2/hello.rb +2 -2
- data/lib/packetgen/header/ospfv2/ls_ack.rb +2 -2
- data/lib/packetgen/header/ospfv2/ls_request.rb +2 -2
- data/lib/packetgen/header/ospfv2/ls_update.rb +2 -2
- data/lib/packetgen/header/ospfv2/lsa.rb +3 -5
- data/lib/packetgen/header/ospfv2/lsa_header.rb +6 -6
- data/lib/packetgen/header/ospfv3.rb +2 -2
- data/lib/packetgen/header/ospfv3/db_description.rb +2 -2
- data/lib/packetgen/header/ospfv3/hello.rb +2 -2
- data/lib/packetgen/header/ospfv3/ipv6_prefix.rb +2 -2
- data/lib/packetgen/header/ospfv3/ls_ack.rb +2 -2
- data/lib/packetgen/header/ospfv3/ls_request.rb +2 -2
- data/lib/packetgen/header/ospfv3/ls_update.rb +2 -2
- data/lib/packetgen/header/ospfv3/lsa.rb +3 -5
- data/lib/packetgen/header/ospfv3/lsa_header.rb +7 -7
- data/lib/packetgen/header/snmp.rb +31 -28
- data/lib/packetgen/header/tcp.rb +3 -3
- data/lib/packetgen/header/tcp/option.rb +3 -5
- data/lib/packetgen/header/tcp/options.rb +2 -2
- data/lib/packetgen/header/tftp.rb +6 -6
- data/lib/packetgen/header/udp.rb +3 -3
- data/lib/packetgen/headerable.rb +5 -4
- data/lib/packetgen/inject.rb +23 -0
- data/lib/packetgen/inspect.rb +24 -5
- data/lib/packetgen/packet.rb +76 -51
- data/lib/packetgen/pcap.rb +29 -0
- data/lib/packetgen/pcapng.rb +2 -2
- data/lib/packetgen/pcapng/block.rb +12 -12
- data/lib/packetgen/pcapng/epb.rb +3 -7
- data/lib/packetgen/pcapng/file.rb +134 -95
- data/lib/packetgen/pcapng/idb.rb +30 -30
- data/lib/packetgen/pcapng/shb.rb +25 -34
- data/lib/packetgen/pcapng/spb.rb +4 -8
- data/lib/packetgen/pcapng/unknown_block.rb +2 -2
- data/lib/packetgen/pcaprub_wrapper.rb +67 -0
- data/lib/packetgen/proto.rb +2 -2
- data/lib/packetgen/types.rb +2 -0
- data/lib/packetgen/types/abstract_tlv.rb +24 -6
- data/lib/packetgen/types/array.rb +5 -5
- data/lib/packetgen/types/cstring.rb +2 -2
- data/lib/packetgen/types/enum.rb +3 -2
- data/lib/packetgen/types/fields.rb +5 -7
- data/lib/packetgen/types/int.rb +3 -5
- data/lib/packetgen/types/int_string.rb +2 -2
- data/lib/packetgen/types/length_from.rb +3 -3
- data/lib/packetgen/types/oui.rb +2 -2
- data/lib/packetgen/types/string.rb +2 -2
- data/lib/packetgen/types/tlv.rb +2 -2
- data/lib/packetgen/utils.rb +2 -2
- data/lib/packetgen/utils/arp_spoofer.rb +2 -2
- data/lib/packetgen/version.rb +3 -3
- metadata +26 -50
- data/.gitignore +0 -13
- data/.rubocop.yml +0 -30
- data/.travis.yml +0 -19
- data/Gemfile +0 -4
- data/Rakefile +0 -21
- data/packetgen.gemspec +0 -36
data/lib/packetgen/pcapng/shb.rb
CHANGED
@@ -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 PcapNG
|
10
10
|
# {SHB} represents a Section Header Block (SHB) of a pcapng file.
|
@@ -89,39 +89,20 @@ module PacketGen
|
|
89
89
|
# @param [::String,IO] str_or_io
|
90
90
|
# @return [self]
|
91
91
|
def read(str_or_io)
|
92
|
-
io =
|
93
|
-
str_or_io
|
94
|
-
else
|
95
|
-
StringIO.new(force_binary(str_or_io.to_s))
|
96
|
-
end
|
92
|
+
io = to_io(str_or_io)
|
97
93
|
return self if io.eof?
|
98
94
|
|
99
|
-
type_str = io
|
100
|
-
unless type_str == PcapNG::SHB_TYPE.to_s
|
101
|
-
type = type_str.unpack('H*').join
|
102
|
-
raise InvalidFileError, "Incorrect type (#{type})for Section Header Block"
|
103
|
-
end
|
104
|
-
|
95
|
+
type_str = check_shb(io)
|
105
96
|
block_len_str = io.read(4)
|
106
|
-
|
107
97
|
magic_str = io.read(4)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
end
|
117
|
-
when :big
|
118
|
-
case magic_str
|
119
|
-
when MAGIC_BIG
|
120
|
-
when MAGIC_LITTLE
|
121
|
-
force_endianness :little
|
122
|
-
else
|
123
|
-
raise InvalidFileError, 'Incorrect magic for Section Header Block'
|
124
|
-
end
|
98
|
+
|
99
|
+
case magic_str
|
100
|
+
when MAGIC_LITTLE
|
101
|
+
force_endianness :little if @endian == :big
|
102
|
+
when MAGIC_BIG
|
103
|
+
force_endianness :big if @endian == :little
|
104
|
+
else
|
105
|
+
raise InvalidFileError, 'Incorrect magic for Section Header Block'
|
125
106
|
end
|
126
107
|
|
127
108
|
self[:type].read type_str
|
@@ -149,9 +130,8 @@ module PacketGen
|
|
149
130
|
# @return [String]
|
150
131
|
def to_s
|
151
132
|
body = @interfaces.map(&:to_s).join
|
152
|
-
unless self.section_len == SECTION_LEN_UNDEFINED
|
153
|
-
|
154
|
-
end
|
133
|
+
self.section_len = body.size unless self.section_len == SECTION_LEN_UNDEFINED
|
134
|
+
|
155
135
|
pad_field :options
|
156
136
|
recalc_block_len
|
157
137
|
super + body
|
@@ -169,6 +149,17 @@ module PacketGen
|
|
169
149
|
self[:section_len] = Types::Int64.new(self[:section_len].to_i, endian)
|
170
150
|
self[:block_len2] = Types::Int32.new(self[:block_len2].to_i, endian)
|
171
151
|
end
|
152
|
+
|
153
|
+
# Check io contains a SHB
|
154
|
+
# @param [IO] io
|
155
|
+
# @return [String] type string
|
156
|
+
def check_shb(io)
|
157
|
+
type_str = io.read(4)
|
158
|
+
return type_str if type_str == PcapNG::SHB_TYPE.to_s
|
159
|
+
|
160
|
+
type = type_str.unpack('H*').join
|
161
|
+
raise InvalidFileError, "Incorrect type (#{type})for Section Header Block"
|
162
|
+
end
|
172
163
|
end
|
173
164
|
end
|
174
165
|
end
|
data/lib/packetgen/pcapng/spb.rb
CHANGED
@@ -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 PcapNG
|
10
10
|
# {SPB} represents a Section Simple Packet Block (SPB) of a pcapng file.
|
@@ -60,11 +60,7 @@ module PacketGen
|
|
60
60
|
# @param [::String,IO] str_or_io
|
61
61
|
# @return [self]
|
62
62
|
def read(str_or_io)
|
63
|
-
io =
|
64
|
-
str_or_io
|
65
|
-
else
|
66
|
-
StringIO.new(force_binary(str_or_io.to_s))
|
67
|
-
end
|
63
|
+
io = to_io(str_or_io)
|
68
64
|
return self if io.eof?
|
69
65
|
|
70
66
|
self[:type].read io.read(4)
|
@@ -72,7 +68,7 @@ module PacketGen
|
|
72
68
|
self[:orig_len].read io.read(4)
|
73
69
|
# Take care of IDB snaplen
|
74
70
|
# CAUTION: snaplen == 0 -> no capture limit
|
75
|
-
data_len = if interface &&
|
71
|
+
data_len = if interface && interface.snaplen.to_i.positive?
|
76
72
|
[self[:orig_len].to_i, interface.snaplen.to_i].min
|
77
73
|
else
|
78
74
|
self[:orig_len].to_i
|
@@ -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 PcapNG
|
10
10
|
# {UnknownBlock} is used to handle unsupported blocks of a pcapng file.
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This file is part of PacketGen
|
4
|
+
# See https://github.com/sdaubert/packetgen for more informations
|
5
|
+
# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
|
6
|
+
# This program is published under MIT license.
|
7
|
+
require 'pcaprub'
|
8
|
+
|
9
|
+
module PacketGen
|
10
|
+
# Wrapper around PCAPRUB
|
11
|
+
# @author Sylvain Daubert
|
12
|
+
# @api private
|
13
|
+
# @since 3.1.4
|
14
|
+
module PCAPRUBWrapper
|
15
|
+
# timeout for PCAPRUB
|
16
|
+
TIMEOUT = 1
|
17
|
+
# Default snaplen to use
|
18
|
+
DEFAULT_SNAPLEN = 0xffff
|
19
|
+
# Default promisc value to use
|
20
|
+
DEFAULT_PROMISC = false
|
21
|
+
|
22
|
+
# Open an interface for capturing
|
23
|
+
# @param [String] iface interface name
|
24
|
+
# @param [Integer] snaplen
|
25
|
+
# @param [Boolean] promisc
|
26
|
+
# @return [PCAPRUB::Pcap]
|
27
|
+
def self.open_iface(iface:, snaplen: DEFAULT_SNAPLEN, promisc: DEFAULT_PROMISC)
|
28
|
+
PCAPRUB::Pcap.open_live(iface, snaplen, promisc, TIMEOUT)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Capture packets from a network interface
|
32
|
+
# @param [String] iface interface name
|
33
|
+
# @param [Integer] snaplen
|
34
|
+
# @param [Boolean] promisc
|
35
|
+
# @param [String] filter BPF filter
|
36
|
+
# @yieldparam [String] packet_data binary packet data
|
37
|
+
# @return [void]
|
38
|
+
def self.capture(iface:, snaplen: DEFAULT_SNAPLEN, promisc: DEFAULT_PROMISC, filter: nil)
|
39
|
+
pcap = self.open_iface(iface: iface, snaplen: snaplen, promisc: promisc)
|
40
|
+
pcap.setfilter filter unless filter.nil?
|
41
|
+
pcap.each do |packet_data|
|
42
|
+
yield packet_data
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Inject given data onto wire
|
47
|
+
# @param [String] iface interface name
|
48
|
+
# @param [String] data to inject
|
49
|
+
# @return [void]
|
50
|
+
def self.inject(iface:, data:)
|
51
|
+
pcap = self.open_iface(iface: iface)
|
52
|
+
pcap.inject(data)
|
53
|
+
pcap.close
|
54
|
+
end
|
55
|
+
|
56
|
+
# Read a PCAP file
|
57
|
+
# @param [String] filename
|
58
|
+
# @yieldparam [String] data binary packet data
|
59
|
+
# @return [void]
|
60
|
+
# @author Kent Gruber
|
61
|
+
def self.read_pcap(filename:)
|
62
|
+
PCAPRUB::Pcap.open_offline(filename).each_packet do |packet|
|
63
|
+
yield packet
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/packetgen/proto.rb
CHANGED
@@ -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 'socket'
|
10
10
|
|
11
11
|
module PacketGen
|
data/lib/packetgen/types.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
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 Types
|
11
11
|
# This class is an abstract class to define type-length-value data.
|
12
12
|
#
|
13
|
-
# This class
|
13
|
+
# This class supersedes {TLV} class, which is not well defined on some corner
|
14
14
|
# cases.
|
15
15
|
#
|
16
16
|
# ===Usage
|
@@ -33,7 +33,7 @@ module PacketGen
|
|
33
33
|
# tlv.value #=> "abcd"
|
34
34
|
#
|
35
35
|
# ===Advanced usage
|
36
|
-
# Each field's type may be
|
36
|
+
# Each field's type may be changed at generating TLV class:
|
37
37
|
# MyTLV = PacketGen::Types::AbstractTLV.create(type_class: PacketGen::Types::Int16,
|
38
38
|
# length_class: PacketGen::Types::Int16,
|
39
39
|
# value_class: PacketGen::Header::IP::Addr)
|
@@ -61,6 +61,7 @@ module PacketGen
|
|
61
61
|
class <<self
|
62
62
|
# @return [Hash]
|
63
63
|
attr_accessor :aliases
|
64
|
+
attr_accessor :header_in_length
|
64
65
|
end
|
65
66
|
self.aliases = {}
|
66
67
|
|
@@ -68,12 +69,17 @@ module PacketGen
|
|
68
69
|
# @param [Class] type_class Class to use for +type+
|
69
70
|
# @param [Class] length_class Class to use for +length+
|
70
71
|
# @param [Class] value_class Class to use for +value+
|
72
|
+
# @param [Boolean] header_in_length if +true +, +type+ and +length+ fields are
|
73
|
+
# included in length
|
71
74
|
# @return [Class]
|
72
|
-
|
75
|
+
# @since 3.1.4 Add +header_in_length+ parameter
|
76
|
+
def self.create(type_class: Int8Enum, length_class: Int8, value_class: String,
|
77
|
+
aliases: {}, header_in_length: false)
|
73
78
|
raise Error, '.create cannot be called on a subclass of PacketGen::Types::AbstractTLV' unless self.equal? AbstractTLV
|
74
79
|
|
75
80
|
klass = Class.new(self)
|
76
81
|
klass.aliases = aliases
|
82
|
+
klass.header_in_length = header_in_length
|
77
83
|
|
78
84
|
if type_class < Enum
|
79
85
|
klass.define_field :type, type_class, enum: {}
|
@@ -117,6 +123,7 @@ module PacketGen
|
|
117
123
|
# @option options [Integer] :length
|
118
124
|
# @option options [Object] :value
|
119
125
|
def initialize(options={})
|
126
|
+
@header_in_length = self.class.header_in_length
|
120
127
|
self.class.aliases.each do |al, orig|
|
121
128
|
options[orig] = options[al] if options.key?(al)
|
122
129
|
end
|
@@ -136,7 +143,7 @@ module PacketGen
|
|
136
143
|
idx += self[:type].sz
|
137
144
|
self[:length].read str[idx, self[:length].sz]
|
138
145
|
idx += self[:length].sz
|
139
|
-
self[:value].read str[idx,
|
146
|
+
self[:value].read str[idx, real_length]
|
140
147
|
self
|
141
148
|
end
|
142
149
|
|
@@ -147,6 +154,7 @@ module PacketGen
|
|
147
154
|
def value=(val)
|
148
155
|
self[:value].from_human val
|
149
156
|
self.length = self[:value].sz
|
157
|
+
self.length += self[:type].sz + self[:length].sz if @header_in_length
|
150
158
|
val
|
151
159
|
end
|
152
160
|
|
@@ -163,6 +171,16 @@ module PacketGen
|
|
163
171
|
my_value = self[:value].is_a?(String) ? self[:value].inspect : self[:value].to_human
|
164
172
|
"type:%s,length:%u,value:#{my_value}" % [human_type, length]
|
165
173
|
end
|
174
|
+
|
175
|
+
private
|
176
|
+
|
177
|
+
def real_length
|
178
|
+
if @header_in_length
|
179
|
+
self.length - self[:type].sz - self[:length].sz
|
180
|
+
else
|
181
|
+
self.length
|
182
|
+
end
|
183
|
+
end
|
166
184
|
end
|
167
185
|
end
|
168
186
|
end
|
@@ -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
|
require 'forwardable'
|
9
9
|
|
10
10
|
module PacketGen
|
@@ -106,7 +106,7 @@ module PacketGen
|
|
106
106
|
# @return [void]
|
107
107
|
def clear!
|
108
108
|
@array.clear
|
109
|
-
@counter
|
109
|
+
@counter&.read(0)
|
110
110
|
end
|
111
111
|
|
112
112
|
# Delete an object from this array. Update associated counter if any
|
@@ -150,7 +150,7 @@ module PacketGen
|
|
150
150
|
# @return [Array] self
|
151
151
|
def <<(obj)
|
152
152
|
push obj
|
153
|
-
@counter
|
153
|
+
@counter&.read(@counter.to_i + 1)
|
154
154
|
self
|
155
155
|
end
|
156
156
|
|
@@ -160,7 +160,7 @@ module PacketGen
|
|
160
160
|
def read(str)
|
161
161
|
clear
|
162
162
|
return self if str.nil?
|
163
|
-
return self if @counter
|
163
|
+
return self if @counter&.to_i&.zero?
|
164
164
|
|
165
165
|
str = read_with_length_from(str)
|
166
166
|
klass = self.class.set_of_klass
|
@@ -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 Types
|
11
11
|
# This class handles null-terminated strings (aka C strings).
|
data/lib/packetgen/types/enum.rb
CHANGED
@@ -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 Types
|
11
11
|
# @abstract Base enum class to handle binary integers with limited
|
@@ -55,6 +55,7 @@ module PacketGen
|
|
55
55
|
nil
|
56
56
|
when ::String
|
57
57
|
raise ArgumentError, "#{value.inspect} not in enumeration" unless @enum.key? value
|
58
|
+
|
58
59
|
@enum[value]
|
59
60
|
else
|
60
61
|
value.to_i
|
@@ -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 Types
|
10
10
|
# @abstract Set of fields
|
@@ -183,14 +183,14 @@ module PacketGen
|
|
183
183
|
" else\n" \
|
184
184
|
" self[:#{name}]\n" \
|
185
185
|
" end\n" \
|
186
|
-
|
186
|
+
'end'
|
187
187
|
define << "def #{name}=(val)\n" \
|
188
188
|
" if self[:#{name}].respond_to?(:to_human) && self[:#{name}].respond_to?(:from_human)\n" \
|
189
189
|
" self[:#{name}].from_human val\n" \
|
190
190
|
" else\n" \
|
191
191
|
" self[:#{name}].read val\n" \
|
192
192
|
" end\n" \
|
193
|
-
|
193
|
+
'end'
|
194
194
|
end
|
195
195
|
|
196
196
|
define.delete_at(1) if instance_methods.include? "#{name}=".to_sym
|
@@ -295,9 +295,7 @@ module PacketGen
|
|
295
295
|
raise ArgumentError, "unknown #{attr} field" if attr_def.nil?
|
296
296
|
|
297
297
|
type = attr_def.type
|
298
|
-
unless type < Types::Int
|
299
|
-
raise TypeError, "#{attr} is not a PacketGen::Types::Int"
|
300
|
-
end
|
298
|
+
raise TypeError, "#{attr} is not a PacketGen::Types::Int" unless type < Types::Int
|
301
299
|
|
302
300
|
total_size = type.new.width * 8
|
303
301
|
idx = total_size - 1
|
data/lib/packetgen/types/int.rb
CHANGED
@@ -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 Types
|
11
11
|
# Base integer class to handle binary integers
|
@@ -56,9 +56,7 @@ module PacketGen
|
|
56
56
|
# @return [::String]
|
57
57
|
# @raise [ParseError] This is an abstrat method and must be redefined
|
58
58
|
def to_s
|
59
|
-
unless defined? @packstr
|
60
|
-
raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method'
|
61
|
-
end
|
59
|
+
raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr
|
62
60
|
|
63
61
|
[to_i].pack(@packstr[@endian])
|
64
62
|
end
|