Skip to content

Commit e8fa401

Browse files
committed
Make received MIB objects resolution more forgiving
Previously, MIB resolution errors were ignored (whenever possible) for objects we were sending and receiving. This change tightens outgoing objects MIB compliance (send will fail), but tolerate non quite compliant objects we receive. Also, extend the same policy onto `NotificationOriginator`.
1 parent 9dd4bcc commit e8fa401

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

CHANGES.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Revision 4.4.10, released 2019-07-29
1616

1717
- Rebased MIB importing code onto `importlib` because `imp` is long
1818
deprecated
19-
- MIB objects resolution made more forgiving to errors, added optional
20-
`ignoreErrors` parameter to `ObjectType.resolveWithMib()` to control
21-
that behaviour.
19+
- Received MIB objects resolution made more forgiving to errors, added
20+
optional `ignoreErrors` parameter to `ObjectType.resolveWithMib()` to
21+
control that behaviour.
2222
- Fixed asyncore main loop to respect non-default timer resolution
2323
- Fixed `.setTimerResolution()` behaviour of abstract main loop dispatcher
2424
to update call intervals of the existing periodic dispatcher jobs

pysnmp/hlapi/varbinds.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ def makeVarBinds(self, snmpEngine, varBinds):
3636
else:
3737
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
3838

39-
__varBinds.append(varBind.resolveWithMib(mibViewController))
39+
__varBinds.append(varBind.resolveWithMib(mibViewController, ignoreErrors=False))
4040

4141
return __varBinds
4242

4343
def unmakeVarBinds(self, snmpEngine, varBinds, lookupMib=True):
4444
if lookupMib:
4545
mibViewController = self.getMibViewController(snmpEngine)
46-
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds]
46+
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(
47+
mibViewController) for x in varBinds]
4748

4849
return varBinds
4950

@@ -52,7 +53,8 @@ class NotificationOriginatorVarBinds(AbstractVarBinds):
5253
def makeVarBinds(self, snmpEngine, varBinds):
5354
mibViewController = self.getMibViewController(snmpEngine)
5455
if isinstance(varBinds, NotificationType):
55-
varBinds.resolveWithMib(mibViewController)
56+
varBinds.resolveWithMib(
57+
mibViewController, ignoreErrors=False)
5658
__varBinds = []
5759
for varBind in varBinds:
5860
if isinstance(varBind, ObjectType):
@@ -61,11 +63,13 @@ def makeVarBinds(self, snmpEngine, varBinds):
6163
varBind = ObjectType(*varBind)
6264
else:
6365
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
64-
__varBinds.append(varBind.resolveWithMib(mibViewController))
66+
__varBinds.append(varBind.resolveWithMib(
67+
mibViewController, ignoreErrors=False))
6568
return __varBinds
6669

6770
def unmakeVarBinds(self, snmpEngine, varBinds, lookupMib=False):
6871
if lookupMib:
6972
mibViewController = self.getMibViewController(snmpEngine)
70-
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds]
73+
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(
74+
mibViewController) for x in varBinds]
7175
return varBinds

pysnmp/smi/rfc1902.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,14 +1113,20 @@ def loadMibs(self, *modNames):
11131113
def isFullyResolved(self):
11141114
return self.__state & self.stClean
11151115

1116-
def resolveWithMib(self, mibViewController):
1116+
def resolveWithMib(self, mibViewController, ignoreErrors=True):
11171117
"""Perform MIB variable ID conversion and notification objects expansion.
11181118
11191119
Parameters
11201120
----------
11211121
mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
11221122
class instance representing MIB browsing functionality.
11231123
1124+
Other Parameters
1125+
----------------
1126+
ignoreErrors: :py:class:`bool`
1127+
If `True` (default), ignore MIB object name or value casting
1128+
failures if possible.
1129+
11241130
Returns
11251131
-------
11261132
: :py:class:`~pysnmp.smi.rfc1902.NotificationType`
@@ -1158,7 +1164,7 @@ class instance representing MIB browsing functionality.
11581164

11591165
self.__varBinds.append(
11601166
ObjectType(ObjectIdentity(v2c.apiTrapPDU.snmpTrapOID),
1161-
self.__objectIdentity).resolveWithMib(mibViewController)
1167+
self.__objectIdentity).resolveWithMib(mibViewController, ignoreErrors)
11621168
)
11631169

11641170
SmiNotificationType, = mibViewController.mibBuilder.importSymbols('SNMPv2-SMI', 'NotificationType')
@@ -1170,11 +1176,11 @@ class instance representing MIB browsing functionality.
11701176
if isinstance(mibNode, SmiNotificationType):
11711177
for notificationObject in mibNode.getObjects():
11721178
objectIdentity = ObjectIdentity(*notificationObject + self.__instanceIndex).resolveWithMib(
1173-
mibViewController)
1179+
mibViewController, ignoreErrors)
11741180
self.__varBinds.append(
11751181
ObjectType(objectIdentity,
11761182
self.__objects.get(notificationObject, rfc1905.unSpecified)).resolveWithMib(
1177-
mibViewController)
1183+
mibViewController, ignoreErrors)
11781184
)
11791185
varBindsLocation[objectIdentity] = len(self.__varBinds) - 1
11801186
else:
@@ -1184,7 +1190,7 @@ class instance representing MIB browsing functionality.
11841190
for varBinds in self.__additionalVarBinds:
11851191
if not isinstance(varBinds, ObjectType):
11861192
varBinds = ObjectType(ObjectIdentity(varBinds[0]), varBinds[1])
1187-
varBinds.resolveWithMib(mibViewController)
1193+
varBinds.resolveWithMib(mibViewController, ignoreErrors)
11881194
if varBinds[0] in varBindsLocation:
11891195
self.__varBinds[varBindsLocation[varBinds[0]]] = varBinds
11901196
else:

0 commit comments

Comments
 (0)