rfc1155.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #
  2. # SNMPv1 message syntax
  3. #
  4. # ASN.1 source from:
  5. # http://www.ietf.org/rfc/rfc1155.txt
  6. #
  7. # Sample captures from:
  8. # http://wiki.wireshark.org/SampleCaptures/
  9. #
  10. from pyasn1.type import univ, namedtype, namedval, tag, constraint
  11. class ObjectName(univ.ObjectIdentifier): pass
  12. class SimpleSyntax(univ.Choice):
  13. componentType = namedtype.NamedTypes(
  14. namedtype.NamedType('number', univ.Integer()),
  15. namedtype.NamedType('string', univ.OctetString()),
  16. namedtype.NamedType('object', univ.ObjectIdentifier()),
  17. namedtype.NamedType('empty', univ.Null())
  18. )
  19. class IpAddress(univ.OctetString):
  20. tagSet = univ.OctetString.tagSet.tagImplicitly(
  21. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0)
  22. )
  23. subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueSizeConstraint(
  24. 4, 4
  25. )
  26. class NetworkAddress(univ.Choice):
  27. componentType = namedtype.NamedTypes(
  28. namedtype.NamedType('internet', IpAddress())
  29. )
  30. class Counter(univ.Integer):
  31. tagSet = univ.Integer.tagSet.tagImplicitly(
  32. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 1)
  33. )
  34. subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
  35. 0, 4294967295
  36. )
  37. class Gauge(univ.Integer):
  38. tagSet = univ.Integer.tagSet.tagImplicitly(
  39. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 2)
  40. )
  41. subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
  42. 0, 4294967295
  43. )
  44. class TimeTicks(univ.Integer):
  45. tagSet = univ.Integer.tagSet.tagImplicitly(
  46. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 3)
  47. )
  48. subtypeSpec = univ.Integer.subtypeSpec + constraint.ValueRangeConstraint(
  49. 0, 4294967295
  50. )
  51. class Opaque(univ.OctetString):
  52. tagSet = univ.OctetString.tagSet.tagImplicitly(
  53. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 4)
  54. )
  55. class ApplicationSyntax(univ.Choice):
  56. componentType = namedtype.NamedTypes(
  57. namedtype.NamedType('address', NetworkAddress()),
  58. namedtype.NamedType('counter', Counter()),
  59. namedtype.NamedType('gauge', Gauge()),
  60. namedtype.NamedType('ticks', TimeTicks()),
  61. namedtype.NamedType('arbitrary', Opaque())
  62. )
  63. class ObjectSyntax(univ.Choice):
  64. componentType = namedtype.NamedTypes(
  65. namedtype.NamedType('simple', SimpleSyntax()),
  66. namedtype.NamedType('application-wide', ApplicationSyntax())
  67. )