1#
2# This file is part of pyasn1-modules software.
3#
4# Copyright (c) 2005-2019, 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#
12from pyasn1.type import constraint
13from pyasn1.type import namedtype
14from pyasn1.type import namedval
15from pyasn1.type import tag
16from pyasn1.type import univ
17
18from pyasn1_modules import rfc1902
19
20max_bindings = rfc1902.Integer(2147483647)
21
22
23class _BindValue(univ.Choice):
24    componentType = namedtype.NamedTypes(
25        namedtype.NamedType('value', rfc1902.ObjectSyntax()),
26        namedtype.NamedType('unSpecified', univ.Null()),
27        namedtype.NamedType('noSuchObject',
28                            univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
29        namedtype.NamedType('noSuchInstance',
30                            univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
31        namedtype.NamedType('endOfMibView',
32                            univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
33    )
34
35
36class VarBind(univ.Sequence):
37    componentType = namedtype.NamedTypes(
38        namedtype.NamedType('name', rfc1902.ObjectName()),
39        namedtype.NamedType('', _BindValue())
40    )
41
42
43class VarBindList(univ.SequenceOf):
44    componentType = VarBind()
45    sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(
46        0, max_bindings
47    )
48
49
50class PDU(univ.Sequence):
51    componentType = namedtype.NamedTypes(
52        namedtype.NamedType('request-id', rfc1902.Integer32()),
53        namedtype.NamedType('error-status', univ.Integer(
54            namedValues=namedval.NamedValues(('noError', 0), ('tooBig', 1), ('noSuchName', 2), ('badValue', 3),
55                                             ('readOnly', 4), ('genErr', 5), ('noAccess', 6), ('wrongType', 7),
56                                             ('wrongLength', 8), ('wrongEncoding', 9), ('wrongValue', 10),
57                                             ('noCreation', 11), ('inconsistentValue', 12), ('resourceUnavailable', 13),
58                                             ('commitFailed', 14), ('undoFailed', 15), ('authorizationError', 16),
59                                             ('notWritable', 17), ('inconsistentName', 18)))),
60        namedtype.NamedType('error-index',
61                            univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, max_bindings))),
62        namedtype.NamedType('variable-bindings', VarBindList())
63    )
64
65
66class BulkPDU(univ.Sequence):
67    componentType = namedtype.NamedTypes(
68        namedtype.NamedType('request-id', rfc1902.Integer32()),
69        namedtype.NamedType('non-repeaters',
70                            univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, max_bindings))),
71        namedtype.NamedType('max-repetitions',
72                            univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, max_bindings))),
73        namedtype.NamedType('variable-bindings', VarBindList())
74    )
75
76
77class GetRequestPDU(PDU):
78    tagSet = PDU.tagSet.tagImplicitly(
79        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)
80    )
81
82
83class GetNextRequestPDU(PDU):
84    tagSet = PDU.tagSet.tagImplicitly(
85        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1)
86    )
87
88
89class ResponsePDU(PDU):
90    tagSet = PDU.tagSet.tagImplicitly(
91        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2)
92    )
93
94
95class SetRequestPDU(PDU):
96    tagSet = PDU.tagSet.tagImplicitly(
97        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3)
98    )
99
100
101class GetBulkRequestPDU(BulkPDU):
102    tagSet = PDU.tagSet.tagImplicitly(
103        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 5)
104    )
105
106
107class InformRequestPDU(PDU):
108    tagSet = PDU.tagSet.tagImplicitly(
109        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 6)
110    )
111
112
113class SNMPv2TrapPDU(PDU):
114    tagSet = PDU.tagSet.tagImplicitly(
115        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7)
116    )
117
118
119class ReportPDU(PDU):
120    tagSet = PDU.tagSet.tagImplicitly(
121        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 8)
122    )
123
124
125class PDUs(univ.Choice):
126    componentType = namedtype.NamedTypes(
127        namedtype.NamedType('get-request', GetRequestPDU()),
128        namedtype.NamedType('get-next-request', GetNextRequestPDU()),
129        namedtype.NamedType('get-bulk-request', GetBulkRequestPDU()),
130        namedtype.NamedType('response', ResponsePDU()),
131        namedtype.NamedType('set-request', SetRequestPDU()),
132        namedtype.NamedType('inform-request', InformRequestPDU()),
133        namedtype.NamedType('snmpV2-trap', SNMPv2TrapPDU()),
134        namedtype.NamedType('report', ReportPDU())
135    )
136