rfc1157.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # SNMPv1 message syntax
  8. #
  9. # ASN.1 source from:
  10. # http://www.ietf.org/rfc/rfc1157.txt
  11. #
  12. # Sample captures from:
  13. # http://wiki.wireshark.org/SampleCaptures/
  14. #
  15. from pyasn1.type import namedtype
  16. from pyasn1.type import namedval
  17. from pyasn1.type import tag
  18. from pyasn1.type import univ
  19. from pyasn1_modules import rfc1155
  20. class Version(univ.Integer):
  21. namedValues = namedval.NamedValues(
  22. ('version-1', 0)
  23. )
  24. defaultValue = 0
  25. class Community(univ.OctetString):
  26. pass
  27. class RequestID(univ.Integer):
  28. pass
  29. class ErrorStatus(univ.Integer):
  30. namedValues = namedval.NamedValues(
  31. ('noError', 0),
  32. ('tooBig', 1),
  33. ('noSuchName', 2),
  34. ('badValue', 3),
  35. ('readOnly', 4),
  36. ('genErr', 5)
  37. )
  38. class ErrorIndex(univ.Integer):
  39. pass
  40. class VarBind(univ.Sequence):
  41. componentType = namedtype.NamedTypes(
  42. namedtype.NamedType('name', rfc1155.ObjectName()),
  43. namedtype.NamedType('value', rfc1155.ObjectSyntax())
  44. )
  45. class VarBindList(univ.SequenceOf):
  46. componentType = VarBind()
  47. class _RequestBase(univ.Sequence):
  48. componentType = namedtype.NamedTypes(
  49. namedtype.NamedType('request-id', RequestID()),
  50. namedtype.NamedType('error-status', ErrorStatus()),
  51. namedtype.NamedType('error-index', ErrorIndex()),
  52. namedtype.NamedType('variable-bindings', VarBindList())
  53. )
  54. class GetRequestPDU(_RequestBase):
  55. tagSet = _RequestBase.tagSet.tagImplicitly(
  56. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)
  57. )
  58. class GetNextRequestPDU(_RequestBase):
  59. tagSet = _RequestBase.tagSet.tagImplicitly(
  60. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1)
  61. )
  62. class GetResponsePDU(_RequestBase):
  63. tagSet = _RequestBase.tagSet.tagImplicitly(
  64. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2)
  65. )
  66. class SetRequestPDU(_RequestBase):
  67. tagSet = _RequestBase.tagSet.tagImplicitly(
  68. tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3)
  69. )
  70. class TrapPDU(univ.Sequence):
  71. componentType = namedtype.NamedTypes(
  72. namedtype.NamedType('enterprise', univ.ObjectIdentifier()),
  73. namedtype.NamedType('agent-addr', rfc1155.NetworkAddress()),
  74. namedtype.NamedType('generic-trap', univ.Integer().clone(
  75. namedValues=namedval.NamedValues(('coldStart', 0), ('warmStart', 1), ('linkDown', 2), ('linkUp', 3),
  76. ('authenticationFailure', 4), ('egpNeighborLoss', 5),
  77. ('enterpriseSpecific', 6)))),
  78. namedtype.NamedType('specific-trap', univ.Integer()),
  79. namedtype.NamedType('time-stamp', rfc1155.TimeTicks()),
  80. namedtype.NamedType('variable-bindings', VarBindList())
  81. )
  82. class Pdus(univ.Choice):
  83. componentType = namedtype.NamedTypes(
  84. namedtype.NamedType('get-request', GetRequestPDU()),
  85. namedtype.NamedType('get-next-request', GetNextRequestPDU()),
  86. namedtype.NamedType('get-response', GetResponsePDU()),
  87. namedtype.NamedType('set-request', SetRequestPDU()),
  88. namedtype.NamedType('trap', TrapPDU())
  89. )
  90. class Message(univ.Sequence):
  91. componentType = namedtype.NamedTypes(
  92. namedtype.NamedType('version', Version()),
  93. namedtype.NamedType('community', Community()),
  94. namedtype.NamedType('data', Pdus())
  95. )