2015-10-03 21:51:53 -04:00
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ipVersion int
|
|
|
|
|
|
|
|
const (
|
|
|
|
v4 = 4
|
|
|
|
v6 = 6
|
|
|
|
)
|
|
|
|
|
2015-11-08 14:18:10 -05:00
|
|
|
func getAddressRange(pool string, masterNw *net.IPNet) (*AddressRange, error) {
|
2015-10-03 21:51:53 -04:00
|
|
|
ip, nw, err := net.ParseCIDR(pool)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ipamapi.ErrInvalidSubPool
|
|
|
|
}
|
2015-11-08 14:18:10 -05:00
|
|
|
lIP, e := types.GetHostPartIP(nw.IP, masterNw.Mask)
|
2015-10-03 21:51:53 -04:00
|
|
|
if e != nil {
|
|
|
|
return nil, fmt.Errorf("failed to compute range's lowest ip address: %v", e)
|
|
|
|
}
|
|
|
|
bIP, e := types.GetBroadcastIP(nw.IP, nw.Mask)
|
|
|
|
if e != nil {
|
|
|
|
return nil, fmt.Errorf("failed to compute range's broadcast ip address: %v", e)
|
|
|
|
}
|
2015-11-08 14:18:10 -05:00
|
|
|
hIP, e := types.GetHostPartIP(bIP, masterNw.Mask)
|
2015-10-03 21:51:53 -04:00
|
|
|
if e != nil {
|
|
|
|
return nil, fmt.Errorf("failed to compute range's highest ip address: %v", e)
|
|
|
|
}
|
|
|
|
nw.IP = ip
|
2015-10-08 23:04:13 -04:00
|
|
|
return &AddressRange{nw, ipToUint64(types.GetMinimalIP(lIP)), ipToUint64(types.GetMinimalIP(hIP))}, nil
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// It generates the ip address in the passed subnet specified by
|
|
|
|
// the passed host address ordinal
|
2015-10-08 23:04:13 -04:00
|
|
|
func generateAddress(ordinal uint64, network *net.IPNet) net.IP {
|
2015-10-03 21:51:53 -04:00
|
|
|
var address [16]byte
|
|
|
|
|
|
|
|
// Get network portion of IP
|
|
|
|
if getAddressVersion(network.IP) == v4 {
|
|
|
|
copy(address[:], network.IP.To4())
|
|
|
|
} else {
|
|
|
|
copy(address[:], network.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
end := len(network.Mask)
|
|
|
|
addIntToIP(address[:end], ordinal)
|
|
|
|
|
|
|
|
return net.IP(address[:end])
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAddressVersion(ip net.IP) ipVersion {
|
|
|
|
if ip.To4() == nil {
|
|
|
|
return v6
|
|
|
|
}
|
|
|
|
return v4
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds the ordinal IP to the current array
|
2016-02-28 11:34:30 -05:00
|
|
|
// 192.168.0.0 + 53 => 192.168.0.53
|
2015-10-08 23:04:13 -04:00
|
|
|
func addIntToIP(array []byte, ordinal uint64) {
|
2015-10-03 21:51:53 -04:00
|
|
|
for i := len(array) - 1; i >= 0; i-- {
|
|
|
|
array[i] |= (byte)(ordinal & 0xff)
|
|
|
|
ordinal >>= 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an ordinal to the respective IP address
|
2015-10-08 23:04:13 -04:00
|
|
|
func ipToUint64(ip []byte) (value uint64) {
|
|
|
|
cip := types.GetMinimalIP(ip)
|
|
|
|
for i := 0; i < len(cip); i++ {
|
|
|
|
j := len(cip) - 1 - i
|
|
|
|
value += uint64(cip[i]) << uint(j*8)
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|