resolve

package module
v0.0.0-...-697c8b8 Latest Latest
Warning

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

Go to latest
Published: May 14, 2023 License: MIT Imports: 9 Imported by: 0

README

resolve

A toy DNS resolver. It's a Go port of Julia Evans' Implement DNS in a weekend guide.

Install or upgrade:

go install github.com/clfs/resolve/cmd/resolve@latest

Uninstall:

rm -i $(which resolve)

Usage:

$ resolve
Usage of resolve:
  -domain string
        domain to lookup
  -record-type string
        record type to lookup (default "A")

Example:

$ resolve -domain twitter.com
2023/05/14 16:39:50 querying 198.41.0.4 for twitter.com
2023/05/14 16:39:50 querying 192.12.94.30 for twitter.com
2023/05/14 16:39:50 querying 198.41.0.4 for a.r06.twtrdns.net
2023/05/14 16:39:50 querying 192.12.94.30 for a.r06.twtrdns.net
2023/05/14 16:39:50 querying 205.251.195.207 for a.r06.twtrdns.net
2023/05/14 16:39:50 querying 205.251.192.179 for twitter.com
2023/05/14 16:39:50 104.244.42.65

Documentation

Overview

Package resolve implements a toy DNS resolver.

Index

Constants

View Source
const (
	FlagRecursionDesired uint16 = 1 << 8
)

Flag constants.

View Source
const RootNSIP = "198.41.0.4"

Variables

This section is empty.

Functions

func DecodeCompressedName

func DecodeCompressedName(length int, r io.ReadSeeker) ([]byte, error)

DecodeCompressedName decodes a compressed DNS name.

func DecodeName

func DecodeName(r io.ReadSeeker) ([]byte, error)

DecodeName decodes a DNS name.

func EncodeDNSName

func EncodeDNSName(s string) []byte

EncodeDNSName encodes a domain name for DNS.

func ID

func ID() uint16

ID returns a random query ID.

func LookupDomain

func LookupDomain(name string) (netip.Addr, error)

func NewQuery

func NewQuery(domain string, t Type) ([]byte, error)

NewQuery returns a new DNS query for a domain name and record type.

func Resolve

func Resolve(domain string, t Type) (netip.Addr, error)

Types

type Class

type Class uint16

A Class is a DNS record class.

const ClassIN Class = 1
type Header struct {
	ID             uint16
	Flags          uint16
	NumQuestions   uint16
	NumAnswers     uint16
	NumAuthorities uint16
	NumAdditionals uint16
}

Header is a DNS header.

func DecodeHeader

func DecodeHeader(r io.Reader) (Header, error)

DecodeHeader decodes a DNS header.

func (*Header) MarshalBinary

func (h *Header) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler for Header.

func (*Header) UnmarshalBinary

func (h *Header) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler for Header.

type Packet

type Packet struct {
	Header      Header
	Questions   []Question
	Answers     []Record
	Authorities []Record
	Additionals []Record
}

Packet represents a DNS packet.

func DecodePacket

func DecodePacket(r io.ReadSeeker) (*Packet, error)

DecodePacket decodes a DNS packet.

func SendQuery

func SendQuery(address, domain string, t Type) (*Packet, error)

func (Packet) Answer

func (p Packet) Answer() (netip.Addr, error)

Answer returns the IP from the first A record in the Answer section.

func (Packet) Nameserver

func (p Packet) Nameserver() (string, error)

Nameserver returns the domain from the first NS record in the Authority section.

func (Packet) NameserverIP

func (p Packet) NameserverIP() (netip.Addr, error)

NameserverIP returns the IP from the first A record in the Additional section.

type Question

type Question struct {
	Name  []byte
	Type  Type
	Class Class
}

Question is a DNS question.

func DecodeQuestion

func DecodeQuestion(r io.ReadSeeker) (Question, error)

DecodeQuestion decodes a DNS question.

func (*Question) MarshalBinary

func (q *Question) MarshalBinary() ([]byte, error)

type Record

type Record struct {
	Name  []byte
	Type  Type
	Class Class
	TTL   uint32
	Data  []byte
}

Record represents a DNS record.

func DecodeRecord

func DecodeRecord(r io.ReadSeeker) (Record, error)

DecodeRecord decodes a DNS record.

type Type

type Type uint16

A Type is a DNS record type.

const (
	TypeA  Type = 1
	TypeNS Type = 2
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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