From bbcf5cbbc537d862e526bc8302e8f8f5cfe5b0a2 Mon Sep 17 00:00:00 2001 From: godcong Date: Wed, 18 Sep 2019 14:19:59 +0800 Subject: [PATCH] move dns protocol definitions (https://github.com/multiformats/go-multiaddr-dns/blob/3974bf3f84c52825588fdcc0fd0e0aa7953ab5ff/dns.go#L11) --- protocols.go | 27 +++++++++++++++++++++++++++ transcoders.go | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/protocols.go b/protocols.go index f3cf252..3eca0be 100644 --- a/protocols.go +++ b/protocols.go @@ -5,6 +5,9 @@ package multiaddr const ( P_IP4 = 0x0004 P_TCP = 0x0006 + P_DNS4 = 0x0036 + P_DNS6 = 0x0037 + P_DNSADDR = 0x0038 P_UDP = 0x0111 P_DCCP = 0x0021 P_IP6 = 0x0029 @@ -44,6 +47,27 @@ var ( Path: false, Transcoder: TranscoderPort, } + protoDNS4 = Protocol{ + Code: P_DNS4, + Size: LengthPrefixedVarSize, + Name: "dns4", + VCode: CodeToVarint(P_DNS4), + Transcoder: TranscoderDns, + } + protoDNS6 = Protocol{ + Code: P_DNS6, + Size: LengthPrefixedVarSize, + Name: "dns6", + VCode: CodeToVarint(P_DNS6), + Transcoder: TranscoderDns, + } + protoDNSADDR = Protocol{ + Code: P_DNSADDR, + Size: LengthPrefixedVarSize, + Name: "dnsaddr", + VCode: CodeToVarint(P_DNSADDR), + Transcoder: TranscoderDns, + } protoUDP = Protocol{ Name: "udp", Code: P_UDP, @@ -175,6 +199,9 @@ func init() { for _, p := range []Protocol{ protoIP4, protoTCP, + protoDNS4, + protoDNS6, + protoDNSADDR, protoUDP, protoDCCP, protoIP6, diff --git a/transcoders.go b/transcoders.go index 77908b7..e1f5b27 100644 --- a/transcoders.go +++ b/transcoders.go @@ -317,3 +317,20 @@ func unixStB(s string) ([]byte, error) { func unixBtS(b []byte) (string, error) { return string(b), nil } + +var TranscoderDns = 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 +} + +func dnsStB(s string) ([]byte, error) { + return []byte(s), nil +} + +func dnsBtS(b []byte) (string, error) { + return string(b), nil +}