dns

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2018 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Query    = 0
	Response = 1
)

Message types

View Source
const (
	QTypeA    uint16 = 1  //ipv4
	QTypeAAAA uint16 = 28 ///ipv6
)

QType .

View Source
const ClassINET uint16 = 1

ClassINET .

View Source
const HeaderLen = 12

HeaderLen is the length of dns msg header

View Source
const LongTTL = 50 * 365 * 24 * 3600

LongTTL is 50 years duration in seconds, used for none-expired items

View Source
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

func MarshalDomain(domain string) []byte

MarshalDomain marshals domain string struct to []byte

func UnmarshalHeader added in v0.6.5

func UnmarshalHeader(b []byte, h *Header) error

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

func NewCache added in v0.6.5

func NewCache() (c *Cache)

NewCache returns a new cache

func (*Cache) Get added in v0.6.5

func (c *Cache) Get(k string) (v []byte)

Get an item from cache

func (*Cache) Len added in v0.6.5

func (c *Cache) Len() int

Len returns the length of cache

func (*Cache) Put added in v0.6.5

func (c *Cache) Put(k string, v []byte, ttl int)

Put an item into cache, invalid after ttl seconds

type Client added in v0.6.5

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

Client is a dns client struct

func NewClient added in v0.6.5

func NewClient(dialer proxy.Dialer, config *Config) (*Client, error)

NewClient returns a new dns client

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

func (c *Client) AddRecord(record string) error

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

func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([]byte, error)

Exchange handles request msg and returns response msg reqBytes = reqLen + reqMsg

func (*Client) GenResponse added in v0.6.5

func (c *Client) GenResponse(domain string, ip string) (*Message, error)

GenResponse generates a dns response message for the given domani an ip address

func (*Client) GetServers added in v0.6.6

func (c *Client) GetServers(domain string) []string

GetServers gets upstream dns servers for the given domain

func (*Client) SetServers added in v0.6.7

func (c *Client) SetServers(domain string, servers ...string)

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

type HandleFunc func(Domain, ip string) error

HandleFunc function handles the dns TypeA or TypeAAAA answer

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) Marshal added in v0.6.5

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

Marshal marshals header struct to []byte

func (*Header) SetAncount added in v0.6.5

func (h *Header) SetAncount(ancount int)

SetAncount sets answers count

func (*Header) SetMsgType added in v0.6.5

func (h *Header) SetMsgType(qr int)

SetMsgType .

func (*Header) SetQdcount added in v0.6.5

func (h *Header) SetQdcount(qdcount int)

SetQdcount sets query count, most dns servers only support 1 query per request

func (*Header) SetTC added in v0.6.5

func (h *Header) SetTC(tc int)

SetTC .

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

func NewMessage(id uint16, msgType int) *Message

NewMessage returns a new message

func UnmarshalMessage added in v0.6.5

func UnmarshalMessage(b []byte) (*Message, error)

UnmarshalMessage unmarshals []bytes to Message

func (*Message) AddAnswer added in v0.6.5

func (m *Message) AddAnswer(rr *RR) error

AddAnswer adds an answer to dns message

func (*Message) Marshal added in v0.6.5

func (m *Message) Marshal() ([]byte, error)

Marshal marshals message struct to []byte

func (*Message) SetQuestion added in v0.6.5

func (m *Message) SetQuestion(q *Question) error

SetQuestion sets a question to dns message,

func (*Message) UnmarshalDomain added in v0.6.5

func (m *Message) UnmarshalDomain(b []byte) (string, int, error)

UnmarshalDomain gets domain from bytes

func (*Message) UnmarshalDomainPoint added in v0.6.5

func (m *Message) UnmarshalDomainPoint(offset int) (string, error)

UnmarshalDomainPoint gets domain from offset point

func (*Message) UnmarshalQuestion added in v0.6.5

func (m *Message) UnmarshalQuestion(b []byte, q *Question) (n int, err error)

UnmarshalQuestion unmarshals []bytes to Question

func (*Message) UnmarshalRR added in v0.6.5

func (m *Message) UnmarshalRR(start int, rr *RR) (n int, err error)

UnmarshalRR unmarshals []bytes to RR

type Question

type Question struct {
	QNAME  string
	QTYPE  uint16
	QCLASS uint16
}

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

func NewQuestion(qtype uint16, domain string) *Question

NewQuestion returns a new dns question

func (*Question) Marshal added in v0.6.5

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

Marshal marshals Question struct to []byte

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                     /
/                                               /
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

func NewRR added in v0.6.5

func NewRR() *RR

NewRR returns a new dns rr

func (*RR) Marshal added in v0.6.5

func (rr *RR) Marshal() ([]byte, error)

Marshal marshals RR struct to []byte

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 NewServer added in v0.6.5

func NewServer(addr string, dialer proxy.Dialer, config *Config) (*Server, error)

NewServer returns a new dns server

func (*Server) ListenAndServeTCP added in v0.6.5

func (s *Server) ListenAndServeTCP(wg *sync.WaitGroup)

ListenAndServeTCP .

func (*Server) ListenAndServeUDP added in v0.6.5

func (s *Server) ListenAndServeUDP(wg *sync.WaitGroup)

ListenAndServeUDP .

func (*Server) ServeTCP added in v0.6.5

func (s *Server) ServeTCP(c net.Conn)

ServeTCP .

func (*Server) Start added in v0.6.7

func (s *Server) Start()

Start .

Jump to

Keyboard shortcuts

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