lib/connections: Allow "tcp4" and "tcp6" addresses

This commit is contained in:
Audrius Butkevicius 2016-03-25 07:15:32 +00:00 committed by Jakob Borg
parent 82e80a479a
commit 690837dbe5

View File

@ -17,11 +17,15 @@ import (
) )
func init() { func init() {
dialers["tcp"] = tcpDialer for _, network := range []string{"tcp", "tcp4", "tcp6"} {
listeners["tcp"] = tcpListener dialers[network] = makeTcpDialer(network)
listeners[network] = makeTcpListener(network)
}
} }
func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) { func makeTcpDialer(network string) DialerFactory {
return func(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
// Check that there is a port number in uri.Host, otherwise add one. // Check that there is a port number in uri.Host, otherwise add one.
host, port, err := net.SplitHostPort(uri.Host) host, port, err := net.SplitHostPort(uri.Host)
if err != nil && strings.HasPrefix(err.Error(), "missing port") { if err != nil && strings.HasPrefix(err.Error(), "missing port") {
@ -34,7 +38,7 @@ func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
// Don't try to resolve the address before dialing. The dialer may be a // Don't try to resolve the address before dialing. The dialer may be a
// proxy, and we should let the proxy do the resolving in that case. // proxy, and we should let the proxy do the resolving in that case.
conn, err := dialer.Dial("tcp", uri.Host) conn, err := dialer.Dial(network, uri.Host)
if err != nil { if err != nil {
l.Debugln(err) l.Debugln(err)
return nil, err return nil, err
@ -48,15 +52,17 @@ func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
} }
return tc, nil return tc, nil
}
} }
func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) { func makeTcpListener(network string) ListenerFactory {
tcaddr, err := net.ResolveTCPAddr("tcp", uri.Host) return func(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) {
tcaddr, err := net.ResolveTCPAddr(network, uri.Host)
if err != nil { if err != nil {
l.Fatalln("listen (BEP/tcp):", err) l.Fatalln("listen (BEP/tcp):", err)
return return
} }
listener, err := net.ListenTCP("tcp", tcaddr) listener, err := net.ListenTCP(network, tcaddr)
if err != nil { if err != nil {
l.Fatalln("listen (BEP/tcp):", err) l.Fatalln("listen (BEP/tcp):", err)
return return
@ -88,4 +94,5 @@ func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.Intermedia
tc, model.ConnectionTypeDirectAccept, tc, model.ConnectionTypeDirectAccept,
} }
} }
}
} }