-
Notifications
You must be signed in to change notification settings - Fork 3
/
v4.go
48 lines (38 loc) · 858 Bytes
/
v4.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
package cidr
import (
"encoding/binary"
"net"
)
func (p *ParsedCIDR) getLastIPv4() net.IP {
if p.IsIPv4 != true {
return nil
}
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, p.LastIPv4())
return ip
}
// FirstIPv4 return Decimal FirstIP
func (p *ParsedCIDR) FirstIPv4() uint32 {
if p.IsIPv4 != true {
return 0
}
ip := p.FirstIP.To4()
var firstIP uint32
firstIP += uint32(ip[0]) << 24
firstIP += uint32(ip[1]) << 16
firstIP += uint32(ip[2]) << 8
firstIP += uint32(ip[3])
return firstIP
}
// HostCountIPv4 return number of IPs on the parsed range
func (p *ParsedCIDR) HostCountIPv4() uint32 {
ones, bits := p.IPNet.Mask.Size()
return uint32(1 << (bits - ones))
}
// LastIPv4 return Decimal LastIP
func (p *ParsedCIDR) LastIPv4() uint32 {
if p.IsIPv4 != true {
return 0
}
return p.FirstIPv4() + p.HostCountIPv4() - 1
}