forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
150 lines (132 loc) · 4.14 KB
/
common.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package netutils
import (
"encoding/binary"
"fmt"
"net"
"github.com/golang/glog"
kerrors "k8s.io/apimachinery/pkg/util/errors"
)
var localHosts []string = []string{"127.0.0.1", "::1", "localhost"}
var localSubnets []string = []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fc00::/7", "fe80::/10"}
func IPToUint32(ip net.IP) uint32 {
return binary.BigEndian.Uint32(ip.To4())
}
func Uint32ToIP(u uint32) net.IP {
ip := make([]byte, 4)
binary.BigEndian.PutUint32(ip, u)
return net.IPv4(ip[0], ip[1], ip[2], ip[3])
}
// Generate the default gateway IP Address for a subnet
func GenerateDefaultGateway(sna *net.IPNet) net.IP {
ip := sna.IP.To4()
return net.IPv4(ip[0], ip[1], ip[2], ip[3]|0x1)
}
// Return Host IP Networks
// Ignores provided interfaces and filters loopback and non IPv4 addrs.
func GetHostIPNetworks(skipInterfaces []string) ([]*net.IPNet, []net.IP, error) {
hostInterfaces, err := net.Interfaces()
if err != nil {
return nil, nil, err
}
skipInterfaceMap := make(map[string]bool)
for _, ifaceName := range skipInterfaces {
skipInterfaceMap[ifaceName] = true
}
errList := []error{}
var hostIPNets []*net.IPNet
var hostIPs []net.IP
for _, iface := range hostInterfaces {
if skipInterfaceMap[iface.Name] {
continue
}
ifAddrs, err := iface.Addrs()
if err != nil {
errList = append(errList, err)
continue
}
for _, addr := range ifAddrs {
ip, ipNet, err := net.ParseCIDR(addr.String())
if err != nil {
errList = append(errList, err)
continue
}
// Skip loopback and non IPv4 addrs
if !ip.IsLoopback() && ip.To4() != nil {
hostIPNets = append(hostIPNets, ipNet)
hostIPs = append(hostIPs, ip)
}
}
}
return hostIPNets, hostIPs, kerrors.NewAggregate(errList)
}
func GetNodeIP(nodeName string) (string, error) {
ip := net.ParseIP(nodeName)
if ip == nil {
addrs, err := net.LookupIP(nodeName)
if err != nil {
return "", fmt.Errorf("Failed to lookup IP address for node %s: %v", nodeName, err)
}
for _, addr := range addrs {
// Skip loopback and non IPv4 addrs
if addr.IsLoopback() || addr.To4() == nil {
glog.V(5).Infof("Skipping loopback/non-IPv4 addr: %q for node %s", addr.String(), nodeName)
continue
}
ip = addr
break
}
} else if ip.IsLoopback() || ip.To4() == nil {
glog.V(5).Infof("Skipping loopback/non-IPv4 addr: %q for node %s", ip.String(), nodeName)
ip = nil
}
if ip == nil || len(ip.String()) == 0 {
return "", fmt.Errorf("Failed to obtain IP address from node name: %s", nodeName)
}
return ip.String(), nil
}
// ParseCIDRMask parses a CIDR string and ensures that it has no bits set beyond the
// network mask length. Use this when the input is supposed to be either a description of
// a subnet (eg, "192.168.1.0/24", meaning "192.168.1.0 to 192.168.1.255"), or a mask for
// matching against (eg, "192.168.1.15/32", meaning "must match all 32 bits of the address
// "192.168.1.15"). Use net.ParseCIDR() when the input is a host address that also
// describes the subnet that it is on (eg, "192.168.1.15/24", meaning "the address
// 192.168.1.15 on the network 192.168.1.0/24").
func ParseCIDRMask(cidr string) (*net.IPNet, error) {
ip, net, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
if !ip.Equal(net.IP) {
maskLen, addrLen := net.Mask.Size()
return nil, fmt.Errorf("CIDR network specification %q is not in canonical form (should be %s/%d or %s/%d?)", cidr, ip.Mask(net.Mask).String(), maskLen, ip.String(), addrLen)
}
return net, nil
}
// IsPrivateAddress returns true if given address in format "<host>[:<port>]" is a localhost or an ip from
// private network range (e.g. 172.30.0.1, 192.168.0.1).
func IsPrivateAddress(addr string) bool {
host, _, err := net.SplitHostPort(addr)
if err != nil {
// assume indexName is of the form `host` without the port and go on.
host = addr
}
for _, localHost := range localHosts {
if host == localHost {
return true
}
}
ip := net.ParseIP(host)
if ip == nil {
return false
}
for _, subnet := range localSubnets {
ipnet, err := ParseCIDRMask(subnet)
if err != nil {
continue // should not happen
}
if ipnet.Contains(ip) {
return true
}
}
return false
}