Coder Social home page Coder Social logo

p2psc's Introduction

p2psc: peer-to-peer socket creation

p2psc is a small C++ library which enables the creation of network sockets between two peers anywhere on the internet, even behind firewalls and NATs.

Design overview

p2psc provides a simple interface to open a network socket between two peers, using a well-known technique called hole punching.

Both peers advertise their public key to an intermediary server, known as a Mediator. Each peer also informs the Mediator of the public key of the peer they wish to create a socket with. When both peers have registered with the Mediator and expressed interest to connect to each other, the Mediator will negotiate a handshake between them, which results in a TCP socket being created between the peers.

Interface

A socket can be created using a single static method call. All three parties involved in the handshake must be defined; the connecting peer provides its own Keypair, a reference to the Peer it wishes to connect to, and a reference to the Mediator that will mediate the connection:

p2psc::connect(const key::Keypair &, const Peer &, const Mediator &, const Callback &);

A Callback is also provided, which will be called either with an error, or with a socket that has been successfully connected to the specified Peer. It is defined as:

using Callback = std::function<void(p2psc::Error, std::shared_ptr<p2psc::Socket>)>;

Example

Here's a basic example, which assumes we know the public key of the Peer we want to connect to. In practice, sharing of the public key will likely happen in the application layer above p2psc.

#include <p2psc/connection.h>

int main() {
  auto my_keypair = p2psc::key::Keypair::from_pem("my_keypair.pem");
  auto their_key = p2psc::key::PublicKey::from_pem("their_public_key.pem");
  auto peer = p2psc::Peer(their_key);
  auto mediator = p2psc::Mediator("127.0.0.1", 1337);

  p2psc::connect(keypair, peer, mediator,
                 [](p2psc::Error error, std::shared_ptr<p2psc::Socket> socket) {
                   if (!error) {
                     socket->send("Hallå!");
                   }
                 });
}

If everything goes well, we'll be able to send a message directly to the other peer with the socket that has been created for us by p2psc!

Mediator specification

p2psc provides a client-side library used to create a p2p socket. It does not provide a Mediator server to mediate the socket creation. The Mediator specification can be used to create a Mediator server.

Protocol specification

Detailed specification of the protocol can be found here.

p2psc's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.