#tcp-connection #peer #exchange #protocols #sockets #contact #file-transfer

gday_contact_exchange_protocol

Protocol for peers to exchange their socket addresses via a server

4 releases (2 breaking)

new 0.3.0 Dec 2, 2024
0.2.1 Jul 12, 2024
0.2.0 Jul 8, 2024
0.1.1 Jun 7, 2024

#1802 in Network programming

Download history 2/week @ 2024-08-24 3/week @ 2024-08-31 24/week @ 2024-09-14 23/week @ 2024-09-21 12/week @ 2024-09-28 5/week @ 2024-10-12 1/week @ 2024-10-19 5/week @ 2024-11-02 2/week @ 2024-11-16 1/week @ 2024-11-23 153/week @ 2024-11-30

158 downloads per month
Used in 3 crates (2 directly)

MIT license

18KB
170 lines

gday_contact_exchange_protocol

Crates.io Version docs.rs

Protocol for peers to exchange their socket addresses via a server.

See the documentation.

Used by:

  • gday - Command line tool for sending files.
  • gday_server - Server that lets two peers share their socket addresses.
  • gday_hole_punch - Library for establishing peer-to-peer TCP connection.

lib.rs:

Protocol for peers to exchange their socket addresses via a server.

On it's own, this library doesn't do anything other than define a shared protocol. In most cases, you should use one of the following crates:

  • gday: A command line tool for sending files to peers.
  • gday_hole_punch: A library for establishing a peer-to-peer TCP connection.
  • gday_server: A server binary that facilitates this protocol.

Example

First, both peers connect with TLS on both IPv4 and IPv6 (if possible) to a gday server on DEFAULT_PORT. Then they exchange contacts like so:

#
let room_code = *b"32-bytes. May be a password hash";

// One client tells the server to create a room.
// The server responds with ServerMsg::RoomCreated or
// an error message.
let request = ClientMsg::CreateRoom { room_code };
write_to(request, &mut tls_ipv4)?;
let ServerMsg::RoomCreated = read_from(&mut tls_ipv4)? else { panic!() };

// Both peers sends ClientMsg::RecordPublicAddr
// from their IPv4 and/or IPv6 endpoints.
// The server records the client's public addresses from these connections.
// The server responds with ServerMsg::ReceivedAddr or an error message.
let request = ClientMsg::RecordPublicAddr { room_code, is_creator: true };
write_to(request, &mut tls_ipv4)?;
let ServerMsg::ReceivedAddr = read_from(&mut tls_ipv4)? else { panic!() };
write_to(request, &mut tls_ipv6)?;
let ServerMsg::ReceivedAddr = read_from(&mut tls_ipv6)? else { panic!() };

// Both peers share their local address with the server.
// The server immediately responds with ServerMsg::ClientContact,
// containing the client's FullContact.
let local_contact = Contact {
    v4: Some("1.8.3.1:2304".parse()?),
    v6: Some("[ab:41::b:43]:92".parse()?),
};
let request = ClientMsg::ReadyToShare { local_contact, room_code, is_creator: true };
write_to(request, &mut tls_ipv4)?;
let ServerMsg::ClientContact(my_contact) = read_from(&mut tls_ipv4)? else { panic!() };

// Once both clients have sent ClientMsg::ReadyToShare,
// the server sends both clients a ServerMsg::PeerContact
// containing the FullContact of the peer.
let ServerMsg::PeerContact(peer_contact) = read_from(&mut tls_ipv4)? else { panic!() };

// The server then closes the room, and the peers disconnect.

// The peers then connect directly to each other using a library
// such as gday_hole_punch.
#

Dependencies

~2.8–9MB
~83K SLoC