How to Architect a P2P Application in .NET

Architectureclientnetp2p

[Moved here at the suggestion of SO users (10k SO+)]

I'd like to develop a peer-to-peer application. While I have a lot of experience in LOB apps, I'm new to the P2P arena.

I've got a rough idea of how things should work but need some more details to fill out my understanding.

What I know (believe) I need to do:

  • A significant proportion of clients need to enable inbound connections (ala uPnP/NAT rules)
  • Nodes should share other known nodes to provide resiliency if any particular node goes down
  • Some form of synchronisation/route finding is required for sending data between arbitrary clients
  • Possibly some resource sniffing to differentiate between "dumb" clients and and more powerful "super nodes" to handle sync/sharing of node lists and maybe relay messages
  • Clients without inbound support should hold open an outbound connection through which they can receive info of nodes to connect to

In short, I'm hoping to offer (at first) a chat/messenger service which doesn't rely on a connection to any particular central server. While I imagine I'll need to supply a number of centralised "supernodes" to get things started (or after significant upgrades), these should be optional once a functional P2P network has been established.

I can see a slew of problems and don't know how to address them. Mainly how to…

  • Authenticate users to other nodes without a central authority to verify
  • Co-ordinate which nodes know about which other nodes (min-max number/by latency/???)
  • Allow a given user to determine if another user (or node) is online
  • Deal with a situation where 2 groups of nodes are physically disconnected (airgapped) and how to resync on reconnection of the groups
  • Etc etc

I know this is a pretty open-ended question, so while high-level design patterns would be appreciated, what I'm really looking for is a decent guide to how others have handled these problems (and the ones I haven't considered yet).

Best Answer

  1. Designing a protocol and building an application on it is a huge project. Take as much as possible from existing protocols.
  2. Most relevant protocols (beyond skype, which is a peer-to-peer messaging, but it's protocol is secret) are those that provide resources over peer-to-peer network which means especially the part of TOR providing the .onion domain and freenet.
  3. Most things you list in "need to do" are handled by freenet and many of them in TOR too.
  4. Identity of users has to be cryptographic. Associating real-word identities with the cryptographic keys requires some form of web-of-trust like in PGP/GPG.
  5. Important reason for using peer-to-peer messaging is privacy. Off-the-record messaging is basically mandatory (specifies how the authentication must work).
  6. Presence has to be off-the-record too, basically a special kind of message.
  7. Disconnects are no special problem beyond general fault tolerance. For each side it looks like the other nodes failing and joining again.
  8. You may want to use the protocol as signalling for some streaming protocol, probably sRTP. That protocol handles NAT traversal, so you can clone the mechanism for the p2p protocol itself.
Related Topic