Verified Commit a34a242c authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

Replace BER DecodeError with ValueError. Reraise as SNMPError

parent 382c9b1e
......@@ -19,7 +19,7 @@ from noc.core.snmp.get import (get_pdu, getnext_pdu, getbulk_pdu,
parse_get_response, parse_get_response_raw)
from noc.core.snmp.set import set_pdu
from noc.core.snmp.error import (NO_ERROR, NO_SUCH_NAME,
SNMPError, TIMED_OUT, UNREACHABLE)
SNMPError, TIMED_OUT, UNREACHABLE, BER_ERROR)
from noc.core.ioloop.udp import UDPSocket
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)
......@@ -74,10 +74,14 @@ def snmp_get(address, oids, port=161,
sock.settimeout(prev_timeout)
else:
sock.close()
if raw_varbinds:
resp = parse_get_response_raw(data)
else:
resp = parse_get_response(data)
try:
if raw_varbinds:
resp = parse_get_response_raw(data)
else:
resp = parse_get_response(data)
except ValueError:
# Broken response
raise SNMPError(code=BER_ERROR, oid=oids[0])
if resp.error_status == NO_ERROR:
# Success
if oid_map:
......@@ -205,7 +209,10 @@ def snmp_count(address, oid, port=161,
else:
sock.close()
# Parse response
resp = parse_get_response(data)
try:
resp = parse_get_response(data)
except ValueError:
raise SNMPError(code=BER_ERROR, oid=oid)
if resp.error_status == NO_SUCH_NAME:
# NULL result
break
......@@ -294,10 +301,13 @@ def snmp_getnext(address, oid, port=161,
close_socket()
raise SNMPError(code=UNREACHABLE, oid=oid)
# Parse response
if raw_varbinds:
resp = parse_get_response_raw(data)
else:
resp = parse_get_response(data)
try:
if raw_varbinds:
resp = parse_get_response_raw(data)
else:
resp = parse_get_response(data)
except ValueError:
raise SNMPError(code=BER_ERROR, oid=oid)
if resp.error_status == NO_SUCH_NAME:
# NULL result
break
......@@ -359,7 +369,10 @@ def snmp_set(address, varbinds, port=161,
sock.settimeout(None)
else:
sock.close()
resp = parse_get_response(data)
try:
resp = parse_get_response(data)
except ValueError:
raise SNMPError(code=BER_ERROR, oid=varbinds[0][0])
if resp.error_status != NO_ERROR:
oid = None
if resp.error_index and resp.varbinds:
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# ASN.1 BER utitities
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2018 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -13,10 +13,6 @@ import struct
from noc.speedup.ber import parse_tlv_header, parse_p_oid, encode_oid, encode_int
class DecodeError(Exception):
pass
def did(tag_class, is_constructed, tag_id):
"""
Calculate decoder_id as <tag id > | tag_class | constructed
......@@ -51,7 +47,7 @@ class BERDecoder(object):
pt = "implicit " + pt
if tag_class:
pt += " application %d" % tag_class
raise DecodeError(
raise ValueError(
"Cannot find BER decoder for %s class %d (0x%X): %s" % (
pt, tag, tag, value.encode('hex'))
)
......@@ -137,11 +133,11 @@ class BERDecoder(object):
elif f & 0x3f == 0x03: # ISO 6093 NR3 form
return float(msg[1:]) # 0123e456
except ValueError:
raise DecodeError("Invalid REAL representation: %s" % msg[1:])
raise ValueError("Invalid REAL representation: %s" % msg[1:])
elif f & 0x40: # infinitive, 8.5.8
return float("-inf" if f & 0x01 else "inf")
else:
raise DecodeError("Unknown REAL encoding: %s" % f)
raise ValueError("Unknown REAL encoding: %s" % f)
def parse_p_bitstring(self, msg):
unused = ord(msg[0])
......
......@@ -59,6 +59,7 @@ INCONSISTENT_NAME = 18
# Virtual code
TIMED_OUT = -1
UNREACHABLE = -2
BER_ERROR = -3
class SNMPError(Exception):
......
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Syslog server
# SNMP Trap Server
# ---------------------------------------------------------------------
# Copyright (C) 2007-2015 The NOC Project
# Copyright (C) 2007-2018 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
......@@ -13,7 +13,6 @@ import time
from noc.core.ioloop.udpserver import UDPServer
from noc.lib.escape import fm_escape
from noc.core.snmp.trap import decode_trap
from noc.core.snmp.ber import DecodeError
from noc.config import config
......@@ -32,7 +31,7 @@ class TrapServer(UDPServer):
return # Invalid event source
try:
community, varbinds = decode_trap(data)
except DecodeError as e:
except ValueError as e:
self.service.perf_metrics["error", ("type", "decode_failed")] += 1
logger.error("Failed to decode trap: %s", data.encode("hex"))
logger.error("Decoder error: %s", e)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment