Network: Simple random IP allocation on the bridge network.

This commit is contained in:
Andrea Luzzardi 2013-02-20 19:18:01 -08:00
parent 5039d4a280
commit 6124c5eb31
1 changed files with 6 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package docker
import ( import (
"fmt" "fmt"
"math/rand"
"net" "net"
) )
@ -15,8 +16,10 @@ type NetworkInterface struct {
Gateway net.IP Gateway net.IP
} }
func allocateIPAddress() string { func allocateIPAddress(network *net.IPNet) net.IP {
return "10.0.3.2" ip := network.IP.Mask(network.Mask)
ip[3] = byte(rand.Intn(254))
return ip
} }
func getBridgeAddr(name string) (net.Addr, error) { func getBridgeAddr(name string) (net.Addr, error) {
@ -52,7 +55,7 @@ func allocateNetwork() (*NetworkInterface, error) {
bridge := bridgeAddr.(*net.IPNet) bridge := bridgeAddr.(*net.IPNet)
ipPrefixLen, _ := bridge.Mask.Size() ipPrefixLen, _ := bridge.Mask.Size()
iface := &NetworkInterface{ iface := &NetworkInterface{
IpAddress: allocateIPAddress(), IpAddress: allocateIPAddress(bridge).String(),
IpPrefixLen: ipPrefixLen, IpPrefixLen: ipPrefixLen,
Gateway: bridge.IP, Gateway: bridge.IP,
} }