privaterr.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package dns
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // PrivateRdata is an interface used for implementing "Private Use" RR types, see
  7. // RFC 6895. This allows one to experiment with new RR types, without requesting an
  8. // official type code. Also see dns.PrivateHandle and dns.PrivateHandleRemove.
  9. type PrivateRdata interface {
  10. // String returns the text presentaton of the Rdata of the Private RR.
  11. String() string
  12. // Parse parses the Rdata of the private RR.
  13. Parse([]string) error
  14. // Pack is used when packing a private RR into a buffer.
  15. Pack([]byte) (int, error)
  16. // Unpack is used when unpacking a private RR from a buffer.
  17. // TODO(miek): diff. signature than Pack, see edns0.go for instance.
  18. Unpack([]byte) (int, error)
  19. // Copy copies the Rdata.
  20. Copy(PrivateRdata) error
  21. // Len returns the length in octets of the Rdata.
  22. Len() int
  23. }
  24. // PrivateRR represents an RR that uses a PrivateRdata user-defined type.
  25. // It mocks normal RRs and implements dns.RR interface.
  26. type PrivateRR struct {
  27. Hdr RR_Header
  28. Data PrivateRdata
  29. }
  30. func mkPrivateRR(rrtype uint16) *PrivateRR {
  31. // Panics if RR is not an instance of PrivateRR.
  32. rrfunc, ok := TypeToRR[rrtype]
  33. if !ok {
  34. panic(fmt.Sprintf("dns: invalid operation with Private RR type %d", rrtype))
  35. }
  36. anyrr := rrfunc()
  37. rr, ok := anyrr.(*PrivateRR)
  38. if !ok {
  39. panic(fmt.Sprintf("dns: RR is not a PrivateRR, TypeToRR[%d] generator returned %T", rrtype, anyrr))
  40. }
  41. return rr
  42. }
  43. // Header return the RR header of r.
  44. func (r *PrivateRR) Header() *RR_Header { return &r.Hdr }
  45. func (r *PrivateRR) String() string { return r.Hdr.String() + r.Data.String() }
  46. // Private len and copy parts to satisfy RR interface.
  47. func (r *PrivateRR) len(off int, compression map[string]struct{}) int {
  48. l := r.Hdr.len(off, compression)
  49. l += r.Data.Len()
  50. return l
  51. }
  52. func (r *PrivateRR) copy() RR {
  53. // make new RR like this:
  54. rr := mkPrivateRR(r.Hdr.Rrtype)
  55. rr.Hdr = r.Hdr
  56. err := r.Data.Copy(rr.Data)
  57. if err != nil {
  58. panic("dns: got value that could not be used to copy Private rdata")
  59. }
  60. return rr
  61. }
  62. func (r *PrivateRR) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
  63. n, err := r.Data.Pack(msg[off:])
  64. if err != nil {
  65. return len(msg), err
  66. }
  67. off += n
  68. return off, nil
  69. }
  70. func (r *PrivateRR) unpack(msg []byte, off int) (int, error) {
  71. off1, err := r.Data.Unpack(msg[off:])
  72. off += off1
  73. return off, err
  74. }
  75. func (r *PrivateRR) parse(c *zlexer, origin, file string) *ParseError {
  76. var l lex
  77. text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
  78. Fetch:
  79. for {
  80. // TODO(miek): we could also be returning _QUOTE, this might or might not
  81. // be an issue (basically parsing TXT becomes hard)
  82. switch l, _ = c.Next(); l.value {
  83. case zNewline, zEOF:
  84. break Fetch
  85. case zString:
  86. text = append(text, l.token)
  87. }
  88. }
  89. err := r.Data.Parse(text)
  90. if err != nil {
  91. return &ParseError{file, err.Error(), l}
  92. }
  93. return nil
  94. }
  95. func (r1 *PrivateRR) isDuplicate(r2 RR) bool { return false }
  96. // PrivateHandle registers a private resource record type. It requires
  97. // string and numeric representation of private RR type and generator function as argument.
  98. func PrivateHandle(rtypestr string, rtype uint16, generator func() PrivateRdata) {
  99. rtypestr = strings.ToUpper(rtypestr)
  100. TypeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
  101. TypeToString[rtype] = rtypestr
  102. StringToType[rtypestr] = rtype
  103. }
  104. // PrivateHandleRemove removes definitions required to support private RR type.
  105. func PrivateHandleRemove(rtype uint16) {
  106. rtypestr, ok := TypeToString[rtype]
  107. if ok {
  108. delete(TypeToRR, rtype)
  109. delete(TypeToString, rtype)
  110. delete(StringToType, rtypestr)
  111. }
  112. }