Skip to content

Commit 5cfe615

Browse files
authored
Properly report DNSLink errors (#12)
* Properly report DNSLink errors Only report that there is no DNSLink for a name when there are no DNSLink TXT records available for that name. For all other errors, such as being offline, report the more general "cannot resolve name" error. * Document that we give precedence to good results from looking up DNSLinks in TXT records from `"_dnslink."+fqdn` over results from `fqdn`. This commit was moved from ipfs/go-namesys@22432d1
1 parent 80ecd1a commit 5cfe615

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

namesys/base.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package namesys
22

33
import (
44
"context"
5-
"fmt"
65
"strings"
76
"time"
87

@@ -37,14 +36,6 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO
3736
}
3837
}
3938

40-
if err == ErrResolveFailed {
41-
i := len(name) - 1
42-
for i >= 0 && name[i] != '/' {
43-
i--
44-
}
45-
// Wrap error so that it can be tested if it is a ErrResolveFailed
46-
err = fmt.Errorf("%w: %q is missing a DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, name[i+1:])
47-
}
4839
return p, err
4940
}
5041

namesys/dns.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net"
8+
gpath "path"
89
"strings"
910

1011
path "github.com/ipfs/go-path"
@@ -88,6 +89,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
8889

8990
go func() {
9091
defer close(out)
92+
var rootResErr, subResErr error
9193
for {
9294
select {
9395
case subRes, ok := <-subChan:
@@ -98,8 +100,11 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
98100
if subRes.error == nil {
99101
p, err := appendPath(subRes.path)
100102
emitOnceResult(ctx, out, onceResult{value: p, err: err})
103+
// Return without waiting for rootRes, since this result
104+
// (for "_dnslink."+fqdn) takes precedence
101105
return
102106
}
107+
subResErr = subRes.error
103108
case rootRes, ok := <-rootChan:
104109
if !ok {
105110
rootChan = nil
@@ -108,11 +113,24 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
108113
if rootRes.error == nil {
109114
p, err := appendPath(rootRes.path)
110115
emitOnceResult(ctx, out, onceResult{value: p, err: err})
116+
// Do not return here. Wait for subRes so that it is
117+
// output last if good, thereby giving subRes precedence.
118+
} else {
119+
rootResErr = rootRes.error
111120
}
112121
case <-ctx.Done():
113122
return
114123
}
115124
if subChan == nil && rootChan == nil {
125+
// If here, then both lookups are done
126+
//
127+
// If both lookups failed due to no TXT records with a
128+
// dnslink, then output a more specific error message
129+
if rootResErr == ErrResolveFailed && subResErr == ErrResolveFailed {
130+
// Wrap error so that it can be tested if it is a ErrResolveFailed
131+
err := fmt.Errorf("%w: %q is missing a DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, gpath.Base(name))
132+
emitOnceResult(ctx, out, onceResult{err: err})
133+
}
116134
return
117135
}
118136
}
@@ -126,7 +144,14 @@ func workDomain(r *DNSResolver, name string, res chan lookupRes) {
126144

127145
txt, err := r.lookupTXT(name)
128146
if err != nil {
129-
// Error is != nil
147+
if dnsErr, ok := err.(*net.DNSError); ok {
148+
// If no TXT records found, return same error as when no text
149+
// records contain dnslink. Otherwise, return the actual error.
150+
if dnsErr.IsNotFound {
151+
err = ErrResolveFailed
152+
}
153+
}
154+
// Could not look up any text records for name
130155
res <- lookupRes{"", err}
131156
return
132157
}
@@ -138,6 +163,8 @@ func workDomain(r *DNSResolver, name string, res chan lookupRes) {
138163
return
139164
}
140165
}
166+
167+
// There were no TXT records with a dnslink
141168
res <- lookupRes{"", ErrResolveFailed}
142169
}
143170

0 commit comments

Comments
 (0)