bunch of other ipv6 fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2019-07-18 00:18:40 +03:00
parent 96cf14ed53
commit 1217ca94b1
7 changed files with 56 additions and 40 deletions

View File

@@ -8,6 +8,15 @@ import (
"strings"
)
// HostPort format addr and port suitable for dial
func HostPort(addr string, port interface{}) string {
host := addr
if strings.Count(addr, ":") > 0 {
host = fmt.Sprintf("[%s]", addr)
}
return fmt.Sprintf("%s:%v", host, port)
}
// Listen takes addr:portmin-portmax and binds to the first available port
// Example: Listen("localhost:5000-6000", fn)
func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error) {
@@ -22,10 +31,6 @@ func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, e
return nil, err
}
if host == "::" {
host = fmt.Sprintf("[%s]", host)
}
// try to extract port range
prange := strings.Split(ports, "-")
@@ -51,7 +56,7 @@ func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, e
// range the ports
for port := min; port <= max; port++ {
// try bind to host:port
ln, err := fn(fmt.Sprintf("%s:%d", host, port))
ln, err := fn(HostPort(host, port))
if err == nil {
return ln, nil
}