forked from idawes/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
404 lines (355 loc) · 10.6 KB
/
msg.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
package gosnmp
import (
"fmt"
"net"
"time"
)
type pduType snmpBlockType
const (
pduType_GET_REQUEST pduType = 0xa0
pduType_GET_NEXT_REQUEST = 0xa1
pduType_GET_RESPONSE = 0xa2
pduType_SET_REQUEST = 0xa3
pduType_V1_TRAP = 0xa4
pduType_GET_BULK_REQUEST = 0xa5
pduType_INFORM_REQUEST = 0xa6
pduType_V2_TRAP = 0xa7
pduType_REPORT = 0xa8
)
func (pduType *pduType) String() string {
switch *pduType {
case pduType_GET_REQUEST:
return "GET REQUEST"
case pduType_GET_NEXT_REQUEST:
return "GET NEXT REQUEST"
case pduType_GET_RESPONSE:
return "GET RESPONSE"
case pduType_SET_REQUEST:
return "SET REQUEST"
default:
return "UNKNOWN PDU TYPE"
}
}
type SnmpMessage interface {
encode(encoderFactory *berEncoderFactory) ([]byte, error)
decode(decoder *berDecoder) error
getAddress() *net.UDPAddr
setAddress(*net.UDPAddr)
getVersion() SnmpVersion
setVersion(version SnmpVersion)
getpduType() pduType
setpduType(pduType pduType)
GetLoggingId() string
}
type SnmpRequest interface {
SnmpMessage
AddOid(oid ObjectIdentifier)
AddOids(oids []ObjectIdentifier)
GetFlightTime() time.Duration
wait()
notify()
setRequestId(requestId uint32)
getRequestId() uint32
isRetryRequired() bool
startTimer(func(SnmpRequest))
stopTimer()
setResponse(resp SnmpResponse)
setError(err error)
}
type SnmpResponse interface {
getRequestId() uint32
}
type V2cMessage interface {
}
// base type for all SNMP messages
type baseMsg struct {
version SnmpVersion
pduType pduType
varbinds []Varbind
address *net.UDPAddr
}
func (msg *baseMsg) getVersion() SnmpVersion {
return msg.version
}
func (msg *baseMsg) setVersion(version SnmpVersion) {
msg.version = version
}
func (msg *baseMsg) getpduType() pduType {
return msg.pduType
}
func (msg *baseMsg) setpduType(pduType pduType) {
msg.pduType = pduType
}
func (msg *baseMsg) getAddress() *net.UDPAddr {
return msg.address
}
func (msg *baseMsg) setAddress(addr *net.UDPAddr) {
msg.address = addr
}
func (msg *baseMsg) decodeVarbinds(decoder *berDecoder) (err error) {
varbindsListType, varbindsListLength, err := decoder.decodeHeader()
if err != nil {
return fmt.Errorf("Unable to decode varbinds list header - err: %s", err)
}
if varbindsListType != snmpBlockType_SEQUENCE {
return fmt.Errorf("Invalid message header type 0x%x - not 0x%x", varbindsListType, snmpBlockType_SEQUENCE)
}
if varbindsListLength != decoder.Len() {
return fmt.Errorf("Encoded varbinds list length %d doesn't match remaining msg length %d", varbindsListLength, decoder.Len())
}
varbindCount := 1
for ; ; varbindCount++ {
varbind, err := decodeVarbind(decoder)
if err != nil {
return fmt.Errorf("Decoding of varbind %d failed - err: %s", varbindCount, err)
}
msg.varbinds = append(msg.varbinds, varbind)
}
}
// base type for all v1/v2c messages
type communityMessage struct {
baseMsg
community string
}
type snmpCommunityMessage interface {
SnmpMessage
getCommunity() string
setCommunity(community string)
}
func (msg *communityMessage) getCommunity() string {
return msg.community
}
func (msg *communityMessage) setCommunity(community string) {
msg.community = community
}
func decodeCommunityMessage(decoder *berDecoder, version SnmpVersion) (snmpCommunityMessage, error) {
communityBytes, err := decoder.decodeOctetStringWithHeader()
if err != nil {
return nil, err
}
community := string(communityBytes)
rawpduType, pduLength, err := decoder.decodeHeader()
if err != nil {
return nil, fmt.Errorf("Unabled to decode pdu header - err: %s", err)
}
if pduLength != decoder.Len() {
return nil, fmt.Errorf("Encoded pdu length %d doesn't match remaining msg length %d", pduLength, decoder.Len())
}
pduType := pduType(rawpduType)
var msg snmpCommunityMessage
switch pduType {
case pduType_GET_REQUEST, pduType_GET_NEXT_REQUEST, pduType_SET_REQUEST:
msg = new(CommunityRequest)
case pduType_GET_RESPONSE:
msg = new(CommunityResponse)
case pduType_GET_BULK_REQUEST, pduType_INFORM_REQUEST, pduType_V2_TRAP, pduType_REPORT:
if version == Version1 {
return nil, fmt.Errorf("Invalid PDU type for SNMP version 1 message: %s", pduType)
}
switch pduType {
case pduType_GET_BULK_REQUEST:
msg = new(CommunityRequest)
case pduType_INFORM_REQUEST, pduType_V2_TRAP, pduType_REPORT:
return nil, fmt.Errorf("PDU type %d not supported yet", pduType)
}
case pduType_V1_TRAP:
if version != Version1 {
return nil, fmt.Errorf("Invalid version for V1 Trap message: %s", version)
}
msg = new(V1Trap)
default:
return nil, fmt.Errorf("Unsupported PDU type: 0x%x", rawpduType)
}
msg.setVersion(version)
msg.setCommunity(community)
msg.setpduType(pduType)
msg.decode(decoder)
return msg, nil
}
// base type for all v1/v2c request/response messages
type communityRequestResponse struct {
communityMessage
requestId uint32
errorVal int32
errorIdx int32
}
func (msg *communityRequestResponse) GetLoggingId() string {
return fmt.Sprintf("%s:%d", msg.pduType.String(), msg.requestId)
}
func (msg *communityRequestResponse) setRequestId(requestId uint32) {
msg.requestId = requestId
}
func (msg *communityRequestResponse) getRequestId() uint32 {
return msg.requestId
}
func (msg *communityRequestResponse) encode(encoderFactory *berEncoderFactory) ([]byte, error) {
encoder := encoderFactory.newberEncoder()
defer encoder.destroy()
msgHeader := encoder.newHeader(snmpBlockType_SEQUENCE)
headerFieldsLen := encoder.encodeInteger(int64(msg.version))
headerFieldsLen += encoder.encodeOctetString([]byte(msg.community))
pduHeader := encoder.newHeader(snmpBlockType(msg.pduType))
pduControlFieldsLen := encoder.encodeInteger(int64(msg.requestId))
pduControlFieldsLen += encoder.encodeInteger(int64(msg.errorVal))
pduControlFieldsLen += encoder.encodeInteger(int64(msg.errorIdx))
varbindsListHeader := encoder.newHeader(snmpBlockType_SEQUENCE)
varbindsLen := 0
for _, varbind := range msg.varbinds {
encodedLen, err := encoder.encodeVarbind(varbind)
if err != nil {
return nil, err
}
varbindsLen += encodedLen
}
_, varbindsListLen := varbindsListHeader.setContentLength(varbindsLen)
_, pduLen := pduHeader.setContentLength(pduControlFieldsLen + varbindsListLen)
msgHeader.setContentLength(headerFieldsLen + pduLen)
return encoder.serialize(), nil
}
func (msg *communityRequestResponse) decode(decoder *berDecoder) error {
var err error
if msg.requestId, err = decoder.decodeUint32WithHeader(); err != nil {
return err
}
if msg.errorVal, err = decoder.decodeInt32WithHeader(); err != nil {
return err
}
if msg.errorIdx, err = decoder.decodeInt32WithHeader(); err != nil {
return err
}
return msg.decodeVarbinds(decoder)
}
type CommunityRequest struct {
communityRequestResponse
response SnmpResponse
timeoutSeconds int
retriesRemaining int
timer *time.Timer
timeoutFunc func(SnmpRequest)
requestDoneChan chan bool
flightStartTime time.Time
flightTime time.Duration
err error
}
func newCommunityRequest() *CommunityRequest {
req := new(CommunityRequest)
req.requestDoneChan = make(chan bool)
return req
}
func (req *CommunityRequest) GetFlightTime() time.Duration {
return req.flightTime
}
func (req *CommunityRequest) startTimer(timeoutFunc func(SnmpRequest)) {
req.timeoutFunc = timeoutFunc
req.flightStartTime = time.Now()
req.timer = time.AfterFunc(time.Duration(req.timeoutSeconds)*time.Second, req.handleTimeout)
}
func (req *CommunityRequest) stopTimer() {
req.timer.Stop()
req.flightTime = time.Since(req.flightStartTime)
}
func (req *CommunityRequest) handleTimeout() {
req.flightTime = time.Since(req.flightStartTime)
req.timeoutFunc(req)
}
func (req *CommunityRequest) isRetryRequired() bool {
if req.retriesRemaining > 0 {
req.retriesRemaining--
return true
}
return false
}
func (req *CommunityRequest) AddOid(oid ObjectIdentifier) {
req.varbinds = append(req.varbinds, NewNullVarbind(oid))
}
func (req *CommunityRequest) AddOids(oids []ObjectIdentifier) {
temp := make([]Varbind, len(oids))
for i, oid := range oids {
temp[i] = NewNullVarbind(oid)
}
req.varbinds = append(req.varbinds, temp...)
}
func (req *CommunityRequest) wait() {
<-req.requestDoneChan
}
func (req *CommunityRequest) notify() {
req.requestDoneChan <- true
}
func (req *CommunityRequest) setResponse(resp SnmpResponse) {
req.response = resp
}
func (req *CommunityRequest) setError(err error) {
req.err = err
}
func (req *CommunityRequest) GetResponse() (resp SnmpResponse) {
return req.response
}
func (req *CommunityRequest) GetError() (err error) {
return req.err
}
func (req *CommunityRequest) GetRequestType() (requestType pduType) {
return req.pduType
}
type CommunityResponse struct {
communityRequestResponse
}
type V1Trap struct {
communityMessage
enterprise []uint32
agentAddr *net.IPAddr
genericTrap uint32
specificTrap uint32
timeStamp uint32
}
func (msg *V1Trap) GetLoggingId() string {
return fmt.Sprintf("%s:%d", msg.pduType, msg.timeStamp)
}
func (msg *V1Trap) encode(encoderFactory *berEncoderFactory) ([]byte, error) {
encoder := encoderFactory.newberEncoder()
defer encoder.destroy()
msgHeader := encoder.newHeader(snmpBlockType_SEQUENCE)
headerFieldsLen := encoder.encodeInteger(int64(msg.version))
headerFieldsLen += encoder.encodeOctetString([]byte(msg.community))
pduHeader := encoder.newHeader(snmpBlockType(msg.pduType))
varbindsListHeader := encoder.newHeader(snmpBlockType_SEQUENCE)
varbindsLen := 0
for _, varbind := range msg.varbinds {
encodedLen, err := encoder.encodeVarbind(varbind)
if err != nil {
return nil, err
}
varbindsLen += encodedLen
}
_, pduLen := varbindsListHeader.setContentLength(varbindsLen)
_, msgLen := pduHeader.setContentLength(pduLen)
msgLen += headerFieldsLen
msgHeader.setContentLength(msgLen)
return encoder.serialize(), nil
}
func (msg *V1Trap) decode(decoder *berDecoder) (err error) {
return
}
func decodeMsg(rawMsg []byte) (decodedMsg SnmpMessage, err error) {
decoder := newberDecoder(rawMsg)
msgType, length, err := decoder.decodeHeader()
if err != nil {
return nil, fmt.Errorf("Unable to decode message header - err: %s", err)
}
if msgType != snmpBlockType_SEQUENCE {
return nil, fmt.Errorf("Invalid message header type 0x%x - not 0x%x", msgType, snmpBlockType_SEQUENCE)
}
if length != decoder.Len() {
return nil, fmt.Errorf("Invalid message length - expected %d, got %d", length, decoder.Len())
}
rawVersion, err := decoder.decodeIntegerWithHeader()
if err != nil {
return nil, err
}
version := SnmpVersion(rawVersion)
switch version {
case Version1, Version2c:
return decodeCommunityMessage(decoder, version)
default:
return nil, fmt.Errorf("Unsupported snmp version code 0x%x", version)
}
}