...
1 package dns
2
3 import (
4 "encoding/hex"
5 "strconv"
6 )
7
8 const (
9 year68 = 1 << 31
10 defaultTtl = 3600
11
12
13 DefaultMsgSize = 4096
14
15 MinMsgSize = 512
16
17 MaxMsgSize = 65535
18 )
19
20
21 type Error struct{ err string }
22
23 func (e *Error) Error() string {
24 if e == nil {
25 return "dns: <nil>"
26 }
27 return "dns: " + e.err
28 }
29
30
31 type RR interface {
32
33
34 Header() *RR_Header
35
36 String() string
37
38
39 copy() RR
40
41
42
43
44
45 len(off int, compression map[string]struct{}) int
46
47
48
49 pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error)
50
51
52
53
54
55 unpack(msg []byte, off int) (off1 int, err error)
56
57
58
59
60 parse(c *zlexer, origin string) *ParseError
61
62
63 isDuplicate(r2 RR) bool
64 }
65
66
67 type RR_Header struct {
68 Name string `dns:"cdomain-name"`
69 Rrtype uint16
70 Class uint16
71 Ttl uint32
72 Rdlength uint16
73 }
74
75
76 func (h *RR_Header) Header() *RR_Header { return h }
77
78
79 func (h *RR_Header) copy() RR { return nil }
80
81 func (h *RR_Header) String() string {
82 var s string
83
84 if h.Rrtype == TypeOPT {
85 s = ";"
86
87 }
88
89 s += sprintName(h.Name) + "\t"
90 s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
91 s += Class(h.Class).String() + "\t"
92 s += Type(h.Rrtype).String() + "\t"
93 return s
94 }
95
96 func (h *RR_Header) len(off int, compression map[string]struct{}) int {
97 l := domainNameLen(h.Name, off, compression, true)
98 l += 10
99 return l
100 }
101
102 func (h *RR_Header) pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error) {
103
104 return off, nil
105 }
106
107 func (h *RR_Header) unpack(msg []byte, off int) (int, error) {
108 panic("dns: internal error: unpack should never be called on RR_Header")
109 }
110
111 func (h *RR_Header) parse(c *zlexer, origin string) *ParseError {
112 panic("dns: internal error: parse should never be called on RR_Header")
113 }
114
115
116 func (rr *RFC3597) ToRFC3597(r RR) error {
117 buf := make([]byte, Len(r))
118 headerEnd, off, err := packRR(r, buf, 0, compressionMap{}, false)
119 if err != nil {
120 return err
121 }
122 buf = buf[:off]
123
124 *rr = RFC3597{Hdr: *r.Header()}
125 rr.Hdr.Rdlength = uint16(off - headerEnd)
126
127 if noRdata(rr.Hdr) {
128 return nil
129 }
130
131 _, err = rr.unpack(buf, headerEnd)
132 return err
133 }
134
135
136 func (rr *RFC3597) fromRFC3597(r RR) error {
137 hdr := r.Header()
138 *hdr = rr.Hdr
139
140
141
142 hdr.Rdlength = uint16(hex.DecodedLen(len(rr.Rdata)))
143
144 if noRdata(*hdr) {
145
146 return nil
147 }
148
149
150
151 msg, err := hex.DecodeString(rr.Rdata)
152 if err != nil {
153 return err
154 }
155
156 _, err = r.unpack(msg, 0)
157 return err
158 }
159
View as plain text