forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
39 lines (35 loc) · 883 Bytes
/
net.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
package common
import "net"
// CidrFirstIP returns the first IP of the provided subnet.
func CidrFirstIP(cidr net.IP) net.IP {
for j := len(cidr) - 1; j >= 0; j-- {
cidr[j]++
if cidr[j] > 0 {
break
}
}
return cidr
}
// CidrStringFirstIP returns the first IP of the provided subnet string. Returns an error
// if the string cannot be parsed.
func CidrStringFirstIP(ip string) (net.IP, error) {
cidr, _, err := net.ParseCIDR(ip)
if err != nil {
return nil, err
}
return CidrFirstIP(cidr), nil
}
// IP4BroadcastAddress returns the broadcast address for the given IP subnet.
func IP4BroadcastAddress(n *net.IPNet) net.IP {
// see https://groups.google.com/d/msg/golang-nuts/IrfXFTUavXE/8YwzIOBwJf0J
ip4 := n.IP.To4()
if ip4 == nil {
return nil
}
last := make(net.IP, len(ip4))
copy(last, ip4)
for i := range ip4 {
last[i] |= ^n.Mask[i]
}
return last
}