forked from slayercat/GoSNMPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snmpserver.go
104 lines (94 loc) · 2.4 KB
/
snmpserver.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
package GoSNMPServer
import "net"
import "github.com/pkg/errors"
import "reflect"
type SNMPServer struct {
wconnStream ISnmpServerListener
master MasterAgent
logger ILogger
}
func NewSNMPServer(master MasterAgent) *SNMPServer {
ret := new(SNMPServer)
if err := master.ReadyForWork(); err != nil {
panic(err)
}
ret.master = master
ret.logger = master.Logger
return ret
}
func (server *SNMPServer) ListenUDP(l3proto, address string) error {
if server.wconnStream != nil {
return errors.New("Listened")
}
i, err := NewUDPListener(l3proto, address)
if err != nil {
return err
}
server.logger.Infof("ListenUDP: l3proto=%s, address=%s", l3proto, address)
i.SetupLogger(server.logger)
server.wconnStream = i
return nil
}
func (server *SNMPServer) Address() net.Addr {
return server.wconnStream.Address()
}
func (server *SNMPServer) Shutdown() {
server.logger.Infof("Shutdown server")
if server.wconnStream != nil {
server.wconnStream.Shutdown()
}
}
func (server *SNMPServer) ServeForever() error {
if server.wconnStream == nil {
return errors.New("Not Listen")
}
for {
err := server.ServeNextRequest()
if err != nil {
var opError *net.OpError
if errors.As(err, &opError) {
server.logger.Debugf("ServeForever: break because of serveNextRequest error %v", opError)
return nil
}
server.logger.Errorf("ServeForever: ServeNextRequest error %v [type %v]", err, reflect.TypeOf(err))
return errors.Wrap(err, "ServeNextRequest")
}
}
}
func (server *SNMPServer) ServeNextRequest() (err error) {
defer func() {
if err := recover(); err != nil {
switch err.(type) {
case error:
err = errors.Wrap(err.(error), "ServeNextRequest fails with panic")
default:
err = errors.Errorf("ServeNextRequest fails with panic. err(type %v)=%v", reflect.TypeOf(err), err)
}
server.logger.Errorf("ServeNextRequest error: %+v", err)
return
}
}()
bytePDU, replyer, err := server.wconnStream.NextSnmp()
if err != nil {
return err
}
result, err := server.master.ResponseForBuffer(bytePDU)
if err != nil {
v := "with"
if len(result) == 0 {
v = "without"
}
server.logger.Warnf("ResponseForBuffer Error: %v. %s result", err, v)
}
if len(result) != 0 {
if errreply := replyer.ReplyPDU(result); errreply != nil {
server.logger.Errorf("Reply PDU meet err:", errreply)
replyer.Shutdown()
return nil
}
}
if err != nil {
replyer.Shutdown()
}
return nil
}