forked from xitongsys/pangolin
/
util.go
66 lines (59 loc) · 1.38 KB
/
util.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
package netinfo
import (
"fmt"
"strconv"
"strings"
)
//aa:bb:cc:dd:ee:ff -> [6]byte{}
func hws2bs(s string) ([]byte, error) {
ss := strings.Split(s, ":")
if len(ss) != 6 {
return nil, fmt.Errorf("hw %v error", s)
}
res := make([]byte, len(ss))
for i := 0; i < len(ss); i++ {
b, err := strconv.ParseUint(ss[i], 16, 8)
if err != nil {
return nil, fmt.Errorf("hw %v error", s)
}
res[i] = byte(b)
}
return res, nil
}
//bs -> uint32
func b2ip(bs []byte) (uint32, error) {
if len(bs) < 4 {
return 0, fmt.Errorf("bs %v error", bs)
}
return (uint32(bs[0]) << 24) + (uint32(bs[1]) << 16) + (uint32(bs[2]) << 8) + uint32(bs[3]), nil
}
//127.0.0.1 -> uint32
func s2ip(s string) (uint32, error) {
ss := strings.Split(s, ".")
if len(ss) != 4 {
return 0, fmt.Errorf("ip %v error", s)
}
res := uint32(0)
for i := 0; i < len(ss); i++ {
n, err := strconv.ParseUint(ss[3-i], 10, 8)
if err != nil {
return 0, fmt.Errorf("ip %v error", s)
}
res += (uint32(n) << uint32(i*8))
}
return res, nil
}
func ip2s(ip uint32) string {
return fmt.Sprintf("%d.%d.%d.%d", (ip>>24)&(0xff), (ip>>16)&(0xff), (ip>>8)&(0xff), ip&(0xff))
}
//3A010000 -> uint32
func ips2ip(s string) uint32 {
r, _ := strconv.ParseUint(s, 16, 32)
return uint32(r)
}
//000013A -> uint32
func iprs2ip(s string) uint32 {
s = s[6:8] + s[4:6] + s[2:4] + s[0:2]
r, _ := strconv.ParseUint(s, 16, 32)
return uint32(r)
}