-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.go
79 lines (68 loc) · 1.21 KB
/
ip.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
package cloudinfo
import (
"net"
"net/netip"
)
type IPList []netip.Addr
func (l *IPList) addIP(ip net.IP) {
a, ok := netip.AddrFromSlice(ip)
if !ok {
return
}
l.addAddr(a)
}
func (l *IPList) addAddr(a netip.Addr) {
a = a.Unmap()
for _, i := range *l {
if i == a {
return
}
}
*l = append(*l, a)
}
func (l *IPList) addString(ip string) error {
a, err := netip.ParseAddr(ip)
if err != nil {
return err
}
l.addAddr(a)
return nil
}
// GetFirstV4 returns the first IPv4 found in the list
func (l IPList) GetFirstV4() (netip.Addr, bool) {
for _, a := range l {
if a.Is4() {
return a, true
}
}
return netip.Addr{}, false
}
// GetFirstV6 returns the first IPv6 found in the list
func (l IPList) GetFirstV6() (netip.Addr, bool) {
for _, a := range l {
if a.Is6() {
return a, true
}
}
return netip.Addr{}, false
}
// V4 returns an IPList with only IPv4 addresses included
func (l IPList) V4() IPList {
var res IPList
for _, a := range l {
if a.Is4() {
res = append(res, a)
}
}
return res
}
// V6 returns an IPList with only IPv6 addresses included
func (l IPList) V6() IPList {
var res IPList
for _, a := range l {
if a.Is6() {
res = append(res, a)
}
}
return res
}