Skip to content

Commit

Permalink
add plain dns support
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Jul 9, 2019
1 parent 129db4c commit 3362f75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
12 changes: 12 additions & 0 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down
17 changes: 9 additions & 8 deletions resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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="
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3362f75

Please sign in to comment.