diff --git a/multiaddr_test.go b/multiaddr_test.go index 9cd6b19..14add25 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -28,6 +28,12 @@ func TestConstructFails(t *testing.T) { "/sctp", "/udp/65536", "/tcp/65536", + // "/dns4/1.2.3.4.5", + // "/dns4/::1", + "/dns4", + // "/dns6/1.2.3.4", + // "/dns6/::g", + "/dns6", "/onion/9imaq4ygg2iegci7:80", "/onion/aaimaq4ygg2iegci7:80", "/onion/timaq4ygg2iegci7:0", @@ -45,6 +51,15 @@ func TestConstructFails(t *testing.T) { "/ip4/127.0.0.1/ipfs/tcp", "/unix", "/ip4/1.2.3.4/tcp/80/unix", + "/http/foo", + "/https/foo", + "/ws/foo", + "/wss/foo", + "/ipfs/Qmnope", + "/ipfs", + "/p2p/Qmnope", + "/p2p", + "/p2p-circuit/asdf", } for _, a := range cases { @@ -77,6 +92,7 @@ func TestConstructSucceeds(t *testing.T) { "/tcp/1234/http", "/tcp/1234/https", "/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", + "/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", "/ip4/127.0.0.1/udp/1234", "/ip4/127.0.0.1/udp/0", "/ip4/127.0.0.1/tcp/1234", @@ -87,6 +103,16 @@ func TestConstructSucceeds(t *testing.T) { "/unix/stdio", "/ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f", "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio", + "/p2p-circuit", + "/p2p-circuit/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", + "/ip4/1.2.3.4/tcp/80/http", + "/ip4/1.2.3.4/tcp/443/https", + "/ip4/1.2.3.4/tcp/80/ws", + "/ip4/1.2.3.4/tcp/443/wss", + "/dns4/example.net", + // "/dns4/1.2.3.4", + "/dns6/example.net", + // "/dns6/::1", } for _, a := range cases { @@ -229,6 +255,10 @@ func TestBytesSplitAndJoin(t *testing.T) { []string{"/ip4/1.2.3.4", "/tcp/1", "/ip4/2.3.4.5", "/udp/2"}) testString("/ip4/1.2.3.4/utp/ip4/2.3.4.5/udp/2/udt", []string{"/ip4/1.2.3.4", "/utp", "/ip4/2.3.4.5", "/udp/2", "/udt"}) + testString("/p2p-circuit/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z/https", + []string{"/p2p-circuit", "/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", "/https"}) + testString("/dns4/relay.libp2p.io/tcp/4001/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/p2p-circuit/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", + []string{"/dns4/relay.libp2p.io", "/tcp/4001", "/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", "/p2p-circuit", "/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z"}) } func TestProtocols(t *testing.T) { diff --git a/protocols.csv b/protocols.csv deleted file mode 100644 index 996b855..0000000 --- a/protocols.csv +++ /dev/null @@ -1,15 +0,0 @@ -code size name - -4 32 ip4 -6 16 tcp -17 16 udp -33 16 dccp -41 128 ip6 -132 16 sctp -301 0 utp -302 0 udt -400 V unix -421 V ipfs -480 0 http -443 0 https -444 96 onion diff --git a/protocols.go b/protocols.go index 1114d4e..81d99e0 100644 --- a/protocols.go +++ b/protocols.go @@ -20,20 +20,31 @@ type Protocol struct { // 1. avoid parsing the csv // 2. ensuring errors in the csv don't screw up code. // 3. changing a number has to happen in two places. +// +// MUST never contradict multiformats/multiaddr/protocols.csv const ( - P_IP4 = 4 - P_TCP = 6 - P_UDP = 17 - P_DCCP = 33 - P_IP6 = 41 - P_SCTP = 132 - P_UTP = 301 - P_UDT = 302 - P_UNIX = 400 - P_IPFS = 421 - P_HTTP = 480 - P_HTTPS = 443 - P_ONION = 444 + P_IP4 = 4 + P_TCP = 6 + P_UDP = 17 + P_DCCP = 33 + P_IP6 = 41 + P_DNS4 = 54 + P_DNS6 = 55 + + // 2-byte varint from here on because code > 127 + P_SCTP = 132 + P_WEBRTC_STAR = 275 + P_P2P_CIRCUIT = 290 + P_UTP = 301 + P_UDT = 302 + P_UNIX = 400 + P_P2P = 420 + P_IPFS = 421 + P_HTTP = 480 + P_HTTPS = 443 + P_ONION = 444 + P_WS = 477 + P_WSS = 478 ) // These are special sizes @@ -48,15 +59,20 @@ var Protocols = []Protocol{ Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP), false, TranscoderPort}, Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP), false, TranscoderPort}, Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6), false, TranscoderIP6}, - // these require varint: + Protocol{P_DNS4, LengthPrefixedVarSize, "dns4", CodeToVarint(P_DNS4), false, TranscoderDNS}, + Protocol{P_DNS6, LengthPrefixedVarSize, "dns6", CodeToVarint(P_DNS6), false, TranscoderDNS}, Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP), false, TranscoderPort}, Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION), false, TranscoderOnion}, Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP), false, nil}, Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT), false, nil}, Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP), false, nil}, Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS), false, nil}, + Protocol{P_WS, 0, "ws", CodeToVarint(P_WS), false, nil}, + Protocol{P_WSS, 0, "wss", CodeToVarint(P_WSS), false, nil}, Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS), false, TranscoderIPFS}, + Protocol{P_P2P, LengthPrefixedVarSize, "p2p", CodeToVarint(P_P2P), false, TranscoderIPFS}, Protocol{P_UNIX, LengthPrefixedVarSize, "unix", CodeToVarint(P_UNIX), true, TranscoderUnix}, + Protocol{P_P2P_CIRCUIT, 0, "p2p-circuit", CodeToVarint(P_P2P_CIRCUIT), false, nil}, } func AddProtocol(p Protocol) error { diff --git a/transcoders.go b/transcoders.go index f6739ed..f4a5e3c 100644 --- a/transcoders.go +++ b/transcoders.go @@ -179,3 +179,24 @@ func unixBtS(b []byte) (string, error) { s = s[1:] // remove starting slash return s, nil } + +var TranscoderDNS = NewTranscoderFromFunctions(dnsStB, dnsBtS) + +func dnsStB(s string) ([]byte, error) { + size := CodeToVarint(len(s)) + b := append(size, []byte(s)...) + return b, nil +} + +func dnsBtS(b []byte) (string, error) { + size, n, err := ReadVarintCode(b) + if err != nil { + return "", err + } + + b = b[n:] + if len(b) != size { + return "", errors.New("inconsistent lengths") + } + return string(b), nil +}