### Home Classes Methods

### In Files

- lib/packetfu/protos/ip/header.rb

### Parent

Struct.new(:ip_addr)

### Methods

- ::new
- #o1
- #o2
- #o3
- #o4
- #octets
- #read
- #read_quad
- #to_i
- #to_s
- #to_x

### Included Modules

- StructFu

### Files

- .document
- README.rdoc

### Class/Module Index

Quicksearch

- PacketFu
- PacketFu::ARPHeader
- PacketFu::ARPHeaderMixin
- PacketFu::ARPPacket
- PacketFu::AddrIpv6
- PacketFu::Capture
- PacketFu::Config
- PacketFu::EthHeader
- PacketFu::EthHeaderMixin
- PacketFu::EthMac
- PacketFu::EthNic
- PacketFu::EthOui
- PacketFu::EthPacket
- PacketFu::HSRPHeader
- PacketFu::HSRPHeaderMixin
- PacketFu::HSRPPacket
- PacketFu::ICMPHeader
- PacketFu::ICMPHeaderMixin
- PacketFu::ICMPPacket
- PacketFu::IPHeader
- PacketFu::IPHeaderMixin
- PacketFu::IPPacket
- PacketFu::IPv6Header
- PacketFu::IPv6HeaderMixin
- PacketFu::IPv6Packet
- PacketFu::Inject
- PacketFu::InvalidHeader
- PacketFu::InvalidPacket
- PacketFu::LLDPHeader
- PacketFu::LLDPHeaderMixin
- PacketFu::LLDPPacket
- PacketFu::Octets
- PacketFu::Packet
- PacketFu::PcapFile
- PacketFu::PcapHeader
- PacketFu::PcapPacket
- PacketFu::PcapPackets
- PacketFu::Read
- PacketFu::TCPHeader
- PacketFu::TCPHeaderMixin
- PacketFu::TCPPacket
- PacketFu::TcpEcn
- PacketFu::TcpFlags
- PacketFu::TcpHlen
- PacketFu::TcpOption
- PacketFu::TcpOption::ECHO
- PacketFu::TcpOption::ECHOREPLY
- PacketFu::TcpOption::EOL
- PacketFu::TcpOption::MSS
- PacketFu::TcpOption::NOP
- PacketFu::TcpOption::SACK
- PacketFu::TcpOption::SACKOK
- PacketFu::TcpOption::TS
- PacketFu::TcpOption::WS
- PacketFu::TcpOptions
- PacketFu::TcpReserved
- PacketFu::Timestamp
- PacketFu::UDPHeader
- PacketFu::UDPHeaderMixin
- PacketFu::UDPPacket
- PacketFu::Utils
- PacketFu::Write
- StructFu
- StructFu::Int
- StructFu::Int16
- StructFu::Int16be
- StructFu::Int16le
- StructFu::Int32
- StructFu::Int32be
- StructFu::Int32le
- StructFu::Int8
- StructFu::IntString
- StructFu::String
- Struct

No matching classes.

# PacketFu::Octets

Octets implements the addressing scheme for IP.

#### Header Definition

    Int32 :ip_addr

### Constants

IPV4_RE

### Public Class Methods

new(args={}) click to toggle source

    # File lib/packetfu/protos/ip/header.rb, line 13
    def initialize(args={})
      super(
      Int32.new(args[:ip_addr]))
    end

### Public Instance Methods

o1() click to toggle source

Returns the value for the first octet

    # File lib/packetfu/protos/ip/header.rb, line 76
    def o1
      (self.to_i >> 24) & 0xff
    end

o2() click to toggle source

Returns the value for the second octet

    # File lib/packetfu/protos/ip/header.rb, line 81
    def o2
      (self.to_i >> 16) & 0xff
    end

o3() click to toggle source

Returns the value for the third octet

    # File lib/packetfu/protos/ip/header.rb, line 86
    def o3
      (self.to_i >> 8) & 0xff
    end

o4() click to toggle source

Returns the value for the fourth octet

    # File lib/packetfu/protos/ip/header.rb, line 91
    def o4
      self.to_i & 0xff
    end

octets() click to toggle source

Returns the IP address as 4 octets

    # File lib/packetfu/protos/ip/header.rb, line 65
    def octets
      addr = self.to_i
      [
        ((addr >> 24) & 0xff),
        ((addr >> 16) & 0xff),
        ((addr >> 8) & 0xff),
        (addr & 0xff)
      ]
    end

read(str) click to toggle source

Reads a string to populate the object.

    # File lib/packetfu/protos/ip/header.rb, line 24
    def read(str)
      force_binary(str)
      return self if str.nil?
      self[:ip_addr].read str[0,4]
      self
    end

read_quad(str) click to toggle source

Set the IP Address by reading a dotted-quad address.

    # File lib/packetfu/protos/ip/header.rb, line 44
    def read_quad(str)
      match = IPV4_RE.match(str)
      if match.nil?
        raise ArgumentError.new("str is not a valid IPV4 address")
      end
        a = match[1].to_i
        b = match[2].to_i
        c = match[3].to_i
        d = match[4].to_i
      unless (a >= 0 && a <= 255 &&
              b >= 0 && b <= 255 &&
              c >= 0 && c <= 255 &&
              d >= 0 && d <= 255)
        raise ArgumentError.new("str is not a valid IPV4 address")
      end

      self[:ip_addr].value = (a<<24) + (b<<16) + (c<<8) + d
      self
    end

to_i() click to toggle source

Returns an address in numerical format.

    # File lib/packetfu/protos/ip/header.rb, line 39
    def to_i
      self[:ip_addr].to_i
    end

to_s() click to toggle source

Returns the object in string form.

    # File lib/packetfu/protos/ip/header.rb, line 19
    def to_s
      [self[:ip_addr].to_i].pack("N")
    end

to_x() click to toggle source

Returns an address in dotted-quad format.

    # File lib/packetfu/protos/ip/header.rb, line 32
    def to_x
      # This could be slightly faster if we reproduced the code in
      # 'octets()' and didn't have to map to strings.
      self.octets.map(&:to_s).join('.')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.