-
Notifications
You must be signed in to change notification settings - Fork 4
/
v4util.go
61 lines (55 loc) · 1.27 KB
/
v4util.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
package netutil
import (
"encoding/binary"
"errors"
"net"
)
var (
// ErrIPv6 is an error that a function does not support IPv6.
ErrIPv6 = errors.New("only IPv4 address is supported")
)
// IP4ToInt returns uint32 value for an IPv4 address.
// If ip is not an IPv4 address, this returns 0.
//
// Deprecated: use IPAdd and IPDiff.
func IP4ToInt(ip net.IP) uint32 {
ip = ip.To4()
if ip == nil {
return 0
}
return binary.BigEndian.Uint32(ip)
}
// IntToIP4 does the reverse of IP4ToInt.
//
// Deprecated: use IPAdd and IPDiff.
func IntToIP4(n uint32) net.IP {
ip := make([]byte, 4)
binary.BigEndian.PutUint32(ip, n)
return ip
}
// HostsFunc returns a function to generate all IP addresses in a
// network. The network address and the broadcast address of the network
// will be excluded.
//
// The returned function will finally generate nil to tell the end.
//
// The network must be an IPv4 network.
//
// Deprecated: this cannot be used for IPv6 networks.
func HostsFunc(n *net.IPNet) (func() net.IP, error) {
if n.IP.To4() == nil {
return nil, ErrIPv6
}
ones, bits := n.Mask.Size()
count := (1 << uint(bits-ones)) - 2
current := IP4ToInt(n.IP) + 1
return func() net.IP {
if count <= 0 {
return nil
}
ip := IntToIP4(current)
current++
count--
return ip
}, nil
}