You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

defaults.go 10KB

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