privaterr.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. newh := r.Hdr.copyHeader()
  52. rr.Hdr = *newh
  53. err := r.Data.Copy(rr.Data)
  54. if err != nil {
  55. panic("dns: got value that could not be used to copy Private rdata")
  56. }
  57. return rr
  58. }
  59. // PrivateHandle registers a private resource record type. It requires
  60. // string and numeric representation of private RR type and generator function as argument.
  61. func PrivateHandle(rtypestr string, rtype uint16, generator func() PrivateRdata) {
  62. rtypestr = strings.ToUpper(rtypestr)
  63. TypeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
  64. TypeToString[rtype] = rtypestr
  65. StringToType[rtypestr] = rtype
  66. setPrivateRR := func(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  67. rr := mkPrivateRR(h.Rrtype)
  68. rr.Hdr = h
  69. var l lex
  70. text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
  71. FETCH:
  72. for {
  73. // TODO(miek): we could also be returning _QUOTE, this might or might not
  74. // be an issue (basically parsing TXT becomes hard)
  75. switch l = <-c; l.value {
  76. case zNewline, zEOF:
  77. break FETCH
  78. case zString:
  79. text = append(text, l.token)
  80. }
  81. }
  82. err := rr.Data.Parse(text)
  83. if err != nil {
  84. return nil, &ParseError{f, err.Error(), l}, ""
  85. }
  86. return rr, nil, ""
  87. }
  88. typeToparserFunc[rtype] = parserFunc{setPrivateRR, true}
  89. }
  90. // PrivateHandleRemove removes defenitions required to support private RR type.
  91. func PrivateHandleRemove(rtype uint16) {
  92. rtypestr, ok := TypeToString[rtype]
  93. if ok {
  94. delete(TypeToRR, rtype)
  95. delete(TypeToString, rtype)
  96. delete(typeToparserFunc, rtype)
  97. delete(StringToType, rtypestr)
  98. }
  99. return
  100. }