2018-07-04 10:51:47 +00:00
|
|
|
package madns
|
|
|
|
|
|
|
|
import (
|
2019-06-09 07:24:20 +00:00
|
|
|
"bytes"
|
2018-07-04 10:51:47 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Extracted from source of truth for multicodec codes: https://github.com/multiformats/multicodec
|
|
|
|
const (
|
|
|
|
P_DNS4 = 0x0036
|
|
|
|
P_DNS6 = 0x0037
|
|
|
|
P_DNSADDR = 0x0038
|
|
|
|
)
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
var Dns4Protocol = ma.Protocol{
|
2019-06-09 07:24:20 +00:00
|
|
|
Code: P_DNS4,
|
2018-07-04 10:51:47 +00:00
|
|
|
Size: ma.LengthPrefixedVarSize,
|
|
|
|
Name: "dns4",
|
2019-06-09 07:24:20 +00:00
|
|
|
VCode: ma.CodeToVarint(P_DNS4),
|
2018-07-04 10:51:47 +00:00
|
|
|
Transcoder: DnsTranscoder,
|
|
|
|
}
|
|
|
|
var Dns6Protocol = ma.Protocol{
|
2019-06-09 07:24:20 +00:00
|
|
|
Code: P_DNS6,
|
2018-07-04 10:51:47 +00:00
|
|
|
Size: ma.LengthPrefixedVarSize,
|
|
|
|
Name: "dns6",
|
2019-06-09 07:24:20 +00:00
|
|
|
VCode: ma.CodeToVarint(P_DNS6),
|
2018-07-04 10:51:47 +00:00
|
|
|
Transcoder: DnsTranscoder,
|
|
|
|
}
|
|
|
|
var DnsaddrProtocol = ma.Protocol{
|
2019-06-09 07:24:20 +00:00
|
|
|
Code: P_DNSADDR,
|
2018-07-04 10:51:47 +00:00
|
|
|
Size: ma.LengthPrefixedVarSize,
|
|
|
|
Name: "dnsaddr",
|
2019-06-09 07:24:20 +00:00
|
|
|
VCode: ma.CodeToVarint(P_DNSADDR),
|
2018-07-04 10:51:47 +00:00
|
|
|
Transcoder: DnsTranscoder,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
err := ma.AddProtocol(Dns4Protocol)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error registering dns4 protocol: %s", err))
|
|
|
|
}
|
|
|
|
err = ma.AddProtocol(Dns6Protocol)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error registering dns6 protocol: %s", err))
|
|
|
|
}
|
|
|
|
err = ma.AddProtocol(DnsaddrProtocol)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error registering dnsaddr protocol: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
var DnsTranscoder = ma.NewTranscoderFromFunctions(dnsStB, dnsBtS, dnsVal)
|
|
|
|
|
|
|
|
func dnsVal(b []byte) error {
|
|
|
|
if bytes.IndexByte(b, '/') >= 0 {
|
|
|
|
return fmt.Errorf("domain name %q contains a slash", string(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
func dnsStB(s string) ([]byte, error) {
|
2019-06-09 07:24:20 +00:00
|
|
|
return []byte(s), nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dnsBtS(b []byte) (string, error) {
|
|
|
|
return string(b), nil
|
|
|
|
}
|