dns

package
v0.0.0-...-e551d31 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package dns provides a DNS server for resolving services against.

Index

Constants

This section is empty.

Variables

View Source
var MetricsFactory = promauto.With(MetricsRegistry)
View Source
var MetricsRegistry = prometheus.NewRegistry()

MetricsRegistry is the metrics registry in which all DNS metrics are registered.

Functions

func IsSubDomain

func IsSubDomain(parent, child string) bool

IsSubDomain returns true if child is the same as or a subdomain of parent. Both names should be in canonical form.

func ParseReverse

func ParseReverse(name string) (ip netip.Addr, bits int, extra bool)

ParseReverse parses name as a reverse lookup name. If name is not a reverse name, the returned IP is invalid. The second return value indicates how many bits of the address are present. The third return value is true if there are extra labels before the reverse name.

func SplitLastLabel

func SplitLastLabel(name string) (rest string, label string)

SplitLastLabel splits off the last label of a domain name. For example, "www.example.com." is split into "www.example." and "com".

Types

type EmptyDNSHandler

type EmptyDNSHandler struct{}

EmptyDNSHandler is a handler that does not handle any queries. It can be used as a placeholder with SetHandler when a handler is inactive.

func (EmptyDNSHandler) HandleDNS

func (EmptyDNSHandler) HandleDNS(*Request)

type Handler

type Handler interface {
	HandleDNS(r *Request)
}

Handler can handle DNS requests. The handler should first inspect the query and decide if it wants to handle it. If not, it should return immediately. The next handler will then be tried. Otherwise, it should fill in the Reply, and then call SendReply. The Answer section may already contain CNAMEs that have been followed.

type Request

type Request struct {
	// Reply is the reply that will be sent, and should be filled in by the
	// handler. It is guaranteed to contain exactly one question.
	Reply *dns.Msg
	// Writer will be used to send the reply, and contains network information.
	Writer dns.ResponseWriter

	// Qopt is the OPT record from the query, or nil if not present.
	Qopt *dns.OPT
	// Ropt, if non-nil, is the OPT record that will be added to the reply. The
	// handler can modify this as needed. Ropt is nil when Qopt is nil.
	Ropt *dns.OPT

	// Qname contains the current question name. This is different from the
	// original question in Reply.Question[0].Name if a CNAME has been followed
	// already.
	Qname string

	// QnameCanonical contains the canonicalized name of the question. This means
	// that ASCII letters are lowercased.
	QnameCanonical string

	// Qtype contains the question type for convenient access.
	Qtype uint16

	// Handled is set to true when the current question name has been handled and
	// no other handlers should be attempted.
	Handled bool
	// contains filtered or unexported fields
}

Request represents an incoming DNS query that is being handled.

func CreateTestRequest

func CreateTestRequest(qname string, qtype uint16, proto string) *Request

CreateTestRequest creates a Request for use in tests.

func (*Request) AddCNAME

func (r *Request) AddCNAME(target string, ttl uint32)

AddCNAME adds a CNAME record to the answer section, and either sends the reply if the query is for the CNAME itself, or else marks the lookup to be restarted at the new name. target must be fully qualified.

func (*Request) AddExtendedError

func (r *Request) AddExtendedError(infoCode uint16, extraText string)

AddExtendedError adds an Extended DNS Error Option if the reply has an OPT. See RFC 8914.

func (*Request) SendRcode

func (r *Request) SendRcode(rcode int)

SendRcode sets the reply RCODE and sends the reply.

func (*Request) SendReply

func (r *Request) SendReply()

SendReply sends the reply. It may only be called once.

func (*Request) SetAuthoritative

func (r *Request) SetAuthoritative()

SetAuthoritative marks the reply as authoritative.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service is a DNS server service with configurable handlers.

The number and names of handlers is fixed when New is called. For each name in handlerNames there is a corresponding pointer to a handler in the handlers slice at the same index, which can be atomically updated at runtime through its atomic.Pointer via the SetHandler function.

func New

func New(handlerNames []string) *Service

New creates a Service instance. DNS handlers with the names given in handlerNames must be set with SetHandler. When serving DNS queries, they will be tried in the order they appear here. Doing it this way instead of directly passing a []Handler avoids circular dependencies.

func (*Service) HandleDNS

func (s *Service) HandleDNS(r *Request)

func (*Service) Run

func (s *Service) Run(ctx context.Context) error

Run runs the DNS service.

func (*Service) RunListenerAddr

func (s *Service) RunListenerAddr(ctx context.Context, addr string) error

RunListenerAddr runs a DNS listener on a specific address.

func (*Service) SetHandler

func (s *Service) SetHandler(name string, h Handler)

SetHandler sets the handler of the given name. This name must have been registered when creating the Service. As long as SetHandler has not been called for a registered name, any queries that are not already handled by an earlier handler in the sequence return SERVFAIL. SetHandler may be called multiple times, each call replaces the previous handler of the same name.

Directories

Path Synopsis
Package forward implements a forwarding proxy.
Package forward implements a forwarding proxy.
cache
Package cache implements a cache.
Package cache implements a cache.
proxy
Package proxy implements a forwarding proxy.
Package proxy implements a forwarding proxy.
up
Package up is used to run a function for some duration.
Package up is used to run a function for some duration.
Package kubernetes provides the kubernetes backend.
Package kubernetes provides the kubernetes backend.
object
Package object holds functions that convert the objects from the k8s API in to a more memory efficient structures.
Package object holds functions that convert the objects from the k8s API in to a more memory efficient structures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL