jbenet / random-ideas

random ideas
juan.benet.ai
323 stars 12 forks source link

Binary packed network addresses #11

Open jbenet opened 10 years ago

jbenet commented 10 years ago

Consider url network addresses like:

tcp4://127.0.0.1:1234
udp4://10.20.30.40:5060
ws://1.2.3.4:5678
tcp6://[1fff:0:a88:85a3::ac1f]:8001

How best to binary pack them? Is there a standard for this? These should be storable in 7 bytes for IPv4 (not 20), and 19 bytes for ipv6 (not 35).

If not, perhaps:

<1 byte proto><4 or 16 byte ip><2 byte port>

Or, if we want encapsulation generality:

(<1 byte proto><n byte addr>)+
<1 byte ipv4 code><4 byte ipv4 addr><1 byte udp code><2 byte udp port>
<1 byte ipv6 code><16 byte ipv6 addr><1 byte tcp code><2 byte tcp port>

Which is nice, because it stacks well.

Back-translating with a better hierarchical approach (let's not get too crazy now...) gives us:

(/<addr str code>/<addr str rep>)+
/ip4/<ipv4 str addr>/udp/<udp int port>
/ip6/<ipv6 str addr>/tcp/<tcp int port>

Rather than:

udp4://<ipv4 addr>:<udp port>
tcp6://[<ipv6 addr>]:<tcp port>
jbenet commented 10 years ago

Bittorrent just concats ip + port: https://github.com/bencevans/node-compact2string It doesn't handle protocol at all, try both tcp + udp.

jbenet commented 10 years ago

Fleshed out the node api: https://github.com/jbenet/node-multiaddr

jbenet commented 10 years ago

Implemented + published on npm.