Class: PacketGen::Header::Base Abstract
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::Base
- Defined in:
- lib/packetgen/header/base.rb
Overview
Base class for all header types. Subclasses may define magic methods:
-
#calc_checksum, which computes header checksum, -
#calc_length, which computes header length, -
#reply!, which inverts needed fields to forge a response.
Direct Known Subclasses
ARP, BOOTP, DHCP, DHCPv6, DHCPv6::Relay, DNS, DNS::Question, Dot11, Dot11::SubMngt, Dot1q, Dot1x, EAP, Eth, GRE, HTTP::Request, HTTP::Response, ICMP, IGMP, IGMPv3::MQ, IGMPv3::MR, IP, IPv6, IPv6::Extension, LLC, MLD, MLDv2::MLR, OSPFv2, OSPFv2::DbDescription, OSPFv2::Hello, OSPFv2::LSAck, OSPFv2::LSRequest, OSPFv2::LSUpdate, OSPFv3, OSPFv3::DbDescription, OSPFv3::Hello, OSPFv3::LSAck, OSPFv3::LSRequest, OSPFv3::LSUpdate, PPI, RadioTap, SNAP, TCP, TFTP, UDP
Defined Under Namespace
Classes: Binding, Bindings, ProcBinding
Instance Attribute Summary collapse
-
#packet ⇒ Packet?
Reference on packet which owns this header.
Class Method Summary collapse
-
.bind(header_klass, args = {}) ⇒ void
Bind a upper header to current one.
-
.calculate_and_set_length(hdr, header_in_size: true) ⇒ Object
Helper method to calculate length of
hdrand set itslengthfield. -
.inherited(klass) ⇒ void
On inheritage, create @known_header class variable.
-
.known_headers ⇒ Hash
private
Get known headers.
-
.protocol_name ⇒ String
Give protocol name for this class.
Instance Method Summary collapse
- #added_to_packet(packet) ⇒ void abstract
-
#header_id(header) ⇒ Integer
private
Get
headerid in Packet#headers array. -
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #ip_header(header) ⇒ Header private
-
#ll_header(header) ⇒ Header
private
Get link layer header from given header.
-
#method_name ⇒ String
return header method name.
-
#parse? ⇒ Boolean
abstract
Call by Packet#parse when guessing first header to check if header is correct.
-
#protocol_name ⇒ String
Return header protocol name.
Methods inherited from Types::Fields
#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, fields, #fields, #inspect, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
234 235 236 237 |
# File 'lib/packetgen/header/base.rb', line 234 def initialize(={}) @packet = .delete(:packet) if .key?(:packet) super end |
Instance Attribute Details
#packet ⇒ Packet?
Reference on packet which owns this header
138 139 140 |
# File 'lib/packetgen/header/base.rb', line 138 def packet @packet end |
Class Method Details
.bind(header_klass, args = {}) ⇒ void
This method returns an undefined value.
Bind a upper header to current one.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/packetgen/header/base.rb', line 181 def self.bind(header_klass, args={}) if @known_headers[header_klass].nil? bindings = Bindings.new @known_headers[header_klass] = bindings else bindings = @known_headers[header_klass] end bindings.new_set args.each do |key, value| bindings << if key == :procs ProcBinding.new(value) else Binding.new(key, value) end end end |
.calculate_and_set_length(hdr, header_in_size: true) ⇒ Object
Helper method to calculate length of hdr and set its length field. To be used by #calc_length in Base subclasses.
217 218 219 220 221 222 223 224 |
# File 'lib/packetgen/header/base.rb', line 217 def self.calculate_and_set_length(hdr, header_in_size: true) length = if header_in_size hdr.sz else hdr[:body].sz end hdr.length = length end |
.inherited(klass) ⇒ void
This method returns an undefined value.
On inheritage, create @known_header class variable
144 145 146 147 |
# File 'lib/packetgen/header/base.rb', line 144 def self.inherited(klass) super klass.class_eval { @known_headers = {} } end |
.known_headers ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get known headers
229 230 231 |
# File 'lib/packetgen/header/base.rb', line 229 def self.known_headers @known_headers end |
.protocol_name ⇒ String
Give protocol name for this class
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/packetgen/header/base.rb', line 201 def self.protocol_name return @protocol_name if defined? @protocol_name classname = to_s @protocol_name = if classname.start_with?('PacketGen::Header') classname.sub(/.*Header::/, '') else classname.sub(/.*::/, '') end end |
Instance Method Details
#added_to_packet(packet) ⇒ void
This method is called when a header is added to a packet. This base method does nothing but may be overriden by subclasses.
This method returns an undefined value.
279 |
# File 'lib/packetgen/header/base.rb', line 279 def added_to_packet(packet) end |
#header_id(header) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get header id in Packet#headers array
286 287 288 289 290 291 292 293 |
# File 'lib/packetgen/header/base.rb', line 286 def header_id(header) raise FormatError, "header of type #{header.class} not in a packet" if packet.nil? id = packet.headers.index(header) raise FormatError, "header of type #{header.class} not in packet #{packet}" if id.nil? id end |
#ip_header(header) ⇒ Header
301 302 303 304 305 306 307 |
# File 'lib/packetgen/header/base.rb', line 301 def ip_header(header) hid = header_id(header) iph = packet.headers[0...hid].reverse.find { |h| h.is_a?(IP) || h.is_a?(IPv6) } raise FormatError, 'no IP or IPv6 header in packet' if iph.nil? iph end |
#ll_header(header) ⇒ Header
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get link layer header from given header
315 316 317 318 319 320 321 |
# File 'lib/packetgen/header/base.rb', line 315 def ll_header(header) hid = header_id(header) llh = packet.headers[0...hid].reverse.find { |h| h.is_a?(Eth) || h.is_a?(Dot11) } raise FormatError, 'no link layer header in packet' if llh.nil? llh end |
#method_name ⇒ String
return header method name
249 250 251 252 253 |
# File 'lib/packetgen/header/base.rb', line 249 def method_name return @method_name if defined? @method_name @method_name = protocol_name.downcase.gsub(/::/, '_') end |
#parse? ⇒ Boolean
Should be redefined by subclasses. This method should check invariant fields from header.
Call by Packet#parse when guessing first header to check if header is correct
259 260 261 |
# File 'lib/packetgen/header/base.rb', line 259 def parse? true end |
#protocol_name ⇒ String
Return header protocol name
241 242 243 |
# File 'lib/packetgen/header/base.rb', line 241 def protocol_name self.class.protocol_name end |