dns.go 3.7 KB

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