Lots of renamed funcs and structures. More readability.

This commit is contained in:
Alex Sergeyev 2014-09-20 18:04:02 -04:00
parent 5b8552609c
commit 8aab8c6fb2
5 changed files with 74 additions and 84 deletions

View File

@ -1,48 +1,53 @@
package dns
import (
"fmt"
"strings"
)
// PrivateRData is an interface to implement non-RFC dictated resource records. See also dns.PrivateRR, dns.RegisterPrivateRR and dns.UnregisterPrivateRR
type PrivateRData interface {
// PrivateRdata is an interface to implement non-RFC dictated resource records. See also dns.PrivateRR, dns.NewPrivateRR and dns.DelPrivateRR
type PrivateRdata interface {
String() string
ReadText([]string) error
Write([]byte) (int, error)
Read([]byte) (int, error)
CopyTo(PrivateRData) error
ParseTextSlice([]string) error
WriteByteSlice([]byte) (int, error)
ParseByteSlice([]byte) (int, error)
PasteRdata(PrivateRdata) error
RdataLen() int
}
// PrivateRR represents RR that uses PrivateRData user-defined type. It mocks normal RRs and implements dns.RR interface.
// PrivateRR represents RR that uses PrivateRdata user-defined type. It mocks normal RRs and implements dns.RR interface.
type PrivateRR struct {
Hdr RR_Header
Data PrivateRData
Data PrivateRdata
}
// Header returns Private RR header.
func (r *PrivateRR) Header() *RR_Header { return &r.Hdr }
// Panics if RR is not an instance of PrivateRR
func mkPrivateRR(rrtype uint16) *PrivateRR {
rrfunc, ok := typeToRR[rrtype]
if !ok {
panic(fmt.Sprintf("dns: invalid operation with Private RR type %d", rrtype))
}
// String returns text representation of a Private Resource Record.
func (r *PrivateRR) String() string { return r.Hdr.String() + r.Data.String() }
anyrr := rrfunc()
switch rr := anyrr.(type) {
case *PrivateRR:
return rr
}
panic(fmt.Sprintf("dns: RR is not a PrivateRR, typeToRR[%d] generator returned %T", rrtype, anyrr))
}
func (r *PrivateRR) Header() *RR_Header { return &r.Hdr }
func (r *PrivateRR) String() string { return r.Hdr.String() + r.Data.String() }
// Private len and copy parts to satisfy RR interface.
func (r *PrivateRR) len() int { return r.Hdr.len() + r.Data.RdataLen() }
func (r *PrivateRR) copy() RR {
// make new RR like this:
rrfunc, ok := typeToRR[r.Hdr.Rrtype]
if !ok {
panic("dns: invalid operation with Private RR " + r.Hdr.String())
}
rr := rrfunc()
r.Header().CopyTo(rr)
rr := mkPrivateRR(r.Hdr.Rrtype)
newh := r.Hdr.copyHeader()
rr.Hdr = *newh
rrcust, ok := rr.(*PrivateRR)
if !ok {
panic("dns: Private RR generator returned wrong interface value")
}
err := r.Data.CopyTo(rrcust.Data)
err := r.Data.PasteRdata(rr.Data)
if err != nil {
panic("dns: got value that could not be used to copy Private rdata")
}
@ -50,9 +55,9 @@ func (r *PrivateRR) copy() RR {
return rr
}
// RegisterPrivateRR adds support for user-defined resource record type to internals of dns library. Requires
// 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 RegisterPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRData) {
func NewPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRdata) {
rtypestr = strings.ToUpper(rtypestr)
typeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
@ -60,27 +65,22 @@ func RegisterPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRD
StringToType[rtypestr] = rtype
setPrivateRR := func(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rrfunc := typeToRR[h.Rrtype]
rr, ok := rrfunc().(*PrivateRR)
if !ok {
panic("dns: invalid handler registered for Private RR " + rtypestr)
}
h.CopyTo(rr)
rr := mkPrivateRR(h.Rrtype)
rr.Hdr = h
var l lex
text := make([]string, 0)
for end := false; !end; {
text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
FETCH:
for {
switch l = <-c; l.value {
case _NEWLINE, _EOF:
end = true
break FETCH
case _STRING:
text = append(text, l.token)
case _BLANK:
continue
}
}
err := rr.Data.ReadText(text)
err := rr.Data.ParseTextSlice(text)
if err != nil {
return nil, &ParseError{f, err.Error(), l}, ""
}
@ -91,8 +91,8 @@ func RegisterPrivateRR(rtypestr string, rtype uint16, generator func() PrivateRD
typeToparserFunc[rtype] = parserFunc{setPrivateRR, false}
}
// UnregisterPrivateRR removes defenitions required to support user RR type.
func UnregisterPrivateRR(rtype uint16) {
// DelPrivateRR removes defenitions required to support user RR type.
func DelPrivateRR(rtype uint16) {
rtypestr, ok := TypeToString[rtype]
if ok {
delete(typeToRR, rtype)

View File

@ -14,15 +14,15 @@ type ISBN struct {
x string // rdata with 10 or 13 numbers, dashes or spaces allowed
}
func NewISBN() dns.PrivateRData { return &ISBN{""} }
func NewISBN() dns.PrivateRdata { return &ISBN{""} }
func (rd *ISBN) String() string { return rd.x }
func (rd *ISBN) ReadText(txt []string) error {
func (rd *ISBN) ParseTextSlice(txt []string) error {
rd.x = strings.TrimSpace(strings.Join(txt, " "))
return nil
}
func (rd *ISBN) Write(buf []byte) (int, error) {
func (rd *ISBN) WriteByteSlice(buf []byte) (int, error) {
b := []byte(rd.x)
n := copy(buf, b)
if n != len(b) {
@ -31,12 +31,12 @@ func (rd *ISBN) Write(buf []byte) (int, error) {
return n, nil
}
func (rd *ISBN) Read(buf []byte) (int, error) {
func (rd *ISBN) ParseByteSlice(buf []byte) (int, error) {
rd.x = string(buf)
return len(buf), nil
}
func (rd *ISBN) CopyTo(dest dns.PrivateRData) error {
func (rd *ISBN) PasteRdata(dest dns.PrivateRdata) error {
isbn, ok := dest.(*ISBN)
if !ok {
return dns.ErrRdata
@ -49,11 +49,11 @@ func (rd *ISBN) RdataLen() int {
return len([]byte(rd.x))
}
var testrecord = "example.org.\t3600\tIN\tISBN\t12-3 456789-0-123"
var testrecord = strings.Join([]string{"example.org.", "3600", "IN", "ISBN", "12-3 456789-0-123"}, "\t")
func TestPrivateText(t *testing.T) {
dns.RegisterPrivateRR("ISBN", TypeISBN, NewISBN)
defer dns.UnregisterPrivateRR(TypeISBN)
dns.NewPrivateRR("ISBN", TypeISBN, NewISBN)
defer dns.DelPrivateRR(TypeISBN)
rr, err := dns.NewRR(testrecord)
if err != nil {
@ -66,9 +66,9 @@ func TestPrivateText(t *testing.T) {
}
}
func TestPrivateWire(t *testing.T) {
dns.RegisterPrivateRR("ISBN", TypeISBN, NewISBN)
defer dns.UnregisterPrivateRR(TypeISBN)
func TestPrivateByteSlice(t *testing.T) {
dns.NewPrivateRR("ISBN", TypeISBN, NewISBN)
defer dns.DelPrivateRR(TypeISBN)
rr, err := dns.NewRR(testrecord)
if err != nil {
@ -105,18 +105,18 @@ func TestPrivateWire(t *testing.T) {
const TypeVERSION uint16 = 0x0F02
type VERSION struct {
x string // to make it simpler it's just as simple as ISBN but ignoring
x string
}
func NewVersion() dns.PrivateRData { return &VERSION{""} }
func NewVersion() dns.PrivateRdata { return &VERSION{""} }
func (rd *VERSION) String() string { return rd.x }
func (rd *VERSION) ReadText(txt []string) error {
func (rd *VERSION) ParseTextSlice(txt []string) error {
rd.x = strings.TrimSpace(strings.Join(txt, " "))
return nil
}
func (rd *VERSION) Write(buf []byte) (int, error) {
func (rd *VERSION) WriteByteSlice(buf []byte) (int, error) {
b := []byte(rd.x)
n := copy(buf, b)
if n != len(b) {
@ -125,12 +125,12 @@ func (rd *VERSION) Write(buf []byte) (int, error) {
return n, nil
}
func (rd *VERSION) Read(buf []byte) (int, error) {
func (rd *VERSION) ParseByteSlice(buf []byte) (int, error) {
rd.x = string(buf)
return len(buf), nil
}
func (rd *VERSION) CopyTo(dest dns.PrivateRData) error {
func (rd *VERSION) PasteRdata(dest dns.PrivateRdata) error {
isbn, ok := dest.(*VERSION)
if !ok {
return dns.ErrRdata
@ -157,10 +157,10 @@ www ISBN 1231-92110-16
`
func TestPrivateZoneParser(t *testing.T) {
dns.RegisterPrivateRR("ISBN", TypeISBN, NewISBN)
dns.RegisterPrivateRR("VERSION", TypeVERSION, NewVersion)
defer dns.UnregisterPrivateRR(TypeISBN)
defer dns.UnregisterPrivateRR(TypeVERSION)
dns.NewPrivateRR("ISBN", TypeISBN, NewISBN)
dns.NewPrivateRR("VERSION", TypeVERSION, NewVersion)
defer dns.DelPrivateRR(TypeISBN)
defer dns.DelPrivateRR(TypeVERSION)
r := strings.NewReader(smallzone)
for x := range dns.ParseZone(r, ".", "") {

View File

@ -14,10 +14,10 @@ type APAIR struct {
addr [2]net.IP
}
func NewAPAIR() dns.PrivateRData { return new(APAIR) }
func NewAPAIR() dns.PrivateRdata { return new(APAIR) }
func (rd *APAIR) String() string { return rd.addr[0].String() + " " + rd.addr[1].String() }
func (rd *APAIR) ReadText(txt []string) error {
func (rd *APAIR) ParseTextSlice(txt []string) error {
if len(txt) != 2 {
return errors.New("Two addresses required for APAIR")
}
@ -31,7 +31,7 @@ func (rd *APAIR) ReadText(txt []string) error {
return nil
}
func (rd *APAIR) Write(buf []byte) (int, error) {
func (rd *APAIR) WriteByteSlice(buf []byte) (int, error) {
b := append([]byte(rd.addr[0]), []byte(rd.addr[1])...)
n := copy(buf, b)
if n != len(b) {
@ -40,7 +40,7 @@ func (rd *APAIR) Write(buf []byte) (int, error) {
return n, nil
}
func (rd *APAIR) Read(buf []byte) (int, error) {
func (rd *APAIR) ParseByteSlice(buf []byte) (int, error) {
ln := net.IPv4len * 2
if len(buf) != ln {
return 0, errors.New("Invalid length of APAIR rdata")
@ -54,9 +54,9 @@ func (rd *APAIR) Read(buf []byte) (int, error) {
return len(buf), nil
}
func (rd *APAIR) CopyTo(dest dns.PrivateRData) error {
func (rd *APAIR) PasteRdata(dest dns.PrivateRdata) error {
cp := make([]byte, rd.RdataLen())
_, err := rd.Write(cp)
_, err := rd.WriteByteSlice(cp)
if err != nil {
return err
}
@ -71,9 +71,9 @@ func (rd *APAIR) RdataLen() int {
return net.IPv4len * 2
}
func ExampleRegisterPrivateRR() {
dns.RegisterPrivateRR("APAIR", TypeAPAIR, NewAPAIR)
defer dns.UnregisterPrivateRR(TypeAPAIR)
func ExampleNewPrivateRR() {
dns.NewPrivateRR("APAIR", TypeAPAIR, NewAPAIR)
defer dns.DelPrivateRR(TypeAPAIR)
rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4 1.2.3.5)")
if err != nil {

10
dns.go
View File

@ -154,16 +154,6 @@ func (h *RR_Header) copyHeader() *RR_Header {
return r
}
func (h *RR_Header) CopyTo(rr RR) *RR_Header {
rrh := rr.Header()
rrh.Name = h.Name
rrh.Rrtype = h.Rrtype
rrh.Class = h.Class
rrh.Ttl = h.Ttl
rrh.Rdlength = h.Rdlength
return rrh
}
func (h *RR_Header) String() string {
var s string

8
msg.go
View File

@ -577,8 +577,8 @@ func packStructValue(val reflect.Value, msg []byte, off int, compression map[str
default:
return lenmsg, &Error{err: "bad kind packing"}
case reflect.Interface:
if data, ok := fv.Interface().(PrivateRData); ok {
n, err := data.Write(msg[off:])
if data, ok := fv.Interface().(PrivateRdata); ok {
n, err := data.WriteByteSlice(msg[off:])
if err != nil {
return lenmsg, err
}
@ -880,8 +880,8 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, err er
default:
return lenmsg, &Error{err: "bad kind unpacking"}
case reflect.Interface:
if data, ok := fv.Interface().(PrivateRData); ok {
n, err := data.Read(msg[off:rdend])
if data, ok := fv.Interface().(PrivateRdata); ok {
n, err := data.ParseByteSlice(msg[off:rdend])
if err != nil {
return lenmsg, err
}