Add support for IPv6 addresses in --dns parameters.

Docker-DCO-1.1-Signed-off-by: Jan Pazdziora <jpazdziora@redhat.com> (github: adelton)
This commit is contained in:
Jan Pazdziora 2014-06-13 14:02:12 +02:00 committed by Victor Vieux
parent 519319b989
commit 899e9e7416
3 changed files with 19 additions and 15 deletions

View File

@ -51,7 +51,7 @@ func main() {
flRoot = flag.String([]string{"g", "-graph"}, "/var/lib/docker", "Path to use as the root of the Docker runtime") flRoot = flag.String([]string{"g", "-graph"}, "/var/lib/docker", "Path to use as the root of the Docker runtime")
flSocketGroup = flag.String([]string{"G", "-group"}, "docker", "Group to assign the unix socket specified by -H when running in daemon mode\nuse '' (the empty string) to disable setting of a group") flSocketGroup = flag.String([]string{"G", "-group"}, "docker", "Group to assign the unix socket specified by -H when running in daemon mode\nuse '' (the empty string) to disable setting of a group")
flEnableCors = flag.Bool([]string{"#api-enable-cors", "-api-enable-cors"}, false, "Enable CORS headers in the remote API") flEnableCors = flag.Bool([]string{"#api-enable-cors", "-api-enable-cors"}, false, "Enable CORS headers in the remote API")
flDns = opts.NewListOpts(opts.ValidateIp4Address) flDns = opts.NewListOpts(opts.ValidateIpAddress)
flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch) flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch)
flEnableIptables = flag.Bool([]string{"#iptables", "-iptables"}, true, "Enable Docker's addition of iptables rules") flEnableIptables = flag.Bool([]string{"#iptables", "-iptables"}, true, "Enable Docker's addition of iptables rules")
flEnableIpForward = flag.Bool([]string{"#ip-forward", "-ip-forward"}, true, "Enable net.ipv4.ip_forward") flEnableIpForward = flag.Bool([]string{"#ip-forward", "-ip-forward"}, true, "Enable net.ipv4.ip_forward")

View File

@ -3,6 +3,7 @@ package opts
import ( import (
"fmt" "fmt"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"net"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -128,13 +129,12 @@ func ValidateEnv(val string) (string, error) {
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
} }
func ValidateIp4Address(val string) (string, error) { func ValidateIpAddress(val string) (string, error) {
re := regexp.MustCompile(`^(([0-9]+\.){3}([0-9]+))\s*$`) var ip = net.ParseIP(strings.TrimSpace(val))
var ns = re.FindSubmatch([]byte(val)) if ip != nil {
if len(ns) > 0 { return ip.String(), nil
return string(ns[1]), nil
} }
return "", fmt.Errorf("%s is not an ip4 address", val) return "", fmt.Errorf("%s is not an ip address", val)
} }
// Validates domain for resolvconf search configuration. // Validates domain for resolvconf search configuration.

View File

@ -5,20 +5,24 @@ import (
) )
func TestValidateIP4(t *testing.T) { func TestValidateIP4(t *testing.T) {
if ret, err := ValidateIp4Address(`1.2.3.4`); err != nil || ret == "" { if ret, err := ValidateIpAddress(`1.2.3.4`); err != nil || ret == "" {
t.Fatalf("ValidateIp4Address(`1.2.3.4`) got %s %s", ret, err) t.Fatalf("ValidateIpAddress(`1.2.3.4`) got %s %s", ret, err)
} }
if ret, err := ValidateIp4Address(`127.0.0.1`); err != nil || ret == "" { if ret, err := ValidateIpAddress(`127.0.0.1`); err != nil || ret == "" {
t.Fatalf("ValidateIp4Address(`127.0.0.1`) got %s %s", ret, err) t.Fatalf("ValidateIpAddress(`127.0.0.1`) got %s %s", ret, err)
} }
if ret, err := ValidateIp4Address(`127`); err == nil || ret != "" { if ret, err := ValidateIpAddress(`::1`); err != nil || ret == "" {
t.Fatalf("ValidateIp4Address(`127`) got %s %s", ret, err) t.Fatalf("ValidateIpAddress(`::1`) got %s %s", ret, err)
} }
if ret, err := ValidateIp4Address(`random invalid string`); err == nil || ret != "" { if ret, err := ValidateIpAddress(`127`); err == nil || ret != "" {
t.Fatalf("ValidateIp4Address(`random invalid string`) got %s %s", ret, err) t.Fatalf("ValidateIpAddress(`127`) got %s %s", ret, err)
}
if ret, err := ValidateIpAddress(`random invalid string`); err == nil || ret != "" {
t.Fatalf("ValidateIpAddress(`random invalid string`) got %s %s", ret, err)
} }
} }