rfc1905.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. # SNMPv2c PDU syntax
  8. #
  9. # ASN.1 source from:
  10. # http://www.ietf.org/rfc/rfc1905.txt
  11. #
  12. from pyasn1.type import constraint
  13. from pyasn1.type import namedtype
  14. from pyasn1.type import namedval
  15. from pyasn1.type import tag
  16. from pyasn1.type import univ
  17. from pyasn1_modules import rfc1902
  18. max_bindings = rfc1902.Integer(2147483647)
  19. class _BindValue(univ.Choice):
  20. componentType = namedtype.NamedTypes(
  21. namedtype.NamedType('value', rfc1902.ObjectSyntax()),
  22. namedtype.NamedType('unSpecified', univ.Null()),
  23. namedtype.NamedType('noSuchObject',
  24. univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  25. namedtype.NamedType('noSuchInstance',
  26. univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  27. namedtype.NamedType('endOfMibView',
  28. univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  29. )
  30. class VarBind(univ.Sequence):
  31. componentType = namedtype.NamedTypes(
  32. namedtype.NamedType('name', rfc1902.ObjectName()),
  33. namedtype.NamedType('', _BindValue())
  34. )
  35. class VarBindList(univ.SequenceOf):
  36. componentType = VarBind()
  37. sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(
  38. 0, max_bindings
  39. )
  40. class PDU(univ.Sequence):
  41. componentType = namedtype.NamedTypes(
  42. namedtype.NamedType('request-id', rfc1902.Integer32()),
  43. namedtype.NamedType('error-status', univ.Integer(
  44. namedValues=namedval.NamedValues(('noError', 0), ('tooBig', 1), ('noSuchName', 2), ('badValue', 3),
  45. ('readOnly', 4), ('genErr', 5), ('noAccess', 6), ('wrongType', 7),
  46. ('wrongLength', 8), ('wrongEncoding', 9), ('wrongValue', 10),
  47. ('noCreation', 11), ('inconsistentValue', 12), ('resourceUnavailable', 13),
  48. ('commitFailed', 14), ('undoFailed', 15), ('authorizationError', 16),
  49. ('notWritable', 17), ('inconsistentName', 18)))),
  50. namedtype.NamedType('error-index',
  51. univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, max_bindings))),
  52. namedtype.NamedType('variable-bindings', VarBindList())
  53. )
  54. class BulkPDU(univ.Sequence):
  55. componentType = namedtype.NamedTypes(
  56. namedtype.NamedType('request-id', rfc1902.Integer32()),
  57. namedtype.NamedType('non-repeaters',
  58. univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, max_bindings))),
  59. namedtype.NamedType('max-repetitions',
  60. univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, max_bindings))),
  61. namedtype.NamedType('variable-bindings', VarBindList())
  62. )
  63. class GetRequestPDU(PDU):
  64. tagSet = PDU.tagSet.tagImplicitly(
  65. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)
  66. )
  67. class GetNextRequestPDU(PDU):
  68. tagSet = PDU.tagSet.tagImplicitly(
  69. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1)
  70. )
  71. class ResponsePDU(PDU):
  72. tagSet = PDU.tagSet.tagImplicitly(
  73. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2)
  74. )
  75. class SetRequestPDU(PDU):
  76. tagSet = PDU.tagSet.tagImplicitly(
  77. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3)
  78. )
  79. class GetBulkRequestPDU(BulkPDU):
  80. tagSet = PDU.tagSet.tagImplicitly(
  81. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 5)
  82. )
  83. class InformRequestPDU(PDU):
  84. tagSet = PDU.tagSet.tagImplicitly(
  85. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 6)
  86. )
  87. class SNMPv2TrapPDU(PDU):
  88. tagSet = PDU.tagSet.tagImplicitly(
  89. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7)
  90. )
  91. class ReportPDU(PDU):
  92. tagSet = PDU.tagSet.tagImplicitly(
  93. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 8)
  94. )
  95. class PDUs(univ.Choice):
  96. componentType = namedtype.NamedTypes(
  97. namedtype.NamedType('get-request', GetRequestPDU()),
  98. namedtype.NamedType('get-next-request', GetNextRequestPDU()),
  99. namedtype.NamedType('get-bulk-request', GetBulkRequestPDU()),
  100. namedtype.NamedType('response', ResponsePDU()),
  101. namedtype.NamedType('set-request', SetRequestPDU()),
  102. namedtype.NamedType('inform-request', InformRequestPDU()),
  103. namedtype.NamedType('snmpV2-trap', SNMPv2TrapPDU()),
  104. namedtype.NamedType('report', ReportPDU())
  105. )