forked from k-sone/snmpgo
/
pdu.go
626 lines (543 loc) · 18 KB
/
pdu.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package snmpclient2
import (
"fmt"
"sort"
"strings"
"github.com/runner-mei/snmpclient2/asn1"
)
// The protocol data unit of SNMP
// The PduV1 is used by SNMP V1 and V2c, other than the SNMP V1 Trap
//
// trap pdu
// +------------+--------------+------------+----------------+---------------+-------------+---------------------+
// | PDU Type | enterprise | agent addr | generic trap | specific trap | time stamp | variable bindings |
// +------------+--------------+------------+----------------+---------------+-------------+---------------------+
//
// reponse pdu
// +------------+--------------+----------------+---------------+----------------------+
// | PDU Type | request id | error status | error index | variable bindings |
// +------------+--------------+----------------+---------------+----------------------+
//
// request pdu
// +------------+--------------+----------------+---------------+----------------------+
// | PDU Type | request id | 0 | 0 | variable bindings |
// +------------+--------------+----------------+---------------+----------------------+
//
// +-------------------------------------------------------------------------------------------------------+
// snmpv1 message | version | community | PDU |
// +-------------------------------------------------------------------------------------------------------+
//
// snmpv2 message same as v1
//
// |<------------ ScopedPdu ---------->|
// +--------------------------------------------------------------------------------------------------------+
// snmpv3 message | version | RequestID | MaxSize | Flag | security | Security | context | context | PDU |
// | | | | | Model | parameters | engineId | name | |
// +--------------------------------------------------------------------------------------------------------+
//
// +-----------------------------------+
// ScopedPdu | context | context | PDU |
// | engineId | name | |
// +-----------------------------------+
//
//
// snmpv1 get/next/set PDU
// +-------------------------------------------------------------------------------------------------------+
// | PDU type | RequestID | 0 | 0 | variable bindings |
// +-------------------------------------------------------------------------------------------------------+
//
// snmpv1 response PDU
// +-------------------------------------------------------------------------------------------------------+
// | PDU type | RequestID | error status | error index | variable bindings |
// +-------------------------------------------------------------------------------------------------------+
//
// snmpv1 trap PDU
// +-------------------------------------------------------------------------------------------------------+
// | PDU type | enterprise | agent addr | generic trap | specific trap | timestamp | variable bindings |
// +-------------------------------------------------------------------------------------------------------+
//
// snmpv2 trap PDU |<---------------- variable bindings ------------------>|
// +-------------------------------------------------------------------------------------------------------+
// | PDU type | RequestID | 0 | 0 | sysUptime.0 | value | snmpTrap oid.0 | value2 | ... |
// +-------------------------------------------------------------------------------------------------------+
//
//
// variable bindings
// +-------------------------------------------------------------------------------------------------------+
// | name1 | value1 | name2 | value2 | ... | namen | valuen |
// +-------------------------------------------------------------------------------------------------------+
type VariableBinding struct {
Oid Oid
Variable Variable
}
func (v *VariableBinding) Marshal() (b []byte, err error) {
var buf []byte
raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}
if v.Variable == nil {
return asn1.Marshal(raw)
}
buf, err = v.Oid.Marshal()
if err != nil {
return
}
raw.Bytes = buf
buf, err = v.Variable.Marshal()
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
return asn1.Marshal(raw)
}
func (v *VariableBinding) Unmarshal(b []byte) (rest []byte, err error) {
var raw asn1.RawValue
rest, err = asn1.Unmarshal(b, &raw)
if err != nil {
return nil, err
}
if raw.Class != asn1.ClassUniversal || raw.Tag != asn1.TagSequence || !raw.IsCompound {
return nil, asn1.StructuralError{fmt.Sprintf(
"Invalid VariableBinding object - Class [%02x], Tag [%02x] : [%s]",
raw.Class, raw.Tag, ToHexStr(b, " "))}
}
var oid Oid
next, err := (&oid).Unmarshal(raw.Bytes)
if err != nil {
return
}
variable, _, err := unmarshalVariable(next)
if err != nil {
return
}
v.Oid = oid
v.Variable = variable
return
}
func (v *VariableBinding) String() string {
var vtype, value string
if v.Variable != nil {
vtype = ToSyntexString(v.Variable.Syntex())
value = escape(v.Variable.ToString())
}
return fmt.Sprintf(`{"Oid": "%s", "Variable": {"Type": "%s", "Value": %s}}`,
v.Oid.ToString(), vtype, value)
}
func NewVarBind(oid Oid, val Variable) VariableBinding {
return VariableBinding{
Oid: oid,
Variable: val,
}
}
type VariableBindings []VariableBinding
// Gets a VariableBinding that matches
func (v VariableBindings) MatchOid(oid Oid) *VariableBinding {
for _, o := range v {
if o.Oid.Equal(&oid) {
return &o
}
}
return nil
}
// Gets a VariableBinding list that matches the prefix
func (v VariableBindings) MatchBaseOids(prefix Oid) VariableBindings {
if 0 == len(prefix.Value) {
return VariableBindings{}
}
result := make(VariableBindings, 0)
for _, o := range v {
if o.Oid.Contains(&prefix) {
result = append(result, o)
}
}
return result
}
// Sort a VariableBinding list by OID
func (v VariableBindings) Sort() VariableBindings {
c := make(VariableBindings, len(v))
copy(c, v)
sort.Sort(sortableVarBinds{c})
return c
}
func (v VariableBindings) uniq(comp func(a, b VariableBinding) bool) VariableBindings {
var before VariableBinding
c := make(VariableBindings, 0, len(v))
for _, val := range v {
if !comp(before, val) {
before = val
c = append(c, val)
}
}
return c
}
// Filter out adjacent VariableBinding list
func (v VariableBindings) Uniq() VariableBindings {
return v.uniq(func(a, b VariableBinding) bool {
return b.Oid.Equal(&a.Oid)
})
}
func (v VariableBindings) String() string {
VariableBindings := make([]string, len(v))
for i, o := range v {
VariableBindings[i] = o.String()
}
return "[" + strings.Join(VariableBindings, ", ") + "]"
}
type sortableVarBinds struct {
VariableBindings
}
func (v sortableVarBinds) Len() int {
return len(v.VariableBindings)
}
func (v sortableVarBinds) Swap(i, j int) {
v.VariableBindings[i], v.VariableBindings[j] = v.VariableBindings[j], v.VariableBindings[i]
}
func (v sortableVarBinds) Less(i, j int) bool {
return v.VariableBindings[i].Oid.Compare(&v.VariableBindings[j].Oid) < 1
}
// The protocol data unit of SNMP
// The PduV1 is used by SNMP V1 and V2c, other than the SNMP V1 Trap
//
// trap pdu
// +------------+--------------+------------+----------------+---------------+-------------+---------------------+
// | PDU Type | enterprise | agent addr | generic trap | specific trap | time stamp | variable bindings |
// +------------+--------------+------------+----------------+---------------+-------------+---------------------+
//
// reponse pdu
// +------------+--------------+----------------+---------------+----------------------+
// | PDU Type | request id | error status | error index | variable bindings |
// +------------+--------------+----------------+---------------+----------------------+
//
// request pdu
// +------------+--------------+----------------+---------------+----------------------+
// | PDU Type | request id | 0 | 0 | variable bindings |
// +------------+--------------+----------------+---------------+----------------------+
type PDU interface {
PduType() PduType
RequestId() int
SetRequestId(int)
ErrorStatus() ErrorStatus
SetErrorStatus(ErrorStatus)
ErrorIndex() int
SetErrorIndex(int)
SetNonrepeaters(int)
SetMaxRepetitions(int)
AppendVariableBinding(Oid, Variable)
VariableBindings() VariableBindings
Marshal() ([]byte, error)
Unmarshal([]byte) (rest []byte, err error)
String() string
}
// The PduV1 is used by SNMP V1 and V2c, other than the SNMP V1 Trap
type PduV1 struct {
pduType PduType
requestId int
errorStatus ErrorStatus
errorIndex int
variableBindings VariableBindings
Enterprise Oid
AgentAddress Ipaddress
GenericTrap int
SpecificTrap int
Timestamp int
}
func (pdu *PduV1) PduType() PduType {
return pdu.pduType
}
func (pdu *PduV1) RequestId() int {
return pdu.requestId
}
func (pdu *PduV1) SetRequestId(i int) {
pdu.requestId = i
}
func (pdu *PduV1) ErrorStatus() ErrorStatus {
return pdu.errorStatus
}
func (pdu *PduV1) SetErrorStatus(i ErrorStatus) {
pdu.errorStatus = i
}
func (pdu *PduV1) ErrorIndex() int {
return pdu.errorIndex
}
func (pdu *PduV1) SetErrorIndex(i int) {
pdu.errorIndex = i
}
func (pdu *PduV1) SetNonrepeaters(i int) {
pdu.errorStatus = ErrorStatus(i)
}
func (pdu *PduV1) SetMaxRepetitions(i int) {
pdu.errorIndex = i
}
func (pdu *PduV1) AppendVariableBinding(oid Oid, variable Variable) {
if nil == variable {
variable = NewNull()
}
pdu.variableBindings = append(pdu.variableBindings, VariableBinding{
Oid: oid,
Variable: variable,
})
}
func (pdu *PduV1) VariableBindings() VariableBindings {
return pdu.variableBindings
}
func (pdu *PduV1) Marshal() (b []byte, err error) {
var buf []byte
raw := asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: int(pdu.pduType), IsCompound: true}
if Trap == pdu.pduType {
buf, err = pdu.Enterprise.Marshal()
if err != nil {
return
}
raw.Bytes = buf
buf, err = pdu.AgentAddress.Marshal()
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
buf, err = asn1.Marshal(pdu.GenericTrap)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
buf, err = asn1.Marshal(pdu.SpecificTrap)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
buf, err = asn1.Marshal(pdu.Timestamp)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
} else {
buf, err = asn1.Marshal(pdu.requestId)
if err != nil {
return
}
raw.Bytes = buf
buf, err = asn1.Marshal(pdu.errorStatus)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
buf, err = asn1.Marshal(pdu.errorIndex)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
}
VariableBindings := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}
for i := 0; i < len(pdu.variableBindings); i++ {
buf, err = pdu.variableBindings[i].Marshal()
if err != nil {
return
}
VariableBindings.Bytes = append(VariableBindings.Bytes, buf...)
}
buf, err = asn1.Marshal(VariableBindings)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
return asn1.Marshal(raw)
}
func (pdu *PduV1) Unmarshal(b []byte) (rest []byte, err error) {
var raw asn1.RawValue
rest, err = asn1.Unmarshal(b, &raw)
if err != nil {
return
}
if raw.Class != asn1.ClassContextSpecific || !raw.IsCompound {
return nil, asn1.StructuralError{fmt.Sprintf(
"Invalid PDU object - Class [%02x], Tag [%02x] : [%s]",
raw.Class, raw.Tag, ToHexStr(b, " "))}
}
var requestId int
var errorStatus int
var errorIndex int
var enterprise Oid
var agentAddress Ipaddress
var genericTrap int
var specificTrap int
var timestamp int
next := raw.Bytes
// The protocol data unit of SNMP
// The PduV1 is used by SNMP V1 and V2c, other than the SNMP V1 Trap
//
// trap pdu
// +------------+--------------+------------+----------------+---------------+-------------+---------------------+
// | PDU Type | enterprise | agent addr | generic trap | specific trap | time stamp | variable bindings |
// +------------+--------------+------------+----------------+---------------+-------------+---------------------+
//
// reponse pdu
// +------------+--------------+----------------+---------------+----------------------+
// | PDU Type | request id | error status | error index | variable bindings |
// +------------+--------------+----------------+---------------+----------------------+
//
// request pdu
// +------------+--------------+----------------+---------------+----------------------+
// | PDU Type | request id | 0 | 0 | variable bindings |
// +------------+--------------+----------------+---------------+----------------------+
//
if Trap == PduType(raw.Tag) {
next, err = enterprise.Unmarshal(next)
if err != nil {
return
}
next, err = agentAddress.Unmarshal(next)
if err != nil {
return
}
next, err = asn1.Unmarshal(next, &genericTrap)
if err != nil {
return
}
next, err = asn1.Unmarshal(next, &specificTrap)
if err != nil {
return
}
var t asn1.RawValue
next, err = asn1.Unmarshal(next, &t)
if err != nil {
return
}
} else {
next, err = asn1.Unmarshal(next, &requestId)
if err != nil {
return
}
next, err = asn1.Unmarshal(next, &errorStatus)
if err != nil {
return
}
next, err = asn1.Unmarshal(next, &errorIndex)
if err != nil {
return
}
}
var VariableBindings asn1.RawValue
_, err = asn1.Unmarshal(next, &VariableBindings)
if err != nil {
return
}
if VariableBindings.Class != asn1.ClassUniversal || VariableBindings.Tag != asn1.TagSequence || !VariableBindings.IsCompound {
return nil, asn1.StructuralError{fmt.Sprintf(
"Invalid VariableBindings object - Class [%02x], Tag [%02x] : [%s]",
VariableBindings.Class, VariableBindings.Tag, ToHexStr(next, " "))}
}
next = VariableBindings.Bytes
for len(next) > 0 {
var variableBinding VariableBinding
next, err = variableBinding.Unmarshal(next)
if err != nil {
return
}
pdu.variableBindings = append(pdu.variableBindings, variableBinding)
}
pdu.pduType = PduType(raw.Tag)
pdu.requestId = requestId
pdu.errorStatus = ErrorStatus(errorStatus)
pdu.errorIndex = errorIndex
pdu.Enterprise = enterprise
pdu.AgentAddress = agentAddress
pdu.GenericTrap = genericTrap
pdu.SpecificTrap = specificTrap
pdu.Timestamp = timestamp
return
}
func (pdu *PduV1) String() string {
return fmt.Sprintf(
`{"Type": "%s", "RequestId": "%d", "ErrorStatus": "%s", `+
`"ErrorIndex": "%d", "VariableBindings": %s}`,
pdu.pduType, pdu.requestId, pdu.errorStatus, pdu.errorIndex,
pdu.variableBindings.String())
}
// The ScopedPdu is used by SNMP V3.
// Includes the PduV1, and contains a SNMP context parameter
type ScopedPdu struct {
ContextEngineId []byte
ContextName []byte
PduV1
}
func (pdu *ScopedPdu) Marshal() (b []byte, err error) {
var buf []byte
raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}
buf, err = asn1.Marshal(pdu.ContextEngineId)
if err != nil {
return
}
raw.Bytes = buf
buf, err = asn1.Marshal(pdu.ContextName)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
buf, err = pdu.PduV1.Marshal()
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
return asn1.Marshal(raw)
}
func (pdu *ScopedPdu) Unmarshal(b []byte) (rest []byte, err error) {
var raw asn1.RawValue
rest, err = asn1.Unmarshal(b, &raw)
if err != nil {
return nil, err
}
if raw.Class != asn1.ClassUniversal || raw.Tag != asn1.TagSequence || !raw.IsCompound {
return nil, asn1.StructuralError{fmt.Sprintf(
"Invalid ScopedPud object - Class [%02x], Tag [%02x] : [%s]",
raw.Class, raw.Tag, ToHexStr(b, " "))}
}
next := raw.Bytes
var contextEngineId []byte
next, err = asn1.Unmarshal(next, &contextEngineId)
if err != nil {
return
}
var contextName []byte
next, err = asn1.Unmarshal(next, &contextName)
if err != nil {
return
}
var pduV1 PduV1
_, err = (&pduV1).Unmarshal(next)
if err != nil {
return
}
pdu.ContextEngineId = contextEngineId
pdu.ContextName = contextName
pdu.PduV1 = pduV1
return
}
func (pdu *ScopedPdu) String() string {
return fmt.Sprintf(
`{"Type": "%s", "RequestId": "%d", "ErrorStatus": "%s", "ErrorIndex": "%d", `+
`"ContextEngineId": "%s", "ContextName": %s, "VariableBindings": %s}`,
pdu.pduType, pdu.requestId, pdu.errorStatus, pdu.errorIndex,
ToHexStr(pdu.ContextEngineId, ""), escape(string(pdu.ContextName)),
pdu.variableBindings.String())
}
func NewPdu(ver SnmpVersion, t PduType) (pdu PDU) {
p := PduV1{pduType: t}
switch ver {
case V1, V2c:
pdu = &p
case V3:
pdu = &ScopedPdu{PduV1: p}
}
return
}
func NewPduWithOids(ver SnmpVersion, t PduType, oids Oids) (pdu PDU) {
pdu = NewPdu(ver, t)
for _, o := range oids {
pdu.AppendVariableBinding(o, NewNull())
}
return
}
func NewPduWithVarBinds(ver SnmpVersion, t PduType, VariableBindings VariableBindings) (pdu PDU) {
pdu = NewPdu(ver, t)
for _, v := range VariableBindings {
pdu.AppendVariableBinding(v.Oid, v.Variable)
}
return
}