Sha256: 960a6cec80fd8aaefffeed698de80408d586da9991f3d39601592239e7e56dc8

Contents?: true

Size: 1.99 KB

Versions: 8

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

# This file is part of PacketGen
# See https://github.com/sdaubert/packetgen for more informations
# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
# This program is published under MIT license.

module PacketGen
  module Header
    class IP
      # IP address, as a group of 4 bytes
      # @author Sylvain Daubert
      class Addr < Types::Fields
        include Types::Fieldable

        # @!attribute a1
        #  @return [Integer] IP address first byte
        define_field :a1, Types::Int8
        # @!attribute a2
        #  @return [Integer] IP address seconf byte
        define_field :a2, Types::Int8
        # @!attribute a3
        #  @return [Integer] IP address third byte
        define_field :a3, Types::Int8
        # @!attribute a4
        #  @return [Integer] IP address fourth byte
        define_field :a4, Types::Int8

        IPV4_ADDR_REGEX = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.freeze

        # Read a dotted address
        # @param [String] str
        # @return [self]
        def from_human(str)
          return self if str.nil?

          m = str.match(IPV4_ADDR_REGEX)
          if m
            self[:a1].read m[1].to_i
            self[:a2].read m[2].to_i
            self[:a3].read m[3].to_i
            self[:a4].read m[4].to_i
          end
          self
        end

        # Addr in human readable form (dotted format)
        # @return [String]
        def to_human
          fields.map { |f| self[f].to_i.to_s }.join('.')
        end

        # Addr as an integer
        # @return [Integer]
        def to_i
          (self.a1 << 24) | (self.a2 << 16) | (self.a3 << 8) |
            self.a4
        end

        # Return true if this address is a multicast one
        # @return [Boolean]
        def mcast?
          self.a1 >= 224 && self.a1 <= 239
        end

        def ==(other)
          other.is_a?(self.class) &&
            fields.all? { |attr| self[attr].value == other[attr].value }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
packetgen-3.3.1 lib/packetgen/header/ip/addr.rb
packetgen-3.3.0 lib/packetgen/header/ip/addr.rb
packetgen-3.2.2 lib/packetgen/header/ip/addr.rb
packetgen-3.2.1 lib/packetgen/header/ip/addr.rb
packetgen-3.2.0 lib/packetgen/header/ip/addr.rb
packetgen-3.1.8 lib/packetgen/header/ip/addr.rb
packetgen-3.1.7 lib/packetgen/header/ip/addr.rb
packetgen-3.1.6 lib/packetgen/header/ip/addr.rb