-
Notifications
You must be signed in to change notification settings - Fork 42
/
net.go
51 lines (47 loc) · 962 Bytes
/
net.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
package routewrapper
import (
"fmt"
"net"
"strconv"
"strings"
)
var dotZero = []byte{'.', '0'}
func ourParseCIDRv4(s string) (*net.IPNet, error) {
c := strings.Count(s, ".")
_s := []byte(s)
for i := c; i < 3; i++ {
_s = append(_s, dotZero...)
}
if strings.LastIndexByte(s, byte('/')) < 0 {
_s = append(_s, byte('/'))
_s = strconv.AppendInt(_s, int64(8*(c+1)), 10)
}
s = string(_s)
_, net, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
return net, err
}
func ourParseCIDRv6(s string) (*net.IPNet, error) {
_s := []byte(s)
i := strings.LastIndexByte(s, byte('/'))
if i < 0 {
i = len(_s)
_s = append(_s, '/', '1', '2', '8')
}
j := strings.LastIndexByte(s, byte('%'))
if j >= 0 {
if j > i {
return nil, fmt.Errorf("Invalid CIDR block notation: %s", s)
}
l := copy(_s[j:], _s[i:])
_s = _s[0 : j+l]
}
s = string(_s)
_, net, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
return net, err
}