Skip to content

n0-computer/iroh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

iroh

less net work for networks

Documentation Crates.io downloads Chat Youtube License: MIT License: Apache 2.0 CI


What is iroh?

Iroh gives you an API for dialing by public key. You say “connect to that phone”, iroh will find & maintain the fastest connection for you, regardless of where it is.

Hole-punching

The fastest route is a direct connection, so if necessary, iroh tries to hole-punch. Should this fail, it can fall back to an open ecosystem of public relay servers. To ensure these connections are as fast as possible, we continuously measure iroh.

Built on QUIC

Iroh uses Quinn to establish QUIC connections between nodes. This way you get authenticated encryption, concurrent streams with stream prioirities, a datagram transport and avoid head-of-line-blocking out of the box.

Compose Protocols

Use pre-existing protocols built on iroh instead of writing your own:

  • iroh-blobs for BLAKE3-based content-addressed blob transfer scaling from kilobytes to terrabytes
  • iroh-gossip for establishing publish-subscribe overlay networks that scale, requiring only resources that your average phone can handle
  • iroh-docs for an eventually-consistent key-value store of iroh-blobs blobs
  • iroh-willow for an (in-construction) implementation of the willow protocol

Getting Started

Rust Library

It's easiest to use iroh from rust. Install it using cargo add iroh, then on the connecting side:

const ALPN: &[u8] = b"iroh-example/echo/0";

let endpoint = Endpoint::builder().discovery_n0().bind().await?;

// Open a connection to the accepting node
let conn = endpoint.connect(addr, ALPN).await?;

// Open a bidirectional QUIC stream
let (mut send, mut recv) = conn.open_bi().await?;

// Send some data to be echoed
send.write_all(b"Hello, world!").await?;
send.finish()?;

// Receive the echo
let response = recv.read_to_end(1000).await?;
assert_eq!(&response, b"Hello, world!");

// Close the endpoint and all its connections
endpoint.close(0u32.into(), b"bye!").await?;

And on the accepting side:

let endpoint = Endpoint::builder().discovery_n0().bind().await?;

let router = Router::builder(endpoint)
    .accept(ALPN.to_vec(), Arc::new(Echo))
    .spawn()
    .await?;

// The protocol definition:
#[derive(Debug, Clone)]
struct Echo;

impl ProtocolHandler for Echo {
    fn accept(self: Arc<Self>, connecting: Connecting) -> BoxedFuture<Result<()>> {
        Box::pin(async move {
            let connection = connecting.await?;
            let (mut send, mut recv) = connection.accept_bi().await?;
            
            // Echo any bytes received back directly.
            let bytes_sent = tokio::io::copy(&mut recv, &mut send).await?;

            send.finish()?;
            connection.closed().await;

            Ok(())
        })
    }
}

The full example code with more comments can be found at echo.rs.

Or use one of the pre-existing protocols, e.g. iroh-blobs or iroh-gossip.

Other Languages

If you want to use iroh from other languages, make sure to check out iroh-ffi, the repository for FFI bindings.

Links

Repository Structure

This repository contains a workspace of crates:

  • iroh: Re-exports of stables APIs like iroh-router and iroh-net.
  • iroh-router: APIs to compose multiple protocols in a node.
  • iroh-net: The core networking code for hole-punching & connections to relays.
  • iroh-relay: The relay server implementation. This is the code we run in production (and you can, too!).
  • iroh-base: Common types like Hash, key types or RelayUrl.
  • iroh-metrics: Helper library for adding metrics support to crates.
  • iroh-test: Test utilities.
  • iroh-dns-server: DNS server implementation powering the n0_discovery for NodeIds, running at dns.iroh.link.
  • iroh-net-report: Analyzes your host's networking ability & NAT.
  • net-tools/*: Networking utility crates

The main entry point for anyone interested in the inner workings should be iroh-net.

License

Copyright 2024 N0, INC.

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.