packetgen 3.1.3 → 3.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/bin/pgconsole +1 -0
  4. data/lib/packetgen.rb +33 -4
  5. data/lib/packetgen/capture.rb +51 -28
  6. data/lib/packetgen/config.rb +17 -11
  7. data/lib/packetgen/deprecation.rb +33 -7
  8. data/lib/packetgen/header.rb +2 -9
  9. data/lib/packetgen/header/arp.rb +2 -2
  10. data/lib/packetgen/header/asn1_base.rb +2 -2
  11. data/lib/packetgen/header/base.rb +70 -74
  12. data/lib/packetgen/header/bootp.rb +3 -3
  13. data/lib/packetgen/header/dhcp.rb +2 -2
  14. data/lib/packetgen/header/dhcp/option.rb +2 -2
  15. data/lib/packetgen/header/dhcp/options.rb +2 -2
  16. data/lib/packetgen/header/dhcpv6.rb +12 -12
  17. data/lib/packetgen/header/dhcpv6/duid.rb +11 -5
  18. data/lib/packetgen/header/dhcpv6/option.rb +8 -16
  19. data/lib/packetgen/header/dhcpv6/options.rb +2 -2
  20. data/lib/packetgen/header/dhcpv6/relay.rb +2 -2
  21. data/lib/packetgen/header/dns.rb +9 -9
  22. data/lib/packetgen/header/dns/name.rb +20 -9
  23. data/lib/packetgen/header/dns/opt.rb +2 -2
  24. data/lib/packetgen/header/dns/option.rb +2 -2
  25. data/lib/packetgen/header/dns/qdsection.rb +3 -3
  26. data/lib/packetgen/header/dns/question.rb +37 -35
  27. data/lib/packetgen/header/dns/rr.rb +3 -3
  28. data/lib/packetgen/header/dns/rrsection.rb +2 -2
  29. data/lib/packetgen/header/dot11.rb +30 -51
  30. data/lib/packetgen/header/dot11/control.rb +5 -5
  31. data/lib/packetgen/header/dot11/data.rb +11 -7
  32. data/lib/packetgen/header/dot11/element.rb +16 -16
  33. data/lib/packetgen/header/dot11/management.rb +2 -2
  34. data/lib/packetgen/header/dot11/sub_mngt.rb +2 -12
  35. data/lib/packetgen/header/dot1q.rb +2 -2
  36. data/lib/packetgen/header/dot1x.rb +7 -20
  37. data/lib/packetgen/header/eap.rb +30 -33
  38. data/lib/packetgen/header/eap/fast.rb +2 -2
  39. data/lib/packetgen/header/eap/md5.rb +2 -2
  40. data/lib/packetgen/header/eap/tls.rb +2 -2
  41. data/lib/packetgen/header/eap/ttls.rb +2 -2
  42. data/lib/packetgen/header/eth.rb +13 -11
  43. data/lib/packetgen/header/gre.rb +2 -2
  44. data/lib/packetgen/header/http.rb +2 -0
  45. data/lib/packetgen/header/http/headers.rb +6 -4
  46. data/lib/packetgen/header/http/request.rb +36 -21
  47. data/lib/packetgen/header/http/response.rb +7 -7
  48. data/lib/packetgen/header/http/verbs.rb +3 -3
  49. data/lib/packetgen/header/icmp.rb +2 -2
  50. data/lib/packetgen/header/icmpv6.rb +2 -2
  51. data/lib/packetgen/header/igmp.rb +4 -4
  52. data/lib/packetgen/header/igmpv3.rb +3 -3
  53. data/lib/packetgen/header/igmpv3/group_record.rb +8 -6
  54. data/lib/packetgen/header/igmpv3/mq.rb +2 -2
  55. data/lib/packetgen/header/igmpv3/mr.rb +2 -2
  56. data/lib/packetgen/header/ip.rb +30 -31
  57. data/lib/packetgen/header/ip/addr.rb +10 -3
  58. data/lib/packetgen/header/ip/option.rb +8 -10
  59. data/lib/packetgen/header/ip/options.rb +3 -5
  60. data/lib/packetgen/header/ipv6.rb +2 -2
  61. data/lib/packetgen/header/ipv6/addr.rb +9 -2
  62. data/lib/packetgen/header/ipv6/extension.rb +2 -2
  63. data/lib/packetgen/header/ipv6/hop_by_hop.rb +3 -3
  64. data/lib/packetgen/header/llc.rb +2 -2
  65. data/lib/packetgen/header/mdns.rb +2 -2
  66. data/lib/packetgen/header/mld.rb +2 -2
  67. data/lib/packetgen/header/mldv2.rb +2 -2
  68. data/lib/packetgen/header/mldv2/mcast_address_record.rb +4 -2
  69. data/lib/packetgen/header/mldv2/mlq.rb +2 -2
  70. data/lib/packetgen/header/mldv2/mlr.rb +2 -2
  71. data/lib/packetgen/header/ospfv2.rb +9 -9
  72. data/lib/packetgen/header/ospfv2/db_description.rb +2 -2
  73. data/lib/packetgen/header/ospfv2/hello.rb +2 -2
  74. data/lib/packetgen/header/ospfv2/ls_ack.rb +2 -2
  75. data/lib/packetgen/header/ospfv2/ls_request.rb +4 -2
  76. data/lib/packetgen/header/ospfv2/ls_update.rb +2 -2
  77. data/lib/packetgen/header/ospfv2/lsa.rb +9 -5
  78. data/lib/packetgen/header/ospfv2/lsa_header.rb +8 -7
  79. data/lib/packetgen/header/ospfv3.rb +2 -2
  80. data/lib/packetgen/header/ospfv3/db_description.rb +2 -2
  81. data/lib/packetgen/header/ospfv3/hello.rb +2 -2
  82. data/lib/packetgen/header/ospfv3/ipv6_prefix.rb +4 -2
  83. data/lib/packetgen/header/ospfv3/ls_ack.rb +2 -2
  84. data/lib/packetgen/header/ospfv3/ls_request.rb +4 -2
  85. data/lib/packetgen/header/ospfv3/ls_update.rb +2 -2
  86. data/lib/packetgen/header/ospfv3/lsa.rb +5 -5
  87. data/lib/packetgen/header/ospfv3/lsa_header.rb +9 -8
  88. data/lib/packetgen/header/snmp.rb +33 -29
  89. data/lib/packetgen/header/tcp.rb +4 -23
  90. data/lib/packetgen/header/tcp/option.rb +11 -11
  91. data/lib/packetgen/header/tcp/options.rb +2 -2
  92. data/lib/packetgen/header/tftp.rb +6 -6
  93. data/lib/packetgen/header/udp.rb +3 -3
  94. data/lib/packetgen/headerable.rb +5 -4
  95. data/lib/packetgen/inject.rb +23 -0
  96. data/lib/packetgen/inspect.rb +23 -20
  97. data/lib/packetgen/packet.rb +82 -53
  98. data/lib/packetgen/pcap.rb +29 -0
  99. data/lib/packetgen/pcapng.rb +13 -13
  100. data/lib/packetgen/pcapng/block.rb +26 -13
  101. data/lib/packetgen/pcapng/epb.rb +25 -22
  102. data/lib/packetgen/pcapng/file.rb +260 -138
  103. data/lib/packetgen/pcapng/idb.rb +36 -38
  104. data/lib/packetgen/pcapng/shb.rb +51 -53
  105. data/lib/packetgen/pcapng/spb.rb +19 -19
  106. data/lib/packetgen/pcapng/unknown_block.rb +5 -13
  107. data/lib/packetgen/pcaprub_wrapper.rb +81 -0
  108. data/lib/packetgen/proto.rb +2 -2
  109. data/lib/packetgen/types.rb +3 -0
  110. data/lib/packetgen/types/abstract_tlv.rb +27 -7
  111. data/lib/packetgen/types/array.rb +22 -15
  112. data/lib/packetgen/types/cstring.rb +57 -20
  113. data/lib/packetgen/types/enum.rb +7 -2
  114. data/lib/packetgen/types/fieldable.rb +65 -0
  115. data/lib/packetgen/types/fields.rb +182 -117
  116. data/lib/packetgen/types/int.rb +18 -6
  117. data/lib/packetgen/types/int_string.rb +10 -2
  118. data/lib/packetgen/types/length_from.rb +20 -12
  119. data/lib/packetgen/types/oui.rb +4 -2
  120. data/lib/packetgen/types/string.rb +59 -8
  121. data/lib/packetgen/types/tlv.rb +4 -2
  122. data/lib/packetgen/utils.rb +4 -4
  123. data/lib/packetgen/utils/arp_spoofer.rb +2 -2
  124. data/lib/packetgen/version.rb +3 -3
  125. metadata +39 -61
  126. data/.gitignore +0 -13
  127. data/.rubocop.yml +0 -30
  128. data/.travis.yml +0 -19
  129. data/Gemfile +0 -4
  130. data/Rakefile +0 -21
  131. data/packetgen.gemspec +0 -36
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4bbe2b2ffd9d59d7fc57ac668744bd794deafa09b813fb7834588da9a571b64
4
- data.tar.gz: 24ac8e7feb4d0ad95aee6fb52f3ab53e8f0304e35991428e8fafba04002ceeaf
3
+ metadata.gz: 539fce01eed5ce8711896ea7c9f22d29e3878b5165676ff673dd527c748d1fdb
4
+ data.tar.gz: bd724c164c6adf60b772b1ead15f9861853f4723711fe44cfdf49520a16a78c7
5
5
  SHA512:
6
- metadata.gz: 961b3c05d1d18c61c1b55042500a73f136654f612c05a8749e18b2d294517b23dde7f3c8fb0ac0e1fdf9bfc672b6b0535aa64e5f90fa156eb88d1a74a095a358
7
- data.tar.gz: 2d274501fa186766775374ad4c8d24c46a646dd3422d1da6930c96426c3e861f7f7cdd04f8cd5e1308b7237fa45679ad669e4503705121d97e6e9562f2a5c9c9
6
+ metadata.gz: f63e7926b399ee2550fdb29fa688bf4c580ad7b8be2f0518089e94c758d1fb84d70f9e1f148c004d5008697b6dbf83bc32637f994119126ceace6e1ef259c1a0
7
+ data.tar.gz: 8be8c39afec1aee25250b7c8e7f1bbcc81f680356079cca3d179fc671f407d53a4365b6df72c58c9b2947df6e6fe1f80f28b3cb9d31f2dfa445b61aaa3de1693
data/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
 
2
2
  [![Gem Version](https://badge.fury.io/rb/packetgen.svg)](https://badge.fury.io/rb/packetgen)
3
- [![Build Status](https://travis-ci.org/sdaubert/packetgen.svg?branch=master)](https://travis-ci.org/sdaubert/packetgen)
4
3
 
5
4
  # PacketGen
6
5
 
data/bin/pgconsole CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'packetgen'
4
5
  require 'packetgen/config'
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'
@@ -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 nor more a hash
48
- def initialize(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: DEFAULT_SNAPLEN)
49
- @iface = iface || Interfacez.default || Interfacez.loopback
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 nor more a hash
61
- def start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: DEFAULT_SNAPLEN)
62
- set_options iface, max, timeout, filter, promisc, parse, snaplen
63
-
64
- pcap = PCAPRUB::Pcap.open_live(self.iface, @snaplen, @promisc, 1)
65
- set_filter_on pcap
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
- pcap.each do |packet_data|
69
- raw_packets << packet_data
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
- def set_options(iface, max, timeout, filter, promisc, parse, snaplen)
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 set_filter_on(pcap)
116
+ def filter_on(pcap)
105
117
  return if filter.nil? || filter.empty?
106
118
 
107
- pcap.setfilter filter
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
@@ -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 = Interfacez.default || Interfacez.loopback
25
+ @default_iface = PacketGen.default_iface || PacketGen.loopback_iface
26
26
  @hwaddr = {}
27
27
  @ipaddr = {}
28
28
  @ip6addr = {}
29
29
 
30
- Interfacez.all do |iface_name|
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 || @default_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 || @default_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 || @default_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
- separator = klass_method ? '.' : '#'
21
- base_name = klass.to_s + separator
22
- complete_deprecated_method_name = base_name + deprecated_method.to_s
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 << ". It will be remove in PacketGen #{remove_version}."
37
+ message << '. ' << self.removed(remove_version)
29
38
  warn message
30
39
  end
31
40
 
@@ -39,7 +48,24 @@ module PacketGen
39
48
  file, line = caller(2..2).first.split(':')[0, 2]
40
49
  message = +"#{file}:#{line}: #{klass} is deprecated"
41
50
  message << " in favor of #{new_klass}" unless new_klass.nil?
42
- message << ". It will be remove in PacketGen #{remove_version}."
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
@@ -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
@@ -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 do |b|
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
- # Bind a upper header to current one.
148
- # @param [Class] header_klass header class to bind to current class
149
- # @param [Hash] args current class fields and their value when +header_klass+
150
- # is embedded in current class.
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
- # Helper method to calculate length of +hdr+ and set its +length+ field.
198
- # To be used by +#calc_length+ in Base subclasses.
199
- # @param [Base] hdr
200
- # @param [Boolean] header_in_size if +true+ header is included in length,
201
- # if +false+, only +body+ is taken into account
202
- def self.calculate_and_set_length(hdr, header_in_size: true)
203
- length = if header_in_size
204
- hdr.sz
205
- else
206
- hdr[:body].sz
207
- end
208
- hdr.length = length
209
- end
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