forked from posteo/go-agentx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
185 lines (159 loc) · 5.08 KB
/
session.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
// Copyright 2018 The agentx authors
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package agentx
import (
"errors"
"log"
"time"
"github.com/posteo/go-agentx/pdu"
"github.com/posteo/go-agentx/value"
"gopkg.in/errgo.v1"
)
// Session defines an agentx session.
type Session struct {
Handler Handler
client *Client
sessionID uint32
timeout time.Duration
openRequestPacket *pdu.HeaderPacket
registerRequestPacket *pdu.HeaderPacket
}
// ID returns the session id.
func (s *Session) ID() uint32 {
return s.sessionID
}
// Register registers the client under the provided rootID with the provided priority
// on the master agent.
func (s *Session) Register(priority byte, baseOID value.OID) error {
if s.registerRequestPacket != nil {
return errgo.Newf("session is already registered")
}
requestPacket := &pdu.Register{}
requestPacket.Timeout.Duration = s.timeout
requestPacket.Timeout.Priority = priority
requestPacket.Subtree.SetIdentifier(baseOID)
request := &pdu.HeaderPacket{Header: &pdu.Header{}, Packet: requestPacket}
response := s.request(request)
if err := checkError(response); err != nil {
return errgo.Mask(err)
}
s.registerRequestPacket = request
return nil
}
// Unregister removes the registration for the provided subtree.
func (s *Session) Unregister(priority byte, baseOID value.OID) error {
if s.registerRequestPacket == nil {
return errgo.Newf("session is not registered")
}
requestPacket := &pdu.Unregister{}
requestPacket.Timeout.Duration = s.timeout
requestPacket.Timeout.Priority = priority
requestPacket.Subtree.SetIdentifier(baseOID)
request := &pdu.HeaderPacket{Header: &pdu.Header{}, Packet: requestPacket}
response := s.request(request)
if err := checkError(response); err != nil {
return errgo.Mask(err)
}
s.registerRequestPacket = nil
return nil
}
// Close tears down the session with the master agent.
func (s *Session) Close() error {
requestPacket := &pdu.Close{Reason: pdu.ReasonShutdown}
response := s.request(&pdu.HeaderPacket{Header: &pdu.Header{}, Packet: requestPacket})
if err := checkError(response); err != nil {
return errgo.Mask(err)
}
return nil
}
func (s *Session) open(nameOID value.OID, name string) error {
requestPacket := &pdu.Open{}
requestPacket.Timeout.Duration = s.timeout
requestPacket.ID.SetIdentifier(nameOID)
requestPacket.Description.Text = name
request := &pdu.HeaderPacket{Header: &pdu.Header{}, Packet: requestPacket}
response := s.request(request)
if err := checkError(response); err != nil {
return errgo.Mask(err)
}
s.sessionID = response.Header.SessionID
s.openRequestPacket = request
return nil
}
func (s *Session) reopen() error {
if s.openRequestPacket != nil {
response := s.request(s.openRequestPacket)
if err := checkError(response); err != nil {
return errgo.Mask(err)
}
s.sessionID = response.Header.SessionID
}
if s.registerRequestPacket != nil {
response := s.request(s.registerRequestPacket)
if err := checkError(response); err != nil {
return errgo.Mask(err)
}
}
return nil
}
func (s *Session) request(hp *pdu.HeaderPacket) *pdu.HeaderPacket {
hp.Header.SessionID = s.sessionID
return s.client.request(hp)
}
func (s *Session) handle(request *pdu.HeaderPacket) *pdu.HeaderPacket {
responseHeader := &pdu.Header{}
responseHeader.SessionID = request.Header.SessionID
responseHeader.TransactionID = request.Header.TransactionID
responseHeader.PacketID = request.Header.PacketID
responsePacket := &pdu.Response{}
switch requestPacket := request.Packet.(type) {
case *pdu.Get:
if s.Handler == nil {
log.Printf("warning: no handler for session specified")
responsePacket.Variables.Add(requestPacket.GetOID(), pdu.VariableTypeNull, nil)
} else {
oid, t, v, err := s.Handler.Get(requestPacket.GetOID())
if err != nil {
log.Printf("error while handling packet: %s", errgo.Details(err))
responsePacket.Error = pdu.ErrorProcessing
}
if oid == nil {
responsePacket.Variables.Add(requestPacket.GetOID(), pdu.VariableTypeNoSuchObject, nil)
} else {
responsePacket.Variables.Add(oid, t, v)
}
}
case *pdu.GetNext:
if s.Handler == nil {
log.Printf("warning: no handler for session specified")
} else {
for _, sr := range requestPacket.SearchRanges {
oid, t, v, err := s.Handler.GetNext(sr.From.GetIdentifier(), (sr.From.Include == 1), sr.To.GetIdentifier())
if err != nil {
log.Printf("error while handling packet: %s", errgo.Details(err))
responsePacket.Error = pdu.ErrorProcessing
}
if oid == nil {
responsePacket.Variables.Add(sr.From.GetIdentifier(), pdu.VariableTypeEndOfMIBView, nil)
} else {
responsePacket.Variables.Add(oid, t, v)
}
}
}
default:
log.Printf("cannot handle unrequested packet: %v", request)
responsePacket.Error = pdu.ErrorProcessing
}
return &pdu.HeaderPacket{Header: responseHeader, Packet: responsePacket}
}
func checkError(hp *pdu.HeaderPacket) error {
response, ok := hp.Packet.(*pdu.Response)
if !ok {
return nil
}
if response.Error == pdu.ErrorNone {
return nil
}
return errors.New(response.Error.String())
}