pysnmp-sky/pysnmp/entity/rfc3413/ntfrcv.py

109 lines
4.1 KiB
Python
Raw Normal View History

2015-11-20 21:57:28 +01:00
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
2015-11-20 21:57:28 +01:00
# License: http://pysnmp.sf.net/license.html
#
import sys
from pyasn1.compat.octets import null
2005-10-11 21:25:16 +02:00
from pysnmp.proto import rfc3411, error
from pysnmp.proto.api import v1, v2c # backend is always SMIv2 compliant
from pysnmp.proto.proxy import rfc2576
2010-07-14 19:29:38 +02:00
from pysnmp import debug
2005-10-11 21:25:16 +02:00
2016-04-02 23:43:14 +02:00
2005-10-11 21:25:16 +02:00
# 3.4
2016-06-12 14:07:24 +02:00
class NotificationReceiver(object):
pduTypes = (v1.TrapPDU.tagSet, v2c.SNMPv2TrapPDU.tagSet,
v2c.InformRequestPDU.tagSet)
2005-10-11 21:25:16 +02:00
def __init__(self, snmpEngine, cbFun, cbCtx=None):
snmpEngine.msgAndPduDsp.registerContextEngineId(
2016-04-02 23:43:14 +02:00
null, self.pduTypes, self.processPdu # '' is a wildcard
)
self.__cbFunVer = 0
2005-10-11 21:25:16 +02:00
self.__cbFun = cbFun
self.__cbCtx = cbCtx
def close(self, snmpEngine):
snmpEngine.msgAndPduDsp.unregisterContextEngineId(
null, self.pduTypes
)
self.__cbFun = self.__cbCtx = None
2005-10-11 21:25:16 +02:00
def processPdu(self, snmpEngine, messageProcessingModel,
securityModel, securityName, securityLevel,
contextEngineId, contextName, pduVersion, PDU,
maxSizeResponseScopedPDU, stateReference):
2005-10-11 21:25:16 +02:00
# Agent-side API complies with SMIv2
if messageProcessingModel == 0:
2010-09-23 08:20:06 +02:00
origPdu = PDU
2005-10-11 21:25:16 +02:00
PDU = rfc2576.v1ToV2(PDU)
2010-09-23 08:20:06 +02:00
else:
origPdu = None
2005-10-11 21:25:16 +02:00
errorStatus = 'noError'
errorIndex = 0
2005-10-11 21:25:16 +02:00
varBinds = v2c.apiPDU.getVarBinds(PDU)
2010-07-14 19:29:38 +02:00
2016-04-02 23:43:14 +02:00
debug.logger & debug.flagApp and debug.logger(
'processPdu: stateReference %s, varBinds %s' % (stateReference, varBinds))
2015-10-17 11:54:53 +02:00
2005-10-11 21:25:16 +02:00
# 3.4
if PDU.tagSet in rfc3411.confirmedClassPDUs:
2005-10-11 21:25:16 +02:00
# 3.4.1 --> no-op
2015-10-17 11:54:53 +02:00
2005-10-14 18:02:07 +02:00
rspPDU = v2c.apiPDU.getResponse(PDU)
2015-10-17 11:54:53 +02:00
2005-10-11 21:25:16 +02:00
# 3.4.2
v2c.apiPDU.setErrorStatus(rspPDU, errorStatus)
v2c.apiPDU.setErrorIndex(rspPDU, errorIndex)
v2c.apiPDU.setVarBinds(rspPDU, varBinds)
2016-04-02 23:43:14 +02:00
debug.logger & debug.flagApp and debug.logger(
'processPdu: stateReference %s, confirm PDU %s' % (stateReference, rspPDU.prettyPrint()))
2010-07-14 19:29:38 +02:00
2005-10-11 21:25:16 +02:00
# Agent-side API complies with SMIv2
if messageProcessingModel == 0:
2010-09-23 08:20:06 +02:00
rspPDU = rfc2576.v2ToV1(rspPDU, origPdu)
2005-11-04 10:09:35 +01:00
statusInformation = {}
2010-07-14 19:29:38 +02:00
2005-10-11 21:25:16 +02:00
# 3.4.3
try:
snmpEngine.msgAndPduDsp.returnResponsePdu(
snmpEngine, messageProcessingModel, securityModel,
securityName, securityLevel, contextEngineId,
contextName, pduVersion, rspPDU, maxSizeResponseScopedPDU,
stateReference, statusInformation)
except error.StatusInformation:
2016-04-02 23:43:14 +02:00
debug.logger & debug.flagApp and debug.logger(
'processPdu: stateReference %s, statusInformation %s' % (stateReference, sys.exc_info()[1]))
snmpSilentDrops, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB',
'snmpSilentDrops')
snmpSilentDrops.syntax += 1
2005-10-11 21:25:16 +02:00
elif PDU.tagSet in rfc3411.unconfirmedClassPDUs:
2005-10-11 21:25:16 +02:00
pass
else:
raise error.ProtocolError('Unexpected PDU class %s' % PDU.tagSet)
2016-04-02 23:43:14 +02:00
debug.logger & debug.flagApp and debug.logger(
'processPdu: stateReference %s, user cbFun %s, cbCtx %s, varBinds %s' % (
stateReference, self.__cbFun, self.__cbCtx, varBinds))
2010-07-14 19:29:38 +02:00
if self.__cbFunVer:
self.__cbFun(snmpEngine, stateReference, contextEngineId,
contextName, varBinds, self.__cbCtx)
else:
# Compatibility stub (handle legacy cbFun interface)
try:
self.__cbFun(snmpEngine, contextEngineId, contextName,
varBinds, self.__cbCtx)
except TypeError:
self.__cbFunVer = 1
self.__cbFun(snmpEngine, stateReference, contextEngineId,
contextName, varBinds, self.__cbCtx)