-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubnet.go
69 lines (55 loc) · 1.14 KB
/
subnet.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
package subnet
import (
"encoding/binary"
"errors"
"net"
)
type Subnet struct {
ipnet *net.IPNet
loc uint32
nwValue uint32
bcValue uint32
err error
}
func (s *Subnet) Begin() net.IP {
s.loc = 0
var b [4]byte
binary.BigEndian.PutUint32(b[:], s.nwValue)
return net.IPv4(b[0], b[1], b[2], b[3])
}
func (s *Subnet) Next() net.IP {
if s.nwValue+s.loc+1 > s.bcValue {
return nil
}
s.loc++
var b [4]byte
binary.BigEndian.PutUint32(b[:], s.nwValue+s.loc)
return net.IPv4(b[0], b[1], b[2], b[3])
}
func (s *Subnet) Prev() net.IP {
if s.nwValue-s.loc-1 < s.nwValue {
return nil
}
s.loc--
var b [4]byte
binary.BigEndian.PutUint32(b[:], s.nwValue+s.loc)
return net.IPv4(b[0], b[1], b[2], b[3])
}
func New(subnet string) (*Subnet, error) {
_, ipnet, err := net.ParseCIDR(subnet)
if err != nil {
return nil, err
}
if ipnet.IP.DefaultMask() == nil {
return nil, errors.New("only ipv4 supported")
}
ones, _ := ipnet.Mask.Size()
nwValue := binary.BigEndian.Uint32(ipnet.IP.To4())
bcValue := nwValue | (0xffffffff >> uint32(ones))
s := Subnet{
ipnet: ipnet,
nwValue: nwValue,
bcValue: bcValue,
}
return &s, nil
}