class SNMP::IpAddress
Public Class Methods
Source
# File lib/snmp/varbind.rb, line 272 def decode(value_data) IpAddress.new(value_data, false) end
Source
# File lib/snmp/varbind.rb, line 287 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
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.
Public Instance Methods
Source
# File lib/snmp/varbind.rb, line 321 def ==(other) if other.respond_to? :to_str return @value.eql?(other.to_str) else return false end end
Source
# File lib/snmp/varbind.rb, line 337 def encode encode_tlv(BER::IpAddress_TAG, @value) end
Source
# File lib/snmp/varbind.rb, line 315 def to_oid oid = ObjectId.new @value.each_byte { |b| oid << b } oid end
Source
# File lib/snmp/varbind.rb, line 309 def to_s octets = [] @value.each_byte { |b| octets << b.to_s } octets.join('.') end
Returns a formatted, dot-separated string representing this IpAddress
.
Private Instance Methods
Source
# File lib/snmp/varbind.rb, line 342 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 = "".dup 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