diff options
author | elie <elie> | 2011-01-21 17:56:29 +0000 |
---|---|---|
committer | elie <elie> | 2011-01-21 17:56:29 +0000 |
commit | f41b73f5c90ae84ec3108c0718dc85a99d538eec (patch) | |
tree | 44995817a297360e0b7a22311b3cfe9450cabd07 | |
parent | d72ff85dd089b1f931a8b9c6e44a27e473395106 (diff) | |
download | pysnmp-f41b73f5c90ae84ec3108c0718dc85a99d538eec.tar.gz |
* pre-compute some more ASN.1 constants
* skip ASN.1 types verification where possible
-rw-r--r-- | pysnmp/proto/api/v1.py | 28 | ||||
-rw-r--r-- | pysnmp/proto/api/v2c.py | 10 | ||||
-rw-r--r-- | pysnmp/proto/mpmod/rfc2576.py | 12 | ||||
-rw-r--r-- | pysnmp/proto/mpmod/rfc3412.py | 66 | ||||
-rw-r--r-- | pysnmp/proto/secmod/rfc2576.py | 8 | ||||
-rw-r--r-- | pysnmp/proto/secmod/rfc3414/service.py | 40 |
6 files changed, 113 insertions, 51 deletions
diff --git a/pysnmp/proto/api/v1.py b/pysnmp/proto/api/v1.py index cdeebcf..dc42267 100644 --- a/pysnmp/proto/api/v1.py +++ b/pysnmp/proto/api/v1.py @@ -44,9 +44,15 @@ class PDUAPI: _null = Null('') _errorStatus = _errorIndex = Integer(0) def setDefaults(self, pdu): - pdu.setComponentByPosition(0, getNextRequestID()) - pdu.setComponentByPosition(1, self._errorStatus) - pdu.setComponentByPosition(2, self._errorIndex) + pdu.setComponentByPosition( + 0, getNextRequestID(), verifyConstraints=False + ) + pdu.setComponentByPosition( + 1, self._errorStatus, verifyConstraints=False + ) + pdu.setComponentByPosition( + 2, self._errorIndex, verifyConstraints=False + ) pdu.setComponentByPosition(3) def getRequestID(self, pdu): return pdu.getComponentByPosition(0) @@ -118,11 +124,11 @@ class TrapPDUAPI: _entOid = ObjectIdentifier((1,3,6,1,4,1,20408)) _zeroInt = univ.Integer(0) def setDefaults(self, pdu): - pdu.setComponentByPosition(0, self._entOid) - pdu.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0, self.agentAddress) - pdu.setComponentByPosition(2, self._zeroInt) - pdu.setComponentByPosition(3, self._zeroInt) - pdu.setComponentByPosition(4, self._zeroInt) + pdu.setComponentByPosition(0, self._entOid, verifyConstraints=False) + pdu.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0, self.agentAddress, verifyConstraints=False) + pdu.setComponentByPosition(2, self._zeroInt, verifyConstraints=False) + pdu.setComponentByPosition(3, self._zeroInt, verifyConstraints=False) + pdu.setComponentByPosition(4, self._zeroInt, verifyConstraints=False) pdu.setComponentByPosition(5) def getEnterprise(self, pdu): return pdu.getComponentByPosition(0) @@ -170,8 +176,8 @@ class MessageAPI: _verInt = univ.Integer(0) _commStr = univ.OctetString('public') def setDefaults(self, msg): - msg.setComponentByPosition(0, self._verInt) - msg.setComponentByPosition(1, self._commStr) + msg.setComponentByPosition(0, self._verInt, verifyConstraints=False) + msg.setComponentByPosition(1, self._commStr, verifyConstraints=False) return msg def getVersion(self, msg): return msg.getComponentByPosition(0) @@ -182,7 +188,7 @@ class MessageAPI: def getPDU(self, msg): return msg.getComponentByPosition(2).getComponent() def setPDU(self, msg, value): - msg.setComponentByPosition(2).getComponentByPosition(2).setComponentByType(value.getTagSet(), value, 1) + msg.setComponentByPosition(2).getComponentByPosition(2).setComponentByType(value.getTagSet(), value, 1, verifyConstraints=False) def getResponse(self, reqMsg): rspMsg = Message() diff --git a/pysnmp/proto/api/v2c.py b/pysnmp/proto/api/v2c.py index 0bff9a9..235b5bc 100644 --- a/pysnmp/proto/api/v2c.py +++ b/pysnmp/proto/api/v2c.py @@ -55,13 +55,13 @@ class PDUAPI(v1.PDUAPI): def setEndOfMibError(self, pdu, errorIndex): varBindList = self.getVarBindList(pdu) varBindList[errorIndex-1].setComponentByPosition( - 1, rfc1905.endOfMibView + 1, rfc1905.endOfMibView, verifyConstraints=False ) def setNoSuchInstanceError(self, pdu, errorIndex): varBindList = self.getVarBindList(pdu) varBindList[errorIndex-1].setComponentByPosition( - 1, rfc1905.noSuchInstance + 1, rfc1905.noSuchInstance, verifyConstraints=False ) apiPDU = PDUAPI() @@ -70,7 +70,7 @@ class BulkPDUAPI(PDUAPI): _tenInt = rfc1902.Integer(10) def setDefaults(self, pdu): PDUAPI.setDefaults(self, pdu) - pdu.setComponentByPosition(2, self._tenInt) + pdu.setComponentByPosition(2, self._tenInt, verifyConstraints=False) def getNonRepeaters(self, pdu): return pdu.getComponentByPosition(1) def setNonRepeaters(self, pdu, value): pdu.setComponentByPosition(1, value) @@ -125,8 +125,8 @@ apiTrapPDU = TrapPDUAPI() class MessageAPI(v1.MessageAPI): _verInt = univ.Integer(1) def setDefaults(self, msg): - msg.setComponentByPosition(0, self._verInt) - msg.setComponentByPosition(1, self._commStr) + msg.setComponentByPosition(0, self._verInt, verifyConstraints=False) + msg.setComponentByPosition(1, self._commStr, verifyConstraints=False) return msg def getResponse(self, reqMsg): diff --git a/pysnmp/proto/mpmod/rfc2576.py b/pysnmp/proto/mpmod/rfc2576.py index f6922b0..c325827 100644 --- a/pysnmp/proto/mpmod/rfc2576.py +++ b/pysnmp/proto/mpmod/rfc2576.py @@ -13,7 +13,7 @@ from pysnmp import debug # references here goes to RFC3412. class SnmpV1MessageProcessingModel(AbstractMessageProcessingModel): - messageProcessingModelID = 0 # SNMPv1 + messageProcessingModelID = univ.Integer(0) # SNMPv1 snmpMsgSpec = v1.Message # rfc3412: 7.1 def prepareOutgoingMessage( @@ -58,7 +58,9 @@ class SnmpV1MessageProcessingModel(AbstractMessageProcessingModel): msg = self.snmpMsgSpec msg.setComponentByPosition(0, self.messageProcessingModelID) msg.setComponentByPosition(2) - msg.getComponentByPosition(2).setComponentByType(pdu.tagSet, pdu) + msg.getComponentByPosition(2).setComponentByType( + pdu.tagSet, pdu, verifyConstraints=False + ) # rfc3412: 7.1.7 globalData = ( msg, ) @@ -169,7 +171,9 @@ class SnmpV1MessageProcessingModel(AbstractMessageProcessingModel): msg = self._snmpMsgSpec msg.setComponentByPosition(0, messageProcessingModel) msg.setComponentByPosition(2) - msg.getComponentByPosition(2).setComponentByType(pdu.tagSet, pdu) + msg.getComponentByPosition(2).setComponentByType( + pdu.tagSet, pdu, verifyConstraints=False + ) # att: msgId not set back to PDU as it's up to responder app @@ -407,5 +411,5 @@ class SnmpV1MessageProcessingModel(AbstractMessageProcessingModel): ) class SnmpV2cMessageProcessingModel(SnmpV1MessageProcessingModel): - messageProcessingModelID = 1 # SNMPv2c + messageProcessingModelID = univ.Integer(1) # SNMPv2c snmpMsgSpec = v2c.Message diff --git a/pysnmp/proto/mpmod/rfc3412.py b/pysnmp/proto/mpmod/rfc3412.py index 35c0844..eb492b8 100644 --- a/pysnmp/proto/mpmod/rfc3412.py +++ b/pysnmp/proto/mpmod/rfc3412.py @@ -52,9 +52,18 @@ _snmpErrors = { } class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): - messageProcessingModelID = 3 # SNMPv3 + messageProcessingModelID = univ.Integer(3) # SNMPv3 snmpMsgSpec = SNMPv3Message _scopedPDU = ScopedPDU() + _emptyStr = univ.OctetString('') + _msgFlags = { + 0: univ.OctetString('\x00'), + 1: univ.OctetString('\x01'), + 3: univ.OctetString('\x03'), + 4: univ.OctetString('\x04'), + 5: univ.OctetString('\x05'), + 7: univ.OctetString('\x07') + } def __init__(self): AbstractMessageProcessingModel.__init__(self) self.__engineIDs = {} @@ -107,7 +116,7 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): contextEngineId = peerSnmpEngineData['securityEngineID'] # 7.1.5 if not contextName: - contextName = '' + contextName = self._emptyStr debug.logger & debug.flagMP and debug.logger('prepareOutgoingMessage: using contextEngineId %s, contextName %s' % (repr(contextEngineId), contextName)) @@ -117,25 +126,28 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): scopedPDU.setComponentByPosition(1, contextName) scopedPDU.setComponentByPosition(2) scopedPDU.getComponentByPosition(2).setComponentByType( - pdu.tagSet, pdu + pdu.tagSet, pdu, verifyConstraints=False ) # 7.1.7 msg = self.snmpMsgSpec # 7.1.7a - msg.setComponentByPosition(0, 3) # version - + msg.setComponentByPosition( + 0, self.messageProcessingModelID, verifyConstraints=False + ) headerData = msg.setComponentByPosition(1).getComponentByPosition(1) # 7.1.7b - headerData.setComponentByPosition(0, msgID) + headerData.setComponentByPosition(0, msgID, verifyConstraints=False) snmpEngineMaxMessageSize, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineMaxMessageSize') # 7.1.7c # XXX need to coerce MIB value as it has incompatible constraints set - headerData.setComponentByPosition(1, int(snmpEngineMaxMessageSize.syntax)) + headerData.setComponentByPosition( + 1, snmpEngineMaxMessageSize.syntax, verifyConstraints=False + ) # 7.1.7d msgFlags = 0 @@ -153,7 +165,9 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): if pdu.tagSet in rfc3411.confirmedClassPDUs: msgFlags = msgFlags | 0x04 - headerData.setComponentByPosition(2, chr(msgFlags)) + headerData.setComponentByPosition( + 2, self._msgFlags[msgFlags], verifyConstraints=False + ) # 7.1.7e # XXX need to coerce MIB value as it has incompatible constraints set @@ -174,13 +188,17 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): else: if peerSnmpEngineData is None: # Force engineID discovery (rfc3414, 4) - securityEngineID = securityName = '' + securityEngineID = securityName = self._emptyStr securityLevel = 1 # Clear possible auth&priv flags - headerData.setComponentByPosition(2, chr(msgFlags & 0xfc)) + headerData.setComponentByPosition( + 2, self._msgFlags[msgFlags & 0xfc], verifyConstraints=False + ) # XXX scopedPDU = self._scopedPDU - scopedPDU.setComponentByPosition(0, '') + scopedPDU.setComponentByPosition( + 0, self._emptyStr, verifyConstraints=False + ) scopedPDU.setComponentByPosition(1, contextName) scopedPDU.setComponentByPosition(2) @@ -189,7 +207,7 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): pMod.apiPDU.setDefaults(emptyPdu) scopedPDU.getComponentByPosition(2).setComponentByType( - emptyPdu.tagSet, emptyPdu + emptyPdu.tagSet, emptyPdu, verifyConstraints=False ) debug.logger & debug.flagMP and debug.logger('prepareOutgoingMessage: force engineID discovery') else: @@ -328,7 +346,7 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): # 7.1.5 if not contextName: - contextName = '' + contextName = self._emptyStr debug.logger & debug.flagMP and debug.logger('prepareResponseMessage: using contextEngineId %s, contextName %s' % (repr(contextEngineId), contextName)) @@ -338,25 +356,31 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): scopedPDU.setComponentByPosition(1, contextName) scopedPDU.setComponentByPosition(2) scopedPDU.getComponentByPosition(2).setComponentByType( - pdu.tagSet, pdu + pdu.tagSet, pdu, verifyConstraints=False ) # 7.1.7 msg = self.snmpMsgSpec # 7.1.7a - msg.setComponentByPosition(0, 3) # version + msg.setComponentByPosition( + 0, self.messageProcessingModelID, verifyConstraints=False + ) headerData = msg.setComponentByPosition(1).getComponentByPosition(1) # 7.1.7b - headerData.setComponentByPosition(0, msgID) + headerData.setComponentByPosition( + 0, msgID, verifyConstraints=False + ) snmpEngineMaxMessageSize, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineMaxMessageSize') # 7.1.7c # XXX need to coerce MIB value as it has incompatible constraints set - headerData.setComponentByPosition(1, int(snmpEngineMaxMessageSize.syntax)) + headerData.setComponentByPosition( + 1, snmpEngineMaxMessageSize.syntax, verifyConstraints=False + ) # 7.1.7d msgFlags = 0 @@ -374,10 +398,14 @@ class SnmpV3MessageProcessingModel(AbstractMessageProcessingModel): if pdu.tagSet in rfc3411.confirmedClassPDUs: # XXX not needed? msgFlags = msgFlags | 0x04 - headerData.setComponentByPosition(2, chr(msgFlags)) + headerData.setComponentByPosition( + 2, self._msgFlags[msgFlags], verifyConstraints=False + ) # 7.1.7e - headerData.setComponentByPosition(3, securityModel) + headerData.setComponentByPosition( + 3, securityModel, verifyConstraints=False + ) debug.logger & debug.flagMP and debug.logger('prepareResponseMessage: %s' % (msg.prettyPrint(),)) diff --git a/pysnmp/proto/secmod/rfc2576.py b/pysnmp/proto/secmod/rfc2576.py index 3eda9e0..b751c4a 100644 --- a/pysnmp/proto/secmod/rfc2576.py +++ b/pysnmp/proto/secmod/rfc2576.py @@ -74,7 +74,9 @@ class SnmpV1SecurityModel(base.AbstractSecurityModel): msg.setComponentByPosition(1, securityParameters) msg.setComponentByPosition(2) - msg.getComponentByPosition(2).setComponentByType(pdu.tagSet, pdu) + msg.getComponentByPosition(2).setComponentByType( + pdu.tagSet, pdu, verifyConstraints=False + ) debug.logger & debug.flagMP and debug.logger('generateRequestMsg: %s' % (msg.prettyPrint(),)) @@ -108,7 +110,9 @@ class SnmpV1SecurityModel(base.AbstractSecurityModel): msg.setComponentByPosition(1, communityName) msg.setComponentByPosition(2) - msg.getComponentByPosition(2).setComponentByType(pdu.tagSet, pdu) + msg.getComponentByPosition(2).setComponentByType( + pdu.tagSet, pdu, verifyConstraints=False + ) debug.logger & debug.flagMP and debug.logger('generateResponseMsg: %s' % (msg.prettyPrint(),)) diff --git a/pysnmp/proto/secmod/rfc3414/service.py b/pysnmp/proto/secmod/rfc3414/service.py index c052537..df1c25e 100644 --- a/pysnmp/proto/secmod/rfc3414/service.py +++ b/pysnmp/proto/secmod/rfc3414/service.py @@ -279,7 +279,9 @@ class SnmpUSMSecurityModel(AbstractSecurityModel): securityParameters = self._securityParametersSpec scopedPDUData = msg.setComponentByPosition(3).getComponentByPosition(3) - scopedPDUData.setComponentByPosition(0, scopedPDU) + scopedPDUData.setComponentByPosition( + 0, scopedPDU, verifyConstraints=False + ) # 3.1.6a if securityStateReference is None and ( # request type check added @@ -333,8 +335,12 @@ class SnmpUSMSecurityModel(AbstractSecurityModel): dataToEncrypt ) - securityParameters.setComponentByPosition(5, privParameters) - scopedPDUData.setComponentByPosition(1, encryptedData) + securityParameters.setComponentByPosition( + 5, privParameters, verifyConstraints=False + ) + scopedPDUData.setComponentByPosition( + 1, encryptedData, verifyConstraints=False + ) debug.logger & debug.flagSM and debug.logger('__generateRequestOrResponseMsg: scopedPDU ciphered') @@ -345,12 +351,20 @@ class SnmpUSMSecurityModel(AbstractSecurityModel): debug.logger & debug.flagSM and debug.logger('__generateRequestOrResponseMsg: %s' % scopedPDUData.prettyPrint()) # 3.1.5 - securityParameters.setComponentByPosition(0, securityEngineID) - securityParameters.setComponentByPosition(1, snmpEngineBoots) - securityParameters.setComponentByPosition(2, snmpEngineTime) + securityParameters.setComponentByPosition( + 0, securityEngineID, verifyConstraints=False + ) + securityParameters.setComponentByPosition( + 1, snmpEngineBoots, verifyConstraints=False + ) + securityParameters.setComponentByPosition( + 2, snmpEngineTime, verifyConstraints=False + ) # 3.1.7 - securityParameters.setComponentByPosition(3, usmUserName) + securityParameters.setComponentByPosition( + 3, usmUserName, verifyConstraints=False + ) # 3.1.8a if securityLevel == 3 or securityLevel == 2: @@ -368,7 +382,9 @@ class SnmpUSMSecurityModel(AbstractSecurityModel): debug.logger & debug.flagSM and debug.logger('__generateRequestOrResponseMsg: %s' % (securityParameters.prettyPrint(),)) - msg.setComponentByPosition(2, encoder.encode(securityParameters)) + msg.setComponentByPosition( + 2, encoder.encode(securityParameters), verifyConstraints=False + ) debug.logger & debug.flagSM and debug.logger('__generateRequestOrResponseMsg: auth outgoing msg: %s' % msg.prettyPrint()) @@ -379,11 +395,15 @@ class SnmpUSMSecurityModel(AbstractSecurityModel): ) # 3.1.8b else: - securityParameters.setComponentByPosition(4, '') + securityParameters.setComponentByPosition( + 4, '', verifyConstraints=False + ) debug.logger & debug.flagSM and debug.logger('__generateRequestOrResponseMsg: %s' % (securityParameters.prettyPrint(),)) - msg.setComponentByPosition(2, encoder.encode(securityParameters)) + msg.setComponentByPosition( + 2, encoder.encode(securityParameters), verifyConstraints=False + ) debug.logger & debug.flagSM and debug.logger('__generateRequestOrResponseMsg: plain outgoing msg: %s' % msg.prettyPrint()) |