diff options
author | greatroar <61184462+greatroar@users.noreply.github.com> | 2022-09-14 08:44:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-14 08:44:46 +0200 |
commit | 8065cf7e971019bcf1225756e0364760984ffd13 (patch) | |
tree | 11da6bde372f65f4fef5be92df2a6872f9b19edd | |
parent | 6e768a838775985a705ceb55d7b61c6470c160fe (diff) |
lib: Factor out getting IP address from net.Addr (#8538)
... and add fast paths for common cases.
-rw-r--r-- | lib/connections/quic_misc.go | 7 | ||||
-rw-r--r-- | lib/connections/structs.go | 7 | ||||
-rw-r--r-- | lib/osutil/net.go (renamed from lib/osutil/lan.go) | 12 | ||||
-rw-r--r-- | lib/pmp/pmp.go | 7 | ||||
-rw-r--r-- | lib/relay/client/methods.go | 11 | ||||
-rw-r--r-- | lib/relay/client/static.go | 3 | ||||
-rw-r--r-- | lib/upnp/upnp.go | 8 |
7 files changed, 26 insertions, 29 deletions
diff --git a/lib/connections/quic_misc.go b/lib/connections/quic_misc.go index 1e502e148f..ce5490fb70 100644 --- a/lib/connections/quic_misc.go +++ b/lib/connections/quic_misc.go @@ -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() } diff --git a/lib/connections/structs.go b/lib/connections/structs.go index 84cb47298f..238ab7fb01 100644 --- a/lib/connections/structs.go +++ b/lib/connections/structs.go @@ -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" } diff --git a/lib/osutil/lan.go b/lib/osutil/net.go index 2b498416a2..1379be592b 100644 --- a/lib/osutil/lan.go +++ b/lib/osutil/net.go @@ -37,3 +37,15 @@ func GetLans() ([]*net.IPNet, error) { } return nets, nil } + +func IPFromAddr(addr net.Addr) (net.IP, error) { + switch a := addr.(type) { + case *net.TCPAddr: + return a.IP, nil + case *net.UDPAddr: + return a.IP, nil + default: + host, _, err := net.SplitHostPort(addr.String()) + return net.ParseIP(host), err + } +} diff --git a/lib/pmp/pmp.go b/lib/pmp/pmp.go index e1cd297917..ac8823e7a0 100644 --- a/lib/pmp/pmp.go +++ b/lib/pmp/pmp.go @@ -18,6 +18,7 @@ import ( natpmp "github.com/jackpal/go-nat-pmp" "github.com/syncthing/syncthing/lib/nat" + "github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/util" ) @@ -66,10 +67,8 @@ func Discover(ctx context.Context, renewal, timeout time.Duration) []nat.Device conn, err := (&net.Dialer{}).DialContext(timeoutCtx, "udp", net.JoinHostPort(ip.String(), "5351")) if err == nil { conn.Close() - localIPAddress, _, err := net.SplitHostPort(conn.LocalAddr().String()) - if err == nil { - localIP = net.ParseIP(localIPAddress) - } else { + localIP, err = osutil.IPFromAddr(conn.LocalAddr()) + if localIP == nil { l.Debugln("Failed to lookup local IP", err) } } diff --git a/lib/relay/client/methods.go b/lib/relay/client/methods.go index cbc8b0f320..fb320ba301 100644 --- a/lib/relay/client/methods.go +++ b/lib/relay/client/methods.go @@ -12,6 +12,7 @@ import ( "time" "github.com/syncthing/syncthing/lib/dialer" + "github.com/syncthing/syncthing/lib/osutil" syncthingprotocol "github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/relay/protocol" ) @@ -66,7 +67,7 @@ func GetInvitationFromRelay(ctx context.Context, uri *url.URL, id syncthingproto l.Debugln("Received invitation", msg, "via", conn.LocalAddr()) ip := net.IP(msg.Address) if len(ip) == 0 || ip.IsUnspecified() { - msg.Address = remoteIPBytes(conn) + msg.Address, _ = osutil.IPFromAddr(conn.RemoteAddr()) } return msg, nil default: @@ -163,11 +164,3 @@ func configForCerts(certs []tls.Certificate) *tls.Config { }, } } - -func remoteIPBytes(conn net.Conn) []byte { - addr := conn.RemoteAddr().String() - if host, _, err := net.SplitHostPort(addr); err == nil { - addr = host - } - return net.ParseIP(addr)[:] -} diff --git a/lib/relay/client/static.go b/lib/relay/client/static.go index 7c65031fc6..1be039fbeb 100644 --- a/lib/relay/client/static.go +++ b/lib/relay/client/static.go @@ -12,6 +12,7 @@ import ( "time" "github.com/syncthing/syncthing/lib/dialer" + "github.com/syncthing/syncthing/lib/osutil" syncthingprotocol "github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/relay/protocol" ) @@ -87,7 +88,7 @@ func (c *staticClient) serve(ctx context.Context) error { case protocol.SessionInvitation: ip := net.IP(msg.Address) if len(ip) == 0 || ip.IsUnspecified() { - msg.Address = remoteIPBytes(c.conn) + msg.Address, _ = osutil.IPFromAddr(c.conn.RemoteAddr()) } select { case c.invitations <- msg: diff --git a/lib/upnp/upnp.go b/lib/upnp/upnp.go index abaab1c1c8..0553dd9eb0 100644 --- a/lib/upnp/upnp.go +++ b/lib/upnp/upnp.go @@ -50,6 +50,7 @@ import ( "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/nat" + "github.com/syncthing/syncthing/lib/osutil" ) func init() { @@ -303,12 +304,7 @@ func localIP(ctx context.Context, url *url.URL) (net.IP, error) { } defer conn.Close() - localIPAddress, _, err := net.SplitHostPort(conn.LocalAddr().String()) - if err != nil { - return nil, err - } - - return net.ParseIP(localIPAddress), nil + return osutil.IPFromAddr(conn.LocalAddr()) } func getChildDevices(d upnpDevice, deviceType string) []upnpDevice { |