summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2011-01-21 17:05:30 +0000
committerelie <elie>2011-01-21 17:05:30 +0000
commitd72ff85dd089b1f931a8b9c6e44a27e473395106 (patch)
tree167f369ff92e98e303ec556f9070ceeb32d907b3
parent5b5dc851819c101a642506d56c49d5e3892d5310 (diff)
downloadpysnmp-d72ff85dd089b1f931a8b9c6e44a27e473395106.tar.gz
make use of ASN.1 scalars for defaults
-rw-r--r--pysnmp/proto/api/v1.py24
-rw-r--r--pysnmp/proto/api/v2c.py17
2 files changed, 25 insertions, 16 deletions
diff --git a/pysnmp/proto/api/v1.py b/pysnmp/proto/api/v1.py
index 2096f33..cdeebcf 100644
--- a/pysnmp/proto/api/v1.py
+++ b/pysnmp/proto/api/v1.py
@@ -41,12 +41,12 @@ apiVarBind = VarBindAPI()
getNextRequestID = nextid.Integer(0xffff)
class PDUAPI:
- _null = univ.Null('')
-
+ _null = Null('')
+ _errorStatus = _errorIndex = Integer(0)
def setDefaults(self, pdu):
pdu.setComponentByPosition(0, getNextRequestID())
- pdu.setComponentByPosition(1, 0)
- pdu.setComponentByPosition(2, 0)
+ pdu.setComponentByPosition(1, self._errorStatus)
+ pdu.setComponentByPosition(2, self._errorIndex)
pdu.setComponentByPosition(3)
def getRequestID(self, pdu): return pdu.getComponentByPosition(0)
@@ -115,12 +115,14 @@ class TrapPDUAPI:
agentAddress = IpAddress(socket.gethostbyname(socket.gethostname()))
except:
agentAddress = IpAddress('0.0.0.0')
+ _entOid = ObjectIdentifier((1,3,6,1,4,1,20408))
+ _zeroInt = univ.Integer(0)
def setDefaults(self, pdu):
- pdu.setComponentByPosition(0, (1,3,6,1,4,1,20408))
+ pdu.setComponentByPosition(0, self._entOid)
pdu.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0, self.agentAddress)
- pdu.setComponentByPosition(2, 0)
- pdu.setComponentByPosition(3, 0)
- pdu.setComponentByPosition(4, 0)
+ pdu.setComponentByPosition(2, self._zeroInt)
+ pdu.setComponentByPosition(3, self._zeroInt)
+ pdu.setComponentByPosition(4, self._zeroInt)
pdu.setComponentByPosition(5)
def getEnterprise(self, pdu): return pdu.getComponentByPosition(0)
@@ -165,9 +167,11 @@ class TrapPDUAPI:
apiTrapPDU = TrapPDUAPI()
class MessageAPI:
+ _verInt = univ.Integer(0)
+ _commStr = univ.OctetString('public')
def setDefaults(self, msg):
- msg.setComponentByPosition(0, 0)
- msg.setComponentByPosition(1, 'public')
+ msg.setComponentByPosition(0, self._verInt)
+ msg.setComponentByPosition(1, self._commStr)
return msg
def getVersion(self, msg): return msg.getComponentByPosition(0)
diff --git a/pysnmp/proto/api/v2c.py b/pysnmp/proto/api/v2c.py
index b258c2d..0bff9a9 100644
--- a/pysnmp/proto/api/v2c.py
+++ b/pysnmp/proto/api/v2c.py
@@ -1,6 +1,6 @@
from pysnmp.proto import rfc1902, rfc1905, error
from pysnmp.proto.api import v1
-from pyasn1.type import univ, namedtype, namedval
+from pyasn1.type import univ, namedtype, namedval, constraint
# Shortcuts to SNMP types
Null = univ.Null
@@ -42,6 +42,7 @@ getNextRequestID = v1.getNextRequestID
apiVarBind = v1.apiVarBind
class PDUAPI(v1.PDUAPI):
+ _errorIndex = univ.Integer(0).subtype(subtypeSpec=constraint.ValueRangeConstraint(0, rfc1905.max_bindings))
def getResponse(self, reqPDU):
rspPDU = ResponsePDU()
self.setDefaults(rspPDU)
@@ -66,9 +67,10 @@ class PDUAPI(v1.PDUAPI):
apiPDU = PDUAPI()
class BulkPDUAPI(PDUAPI):
+ _tenInt = rfc1902.Integer(10)
def setDefaults(self, pdu):
PDUAPI.setDefaults(self, pdu)
- pdu.setComponentByPosition(2, 10)
+ pdu.setComponentByPosition(2, self._tenInt)
def getNonRepeaters(self, pdu): return pdu.getComponentByPosition(1)
def setNonRepeaters(self, pdu, value): pdu.setComponentByPosition(1, value)
@@ -107,21 +109,24 @@ class TrapPDUAPI(v1.PDUAPI):
snmpTrapCommunity = (1,3,6,1,6,3,18,1,4,0)
snmpTrapOID = (1,3,6,1,6,3,1,1,4,1,0)
snmpTrapEnterprise = (1,3,6,1,6,3,1,1,4,3,0)
+ _zeroTime = TimeTicks(0)
+ _genTrap = ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))
def setDefaults(self, pdu):
v1.PDUAPI.setDefaults(self, pdu)
varBinds = [
- ( self.sysUpTime, TimeTicks(0)),
+ ( self.sysUpTime, self._zeroTime),
# generic trap
- ( self.snmpTrapOID, ObjectIdentifier((1,3,6,1,6,3,1,1,5,1)))
+ ( self.snmpTrapOID, self._genTrap)
]
self.setVarBinds(pdu, varBinds)
apiTrapPDU = TrapPDUAPI()
class MessageAPI(v1.MessageAPI):
+ _verInt = univ.Integer(1)
def setDefaults(self, msg):
- msg.setComponentByPosition(0, 1)
- msg.setComponentByPosition(1, 'public')
+ msg.setComponentByPosition(0, self._verInt)
+ msg.setComponentByPosition(1, self._commStr)
return msg
def getResponse(self, reqMsg):