Skip to content

Commit

Permalink
Add /ws, /wss, /exp-dns, /libp2p-circuit-relay, /p2p
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Gierth committed Jan 19, 2017
1 parent 02956ba commit b8d535f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
23 changes: 23 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ 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",
"/p2p/Qmnope",
"/libp2p-relay/Qmnope",
"/exp-dns",
"/exp-dns4",
"/exp-dns6",
}

for _, a := range cases {
Expand Down Expand Up @@ -77,6 +87,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 +98,14 @@ 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",
"/libp2p-relay/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",
"/exp-dns/example.net",
"/exp-dns4/example.net",
"/exp-dns6/example.net",
}

for _, a := range cases {
Expand Down Expand Up @@ -229,6 +248,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("/libp2p-relay/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z/https",
[]string{"/libp2p-relay/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", "/https"})
testString("/exp-dns/relay.libp2p.io/tcp/4001/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/libp2p-relay/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z",
[]string{"/exp-dns/relay.libp2p.io", "/tcp/4001", "/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", "/libp2p-relay/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z"})
}

func TestProtocols(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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
Expand All @@ -31,7 +33,7 @@ const (
P_SCTP = 132
P_WEBRTC_STAR = 275
P_WEBRTC_DIRECT = 275
P_RELAY = 290
P_CIRCUIT_RELAY = 290
P_UTP = 301
P_UDT = 302
P_UNIX = 400
Expand Down Expand Up @@ -62,15 +64,22 @@ 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_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_CIRCUIT_RELAY, LengthPrefixedVarSize, "libp2p-relay", CodeToVarint(P_CIRCUIT_RELAY), false, TranscoderCircuitRelay},
// experiments
Protocol{P_EXP_DNS, LengthPrefixedVarSize, "exp-dns", CodeToVarint(P_EXP_DNS), false, TranscoderExpDNS},
Protocol{P_EXP_DNS4, LengthPrefixedVarSize, "exp-dns4", CodeToVarint(P_EXP_DNS4), false, TranscoderExpDNS},
Protocol{P_EXP_DNS6, LengthPrefixedVarSize, "exp-dns6", CodeToVarint(P_EXP_DNS6), false, TranscoderExpDNS},
}

func AddProtocol(p Protocol) error {
Expand Down
52 changes: 52 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,55 @@ func unixBtS(b []byte) (string, error) {
s = s[1:] // remove starting slash
return s, nil
}

var TranscoderExpDNS = NewTranscoderFromFunctions(expDnsStB, expDnsBtS)

func expDnsStB(s string) ([]byte, error) {
size := CodeToVarint(len(s))
b := append(size, []byte(s)...)
return b, nil
}

func expDnsBtS(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
}

var TranscoderRelay = NewTranscoderFromFunctions(relayStB, relayBtS)

func relayStB(s string) ([]byte, error) {
// the address is a varint prefixed multihash string representation
m, err := mh.FromB58String(s)
if err != nil {
return nil, fmt.Errorf("failed to parse exp-relay addr: %s %s", s, err)
}
size := CodeToVarint(len(m))
b := append(size, m...)
return b, nil
}

func relayBtS(b []byte) (string, error) {
// the address is a varint-prefixed multihash string representation
size, n, err := ReadVarintCode(b)
if err != nil {
return "", err
}

b = b[n:]
if len(b) != size {
return "", errors.New("inconsistent lengths")
}
m, err := mh.Cast(b)
if err != nil {
return "", err
}
return m.B58String(), nil
}

0 comments on commit b8d535f

Please sign in to comment.