2014-09-19 13:04:53 +10:00
|
|
|
package dns
|
|
|
|
|
2014-09-19 23:40:24 +10:00
|
|
|
import (
|
2014-09-21 08:04:02 +10:00
|
|
|
"fmt"
|
2014-09-19 23:40:24 +10:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-09-21 17:28:38 +10:00
|
|
|
// 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.
|
2014-09-21 08:04:02 +10:00
|
|
|
type PrivateRdata interface {
|
2014-09-21 17:28:38 +10:00
|
|
|
// String returns the text presentaton of the Rdata of the Private RR.
|
2014-09-19 13:04:53 +10:00
|
|
|
String() string
|
2014-09-22 04:09:13 +10:00
|
|
|
// Parse parses the Rdata of the private RR.
|
|
|
|
Parse([]string) error
|
2014-09-21 17:28:38 +10:00
|
|
|
// 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.
|
2014-09-21 17:38:17 +10:00
|
|
|
// TODO(miek): diff. signature than Pack, see edns0.go for instance.
|
2014-09-21 17:28:38 +10:00
|
|
|
Unpack([]byte) (int, error)
|
2014-09-22 03:30:16 +10:00
|
|
|
// Copy copies the Rdata.
|
|
|
|
Copy(PrivateRdata) error
|
2014-09-22 03:21:34 +10:00
|
|
|
// Len returns the length in octets of the Rdata.
|
|
|
|
Len() int
|
2014-09-19 13:04:53 +10:00
|
|
|
}
|
|
|
|
|
2014-09-21 17:28:38 +10:00
|
|
|
// PrivateRR represents an RR that uses a PrivateRdata user-defined type.
|
|
|
|
// It mocks normal RRs and implements dns.RR interface.
|
2014-09-19 23:40:24 +10:00
|
|
|
type PrivateRR struct {
|
2014-09-19 13:04:53 +10:00
|
|
|
Hdr RR_Header
|
2014-09-21 08:04:02 +10:00
|
|
|
Data PrivateRdata
|
2014-09-19 13:04:53 +10:00
|
|
|
}
|
|
|
|
|
2014-09-21 08:04:02 +10:00
|
|
|
func mkPrivateRR(rrtype uint16) *PrivateRR {
|
2014-09-21 17:28:38 +10:00
|
|
|
// Panics if RR is not an instance of PrivateRR.
|
2015-10-17 03:01:49 +11:00
|
|
|
rrfunc, ok := TypeToRR[rrtype]
|
2014-09-21 08:04:02 +10:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("dns: invalid operation with Private RR type %d", rrtype))
|
|
|
|
}
|
2014-09-19 23:40:24 +10:00
|
|
|
|
2014-09-21 08:04:02 +10:00
|
|
|
anyrr := rrfunc()
|
|
|
|
switch rr := anyrr.(type) {
|
|
|
|
case *PrivateRR:
|
|
|
|
return rr
|
|
|
|
}
|
2015-10-17 03:01:49 +11:00
|
|
|
panic(fmt.Sprintf("dns: RR is not a PrivateRR, TypeToRR[%d] generator returned %T", rrtype, anyrr))
|
2014-09-21 08:04:02 +10:00
|
|
|
}
|
|
|
|
|
2015-08-23 17:03:13 +10:00
|
|
|
// Header return the RR header of r.
|
2014-09-21 08:04:02 +10:00
|
|
|
func (r *PrivateRR) Header() *RR_Header { return &r.Hdr }
|
2015-08-23 17:03:13 +10:00
|
|
|
|
2015-08-25 07:02:32 +10:00
|
|
|
func (r *PrivateRR) String() string { return r.Hdr.String() + r.Data.String() }
|
2014-09-19 23:40:24 +10:00
|
|
|
|
|
|
|
// Private len and copy parts to satisfy RR interface.
|
2018-11-30 10:33:41 +11:00
|
|
|
func (r *PrivateRR) len(off int, compression map[string]struct{}) int {
|
|
|
|
l := r.Hdr.len(off, compression)
|
|
|
|
l += r.Data.Len()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2014-09-19 23:40:24 +10:00
|
|
|
func (r *PrivateRR) copy() RR {
|
2014-09-19 13:04:53 +10:00
|
|
|
// make new RR like this:
|
2014-09-21 08:04:02 +10:00
|
|
|
rr := mkPrivateRR(r.Hdr.Rrtype)
|
2018-05-10 23:50:26 +10:00
|
|
|
rr.Hdr = r.Hdr
|
2014-09-19 13:04:53 +10:00
|
|
|
|
2014-09-22 03:30:16 +10:00
|
|
|
err := r.Data.Copy(rr.Data)
|
2014-09-19 13:04:53 +10:00
|
|
|
if err != nil {
|
2014-09-19 23:40:24 +10:00
|
|
|
panic("dns: got value that could not be used to copy Private rdata")
|
2014-09-19 13:04:53 +10:00
|
|
|
}
|
|
|
|
return rr
|
|
|
|
}
|
2018-12-02 09:20:51 +11:00
|
|
|
func (r *PrivateRR) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
|
2016-06-13 01:09:37 +10:00
|
|
|
off, err := r.Hdr.pack(msg, off, compression, compress)
|
|
|
|
if err != nil {
|
|
|
|
return off, err
|
|
|
|
}
|
|
|
|
headerEnd := off
|
2016-06-05 16:53:12 +10:00
|
|
|
n, err := r.Data.Pack(msg[off:])
|
|
|
|
if err != nil {
|
|
|
|
return len(msg), err
|
|
|
|
}
|
|
|
|
off += n
|
2016-06-13 01:09:37 +10:00
|
|
|
r.Header().Rdlength = uint16(off - headerEnd)
|
2016-06-05 16:53:12 +10:00
|
|
|
return off, nil
|
|
|
|
}
|
2014-09-19 13:04:53 +10:00
|
|
|
|
2014-09-21 17:28:38 +10:00
|
|
|
// 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) {
|
2014-09-19 23:40:24 +10:00
|
|
|
rtypestr = strings.ToUpper(rtypestr)
|
2014-09-19 13:04:53 +10:00
|
|
|
|
2015-10-17 03:01:49 +11:00
|
|
|
TypeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator()} }
|
2014-09-19 13:04:53 +10:00
|
|
|
TypeToString[rtype] = rtypestr
|
|
|
|
StringToType[rtypestr] = rtype
|
|
|
|
|
2016-06-13 01:09:37 +10:00
|
|
|
typeToUnpack[rtype] = func(h RR_Header, msg []byte, off int) (RR, int, error) {
|
|
|
|
if noRdata(h) {
|
|
|
|
return &h, off, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
rr := mkPrivateRR(h.Rrtype)
|
|
|
|
rr.Hdr = h
|
|
|
|
|
|
|
|
off1, err := rr.Data.Unpack(msg[off:])
|
|
|
|
off += off1
|
|
|
|
if err != nil {
|
|
|
|
return rr, off, err
|
|
|
|
}
|
|
|
|
return rr, off, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 18:12:31 +11:00
|
|
|
setPrivateRR := func(h RR_Header, c *zlexer, o, f string) (RR, *ParseError, string) {
|
2014-09-21 08:04:02 +10:00
|
|
|
rr := mkPrivateRR(h.Rrtype)
|
|
|
|
rr.Hdr = h
|
2014-09-19 13:04:53 +10:00
|
|
|
|
|
|
|
var l lex
|
2014-09-21 08:04:02 +10:00
|
|
|
text := make([]string, 0, 2) // could be 0..N elements, median is probably 1
|
2016-06-13 01:09:37 +10:00
|
|
|
Fetch:
|
2014-09-21 08:04:02 +10:00
|
|
|
for {
|
2014-09-21 17:28:38 +10:00
|
|
|
// TODO(miek): we could also be returning _QUOTE, this might or might not
|
|
|
|
// be an issue (basically parsing TXT becomes hard)
|
2018-10-15 18:12:31 +11:00
|
|
|
switch l, _ = c.Next(); l.value {
|
2015-02-19 21:45:59 +11:00
|
|
|
case zNewline, zEOF:
|
2016-06-13 01:09:37 +10:00
|
|
|
break Fetch
|
2015-02-19 21:45:59 +11:00
|
|
|
case zString:
|
2014-09-19 13:04:53 +10:00
|
|
|
text = append(text, l.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 04:09:13 +10:00
|
|
|
err := rr.Data.Parse(text)
|
2014-09-19 13:04:53 +10:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{f, err.Error(), l}, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return rr, nil, ""
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:59:09 +10:00
|
|
|
typeToparserFunc[rtype] = parserFunc{setPrivateRR, true}
|
2014-09-19 13:04:53 +10:00
|
|
|
}
|
|
|
|
|
Fix dominikh/go-tools nits (#758)
* Remove unused functions and consts
* Address gosimple nits
* Address staticcheck nits
This excludes several that were intentional or weren't actual errors.
* Reduce size of lex struct
This reduces the size of the lex struct by 8 bytes from:
lex.token string: 0-16 (size 16, align 8)
lex.tokenUpper string: 16-32 (size 16, align 8)
lex.length int: 32-40 (size 8, align 8)
lex.err bool: 40-41 (size 1, align 1)
lex.value uint8: 41-42 (size 1, align 1)
padding: 42-48 (size 6, align 0)
lex.line int: 48-56 (size 8, align 8)
lex.column int: 56-64 (size 8, align 8)
lex.torc uint16: 64-66 (size 2, align 2)
padding: 66-72 (size 6, align 0)
lex.comment string: 72-88 (size 16, align 8)
to:
lex.token string: 0-16 (size 16, align 8)
lex.tokenUpper string: 16-32 (size 16, align 8)
lex.length int: 32-40 (size 8, align 8)
lex.err bool: 40-41 (size 1, align 1)
lex.value uint8: 41-42 (size 1, align 1)
lex.torc uint16: 42-44 (size 2, align 2)
padding: 44-48 (size 4, align 0)
lex.line int: 48-56 (size 8, align 8)
lex.column int: 56-64 (size 8, align 8)
lex.comment string: 64-80 (size 16, align 8)
* Reduce size of response struct
This reduces the size of the response struct by 8 bytes from:
response.msg []byte: 0-24 (size 24, align 8)
response.hijacked bool: 24-25 (size 1, align 1)
padding: 25-32 (size 7, align 0)
response.tsigStatus error: 32-48 (size 16, align 8)
response.tsigTimersOnly bool: 48-49 (size 1, align 1)
padding: 49-56 (size 7, align 0)
response.tsigRequestMAC string: 56-72 (size 16, align 8)
response.tsigSecret map[string]string: 72-80 (size 8, align 8)
response.udp *net.UDPConn: 80-88 (size 8, align 8)
response.tcp net.Conn: 88-104 (size 16, align 8)
response.udpSession *github.com/tmthrgd/dns.SessionUDP: 104-112 (size 8, align 8)
response.writer github.com/tmthrgd/dns.Writer: 112-128 (size 16, align 8)
response.wg *sync.WaitGroup: 128-136 (size 8, align 8)
to:
response.msg []byte: 0-24 (size 24, align 8)
response.hijacked bool: 24-25 (size 1, align 1)
response.tsigTimersOnly bool: 25-26 (size 1, align 1)
padding: 26-32 (size 6, align 0)
response.tsigStatus error: 32-48 (size 16, align 8)
response.tsigRequestMAC string: 48-64 (size 16, align 8)
response.tsigSecret map[string]string: 64-72 (size 8, align 8)
response.udp *net.UDPConn: 72-80 (size 8, align 8)
response.tcp net.Conn: 80-96 (size 16, align 8)
response.udpSession *github.com/tmthrgd/dns.SessionUDP: 96-104 (size 8, align 8)
response.writer github.com/tmthrgd/dns.Writer: 104-120 (size 16, align 8)
response.wg *sync.WaitGroup: 120-128 (size 8, align 8)
2018-09-27 04:32:05 +10:00
|
|
|
// PrivateHandleRemove removes definitions required to support private RR type.
|
2014-09-21 17:28:38 +10:00
|
|
|
func PrivateHandleRemove(rtype uint16) {
|
2014-09-19 13:04:53 +10:00
|
|
|
rtypestr, ok := TypeToString[rtype]
|
|
|
|
if ok {
|
2015-10-17 03:01:49 +11:00
|
|
|
delete(TypeToRR, rtype)
|
2014-09-19 13:04:53 +10:00
|
|
|
delete(TypeToString, rtype)
|
|
|
|
delete(typeToparserFunc, rtype)
|
|
|
|
delete(StringToType, rtypestr)
|
2016-06-13 01:09:37 +10:00
|
|
|
delete(typeToUnpack, rtype)
|
2014-09-19 13:04:53 +10:00
|
|
|
}
|
|
|
|
}
|