privaterr.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. switch rr := anyrr.(type) {
  38. case *PrivateRR:
  39. return rr
  40. }
  41. panic(fmt.Sprintf("dns: RR is not a PrivateRR, TypeToRR[%d] generator returned %T", rrtype, anyrr))
  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() int { return r.Hdr.len() + r.Data.Len() }
  48. func (r *PrivateRR) copy() RR {
  49. // make new RR like this:
  50. rr := mkPrivateRR(r.Hdr.Rrtype)
  51. rr.Hdr = r.Hdr
  52. err := r.Data.Copy(rr.Data)
  53. if err != nil {
  54. panic("dns: got value that could not be used to copy Private rdata")
  55. }
  56. return rr
  57. }
  58. func (r *PrivateRR) pack(msg []byte, off int, compression map[string]int, compress bool) (int, error) {
  59. off, err := r.Hdr.pack(msg, off, compression, compress)
  60. if err != nil {
  61. return off, err
  62. }
  63. headerEnd := off
  64. n, err := r.Data.Pack(msg[off:])
  65. if err != nil {
  66. return len(msg), err
  67. }
  68. off += n
  69. r.Header().Rdlength = uint16(off - headerEnd)
  70. return off, nil
  71. }
  72. // PrivateHandle registers a private resource record type. It requires
  73. // string and numeric representation of private RR type and generator function as argument.
  74. func PrivateHandle(rtypestr string, rtype uint16, generator func() PrivateRdata) {
  75. rtypestr = strings.ToUpper(rtypestr)
  76. TypeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
  77. TypeToString[rtype] = rtypestr
  78. StringToType[rtypestr] = rtype
  79. typeToUnpack[rtype] = func(h RR_Header, msg []byte, off int) (RR, int, error) {
  80. if noRdata(h) {
  81. return &h, off, nil
  82. }
  83. var err error
  84. rr := mkPrivateRR(h.Rrtype)
  85. rr.Hdr = h
  86. off1, err := rr.Data.Unpack(msg[off:])
  87. off += off1
  88. if err != nil {
  89. return rr, off, err
  90. }
  91. return rr, off, err
  92. }
  93. setPrivateRR := func(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  94. rr := mkPrivateRR(h.Rrtype)
  95. rr.Hdr = h
  96. var l lex
  97. text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
  98. Fetch:
  99. for {
  100. // TODO(miek): we could also be returning _QUOTE, this might or might not
  101. // be an issue (basically parsing TXT becomes hard)
  102. switch l = <-c; l.value {
  103. case zNewline, zEOF:
  104. break Fetch
  105. case zString:
  106. text = append(text, l.token)
  107. }
  108. }
  109. err := rr.Data.Parse(text)
  110. if err != nil {
  111. return nil, &ParseError{f, err.Error(), l}, ""
  112. }
  113. return rr, nil, ""
  114. }
  115. typeToparserFunc[rtype] = parserFunc{setPrivateRR, true}
  116. }
  117. // PrivateHandleRemove removes defenitions required to support private RR type.
  118. func PrivateHandleRemove(rtype uint16) {
  119. rtypestr, ok := TypeToString[rtype]
  120. if ok {
  121. delete(TypeToRR, rtype)
  122. delete(TypeToString, rtype)
  123. delete(typeToparserFunc, rtype)
  124. delete(StringToType, rtypestr)
  125. delete(typeToUnpack, rtype)
  126. }
  127. return
  128. }