refactor: DNSTransport I/Os DNS messages (#760)

This diff refactors the DNSTransport model to receive in input a DNSQuery and return in output a DNSResponse.

The design of DNSQuery and DNSResponse takes into account the use case of a transport using getaddrinfo, meaning that we don't need to serialize and deserialize messages when using getaddrinfo.

The current codebase does not use a getaddrinfo transport, but I wrote one such a transport in the Websteps Winter 2021 prototype (https://github.com/bassosimone/websteps-illustrated/).

The design conversation that lead to producing this diff is https://github.com/ooni/probe/issues/2099
This commit is contained in:
Simone Basso 2022-05-25 17:03:58 +02:00 committed by GitHub
commit 01a513a496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 1694 additions and 1039 deletions

View file

@ -9,7 +9,6 @@ import (
"net"
"github.com/miekg/dns"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/model"
)
@ -19,15 +18,6 @@ import (
// You should probably use NewUnwrappedParallelResolver to
// create a new instance of this type.
type ParallelResolver struct {
// Encoder is the MANDATORY encoder to use.
Encoder model.DNSEncoder
// Decoder is the MANDATORY decoder to use.
Decoder model.DNSDecoder
// NumTimeouts is MANDATORY and counts the number of timeouts.
NumTimeouts *atomicx.Int64
// Txp is the MANDATORY underlying DNS transport.
Txp model.DNSTransport
}
@ -36,10 +26,7 @@ type ParallelResolver struct {
// not wrapped and you should wrap if before using it.
func NewUnwrappedParallelResolver(t model.DNSTransport) *ParallelResolver {
return &ParallelResolver{
Encoder: &DNSEncoderMiekg{},
Decoder: &DNSDecoderMiekg{},
NumTimeouts: &atomicx.Int64{},
Txp: t,
Txp: t,
}
}
@ -80,22 +67,22 @@ func (r *ParallelResolver) LookupHost(ctx context.Context, hostname string) ([]s
var addrs []string
addrs = append(addrs, ares.addrs...)
addrs = append(addrs, aaaares.addrs...)
if len(addrs) < 1 {
return nil, ErrOODNSNoAnswer
}
return addrs, nil
}
// LookupHTTPS implements Resolver.LookupHTTPS.
func (r *ParallelResolver) LookupHTTPS(
ctx context.Context, hostname string) (*model.HTTPSSvc, error) {
querydata, queryID, err := r.Encoder.Encode(
hostname, dns.TypeHTTPS, r.Txp.RequiresPadding())
encoder := &DNSEncoderMiekg{}
query := encoder.Encode(hostname, dns.TypeHTTPS, r.Txp.RequiresPadding())
response, err := r.Txp.RoundTrip(ctx, query)
if err != nil {
return nil, err
}
replydata, err := r.Txp.RoundTrip(ctx, querydata)
if err != nil {
return nil, err
}
return r.Decoder.DecodeHTTPS(replydata, queryID)
return response.DecodeHTTPS()
}
// parallelResolverResult is the internal representation of a
@ -108,7 +95,9 @@ type parallelResolverResult struct {
// lookupHost issues a lookup host query for the specified qtype (e.g., dns.A).
func (r *ParallelResolver) lookupHost(ctx context.Context, hostname string,
qtype uint16, out chan<- *parallelResolverResult) {
querydata, queryID, err := r.Encoder.Encode(hostname, qtype, r.Txp.RequiresPadding())
encoder := &DNSEncoderMiekg{}
query := encoder.Encode(hostname, qtype, r.Txp.RequiresPadding())
response, err := r.Txp.RoundTrip(ctx, query)
if err != nil {
out <- &parallelResolverResult{
addrs: []string{},
@ -116,15 +105,7 @@ func (r *ParallelResolver) lookupHost(ctx context.Context, hostname string,
}
return
}
replydata, err := r.Txp.RoundTrip(ctx, querydata)
if err != nil {
out <- &parallelResolverResult{
addrs: []string{},
err: err,
}
return
}
addrs, err := r.Decoder.DecodeLookupHost(qtype, replydata, queryID)
addrs, err := response.DecodeLookupHost()
out <- &parallelResolverResult{
addrs: addrs,
err: err,
@ -134,14 +115,11 @@ func (r *ParallelResolver) lookupHost(ctx context.Context, hostname string,
// LookupNS implements Resolver.LookupNS.
func (r *ParallelResolver) LookupNS(
ctx context.Context, hostname string) ([]*net.NS, error) {
querydata, queryID, err := r.Encoder.Encode(
hostname, dns.TypeNS, r.Txp.RequiresPadding())
encoder := &DNSEncoderMiekg{}
query := encoder.Encode(hostname, dns.TypeNS, r.Txp.RequiresPadding())
response, err := r.Txp.RoundTrip(ctx, query)
if err != nil {
return nil, err
}
replydata, err := r.Txp.RoundTrip(ctx, querydata)
if err != nil {
return nil, err
}
return r.Decoder.DecodeNS(replydata, queryID)
return response.DecodeNS()
}