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:
parent
7e792338bb
commit
9c455b0214
4
msg.go
4
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
|
// therefore it's expected that this interface would be PrivateRdata
|
||||||
switch data := fv.Interface().(type) {
|
switch data := fv.Interface().(type) {
|
||||||
case PrivateRdata:
|
case PrivateRdata:
|
||||||
n, err := data.WriteByteSlice(msg[off:])
|
n, err := data.Pack(msg[off:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lenmsg, err
|
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
|
// therefore it's expected that this interface would be PrivateRdata
|
||||||
switch data := fv.Interface().(type) {
|
switch data := fv.Interface().(type) {
|
||||||
case PrivateRdata:
|
case PrivateRdata:
|
||||||
n, err := data.ParseByteSlice(msg[off:rdend])
|
n, err := data.Unpack(msg[off:rdend])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lenmsg, err
|
return lenmsg, err
|
||||||
}
|
}
|
||||||
|
|
30
privaterr.go
30
privaterr.go
|
@ -5,24 +5,31 @@ import (
|
||||||
"strings"
|
"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 {
|
type PrivateRdata interface {
|
||||||
|
// String returns the text presentaton of the Rdata of the Private RR.
|
||||||
String() string
|
String() string
|
||||||
ParseTextSlice([]string) error
|
ParseTextSlice([]string) error
|
||||||
WriteByteSlice([]byte) (int, error)
|
// Pack is used when packing a private RR into a buffer.
|
||||||
ParseByteSlice([]byte) (int, error)
|
Pack([]byte) (int, error)
|
||||||
|
// Unpack is used when unpacking a private RR from a buffer.
|
||||||
|
Unpack([]byte) (int, error)
|
||||||
PasteRdata(PrivateRdata) error
|
PasteRdata(PrivateRdata) error
|
||||||
|
// RdataLen returns the length in octets of the Rdata.
|
||||||
RdataLen() int
|
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 {
|
type PrivateRR struct {
|
||||||
Hdr RR_Header
|
Hdr RR_Header
|
||||||
Data PrivateRdata
|
Data PrivateRdata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panics if RR is not an instance of PrivateRR
|
|
||||||
func mkPrivateRR(rrtype uint16) *PrivateRR {
|
func mkPrivateRR(rrtype uint16) *PrivateRR {
|
||||||
|
// Panics if RR is not an instance of PrivateRR.
|
||||||
rrfunc, ok := typeToRR[rrtype]
|
rrfunc, ok := typeToRR[rrtype]
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Sprintf("dns: invalid operation with Private RR type %d", rrtype))
|
panic(fmt.Sprintf("dns: invalid operation with Private RR type %d", rrtype))
|
||||||
|
@ -51,13 +58,12 @@ func (r *PrivateRR) copy() RR {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("dns: got value that could not be used to copy Private rdata")
|
panic("dns: got value that could not be used to copy Private rdata")
|
||||||
}
|
}
|
||||||
|
|
||||||
return rr
|
return rr
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPrivateRR adds support for user-defined resource record type to internals of dns library. Requires
|
// PrivateHandle registers a private resource record type. It requires
|
||||||
// string and numeric representation of RR type and generator function as argument.
|
// string and numeric representation of private RR type and generator function as argument.
|
||||||
func NewPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRdata) {
|
func PrivateHandle(rtypestr string, rtype uint16, generator func() PrivateRdata) {
|
||||||
rtypestr = strings.ToUpper(rtypestr)
|
rtypestr = strings.ToUpper(rtypestr)
|
||||||
|
|
||||||
typeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
|
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
|
text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
|
||||||
FETCH:
|
FETCH:
|
||||||
for {
|
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 {
|
switch l = <-c; l.value {
|
||||||
case _NEWLINE, _EOF:
|
case _NEWLINE, _EOF:
|
||||||
break FETCH
|
break FETCH
|
||||||
|
@ -91,8 +99,8 @@ func NewPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRdata)
|
||||||
typeToparserFunc[rtype] = parserFunc{setPrivateRR, false}
|
typeToparserFunc[rtype] = parserFunc{setPrivateRR, false}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelPrivateRR removes defenitions required to support user RR type.
|
// PrivateHandleRemove removes defenitions required to support private RR type.
|
||||||
func DelPrivateRR(rtype uint16) {
|
func PrivateHandleRemove(rtype uint16) {
|
||||||
rtypestr, ok := TypeToString[rtype]
|
rtypestr, ok := TypeToString[rtype]
|
||||||
if ok {
|
if ok {
|
||||||
delete(typeToRR, rtype)
|
delete(typeToRR, rtype)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package dns
|
package dns_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
@ -6,7 +6,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
const typeISBN uint16 = 0x0F01
|
const TypeISBN uint16 = 0x0F01
|
||||||
|
|
||||||
// A crazy new RR type :)
|
// A crazy new RR type :)
|
||||||
type ISBN struct {
|
type ISBN struct {
|
||||||
|
@ -15,13 +15,15 @@ type ISBN struct {
|
||||||
|
|
||||||
func NewISBN() dns.PrivateRdata { return &ISBN{""} }
|
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) String() string { return rd.x }
|
||||||
|
|
||||||
func (rd *ISBN) ParseTextSlice(txt []string) error {
|
func (rd *ISBN) ParseTextSlice(txt []string) error {
|
||||||
rd.x = strings.TrimSpace(strings.Join(txt, " "))
|
rd.x = strings.TrimSpace(strings.Join(txt, " "))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *ISBN) WriteByteSlice(buf []byte) (int, error) {
|
func (rd *ISBN) Pack(buf []byte) (int, error) {
|
||||||
b := []byte(rd.x)
|
b := []byte(rd.x)
|
||||||
n := copy(buf, b)
|
n := copy(buf, b)
|
||||||
if n != len(b) {
|
if n != len(b) {
|
||||||
|
@ -30,7 +32,7 @@ func (rd *ISBN) WriteByteSlice(buf []byte) (int, error) {
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *ISBN) ParseByteSlice(buf []byte) (int, error) {
|
func (rd *ISBN) Unpack(buf []byte) (int, error) {
|
||||||
rd.x = string(buf)
|
rd.x = string(buf)
|
||||||
return len(buf), nil
|
return len(buf), nil
|
||||||
}
|
}
|
||||||
|
@ -44,10 +46,6 @@ func (rd *ISBN) PasteRdata(dest dns.PrivateRdata) error {
|
||||||
return nil
|
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")
|
var testrecord = strings.Join([]string{"example.org.", "3600", "IN", "ISBN", "12-3 456789-0-123"}, "\t")
|
||||||
|
|
||||||
func TestPrivateText(t *testing.T) {
|
func TestPrivateText(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue