diff --git a/privaterr_ex_test.go b/customrrex_test.go similarity index 100% rename from privaterr_ex_test.go rename to customrrex_test.go diff --git a/msg.go b/msg.go index b7cc6859..e84c1984 100644 --- a/msg.go +++ b/msg.go @@ -581,7 +581,7 @@ func packStructValue(val reflect.Value, msg []byte, off int, compression map[str // therefore it's expected that this interface would be PrivateRdata switch data := fv.Interface().(type) { case PrivateRdata: - n, err := data.WriteByteSlice(msg[off:]) + n, err := data.Pack(msg[off:]) if err != nil { return lenmsg, err } @@ -887,7 +887,7 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, err er // therefore it's expected that this interface would be PrivateRdata switch data := fv.Interface().(type) { case PrivateRdata: - n, err := data.ParseByteSlice(msg[off:rdend]) + n, err := data.Unpack(msg[off:rdend]) if err != nil { return lenmsg, err } diff --git a/privaterr.go b/privaterr.go index 17853c71..efa4fe2e 100644 --- a/privaterr.go +++ b/privaterr.go @@ -5,24 +5,31 @@ import ( "strings" ) -// PrivateRdata is an interface to implement non-RFC dictated resource records. See also dns.PrivateRR, dns.NewPrivateRR and dns.DelPrivateRR +// PrivateRdata is an interface used for implementing "Private Use" RR types, see +// RFC 6895. This allows one to experiment with new RR types, without requesting an +// official type code. Also see dns.PrivateHandle and dns.PrivateHandleRemove. type PrivateRdata interface { + // String returns the text presentaton of the Rdata of the Private RR. String() string ParseTextSlice([]string) error - WriteByteSlice([]byte) (int, error) - ParseByteSlice([]byte) (int, error) + // Pack is used when packing a private RR into a buffer. + Pack([]byte) (int, error) + // Unpack is used when unpacking a private RR from a buffer. + Unpack([]byte) (int, error) PasteRdata(PrivateRdata) error + // RdataLen returns the length in octets of the Rdata. RdataLen() int } -// PrivateRR represents RR that uses PrivateRdata user-defined type. It mocks normal RRs and implements dns.RR interface. +// PrivateRR represents an RR that uses a PrivateRdata user-defined type. +// It mocks normal RRs and implements dns.RR interface. type PrivateRR struct { Hdr RR_Header Data PrivateRdata } -// Panics if RR is not an instance of PrivateRR func mkPrivateRR(rrtype uint16) *PrivateRR { + // Panics if RR is not an instance of PrivateRR. rrfunc, ok := typeToRR[rrtype] if !ok { panic(fmt.Sprintf("dns: invalid operation with Private RR type %d", rrtype)) @@ -51,13 +58,12 @@ func (r *PrivateRR) copy() RR { if err != nil { panic("dns: got value that could not be used to copy Private rdata") } - return rr } -// NewPrivateRR adds support for user-defined resource record type to internals of dns library. Requires -// string and numeric representation of RR type and generator function as argument. -func NewPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRdata) { +// PrivateHandle registers a private resource record type. It requires +// string and numeric representation of private RR type and generator function as argument. +func PrivateHandle(rtypestr string, rtype uint16, generator func() PrivateRdata) { rtypestr = strings.ToUpper(rtypestr) typeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} } @@ -72,6 +78,8 @@ func NewPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRdata) text := make([]string, 0, 2) // could be 0..N elements, median is probably 1 FETCH: for { + // TODO(miek): we could also be returning _QUOTE, this might or might not + // be an issue (basically parsing TXT becomes hard) switch l = <-c; l.value { case _NEWLINE, _EOF: break FETCH @@ -91,8 +99,8 @@ func NewPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRdata) typeToparserFunc[rtype] = parserFunc{setPrivateRR, false} } -// DelPrivateRR removes defenitions required to support user RR type. -func DelPrivateRR(rtype uint16) { +// PrivateHandleRemove removes defenitions required to support private RR type. +func PrivateHandleRemove(rtype uint16) { rtypestr, ok := TypeToString[rtype] if ok { delete(typeToRR, rtype) diff --git a/privaterr_test.go b/privaterr_test.go index 7b0a1a46..cabc5136 100644 --- a/privaterr_test.go +++ b/privaterr_test.go @@ -1,4 +1,4 @@ -package dns +package dns_test import ( "github.com/miekg/dns" @@ -6,7 +6,7 @@ import ( "testing" ) -const typeISBN uint16 = 0x0F01 +const TypeISBN uint16 = 0x0F01 // A crazy new RR type :) type ISBN struct { @@ -15,13 +15,15 @@ type ISBN struct { func NewISBN() dns.PrivateRdata { return &ISBN{""} } +func (rd *ISBN) RdataLen() int { return len([]byte(rd.x)) } func (rd *ISBN) String() string { return rd.x } + func (rd *ISBN) ParseTextSlice(txt []string) error { rd.x = strings.TrimSpace(strings.Join(txt, " ")) return nil } -func (rd *ISBN) WriteByteSlice(buf []byte) (int, error) { +func (rd *ISBN) Pack(buf []byte) (int, error) { b := []byte(rd.x) n := copy(buf, b) if n != len(b) { @@ -30,7 +32,7 @@ func (rd *ISBN) WriteByteSlice(buf []byte) (int, error) { return n, nil } -func (rd *ISBN) ParseByteSlice(buf []byte) (int, error) { +func (rd *ISBN) Unpack(buf []byte) (int, error) { rd.x = string(buf) return len(buf), nil } @@ -44,10 +46,6 @@ func (rd *ISBN) PasteRdata(dest dns.PrivateRdata) error { return nil } -func (rd *ISBN) RdataLen() int { - return len([]byte(rd.x)) -} - var testrecord = strings.Join([]string{"example.org.", "3600", "IN", "ISBN", "12-3 456789-0-123"}, "\t") func TestPrivateText(t *testing.T) {