1#
2# This file is part of pyasn1-modules software.
3#
4# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
5# License: http://pyasn1.sf.net/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#
15from pyasn1.type import namedtype
16from pyasn1.type import namedval
17from pyasn1.type import tag
18from pyasn1.type import univ
19
20from pyasn1_modules import rfc1155
21
22
23class Version(univ.Integer):
24    namedValues = namedval.NamedValues(
25        ('version-1', 0)
26    )
27    defaultValue = 0
28
29
30class Community(univ.OctetString):
31    pass
32
33
34class RequestID(univ.Integer):
35    pass
36
37
38class ErrorStatus(univ.Integer):
39    namedValues = namedval.NamedValues(
40        ('noError', 0),
41        ('tooBig', 1),
42        ('noSuchName', 2),
43        ('badValue', 3),
44        ('readOnly', 4),
45        ('genErr', 5)
46    )
47
48
49class ErrorIndex(univ.Integer):
50    pass
51
52
53class VarBind(univ.Sequence):
54    componentType = namedtype.NamedTypes(
55        namedtype.NamedType('name', rfc1155.ObjectName()),
56        namedtype.NamedType('value', rfc1155.ObjectSyntax())
57    )
58
59
60class VarBindList(univ.SequenceOf):
61    componentType = VarBind()
62
63
64class _RequestBase(univ.Sequence):
65    componentType = namedtype.NamedTypes(
66        namedtype.NamedType('request-id', RequestID()),
67        namedtype.NamedType('error-status', ErrorStatus()),
68        namedtype.NamedType('error-index', ErrorIndex()),
69        namedtype.NamedType('variable-bindings', VarBindList())
70    )
71
72
73class GetRequestPDU(_RequestBase):
74    tagSet = _RequestBase.tagSet.tagImplicitly(
75        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)
76    )
77
78
79class GetNextRequestPDU(_RequestBase):
80    tagSet = _RequestBase.tagSet.tagImplicitly(
81        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1)
82    )
83
84
85class GetResponsePDU(_RequestBase):
86    tagSet = _RequestBase.tagSet.tagImplicitly(
87        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2)
88    )
89
90
91class SetRequestPDU(_RequestBase):
92    tagSet = _RequestBase.tagSet.tagImplicitly(
93        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3)
94    )
95
96
97class TrapPDU(univ.Sequence):
98    componentType = namedtype.NamedTypes(
99        namedtype.NamedType('enterprise', univ.ObjectIdentifier()),
100        namedtype.NamedType('agent-addr', rfc1155.NetworkAddress()),
101        namedtype.NamedType('generic-trap', univ.Integer().clone(
102            namedValues=namedval.NamedValues(('coldStart', 0), ('warmStart', 1), ('linkDown', 2), ('linkUp', 3),
103                                             ('authenticationFailure', 4), ('egpNeighborLoss', 5),
104                                             ('enterpriseSpecific', 6)))),
105        namedtype.NamedType('specific-trap', univ.Integer()),
106        namedtype.NamedType('time-stamp', rfc1155.TimeTicks()),
107        namedtype.NamedType('variable-bindings', VarBindList())
108    )
109
110
111class Pdus(univ.Choice):
112    componentType = namedtype.NamedTypes(
113        namedtype.NamedType('get-request', GetRequestPDU()),
114        namedtype.NamedType('get-next-request', GetNextRequestPDU()),
115        namedtype.NamedType('get-response', GetResponsePDU()),
116        namedtype.NamedType('set-request', SetRequestPDU()),
117        namedtype.NamedType('trap', TrapPDU())
118    )
119
120
121class Message(univ.Sequence):
122    componentType = namedtype.NamedTypes(
123        namedtype.NamedType('version', Version()),
124        namedtype.NamedType('community', Community()),
125        namedtype.NamedType('data', Pdus())
126    )
127