dns.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dns
  2. import "strconv"
  3. const (
  4. year68 = 1 << 31 // For RFC1982 (Serial Arithmetic) calculations in 32 bits.
  5. // DefaultMsgSize is the standard default for messages larger than 512 bytes.
  6. DefaultMsgSize = 4096
  7. // MinMsgSize is the minimal size of a DNS packet.
  8. MinMsgSize = 512
  9. // MaxMsgSize is the largest possible DNS packet.
  10. MaxMsgSize = 65535
  11. defaultTtl = 3600 // Default internal TTL.
  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 uncompressed RR in wire format.
  31. len() int
  32. }
  33. // RR_Header is the header all DNS resource records share.
  34. type RR_Header struct {
  35. Name string `dns:"cdomain-name"`
  36. Rrtype uint16
  37. Class uint16
  38. Ttl uint32
  39. Rdlength uint16 // length of data after header
  40. }
  41. // Header returns itself. This is here to make RR_Header implement the RR interface.
  42. func (h *RR_Header) Header() *RR_Header { return h }
  43. // Just to imlement the RR interface.
  44. func (h *RR_Header) copy() RR { return nil }
  45. func (h *RR_Header) copyHeader() *RR_Header {
  46. r := new(RR_Header)
  47. r.Name = h.Name
  48. r.Rrtype = h.Rrtype
  49. r.Class = h.Class
  50. r.Ttl = h.Ttl
  51. r.Rdlength = h.Rdlength
  52. return r
  53. }
  54. func (h *RR_Header) String() string {
  55. var s string
  56. if h.Rrtype == TypeOPT {
  57. s = ";"
  58. // and maybe other things
  59. }
  60. s += sprintName(h.Name) + "\t"
  61. s += strconv.FormatInt(int64(h.Ttl), 10) + "\t"
  62. s += Class(h.Class).String() + "\t"
  63. s += Type(h.Rrtype).String() + "\t"
  64. return s
  65. }
  66. func (h *RR_Header) len() int {
  67. l := len(h.Name) + 1
  68. l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
  69. return l
  70. }
  71. // ToRFC3597 converts a known RR to the unknown RR representation
  72. // from RFC 3597.
  73. func (rr *RFC3597) ToRFC3597(r RR) error {
  74. buf := make([]byte, r.len()*2)
  75. off, err := PackStruct(r, buf, 0)
  76. if err != nil {
  77. return err
  78. }
  79. buf = buf[:off]
  80. rawSetRdlength(buf, 0, off)
  81. _, err = UnpackStruct(rr, buf, 0)
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }