defaults.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. for _, r := range dns.Extra {
  131. if r.Header().Rrtype == TypeOPT {
  132. return r.(*OPT)
  133. }
  134. }
  135. return nil
  136. }
  137. // IsDomainName checks if s is a valid domain name, it returns the number of
  138. // labels and true, when a domain name is valid. Note that non fully qualified
  139. // domain name is considered valid, in this case the last label is counted in
  140. // the number of labels. When false is returned the number of labels is not
  141. // defined. Also note that this function is extremely liberal; almost any
  142. // string is a valid domain name as the DNS is 8 bit protocol. It checks if each
  143. // label fits in 63 characters, but there is no length check for the entire
  144. // string s. I.e. a domain name longer than 255 characters is considered valid.
  145. func IsDomainName(s string) (labels int, ok bool) {
  146. _, labels, err := packDomainName(s, nil, 0, nil, false)
  147. return labels, err == nil
  148. }
  149. // IsSubDomain checks if child is indeed a child of the parent. Both child and
  150. // parent are *not* downcased before doing the comparison.
  151. func IsSubDomain(parent, child string) bool {
  152. // Entire child is contained in parent
  153. return CompareDomainName(parent, child) == CountLabel(parent)
  154. }
  155. // IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet.
  156. // The checking is performed on the binary payload.
  157. func IsMsg(buf []byte) error {
  158. // Header
  159. if len(buf) < 12 {
  160. return errors.New("dns: bad message header")
  161. }
  162. // Header: Opcode
  163. // TODO(miek): more checks here, e.g. check all header bits.
  164. return nil
  165. }
  166. // IsFqdn checks if a domain name is fully qualified.
  167. func IsFqdn(s string) bool {
  168. l := len(s)
  169. if l == 0 {
  170. return false
  171. }
  172. return s[l-1] == '.'
  173. }
  174. // IsRRset checks if a set of RRs is a valid RRset as defined by RFC 2181.
  175. // This means the RRs need to have the same type, name, and class. Returns true
  176. // if the RR set is valid, otherwise false.
  177. func IsRRset(rrset []RR) bool {
  178. if len(rrset) == 0 {
  179. return false
  180. }
  181. if len(rrset) == 1 {
  182. return true
  183. }
  184. rrHeader := rrset[0].Header()
  185. rrType := rrHeader.Rrtype
  186. rrClass := rrHeader.Class
  187. rrName := rrHeader.Name
  188. for _, rr := range rrset[1:] {
  189. curRRHeader := rr.Header()
  190. if curRRHeader.Rrtype != rrType || curRRHeader.Class != rrClass || curRRHeader.Name != rrName {
  191. // Mismatch between the records, so this is not a valid rrset for
  192. //signing/verifying
  193. return false
  194. }
  195. }
  196. return true
  197. }
  198. // Fqdn return the fully qualified domain name from s.
  199. // If s is already fully qualified, it behaves as the identity function.
  200. func Fqdn(s string) string {
  201. if IsFqdn(s) {
  202. return s
  203. }
  204. return s + "."
  205. }
  206. // Copied from the official Go code.
  207. // ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
  208. // address suitable for reverse DNS (PTR) record lookups or an error if it fails
  209. // to parse the IP address.
  210. func ReverseAddr(addr string) (arpa string, err error) {
  211. ip := net.ParseIP(addr)
  212. if ip == nil {
  213. return "", &Error{err: "unrecognized address: " + addr}
  214. }
  215. if ip.To4() != nil {
  216. return strconv.Itoa(int(ip[15])) + "." + strconv.Itoa(int(ip[14])) + "." + strconv.Itoa(int(ip[13])) + "." +
  217. strconv.Itoa(int(ip[12])) + ".in-addr.arpa.", nil
  218. }
  219. // Must be IPv6
  220. buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
  221. // Add it, in reverse, to the buffer
  222. for i := len(ip) - 1; i >= 0; i-- {
  223. v := ip[i]
  224. buf = append(buf, hexDigit[v&0xF])
  225. buf = append(buf, '.')
  226. buf = append(buf, hexDigit[v>>4])
  227. buf = append(buf, '.')
  228. }
  229. // Append "ip6.arpa." and return (buf already has the final .)
  230. buf = append(buf, "ip6.arpa."...)
  231. return string(buf), nil
  232. }
  233. // String returns the string representation for the type t.
  234. func (t Type) String() string {
  235. if t1, ok := TypeToString[uint16(t)]; ok {
  236. return t1
  237. }
  238. return "TYPE" + strconv.Itoa(int(t))
  239. }
  240. // String returns the string representation for the class c.
  241. func (c Class) String() string {
  242. if c1, ok := ClassToString[uint16(c)]; ok {
  243. return c1
  244. }
  245. return "CLASS" + strconv.Itoa(int(c))
  246. }
  247. // String returns the string representation for the name n.
  248. func (n Name) String() string {
  249. return sprintName(string(n))
  250. }