Skip to content

Commit

Permalink
lib: Factor out getting IP address from net.Addr (#8538)
Browse files Browse the repository at this point in the history
... and add fast paths for common cases.
  • Loading branch information
greatroar committed Sep 14, 2022
1 parent 6e768a8 commit 8065cf7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
7 changes: 3 additions & 4 deletions lib/connections/quic_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/lucas-clemente/quic-go"
"github.com/syncthing/syncthing/lib/osutil"
)

var (
Expand Down Expand Up @@ -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()
}
7 changes: 2 additions & 5 deletions lib/connections/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
}
Expand Down
12 changes: 12 additions & 0 deletions lib/osutil/lan.go → lib/osutil/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
7 changes: 3 additions & 4 deletions lib/pmp/pmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
Expand Down
11 changes: 2 additions & 9 deletions lib/relay/client/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)[:]
}
3 changes: 2 additions & 1 deletion lib/relay/client/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions lib/upnp/upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8065cf7

Please sign in to comment.