Sha256: 3351de89ff4f4f01f593360524b10570917c027bda55ee3cd9e7e811cb5bbad4

Contents?: true

Size: 1.36 KB

Versions: 14

Compression:

Stored size: 1.36 KB

Contents

# coding: utf-8
# 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 Types

    # OUI type, defined as a set of 3 bytes
    #  oui = OUI.new
    #  oui.from_human('00:01:02')
    #  oui.to_human   # => "00:01:02"
    #@author Sylvain Daubert
    class OUI < Types::Fields
      # @attribute b2
      #  @return [Integer] left-most byte
      define_field :b2, Types::Int8
      # @attribute b1
      #  @return [Integer] center byte
      define_field :b1, Types::Int8
      # @attribute b0
      #  @return [Integer] right-most byte
      define_field :b0, Types::Int8

      # Read a human-readable string to populate object
      # @param [String] str
      # @return [OUI] self
      def from_human(str)
        return self if str.nil?
        bytes = str.split(/:/)
        unless bytes.size == 3
          raise ArgumentError, 'not a OUI'
        end
        self[:b2].read(bytes[0].to_i(16))
        self[:b1].read(bytes[1].to_i(16))
        self[:b0].read(bytes[2].to_i(16))
        self
      end

      # Get OUI in human readable form (colon-separated bytes)
      # @return [String]
      def to_human
        fields.map { |m| "#{'%02x' % self[m]}" }.join(':')
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
packetgen-2.4.0 lib/packetgen/types/oui.rb
packetgen-2.3.0 lib/packetgen/types/oui.rb
packetgen-2.2.0 lib/packetgen/types/oui.rb
packetgen-2.1.4 lib/packetgen/types/oui.rb
packetgen-2.1.3 lib/packetgen/types/oui.rb
packetgen-2.1.2 lib/packetgen/types/oui.rb
packetgen-2.1.1 lib/packetgen/types/oui.rb
packetgen-2.1.0 lib/packetgen/types/oui.rb
packetgen-2.0.1 lib/packetgen/types/oui.rb
packetgen-2.0.0 lib/packetgen/types/oui.rb
packetgen-1.4.3 lib/packetgen/types/oui.rb
packetgen-1.4.2 lib/packetgen/types/oui.rb
packetgen-1.4.1 lib/packetgen/types/oui.rb
packetgen-1.4.0 lib/packetgen/types/oui.rb