From 2187ea163e879556cb9fdb45657c96d99db8a6f0 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Sat, 3 Feb 2024 23:58:34 +0100 Subject: imap: fix connection when host only has ipv6 address Some IMAP servers report both IPv4 and IPv6 addresses from their DNS name: $ host imap.gmail.com imap.gmail.com has address 108.177.15.109 imap.gmail.com has address 108.177.15.108 imap.gmail.com has IPv6 address 2a00:1450:400c:c0a::6c imap.gmail.com has IPv6 address 2a00:1450:400c:c0a::6d ResolveTCPAddr actually returns the first *IPv4* address by default, unless the address string is an explicit IPv6 address. Directly use net.Dial which has a fast fallback mechanism. It first tries to connect with an IPv6 address (if any) and if that fails, it will retry with an IPv4 address (if any) before failing completely. Link: https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/net/ipsock.go;l=81 Link: https://lists.sr.ht/~rjarry/aerc-discuss/%3CCYVLU3AOA00I.26I5IMAF3T4CK%40dow.land%3E Link: https://pkg.go.dev/net#Dial Reported-by: Jonathan Dowland Signed-off-by: Robin Jarry Tested-by: Koni Marti --- worker/imap/connect.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'worker') diff --git a/worker/imap/connect.go b/worker/imap/connect.go index 818952ac..79254bb9 100644 --- a/worker/imap/connect.go +++ b/worker/imap/connect.go @@ -123,19 +123,13 @@ func newTCPConn(addr string, timeout time.Duration) (*net.TCPConn, error) { done := make(chan tcpConn) go func() { defer log.PanicHandler() - addr, err := net.ResolveTCPAddr("tcp", addr) - if err != nil { - done <- tcpConn{nil, err} - return - } - newConn, err := net.DialTCP("tcp", nil, addr) + newConn, err := net.Dial("tcp", addr) if err != nil { done <- tcpConn{nil, err} return } - - done <- tcpConn{newConn, nil} + done <- tcpConn{newConn.(*net.TCPConn), nil} }() select { -- cgit