-
Notifications
You must be signed in to change notification settings - Fork 51
/
pducontrol.go
137 lines (106 loc) · 4.44 KB
/
pducontrol.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
package GoSNMPServer
import (
"net"
"github.com/gosnmp/gosnmp"
"github.com/pkg/errors"
)
// PermissionAllowance ENUM controls for Allowance
type PermissionAllowance int
// PermissionAllowanceAllowed allowed for access
const PermissionAllowanceAllowed PermissionAllowance = 0
// PermissionAllowanceDenied denies for access
const PermissionAllowanceDenied PermissionAllowance = 1
// FuncPDUControlCheckPermission checks for permission.
//
// return PermissionAllowanceAllowed / PermissionAllowanceDenied
type FuncPDUControlCheckPermission func(pktVersion gosnmp.SnmpVersion, pduType gosnmp.PDUType, contextName string) PermissionAllowance
// FuncPDUControlTrap will be called on trap.
//
// args:
// isInform: indicate if the request is a InformRequest.
// true -- It's a InformRequest. data will be returns to the client
// false -- It's a trap. data to returned will drop silencely.
// trapdata: what client asks for.
// returns:
// dataret -- try to return to client. nil for nothing to return
// err -- any error?(will return to client by string)
type FuncPDUControlTrap func(isInform bool, trapdata gosnmp.SnmpPDU) (dataret interface{}, err error)
// FuncPDUControlGet will be called on get value
type FuncPDUControlGet func() (value interface{}, err error)
// FuncPDUControlSet will be called on set value
type FuncPDUControlSet func(value interface{}) error
// PDUValueControlItem describe the action of get / set / walk in pdu tree
type PDUValueControlItem struct {
// OID controls which OID does this PDUValue works
OID string
// Type defines which type this OID is.
Type gosnmp.Asn1BER
// NonWalkable marks this oid as not walkable. It **WILL NOT** returned in walk items. but do retuend
// in direct get.
// All write only item will be NonWalkable
NonWalkable bool
/////////// Callbacks
// OnCheckPermission will be called on access this OID. set to nil to allow all access.
// return PermissionAllowanceAllowed for allow this access.
// (otherwrise) PermissionAllowanceDenied for disable access.
OnCheckPermission FuncPDUControlCheckPermission
// OnGet will be called on any GET / walk option. set to nil for mark this as a write-only item
OnGet FuncPDUControlGet
// OnSet will be called on any Set option. set to nil for mark as a read-only item.
OnSet FuncPDUControlSet
// OnTrap will be called on TRAP.
OnTrap FuncPDUControlTrap
//////////// For human document
//Document for this PDU Item. ignored by the program.
Document string
}
func Asn1IntegerUnwrap(i interface{}) int { return i.(int) }
func Asn1IntegerWrap(i int) interface{} { return i }
func Asn1OctetStringUnwrap(i interface{}) string {
switch i.(type) {
case string:
return i.(string)
default:
return string(i.([]uint8))
}
}
func Asn1OctetStringWrap(i string) interface{} { return i }
func Asn1ObjectIdentifierUnwrap(i interface{}) string { return i.(string) }
func Asn1ObjectIdentifierWrap(i string) interface{} { return i }
func Asn1IPAddressUnwrap(i interface{}) net.IP {
ip := net.ParseIP(i.(string))
if ip == nil {
panic(errors.Errorf("not valid ip: %v", i))
}
return ip
}
func Asn1IPAddressWrap(i net.IP) interface{} {
return i.String()
}
func Asn1Counter32Unwrap(i interface{}) uint { return i.(uint) }
func Asn1Counter32Wrap(i uint) interface{} { return i }
func Asn1Gauge32Unwrap(i interface{}) uint { return i.(uint) }
func Asn1Gauge32Wrap(i uint) interface{} { return i }
func Asn1TimeTicksUnwrap(i interface{}) uint32 { return i.(uint32) }
func Asn1TimeTicksWrap(i uint32) interface{} { return i }
func Asn1Counter64Unwrap(i interface{}) uint64 { return i.(uint64) }
func Asn1Counter64Wrap(i uint64) interface{} { return i }
func Asn1Uinteger32Unwrap(i interface{}) uint32 { return i.(uint32) }
func Asn1Uinteger32Wrap(i uint32) interface{} { return i }
func Asn1OpaqueFloatUnwrap(i interface{}) float32 { return i.(float32) }
func Asn1OpaqueFloatWrap(i float32) interface{} { return i }
func Asn1OpaqueDoubleUnwrap(i interface{}) float64 { return i.(float64) }
func Asn1OpaqueDoubleWrap(i float64) interface{} { return i }
type byOID []*PDUValueControlItem
func (x byOID) Len() int {
return len(x)
}
func (x byOID) Less(i, j int) bool {
bsi := oidToByteString(x[i].OID)
bsj := oidToByteString(x[j].OID)
// oidToByteString(x[i].OID) < oidToByteString(x[j].OID)
return compareByteString(bsi, bsj) == ByteStringCompareResultLessThen
}
func (x byOID) Swap(i, j int) {
x[i], x[j] = x[j], x[i]
}