Class: SNMP::IpAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp/varbind.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_data, validate = true) ⇒ IpAddress

Create an IpAddress object. The constructor accepts either a raw four-octet string or a formatted string of integers separated by dots (i.e. “10.1.2.3”). Validation of the format can be disabled by setting the ‘validate’ flag to false.



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/snmp/varbind.rb', line 278

def initialize(value_data, validate=true)
  ip = value_data.to_str
  if (validate)
    if ip.length > 4
      ip = parse_string(ip)
    elsif ip.length != 4
      raise InvalidIpAddress, "Expected 4 octets or formatted string, got #{value_data.inspect}"
    end
  end
  @value = ip
end

Class Method Details

.decode(value_data) ⇒ Object



263
264
265
# File 'lib/snmp/varbind.rb', line 263

def decode(value_data)
  IpAddress.new(value_data, false)
end

Instance Method Details

#==(other) ⇒ Object



312
313
314
315
316
317
318
# File 'lib/snmp/varbind.rb', line 312

def ==(other)
  if other.respond_to? :to_str
    return @value.eql?(other.to_str)
  else
    return false
  end
end

#asn1_typeObject



268
269
270
# File 'lib/snmp/varbind.rb', line 268

def asn1_type
  "IpAddress"
end

#encodeObject



328
329
330
# File 'lib/snmp/varbind.rb', line 328

def encode
  encode_tlv(IpAddress_TAG, @value)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


320
321
322
# File 'lib/snmp/varbind.rb', line 320

def eql?(other)
  self == other
end

#hashObject



324
325
326
# File 'lib/snmp/varbind.rb', line 324

def hash
  @value.hash
end

#to_oidObject



306
307
308
309
310
# File 'lib/snmp/varbind.rb', line 306

def to_oid
  oid = ObjectId.new
  @value.each_byte { |b| oid << b }
  oid
end

#to_sObject

Returns a formatted, dot-separated string representing this IpAddress.



300
301
302
303
304
# File 'lib/snmp/varbind.rb', line 300

def to_s
  octets = []
  @value.each_byte { |b| octets << b.to_s }
  octets.join('.')
end

#to_strObject

Returns a raw four-octet string representing this IpAddress.



293
294
295
# File 'lib/snmp/varbind.rb', line 293

def to_str
  @value.dup
end