Lots of renamed funcs and structures. More readability.
This commit is contained in:
parent
5b8552609c
commit
8aab8c6fb2
80
customrr.go
80
customrr.go
|
@ -1,48 +1,53 @@
|
||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PrivateRData is an interface to implement non-RFC dictated resource records. See also dns.PrivateRR, dns.RegisterPrivateRR and dns.UnregisterPrivateRR
|
// PrivateRdata is an interface to implement non-RFC dictated resource records. See also dns.PrivateRR, dns.NewPrivateRR and dns.DelPrivateRR
|
||||||
type PrivateRData interface {
|
type PrivateRdata interface {
|
||||||
String() string
|
String() string
|
||||||
ReadText([]string) error
|
ParseTextSlice([]string) error
|
||||||
Write([]byte) (int, error)
|
WriteByteSlice([]byte) (int, error)
|
||||||
Read([]byte) (int, error)
|
ParseByteSlice([]byte) (int, error)
|
||||||
CopyTo(PrivateRData) error
|
PasteRdata(PrivateRdata) error
|
||||||
RdataLen() int
|
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 {
|
type PrivateRR struct {
|
||||||
Hdr RR_Header
|
Hdr RR_Header
|
||||||
Data PrivateRData
|
Data PrivateRdata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header returns Private RR header.
|
// Panics if RR is not an instance of PrivateRR
|
||||||
func (r *PrivateRR) Header() *RR_Header { return &r.Hdr }
|
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.
|
anyrr := rrfunc()
|
||||||
func (r *PrivateRR) String() string { return r.Hdr.String() + r.Data.String() }
|
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.
|
// Private len and copy parts to satisfy RR interface.
|
||||||
func (r *PrivateRR) len() int { return r.Hdr.len() + r.Data.RdataLen() }
|
func (r *PrivateRR) len() int { return r.Hdr.len() + r.Data.RdataLen() }
|
||||||
func (r *PrivateRR) copy() RR {
|
func (r *PrivateRR) copy() RR {
|
||||||
// make new RR like this:
|
// make new RR like this:
|
||||||
rrfunc, ok := typeToRR[r.Hdr.Rrtype]
|
rr := mkPrivateRR(r.Hdr.Rrtype)
|
||||||
if !ok {
|
newh := r.Hdr.copyHeader()
|
||||||
panic("dns: invalid operation with Private RR " + r.Hdr.String())
|
rr.Hdr = *newh
|
||||||
}
|
|
||||||
rr := rrfunc()
|
|
||||||
r.Header().CopyTo(rr)
|
|
||||||
|
|
||||||
rrcust, ok := rr.(*PrivateRR)
|
err := r.Data.PasteRdata(rr.Data)
|
||||||
if !ok {
|
|
||||||
panic("dns: Private RR generator returned wrong interface value")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := r.Data.CopyTo(rrcust.Data)
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
@ -50,9 +55,9 @@ func (r *PrivateRR) copy() RR {
|
||||||
return 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.
|
// 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)
|
rtypestr = strings.ToUpper(rtypestr)
|
||||||
|
|
||||||
typeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
|
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
|
StringToType[rtypestr] = rtype
|
||||||
|
|
||||||
setPrivateRR := func(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
|
setPrivateRR := func(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
|
||||||
rrfunc := typeToRR[h.Rrtype]
|
rr := mkPrivateRR(h.Rrtype)
|
||||||
rr, ok := rrfunc().(*PrivateRR)
|
rr.Hdr = h
|
||||||
if !ok {
|
|
||||||
panic("dns: invalid handler registered for Private RR " + rtypestr)
|
|
||||||
}
|
|
||||||
h.CopyTo(rr)
|
|
||||||
|
|
||||||
var l lex
|
var l lex
|
||||||
text := make([]string, 0)
|
text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
|
||||||
for end := false; !end; {
|
FETCH:
|
||||||
|
for {
|
||||||
switch l = <-c; l.value {
|
switch l = <-c; l.value {
|
||||||
case _NEWLINE, _EOF:
|
case _NEWLINE, _EOF:
|
||||||
end = true
|
break FETCH
|
||||||
case _STRING:
|
case _STRING:
|
||||||
text = append(text, l.token)
|
text = append(text, l.token)
|
||||||
case _BLANK:
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := rr.Data.ReadText(text)
|
err := rr.Data.ParseTextSlice(text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &ParseError{f, err.Error(), l}, ""
|
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}
|
typeToparserFunc[rtype] = parserFunc{setPrivateRR, false}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnregisterPrivateRR removes defenitions required to support user RR type.
|
// DelPrivateRR removes defenitions required to support user RR type.
|
||||||
func UnregisterPrivateRR(rtype uint16) {
|
func DelPrivateRR(rtype uint16) {
|
||||||
rtypestr, ok := TypeToString[rtype]
|
rtypestr, ok := TypeToString[rtype]
|
||||||
if ok {
|
if ok {
|
||||||
delete(typeToRR, rtype)
|
delete(typeToRR, rtype)
|
||||||
|
|
|
@ -14,15 +14,15 @@ type ISBN struct {
|
||||||
x string // rdata with 10 or 13 numbers, dashes or spaces allowed
|
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) 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, " "))
|
rd.x = strings.TrimSpace(strings.Join(txt, " "))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *ISBN) Write(buf []byte) (int, error) {
|
func (rd *ISBN) WriteByteSlice(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) {
|
||||||
|
@ -31,12 +31,12 @@ func (rd *ISBN) Write(buf []byte) (int, error) {
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *ISBN) Read(buf []byte) (int, error) {
|
func (rd *ISBN) ParseByteSlice(buf []byte) (int, error) {
|
||||||
rd.x = string(buf)
|
rd.x = string(buf)
|
||||||
return len(buf), nil
|
return len(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *ISBN) CopyTo(dest dns.PrivateRData) error {
|
func (rd *ISBN) PasteRdata(dest dns.PrivateRdata) error {
|
||||||
isbn, ok := dest.(*ISBN)
|
isbn, ok := dest.(*ISBN)
|
||||||
if !ok {
|
if !ok {
|
||||||
return dns.ErrRdata
|
return dns.ErrRdata
|
||||||
|
@ -49,11 +49,11 @@ func (rd *ISBN) RdataLen() int {
|
||||||
return len([]byte(rd.x))
|
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) {
|
func TestPrivateText(t *testing.T) {
|
||||||
dns.RegisterPrivateRR("ISBN", TypeISBN, NewISBN)
|
dns.NewPrivateRR("ISBN", TypeISBN, NewISBN)
|
||||||
defer dns.UnregisterPrivateRR(TypeISBN)
|
defer dns.DelPrivateRR(TypeISBN)
|
||||||
|
|
||||||
rr, err := dns.NewRR(testrecord)
|
rr, err := dns.NewRR(testrecord)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -66,9 +66,9 @@ func TestPrivateText(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrivateWire(t *testing.T) {
|
func TestPrivateByteSlice(t *testing.T) {
|
||||||
dns.RegisterPrivateRR("ISBN", TypeISBN, NewISBN)
|
dns.NewPrivateRR("ISBN", TypeISBN, NewISBN)
|
||||||
defer dns.UnregisterPrivateRR(TypeISBN)
|
defer dns.DelPrivateRR(TypeISBN)
|
||||||
|
|
||||||
rr, err := dns.NewRR(testrecord)
|
rr, err := dns.NewRR(testrecord)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,18 +105,18 @@ func TestPrivateWire(t *testing.T) {
|
||||||
const TypeVERSION uint16 = 0x0F02
|
const TypeVERSION uint16 = 0x0F02
|
||||||
|
|
||||||
type VERSION struct {
|
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) 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, " "))
|
rd.x = strings.TrimSpace(strings.Join(txt, " "))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *VERSION) Write(buf []byte) (int, error) {
|
func (rd *VERSION) WriteByteSlice(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) {
|
||||||
|
@ -125,12 +125,12 @@ func (rd *VERSION) Write(buf []byte) (int, error) {
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *VERSION) Read(buf []byte) (int, error) {
|
func (rd *VERSION) ParseByteSlice(buf []byte) (int, error) {
|
||||||
rd.x = string(buf)
|
rd.x = string(buf)
|
||||||
return len(buf), nil
|
return len(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *VERSION) CopyTo(dest dns.PrivateRData) error {
|
func (rd *VERSION) PasteRdata(dest dns.PrivateRdata) error {
|
||||||
isbn, ok := dest.(*VERSION)
|
isbn, ok := dest.(*VERSION)
|
||||||
if !ok {
|
if !ok {
|
||||||
return dns.ErrRdata
|
return dns.ErrRdata
|
||||||
|
@ -157,10 +157,10 @@ www ISBN 1231-92110-16
|
||||||
`
|
`
|
||||||
|
|
||||||
func TestPrivateZoneParser(t *testing.T) {
|
func TestPrivateZoneParser(t *testing.T) {
|
||||||
dns.RegisterPrivateRR("ISBN", TypeISBN, NewISBN)
|
dns.NewPrivateRR("ISBN", TypeISBN, NewISBN)
|
||||||
dns.RegisterPrivateRR("VERSION", TypeVERSION, NewVersion)
|
dns.NewPrivateRR("VERSION", TypeVERSION, NewVersion)
|
||||||
defer dns.UnregisterPrivateRR(TypeISBN)
|
defer dns.DelPrivateRR(TypeISBN)
|
||||||
defer dns.UnregisterPrivateRR(TypeVERSION)
|
defer dns.DelPrivateRR(TypeVERSION)
|
||||||
|
|
||||||
r := strings.NewReader(smallzone)
|
r := strings.NewReader(smallzone)
|
||||||
for x := range dns.ParseZone(r, ".", "") {
|
for x := range dns.ParseZone(r, ".", "") {
|
||||||
|
|
|
@ -14,10 +14,10 @@ type APAIR struct {
|
||||||
addr [2]net.IP
|
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) 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 {
|
if len(txt) != 2 {
|
||||||
return errors.New("Two addresses required for APAIR")
|
return errors.New("Two addresses required for APAIR")
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ func (rd *APAIR) ReadText(txt []string) error {
|
||||||
return nil
|
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])...)
|
b := append([]byte(rd.addr[0]), []byte(rd.addr[1])...)
|
||||||
n := copy(buf, b)
|
n := copy(buf, b)
|
||||||
if n != len(b) {
|
if n != len(b) {
|
||||||
|
@ -40,7 +40,7 @@ func (rd *APAIR) Write(buf []byte) (int, error) {
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd *APAIR) Read(buf []byte) (int, error) {
|
func (rd *APAIR) ParseByteSlice(buf []byte) (int, error) {
|
||||||
ln := net.IPv4len * 2
|
ln := net.IPv4len * 2
|
||||||
if len(buf) != ln {
|
if len(buf) != ln {
|
||||||
return 0, errors.New("Invalid length of APAIR rdata")
|
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
|
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())
|
cp := make([]byte, rd.RdataLen())
|
||||||
_, err := rd.Write(cp)
|
_, err := rd.WriteByteSlice(cp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -71,9 +71,9 @@ func (rd *APAIR) RdataLen() int {
|
||||||
return net.IPv4len * 2
|
return net.IPv4len * 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRegisterPrivateRR() {
|
func ExampleNewPrivateRR() {
|
||||||
dns.RegisterPrivateRR("APAIR", TypeAPAIR, NewAPAIR)
|
dns.NewPrivateRR("APAIR", TypeAPAIR, NewAPAIR)
|
||||||
defer dns.UnregisterPrivateRR(TypeAPAIR)
|
defer dns.DelPrivateRR(TypeAPAIR)
|
||||||
|
|
||||||
rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4 1.2.3.5)")
|
rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4 1.2.3.5)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
10
dns.go
10
dns.go
|
@ -154,16 +154,6 @@ func (h *RR_Header) copyHeader() *RR_Header {
|
||||||
return r
|
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 {
|
func (h *RR_Header) String() string {
|
||||||
var s string
|
var s string
|
||||||
|
|
||||||
|
|
8
msg.go
8
msg.go
|
@ -577,8 +577,8 @@ func packStructValue(val reflect.Value, msg []byte, off int, compression map[str
|
||||||
default:
|
default:
|
||||||
return lenmsg, &Error{err: "bad kind packing"}
|
return lenmsg, &Error{err: "bad kind packing"}
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
if data, ok := fv.Interface().(PrivateRData); ok {
|
if data, ok := fv.Interface().(PrivateRdata); ok {
|
||||||
n, err := data.Write(msg[off:])
|
n, err := data.WriteByteSlice(msg[off:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lenmsg, err
|
return lenmsg, err
|
||||||
}
|
}
|
||||||
|
@ -880,8 +880,8 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, err er
|
||||||
default:
|
default:
|
||||||
return lenmsg, &Error{err: "bad kind unpacking"}
|
return lenmsg, &Error{err: "bad kind unpacking"}
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
if data, ok := fv.Interface().(PrivateRData); ok {
|
if data, ok := fv.Interface().(PrivateRdata); ok {
|
||||||
n, err := data.Read(msg[off:rdend])
|
n, err := data.ParseByteSlice(msg[off:rdend])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lenmsg, err
|
return lenmsg, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue