dnsutil

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package dnsutil provides a motley collection functions which manipulated and render structs created by the "net" and "github.com/miekg/dns".

Index

Constants

View Source
const (
	V4Suffix = ".in-addr.arpa." // The leading '.' is important here as some callers
	V6Suffix = ".ip6.arpa."     // rely on strings.HasSuffix() to label match.

	TCPNetwork = "tcp" // Yeah, yea, a bit silly, but case is important
	UDPNetwork = "udp" // so having consts here avoids pernickety errors

	MaxUDPSize uint16 = 1232 // Generally suggested as universally safe in edns0
)

Variables

This section is empty.

Functions

func ChompCanonicalName

func ChompCanonicalName(n string) string

ChompCanonicalName makes a name canonical but loses the trailing dot. For logging and mock processing, where zones names are often converted to file names, the trailing dot is more of a hindrance than a help, so this helps.

func ClassToString added in v1.1.0

func ClassToString(c dns.Class) (s string)

ClassToString converts an miekg class to a string, but if the resulting string is empty it's replaced with the numeric value.

func DeducePtr

func DeducePtr(rr dns.RR) (ptr *dns.PTR, key string)

DeducePtr converts a dns.A/AAAA RR into a dns.PTR or returns the supplied RR if it's already a recognizable/convertable PTR. If the wrong type of RR is supplied a nil value is returned. The returned "key" value is effectively the IP address expressed as a string regardless of the RR type. It can be used by callers who want to reference the PTR via the original or extracted IP address.

func IPToReverseQName

func IPToReverseQName(ip net.IP) string

IPToReverseQName converts an IP address into the reverse string normally looked up in the reverse path. It includes the reverse suffix, is fully qualified and is ready for querying.

An empty string is returned if the IP address cannot be parsed.

This is not intended to be a high-speed function.

func InDomain added in v1.4.0

func InDomain(sub, parent string) bool

InDomain returns true if the purported sub-domain is in-domain of the parent domain. This function assumes two relatively well-formed domain names but makes sure they are both Canonical before comparisons are made. In the interest of being "helpful" the parent domain may or may not have a leading "." as that is common for a lot of domain storage in this program.

func InvertPtrToIP

func InvertPtrToIP(qName string) (net.IP, bool, error)

InvertPtrToIP extracts and inverts the purported IP address from a reverse qName. Like any name in the DNS, a reverse qName does not *have* to represent an IP address, but this code ignores all else. Return an error if an IP address cannot be extracted. The return bool is true if the IP address is as valid as far as it goes, but is truncated.

func InvertPtrToIPv4

func InvertPtrToIPv4(qName string) (net.IP, bool, error)

InvertPtrToIPv4 takes the first part of the reverse qName from the ipv4 zone and converts it back into an ipv4 Address, if possible. As a reminder, a dig -x 192.168.1.2 results in a qName of 2.1.168.192.in-addr.arpa. The suffix is removed by the caller leaving just 2.1.168.192. There are of course no guarantees that this string is in reversed IP address format as a rogue query can come in directly with anything in qName, thus all the checking and potential error return if the string doesn't parse.

The returned bool is true if the IP address is valid as far as it goes, but is truncated, e.g. 1.168.192.in-addr.arpa. The reason for converting truncated IPs is so that the caller can distinguish between a malformed address and a truncated one as the former results in an NXDomain and the latter results in a NoError.

func InvertPtrToIPv6

func InvertPtrToIPv6(qName string) (net.IP, bool, error)

InvertPtrToIPv6 takes the first part of the reverse query name, and converts it back into an ipv6 address, if possible. Expected input looks something like: 3.f.6.d.4.d.3.b.c.4.3.0.1.3.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa less the "ip6.arpa" suffix.

The returned bool is true if the IP address is valid as far as it goes, but is truncated, e.g. 0.8.e.f.ip6.arpa returns an ipv6 address of fe80::0 with truncated=true. See the discussion of InvertPtrToIPv4.

func OpcodeToString added in v1.1.0

func OpcodeToString(o int) (s string)

OpcodeToString converts an miekg opcode to a string, but if the resulting string is empty it's replaced with the numeric value.

func PrettyA

func PrettyA(rr *dns.A, includeName bool) (s string)

PrettyA returns a compact representation of a single A RR

func PrettyAAAA

func PrettyAAAA(rr *dns.AAAA, includeName bool) (s string)

PrettyAAAA returns a compact representation of a single AAAA RR

func PrettyAddr

func PrettyAddr(rr dns.RR, includeName bool) (s string)

PrettyAddr returns a compact representation of the single address RR. It can be either an A or an AAAA RR.

func PrettyMsg1

func PrettyMsg1(m *dns.Msg) string

PrettyMsg1 returns a compact string representing the complete message.

func PrettyNS

func PrettyNS(rr *dns.NS, includeName bool) (s string)

PrettyNS returns a compact representation of a single NS RR

func PrettyPTR

func PrettyPTR(rr *dns.PTR, includeName bool) (s string)

PrettyPTR returns a compact representation of a single PTR RR

func PrettyQuestion

func PrettyQuestion(q dns.Question) string

PrettyQuestion returns a compact representation of the dns.Question

func PrettyRR

func PrettyRR(rr dns.RR, includeName bool) string

PrettyRR returns a compact representation of the single RR. Known RR-types use the other pretty functions while unknown RRs use the general rendering offered by miekg.

func PrettyRRSet

func PrettyRRSet(rrs []dns.RR, includeName bool) (s string)

PrettyRRSet returns a compact representation of the slice of RRs. Each RR is separated a comma.

func PrettySOA

func PrettySOA(rr *dns.SOA, includeName bool) (s string)

PrettySOA returns a compact representation of a single SOA RR

func PrettyShortNSSet

func PrettyShortNSSet(rrs []dns.RR) string

PrettyShortNSSet returns just the name server names (RHS) separated by ", "

func RRIsEqual

func RRIsEqual(a, b dns.RR) bool

RRIsEqual returns true if the RRs are "effectively" identical. That means they are identical excepting for TTL. Miekg does not offer an IsEqual() public function that compares the non-header part of an RR so we use the Stringer function and compare them as a string. A bit of a hack as we have to remove the header part of the string to eliminate the TTL, but it works, albeit slowly.

func RcodeToString added in v1.1.0

func RcodeToString(r int) (s string)

RcodeToString converts an miekg rcode to a string, but if the resulting string is empty it's replaced with the numeric value.

func ShortenLookupError

func ShortenLookupError(err error) error

ShortenLookupError turns a long unwieldy error return from net.Resolver into a succinct error in the common cases which don't require the extensive error normally returned.

func SynthesizePTR

func SynthesizePTR(qname, suffix string, ip net.IP) *dns.PTR

SynthesizePTR converts an IP address into a synthetic PTR. Mainly it just string substitutes ":" and "." in ip addresses to "-" which is an acceptable <domain-name> character.

According to rfc1035 Ptr has to hold a <domain-name> which is constrainted to "let-dig-hyp", but I'll bet if the Ptr data contained "." and ":" (which would allow an *exact* representation of the query address) that almost nothing would care. If you want to toy with that idea, set obeyRFC to false.

The suffix parameter is assumed to be canonical.

func TypeToString added in v1.1.0

func TypeToString(t uint16) (s string)

TypeToString converts an miekg type to a string, but if the resulting string is empty it's replaced with the numeric value.

func ValidDelegation

func ValidDelegation(response *dns.Msg) bool

ValidDelegation returns true if the message contains a standard-conforming delegation response.

Strictly, a valid delegation is one which is !Authoritative, has zero Answer RRs, has at least one Ns RR and optional contains glue in Extra.

Types

This section is empty.

Jump to

Keyboard shortcuts

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