From 6124c5eb31ab8f9db4db288002388554d6181c86 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Wed, 20 Feb 2013 19:18:01 -0800 Subject: [PATCH] Network: Simple random IP allocation on the bridge network. --- network.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/network.go b/network.go index 0a6d1b8598..75f4ceae3b 100644 --- a/network.go +++ b/network.go @@ -2,6 +2,7 @@ package docker import ( "fmt" + "math/rand" "net" ) @@ -15,8 +16,10 @@ type NetworkInterface struct { Gateway net.IP } -func allocateIPAddress() string { - return "10.0.3.2" +func allocateIPAddress(network *net.IPNet) net.IP { + ip := network.IP.Mask(network.Mask) + ip[3] = byte(rand.Intn(254)) + return ip } func getBridgeAddr(name string) (net.Addr, error) { @@ -52,7 +55,7 @@ func allocateNetwork() (*NetworkInterface, error) { bridge := bridgeAddr.(*net.IPNet) ipPrefixLen, _ := bridge.Mask.Size() iface := &NetworkInterface{ - IpAddress: allocateIPAddress(), + IpAddress: allocateIPAddress(bridge).String(), IpPrefixLen: ipPrefixLen, Gateway: bridge.IP, }