From e56ff6bda9b6be1bdcf6940542a7516e1d607263 Mon Sep 17 00:00:00 2001 From: Jan Pazdziora Date: Fri, 13 Jun 2014 14:02:12 +0200 Subject: [PATCH] Add support for IPv6 addresses in --dns parameters. Docker-DCO-1.1-Signed-off-by: Jan Pazdziora (github: adelton) --- opts/opts.go | 12 ++++++------ opts/opts_test.go | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/opts/opts.go b/opts/opts.go index d17b57e07c..b8018c13bf 100644 --- a/opts/opts.go +++ b/opts/opts.go @@ -3,6 +3,7 @@ package opts import ( "fmt" "github.com/dotcloud/docker/utils" + "net" "os" "path/filepath" "regexp" @@ -128,13 +129,12 @@ func ValidateEnv(val string) (string, error) { return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil } -func ValidateIp4Address(val string) (string, error) { - re := regexp.MustCompile(`^(([0-9]+\.){3}([0-9]+))\s*$`) - var ns = re.FindSubmatch([]byte(val)) - if len(ns) > 0 { - return string(ns[1]), nil +func ValidateIpAddress(val string) (string, error) { + var ip = net.ParseIP(strings.TrimSpace(val)) + if ip != nil { + return ip.String(), 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. diff --git a/opts/opts_test.go b/opts/opts_test.go index b18088b934..4d37fcaf3a 100644 --- a/opts/opts_test.go +++ b/opts/opts_test.go @@ -5,20 +5,24 @@ import ( ) func TestValidateIP4(t *testing.T) { - if ret, err := ValidateIp4Address(`1.2.3.4`); err != nil || ret == "" { - t.Fatalf("ValidateIp4Address(`1.2.3.4`) got %s %s", ret, err) + if ret, err := ValidateIpAddress(`1.2.3.4`); err != nil || ret == "" { + t.Fatalf("ValidateIpAddress(`1.2.3.4`) got %s %s", ret, err) } - if ret, err := ValidateIp4Address(`127.0.0.1`); err != nil || ret == "" { - t.Fatalf("ValidateIp4Address(`127.0.0.1`) got %s %s", ret, err) + if ret, err := ValidateIpAddress(`127.0.0.1`); err != nil || ret == "" { + t.Fatalf("ValidateIpAddress(`127.0.0.1`) got %s %s", ret, err) } - if ret, err := ValidateIp4Address(`127`); err == nil || ret != "" { - t.Fatalf("ValidateIp4Address(`127`) got %s %s", ret, err) + if ret, err := ValidateIpAddress(`::1`); err != nil || ret == "" { + t.Fatalf("ValidateIpAddress(`::1`) got %s %s", ret, err) } - if ret, err := ValidateIp4Address(`random invalid string`); err == nil || ret != "" { - t.Fatalf("ValidateIp4Address(`random invalid string`) got %s %s", ret, err) + if ret, err := ValidateIpAddress(`127`); err == nil || ret != "" { + 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) } }