/
types.go
121 lines (94 loc) · 2.83 KB
/
types.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
package snmp
import (
"bytes"
)
const (
TypeInteger = 0x02
TypeString = 0x04
TypeNull = 0x05
TypeOID = 0x06
TypeSequence = 0x30
TypeIpAddress = 0x40
TypeCounter = 0x41
TypeGauge = 0x42
TypeTimeTicks = 0x43
TypeCounter64 = 0x46
TypeNoSuchObject = 0x80
TypeNoSuchInstance = 0x81
TypeEndOfMIBView = 0x82
TypeGetRequest = 0xa0
TypeGetNextRequest = 0xa1
TypeGetResponse = 0xa2
TypeReport = 0xa8
)
// DataType represents an SNMP data type.
type DataType interface {
Encode() ([]byte, error)
}
// String represents an SNMP OCTET STRING.
type String string
// Encode encodes a String with the proper header.
func (s String) Encode() ([]byte, error) {
return append(encodeHeaderSequence(0x4, len(s)), []byte(s)...), nil
}
// String represents an SNMP IpAddress.
type IpAddress []byte
// Encode encodes a String with the proper header.
func (ip IpAddress) Encode() ([]byte, error) {
return append(encodeHeaderSequence(TypeIpAddress, len(ip)), []byte(ip)...), nil
}
// Report represents an SNMP Report-PDU.
type Report []DataType
// Encode encodes a Report with the proper header.
func (s Report) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
for _, entry := range s {
encodedEntry, err := entry.Encode()
if err != nil {
return nil, err
}
_, err = buf.Write(encodedEntry)
if err != nil {
return nil, err
}
}
seqLength := buf.Len()
return append(encodeHeaderSequence(0xa8, seqLength), buf.Bytes()...), nil
}
type tag byte
func (t tag) Encode() ([]byte, error) {
return []byte{byte(t), 0}, nil
}
// Null represents an SNMP NULL.
const Null tag = 0x05
const NoSuchObject tag = TypeNoSuchObject
const NoSuchInstance tag = TypeNoSuchInstance
const EndOfMIBView tag = TypeEndOfMIBView
// Gauge represents an SNMP Gauge data type.
type Gauge int
// Encode encodes a Gauge with the proper header.
func (g Gauge) Encode() ([]byte, error) {
result := encodeInteger(int(g))
return append(encodeHeaderSequence(TypeGauge, len(result)), result...), nil
}
// Counter represents an SNMP Counter data type.
type Counter int
// Encode encodes a Counter with the proper header.
func (c Counter) Encode() ([]byte, error) {
result := encodeInteger(int(c))
return append(encodeHeaderSequence(TypeCounter, len(result)), result...), nil
}
// Counter64 represents an SNMP Counter64 data type.
type Counter64 int
// Encode encodes a Counter with the proper header.
func (c Counter64) Encode() ([]byte, error) {
result := encodeInteger(int(c))
return append(encodeHeaderSequence(TypeCounter64, len(result)), result...), nil
}
// TimeTicks represents an SNMP TimeTicks data type.
type TimeTicks int
// Encode encodes a Gauge with the proper header.
func (t TimeTicks) Encode() ([]byte, error) {
result := encodeInteger(int(t))
return append(encodeHeaderSequence(TypeTimeTicks, len(result)), result...), nil
}