dns.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package dns
  2. import (
  3. "encoding/hex"
  4. "strconv"
  5. )
  6. const (
  7. year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
  8. defaultTtl = 3600 // Default internal TTL.
  9. // DefaultMsgSize is the standard default for messages larger than 512 bytes.
  10. DefaultMsgSize = 4096
  11. // MinMsgSize is the minimal size of a DNS packet.
  12. MinMsgSize = 512
  13. // MaxMsgSize is the largest possible DNS packet.
  14. MaxMsgSize = 65535
  15. )
  16. // Error represents a DNS error.
  17. type Error struct{ err string }
  18. func (e *Error) Error() string {
  19. if e == nil {
  20. return "dns: <nil>"
  21. }
  22. return "dns: " + e.err
  23. }
  24. // An RR represents a resource record.
  25. type RR interface {
  26. // Header returns the header of an resource record. The header contains
  27. // everything up to the rdata.
  28. Header() *RR_Header
  29. // String returns the text representation of the resource record.
  30. String() string
  31. // copy returns a copy of the RR
  32. copy() RR
  33. // len returns the length (in octets) of the compressed or uncompressed RR in wire format.
  34. //
  35. // If compression is nil, the uncompressed size will be returned, otherwise the compressed
  36. // size will be returned and domain names will be added to the map for future compression.
  37. len(off int, compression map[string]struct{}) int
  38. // pack packs the records RDATA into wire format. The header will
  39. // already have been packed into msg.
  40. pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error)
  41. // unpack unpacks an RR from wire format.
  42. //
  43. // This will only be called on a new and empty RR type with only the header populated. It
  44. // will only be called if the record's RDATA is non-empty.
  45. unpack(msg []byte, off int) (off1 int, err error)
  46. // parse parses an RR from zone file format.
  47. //
  48. // This will only be called on a new and empty RR type with only the header populated.
  49. parse(c *zlexer, origin string) *ParseError
  50. // isDuplicate returns whether the two RRs are duplicates.
  51. isDuplicate(r2 RR) bool
  52. }
  53. // RR_Header is the header all DNS resource records share.
  54. type RR_Header struct {
  55. Name string `dns:"cdomain-name"`
  56. Rrtype uint16
  57. Class uint16
  58. Ttl uint32
  59. Rdlength uint16 // Length of data after header.
  60. }
  61. // Header returns itself. This is here to make RR_Header implements the RR interface.
  62. func (h *RR_Header) Header() *RR_Header { return h }
  63. // Just to implement the RR interface.
  64. func (h *RR_Header) copy() RR { return nil }
  65. func (h *RR_Header) String() string {
  66. var s string
  67. if h.Rrtype == TypeOPT {
  68. s = ";"
  69. // and maybe other things
  70. }
  71. s += sprintName(h.Name) + "\t"
  72. s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
  73. s += Class(h.Class).String() + "\t"
  74. s += Type(h.Rrtype).String() + "\t"
  75. return s
  76. }
  77. func (h *RR_Header) len(off int, compression map[string]struct{}) int {
  78. l := domainNameLen(h.Name, off, compression, true)
  79. l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
  80. return l
  81. }
  82. func (h *RR_Header) pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error) {
  83. // RR_Header has no RDATA to pack.
  84. return off, nil
  85. }
  86. func (h *RR_Header) unpack(msg []byte, off int) (int, error) {
  87. panic("dns: internal error: unpack should never be called on RR_Header")
  88. }
  89. func (h *RR_Header) parse(c *zlexer, origin string) *ParseError {
  90. panic("dns: internal error: parse should never be called on RR_Header")
  91. }
  92. // ToRFC3597 converts a known RR to the unknown RR representation from RFC 3597.
  93. func (rr *RFC3597) ToRFC3597(r RR) error {
  94. buf := make([]byte, Len(r))
  95. headerEnd, off, err := packRR(r, buf, 0, compressionMap{}, false)
  96. if err != nil {
  97. return err
  98. }
  99. buf = buf[:off]
  100. *rr = RFC3597{Hdr: *r.Header()}
  101. rr.Hdr.Rdlength = uint16(off - headerEnd)
  102. if noRdata(rr.Hdr) {
  103. return nil
  104. }
  105. _, err = rr.unpack(buf, headerEnd)
  106. return err
  107. }
  108. // fromRFC3597 converts an unknown RR representation from RFC 3597 to the known RR type.
  109. func (rr *RFC3597) fromRFC3597(r RR) error {
  110. hdr := r.Header()
  111. *hdr = rr.Hdr
  112. // Can't overflow uint16 as the length of Rdata is validated in (*RFC3597).parse.
  113. // We can only get here when rr was constructed with that method.
  114. hdr.Rdlength = uint16(hex.DecodedLen(len(rr.Rdata)))
  115. if noRdata(*hdr) {
  116. // Dynamic update.
  117. return nil
  118. }
  119. // rr.pack requires an extra allocation and a copy so we just decode Rdata
  120. // manually, it's simpler anyway.
  121. msg, err := hex.DecodeString(rr.Rdata)
  122. if err != nil {
  123. return err
  124. }
  125. _, err = r.unpack(msg, 0)
  126. return err
  127. }