defaults.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package dns
  2. import (
  3. "errors"
  4. "net"
  5. "strconv"
  6. )
  7. const hexDigit = "0123456789abcdef"
  8. // Everything is assumed in ClassINET.
  9. // SetReply creates a reply message from a request message.
  10. func (dns *Msg) SetReply(request *Msg) *Msg {
  11. dns.Id = request.Id
  12. dns.RecursionDesired = request.RecursionDesired // Copy rd bit
  13. dns.Response = true
  14. dns.Opcode = OpcodeQuery
  15. dns.Rcode = RcodeSuccess
  16. if len(request.Question) > 0 {
  17. dns.Question = make([]Question, 1)
  18. dns.Question[0] = request.Question[0]
  19. }
  20. return dns
  21. }
  22. // SetQuestion creates a question message, it sets the Question
  23. // section, generates an Id and sets the RecursionDesired (RD)
  24. // bit to true.
  25. func (dns *Msg) SetQuestion(z string, t uint16) *Msg {
  26. dns.Id = Id()
  27. dns.RecursionDesired = true
  28. dns.Question = make([]Question, 1)
  29. dns.Question[0] = Question{z, t, ClassINET}
  30. return dns
  31. }
  32. // SetNotify creates a notify message, it sets the Question
  33. // section, generates an Id and sets the Authoritative (AA)
  34. // bit to true.
  35. func (dns *Msg) SetNotify(z string) *Msg {
  36. dns.Opcode = OpcodeNotify
  37. dns.Authoritative = true
  38. dns.Id = Id()
  39. dns.Question = make([]Question, 1)
  40. dns.Question[0] = Question{z, TypeSOA, ClassINET}
  41. return dns
  42. }
  43. // SetRcode creates an error message suitable for the request.
  44. func (dns *Msg) SetRcode(request *Msg, rcode int) *Msg {
  45. dns.SetReply(request)
  46. dns.Rcode = rcode
  47. return dns
  48. }
  49. // SetRcodeFormatError creates a message with FormError set.
  50. func (dns *Msg) SetRcodeFormatError(request *Msg) *Msg {
  51. dns.Rcode = RcodeFormatError
  52. dns.Opcode = OpcodeQuery
  53. dns.Response = true
  54. dns.Authoritative = false
  55. dns.Id = request.Id
  56. return dns
  57. }
  58. // SetUpdate makes the message a dynamic update message. It
  59. // sets the ZONE section to: z, TypeSOA, ClassINET.
  60. func (dns *Msg) SetUpdate(z string) *Msg {
  61. dns.Id = Id()
  62. dns.Response = false
  63. dns.Opcode = OpcodeUpdate
  64. dns.Compress = false // BIND9 cannot handle compression
  65. dns.Question = make([]Question, 1)
  66. dns.Question[0] = Question{z, TypeSOA, ClassINET}
  67. return dns
  68. }
  69. // SetIxfr creates message for requesting an IXFR.
  70. func (dns *Msg) SetIxfr(z string, serial uint32, ns, mbox string) *Msg {
  71. dns.Id = Id()
  72. dns.Question = make([]Question, 1)
  73. dns.Ns = make([]RR, 1)
  74. s := new(SOA)
  75. s.Hdr = RR_Header{z, TypeSOA, ClassINET, defaultTtl, 0}
  76. s.Serial = serial
  77. s.Ns = ns
  78. s.Mbox = mbox
  79. dns.Question[0] = Question{z, TypeIXFR, ClassINET}
  80. dns.Ns[0] = s
  81. return dns
  82. }
  83. // SetAxfr creates message for requesting an AXFR.
  84. func (dns *Msg) SetAxfr(z string) *Msg {
  85. dns.Id = Id()
  86. dns.Question = make([]Question, 1)
  87. dns.Question[0] = Question{z, TypeAXFR, ClassINET}
  88. return dns
  89. }
  90. // SetTsig appends a TSIG RR to the message.
  91. // This is only a skeleton TSIG RR that is added as the last RR in the
  92. // additional section. The Tsig is calculated when the message is being send.
  93. func (dns *Msg) SetTsig(z, algo string, fudge, timesigned int64) *Msg {
  94. t := new(TSIG)
  95. t.Hdr = RR_Header{z, TypeTSIG, ClassANY, 0, 0}
  96. t.Algorithm = algo
  97. t.Fudge = 300
  98. t.TimeSigned = uint64(timesigned)
  99. t.OrigId = dns.Id
  100. dns.Extra = append(dns.Extra, t)
  101. return dns
  102. }
  103. // SetEdns0 appends a EDNS0 OPT RR to the message.
  104. // TSIG should always the last RR in a message.
  105. func (dns *Msg) SetEdns0(udpsize uint16, do bool) *Msg {
  106. e := new(OPT)
  107. e.Hdr.Name = "."
  108. e.Hdr.Rrtype = TypeOPT
  109. e.SetUDPSize(udpsize)
  110. if do {
  111. e.SetDo()
  112. }
  113. dns.Extra = append(dns.Extra, e)
  114. return dns
  115. }
  116. // IsTsig checks if the message has a TSIG record as the last record
  117. // in the additional section. It returns the TSIG record found or nil.
  118. func (dns *Msg) IsTsig() *TSIG {
  119. if len(dns.Extra) > 0 {
  120. if dns.Extra[len(dns.Extra)-1].Header().Rrtype == TypeTSIG {
  121. return dns.Extra[len(dns.Extra)-1].(*TSIG)
  122. }
  123. }
  124. return nil
  125. }
  126. // IsEdns0 checks if the message has a EDNS0 (OPT) record, any EDNS0
  127. // record in the additional section will do. It returns the OPT record
  128. // found or nil.
  129. func (dns *Msg) IsEdns0() *OPT {
  130. // EDNS0 is at the end of the additional section, start there.
  131. // We might want to change this to *only* look at the last two
  132. // records. So we see TSIG and/or OPT - this a slightly bigger
  133. // change though.
  134. for i := len(dns.Extra) - 1; i >= 0; i-- {
  135. if dns.Extra[i].Header().Rrtype == TypeOPT {
  136. return dns.Extra[i].(*OPT)
  137. }
  138. }
  139. return nil
  140. }
  141. // IsDomainName checks if s is a valid domain name, it returns the number of
  142. // labels and true, when a domain name is valid. Note that non fully qualified
  143. // domain name is considered valid, in this case the last label is counted in
  144. // the number of labels. When false is returned the number of labels is not
  145. // defined. Also note that this function is extremely liberal; almost any
  146. // string is a valid domain name as the DNS is 8 bit protocol. It checks if each
  147. // label fits in 63 characters, but there is no length check for the entire
  148. // string s. I.e. a domain name longer than 255 characters is considered valid.
  149. func IsDomainName(s string) (labels int, ok bool) {
  150. _, labels, err := packDomainName(s, nil, 0, nil, false)
  151. return labels, err == nil
  152. }
  153. // IsSubDomain checks if child is indeed a child of the parent. If child and parent
  154. // are the same domain true is returned as well.
  155. func IsSubDomain(parent, child string) bool {
  156. // Entire child is contained in parent
  157. return CompareDomainName(parent, child) == CountLabel(parent)
  158. }
  159. // IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
  160. // The checking is performed on the binary payload.
  161. func IsMsg(buf []byte) error {
  162. // Header
  163. if len(buf) < 12 {
  164. return errors.New("dns: bad message header")
  165. }
  166. // Header: Opcode
  167. // TODO(miek): more checks here, e.g. check all header bits.
  168. return nil
  169. }
  170. // IsFqdn checks if a domain name is fully qualified.
  171. func IsFqdn(s string) bool {
  172. l := len(s)
  173. if l == 0 {
  174. return false
  175. }
  176. return s[l-1] == '.'
  177. }
  178. // IsRRset checks if a set of RRs is a valid RRset as defined by RFC 2181.
  179. // This means the RRs need to have the same type, name, and class. Returns true
  180. // if the RR set is valid, otherwise false.
  181. func IsRRset(rrset []RR) bool {
  182. if len(rrset) == 0 {
  183. return false
  184. }
  185. if len(rrset) == 1 {
  186. return true
  187. }
  188. rrHeader := rrset[0].Header()
  189. rrType := rrHeader.Rrtype
  190. rrClass := rrHeader.Class
  191. rrName := rrHeader.Name
  192. for _, rr := range rrset[1:] {
  193. curRRHeader := rr.Header()
  194. if curRRHeader.Rrtype != rrType || curRRHeader.Class != rrClass || curRRHeader.Name != rrName {
  195. // Mismatch between the records, so this is not a valid rrset for
  196. //signing/verifying
  197. return false
  198. }
  199. }
  200. return true
  201. }
  202. // Fqdn return the fully qualified domain name from s.
  203. // If s is already fully qualified, it behaves as the identity function.
  204. func Fqdn(s string) string {
  205. if IsFqdn(s) {
  206. return s
  207. }
  208. return s + "."
  209. }
  210. // Copied from the official Go code.
  211. // ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
  212. // address suitable for reverse DNS (PTR) record lookups or an error if it fails
  213. // to parse the IP address.
  214. func ReverseAddr(addr string) (arpa string, err error) {
  215. ip := net.ParseIP(addr)
  216. if ip == nil {
  217. return "", &Error{err: "unrecognized address: " + addr}
  218. }
  219. if ip.To4() != nil {
  220. return strconv.Itoa(int(ip[15])) + "." + strconv.Itoa(int(ip[14])) + "." + strconv.Itoa(int(ip[13])) + "." +
  221. strconv.Itoa(int(ip[12])) + ".in-addr.arpa.", nil
  222. }
  223. // Must be IPv6
  224. buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
  225. // Add it, in reverse, to the buffer
  226. for i := len(ip) - 1; i >= 0; i-- {
  227. v := ip[i]
  228. buf = append(buf, hexDigit[v&0xF])
  229. buf = append(buf, '.')
  230. buf = append(buf, hexDigit[v>>4])
  231. buf = append(buf, '.')
  232. }
  233. // Append "ip6.arpa." and return (buf already has the final .)
  234. buf = append(buf, "ip6.arpa."...)
  235. return string(buf), nil
  236. }
  237. // String returns the string representation for the type t.
  238. func (t Type) String() string {
  239. if t1, ok := TypeToString[uint16(t)]; ok {
  240. return t1
  241. }
  242. return "TYPE" + strconv.Itoa(int(t))
  243. }
  244. // String returns the string representation for the class c.
  245. func (c Class) String() string {
  246. if c1, ok := ClassToString[uint16(c)]; ok {
  247. return c1
  248. }
  249. return "CLASS" + strconv.Itoa(int(c))
  250. }
  251. // String returns the string representation for the name n.
  252. func (n Name) String() string {
  253. return sprintName(string(n))
  254. }