Skip to content

Commit c964d9a

Browse files
authored
Merge pull request #140 from multiformats/marco/multiaddrs
Interpreting multiaddrs
2 parents c950ec4 + 5787c6d commit c964d9a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ Multiaddr solves these problems by modelling network addresses as arbitrary enca
4444

4545
Multiaddr was originally [thought up by @jbenet](https://github.com/jbenet/random-ideas/issues/11).
4646

47+
## Interpreting multiaddrs
48+
49+
Multiaddrs are parsed from left to right, but they should be interpreted right
50+
to left. Each component of a multiaddr wraps all the left components in its
51+
context. For example, the multiaddr `/dns4/example.com/tcp/1234/tls/ws/tls`
52+
(ignore the double encryption for now) is interpreted by taking the first `tls`
53+
component from the right and interpreting it as the libp2p security protocol to
54+
use for the connection, then passing the rest of the multiaddr to the websocket
55+
transport to create the websocket connection. The websocket transport sees
56+
`/dns4/example.com/tcp/1234/tls/ws/` and interprets the `tls` in this context to
57+
mean that this is going to be a secure websocket connection. The websocket
58+
transport also gets the host to dial along with the tcp port from the rest of
59+
the multiaddr.
60+
61+
Components to the right can also provide parameters to components to the left,
62+
since they are in charge of the rest of the multiaddr's interpretation. For
63+
example, in `/ip4/1.2.3.4/tcp/1234/tls/p2p/QmFoo` the `p2p` component has the
64+
value of the peer id and it passes it to the next component, in this case the
65+
`tls` security protocol, as the expected peer id for this connection. Another
66+
example is `/ip4/.../p2p/QmR/p2p-circuit/p2p/QmA`, here `p2p/QmA` is passed to
67+
`p2p-circuit` and then the `p2p-circuit` component knows it needs to use the
68+
rest of the multiaddr as the information to connect to the relay node.
69+
70+
This enables nesting and arbitrary parameters. A component can parse
71+
arbitrary data with some encoding and pass it as a parameter to the next
72+
component of the multiaddr. For example, we could reference a specific HTTP path
73+
by composing `path` and `urlencode` components along with an `http` component.
74+
This would look like
75+
`/dns4/example.com/http/path/percentencode/somepath%2ftosomething`. The
76+
`percentencode` parses the data and passes it as a parameter to `path`, which
77+
passes it as a named parameter (`path=somepath/tosomething`). A user may not
78+
like percentencode for their use case and may prefer to use `lenprefixencode` to
79+
have the multiaddr instead look like
80+
`/dns4/example.com/http/path/lenprefixencode/20_somepath/tosomething`. This
81+
would work the same and require no changes to the `path` or `http` component.
82+
It's important to note that the binary representation of the data in
83+
`percentencode` and `lenprefixencode` would be the same. The only difference is
84+
how it appears in the human-readable representation.
4785

4886
## Use cases
4987

0 commit comments

Comments
 (0)