Allow API to bind to ipv6 addresses

Use `net.SplitHostPort` which supports ipv6 rather than relying on
splitting on `:`

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2015-10-12 13:40:38 -04:00
parent 56ed0a6e8c
commit a82e3bc704
2 changed files with 47 additions and 23 deletions

View File

@ -5,6 +5,7 @@ package parsers
import ( import (
"fmt" "fmt"
"net"
"net/url" "net/url"
"path" "path"
"runtime" "runtime"
@ -74,26 +75,33 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
hostParts := strings.Split(u.Host, ":")
if len(hostParts) != 2 { host, port, err := net.SplitHostPort(u.Host)
if err != nil {
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr) return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
} }
defaults := strings.Split(defaultAddr, ":")
if len(defaults) != 3 {
return "", fmt.Errorf("Invalid defaults address format: %s", defaultAddr)
}
host := hostParts[0] defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://")
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
if err != nil {
return "", err
}
if host == "" { if host == "" {
host = strings.TrimPrefix(defaults[1], "//") host = defaultHost
} }
if hostParts[1] == "" { if port == "" {
hostParts[1] = defaults[2] port = defaultPort
} }
p, err := strconv.Atoi(hostParts[1]) p, err := strconv.Atoi(port)
if err != nil && p == 0 { if err != nil && p == 0 {
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr) return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
} }
if net.ParseIP(host).To4() == nil && strings.Contains(host, ":") {
// This is either an ipv6 address
host = "[" + host + "]"
}
return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil
} }

View File

@ -28,9 +28,13 @@ func TestParseDockerDaemonHost(t *testing.T) {
"fd": "Invalid bind address format: fd", "fd": "Invalid bind address format: fd",
} }
valids := map[string]string{ valids := map[string]string{
"0.0.0.1:": "tcp://0.0.0.1:2376", "0.0.0.1:": "tcp://0.0.0.1:2376",
"0.0.0.1:5555": "tcp://0.0.0.1:5555", "0.0.0.1:5555": "tcp://0.0.0.1:5555",
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
"[::1]:": "tcp://[::1]:2376",
"[::1]:5555/path": "tcp://[::1]:5555/path",
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376",
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
":6666": "tcp://127.0.0.1:6666", ":6666": "tcp://127.0.0.1:6666",
":6666/path": "tcp://127.0.0.1:6666/path", ":6666/path": "tcp://127.0.0.1:6666/path",
"": defaultHOST, "": defaultHOST,
@ -44,6 +48,9 @@ func TestParseDockerDaemonHost(t *testing.T) {
"unix://": "unix:///var/run/docker.sock", "unix://": "unix:///var/run/docker.sock",
"fd://": "fd://", "fd://": "fd://",
"fd://something": "fd://something", "fd://something": "fd://something",
"localhost:": "tcp://localhost:2376",
"localhost:5555": "tcp://localhost:5555",
"localhost:5555/path": "tcp://localhost:5555/path",
} }
for invalidAddr, expectedError := range invalids { for invalidAddr, expectedError := range invalids {
if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError { if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
@ -69,15 +76,24 @@ func TestParseTCP(t *testing.T) {
"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375", "udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
} }
valids := map[string]string{ valids := map[string]string{
"": defaultHTTPHost, "": defaultHTTPHost,
"tcp://": defaultHTTPHost, "tcp://": defaultHTTPHost,
"0.0.0.1:": "tcp://0.0.0.1:2376", "0.0.0.1:": "tcp://0.0.0.1:2376",
"0.0.0.1:5555": "tcp://0.0.0.1:5555", "0.0.0.1:5555": "tcp://0.0.0.1:5555",
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
":6666": "tcp://127.0.0.1:6666", ":6666": "tcp://127.0.0.1:6666",
":6666/path": "tcp://127.0.0.1:6666/path", ":6666/path": "tcp://127.0.0.1:6666/path",
"tcp://:7777": "tcp://127.0.0.1:7777", "tcp://:7777": "tcp://127.0.0.1:7777",
"tcp://:7777/path": "tcp://127.0.0.1:7777/path", "tcp://:7777/path": "tcp://127.0.0.1:7777/path",
"[::1]:": "tcp://[::1]:2376",
"[::1]:5555": "tcp://[::1]:5555",
"[::1]:5555/path": "tcp://[::1]:5555/path",
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376",
"[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555",
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
"localhost:": "tcp://localhost:2376",
"localhost:5555": "tcp://localhost:5555",
"localhost:5555/path": "tcp://localhost:5555/path",
} }
for invalidAddr, expectedError := range invalids { for invalidAddr, expectedError := range invalids {
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError { if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {