defaults.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package dns
  2. import (
  3. "errors"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "unicode"
  8. )
  9. const hexDigit = "0123456789abcdef"
  10. // Everything is assumed in ClassINET.
  11. // SetReply creates a reply message from a request message.
  12. func (dns *Msg) SetReply(request *Msg) *Msg {
  13. dns.Id = request.Id
  14. dns.Response = true
  15. dns.Opcode = request.Opcode
  16. if dns.Opcode == OpcodeQuery {
  17. dns.RecursionDesired = request.RecursionDesired // Copy rd bit
  18. dns.CheckingDisabled = request.CheckingDisabled // Copy cd bit
  19. }
  20. dns.Rcode = RcodeSuccess
  21. if len(request.Question) > 0 {
  22. dns.Question = make([]Question, 1)
  23. dns.Question[0] = request.Question[0]
  24. }
  25. return dns
  26. }
  27. // SetQuestion creates a question message, it sets the Question
  28. // section, generates an Id and sets the RecursionDesired (RD)
  29. // bit to true.
  30. func (dns *Msg) SetQuestion(z string, t uint16) *Msg {
  31. dns.Id = Id()
  32. dns.RecursionDesired = true
  33. dns.Question = make([]Question, 1)
  34. dns.Question[0] = Question{z, t, ClassINET}
  35. return dns
  36. }
  37. // SetNotify creates a notify message, it sets the Question
  38. // section, generates an Id and sets the Authoritative (AA)
  39. // bit to true.
  40. func (dns *Msg) SetNotify(z string) *Msg {
  41. dns.Opcode = OpcodeNotify
  42. dns.Authoritative = true
  43. dns.Id = Id()
  44. dns.Question = make([]Question, 1)
  45. dns.Question[0] = Question{z, TypeSOA, ClassINET}
  46. return dns
  47. }
  48. // SetRcode creates an error message suitable for the request.
  49. func (dns *Msg) SetRcode(request *Msg, rcode int) *Msg {
  50. dns.SetReply(request)
  51. dns.Rcode = rcode
  52. return dns
  53. }
  54. // SetRcodeFormatError creates a message with FormError set.
  55. func (dns *Msg) SetRcodeFormatError(request *Msg) *Msg {
  56. dns.Rcode = RcodeFormatError
  57. dns.Opcode = OpcodeQuery
  58. dns.Response = true
  59. dns.Authoritative = false
  60. dns.Id = request.Id
  61. return dns
  62. }
  63. // SetUpdate makes the message a dynamic update message. It
  64. // sets the ZONE section to: z, TypeSOA, ClassINET.
  65. func (dns *Msg) SetUpdate(z string) *Msg {
  66. dns.Id = Id()
  67. dns.Response = false
  68. dns.Opcode = OpcodeUpdate
  69. dns.Compress = false // BIND9 cannot handle compression
  70. dns.Question = make([]Question, 1)
  71. dns.Question[0] = Question{z, TypeSOA, ClassINET}
  72. return dns
  73. }
  74. // SetIxfr creates message for requesting an IXFR.
  75. func (dns *Msg) SetIxfr(z string, serial uint32, ns, mbox string) *Msg {
  76. dns.Id = Id()
  77. dns.Question = make([]Question, 1)
  78. dns.Ns = make([]RR, 1)
  79. s := new(SOA)
  80. s.Hdr = RR_Header{z, TypeSOA, ClassINET, defaultTtl, 0}
  81. s.Serial = serial
  82. s.Ns = ns
  83. s.Mbox = mbox
  84. dns.Question[0] = Question{z, TypeIXFR, ClassINET}
  85. dns.Ns[0] = s
  86. return dns
  87. }
  88. // SetAxfr creates message for requesting an AXFR.
  89. func (dns *Msg) SetAxfr(z string) *Msg {
  90. dns.Id = Id()
  91. dns.Question = make([]Question, 1)
  92. dns.Question[0] = Question{z, TypeAXFR, ClassINET}
  93. return dns
  94. }
  95. // SetTsig appends a TSIG RR to the message.
  96. // This is only a skeleton TSIG RR that is added as the last RR in the
  97. // additional section. The TSIG is calculated when the message is being send.
  98. func (dns *Msg) SetTsig(z, algo string, fudge uint16, timesigned int64) *Msg {
  99. t := new(TSIG)
  100. t.Hdr = RR_Header{z, TypeTSIG, ClassANY, 0, 0}
  101. t.Algorithm = algo
  102. t.Fudge = fudge
  103. t.TimeSigned = uint64(timesigned)
  104. t.OrigId = dns.Id
  105. dns.Extra = append(dns.Extra, t)
  106. return dns
  107. }
  108. // SetEdns0 appends a EDNS0 OPT RR to the message.
  109. // TSIG should always the last RR in a message.
  110. func (dns *Msg) SetEdns0(udpsize uint16, do bool) *Msg {
  111. e := new(OPT)
  112. e.Hdr.Name = "."
  113. e.Hdr.Rrtype = TypeOPT
  114. e.SetUDPSize(udpsize)
  115. if do {
  116. e.SetDo()
  117. }
  118. dns.Extra = append(dns.Extra, e)
  119. return dns
  120. }
  121. // IsTsig checks if the message has a TSIG record as the last record
  122. // in the additional section. It returns the TSIG record found or nil.
  123. func (dns *Msg) IsTsig() *TSIG {
  124. if len(dns.Extra) > 0 {
  125. if dns.Extra[len(dns.Extra)-1].Header().Rrtype == TypeTSIG {
  126. return dns.Extra[len(dns.Extra)-1].(*TSIG)
  127. }
  128. }
  129. return nil
  130. }
  131. // IsEdns0 checks if the message has a EDNS0 (OPT) record, any EDNS0
  132. // record in the additional section will do. It returns the OPT record
  133. // found or nil.
  134. func (dns *Msg) IsEdns0() *OPT {
  135. // RFC 6891, Section 6.1.1 allows the OPT record to appear
  136. // anywhere in the additional record section, but it's usually at
  137. // the end so start there.
  138. for i := len(dns.Extra) - 1; i >= 0; i-- {
  139. if dns.Extra[i].Header().Rrtype == TypeOPT {
  140. return dns.Extra[i].(*OPT)
  141. }
  142. }
  143. return nil
  144. }
  145. // popEdns0 is like IsEdns0, but it removes the record from the message.
  146. func (dns *Msg) popEdns0() *OPT {
  147. // RFC 6891, Section 6.1.1 allows the OPT record to appear
  148. // anywhere in the additional record section, but it's usually at
  149. // the end so start there.
  150. for i := len(dns.Extra) - 1; i >= 0; i-- {
  151. if dns.Extra[i].Header().Rrtype == TypeOPT {
  152. opt := dns.Extra[i].(*OPT)
  153. dns.Extra = append(dns.Extra[:i], dns.Extra[i+1:]...)
  154. return opt
  155. }
  156. }
  157. return nil
  158. }
  159. // IsDomainName checks if s is a valid domain name, it returns the number of
  160. // labels and true, when a domain name is valid. Note that non fully qualified
  161. // domain name is considered valid, in this case the last label is counted in
  162. // the number of labels. When false is returned the number of labels is not
  163. // defined. Also note that this function is extremely liberal; almost any
  164. // string is a valid domain name as the DNS is 8 bit protocol. It checks if each
  165. // label fits in 63 characters and that the entire name will fit into the 255
  166. // octet wire format limit.
  167. func IsDomainName(s string) (labels int, ok bool) {
  168. // XXX: The logic in this function was copied from packDomainName and
  169. // should be kept in sync with that function.
  170. const lenmsg = 256
  171. if len(s) == 0 { // Ok, for instance when dealing with update RR without any rdata.
  172. return 0, false
  173. }
  174. s = Fqdn(s)
  175. // Each dot ends a segment of the name. Except for escaped dots (\.), which
  176. // are normal dots.
  177. var (
  178. off int
  179. begin int
  180. wasDot bool
  181. )
  182. for i := 0; i < len(s); i++ {
  183. switch s[i] {
  184. case '\\':
  185. if off+1 > lenmsg {
  186. return labels, false
  187. }
  188. // check for \DDD
  189. if isDDD(s[i+1:]) {
  190. i += 3
  191. begin += 3
  192. } else {
  193. i++
  194. begin++
  195. }
  196. wasDot = false
  197. case '.':
  198. if i == 0 && len(s) > 1 {
  199. // leading dots are not legal except for the root zone
  200. return labels, false
  201. }
  202. if wasDot {
  203. // two dots back to back is not legal
  204. return labels, false
  205. }
  206. wasDot = true
  207. labelLen := i - begin
  208. if labelLen >= 1<<6 { // top two bits of length must be clear
  209. return labels, false
  210. }
  211. // off can already (we're in a loop) be bigger than lenmsg
  212. // this happens when a name isn't fully qualified
  213. off += 1 + labelLen
  214. if off > lenmsg {
  215. return labels, false
  216. }
  217. labels++
  218. begin = i + 1
  219. default:
  220. wasDot = false
  221. }
  222. }
  223. return labels, true
  224. }
  225. // IsSubDomain checks if child is indeed a child of the parent. If child and parent
  226. // are the same domain true is returned as well.
  227. func IsSubDomain(parent, child string) bool {
  228. // Entire child is contained in parent
  229. return CompareDomainName(parent, child) == CountLabel(parent)
  230. }
  231. // IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
  232. // The checking is performed on the binary payload.
  233. func IsMsg(buf []byte) error {
  234. // Header
  235. if len(buf) < headerSize {
  236. return errors.New("dns: bad message header")
  237. }
  238. // Header: Opcode
  239. // TODO(miek): more checks here, e.g. check all header bits.
  240. return nil
  241. }
  242. // IsFqdn checks if a domain name is fully qualified.
  243. func IsFqdn(s string) bool {
  244. // Check for (and remove) a trailing dot, returning if there isn't one.
  245. if s == "" || s[len(s)-1] != '.' {
  246. return false
  247. }
  248. s = s[:len(s)-1]
  249. // If we don't have an escape sequence before the final dot, we know it's
  250. // fully qualified and can return here.
  251. if s == "" || s[len(s)-1] != '\\' {
  252. return true
  253. }
  254. // Otherwise we have to check if the dot is escaped or not by checking if
  255. // there are an odd or even number of escape sequences before the dot.
  256. i := strings.LastIndexFunc(s, func(r rune) bool {
  257. return r != '\\'
  258. })
  259. return (len(s)-i)%2 != 0
  260. }
  261. // IsRRset checks if a set of RRs is a valid RRset as defined by RFC 2181.
  262. // This means the RRs need to have the same type, name, and class. Returns true
  263. // if the RR set is valid, otherwise false.
  264. func IsRRset(rrset []RR) bool {
  265. if len(rrset) == 0 {
  266. return false
  267. }
  268. if len(rrset) == 1 {
  269. return true
  270. }
  271. rrHeader := rrset[0].Header()
  272. rrType := rrHeader.Rrtype
  273. rrClass := rrHeader.Class
  274. rrName := rrHeader.Name
  275. for _, rr := range rrset[1:] {
  276. curRRHeader := rr.Header()
  277. if curRRHeader.Rrtype != rrType || curRRHeader.Class != rrClass || curRRHeader.Name != rrName {
  278. // Mismatch between the records, so this is not a valid rrset for
  279. //signing/verifying
  280. return false
  281. }
  282. }
  283. return true
  284. }
  285. // Fqdn return the fully qualified domain name from s.
  286. // If s is already fully qualified, it behaves as the identity function.
  287. func Fqdn(s string) string {
  288. if IsFqdn(s) {
  289. return s
  290. }
  291. return s + "."
  292. }
  293. // CanonicalName returns the domain name in canonical form. A name in canonical
  294. // form is lowercase and fully qualified. See Section 6.2 in RFC 4034.
  295. // According to the RFC all uppercase US-ASCII letters in the owner name of the
  296. // RR areeplaced by the corresponding lowercase US-ASCII letters.
  297. func CanonicalName(s string) string {
  298. var result strings.Builder
  299. for _, ch := range s {
  300. if unicode.IsUpper(ch) && (ch >= 0x00 && ch <= 0x7F) {
  301. result.WriteRune(unicode.ToLower(ch))
  302. } else {
  303. result.WriteRune(ch)
  304. }
  305. }
  306. return Fqdn(result.String())
  307. }
  308. // Copied from the official Go code.
  309. // ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
  310. // address suitable for reverse DNS (PTR) record lookups or an error if it fails
  311. // to parse the IP address.
  312. func ReverseAddr(addr string) (arpa string, err error) {
  313. ip := net.ParseIP(addr)
  314. if ip == nil {
  315. return "", &Error{err: "unrecognized address: " + addr}
  316. }
  317. if v4 := ip.To4(); v4 != nil {
  318. buf := make([]byte, 0, net.IPv4len*4+len("in-addr.arpa."))
  319. // Add it, in reverse, to the buffer
  320. for i := len(v4) - 1; i >= 0; i-- {
  321. buf = strconv.AppendInt(buf, int64(v4[i]), 10)
  322. buf = append(buf, '.')
  323. }
  324. // Append "in-addr.arpa." and return (buf already has the final .)
  325. buf = append(buf, "in-addr.arpa."...)
  326. return string(buf), nil
  327. }
  328. // Must be IPv6
  329. buf := make([]byte, 0, net.IPv6len*4+len("ip6.arpa."))
  330. // Add it, in reverse, to the buffer
  331. for i := len(ip) - 1; i >= 0; i-- {
  332. v := ip[i]
  333. buf = append(buf, hexDigit[v&0xF], '.', hexDigit[v>>4], '.')
  334. }
  335. // Append "ip6.arpa." and return (buf already has the final .)
  336. buf = append(buf, "ip6.arpa."...)
  337. return string(buf), nil
  338. }
  339. // String returns the string representation for the type t.
  340. func (t Type) String() string {
  341. if t1, ok := TypeToString[uint16(t)]; ok {
  342. return t1
  343. }
  344. return "TYPE" + strconv.Itoa(int(t))
  345. }
  346. // String returns the string representation for the class c.
  347. func (c Class) String() string {
  348. if s, ok := ClassToString[uint16(c)]; ok {
  349. // Only emit mnemonics when they are unambiguous, specially ANY is in both.
  350. if _, ok := StringToType[s]; !ok {
  351. return s
  352. }
  353. }
  354. return "CLASS" + strconv.Itoa(int(c))
  355. }
  356. // String returns the string representation for the name n.
  357. func (n Name) String() string {
  358. return sprintName(string(n))
  359. }