defaults.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. // EDNS0 is at the end of the additional section, start there.
  135. // We might want to change this to *only* look at the last two
  136. // records. So we see TSIG and/or OPT - this a slightly bigger
  137. // change though.
  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. // IsDomainName checks if s is a valid domain name, it returns the number of
  146. // labels and true, when a domain name is valid. Note that non fully qualified
  147. // domain name is considered valid, in this case the last label is counted in
  148. // the number of labels. When false is returned the number of labels is not
  149. // defined. Also note that this function is extremely liberal; almost any
  150. // string is a valid domain name as the DNS is 8 bit protocol. It checks if each
  151. // label fits in 63 characters and that the entire name will fit into the 255
  152. // octet wire format limit.
  153. func IsDomainName(s string) (labels int, ok bool) {
  154. // XXX: The logic in this function was copied from packDomainName and
  155. // should be kept in sync with that function.
  156. const lenmsg = 256
  157. if len(s) == 0 { // Ok, for instance when dealing with update RR without any rdata.
  158. return 0, false
  159. }
  160. s = Fqdn(s)
  161. // Each dot ends a segment of the name. Except for escaped dots (\.), which
  162. // are normal dots.
  163. var (
  164. off int
  165. begin int
  166. wasDot bool
  167. )
  168. for i := 0; i < len(s); i++ {
  169. switch s[i] {
  170. case '\\':
  171. if off+1 > lenmsg {
  172. return labels, false
  173. }
  174. // check for \DDD
  175. if i+3 < len(s) && isDigit(s[i+1]) && isDigit(s[i+2]) && isDigit(s[i+3]) {
  176. i += 3
  177. begin += 3
  178. } else {
  179. i++
  180. begin++
  181. }
  182. wasDot = false
  183. case '.':
  184. if wasDot {
  185. // two dots back to back is not legal
  186. return labels, false
  187. }
  188. wasDot = true
  189. labelLen := i - begin
  190. if labelLen >= 1<<6 { // top two bits of length must be clear
  191. return labels, false
  192. }
  193. // off can already (we're in a loop) be bigger than lenmsg
  194. // this happens when a name isn't fully qualified
  195. off += 1 + labelLen
  196. if off > lenmsg {
  197. return labels, false
  198. }
  199. labels++
  200. begin = i + 1
  201. default:
  202. wasDot = false
  203. }
  204. }
  205. return labels, true
  206. }
  207. // IsSubDomain checks if child is indeed a child of the parent. If child and parent
  208. // are the same domain true is returned as well.
  209. func IsSubDomain(parent, child string) bool {
  210. // Entire child is contained in parent
  211. return CompareDomainName(parent, child) == CountLabel(parent)
  212. }
  213. // IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
  214. // The checking is performed on the binary payload.
  215. func IsMsg(buf []byte) error {
  216. // Header
  217. if len(buf) < headerSize {
  218. return errors.New("dns: bad message header")
  219. }
  220. // Header: Opcode
  221. // TODO(miek): more checks here, e.g. check all header bits.
  222. return nil
  223. }
  224. // IsFqdn checks if a domain name is fully qualified.
  225. func IsFqdn(s string) bool {
  226. s2 := strings.TrimSuffix(s, ".")
  227. if s == s2 {
  228. return false
  229. }
  230. i := strings.LastIndexFunc(s2, func(r rune) bool {
  231. return r != '\\'
  232. })
  233. // Test whether we have an even number of escape sequences before
  234. // the dot or none.
  235. return (len(s2)-i)%2 != 0
  236. }
  237. // IsRRset checks if a set of RRs is a valid RRset as defined by RFC 2181.
  238. // This means the RRs need to have the same type, name, and class. Returns true
  239. // if the RR set is valid, otherwise false.
  240. func IsRRset(rrset []RR) bool {
  241. if len(rrset) == 0 {
  242. return false
  243. }
  244. if len(rrset) == 1 {
  245. return true
  246. }
  247. rrHeader := rrset[0].Header()
  248. rrType := rrHeader.Rrtype
  249. rrClass := rrHeader.Class
  250. rrName := rrHeader.Name
  251. for _, rr := range rrset[1:] {
  252. curRRHeader := rr.Header()
  253. if curRRHeader.Rrtype != rrType || curRRHeader.Class != rrClass || curRRHeader.Name != rrName {
  254. // Mismatch between the records, so this is not a valid rrset for
  255. //signing/verifying
  256. return false
  257. }
  258. }
  259. return true
  260. }
  261. // Fqdn return the fully qualified domain name from s.
  262. // If s is already fully qualified, it behaves as the identity function.
  263. func Fqdn(s string) string {
  264. if IsFqdn(s) {
  265. return s
  266. }
  267. return s + "."
  268. }
  269. // Copied from the official Go code.
  270. // ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
  271. // address suitable for reverse DNS (PTR) record lookups or an error if it fails
  272. // to parse the IP address.
  273. func ReverseAddr(addr string) (arpa string, err error) {
  274. ip := net.ParseIP(addr)
  275. if ip == nil {
  276. return "", &Error{err: "unrecognized address: " + addr}
  277. }
  278. if v4 := ip.To4(); v4 != nil {
  279. buf := make([]byte, 0, net.IPv4len*4+len("in-addr.arpa."))
  280. // Add it, in reverse, to the buffer
  281. for i := len(v4) - 1; i >= 0; i-- {
  282. buf = strconv.AppendInt(buf, int64(v4[i]), 10)
  283. buf = append(buf, '.')
  284. }
  285. // Append "in-addr.arpa." and return (buf already has the final .)
  286. buf = append(buf, "in-addr.arpa."...)
  287. return string(buf), nil
  288. }
  289. // Must be IPv6
  290. buf := make([]byte, 0, net.IPv6len*4+len("ip6.arpa."))
  291. // Add it, in reverse, to the buffer
  292. for i := len(ip) - 1; i >= 0; i-- {
  293. v := ip[i]
  294. buf = append(buf, hexDigit[v&0xF])
  295. buf = append(buf, '.')
  296. buf = append(buf, hexDigit[v>>4])
  297. buf = append(buf, '.')
  298. }
  299. // Append "ip6.arpa." and return (buf already has the final .)
  300. buf = append(buf, "ip6.arpa."...)
  301. return string(buf), nil
  302. }
  303. // String returns the string representation for the type t.
  304. func (t Type) String() string {
  305. if t1, ok := TypeToString[uint16(t)]; ok {
  306. return t1
  307. }
  308. return "TYPE" + strconv.Itoa(int(t))
  309. }
  310. // String returns the string representation for the class c.
  311. func (c Class) String() string {
  312. if s, ok := ClassToString[uint16(c)]; ok {
  313. // Only emit mnemonics when they are unambiguous, specically ANY is in both.
  314. if _, ok := StringToType[s]; !ok {
  315. return s
  316. }
  317. }
  318. return "CLASS" + strconv.Itoa(int(c))
  319. }
  320. // String returns the string representation for the name n.
  321. func (n Name) String() string {
  322. return sprintName(string(n))
  323. }