Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
61 lines (56 loc) · 2.13 KB
/
Copy pathrequest.go
File metadata and controls
61 lines (56 loc) · 2.13 KB
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
// Copyright 2012 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
package gosnmp
import (
"fmt"
"math"
)
// MkSnmpPacket builds a request or trap packet of pdutype with the variable
// bindings pdus from the client's version, community, SNMPv3 flags, security
// model, a copy of its security parameters (none for a nil
// *UsmSecurityParameters) and its context; maxRepetitions is kept with its top
// bit cleared.
func (x *GoSNMP) MkSnmpPacket(pdutype PDUType, pdus []SnmpPDU, nonRepeaters uint8, maxRepetitions uint32) *SnmpPacket {
var newSecParams SnmpV3SecurityParameters
if sp := usmOf(x.SecurityParameters); sp != nil {
newSecParams = sp.Copy()
}
return &SnmpPacket{
Version: x.Version,
Community: x.Community,
MsgFlags: x.MsgFlags,
SecurityModel: x.SecurityModel,
SecurityParameters: newSecParams,
ContextEngineID: x.ContextEngineID,
ContextName: x.ContextName,
Error: 0,
ErrorIndex: 0,
PDUType: pdutype,
NonRepeaters: nonRepeaters,
MaxRepetitions: maxRepetitions & math.MaxInt32,
Variables: pdus,
}
}
// Get sends a GET request for oids, at most MaxOids of them, and returns the
// answer.
func (x *GoSNMP) Get(oids []string) (result *SnmpPacket, err error) {
packetOut, err := x.oidRequest(GetRequest, oids, 0)
if err != nil {
return nil, err
}
return x.send(packetOut)
}
// oidRequest builds the request packet of pdutype for oids, each as a Null
// varbind, with maxRepetitions for a GetBulk, after checking their count
// against MaxOids: Get's, and each of a walk's GetNext and GetBulk requests.
func (x *GoSNMP) oidRequest(pdutype PDUType, oids []string, maxRepetitions uint32) (*SnmpPacket, error) {
if len(oids) > x.MaxOids {
return nil, fmt.Errorf("oid count (%d) is greater than MaxOids (%d)", len(oids), x.MaxOids)
}
pdus := make([]SnmpPDU, 0, len(oids))
for _, oid := range oids {
pdus = append(pdus, SnmpPDU{Name: oid, Type: Null, Value: nil})
}
return x.MkSnmpPacket(pdutype, pdus, 0, maxRepetitions), nil
}