packetgen 3.1.1 → 3.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/pgconsole +1 -0
- data/lib/packetgen.rb +33 -4
- data/lib/packetgen/capture.rb +51 -28
- data/lib/packetgen/config.rb +17 -11
- data/lib/packetgen/deprecation.rb +34 -8
- 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 +70 -74
- 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 +11 -5
- data/lib/packetgen/header/dhcpv6/option.rb +8 -16
- 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 +20 -9
- 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 +3 -3
- data/lib/packetgen/header/dns/question.rb +37 -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 +30 -51
- data/lib/packetgen/header/dot11/control.rb +5 -5
- data/lib/packetgen/header/dot11/data.rb +11 -7
- 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 +7 -20
- data/lib/packetgen/header/eap.rb +30 -33
- 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 +13 -11
- data/lib/packetgen/header/gre.rb +2 -2
- data/lib/packetgen/header/http.rb +2 -0
- data/lib/packetgen/header/http/headers.rb +6 -4
- data/lib/packetgen/header/http/request.rb +36 -21
- data/lib/packetgen/header/http/response.rb +7 -7
- data/lib/packetgen/header/http/verbs.rb +3 -3
- 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 +8 -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 +30 -31
- data/lib/packetgen/header/ip/addr.rb +10 -3
- data/lib/packetgen/header/ip/option.rb +8 -10
- data/lib/packetgen/header/ip/options.rb +3 -5
- data/lib/packetgen/header/ipv6.rb +2 -2
- data/lib/packetgen/header/ipv6/addr.rb +9 -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 +4 -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 +4 -2
- data/lib/packetgen/header/ospfv2/ls_update.rb +2 -2
- data/lib/packetgen/header/ospfv2/lsa.rb +9 -5
- data/lib/packetgen/header/ospfv2/lsa_header.rb +8 -7
- 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 +4 -2
- data/lib/packetgen/header/ospfv3/ls_ack.rb +2 -2
- data/lib/packetgen/header/ospfv3/ls_request.rb +4 -2
- data/lib/packetgen/header/ospfv3/ls_update.rb +2 -2
- data/lib/packetgen/header/ospfv3/lsa.rb +5 -5
- data/lib/packetgen/header/ospfv3/lsa_header.rb +9 -8
- data/lib/packetgen/header/snmp.rb +33 -29
- data/lib/packetgen/header/tcp.rb +4 -23
- data/lib/packetgen/header/tcp/option.rb +11 -11
- 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 +23 -20
- data/lib/packetgen/packet.rb +96 -53
- data/lib/packetgen/pcap.rb +29 -0
- data/lib/packetgen/pcapng.rb +13 -13
- data/lib/packetgen/pcapng/block.rb +26 -13
- data/lib/packetgen/pcapng/epb.rb +25 -22
- data/lib/packetgen/pcapng/file.rb +260 -138
- data/lib/packetgen/pcapng/idb.rb +36 -38
- data/lib/packetgen/pcapng/shb.rb +51 -53
- data/lib/packetgen/pcapng/spb.rb +19 -19
- data/lib/packetgen/pcapng/unknown_block.rb +5 -13
- data/lib/packetgen/pcaprub_wrapper.rb +81 -0
- data/lib/packetgen/proto.rb +2 -2
- data/lib/packetgen/types.rb +3 -0
- data/lib/packetgen/types/abstract_tlv.rb +28 -8
- data/lib/packetgen/types/array.rb +22 -15
- data/lib/packetgen/types/cstring.rb +40 -16
- data/lib/packetgen/types/enum.rb +8 -3
- data/lib/packetgen/types/fieldable.rb +65 -0
- data/lib/packetgen/types/fields.rb +182 -117
- data/lib/packetgen/types/int.rb +18 -6
- data/lib/packetgen/types/int_string.rb +10 -2
- data/lib/packetgen/types/length_from.rb +20 -12
- data/lib/packetgen/types/oui.rb +4 -2
- data/lib/packetgen/types/string.rb +46 -8
- data/lib/packetgen/types/tlv.rb +4 -2
- data/lib/packetgen/utils.rb +4 -4
- data/lib/packetgen/utils/arp_spoofer.rb +2 -2
- data/lib/packetgen/version.rb +3 -3
- metadata +35 -50
- data/.gitignore +0 -13
- data/.rubocop.yml +0 -28
- data/.travis.yml +0 -17
- data/Gemfile +0 -4
- data/Rakefile +0 -21
- data/packetgen.gemspec +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8959dbe60df80490d1ac31eaf40b05a206860bf6bed306997a66ca9c77dfb0b
|
4
|
+
data.tar.gz: 4615a1084f86e4b0ab216b3766dbde7d1b0b2b1e3c2e6b0d2e1a63aba1ff7bd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 636f0dcea926b57cb7ec7a824744dfbcab9c6ddbb7f6749d8fda8f63fec410d20dcf199f362ae63878104725e94c7075d17f94b3f0dc8458b3d975e9521343da
|
7
|
+
data.tar.gz: 50e679400b1f882fbe2d3bc9e3ba5e01bb24dbe4a83b3372b999cbe0d1565703a7fc6afc08c437900e23b92c549c46ad422e3e76140254034c8840ba3a8ecb2e
|
data/bin/pgconsole
CHANGED
data/lib/packetgen.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 'packetgen/version'
|
10
10
|
require 'interfacez'
|
11
11
|
|
@@ -24,6 +24,27 @@ module PacketGen
|
|
24
24
|
# Sending packet on wire error
|
25
25
|
class WireError < Error; end
|
26
26
|
|
27
|
+
# No known binding
|
28
|
+
class BindingError < Error
|
29
|
+
# @return [Headerable]
|
30
|
+
attr_reader :prev_hdr
|
31
|
+
# @return [Headerable]
|
32
|
+
attr_reader :hdr
|
33
|
+
|
34
|
+
def initialize(prev_hdr, hdr)
|
35
|
+
super()
|
36
|
+
@prev_hdr = prev_hdr
|
37
|
+
@hdr = hdr
|
38
|
+
end
|
39
|
+
|
40
|
+
def message
|
41
|
+
"#{prev_hdr.class} knowns no layer association with #{hdr.protocol_name}. " \
|
42
|
+
"Try #{prev_hdr.class}.bind_layer(#{hdr.class}, " \
|
43
|
+
"#{prev_hdr.method_name}_proto_field: " \
|
44
|
+
"<value_for_#{hdr.method_name}>)"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
27
48
|
# Shortcut for {Packet.gen}
|
28
49
|
# @param [String] protocol base protocol for packet
|
29
50
|
# @param [Hash] options specific options for +protocol+
|
@@ -46,7 +67,7 @@ module PacketGen
|
|
46
67
|
# @yieldparam [Packet] packet
|
47
68
|
# @return [Array<Packet>]
|
48
69
|
def self.capture(**kwargs)
|
49
|
-
Packet.capture(kwargs) { |packet| yield packet if block_given? }
|
70
|
+
Packet.capture(**kwargs) { |packet| yield packet if block_given? }
|
50
71
|
end
|
51
72
|
|
52
73
|
# Shortcut for {Packet.read}
|
@@ -77,6 +98,12 @@ module PacketGen
|
|
77
98
|
Interfacez.default
|
78
99
|
end
|
79
100
|
|
101
|
+
# Get loopback network interface
|
102
|
+
# @return [String]
|
103
|
+
def self.loopback_iface
|
104
|
+
Interfacez.loopback
|
105
|
+
end
|
106
|
+
|
80
107
|
# Shortcut to get a header class
|
81
108
|
# @example builtin class
|
82
109
|
# # same as PacketGen::Header::Dot11:Data.new(id: 0xfedc)
|
@@ -94,9 +121,11 @@ module PacketGen
|
|
94
121
|
end
|
95
122
|
|
96
123
|
require 'packetgen/deprecation'
|
97
|
-
require 'packetgen/types'
|
98
124
|
require 'packetgen/inspect'
|
125
|
+
require 'packetgen/types'
|
99
126
|
require 'packetgen/pcapng'
|
127
|
+
require 'packetgen/pcap'
|
100
128
|
require 'packetgen/packet'
|
101
129
|
require 'packetgen/capture'
|
130
|
+
require 'packetgen/inject'
|
102
131
|
require 'packetgen/proto'
|
data/lib/packetgen/capture.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
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
|
-
|
6
|
-
# frozen_string_literal: true
|
7
|
+
require_relative 'pcaprub_wrapper'
|
7
8
|
|
8
9
|
module PacketGen
|
9
10
|
# Capture packets from wire
|
10
11
|
# @author Sylvain Daubert
|
11
12
|
# @author Kent 'picat' Gruber
|
12
13
|
class Capture
|
13
|
-
# Default snaplen to use if :snaplen option not defined.
|
14
|
-
DEFAULT_SNAPLEN = 0xffff
|
15
|
-
|
16
14
|
private
|
17
15
|
|
18
|
-
attr_reader :filter, :cap_thread
|
16
|
+
attr_reader :filter, :cap_thread, :snaplen, :promisc, :monitor
|
17
|
+
|
18
|
+
# rubocop:disable Metrics/ParameterLists
|
19
19
|
|
20
20
|
public
|
21
21
|
|
@@ -43,37 +43,34 @@ module PacketGen
|
|
43
43
|
# yielding. Default: +true+
|
44
44
|
# @param [Integer] snaplen maximum number of bytes to capture for
|
45
45
|
# each packet.
|
46
|
+
# @param [Boolean] monitor enable or disable monitor mode on interface (if supported by +iface+).
|
46
47
|
# @since 2.0.0 remove old 1.x API
|
47
|
-
# @since 3.0.0 arguments are kwargs and
|
48
|
-
|
49
|
-
|
48
|
+
# @since 3.0.0 arguments are kwargs and no more a hash
|
49
|
+
# @since 3.1.5 add monitor argument
|
50
|
+
# @author Sylvain Daubert
|
51
|
+
# @author optix2000 - add monitor argument
|
52
|
+
def initialize(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil)
|
53
|
+
@iface = iface || PacketGen.default_iface || PacketGen.loopback_iface
|
50
54
|
|
51
55
|
@packets = []
|
52
56
|
@raw_packets = []
|
53
|
-
set_options iface, max, timeout, filter, promisc, parse, snaplen
|
57
|
+
set_options iface, max, timeout, filter, promisc, parse, snaplen, monitor
|
54
58
|
end
|
55
59
|
|
56
60
|
# Start capture
|
57
61
|
# @see {#initialize} for parameters
|
58
62
|
# @yieldparam [Packet,String] packet if a block is given, yield each
|
59
63
|
# captured packet (Packet or raw data String, depending on +:parse+ option)
|
60
|
-
# @since 3.0.0 arguments are kwargs and
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
# @since 3.0.0 arguments are kwargs and no more a hash
|
65
|
+
# @since 3.1.5 add monitor argument
|
66
|
+
# @author Sylvain Daubert
|
67
|
+
# @author optix2000 - add monitor argument
|
68
|
+
def start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil, &block)
|
69
|
+
set_options iface, max, timeout, filter, promisc, parse, snaplen, monitor
|
66
70
|
|
67
71
|
@cap_thread = Thread.new do
|
68
|
-
|
69
|
-
|
70
|
-
if @parse
|
71
|
-
packet = Packet.parse(packet_data)
|
72
|
-
packets << packet
|
73
|
-
yield packet if block_given?
|
74
|
-
elsif block_given?
|
75
|
-
yield packet_data
|
76
|
-
end
|
72
|
+
PCAPRUBWrapper.capture(**capture_args) do |packet_data|
|
73
|
+
add_packet(packet_data, &block)
|
77
74
|
break if defined?(@max) && (raw_packets.size >= @max)
|
78
75
|
end
|
79
76
|
end
|
@@ -91,7 +88,10 @@ module PacketGen
|
|
91
88
|
|
92
89
|
private
|
93
90
|
|
94
|
-
|
91
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
92
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
93
|
+
|
94
|
+
def set_options(iface, max, timeout, filter, promisc, parse, snaplen, monitor)
|
95
95
|
@max = max if max
|
96
96
|
@filter = filter unless filter.nil?
|
97
97
|
@timeout = timeout unless timeout.nil?
|
@@ -99,12 +99,35 @@ module PacketGen
|
|
99
99
|
@snaplen = snaplen unless snaplen.nil?
|
100
100
|
@parse = parse unless parse.nil?
|
101
101
|
@iface = iface unless iface.nil?
|
102
|
+
@monitor = monitor unless monitor.nil?
|
103
|
+
end
|
104
|
+
|
105
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
106
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
107
|
+
# rubocop:enable Metrics/ParameterLists
|
108
|
+
|
109
|
+
def capture_args
|
110
|
+
h = { iface: iface, filter: filter, monitor: monitor }
|
111
|
+
h[:snaplen] = snaplen unless snaplen.nil?
|
112
|
+
h[:promisc] = promisc unless promisc.nil?
|
113
|
+
h
|
102
114
|
end
|
103
115
|
|
104
|
-
def
|
116
|
+
def filter_on(pcap)
|
105
117
|
return if filter.nil? || filter.empty?
|
106
118
|
|
107
|
-
pcap
|
119
|
+
PCAPRUBWrapper.filter_on(pcap: pcap, filter: filter)
|
120
|
+
end
|
121
|
+
|
122
|
+
def add_packet(data, &block)
|
123
|
+
raw_packets << data
|
124
|
+
if @parse
|
125
|
+
packet = Packet.parse(data)
|
126
|
+
packets << packet
|
127
|
+
block&.call(packet)
|
128
|
+
elsif block
|
129
|
+
yield data
|
130
|
+
end
|
108
131
|
end
|
109
132
|
end
|
110
133
|
end
|
data/lib/packetgen/config.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
|
require 'socket'
|
9
9
|
require 'singleton'
|
10
10
|
|
@@ -22,37 +22,43 @@ module PacketGen
|
|
22
22
|
attr_reader :default_iface
|
23
23
|
|
24
24
|
def initialize
|
25
|
-
@default_iface =
|
25
|
+
@default_iface = PacketGen.default_iface || PacketGen.loopback_iface
|
26
26
|
@hwaddr = {}
|
27
27
|
@ipaddr = {}
|
28
28
|
@ip6addr = {}
|
29
29
|
|
30
|
-
|
31
|
-
@hwaddr[iface_name] = Interfacez.mac_address_of(iface_name)
|
32
|
-
@ipaddr[iface_name] = Interfacez.ipv4_address_of(iface_name)
|
33
|
-
@ip6addr[iface_name] = Interfacez.ipv6_addresses_of(iface_name)
|
34
|
-
end
|
30
|
+
initialize_local_addresses
|
35
31
|
end
|
36
32
|
|
37
33
|
# Get MAC address for given network interface
|
38
34
|
# @param [String,nil] iface network interface. If +nil+, use default one.
|
39
35
|
# @return [String]
|
40
36
|
def hwaddr(iface=nil)
|
41
|
-
@hwaddr[iface ||
|
37
|
+
@hwaddr[iface || default_iface]
|
42
38
|
end
|
43
39
|
|
44
40
|
# Get IP address for given network interface
|
45
41
|
# @param [String,nil] iface network interface. If +nil+, use default one.
|
46
42
|
# @return [String]
|
47
43
|
def ipaddr(iface=nil)
|
48
|
-
@ipaddr[iface ||
|
44
|
+
@ipaddr[iface || default_iface]
|
49
45
|
end
|
50
46
|
|
51
47
|
# Get IPv6 addresses for given network interface
|
52
48
|
# @param [String,nil] iface network interface. If +nil+, use default one.
|
53
49
|
# @return [Array<String>]
|
54
50
|
def ip6addr(iface=nil)
|
55
|
-
@ip6addr[iface ||
|
51
|
+
@ip6addr[iface || default_iface]
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def initialize_local_addresses
|
57
|
+
Interfacez.all do |iface_name|
|
58
|
+
@hwaddr[iface_name] = Interfacez.mac_address_of(iface_name)
|
59
|
+
@ipaddr[iface_name] = Interfacez.ipv4_address_of(iface_name)
|
60
|
+
@ip6addr[iface_name] = Interfacez.ipv6_addresses_of(iface_name)
|
61
|
+
end
|
56
62
|
end
|
57
63
|
end
|
58
64
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module PacketGen
|
2
4
|
# Deprecation module
|
3
5
|
# @since 2.7.0
|
@@ -8,6 +10,14 @@ module PacketGen
|
|
8
10
|
# @since 3.1.0
|
9
11
|
REMOVE_VERSION = '4.0.0'
|
10
12
|
|
13
|
+
# @private
|
14
|
+
# @param [String] remove_version
|
15
|
+
# @return [String]
|
16
|
+
# @since 3.1.4
|
17
|
+
def self.removed(remove_version)
|
18
|
+
"It will be removed in PacketGen #{remove_version}"
|
19
|
+
end
|
20
|
+
|
11
21
|
# Warn when using a deprecated method
|
12
22
|
# @param [Module] klass class/module of deprecated method
|
13
23
|
# @param [Symbol,String] deprecated_method
|
@@ -17,15 +27,14 @@ module PacketGen
|
|
17
27
|
# @param [String] remove_version version from which +deprecated_method+ will
|
18
28
|
# no more exist.
|
19
29
|
def self.deprecated(klass, deprecated_method, new_method=nil, klass_method: false, remove_version: REMOVE_VERSION)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
complete_new_method_name = base_name + new_method.to_s unless new_method.nil?
|
30
|
+
base_name = "#{klass}#{klass_method ? '.' : '#'}"
|
31
|
+
complete_deprecated_method_name = "#{base_name}#{deprecated_method}"
|
32
|
+
complete_new_method_name = "#{base_name}#{new_method}" unless new_method.nil?
|
24
33
|
|
25
|
-
file, line = caller(2).first.split(':')[0, 2]
|
34
|
+
file, line = caller(2..2).first.split(':')[0, 2]
|
26
35
|
message = +"#{file}:#{line}: #{complete_deprecated_method_name} is deprecated"
|
27
36
|
message << " in favor of #{complete_new_method_name}" unless new_method.nil?
|
28
|
-
message <<
|
37
|
+
message << '. ' << self.removed(remove_version)
|
29
38
|
warn message
|
30
39
|
end
|
31
40
|
|
@@ -38,8 +47,25 @@ module PacketGen
|
|
38
47
|
def self.deprecated_class(klass, new_klass=nil, remove_version: REMOVE_VERSION)
|
39
48
|
file, line = caller(2..2).first.split(':')[0, 2]
|
40
49
|
message = +"#{file}:#{line}: #{klass} is deprecated"
|
41
|
-
message << " in favor
|
42
|
-
message <<
|
50
|
+
message << " in favor of #{new_klass}" unless new_klass.nil?
|
51
|
+
message << '. ' << self.removed(remove_version)
|
52
|
+
warn message
|
53
|
+
end
|
54
|
+
|
55
|
+
# Warn when using a deprecated method's option
|
56
|
+
# @param [Module] klass deprecated class/module
|
57
|
+
# @param [Module] method method name
|
58
|
+
# @param [Symbol] option option name
|
59
|
+
# @param [Boolean] klass_method +deprecated_method+ is a class method (+true+)
|
60
|
+
# or a, instance one (+false+)
|
61
|
+
# @param [String] remove_version version from which +klass+ will
|
62
|
+
# no more exist.
|
63
|
+
# @since 3.1.4
|
64
|
+
def self.deprecated_option(klass, method, option, klass_method: false, remove_version: REMOVE_VERSION)
|
65
|
+
base_name = "#{klass}#{klass_method ? '.' : '#'}"
|
66
|
+
method_name = "#{base_name}#{method}"
|
67
|
+
message = +"option #{option} is deprecated for method #{method_name}. "
|
68
|
+
message << self.removed(remove_version)
|
43
69
|
warn message
|
44
70
|
end
|
45
71
|
end
|
data/lib/packetgen/header.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
|
# Namespace for protocol header classes.
|
11
11
|
#
|
@@ -30,13 +30,6 @@ module PacketGen
|
|
30
30
|
# pkt.myheader.field2.read 0x01
|
31
31
|
# @author Sylvain Daubert
|
32
32
|
module Header
|
33
|
-
# @private snap length for PCAPRUB
|
34
|
-
PCAP_SNAPLEN = 0xffff
|
35
|
-
# @private promiscuous (or not) for PCAPRUB
|
36
|
-
PCAP_PROMISC = false
|
37
|
-
# @private timeout for PCAPRUB
|
38
|
-
PCAP_TIMEOUT = 1
|
39
|
-
|
40
33
|
@added_header_classes = {}
|
41
34
|
|
42
35
|
class << self
|
data/lib/packetgen/header/arp.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 Header
|
10
10
|
# An ARP header consists of:
|
@@ -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 'rasn1'
|
9
9
|
|
10
10
|
module PacketGen
|
@@ -1,10 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# This file is part of PacketGen
|
2
4
|
# See https://github.com/sdaubert/packetgen for more informations
|
3
5
|
# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
|
4
6
|
# This program is published under MIT license.
|
5
7
|
|
6
|
-
# frozen_string_literal: true
|
7
|
-
|
8
8
|
module PacketGen
|
9
9
|
module Header
|
10
10
|
# @abstract Base class for all header types.
|
@@ -99,10 +99,8 @@ module PacketGen
|
|
99
99
|
|
100
100
|
# each iterator
|
101
101
|
# @return [void]
|
102
|
-
def each
|
103
|
-
@bindings.each
|
104
|
-
yield b
|
105
|
-
end
|
102
|
+
def each(&block)
|
103
|
+
@bindings.each(&block)
|
106
104
|
end
|
107
105
|
|
108
106
|
# @return [Boolean]
|
@@ -144,75 +142,74 @@ module PacketGen
|
|
144
142
|
klass.class_eval { @known_headers = {} }
|
145
143
|
end
|
146
144
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
# Given value may be a lambda, whose alone argument is the value extracted
|
153
|
-
# from header field (or +nil+ when lambda is used to set field while adding
|
154
|
-
# a header).
|
155
|
-
#
|
156
|
-
# Special key +procs+ may be used to set 2 lambdas, the former to set
|
157
|
-
# fields, the latter to check bindings. This may be used when multiple and
|
158
|
-
# non-trivial checks should be made.
|
159
|
-
# @return [void]
|
160
|
-
# @example Basic examples
|
161
|
-
# # Bind Header2 to Header1 when field1 from Header1 has a value of 42
|
162
|
-
# Header1.bind Header2, field1: 42
|
163
|
-
# # Bind Header3 to Header1 when field1 from Header1 has a value of 43
|
164
|
-
# # and field2 has value 43 or 44
|
165
|
-
# Header1.bind Header3, field1: 43, field2: 43
|
166
|
-
# Header1.bind Header3, field1: 43, field2: 44
|
167
|
-
# @example Defining a binding on a field using a lambda.
|
168
|
-
# # Bind Header4 to Header1 when field1 from Header1 has a value
|
169
|
-
# # greater or equal to 44. When adding a Header2 to a Header1
|
170
|
-
# # with Packet#add, force value to 44.
|
171
|
-
# Header1.bind Header4, field1: ->(v) { v.nil? ? 44 : v >= 44 }
|
172
|
-
# @example Defining a binding using procs key
|
173
|
-
# # Bind Header5 to Header1 when field1 from Header1 has a value of 41
|
174
|
-
# # and first two bytes of header1's body are null.
|
175
|
-
# # When adding a Header2 to a Header1 with Packet#add, force value to 44.
|
176
|
-
# Header1.bind Header5, procs: [->(hdr) { hdr.field1 = 41 }
|
177
|
-
# ->(hdr) { hdr.field1 == 41 && hdr.body[0..1] == "\x00\x00" }]
|
178
|
-
# @since 2.7.0
|
179
|
-
def self.bind(header_klass, args={})
|
180
|
-
if @known_headers[header_klass].nil?
|
181
|
-
bindings = Bindings.new
|
182
|
-
@known_headers[header_klass] = bindings
|
183
|
-
else
|
184
|
-
bindings = @known_headers[header_klass]
|
185
|
-
end
|
186
|
-
bindings.new_set
|
187
|
-
args.each do |key, value|
|
188
|
-
bindings << if key == :procs
|
189
|
-
ProcBinding.new(value)
|
190
|
-
else
|
191
|
-
Binding.new(key, value)
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
145
|
+
class <<self
|
146
|
+
# @api private
|
147
|
+
# Get known headers
|
148
|
+
# @return [Hash] keys: header classes, values: hashes
|
149
|
+
attr_reader :known_headers
|
195
150
|
|
151
|
+
# Bind a upper header to current one.
|
152
|
+
# @param [Class] header_klass header class to bind to current class
|
153
|
+
# @param [Hash] args current class fields and their value when +header_klass+
|
154
|
+
# is embedded in current class.
|
155
|
+
#
|
156
|
+
# Given value may be a lambda, whose alone argument is the value extracted
|
157
|
+
# from header field (or +nil+ when lambda is used to set field while adding
|
158
|
+
# a header).
|
159
|
+
#
|
160
|
+
# Special key +procs+ may be used to set 2 lambdas, the former to set
|
161
|
+
# fields, the latter to check bindings. This may be used when multiple and
|
162
|
+
# non-trivial checks should be made.
|
163
|
+
# @return [void]
|
164
|
+
# @example Basic examples
|
165
|
+
# # Bind Header2 to Header1 when field1 from Header1 has a value of 42
|
166
|
+
# Header1.bind Header2, field1: 42
|
167
|
+
# # Bind Header3 to Header1 when field1 from Header1 has a value of 43
|
168
|
+
# # and field2 has value 43 or 44
|
169
|
+
# Header1.bind Header3, field1: 43, field2: 43
|
170
|
+
# Header1.bind Header3, field1: 43, field2: 44
|
171
|
+
# @example Defining a binding on a field using a lambda.
|
172
|
+
# # Bind Header4 to Header1 when field1 from Header1 has a value
|
173
|
+
# # greater or equal to 44. When adding a Header2 to a Header1
|
174
|
+
# # with Packet#add, force value to 44.
|
175
|
+
# Header1.bind Header4, field1: ->(v) { v.nil? ? 44 : v >= 44 }
|
176
|
+
# @example Defining a binding using procs key
|
177
|
+
# # Bind Header5 to Header1 when field1 from Header1 has a value of 41
|
178
|
+
# # and first two bytes of header1's body are null.
|
179
|
+
# # When adding a Header2 to a Header1 with Packet#add, force value to 44.
|
180
|
+
# Header1.bind Header5, procs: [->(hdr) { hdr.field1 = 41 }
|
181
|
+
# ->(hdr) { hdr.field1 == 41 && hdr.body[0..1] == "\x00\x00" }]
|
182
|
+
# @since 2.7.0
|
183
|
+
def bind(header_klass, args={})
|
184
|
+
if @known_headers[header_klass].nil?
|
185
|
+
bindings = Bindings.new
|
186
|
+
@known_headers[header_klass] = bindings
|
187
|
+
else
|
188
|
+
bindings = @known_headers[header_klass]
|
189
|
+
end
|
190
|
+
bindings.new_set
|
191
|
+
args.each do |key, value|
|
192
|
+
bindings << if key == :procs
|
193
|
+
ProcBinding.new(value)
|
194
|
+
else
|
195
|
+
Binding.new(key, value)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
196
199
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
# @api private
|
212
|
-
# Get known headers
|
213
|
-
# @return [Hash] keys: header classes, values: hashes
|
214
|
-
def self.known_headers
|
215
|
-
@known_headers
|
200
|
+
# Helper method to calculate length of +hdr+ and set its +length+ field.
|
201
|
+
# To be used by +#calc_length+ in Base subclasses.
|
202
|
+
# @param [Base] hdr
|
203
|
+
# @param [Boolean] header_in_size if +true+ header is included in length,
|
204
|
+
# if +false+, only +body+ is taken into account
|
205
|
+
def calculate_and_set_length(hdr, header_in_size: true)
|
206
|
+
length = if header_in_size
|
207
|
+
hdr.sz
|
208
|
+
else
|
209
|
+
hdr[:body].sz
|
210
|
+
end
|
211
|
+
hdr.length = length
|
212
|
+
end
|
216
213
|
end
|
217
214
|
|
218
215
|
# @see Types::Fields#initialize
|
@@ -221,7 +218,6 @@ module PacketGen
|
|
221
218
|
super
|
222
219
|
end
|
223
220
|
|
224
|
-
|
225
221
|
# @api private
|
226
222
|
# Get +header+ id in {Packet#headers} array
|
227
223
|
# @param [Header] header
|