k3s/vendor/github.com/miekg/dns/privaterr.go

114 lines
3.2 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
package dns
import "strings"
2019-01-12 04:58:27 +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.
type PrivateRdata interface {
// String returns the text presentaton of the Rdata of the Private RR.
String() string
// Parse parses the Rdata of the private RR.
Parse([]string) error
// 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.
Unpack([]byte) (int, error)
// Copy copies the Rdata into the PrivateRdata argument.
2019-01-12 04:58:27 +00:00
Copy(PrivateRdata) error
// Len returns the length in octets of the Rdata.
Len() int
}
// PrivateRR represents an RR that uses a PrivateRdata user-defined type.
// It mocks normal RRs and implements dns.RR interface.
type PrivateRR struct {
Hdr RR_Header
Data PrivateRdata
2019-09-27 21:51:53 +00:00
generator func() PrivateRdata // for copy
2019-01-12 04:58:27 +00:00
}
// Header return the RR header of r.
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.
2019-09-27 21:51:53 +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
}
2019-01-12 04:58:27 +00:00
func (r *PrivateRR) copy() RR {
// make new RR like this:
rr := &PrivateRR{r.Hdr, r.generator(), r.generator}
2019-01-12 04:58:27 +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())
2019-01-12 04:58:27 +00:00
}
2019-01-12 04:58:27 +00:00
return rr
}
2019-09-27 21:51:53 +00:00
func (r *PrivateRR) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
2019-01-12 04:58:27 +00:00
n, err := r.Data.Pack(msg[off:])
if err != nil {
return len(msg), err
}
off += n
return off, nil
}
2019-09-27 21:51:53 +00:00
func (r *PrivateRR) unpack(msg []byte, off int) (int, error) {
off1, err := r.Data.Unpack(msg[off:])
off += off1
return off, err
}
2019-01-12 04:58:27 +00:00
func (r *PrivateRR) parse(c *zlexer, origin string) *ParseError {
2019-09-27 21:51:53 +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)
2019-01-12 04:58:27 +00:00
}
2019-09-27 21:51:53 +00:00
}
2019-01-12 04:58:27 +00:00
2019-09-27 21:51:53 +00:00
err := r.Data.Parse(text)
if err != nil {
return &ParseError{"", err.Error(), l}
2019-01-12 04:58:27 +00:00
}
2019-09-27 21:51:53 +00:00
return nil
}
2019-01-12 04:58:27 +00:00
2019-09-27 21:51:53 +00:00
func (r1 *PrivateRR) isDuplicate(r2 RR) bool { return false }
2019-01-12 04:58:27 +00:00
2019-09-27 21:51:53 +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) {
rtypestr = strings.ToUpper(rtypestr)
2019-01-12 04:58:27 +00:00
TypeToRR[rtype] = func() RR { return &PrivateRR{RR_Header{}, generator(), generator} }
2019-09-27 21:51:53 +00:00
TypeToString[rtype] = rtypestr
StringToType[rtypestr] = rtype
2019-01-12 04:58:27 +00:00
}
2019-09-27 21:51:53 +00:00
// PrivateHandleRemove removes definitions required to support private RR type.
2019-01-12 04:58:27 +00:00
func PrivateHandleRemove(rtype uint16) {
rtypestr, ok := TypeToString[rtype]
if ok {
delete(TypeToRR, rtype)
delete(TypeToString, rtype)
delete(StringToType, rtypestr)
}
}