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 Vincent Demeester
parent 9fdc86ac55
commit e56ff6bda9
2 changed files with 18 additions and 14 deletions

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)
} }
} }