Documentation ¶
Index ¶
- Constants
- func MarshalDomain(domain string) []byte
- func UnmarshalHeader(b []byte, h *Header) error
- type Cache
- type Client
- func (c *Client) AddHandler(h HandleFunc)
- func (c *Client) AddRecord(record string) error
- func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([]byte, error)
- func (c *Client) GenResponse(domain string, ip string) (*Message, error)
- func (c *Client) GetServers(domain string) []string
- func (c *Client) SetServers(domain string, servers ...string)
- type Config
- type HandleFunc
- type Header
- type Message
- func (m *Message) AddAnswer(rr *RR) error
- func (m *Message) Marshal() ([]byte, error)
- func (m *Message) SetQuestion(q *Question) error
- func (m *Message) UnmarshalDomain(b []byte) (string, int, error)
- func (m *Message) UnmarshalDomainPoint(offset int) (string, error)
- func (m *Message) UnmarshalQuestion(b []byte, q *Question) (n int, err error)
- func (m *Message) UnmarshalRR(start int, rr *RR) (n int, err error)
- type Question
- type RR
- type Server
Constants ¶
const ( Query = 0 Response = 1 )
Message types
const ( QTypeA uint16 = 1 //ipv4 QTypeAAAA uint16 = 28 ///ipv6 )
Query types
const ClassINET uint16 = 1
ClassINET .
const HeaderLen = 12
HeaderLen is the length of dns msg header.
const LongTTL = 50 * 365 * 24 * 3600
LongTTL is 50 years duration in seconds, used for none-expired items.
const UDPMaxLen = 512
UDPMaxLen is the max size of udp dns request. https://tools.ietf.org/html/rfc1035#section-4.2.1 Messages carried by UDP are restricted to 512 bytes (not counting the IP or UDP headers). Longer messages are truncated and the TC bit is set in the header.
Variables ¶
This section is empty.
Functions ¶
func MarshalDomain ¶ added in v0.6.5
MarshalDomain marshals domain string struct to []byte.
func UnmarshalHeader ¶ added in v0.6.5
UnmarshalHeader unmarshals []bytes to Header.
Types ¶
type Cache ¶ added in v0.6.5
type Cache struct {
// contains filtered or unexported fields
}
Cache is the struct of cache.
type Client ¶ added in v0.6.5
type Client struct {
// contains filtered or unexported fields
}
Client is a dns client struct.
func (*Client) AddHandler ¶ added in v0.6.5
func (c *Client) AddHandler(h HandleFunc)
AddHandler adds a custom handler to handle the resolved result (A and AAAA).
func (*Client) AddRecord ¶ added in v0.6.5
AddRecord adds custom record to dns cache, format: www.example.com/1.2.3.4 or www.example.com/2606:2800:220:1:248:1893:25c8:1946
func (*Client) Exchange ¶ added in v0.6.5
Exchange handles request message and returns response message. reqBytes = reqLen + reqMsg
func (*Client) GenResponse ¶ added in v0.6.5
GenResponse generates a dns response message for the given domain and ip address.
func (*Client) GetServers ¶ added in v0.6.6
GetServers gets upstream dns servers for the given domain
func (*Client) SetServers ¶ added in v0.6.7
SetServers sets upstream dns servers for the given domain.
type Config ¶ added in v0.6.6
type Config struct { Servers []string Timeout int MaxTTL int MinTTL int Records []string AlwaysTCP bool }
Config for dns.
type HandleFunc ¶ added in v0.6.5
HandleFunc function handles the dns TypeA or TypeAAAA answer.
type Header ¶ added in v0.6.5
type Header struct { ID uint16 Bits uint16 QDCOUNT uint16 ANCOUNT uint16 NSCOUNT uint16 ARCOUNT uint16 }
Header format https://tools.ietf.org/html/rfc1035#section-4.1.1 The header contains the following fields:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA| Z | RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
func (*Header) SetAncount ¶ added in v0.6.5
SetAncount sets answers count.
func (*Header) SetMsgType ¶ added in v0.6.5
SetMsgType sets the message type.
func (*Header) SetQdcount ¶ added in v0.6.5
SetQdcount sets query count, most dns servers only support 1 query per request.
type Message ¶ added in v0.6.5
type Message struct { Header // most dns implementation only support 1 question Question *Question Answers []*RR Authority []*RR Additional []*RR // contains filtered or unexported fields }
Message format https://tools.ietf.org/html/rfc1035#section-4.1 All communications inside of the domain protocol are carried in a single format called a message. The top level format of message is divided into 5 sections (some of which are empty in certain cases) shown below:
+---------------------+ | Header | +---------------------+ | Question | the question for the name server +---------------------+ | Answer | RRs answering the question +---------------------+ | Authority | RRs pointing toward an authority +---------------------+ | Additional | RRs holding additional information
func NewMessage ¶ added in v0.6.5
NewMessage returns a new message.
func UnmarshalMessage ¶ added in v0.6.5
UnmarshalMessage unmarshals []bytes to Message.
func (*Message) SetQuestion ¶ added in v0.6.5
SetQuestion sets a question to dns message.
func (*Message) UnmarshalDomain ¶ added in v0.6.5
UnmarshalDomain gets domain from bytes.
func (*Message) UnmarshalDomainPoint ¶ added in v0.6.5
UnmarshalDomainPoint gets domain from offset point.
func (*Message) UnmarshalQuestion ¶ added in v0.6.5
UnmarshalQuestion unmarshals []bytes to Question.
type Question ¶
Question format https://tools.ietf.org/html/rfc1035#section-4.1.2 The question section is used to carry the "question" in most queries, i.e., the parameters that define what is being asked. The section contains QDCOUNT (usually 1) entries, each of the following format:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | / QNAME / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QTYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QCLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
func NewQuestion ¶ added in v0.6.5
NewQuestion returns a new dns question.
type RR ¶
type RR struct { NAME string TYPE uint16 CLASS uint16 TTL uint32 RDLENGTH uint16 RDATA []byte IP string }
RR format https://tools.ietf.org/html/rfc1035#section-3.2.1 https://tools.ietf.org/html/rfc1035#section-4.1.3 The answer, authority, and additional sections all share the same format: a variable number of resource records, where the number of records is specified in the corresponding count field in the header. Each resource record has the following format:
1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | / / / NAME / | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | TYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | CLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | TTL | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | RDLENGTH | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| / RDATA / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
type Server ¶ added in v0.6.5
type Server struct { // Client is used to communicate with upstream dns servers *Client // contains filtered or unexported fields }
Server is a dns server struct.
func (*Server) ListenAndServeTCP ¶ added in v0.6.5
ListenAndServeTCP listen and serves on tcp port.
func (*Server) ListenAndServeUDP ¶ added in v0.6.5
ListenAndServeUDP listen and serves on udp port.