forked from slayercat/GoSNMPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
116 lines (102 loc) · 2.68 KB
/
helpers.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
package GoSNMPServer
import (
"strconv"
"strings"
"github.com/gosnmp/gosnmp"
"github.com/pkg/errors"
)
func getPktContextOrCommunity(i *gosnmp.SnmpPacket) string {
if i.Version == gosnmp.Version3 {
return i.ContextName
} else {
return i.Community
}
}
func copySnmpPacket(i *gosnmp.SnmpPacket) gosnmp.SnmpPacket {
var ret gosnmp.SnmpPacket = *i
if i.SecurityParameters != nil {
ret.SecurityParameters = i.SecurityParameters.Copy()
}
return ret
}
type ByteString []int
type ByteStringCompareResult int
const ByteStringCompareResultEqual = 0
const ByteStringCompareResultLessThen = -1
const ByteStringCompareResultGreaterThen = 1
func compareByteString(arr1, arr2 ByteString) ByteStringCompareResult {
minLength := len(arr1)
if len(arr2) < minLength {
minLength = len(arr2)
}
for i := 0; i < minLength; i++ {
if arr1[i] < arr2[i] {
return ByteStringCompareResultLessThen
} else if arr1[i] > arr2[i] {
return ByteStringCompareResultGreaterThen
}
}
if len(arr1) < len(arr2) {
return ByteStringCompareResultLessThen
} else if len(arr1) > len(arr2) {
return ByteStringCompareResultGreaterThen
}
return ByteStringCompareResultEqual
}
// Fix BUG: When converting certain byte values to the rune type,
// some byte values may not be represented correctly because the rune type represents a Unicode character.
// This can cause byte values to become unpredictable or incorrect after conversion.
func oidToByteString(oid string) ByteString {
xi := strings.Split(oid, ".")
out := []int{}
for id, each := range xi {
if each == "" {
if id == 0 {
continue
} else {
panic(errors.Errorf("oidToByteString not valid id. value=%v", oid))
}
}
i, err := strconv.ParseInt(each, 10, 32)
if err != nil {
panic(err)
}
out = append(out, int(i))
}
return ByteString(out)
}
// IsValidObjectIdentifier will check an oid string is valid oid
// Deprecated: instead use VerifyOid.
func IsValidObjectIdentifier(oid string) (result bool) {
defer func() {
if err := recover(); err != nil {
result = false
return
}
}()
if len(oid) == 0 {
return false
}
oidToByteString(oid)
return true
}
// VerifyOid will check an oid string is valid oid,
// each number should be positive uint32.
func VerifyOid(oid string) error {
xi := strings.Split(oid, ".")
for id, each := range xi {
if each == "" {
if id == 0 {
continue
}
return errors.New("oidToByteString not valid int,but it is empty " + oid)
}
i, err := strconv.ParseInt(each, 10, 32)
if err != nil {
return errors.New("oidToByteString not valid int. value=" + each)
} else if i < 0 {
return errors.New("oidToByteString not valid int. value=" + each + " should be positive")
}
}
return nil
}