Class: Fields::OctetstringField

Inherits:
Field show all
Defined in:
lib/fields.rb

Overview

For IP addresses etc

Instance Attribute Summary

Attributes inherited from Field

#bitstring, #default_value, #desc, #endianness, #length, #length_type, #name, #type

Instance Method Summary collapse

Methods inherited from Field

#parse_buffer, #parse_buffer_lazy, #parse_buffer_strict, #randomize!, #set_raw, #set_value, #to_s

Constructor Details

#initialize(*args) ⇒ OctetstringField

Returns a new instance of OctetstringField.

Raises:

  • (ArgumentError)


161
162
163
164
165
# File 'lib/fields.rb', line 161

def initialize(*args)
    raise ArgumentError, "OctetstringField  (#{args[1]})(CREATE): Length must be a multiple of 8 bits" unless args[2]%8==0
    @length_type="fixed"
    super
end

Instance Method Details

#get_valueObject



184
185
186
# File 'lib/fields.rb', line 184

def get_value
    @bitstring.scan(/.{8}/).map {|e| e.to_i(2).to_s}.join('.')
end

#input_to_bitstring(value) ⇒ Object

Raises:

  • (ArgumentError)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fields.rb', line 167

def input_to_bitstring( value )
    # Expects [0-255][.0-255] .....
    begin
        unless value.split('.').all? {|elem| Integer(elem)<256 && Integer(elem) >= 0}
            raise ArgumentError, "OctetstringField (#{@name})(PARSE): Unable to parse input value"
        end
    rescue
        raise ArgumentError, "OctetstringField (#{@name})(PARSE): Unable to parse input value"
    end
    octets=value.split('.')
    raise ArgumentError, "OctetstringField (#{@name}): Not enough octets?" if (octets.length)*8 < @length
    raise ArgumentError, "OctetstringField (#{@name}): Too many octets?" if (octets.length*8) > @length
    octets.inject("") do |str,num| 
        str << "%.8b"%num
    end
end