lib: Factor out getting IP address from net.Addr (#8538)
... and add fast paths for common cases.
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/syncthing/syncthing/lib/osutil"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -65,9 +66,7 @@ func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
|
||||
}
|
||||
|
||||
func packetConnUnspecified(conn interface{}) bool {
|
||||
// Since QUIC connections are wrapped, we can't do a simple typecheck
|
||||
// on *net.UDPAddr here.
|
||||
addr := conn.(net.PacketConn).LocalAddr()
|
||||
host, _, err := net.SplitHostPort(addr.String())
|
||||
return err == nil && net.ParseIP(host).IsUnspecified()
|
||||
ip, err := osutil.IPFromAddr(addr)
|
||||
return err == nil && ip.IsUnspecified()
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/syncthing/syncthing/lib/config"
|
||||
"github.com/syncthing/syncthing/lib/connections/registry"
|
||||
"github.com/syncthing/syncthing/lib/nat"
|
||||
"github.com/syncthing/syncthing/lib/osutil"
|
||||
"github.com/syncthing/syncthing/lib/protocol"
|
||||
"github.com/syncthing/syncthing/lib/stats"
|
||||
|
||||
@@ -117,14 +118,10 @@ func (c internalConn) Crypto() string {
|
||||
|
||||
func (c internalConn) Transport() string {
|
||||
transport := c.connType.Transport()
|
||||
host, _, err := net.SplitHostPort(c.LocalAddr().String())
|
||||
ip, err := osutil.IPFromAddr(c.LocalAddr())
|
||||
if err != nil {
|
||||
return transport
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return transport
|
||||
}
|
||||
if ip.To4() != nil {
|
||||
return transport + "4"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user