lib/packetgen/header/bootp.rb in packetgen-2.6.0 vs lib/packetgen/header/bootp.rb in packetgen-2.7.0

- old
+ new

@@ -5,11 +5,10 @@ # frozen_string_literal: true module PacketGen module Header - # Bootstrap Protocol, {https://tools.ietf.org/html/rfc951 # RFC 951} # # A BOOTP header consists of: # * an operation code field ({#op} of type {Types::Int8Enum}), @@ -38,19 +37,18 @@ # # access to BOOTP header # pkt.bootp # => PacketGen::Header::BOOTP # @author Sylvain Daubert # @since 2.2.0 class BOOTP < Base - UDP_SERVER_PORT = 67 UDP_CLIENT_PORT = 68 - + # DHCP opcodes OPCODES = { 'BOOTREQUEST' => 1, 'BOOTREPLY' => 2 - } + }.freeze # @!attribute op # 8-bit opcode # @return [Integer] define_field :op, Types::Int8Enum, enum: OPCODES @@ -71,17 +69,17 @@ # @!attribute xid # 32-bit Transaction ID # @return [Integer] define_field :xid, Types::Int32 - + # @!attribute secs # 16-bit integer: number of seconds elapsed since client began address # acquisition or renewal process # @return [Integer] define_field :secs, Types::Int16 - + # @!attribute flags # 16-bit flag field # @return [Integer] define_field :flags, Types::Int16 @@ -92,21 +90,21 @@ # @!attribute yiaddr # 'your' (client) IP address # @return [String] define_field :yiaddr, IP::Addr - + # @!attribute siaddr # IP address of next server to use in bootstrap # @return [String] define_field :siaddr, IP::Addr - + # @!attribute giaddr # Relay agent IP address, used in booting via a relay agent # @return [String] define_field :giaddr, IP::Addr - + # @!attribute chaddr # client hardware address # @return [String] define_field :chaddr, Types::String, static_length: 16 @@ -121,33 +119,44 @@ define_field :file, Types::CString, static_length: 128 # @!attribute body # @return [String] define_field :body, Types::String - + # @!attribute b # Broadcast flag, from {#flags} # @return [Boolean] # @!attribute mbz # 15-bit Must Be Zero bits, from {#flags} # @return [Boolean] define_bit_fields_on :flags, :b, :mbz, 15 - + + # @return [String] def inspect str = Inspect.dashed_line(self.class, 2) fields.each do |attr| next if attr == :body next unless is_present?(attr) - if attr == :chaddr and self.hlen == 6 - str << Inspect.inspect_attribute(attr, Eth::MacAddr.new.read(self[:chaddr][0, 6]), 2) - else - str << Inspect.inspect_attribute(attr, self[attr], 2) - end + str << if (attr == :chaddr) && (self.hlen == 6) + Inspect.inspect_attribute(attr, Eth::MacAddr.new.read(self[:chaddr][0, 6]), 2) + else + Inspect.inspect_attribute(attr, self[attr], 2) + end end str end + + # Invert opcode, if known + # @return [self] + def reply! + case self.op + when 1 then self.op = 2 + when 2 then self.op = 1 + end + self + end end - - UDP.bind_header BOOTP, sport: 67, dport: 68 - UDP.bind_header BOOTP, sport: 68, dport: 67 + + UDP.bind BOOTP, sport: 67, dport: 68 + UDP.bind BOOTP, sport: 68, dport: 67 end end