Object
# File lib/net/snmp.rb, line 125 125: def initialize args={} 126: @version = args[:version] || 0 127: @community = args[:community] || "public" 128: @pdu_type = args[:pdu_type] # leave nil unless specified; there's no reasonable default value. 129: @error_status = args[:error_status] || 0 130: @error_index = args[:error_index] || 0 131: @variables = args[:variables] || [] 132: end
# File lib/net/snmp.rb, line 236 236: def add_variable_binding name, value=nil 237: @variables ||= [] 238: @variables << [name, value] 239: end
# File lib/net/snmp.rb, line 230 230: def community= c 231: @community = c.to_s 232: end
# File lib/net/snmp.rb, line 223 223: def error_status= es 224: unless ErrorStatusCodes.has_key?(es) 225: raise Error.new("unknown error-status: #{es}") 226: end 227: @error_status = es 228: end
# File lib/net/snmp.rb, line 216 216: def pdu_type= t 217: unless PduTypes.include?(t) 218: raise Error.new("unknown pdu-type: #{t}") 219: end 220: @pdu_type = t 221: end
# File lib/net/snmp.rb, line 135 135: def parse ber_object 136: begin 137: parse_ber_object ber_object 138: rescue Error 139: # Pass through any SnmpPdu::Error instances 140: raise $! 141: rescue 142: # Wrap any basic parsing error so it becomes a PDU-format error 143: raise Error.new( "snmp-pdu format error" ) 144: end 145: end
# File lib/net/snmp.rb, line 148 148: def parse_ber_object ber_object 149: send :version=, ber_object[0].to_i 150: send :community=, ber_object[1].to_s 151: 152: data = ber_object[2] 153: case (app_tag = data.ber_identifier & 31) 154: when 0 155: send :pdu_type=, :get_request 156: parse_get_request data 157: when 1 158: send :pdu_type=, :get_next_request 159: # This PDU is identical to get-request except for the type. 160: parse_get_request data 161: when 2 162: send :pdu_type=, :get_response 163: # This PDU is identical to get-request except for the type, 164: # the error_status and error_index values are meaningful, 165: # and the fact that the variable bindings will be non-null. 166: parse_get_response data 167: else 168: raise Error.new( "unknown snmp-pdu type: #{app_tag}" ) 169: end 170: end
# File lib/net/snmp.rb, line 175 175: def parse_get_request data 176: send :request_id=, data[0].to_i 177: # data[1] is error_status, always zero. 178: # data[2] is error_index, always zero. 179: send :error_status=, 0 180: send :error_index=, 0 181: data[3].each {|n,v| 182: # A variable-binding, of which there may be several, 183: # consists of an OID and a BER null. 184: # We're ignoring the null, we might want to verify it instead. 185: unless v.is_a?(Net::BER::BerIdentifiedNull) 186: raise Error.new(" invalid variable-binding in get-request" ) 187: end 188: add_variable_binding n, nil 189: } 190: end
# File lib/net/snmp.rb, line 195 195: def parse_get_response data 196: send :request_id=, data[0].to_i 197: send :error_status=, data[1].to_i 198: send :error_index=, data[2].to_i 199: data[3].each {|n,v| 200: # A variable-binding, of which there may be several, 201: # consists of an OID and a BER null. 202: # We're ignoring the null, we might want to verify it instead. 203: add_variable_binding n, v 204: } 205: end
# File lib/net/snmp.rb, line 252 252: def pdu_to_ber_string 253: case pdu_type 254: when :get_request 255: [ 256: request_id.to_ber, 257: error_status.to_ber, 258: error_index.to_ber, 259: [ 260: @variables.map {|n,v| 261: [n.to_ber_oid, Net::BER::BerIdentifiedNull.new.to_ber].to_ber_sequence 262: } 263: ].to_ber_sequence 264: ].to_ber_contextspecific(0) 265: when :get_next_request 266: [ 267: request_id.to_ber, 268: error_status.to_ber, 269: error_index.to_ber, 270: [ 271: @variables.map {|n,v| 272: [n.to_ber_oid, Net::BER::BerIdentifiedNull.new.to_ber].to_ber_sequence 273: } 274: ].to_ber_sequence 275: ].to_ber_contextspecific(1) 276: when :get_response 277: [ 278: request_id.to_ber, 279: error_status.to_ber, 280: error_index.to_ber, 281: [ 282: @variables.map {|n,v| 283: [n.to_ber_oid, v.to_ber].to_ber_sequence 284: } 285: ].to_ber_sequence 286: ].to_ber_contextspecific(2) 287: else 288: raise Error.new( "unknown pdu-type: #{pdu_type}" ) 289: end 290: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.