Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / miekg / dns / dns.go
1 package dns
2
3 import "strconv"
4
5 const (
6         year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
7         // DefaultMsgSize is the standard default for messages larger than 512 bytes.
8         DefaultMsgSize = 4096
9         // MinMsgSize is the minimal size of a DNS packet.
10         MinMsgSize = 512
11         // MaxMsgSize is the largest possible DNS packet.
12         MaxMsgSize = 65535
13         defaultTtl = 3600 // Default internal TTL.
14 )
15
16 // Error represents a DNS error
17 type Error struct{ err string }
18
19 func (e *Error) Error() string {
20         if e == nil {
21                 return "dns: <nil>"
22         }
23         return "dns: " + e.err
24 }
25
26 // An RR represents a resource record.
27 type RR interface {
28         // Header returns the header of an resource record. The header contains
29         // everything up to the rdata.
30         Header() *RR_Header
31         // String returns the text representation of the resource record.
32         String() string
33         // copy returns a copy of the RR
34         copy() RR
35         // len returns the length (in octets) of the uncompressed RR in wire format.
36         len() int
37 }
38
39 // RR_Header is the header all DNS resource records share.
40 type RR_Header struct {
41         Name     string `dns:"cdomain-name"`
42         Rrtype   uint16
43         Class    uint16
44         Ttl      uint32
45         Rdlength uint16 // length of data after header
46 }
47
48 // Header returns itself. This is here to make RR_Header implement the RR interface.
49 func (h *RR_Header) Header() *RR_Header { return h }
50
51 // Just to imlement the RR interface.
52 func (h *RR_Header) copy() RR { return nil }
53
54 func (h *RR_Header) copyHeader() *RR_Header {
55         r := new(RR_Header)
56         r.Name = h.Name
57         r.Rrtype = h.Rrtype
58         r.Class = h.Class
59         r.Ttl = h.Ttl
60         r.Rdlength = h.Rdlength
61         return r
62 }
63
64 func (h *RR_Header) String() string {
65         var s string
66
67         if h.Rrtype == TypeOPT {
68                 s = ";"
69                 // and maybe other things
70         }
71
72         s += sprintName(h.Name) + "\t"
73         s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
74         s += Class(h.Class).String() + "\t"
75         s += Type(h.Rrtype).String() + "\t"
76         return s
77 }
78
79 func (h *RR_Header) len() int {
80         l := len(h.Name) + 1
81         l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
82         return l
83 }
84
85 // ToRFC3597 converts a known RR to the unknown RR representation
86 // from RFC 3597.
87 func (rr *RFC3597) ToRFC3597(r RR) error {
88         buf := make([]byte, r.len()*2)
89         off, err := PackStruct(r, buf, 0)
90         if err != nil {
91                 return err
92         }
93         buf = buf[:off]
94         rawSetRdlength(buf, 0, off)
95         _, err = UnpackStruct(rr, buf, 0)
96         if err != nil {
97                 return err
98         }
99         return nil
100 }