Skip to content

Instantly share code, notes, and snippets.

@freddi301
Created March 12, 2020 18:07
Show Gist options
  • Save freddi301/09ebc8ba6c39115f3f4da9d01db32576 to your computer and use it in GitHub Desktop.
Save freddi301/09ebc8ba6c39115f3f4da9d01db32576 to your computer and use it in GitHub Desktop.
Notes about implementing p2p

docker run --rm -it $(docker build -q .)

docker build -t gobi301/p2p-network-rust . && docker push gobi301/p2p-network-rust:latest

Features

  • nat traversal
  • dht
  • lan discovery
  • relay comunications

Api

  • content addressed file sharing (ipfs/torrent like, as merkle-dag)
  • p2p streaming (tcp, udp socket wrapper)
  • topic swarm (find same app peers, supported app manifest)
  • mutable topic (as dns and personal profile)

Wrappers

  • browser
  • filesystem

Apps

  • chat
  • micro blogging social
  • music / video streaming
  • file sharing

Thoughts

abstracions

mutable state event oriented ((state, event) -> event) goal oriented (given a goal and state, do action to go closer to the state) streams futures dependency oriented

all code supposes there is a fixed ram, and a cpu and ip addresses, and can't fail external storage as hard-disk or other adapted resources are treated as attached storage (can fail)

resource aware resource limiting resources are limited so they should be trated as that resource accounting resource usage should be counted resource sharing resources should be allocated evenly (size limit by bytes, time share by priority queues) ex (prioritize by ip (local ips has lower priority), then by port)

browser adapter given peer id, open localhost port that forwards to that peer (tcp, udp) dns server .overlay resolves to local port

server adapter creates a proxy to a localhost port

discovery messages must be signed direct messages must be encrypted

icmp is blocked sometimes too much unsolicited packets can trigger anti-flood block

discovery modes public ip bootstrap nodes lan broadcast (ipv4 broadcast, ipv6 multicast, ipv4 mutlicast dns as in dat protocol) dht custom

nat traversal tecniques holepunch same port (port mapping reuse) holepunch guess port holepunch two-stage (https://lists.gnu.org/archive/html/gnunet-developers/2015-08/pdfWmTHEBX5S1.pdf) unpn-igd nat-pmp pcp pwnat turn/stun/ice relay (peer, own, paid)

test for hairpin and loopback

nat types:

  • full cone (IP only mapping, any port (same))
  • address restricted (allow inbound only after contacting ip, any port (same))
  • port restricted (allow inbound only after conatacting ip on port)

nat features:

  • flood protection (too many unsolicited packets)
  • hairpin (able to route between machines under same nat)
  • port resuse from different ips (needs port guessing)
  • automatic port mapping (upnp (100% support) nat-pmp (50% support)) (does not support mutlilevel nat)

nat-type no (has public ip, allow inbound connections, same ports) full cone (onyl ip gets translated, ports remain the same, allow inbound) address restricted cone (ip gets translated, ports remain same, allow inbound after outbound ip) port restricted cone (ip gets translated, ports remain same, allow inbound after outbound ip and port) simmetric nat

nat-feature hairpin port preserving | port continous

route {me: (nat-type, nat-feature), peer: (nat-type, nat-feature)}

nat scenarios nat (?, port-preserving) -> no nat nat (?, port continous) -> no nat

core abstraction: port-mapping path (from -> to) is unidirectional actions: open mantain send close (optional) state: useable unuseable (ringing up, shutdown)

a connection is generally a pair of port mappings

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment