1// Copyright 2012 The GoSNMP Authors. All rights reserved.  Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5// Copyright 2009 The Go Authors. All rights reserved.
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file.
8
9package gosnmp
10
11import (
12	"time"
13)
14
15//go:generate mockgen --destination gosnmp_mock.go --package=gosnmp --source interface.go
16
17// Handler is a GoSNMP interface
18//
19// Handler is provided to assist with testing using mocks
20type Handler interface {
21	// Connect creates and opens a socket. Because UDP is a connectionless
22	// protocol, you won't know if the remote host is responding until you send
23	// packets. And if the host is regularly disappearing and reappearing, you won't
24	// know if you've only done a Connect().
25	//
26	// For historical reasons (ie this is part of the public API), the method won't
27	// be renamed.
28	Connect() error
29
30	// ConnectIPv4 connects using IPv4
31	ConnectIPv4() error
32
33	// ConnectIPv6 connects using IPv6
34	ConnectIPv6() error
35
36	// Get sends an SNMP GET request
37	Get(oids []string) (result *SnmpPacket, err error)
38
39	// GetBulk sends an SNMP GETBULK request
40	//
41	// For maxRepetitions greater than 255, use BulkWalk() or BulkWalkAll()
42	GetBulk(oids []string, nonRepeaters uint8, maxRepetitions uint8) (result *SnmpPacket, err error)
43
44	// GetNext sends an SNMP GETNEXT request
45	GetNext(oids []string) (result *SnmpPacket, err error)
46
47	// Walk retrieves a subtree of values using GETNEXT - a request is made for each
48	// value, unlike BulkWalk which does this operation in batches. As the tree is
49	// walked walkFn is called for each new value. The function immediately returns
50	// an error if either there is an underlaying SNMP error (e.g. GetNext fails),
51	// or if walkFn returns an error.
52	Walk(rootOid string, walkFn WalkFunc) error
53
54	// WalkAll is similar to Walk but returns a filled array of all values rather
55	// than using a callback function to stream results.
56	WalkAll(rootOid string) (results []SnmpPDU, err error)
57
58	// BulkWalk retrieves a subtree of values using GETBULK. As the tree is
59	// walked walkFn is called for each new value. The function immediately returns
60	// an error if either there is an underlaying SNMP error (e.g. GetBulk fails),
61	// or if walkFn returns an error.
62	BulkWalk(rootOid string, walkFn WalkFunc) error
63
64	// BulkWalkAll is similar to BulkWalk but returns a filled array of all values
65	// rather than using a callback function to stream results.
66	BulkWalkAll(rootOid string) (results []SnmpPDU, err error)
67
68	// SendTrap sends a SNMP Trap (v2c/v3 only)
69	//
70	// pdus[0] can a pdu of Type TimeTicks (with the desired uint32 epoch
71	// time).  Otherwise a TimeTicks pdu will be prepended, with time set to
72	// now. This mirrors the behaviour of the Net-SNMP command-line tools.
73	//
74	// SendTrap doesn't wait for a return packet from the NMS (Network
75	// Management Station).
76	//
77	// See also Listen() and examples for creating an NMS.
78	SendTrap(trap SnmpTrap) (result *SnmpPacket, err error)
79
80	// UnmarshalTrap unpacks the SNMP Trap.
81	UnmarshalTrap(trap []byte, useResponseSecurityParameters bool) (result *SnmpPacket)
82
83	// Set sends an SNMP SET request
84	Set(pdus []SnmpPDU) (result *SnmpPacket, err error)
85
86	// Check makes checking errors easy, so they actually get a minimal check
87	Check(err error)
88
89	// Close closes the connection
90	Close() error
91
92	// Target gets the Target
93	Target() string
94
95	// SetTarget sets the Target
96	SetTarget(target string)
97
98	// Port gets the Port
99	Port() uint16
100
101	// SetPort sets the Port
102	SetPort(port uint16)
103
104	// Community gets the Community
105	Community() string
106
107	// SetCommunity sets the Community
108	SetCommunity(community string)
109
110	// Version gets the Version
111	Version() SnmpVersion
112
113	// SetVersion sets the Version
114	SetVersion(version SnmpVersion)
115
116	// Timeout gets the Timeout
117	Timeout() time.Duration
118
119	// SetTimeout sets the Timeout
120	SetTimeout(timeout time.Duration)
121
122	// Retries gets the Retries
123	Retries() int
124
125	// SetRetries sets the Retries
126	SetRetries(retries int)
127
128	// GetExponentialTimeout gets the ExponentialTimeout
129	GetExponentialTimeout() bool
130
131	// SetExponentialTimeout sets the ExponentialTimeout
132	SetExponentialTimeout(value bool)
133
134	// Logger gets the Logger
135	Logger() Logger
136
137	// SetLogger sets the Logger
138	SetLogger(logger Logger)
139
140	// MaxOids gets the MaxOids
141	MaxOids() int
142
143	// SetMaxOids sets the MaxOids
144	SetMaxOids(maxOids int)
145
146	// MaxRepetitions gets the maxRepetitions
147	MaxRepetitions() uint8
148
149	// SetMaxRepetitions sets the maxRepetitions
150	SetMaxRepetitions(maxRepetitions uint8)
151
152	// NonRepeaters gets the nonRepeaters
153	NonRepeaters() int
154
155	// SetNonRepeaters sets the nonRepeaters
156	SetNonRepeaters(nonRepeaters int)
157
158	// MsgFlags gets the MsgFlags
159	MsgFlags() SnmpV3MsgFlags
160
161	// SetMsgFlags sets the MsgFlags
162	SetMsgFlags(msgFlags SnmpV3MsgFlags)
163
164	// SecurityModel gets the SecurityModel
165	SecurityModel() SnmpV3SecurityModel
166
167	// SetSecurityModel sets the SecurityModel
168	SetSecurityModel(securityModel SnmpV3SecurityModel)
169
170	// SecurityParameters gets the SecurityParameters
171	SecurityParameters() SnmpV3SecurityParameters
172
173	// SetSecurityParameters sets the SecurityParameters
174	SetSecurityParameters(securityParameters SnmpV3SecurityParameters)
175
176	// ContextEngineID gets the ContextEngineID
177	ContextEngineID() string
178
179	// SetContextEngineID sets the ContextEngineID
180	SetContextEngineID(contextEngineID string)
181
182	// ContextName gets the ContextName
183	ContextName() string
184
185	// SetContextName sets the ContextName
186	SetContextName(contextName string)
187}
188
189// snmpHandler is a wrapper around gosnmp
190type snmpHandler struct {
191	GoSNMP
192}
193
194// NewHandler creates a new Handler using gosnmp
195func NewHandler() Handler {
196	return &snmpHandler{
197		GoSNMP{
198			Port:      Default.Port,
199			Community: Default.Community,
200			Version:   Default.Version,
201			Timeout:   Default.Timeout,
202			Retries:   Default.Retries,
203			MaxOids:   Default.MaxOids,
204		},
205	}
206}
207
208func (x *snmpHandler) Target() string {
209	// not x.Target because it would reference function Target
210	return x.GoSNMP.Target
211}
212
213func (x *snmpHandler) SetTarget(target string) {
214	x.GoSNMP.Target = target
215}
216
217func (x *snmpHandler) Port() uint16 {
218	return x.GoSNMP.Port
219}
220
221func (x *snmpHandler) SetPort(port uint16) {
222	x.GoSNMP.Port = port
223}
224
225func (x *snmpHandler) Community() string {
226	return x.GoSNMP.Community
227}
228
229func (x *snmpHandler) SetCommunity(community string) {
230	x.GoSNMP.Community = community
231}
232
233func (x *snmpHandler) Version() SnmpVersion {
234	return x.GoSNMP.Version
235}
236
237func (x *snmpHandler) SetVersion(version SnmpVersion) {
238	x.GoSNMP.Version = version
239}
240
241func (x *snmpHandler) Timeout() time.Duration {
242	return x.GoSNMP.Timeout
243}
244
245func (x *snmpHandler) SetTimeout(timeout time.Duration) {
246	x.GoSNMP.Timeout = timeout
247}
248
249func (x *snmpHandler) Retries() int {
250	return x.GoSNMP.Retries
251}
252
253func (x *snmpHandler) SetRetries(retries int) {
254	x.GoSNMP.Retries = retries
255}
256
257func (x *snmpHandler) GetExponentialTimeout() bool {
258	return x.GoSNMP.ExponentialTimeout
259}
260
261func (x *snmpHandler) SetExponentialTimeout(value bool) {
262	x.GoSNMP.ExponentialTimeout = value
263}
264
265func (x *snmpHandler) Logger() Logger {
266	return x.GoSNMP.Logger
267}
268
269func (x *snmpHandler) SetLogger(logger Logger) {
270	x.GoSNMP.Logger = logger
271}
272
273func (x *snmpHandler) MaxOids() int {
274	return x.GoSNMP.MaxOids
275}
276
277func (x *snmpHandler) SetMaxOids(maxOids int) {
278	x.GoSNMP.MaxOids = maxOids
279}
280
281func (x *snmpHandler) MaxRepetitions() uint8 {
282	return x.GoSNMP.MaxRepetitions
283}
284
285func (x *snmpHandler) SetMaxRepetitions(maxRepetitions uint8) {
286	x.GoSNMP.MaxRepetitions = maxRepetitions
287}
288
289func (x *snmpHandler) NonRepeaters() int {
290	return x.GoSNMP.NonRepeaters
291}
292
293func (x *snmpHandler) SetNonRepeaters(nonRepeaters int) {
294	x.GoSNMP.NonRepeaters = nonRepeaters
295}
296
297func (x *snmpHandler) MsgFlags() SnmpV3MsgFlags {
298	return x.GoSNMP.MsgFlags
299}
300
301func (x *snmpHandler) SetMsgFlags(msgFlags SnmpV3MsgFlags) {
302	x.GoSNMP.MsgFlags = msgFlags
303}
304
305func (x *snmpHandler) SecurityModel() SnmpV3SecurityModel {
306	return x.GoSNMP.SecurityModel
307}
308
309func (x *snmpHandler) SetSecurityModel(securityModel SnmpV3SecurityModel) {
310	x.GoSNMP.SecurityModel = securityModel
311}
312
313func (x *snmpHandler) SecurityParameters() SnmpV3SecurityParameters {
314	return x.GoSNMP.SecurityParameters
315}
316
317func (x *snmpHandler) SetSecurityParameters(securityParameters SnmpV3SecurityParameters) {
318	x.GoSNMP.SecurityParameters = securityParameters
319}
320
321func (x *snmpHandler) ContextEngineID() string {
322	return x.GoSNMP.ContextEngineID
323}
324
325func (x *snmpHandler) SetContextEngineID(contextEngineID string) {
326	x.GoSNMP.ContextEngineID = contextEngineID
327}
328
329func (x *snmpHandler) ContextName() string {
330	return x.GoSNMP.ContextName
331}
332
333func (x *snmpHandler) SetContextName(contextName string) {
334	x.GoSNMP.ContextName = contextName
335}
336
337func (x *snmpHandler) Close() error {
338	// not x.Conn for consistency
339	return x.GoSNMP.Conn.Close()
340}
341