lib/connections: Handle wrapped connection in SetTCPOptions (fixes #3223)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3225
This commit is contained in:
Jakob Borg 2016-05-31 08:11:57 +00:00 committed by Audrius Butkevicius
parent 9d756525ce
commit ac40b27c79
7 changed files with 35 additions and 29 deletions

View File

@ -8,7 +8,6 @@ package connections
import ( import (
"crypto/tls" "crypto/tls"
"net"
"net/url" "net/url"
"time" "time"
@ -40,7 +39,7 @@ func (d *relayDialer) Dial(id protocol.DeviceID, uri *url.URL) (IntermediateConn
return IntermediateConnection{}, err return IntermediateConnection{}, err
} }
err = dialer.SetTCPOptions(conn.(*net.TCPConn)) err = dialer.SetTCPOptions(conn)
if err != nil { if err != nil {
conn.Close() conn.Close()
return IntermediateConnection{}, err return IntermediateConnection{}, err

View File

@ -8,7 +8,6 @@ package connections
import ( import (
"crypto/tls" "crypto/tls"
"net"
"net/url" "net/url"
"sync" "sync"
"time" "time"
@ -74,7 +73,7 @@ func (t *relayListener) Serve() {
continue continue
} }
err = dialer.SetTCPOptions(conn.(*net.TCPConn)) err = dialer.SetTCPOptions(conn)
if err != nil { if err != nil {
l.Infoln(err) l.Infoln(err)
} }

View File

@ -102,7 +102,7 @@ func (t *tcpListener) Serve() {
l.Debugln("connect from", conn.RemoteAddr()) l.Debugln("connect from", conn.RemoteAddr())
err = dialer.SetTCPOptions(conn.(*net.TCPConn)) err = dialer.SetTCPOptions(conn)
if err != nil { if err != nil {
l.Infoln(err) l.Infoln(err)
} }

View File

@ -57,9 +57,7 @@ func dialWithFallback(proxyDialFunc dialFunc, fallbackDialFunc dialFunc, network
conn, err := proxyDialFunc(network, addr) conn, err := proxyDialFunc(network, addr)
if err == nil { if err == nil {
l.Debugf("Dialing %s address %s via proxy - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr()) l.Debugf("Dialing %s address %s via proxy - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr())
if tcpconn, ok := conn.(*net.TCPConn); ok { SetTCPOptions(conn)
SetTCPOptions(tcpconn)
}
return dialerConn{ return dialerConn{
conn, newDialerAddr(network, addr), conn, newDialerAddr(network, addr),
}, nil }, nil
@ -73,9 +71,7 @@ func dialWithFallback(proxyDialFunc dialFunc, fallbackDialFunc dialFunc, network
conn, err = fallbackDialFunc(network, addr) conn, err = fallbackDialFunc(network, addr)
if err == nil { if err == nil {
l.Debugf("Dialing %s address %s via fallback - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr()) l.Debugf("Dialing %s address %s via fallback - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr())
if tcpconn, ok := conn.(*net.TCPConn); ok { SetTCPOptions(conn)
SetTCPOptions(tcpconn)
}
} else { } else {
l.Debugf("Dialing %s address %s via fallback - error %s", network, addr, err) l.Debugf("Dialing %s address %s via fallback - error %s", network, addr, err)
} }

View File

@ -7,6 +7,7 @@
package dialer package dialer
import ( import (
"fmt"
"net" "net"
"time" "time"
) )
@ -47,8 +48,11 @@ func DialTimeout(network, addr string, timeout time.Duration) (net.Conn, error)
return net.DialTimeout(network, addr, timeout) return net.DialTimeout(network, addr, timeout)
} }
// SetTCPOptions sets syncthings default TCP options on a TCP connection // SetTCPOptions sets our default TCP options on a TCP connection, possibly
func SetTCPOptions(conn *net.TCPConn) error { // digging through dialerConn to extract the *net.TCPConn
func SetTCPOptions(conn net.Conn) error {
switch conn := conn.(type) {
case *net.TCPConn:
var err error var err error
if err = conn.SetLinger(0); err != nil { if err = conn.SetLinger(0); err != nil {
return err return err
@ -63,4 +67,11 @@ func SetTCPOptions(conn *net.TCPConn) error {
return err return err
} }
return nil return nil
case dialerConn:
return SetTCPOptions(conn.Conn)
default:
return fmt.Errorf("unknown connection type %T", conn)
}
} }

View File

@ -131,8 +131,8 @@ func getTCPConnectionPair() (net.Conn, net.Conn, error) {
} }
// Set the buffer sizes etc as usual // Set the buffer sizes etc as usual
dialer.SetTCPOptions(conn0.(*net.TCPConn)) dialer.SetTCPOptions(conn0)
dialer.SetTCPOptions(conn1.(*net.TCPConn)) dialer.SetTCPOptions(conn1)
return conn0, conn1, nil return conn0, conn1, nil
} }

View File

@ -122,7 +122,8 @@ func (c *staticClient) Serve() {
case protocol.SessionInvitation: case protocol.SessionInvitation:
ip := net.IP(msg.Address) ip := net.IP(msg.Address)
if len(ip) == 0 || ip.IsUnspecified() { if len(ip) == 0 || ip.IsUnspecified() {
msg.Address = c.conn.RemoteAddr().(*net.TCPAddr).IP[:] ip := net.ParseIP(c.conn.RemoteAddr().String())
msg.Address = ip[:]
} }
c.invitations <- msg c.invitations <- msg