diff --git a/dns.go b/dns.go index 53c82df..da4dd05 100644 --- a/dns.go +++ b/dns.go @@ -9,11 +9,19 @@ import ( // Extracted from source of truth for multicodec codes: https://github.com/multiformats/multicodec const ( + P_DNS = 0x0035 P_DNS4 = 0x0036 P_DNS6 = 0x0037 P_DNSADDR = 0x0038 ) +var DnsProtocol = ma.Protocol{ + Code: P_DNS, + Size: ma.LengthPrefixedVarSize, + Name: "dns", + VCode: ma.CodeToVarint(P_DNS), + Transcoder: DnsTranscoder, +} var Dns4Protocol = ma.Protocol{ Code: P_DNS4, Size: ma.LengthPrefixedVarSize, @@ -37,6 +45,10 @@ var DnsaddrProtocol = ma.Protocol{ } func init() { + err := ma.AddProtocol(DnsProtocol) + if err != nil { + panic(fmt.Errorf("error registering dns protocol: %s", err)) + } err := ma.AddProtocol(Dns4Protocol) if err != nil { panic(fmt.Errorf("error registering dns4 protocol: %s", err)) diff --git a/resolve.go b/resolve.go index a85490c..6134781 100644 --- a/resolve.go +++ b/resolve.go @@ -8,7 +8,7 @@ import ( ma "github.com/multiformats/go-multiaddr" ) -var ResolvableProtocols = []ma.Protocol{DnsaddrProtocol, Dns4Protocol, Dns6Protocol} +var ResolvableProtocols = []ma.Protocol{DnsaddrProtocol, Dns4Protocol, Dns6Protocol, DnsProtocol} var DefaultResolver = &Resolver{Backend: net.DefaultResolver} const dnsaddrTXTPrefix = "dnsaddr=" @@ -97,8 +97,9 @@ func (r *Resolver) Resolve(ctx context.Context, maddr ma.Multiaddr) ([]ma.Multia var resolved []ma.Multiaddr switch proto.Code { - case Dns4Protocol.Code, Dns6Protocol.Code: - v4 := proto.Code == Dns4Protocol.Code + case Dns4Protocol.Code, Dns6Protocol.Code, DnsProtocol.Code: + v4only := proto.Code == Dns4Protocol.Code + v6only := proto.Code == Dns6Protocol.Code // XXX: Unfortunately, go does a pretty terrible job of // differentiating between IPv6 and IPv4. A v4-in-v6 @@ -115,16 +116,16 @@ func (r *Resolver) Resolve(ctx context.Context, maddr ma.Multiaddr) ([]ma.Multia err error ) ip4 := r.IP.To4() - if v4 { - if ip4 == nil { + if ip4 == nil { + if v4only { continue } - rmaddr, err = ma.NewMultiaddr("/ip4/" + ip4.String()) + rmaddr, err = ma.NewMultiaddr("/ip6/" + r.IP.String()) } else { - if ip4 != nil { + if v6only { continue } - rmaddr, err = ma.NewMultiaddr("/ip6/" + r.IP.String()) + rmaddr, err = ma.NewMultiaddr("/ip4/" + ip4.String()) } if err != nil { return nil, err