#exchange-protocols #addresses #p2p #gday #sockets #command-line-tool #ipv6 #ipv4

gday_contact_exchange_protocol

Protocol for peers to exchange their socket addresses via a server

7 releases (4 breaking)

Uses new Rust 2024

0.5.1 Sep 20, 2025
0.5.0 Sep 12, 2025
0.4.0 Jun 28, 2025
0.3.0 Dec 2, 2024
0.1.1 Jun 7, 2024

#4 in #exchange-protocols

Download history 3/week @ 2025-07-11 2/week @ 2025-07-18 1/week @ 2025-08-15 7/week @ 2025-08-22 8/week @ 2025-08-29 11/week @ 2025-09-05 161/week @ 2025-09-12 146/week @ 2025-09-19 41/week @ 2025-09-26 20/week @ 2025-10-03 15/week @ 2025-10-10 23/week @ 2025-10-17 9/week @ 2025-10-24

75 downloads per month
Used in 4 crates (2 directly)

MIT license

18KB
180 lines

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:

#
use gday_contact_exchange_protocol::{ClientMsg, Contact, ServerMsg, read_from, write_to};
let room_code = String::from("Room code");

// 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: room_code.clone(),
};
write_to(&request, &mut tls_ipv4)?;
let ServerMsg::RoomCreated = read_from(&mut tls_ipv4)? else {
    panic!()
};

// Both peers send 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: room_code.clone(),
    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.
#

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_gui - GUI app 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.

Dependencies

~2.7–6.5MB
~116K SLoC