/usr/lib/python3/dist-packages/pysnmp/proto/rfc1902.py is in python3-pysnmp4 4.4.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | #
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
from sys import version_info
from pyasn1.type import univ, tag, constraint, namedtype, namedval
from pysnmp.proto import rfc1155, error
__all__ = ['Opaque', 'TimeTicks', 'Bits', 'Integer', 'OctetString',
'IpAddress', 'Counter64', 'Unsigned32', 'Gauge32', 'Integer32',
'ObjectIdentifier', 'Counter32']
class Integer32(univ.Integer):
"""Creates an instance of SNMP Integer32 class.
:py:class:`~pysnmp.proto.rfc1902.Integer32` type represents
integer-valued information between -2147483648 to 2147483647
inclusive (:RFC:`1902#section-7.1.1`). This type is indistinguishable
from the :py:class:`~pysnmp.proto.rfc1902.Integer` type.
The :py:class:`~pysnmp.proto.rfc1902.Integer32` type may be sub-typed
to be more constrained than the base
:py:class:`~pysnmp.proto.rfc1902.Integer32` type.
Parameters
----------
initializer : int
Python integer in range between -2147483648 to 2147483647 inclusive
or :py:class:`~pysnmp.proto.rfc1902.Integer32`.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Integer32(1234)
Integer32(1234)
>>> Integer32(1) > 2
True
>>> Integer32(1) + 1
Integer32(2)
>>> int(Integer32(321))
321
>>> SmallInteger = Integer32.withRange(1,3)
>>> SmallInteger(1)
Integer32(1)
>>> DiscreetInteger = Integer32.withValues(4, 8, 1)
>>> DiscreetInteger(4)
Integer32(4)
>>>
"""
subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
-2147483648, 2147483647
)
@classmethod
def withValues(cls, *values):
"""Creates a subclass with discreet values constraint.
"""
class X(cls):
subtypeSpec = cls.subtypeSpec + constraint.SingleValueConstraint(*values)
X.__name__ = cls.__name__
return X
@classmethod
def withRange(cls, minimum, maximum):
"""Creates a subclass with value range constraint.
"""
class X(cls):
subtypeSpec = cls.subtypeSpec + constraint.ValueRangeConstraint(minimum, maximum)
X.__name__ = cls.__name__
return X
class Integer(Integer32):
"""Creates an instance of SNMP INTEGER class.
The :py:class:`~pysnmp.proto.rfc1902.Integer` type represents
integer-valued information as named-number enumerations
(:RFC:`1902#section-7.1.1`). This type inherits and is indistinguishable
from :py:class:`~pysnmp.proto.rfc1902.Integer32` class.
The :py:class:`~pysnmp.proto.rfc1902.Integer` type may be sub-typed
to be more constrained than the base
:py:class:`~pysnmp.proto.rfc1902.Integer` type.
Parameters
----------
initializer : int
Python integer in range between -2147483648 to 2147483647 inclusive
or :py:class:`~pysnmp.proto.rfc1902.Integer` class instance.
In case of named-numbered enumerations, initialization is also
possible by enumerated literal.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Integer(1234)
Integer(1234)
>>> Integer(1) > 2
True
>>> Integer(1) + 1
Integer(2)
>>> int(Integer(321))
321
>>> SomeState = Integer.withNamedValues(enable=1, disable=0)
>>> SomeState(1)
Integer('enable')
>>> int(SomeState('disable'))
0
>>>
"""
@classmethod
def withNamedValues(cls, **values):
"""Creates a subclass with discreet named values constraint.
"""
class X(cls):
namedValues = cls.namedValues + namedval.NamedValues(*values.items())
subtypeSpec = cls.subtypeSpec + constraint.SingleValueConstraint(*values.values())
X.__name__ = cls.__name__
return X
class OctetString(univ.OctetString):
"""Creates an instance of SNMP OCTET STRING class.
The :py:class:`~pysnmp.proto.rfc1902.OctetString` type represents
arbitrary binary or text data (:RFC:`1902#section-7.1.2`).
It may be sub-typed to be constrained in size.
Parameters
----------
strValue : str
Python string or :py:class:`~pysnmp.proto.rfc1902.OctetString`
class instance.
Other parameters
----------------
hexValue : str
Python string representing octets in a hexadecimal notation
(e.g. DEADBEEF).
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> OctetString('some apples')
OctetString('some apples')
>>> OctetString('some apples') + ' and oranges'
OctetString('some apples and oranges')
>>> str(OctetString('some apples'))
'some apples'
>>> SomeString = OctetString.withSize(3, 12)
>>> str(SomeString(hexValue='deadbeef'))
'\xde\xad\xbe\xef'
>>>
"""
subtypeSpec = univ.OctetString.subtypeSpec + constraint.ValueSizeConstraint(
0, 65535
)
# rfc1902 uses a notion of "fixed length string" what might mean
# having zero-range size constraint applied. The following is
# supposed to be used for setting and querying this property.
fixedLength = None
def setFixedLength(self, value):
self.fixedLength = value
return self
def isFixedLength(self):
return self.fixedLength is not None
def getFixedLength(self):
return self.fixedLength
def clone(self, *args, **kwargs):
return univ.OctetString.clone(self, *args, **kwargs).setFixedLength(self.getFixedLength())
def subtype(self, *args, **kwargs):
return univ.OctetString.subtype(self, *args, **kwargs).setFixedLength(self.getFixedLength())
@classmethod
def withSize(cls, minimum, maximum):
"""Creates a subclass with value size constraint.
"""
class X(cls):
subtypeSpec = cls.subtypeSpec + constraint.ValueSizeConstraint(minimum, maximum)
X.__name__ = cls.__name__
return X
class ObjectIdentifier(univ.ObjectIdentifier):
"""Creates an instance of SNMP OBJECT IDENTIFIER class.
The :py:class:`~pysnmp.proto.rfc1902.ObjectIdentifier` type represents
administratively assigned names (:RFC:`1902#section-7.1.3`).
Supports sequence protocol where elements are integer sub-identifiers.
Parameters
----------
initializer: tuple, str
Python tuple of up to 128 integers in range between 0 to 4294967295
inclusive or Python string containing OID in "dotted" form or
:py:class:`~pysnmp.proto.rfc1902.ObjectIdentifier`.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> ObjectIdentifier((1, 3, 6))
ObjectIdentifier('1.3.6')
>>> ObjectIdentifier('1.3.6')
ObjectIdentifier('1.3.6')
>>> tuple(ObjectIdentifier('1.3.6'))
(1, 3, 6)
>>> str(ObjectIdentifier('1.3.6'))
'1.3.6'
>>>
"""
class IpAddress(OctetString):
"""Creates an instance of SNMP IpAddress class.
The :py:class:`~pysnmp.proto.rfc1902.IpAddress` class represents
a 32-bit internet address as an OCTET STRING of length 4, in network
byte-order (:RFC:`1902#section-7.1.5`).
Parameters
----------
strValue : str
The same as :py:class:`~pysnmp.proto.rfc1902.OctetString`,
additionally IPv4 address in dotted notation ('127.0.0.1').
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> IpAddress('127.0.0.1')
IpAddress(hexValue='7f000001')
>>> str(IpAddress(hexValue='7f000001'))
'\x7f\x00\x00\x01'
>>> IpAddress('\x7f\x00\x00\x01')
IpAddress(hexValue='7f000001')
>>>
"""
tagSet = OctetString.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x00)
)
subtypeSpec = OctetString.subtypeSpec + constraint.ValueSizeConstraint(
4, 4
)
fixedLength = 4
def prettyIn(self, value):
if isinstance(value, str) and len(value) != 4:
try:
value = [int(x) for x in value.split('.')]
except:
raise error.ProtocolError('Bad IP address syntax %s' % value)
value = OctetString.prettyIn(self, value)
if len(value) != 4:
raise error.ProtocolError('Bad IP address syntax')
return value
def prettyOut(self, value):
if value:
return '.'.join(
['%d' % x for x in self.__class__(value).asNumbers()]
)
else:
return ''
class Counter32(univ.Integer):
"""Creates an instance of SNMP Counter32 class.
:py:class:`~pysnmp.proto.rfc1902.Counter32` type represents
a non-negative integer which monotonically increases until it
reaches a maximum value of 4294967295, when it wraps around and
starts increasing again from zero (:RFC:`1902#section-7.1.6`).
Parameters
----------
initializer : int
Python integer in range between 0 to 4294967295 inclusive
or any :py:class:`~pysnmp.proto.rfc1902.Integer`-based class.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Counter32(1234)
Counter32(1234)
>>> Counter32(1) + 1
Counter32(2)
>>> int(Counter32(321))
321
>>>
"""
tagSet = univ.Integer.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x01)
)
subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
0, 4294967295
)
class Gauge32(univ.Integer):
"""Creates an instance of SNMP Gauge32 class.
:py:class:`~pysnmp.proto.rfc1902.Gauge32` type represents
a non-negative integer, which may increase or decrease, but shall
never exceed a maximum value. The maximum value can not be greater
than 4294967295 (:RFC:`1902#section-7.1.7`).
Parameters
----------
initializer : int
Python integer in range between 0 to 4294967295 inclusive
or any :py:class:`~pysnmp.proto.rfc1902.Integer`-based class.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Gauge32(1234)
Gauge32(1234)
>>> Gauge32(1) + 1
Gauge32(2)
>>> int(Gauge32(321))
321
>>>
"""
tagSet = univ.Integer.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x02)
)
subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
0, 4294967295
)
class Unsigned32(univ.Integer):
"""Creates an instance of SNMP Unsigned32 class.
:py:class:`~pysnmp.proto.rfc1902.Unsigned32` type represents
integer-valued information between 0 and 4294967295
(:RFC:`1902#section-7.1.11`).
Parameters
----------
initializer : int
Python integer in range between 0 to 4294967295 inclusive
or any :py:class:`~pysnmp.proto.rfc1902.Integer`-based class.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Unsigned32(1234)
Unsigned32(1234)
>>> Unsigned32(1) + 1
Unsigned32(2)
>>> int(Unsigned32(321))
321
>>>
"""
tagSet = univ.Integer.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x02)
)
subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
0, 4294967295
)
class TimeTicks(univ.Integer):
"""Creates an instance of SNMP TimeTicks class.
:py:class:`~pysnmp.proto.rfc1902.TimeTicks` type represents
a non-negative integer which represents the time, modulo 4294967296,
in hundredths of a second between two epochs (:RFC:`1902#section-7.1.8`).
Parameters
----------
initializer : int
Python integer in range between 0 to 4294967295 inclusive
or any :py:class:`~pysnmp.proto.rfc1902.Integer`-based class.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> TimeTicks(1234)
TimeTicks(1234)
>>> TimeTicks(1) + 1
TimeTicks(2)
>>> int(TimeTicks(321))
321
>>>
"""
tagSet = univ.Integer.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x03)
)
subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
0, 4294967295
)
class Opaque(univ.OctetString):
"""Creates an instance of SNMP Opaque class.
The :py:class:`~pysnmp.proto.rfc1902.Opaque` type supports the
capability to pass arbitrary ASN.1 syntax. A value is encoded
using the ASN.1 BER into a string of octets. This, in turn, is
encoded as an OCTET STRING, in effect "double-wrapping" the original
ASN.1 value (:RFC:`1902#section-7.1.9`).
Parameters
----------
strValue : str
Python string or :py:class:`~pysnmp.proto.rfc1902.OctetString`-based
class instance.
Other parameters
----------------
hexValue : str
Python string representing octets in a hexadecimal notation
(e.g. DEADBEEF).
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Opaque('some apples')
Opaque('some apples')
>>> Opaque('some apples') + ' and oranges'
Opaque('some apples and oranges')
>>> str(Opaque('some apples'))
'some apples'
>>> str(Opaque(hexValue='deadbeef'))
'\xde\xad\xbe\xef'
>>>
"""
tagSet = univ.OctetString.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x04)
)
class Counter64(univ.Integer):
"""Creates an instance of SNMP Counter64 class.
:py:class:`~pysnmp.proto.rfc1902.Counter64` type represents
a non-negative integer which monotonically increases until it reaches
a maximum value of 18446744073709551615, when it wraps around and starts
increasing again from zero (:RFC:`1902#section-7.1.10`).
Parameters
----------
initializer : int
Python integer in range between 0 to 4294967295 inclusive
or any :py:class:`~pysnmp.proto.rfc1902.Integer`-based class.
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> Counter64(1234)
Counter64(1234)
>>> Counter64(1) + 1
Counter64(2)
>>> int(Counter64(321))
321
>>>
"""
tagSet = univ.Integer.tagSet.tagImplicitly(
tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x06)
)
subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
0, 18446744073709551615
)
class Bits(OctetString):
"""Creates an instance of SNMP BITS class.
The :py:class:`~pysnmp.proto.rfc1902.Bits` type represents
an enumeration of named bits. This collection is assigned non-negative,
contiguous values, starting at zero. Only those named-bits so enumerated
may be present in a value (:RFC:`1902#section-7.1.4`).
The bits are named and identified by their position in the octet string.
Position zero is the high order (or left-most) bit in the first octet of
the string. Position 7 is the low order (or right-most) bit of the first
octet of the string. Position 8 is the high order bit in the second octet
of the string, and so on
(`BITS Pseudotype <https://tools.ietf.org/html/draft-perkins-bits-00>`_).
Parameters
----------
strValue : str, tuple
Sequence of bit names or a Python string (as a raw data) or
:py:class:`~pysnmp.proto.rfc1902.OctetString` class instance.
Other parameters
----------------
hexValue : str
Python string representing octets in a hexadecimal notation
(e.g. DEADBEEF).
Raises
------
PyAsn1Error :
On constraint violation or bad initializer.
Examples
--------
>>> from pysnmp.proto.rfc1902 import *
>>> SomeBits = Bits.withNamedBits(apple=0, orange=1, peach=2)
>>> SomeBits(('apple', 'orange')).prettyPrint()
'apple, orange'
>>> SomeBits(('apple', 'orange'))
Bits(hexValue='c0')
>>> SomeBits('\x80')
Bits(hexValue='80')
>>> SomeBits(hexValue='80')
Bits(hexValue='80')
>>> SomeBits(hexValue='80').prettyPrint()
'apple'
>>>
"""
namedValues = namedval.NamedValues()
def __init__(self, value=univ.noValue, **kwargs):
if 'namedValues' not in kwargs:
kwargs['namedValues'] = self.namedValues
OctetString.__init__(self, value, **kwargs)
def prettyIn(self, bits):
if not isinstance(bits, (tuple, list)):
return OctetString.prettyIn(self, bits) # raw bitstring
octets = []
for bit in bits: # tuple of named bits
v = self.namedValues.getValue(bit)
if v is None:
raise error.ProtocolError('Unknown named bit %s' % bit)
d, m = divmod(v, 8)
if d >= len(octets):
octets.extend([0] * (d - len(octets) + 1))
octets[d] |= 0x01 << (7 - m)
return OctetString.prettyIn(self, octets)
def prettyOut(self, value):
names = []
ints = self.__class__(value).asNumbers()
for i, v in enumerate(ints):
v = ints[i]
j = 7
while j >= 0:
if v & (0x01 << j):
name = self.namedValues.getName(i * 8 + 7 - j)
if name is None:
name = 'UnknownBit-%s' % (i * 8 + 7 - j,)
names.append(name)
j -= 1
return ', '.join([str(x) for x in names])
@classmethod
def withNamedBits(cls, **values):
"""Creates a subclass with discreet named bits constraint.
"""
class X(cls):
namedValues = cls.namedValues + namedval.NamedValues(*values.items())
X.__name__ = cls.__name__
return X
class ObjectName(univ.ObjectIdentifier):
pass
class SimpleSyntax(rfc1155.TypeCoercionHackMixIn, univ.Choice):
componentType = namedtype.NamedTypes(
namedtype.NamedType('integer-value', Integer()),
namedtype.NamedType('string-value', OctetString()),
namedtype.NamedType('objectID-value', univ.ObjectIdentifier())
)
class ApplicationSyntax(rfc1155.TypeCoercionHackMixIn, univ.Choice):
componentType = namedtype.NamedTypes(
namedtype.NamedType('ipAddress-value', IpAddress()),
namedtype.NamedType('counter-value', Counter32()),
namedtype.NamedType('timeticks-value', TimeTicks()),
namedtype.NamedType('arbitrary-value', Opaque()),
namedtype.NamedType('big-counter-value', Counter64()),
# This conflicts with Counter32
# namedtype.NamedType('unsigned-integer-value', Unsigned32()),
namedtype.NamedType('gauge32-value', Gauge32())
) # BITS misplaced?
class ObjectSyntax(univ.Choice):
componentType = namedtype.NamedTypes(
namedtype.NamedType('simple', SimpleSyntax()),
namedtype.NamedType('application-wide', ApplicationSyntax())
)
|