rfc1902.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #
  2. # SNMPv2c message syntax
  3. #
  4. # ASN.1 source from:
  5. # http://www.ietf.org/rfc/rfc1902.txt
  6. #
  7. from pyasn1.type import univ, namedtype, namedval, tag, constraint
  8. class Integer(univ.Integer):
  9. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  10. -2147483648, 2147483647
  11. )
  12. class Integer32(univ.Integer):
  13. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  14. -2147483648, 2147483647
  15. )
  16. class OctetString(univ.OctetString):
  17. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueSizeConstraint(
  18. 0, 65535
  19. )
  20. class IpAddress(univ.OctetString):
  21. tagSet = univ.OctetString.tagSet.tagImplicitly(
  22. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x00)
  23. )
  24. subtypeSpec = univ.OctetString.subtypeSpec+constraint.ValueSizeConstraint(
  25. 4, 4
  26. )
  27. class Counter32(univ.Integer):
  28. tagSet = univ.Integer.tagSet.tagImplicitly(
  29. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x01)
  30. )
  31. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  32. 0, 4294967295
  33. )
  34. class Gauge32(univ.Integer):
  35. tagSet = univ.Integer.tagSet.tagImplicitly(
  36. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x02)
  37. )
  38. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  39. 0, 4294967295
  40. )
  41. class Unsigned32(univ.Integer):
  42. tagSet = univ.Integer.tagSet.tagImplicitly(
  43. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x02)
  44. )
  45. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  46. 0, 4294967295
  47. )
  48. class TimeTicks(univ.Integer):
  49. tagSet = univ.Integer.tagSet.tagImplicitly(
  50. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x03)
  51. )
  52. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  53. 0, 4294967295
  54. )
  55. class Opaque(univ.OctetString):
  56. tagSet = univ.OctetString.tagSet.tagImplicitly(
  57. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x04)
  58. )
  59. class Counter64(univ.Integer):
  60. tagSet = univ.Integer.tagSet.tagImplicitly(
  61. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0x06)
  62. )
  63. subtypeSpec = univ.Integer.subtypeSpec+constraint.ValueRangeConstraint(
  64. 0, 18446744073709551615
  65. )
  66. class Bits(univ.OctetString): pass
  67. class ObjectName(univ.ObjectIdentifier): pass
  68. class SimpleSyntax(univ.Choice):
  69. componentType = namedtype.NamedTypes(
  70. namedtype.NamedType('integer-value', Integer()),
  71. namedtype.NamedType('string-value', OctetString()),
  72. namedtype.NamedType('objectID-value', univ.ObjectIdentifier())
  73. )
  74. class ApplicationSyntax(univ.Choice):
  75. componentType = namedtype.NamedTypes(
  76. namedtype.NamedType('ipAddress-value', IpAddress()),
  77. namedtype.NamedType('counter-value', Counter32()),
  78. namedtype.NamedType('timeticks-value', TimeTicks()),
  79. namedtype.NamedType('arbitrary-value', Opaque()),
  80. namedtype.NamedType('big-counter-value', Counter64()),
  81. # This conflicts with Counter32
  82. # namedtype.NamedType('unsigned-integer-value', Unsigned32()),
  83. namedtype.NamedType('gauge32-value', Gauge32())
  84. ) # BITS misplaced?
  85. class ObjectSyntax(univ.Choice):
  86. componentType = namedtype.NamedTypes(
  87. namedtype.NamedType('simple', SimpleSyntax()),
  88. namedtype.NamedType('application-wide', ApplicationSyntax())
  89. )