1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #6920 from vieux/pr_6408

Add support for IPv6 addresses in --dns parameters.
This commit is contained in:
unclejack 2014-07-10 19:16:23 +03:00
commit 0c67dcb0df
4 changed files with 23 additions and 18 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")
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")
flDns = opts.NewListOpts(opts.ValidateIp4Address)
flDns = opts.NewListOpts(opts.ValidateIPAddress)
flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch)
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")

View file

@ -2,11 +2,13 @@ package opts
import (
"fmt"
"github.com/dotcloud/docker/utils"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/dotcloud/docker/utils"
)
// ListOpts type
@ -128,13 +130,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.

View file

@ -4,21 +4,25 @@ import (
"testing"
)
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)
func TestValidateIPAddress(t *testing.T) {
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)
}
}

View file

@ -44,7 +44,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flPublish opts.ListOpts
flExpose opts.ListOpts
flDns opts.ListOpts
flDns = opts.NewListOpts(opts.ValidateIPAddress)
flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch)
flVolumesFrom opts.ListOpts
flLxcOpts opts.ListOpts