2014-09-19 03:04:53 +00:00
|
|
|
package dns
|
|
|
|
|
2019-04-30 08:14:59 +00:00
|
|
|
import "strings"
|
2014-09-19 13:40:24 +00:00
|
|
|
|
2014-09-21 07:28:38 +00: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-20 22:04:02 +00:00
|
|
|
type PrivateRdata interface {
|
2014-09-21 07:28:38 +00:00
|
|
|
// String returns the text presentaton of the Rdata of the Private RR.
|
2014-09-19 03:04:53 +00:00
|
|
|
String() string
|
2014-09-21 18:09:13 +00:00
|
|
|
// Parse parses the Rdata of the private RR.
|
|
|
|
Parse([]string) error
|
2014-09-21 07:28:38 +00: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 07:38:17 +00:00
|
|
|
// TODO(miek): diff. signature than Pack, see edns0.go for instance.
|
2014-09-21 07:28:38 +00:00
|
|
|
Unpack([]byte) (int, error)
|
2019-04-30 08:14:59 +00:00
|
|
|
// Copy copies the Rdata into the PrivateRdata argument.
|
2014-09-21 17:30:16 +00:00
|
|
|
Copy(PrivateRdata) error
|
2014-09-21 17:21:34 +00:00
|
|
|
// Len returns the length in octets of the Rdata.
|
|
|
|
Len() int
|
2014-09-19 03:04:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 07:28:38 +00:00
|
|
|
// PrivateRR represents an RR that uses a PrivateRdata user-defined type.
|
|
|
|
// It mocks normal RRs and implements dns.RR interface.
|
2014-09-19 13:40:24 +00:00
|
|
|
type PrivateRR struct {
|
2014-09-19 03:04:53 +00:00
|
|
|
Hdr RR_Header
|
2014-09-20 22:04:02 +00:00
|
|
|
Data PrivateRdata
|
2019-01-04 10:19:42 +00:00
|
|
|
|
2019-04-30 08:14:59 +00:00
|
|
|
generator func() PrivateRdata // for copy
|
2014-09-20 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 07:03:13 +00:00
|
|
|
// Header return the RR header of r.
|
2014-09-20 22:04:02 +00:00
|
|
|
func (r *PrivateRR) Header() *RR_Header { return &r.Hdr }
|
2015-08-23 07:03:13 +00:00
|
|
|
|
2015-08-24 21:02:32 +00:00
|
|
|
func (r *PrivateRR) String() string { return r.Hdr.String() + r.Data.String() }
|
2014-09-19 13:40:24 +00:00
|
|
|
|
|
|
|
// Private len and copy parts to satisfy RR interface.
|
2018-11-29 23:33:41 +00: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 13:40:24 +00:00
|
|
|
func (r *PrivateRR) copy() RR {
|
2014-09-19 03:04:53 +00:00
|
|
|
// make new RR like this:
|
2019-04-30 08:14:59 +00:00
|
|
|
rr := &PrivateRR{r.Hdr, r.generator(), r.generator}
|
2014-09-19 03:04:53 +00:00
|
|
|
|
2019-04-30 08:14:59 +00:00
|
|
|
if err := r.Data.Copy(rr.Data); err != nil {
|
|
|
|
panic("dns: got value that could not be used to copy Private rdata: " + err.Error())
|
2014-09-19 03:04:53 +00:00
|
|
|
}
|
2019-04-30 08:14:59 +00:00
|
|
|
|
2014-09-19 03:04:53 +00:00
|
|
|
return rr
|
|
|
|
}
|
2018-12-02 08:23:35 +00:00
|
|
|
|
2019-01-03 23:39:14 +00:00
|
|
|
func (r *PrivateRR) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
|
2016-06-05 06:53:12 +00:00
|
|
|
n, err := r.Data.Pack(msg[off:])
|
|
|
|
if err != nil {
|
2019-01-03 23:39:14 +00:00
|
|
|
return len(msg), err
|
2016-06-05 06:53:12 +00:00
|
|
|
}
|
|
|
|
off += n
|
2019-01-03 23:39:14 +00:00
|
|
|
return off, nil
|
2016-06-05 06:53:12 +00:00
|
|
|
}
|
2014-09-19 03:04:53 +00:00
|
|
|
|
2019-01-03 16:35:32 +00:00
|
|
|
func (r *PrivateRR) unpack(msg []byte, off int) (int, error) {
|
|
|
|
off1, err := r.Data.Unpack(msg[off:])
|
|
|
|
off += off1
|
2019-01-04 10:19:42 +00:00
|
|
|
return off, err
|
2019-01-03 16:35:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 06:38:54 +00:00
|
|
|
func (r *PrivateRR) parse(c *zlexer, origin string) *ParseError {
|
2019-01-06 04:06:16 +00:00
|
|
|
var l lex
|
|
|
|
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.Next(); l.value {
|
|
|
|
case zNewline, zEOF:
|
|
|
|
break Fetch
|
|
|
|
case zString:
|
|
|
|
text = append(text, l.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Data.Parse(text)
|
|
|
|
if err != nil {
|
2019-06-10 06:38:54 +00:00
|
|
|
return &ParseError{"", err.Error(), l}
|
2019-01-06 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-06 04:12:38 +00:00
|
|
|
func (r1 *PrivateRR) isDuplicate(r2 RR) bool { return false }
|
|
|
|
|
2014-09-21 07:28:38 +00: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 13:40:24 +00:00
|
|
|
rtypestr = strings.ToUpper(rtypestr)
|
2014-09-19 03:04:53 +00:00
|
|
|
|
2019-04-30 08:14:59 +00:00
|
|
|
TypeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator(), generator} }
|
2014-09-19 03:04:53 +00:00
|
|
|
TypeToString[rtype] = rtypestr
|
|
|
|
StringToType[rtypestr] = rtype
|
|
|
|
}
|
|
|
|
|
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-26 18:32:05 +00:00
|
|
|
// PrivateHandleRemove removes definitions required to support private RR type.
|
2014-09-21 07:28:38 +00:00
|
|
|
func PrivateHandleRemove(rtype uint16) {
|
2014-09-19 03:04:53 +00:00
|
|
|
rtypestr, ok := TypeToString[rtype]
|
|
|
|
if ok {
|
2015-10-16 16:01:49 +00:00
|
|
|
delete(TypeToRR, rtype)
|
2014-09-19 03:04:53 +00:00
|
|
|
delete(TypeToString, rtype)
|
|
|
|
delete(StringToType, rtypestr)
|
|
|
|
}
|
|
|
|
}
|