class SNMP::IpAddress
Public Class Methods
decode(value_data)
click to toggle source
# File lib/snmp/varbind.rb, line 249 def decode(value_data) IpAddress.new(value_data, false) end
new(value_data, validate=true)
click to toggle source
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.
# File lib/snmp/varbind.rb, line 264 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
Public Instance Methods
==(other)
click to toggle source
# File lib/snmp/varbind.rb, line 298 def ==(other) if other.respond_to? :to_str return @value.eql?(other.to_str) else return false end end
asn1_type()
click to toggle source
# File lib/snmp/varbind.rb, line 254 def asn1_type "IpAddress" end
encode()
click to toggle source
# File lib/snmp/varbind.rb, line 314 def encode encode_tlv(IpAddress_TAG, @value) end
eql?(other)
click to toggle source
# File lib/snmp/varbind.rb, line 306 def eql?(other) self == other end
hash()
click to toggle source
# File lib/snmp/varbind.rb, line 310 def hash @value.hash end
to_oid()
click to toggle source
# File lib/snmp/varbind.rb, line 292 def to_oid oid = ObjectId.new @value.each_byte { |b| oid << b } oid end
to_s()
click to toggle source
Returns a formatted, dot-separated string representing this IpAddress.
# File lib/snmp/varbind.rb, line 286 def to_s octets = [] @value.each_byte { |b| octets << b.to_s } octets.join('.') end
to_str()
click to toggle source
Returns a raw four-octet string representing this IpAddress.
# File lib/snmp/varbind.rb, line 279 def to_str @value.dup end
Private Instance Methods
parse_string(ip_string)
click to toggle source
# File lib/snmp/varbind.rb, line 319 def parse_string(ip_string) parts = ip_string.split(".") if parts.length != 4 raise InvalidIpAddress, "Expected four octets separated by dots, not #{ip_string.inspect}" end value_data = "" parts.each do |s| octet = s.to_i raise InvalidIpAddress, "Octets cannot be greater than 255: #{ip_string.inspect}" if octet > 255 raise InvalidIpAddress, "Octets cannot be negative: #{ip_string.inspect}" if octet < 0 value_data << octet.chr end value_data end