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:
parent
7a0a156aec
commit
01a513a496
35 changed files with 1694 additions and 1039 deletions
|
|
@ -5,7 +5,10 @@ package netxlite
|
|||
//
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
|
|
@ -23,18 +26,82 @@ const (
|
|||
dnsDNSSECEnabled = true
|
||||
)
|
||||
|
||||
func (e *DNSEncoderMiekg) Encode(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
// Encoder implements model.DNSEncoder.Encode.
|
||||
func (e *DNSEncoderMiekg) Encode(domain string, qtype uint16, padding bool) model.DNSQuery {
|
||||
return &dnsQuery{
|
||||
bytesCalls: &atomicx.Int64{},
|
||||
domain: domain,
|
||||
kind: qtype,
|
||||
id: dns.Id(),
|
||||
memoizedBytes: []byte{},
|
||||
mu: sync.Mutex{},
|
||||
padding: padding,
|
||||
}
|
||||
}
|
||||
|
||||
// dnsQuery implements model.DNSQuery.
|
||||
type dnsQuery struct {
|
||||
// bytesCalls counts the calls to the bytes() method
|
||||
bytesCalls *atomicx.Int64
|
||||
|
||||
// domain is the domain.
|
||||
domain string
|
||||
|
||||
// kind is the query type.
|
||||
kind uint16
|
||||
|
||||
// id is the query ID.
|
||||
id uint16
|
||||
|
||||
// memoizedBytes contains the query encoded as bytes. We only fill
|
||||
// this field the first time the Bytes method is called.
|
||||
memoizedBytes []byte
|
||||
|
||||
// mu provides mutual exclusion.
|
||||
mu sync.Mutex
|
||||
|
||||
// padding indicates whether we need padding.
|
||||
padding bool
|
||||
}
|
||||
|
||||
// Domain implements model.DNSQuery.Domain.
|
||||
func (q *dnsQuery) Domain() string {
|
||||
return q.domain
|
||||
}
|
||||
|
||||
// Type implements model.DNSQuery.Type.
|
||||
func (q *dnsQuery) Type() uint16 {
|
||||
return q.kind
|
||||
}
|
||||
|
||||
// Bytes implements model.DNSQuery.Bytes.
|
||||
func (q *dnsQuery) Bytes() ([]byte, error) {
|
||||
defer q.mu.Unlock()
|
||||
q.mu.Lock()
|
||||
if len(q.memoizedBytes) <= 0 {
|
||||
q.bytesCalls.Add(1) // for testing
|
||||
data, err := q.bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q.memoizedBytes = data
|
||||
}
|
||||
return q.memoizedBytes, nil
|
||||
}
|
||||
|
||||
// bytes is the unmemoized implementation of Bytes
|
||||
func (q *dnsQuery) bytes() ([]byte, error) {
|
||||
question := dns.Question{
|
||||
Name: dns.Fqdn(domain),
|
||||
Qtype: qtype,
|
||||
Name: dns.Fqdn(q.domain),
|
||||
Qtype: q.kind,
|
||||
Qclass: dns.ClassINET,
|
||||
}
|
||||
query := new(dns.Msg)
|
||||
query.Id = dns.Id()
|
||||
query.Id = q.id
|
||||
query.RecursionDesired = true
|
||||
query.Question = make([]dns.Question, 1)
|
||||
query.Question[0] = question
|
||||
if padding {
|
||||
if q.padding {
|
||||
query.SetEdns0(dnsEDNS0MaxResponseSize, dnsDNSSECEnabled)
|
||||
// Clients SHOULD pad queries to the closest multiple of
|
||||
// 128 octets RFC8467#section-4.1. We inflate the query
|
||||
|
|
@ -47,8 +114,13 @@ func (e *DNSEncoderMiekg) Encode(domain string, qtype uint16, padding bool) ([]b
|
|||
opt.Padding = make([]byte, remainder)
|
||||
query.IsEdns0().Option = append(query.IsEdns0().Option, opt)
|
||||
}
|
||||
data, err := query.Pack()
|
||||
return data, query.Id, err
|
||||
return query.Pack()
|
||||
}
|
||||
|
||||
// ID implements model.DNSQuery.ID
|
||||
func (q *dnsQuery) ID() uint16 {
|
||||
return q.id
|
||||
}
|
||||
|
||||
var _ model.DNSEncoder = &DNSEncoderMiekg{}
|
||||
var _ model.DNSQuery = &dnsQuery{}
|
||||
|
|
|
|||
Loading…
Reference in a new issue