summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2011-02-11 08:13:46 +0000
committerelie <elie>2011-02-11 08:13:46 +0000
commitc9a1759aa53404b74bedb6ac58c9d5ac16482633 (patch)
treead199061611bfa8b7aa7070af08b40c06b43601c
parent02d0597150f8e45dd52cba8f4b7c9def506da619 (diff)
downloadpysnmp-c9a1759aa53404b74bedb6ac58c9d5ac16482633.tar.gz
internal cache split off security modules to facilitate code reuse
-rw-r--r--pysnmp/proto/secmod/base.py25
-rw-r--r--pysnmp/proto/secmod/cache.py23
-rw-r--r--pysnmp/proto/secmod/rfc2576.py4
-rw-r--r--pysnmp/proto/secmod/rfc3414/service.py8
4 files changed, 32 insertions, 28 deletions
diff --git a/pysnmp/proto/secmod/base.py b/pysnmp/proto/secmod/base.py
index 70ca456..d4daaac 100644
--- a/pysnmp/proto/secmod/base.py
+++ b/pysnmp/proto/secmod/base.py
@@ -1,10 +1,10 @@
+from pysnmp.proto.secmod import cache
from pysnmp.proto import error
class AbstractSecurityModel:
securityModelID = None
- __stateReference = 0L
def __init__(self):
- self.__cacheEntries = {}
+ self._cache = cache.Cache()
def processIncomingMsg(
self,
@@ -54,27 +54,8 @@ class AbstractSecurityModel:
'Security model %s not implemented' % self
)
- # Caching stuff
-
- def _cachePush(self, **securityData):
- stateReference = AbstractSecurityModel.__stateReference
- AbstractSecurityModel.__stateReference = stateReference + 1
- self.__cacheEntries[stateReference] = securityData
- return stateReference
-
- def _cachePop(self, stateReference):
- if stateReference in self.__cacheEntries:
- securityData = self.__cacheEntries[stateReference]
- else:
- raise error.ProtocolError(
- 'Cache miss for stateReference=%s at %s' %
- (stateReference, self)
- )
- del self.__cacheEntries[stateReference]
- return securityData
-
def releaseStateInformation(self, stateReference):
- self._cachePop(stateReference)
+ self._cache.pop(stateReference)
def receiveTimerTick(self, snmpEngine, timeNow):
pass
diff --git a/pysnmp/proto/secmod/cache.py b/pysnmp/proto/secmod/cache.py
new file mode 100644
index 0000000..89e45ca
--- /dev/null
+++ b/pysnmp/proto/secmod/cache.py
@@ -0,0 +1,23 @@
+from pysnmp import nextid
+from pysnmp.proto import error
+
+class Cache:
+ __stateReference = nextid.Integer(0xffffff)
+ def __init__(self):
+ self.__cacheEntries = {}
+
+ def push(self, **securityData):
+ stateReference = self.__stateReference()
+ self.__cacheEntries[stateReference] = securityData
+ return stateReference
+
+ def pop(self, stateReference):
+ if stateReference in self.__cacheEntries:
+ securityData = self.__cacheEntries[stateReference]
+ else:
+ raise error.ProtocolError(
+ 'Cache miss for stateReference=%s at %s' %
+ (stateReference, self)
+ )
+ del self.__cacheEntries[stateReference]
+ return securityData
diff --git a/pysnmp/proto/secmod/rfc2576.py b/pysnmp/proto/secmod/rfc2576.py
index b751c4a..a14e787 100644
--- a/pysnmp/proto/secmod/rfc2576.py
+++ b/pysnmp/proto/secmod/rfc2576.py
@@ -103,7 +103,7 @@ class SnmpV1SecurityModel(base.AbstractSecurityModel):
# rfc2576: 5.2.2
msg, = globalData
contextEngineId, contextName, pdu = scopedPDU
- cachedSecurityData = self._cachePop(securityStateReference)
+ cachedSecurityData = self._cache.pop(securityStateReference)
communityName = cachedSecurityData['communityName']
debug.logger & debug.flagSM and debug.logger('generateResponseMsg: recovered community %s by securityStateReference %s' % (communityName, securityStateReference))
@@ -238,7 +238,7 @@ class SnmpV1SecurityModel(base.AbstractSecurityModel):
debug.logger & debug.flagSM and debug.logger('processIncomingMsg: looked up securityName %s contextEngineId %s contextName %s by communityName %s' % (securityName, contextEngineId, contextName, communityName))
- stateReference = self._cachePush(
+ stateReference = self._cache.push(
communityName=communityName.syntax
)
diff --git a/pysnmp/proto/secmod/rfc3414/service.py b/pysnmp/proto/secmod/rfc3414/service.py
index df1c25e..82b3b7a 100644
--- a/pysnmp/proto/secmod/rfc3414/service.py
+++ b/pysnmp/proto/secmod/rfc3414/service.py
@@ -197,7 +197,7 @@ class SnmpUSMSecurityModel(AbstractSecurityModel):
# 3.1.1
if securityStateReference is not None:
# 3.1.1a
- cachedSecurityData = self._cachePop(securityStateReference)
+ cachedSecurityData = self._cache.pop(securityStateReference)
usmUserName = cachedSecurityData['msgUserName']
if 'usmUserAuthProtocol' in cachedSecurityData:
usmUserAuthProtocol = cachedSecurityData['usmUserAuthProtocol']
@@ -499,7 +499,7 @@ class SnmpUSMSecurityModel(AbstractSecurityModel):
# 3.2.2
msgAuthoritativeEngineID = securityParameters.getComponentByPosition(0)
- securityStateReference = self._cachePush(
+ securityStateReference = self._cache.push(
msgUserName=securityParameters.getComponentByPosition(3)
)
@@ -612,8 +612,8 @@ class SnmpUSMSecurityModel(AbstractSecurityModel):
debug.logger & debug.flagSM and debug.logger('processIncomingMsg: now have usmUserSecurityName %s usmUserAuthProtocol %s usmUserPrivProtocol %s for msgUserName %s' % (usmUserSecurityName, usmUserAuthProtocol, usmUserPrivProtocol, msgUserName))
# 3.2.11 (moved up here to let Reports be authenticated & encrypted)
- self._cachePop(securityStateReference)
- securityStateReference = self._cachePush(
+ self._cache.pop(securityStateReference)
+ securityStateReference = self._cache.push(
msgUserName=securityParameters.getComponentByPosition(3),
usmUserAuthProtocol=usmUserAuthProtocol,
usmUserAuthKeyLocalized=usmUserAuthKeyLocalized,