-
Notifications
You must be signed in to change notification settings - Fork 48
/
engine.go
94 lines (81 loc) · 2.01 KB
/
engine.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
package snmpgo
import (
"fmt"
"net"
"time"
)
type snmpEngine struct {
mp messageProcessing
sec security
}
func (e *snmpEngine) SendPdu(pdu Pdu, conn net.Conn, args *SNMPArguments) (result Pdu, err error) {
size := args.MessageMaxSize
if size < recvBufferSize {
size = recvBufferSize
}
var sendMsg message
sendMsg, err = e.mp.PrepareOutgoingMessage(e.sec, pdu, args)
if err != nil {
return
}
var buf []byte
buf, err = sendMsg.Marshal()
if err != nil {
return
}
if err = conn.SetDeadline(time.Now().Add(args.Timeout)); err != nil {
return
}
_, err = conn.Write(buf)
if !confirmedType(pdu.PduType()) || err != nil {
return
}
buf = make([]byte, size)
_, err = conn.Read(buf)
if err != nil {
return
}
var recvMsg message
if recvMsg, _, err = unmarshalMessage(buf); err != nil {
return nil, &MessageError{
Cause: err,
Message: "Failed to Unmarshal message",
Detail: fmt.Sprintf("message Bytes - [%s]", toHexStr(buf, " ")),
}
}
result, err = e.mp.PrepareDataElements(e.sec, recvMsg, sendMsg)
if result != nil && len(pdu.VarBinds()) > 0 {
if err = e.checkPdu(result, args); err != nil {
result = nil
}
}
return
}
func (e *snmpEngine) checkPdu(pdu Pdu, args *SNMPArguments) (err error) {
varBinds := pdu.VarBinds()
if args.Version == V3 && pdu.PduType() == Report && len(varBinds) > 0 {
oid := varBinds[0].Oid.String()
rep := reportStatusOid(oid)
err = &MessageError{
Message: fmt.Sprintf("Received a report from the agent - %s(%s)", rep, oid),
Detail: fmt.Sprintf("Pdu - %s", pdu),
}
// perhaps the agent has rebooted after the previous communication
if rep == usmStatsNotInTimeWindows {
err = ¬InTimeWindowError{err}
}
}
return
}
func (e *snmpEngine) Discover(snmp *SNMP) error {
return e.sec.Discover(snmp)
}
func (e *snmpEngine) String() string {
return fmt.Sprintf(`{"sec": %s}`, e.sec.String())
}
func newSNMPEngine(args *SNMPArguments) *snmpEngine {
return &snmpEngine{
mp: newMessageProcessing(args.Version),
sec: newSecurity(args),
}
}