-
Notifications
You must be signed in to change notification settings - Fork 17
/
parse.go
66 lines (62 loc) · 1.6 KB
/
parse.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
/*************************************************************************
* Copyright 2017 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package utils
import (
"encoding/binary"
"encoding/hex"
"errors"
"net"
"strconv"
"strings"
)
// ParseSource attempts to parse a string as a source override
// The priority logic is:
// 1. IP address
// 2. Numeric ID
// 3. hexadecimal hash
func ParseSource(v string) (ret net.IP, err error) {
var r uint64
var x []byte
if v = strings.TrimSpace(v); len(v) == 0 {
err = errors.New("Empty override")
return
}
if ret = net.ParseIP(v); len(ret) == 4 || len(ret) == 16 {
return
} else if r, err = ParseInt(v); err == nil {
ret = make(net.IP, 16)
binary.BigEndian.PutUint64(ret[8:], r)
return
}
//the string length must be > 0 && <= 32 && even
if len(v) == 0 || len(v) > 32 || (len(v)&0x1) != 0 {
err = errors.New("invalid source override")
}
if x, err = hex.DecodeString(v); err == nil {
if len(x) > 16 {
err = errors.New("source override too large")
return
}
if len(x) <= 4 {
ret = make([]byte, 4)
} else {
ret = make([]byte, 16)
}
offset := len(ret) - len(x)
copy(ret[offset:], x)
}
return
}
func ParseInt(v string) (r uint64, err error) {
if strings.HasPrefix(v, `0x`) {
r, err = strconv.ParseUint(strings.TrimPrefix(v, `0x`), 16, 64)
} else {
r, err = strconv.ParseUint(v, 10, 64)
}
return
}