Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /p2p, /dns4, /dns6, /wss, /p2p-circuit #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 0 additions & 15 deletions protocols.csv

This file was deleted.

44 changes: 30 additions & 14 deletions protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}