2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-15 19:25:27 +02:00
|
|
|
//
|
|
|
|
// DNS-over-UDP transport
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
2022-01-03 13:53:23 +01:00
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-05-14 17:38:31 +02:00
|
|
|
// DNSOverUDPTransport is a DNS-over-UDP DNSTransport.
|
|
|
|
type DNSOverUDPTransport struct {
|
2022-01-03 13:53:23 +01:00
|
|
|
dialer model.Dialer
|
2022-05-25 17:03:58 +02:00
|
|
|
decoder model.DNSDecoder
|
2021-02-02 12:05:47 +01:00
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
2022-05-14 17:38:31 +02:00
|
|
|
// NewDNSOverUDPTransport creates a DNSOverUDPTransport instance.
|
2021-09-29 20:21:25 +02:00
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// - dialer is any type that implements the Dialer interface;
|
|
|
|
//
|
|
|
|
// - address is the endpoint address (e.g., 8.8.8.8:53).
|
2022-05-14 17:38:31 +02:00
|
|
|
func NewDNSOverUDPTransport(dialer model.Dialer, address string) *DNSOverUDPTransport {
|
2022-05-25 17:03:58 +02:00
|
|
|
return &DNSOverUDPTransport{
|
|
|
|
dialer: dialer,
|
|
|
|
decoder: &DNSDecoderMiekg{},
|
|
|
|
address: address,
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// RoundTrip sends a query and receives a reply.
|
2022-05-25 17:03:58 +02:00
|
|
|
func (t *DNSOverUDPTransport) RoundTrip(
|
|
|
|
ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
rawQuery, err := query.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, err := t.dialer.DialContext(ctx, "udp", t.address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
// Use five seconds timeout like Bionic does. See
|
|
|
|
// https://labs.ripe.net/Members/baptiste_jonglez_1/persistent-dns-connections-for-reliability-and-performance
|
2022-05-25 17:03:58 +02:00
|
|
|
const iotimeout = 5 * time.Second
|
|
|
|
conn.SetDeadline(time.Now().Add(iotimeout))
|
|
|
|
if _, err = conn.Write(rawQuery); err != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
const maxmessagesize = 1 << 17
|
|
|
|
rawResponse := make([]byte, maxmessagesize)
|
|
|
|
count, err := conn.Read(rawResponse)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
rawResponse = rawResponse[:count]
|
|
|
|
return t.decoder.DecodeResponse(rawResponse, query)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// RequiresPadding returns false for UDP according to RFC8467.
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverUDPTransport) RequiresPadding() bool {
|
2021-02-02 12:05:47 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// Network returns the transport network, i.e., "udp".
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverUDPTransport) Network() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return "udp"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the upstream server address.
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverUDPTransport) Address() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:21:25 +02:00
|
|
|
// CloseIdleConnections closes idle connections, if any.
|
2022-05-14 17:38:31 +02:00
|
|
|
func (t *DNSOverUDPTransport) CloseIdleConnections() {
|
2021-09-09 20:49:12 +02:00
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
2022-05-14 17:38:31 +02:00
|
|
|
var _ model.DNSTransport = &DNSOverUDPTransport{}
|