File: verdec.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (32 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from pyasn1.codec.ber import decoder, eoo
from pyasn1.error import PyAsn1Error
from pyasn1.type import univ
from pysnmp.proto.error import ProtocolError


def decode_message_version(wholeMsg):
    """Decode SNMP version from the message."""
    try:
        seq, wholeMsg = decoder.decode(
            wholeMsg,
            asn1Spec=univ.Sequence(),
            recursiveFlag=False,
            substrateFun=lambda a, b, c: (a, b[:c]),
        )
        ver, wholeMsg = decoder.decode(
            wholeMsg,
            asn1Spec=univ.Integer(),
            recursiveFlag=False,
            substrateFun=lambda a, b, c: (a, b[:c]),
        )
        if eoo.endOfOctets.isSameTypeWith(ver):
            raise ProtocolError("EOO at SNMP version component")
        return ver
    except PyAsn1Error as exc:
        raise ProtocolError("Invalid BER at SNMP version component: %s" % exc)