forked from k-sone/snmpgo
/
pdu_trap.go
155 lines (130 loc) · 3.47 KB
/
pdu_trap.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
package snmpgo
import (
"encoding/asn1"
"errors"
"fmt"
"time"
)
// The PduTrapV1 is used by SNMP V1 and V2c, other than the SNMP V1 Trap
type PduTrapV1 struct {
pduType PduType
enterprise *Oid
agentAddr *Ipaddress
genericTrap int
specificTrap int
timeStamp int
varBinds VarBinds
}
func (pdu *PduTrapV1) PduType() PduType {
return pdu.pduType
}
func (pdu *PduTrapV1) RequestId() int {
return 0
}
func (pdu *PduTrapV1) SetRequestId(int) {
}
func (pdu *PduTrapV1) ErrorStatus() ErrorStatus {
return NoError
}
func (pdu *PduTrapV1) SetErrorStatus(ErrorStatus) {
}
func (pdu *PduTrapV1) ErrorIndex() int {
return 0
}
func (pdu *PduTrapV1) SetErrorIndex(int) {
}
func (pdu *PduTrapV1) SetNonrepeaters(int) {
}
func (pdu *PduTrapV1) SetMaxRepetitions(int) {
}
func (pdu *PduTrapV1) AppendVarBind(oid *Oid, variable Variable) {
pdu.varBinds = append(pdu.varBinds, &VarBind{
Oid: oid,
Variable: variable,
})
}
func (pdu *PduTrapV1) VarBinds() VarBinds {
return pdu.varBinds
}
func (pdu *PduTrapV1) Marshal() (b []byte, err error) {
var buf []byte
raw := asn1.RawValue{Class: classContextSpecific, Tag: int(pdu.pduType), IsCompound: true}
buf, err = asn1.Marshal(pdu.enterprise.Value)
if err != nil {
return
}
raw.Bytes = buf
buf, err = asn1.MarshalWithParams(pdu.agentAddr.Value, "tag:0,application")
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.MarshalWithParams(pdu.timeStamp, "tag:3,application")
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
varBinds := asn1.RawValue{Class: classUniversal, Tag: tagSequence, IsCompound: true}
for i := 0; i < len(pdu.varBinds); i++ {
buf, err = pdu.varBinds[i].Marshal()
if err != nil {
return
}
varBinds.Bytes = append(varBinds.Bytes, buf...)
}
buf, err = asn1.Marshal(varBinds)
if err != nil {
return
}
raw.Bytes = append(raw.Bytes, buf...)
return asn1.Marshal(raw)
}
func (pdu *PduTrapV1) Unmarshal(b []byte) (rest []byte, err error) {
return nil, errors.New("Not Implemented")
}
func (pdu *PduTrapV1) String() string {
return fmt.Sprintf(
`{"Type": "%s", "Enterprise": "%v", "genericTrap": "%v", `+
`"specificTrap": "%d", "VarBinds": %s}`,
pdu.pduType, pdu.enterprise, pdu.genericTrap, pdu.specificTrap,
pdu.varBinds.String())
}
func NewPduTrapV1(ver SNMPVersion, t PduType, enterprise *Oid, genericTrap int, specificTrap int, ipaddress *Ipaddress) (pdu Pdu) {
p := PduTrapV1{
pduType: t,
enterprise: enterprise,
agentAddr: ipaddress,
genericTrap: 6,
specificTrap: specificTrap,
timeStamp: int(time.Now().UnixNano() / 10000000),
}
switch ver {
case V1:
pdu = &p
}
return
}
func NewTrapV1WithOids(ver SNMPVersion, t PduType, enterprise *Oid, genericTrap int, specificTrap int, ipaddress *Ipaddress, oids Oids) (pdu Pdu) {
pdu = NewPduTrapV1(ver, t, enterprise, genericTrap, specificTrap, ipaddress)
for _, o := range oids {
pdu.AppendVarBind(o, NewNull())
}
return
}
func NewPduTrapV1WithVarBinds(ver SNMPVersion, t PduType, enterprise *Oid, genericTrap int, specificTrap int, ipaddress *Ipaddress, varBinds VarBinds) (pdu Pdu) {
pdu = NewPduTrapV1(ver, t, enterprise, genericTrap, specificTrap, ipaddress)
for _, v := range varBinds {
pdu.AppendVarBind(v.Oid, v.Variable)
}
return
}