PrivateRR: naming naming naming

Try to find better (=more in sync with the rest of the lib) naming. My
guess is that these are better, but YMMV.
This commit is contained in:
Miek Gieben 2014-09-21 08:28:38 +01:00
parent 7e792338bb
commit 9c455b0214
4 changed files with 27 additions and 21 deletions

4
msg.go
View File

@ -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
}

View File

@ -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)

View File

@ -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) {