dns.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = 4096 // DefaultMsgSize is the standard default for messages larger than 512 bytes.
  7. MinMsgSize = 512 // MinMsgSize is the minimal size of a DNS packet.
  8. MaxMsgSize = 65535 // MaxMsgSize is the largest possible DNS packet.
  9. )
  10. // Error represents a DNS error.
  11. type Error struct{ err string }
  12. func (e *Error) Error() string {
  13. if e == nil {
  14. return "dns: <nil>"
  15. }
  16. return "dns: " + e.err
  17. }
  18. // An RR represents a resource record.
  19. type RR interface {
  20. // Header returns the header of an resource record. The header contains
  21. // everything up to the rdata.
  22. Header() *RR_Header
  23. // String returns the text representation of the resource record.
  24. String() string
  25. // copy returns a copy of the RR
  26. copy() RR
  27. // len returns the length (in octets) of the uncompressed RR in wire format.
  28. len() int
  29. // pack packs an RR into wire format.
  30. pack([]byte, int, map[string]int, bool) (int, error)
  31. }
  32. // RR_Header is the header all DNS resource records share.
  33. type RR_Header struct {
  34. Name string `dns:"cdomain-name"`
  35. Rrtype uint16
  36. Class uint16
  37. Ttl uint32
  38. Rdlength uint16 // Length of data after header.
  39. }
  40. // Header returns itself. This is here to make RR_Header implements the RR interface.
  41. func (h *RR_Header) Header() *RR_Header { return h }
  42. // Just to implement the RR interface.
  43. func (h *RR_Header) copy() RR { return nil }
  44. func (h *RR_Header) copyHeader() *RR_Header {
  45. r := new(RR_Header)
  46. r.Name = h.Name
  47. r.Rrtype = h.Rrtype
  48. r.Class = h.Class
  49. r.Ttl = h.Ttl
  50. r.Rdlength = h.Rdlength
  51. return r
  52. }
  53. func (h *RR_Header) String() string {
  54. var s string
  55. if h.Rrtype == TypeOPT {
  56. s = ";"
  57. // and maybe other things
  58. }
  59. s += sprintName(h.Name) + "\t"
  60. s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
  61. s += Class(h.Class).String() + "\t"
  62. s += Type(h.Rrtype).String() + "\t"
  63. return s
  64. }
  65. func (h *RR_Header) len() int {
  66. l := len(h.Name) + 1
  67. l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
  68. return l
  69. }
  70. // ToRFC3597 converts a known RR to the unknown RR representation from RFC 3597.
  71. func (rr *RFC3597) ToRFC3597(r RR) error {
  72. buf := make([]byte, r.len()*2)
  73. off, err := PackRR(r, buf, 0, nil, false)
  74. if err != nil {
  75. return err
  76. }
  77. buf = buf[:off]
  78. if int(r.Header().Rdlength) > off {
  79. return ErrBuf
  80. }
  81. rfc3597, _, err := unpackRFC3597(*r.Header(), buf, off-int(r.Header().Rdlength))
  82. if err != nil {
  83. return err
  84. }
  85. *rr = *rfc3597.(*RFC3597)
  86. return nil
  87. }