This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
asn1.go
229 lines (198 loc) · 4.74 KB
/
asn1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package snmp
import (
"fmt"
"reflect"
"github.com/PromonLogicalis/asn1"
)
// SNMP error codes.
const (
NoError = 0
TooBig = 1
NoSuchName = 2
BadValue = 3
ReadOnly = 4
GenErr = 5
NoAccess = 6
WrongType = 7
WrongLength = 8
WrongEncoding = 9
WrongValue = 10
NoCreation = 11
InconsistentValue = 12
ResourceUnavailable = 13
CommitFailed = 14
UndoFailed = 15
AuthorizationError = 16
NotWritable = 17
InconsistentName = 18
)
// Message is the top level element of the SNMP protocol.
type Message struct {
Version int
Community string
Pdu interface{} `asn1:"choice:pdu"`
}
// Pdu is a generic type for other Protocol Data Units.
type Pdu struct {
Identifier int
ErrorStatus int
ErrorIndex int
Variables []Variable
}
// BulkPdu is a generic type for other Protocol Data Units.
type BulkPdu struct {
Identifier int
NonRepeaters int
MaxRepetitions int
Variables []Variable
}
// GetRequestPdu is used to request data.
type GetRequestPdu Pdu
// GetNextRequestPdu works similarly to GetRequestPdu, but it's returned the
// value for the next valid Oid.
type GetNextRequestPdu Pdu
// GetResponsePdu is used in responses to SNMP requests.
type GetResponsePdu Pdu
// SetRequestPdu is used to request data to be updated.
type SetRequestPdu Pdu
// V1TrapPdu is used when sending a trap in SNMPv1.
type V1TrapPdu struct {
Enterprise asn1.Oid
AgentAddr IPAddress
GenericTrap int
SpecificTrap int
Timestamp TimeTicks
Variables []Variable
}
// GetBulkRequestPdu is used for bulk requests.
type GetBulkRequestPdu BulkPdu
// InformRequestPdu is used for inform requests.
type InformRequestPdu Pdu
// V2TrapPdu is used when sending a trap in SNMPv2.
type V2TrapPdu Pdu
// Variable represents an entry of the variable bindings
type Variable struct {
Name asn1.Oid
Value interface{} `asn1:"choice:val"`
}
// Types available for Variable.Value
// IPAddress is a IPv4 address.
type IPAddress [4]byte
// String returns a representation of IPAddress in dot notation.
func (ip IPAddress) String() string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
// Counter32 is a counter type.
type Counter32 uint32
// Unsigned32 is an integer type.
type Unsigned32 uint32
// TimeTicks is a type for time.
type TimeTicks uint32
// Opaque is a type for blobs.
type Opaque []byte
// Counter64 is a counter type.
type Counter64 uint64
// Exceptions available for Variable.Value
// NoSuchObject exception.
type NoSuchObject asn1.Null
func (e NoSuchObject) String() string { return "NoSuchObject" }
// NoSuchInstance exception.
type NoSuchInstance asn1.Null
func (e NoSuchInstance) String() string { return "NoSuchInstance" }
// EndOfMibView exception.
type EndOfMibView asn1.Null
func (e EndOfMibView) String() string { return "EndOfMibView" }
// Asn1Context returns a new allocated asn1.Context and registers all the
// choice types necessary for SNMPv1 and SNMPv2.
func Asn1Context() *asn1.Context {
ctx := asn1.NewContext()
ctx.AddChoice("pdu", []asn1.Choice{
{
Type: reflect.TypeOf(GetRequestPdu{}),
Options: "tag:0",
},
{
Type: reflect.TypeOf(GetNextRequestPdu{}),
Options: "tag:1",
},
{
Type: reflect.TypeOf(GetResponsePdu{}),
Options: "tag:2",
},
{
Type: reflect.TypeOf(SetRequestPdu{}),
Options: "tag:3",
},
{
Type: reflect.TypeOf(V1TrapPdu{}),
Options: "tag:4",
},
{
Type: reflect.TypeOf(GetBulkRequestPdu{}),
Options: "tag:5",
},
{
Type: reflect.TypeOf(InformRequestPdu{}),
Options: "tag:6",
},
{
Type: reflect.TypeOf(V2TrapPdu{}),
Options: "tag:7",
},
})
ctx.AddChoice("val", []asn1.Choice{
// Simple syntax
{
Type: reflect.TypeOf(asn1.Null{}),
},
{
Type: reflect.TypeOf(int(0)),
},
{
Type: reflect.TypeOf(""),
},
{
Type: reflect.TypeOf(asn1.Oid{}),
},
// Application wide
{
Type: reflect.TypeOf(IPAddress{}),
Options: "application,tag:0",
},
{
Type: reflect.TypeOf(Counter32(0)),
Options: "application,tag:1",
},
{
Type: reflect.TypeOf(Unsigned32(0)),
Options: "application,tag:2",
},
{
Type: reflect.TypeOf(TimeTicks(0)),
Options: "application,tag:3",
},
{
Type: reflect.TypeOf(Opaque("")),
Options: "application,tag:4",
},
// [APPLICATION 5] does not exist.
{
Type: reflect.TypeOf(Counter64(0)),
Options: "application,tag:6",
},
// Exceptions
{
Type: reflect.TypeOf(NoSuchObject{}),
Options: "tag:0",
},
{
Type: reflect.TypeOf(NoSuchInstance{}),
Options: "tag:1",
},
{
Type: reflect.TypeOf(EndOfMibView{}),
Options: "tag:2",
},
})
return ctx
}