Links

Routing and Transports

Ockam Routing and Transports enable higher level protocols that provide end-to-end guarantees to messages traveling across many network connection hops and protocols boundaries.
Ockam Routing is a simple and lightweight message-based protocol that makes it possible to bidirectionally exchange messages over a large variety of communication topologies.
Ockam Transports adapt Ockam Routing to various transport protocols like TCP, UDP, WebSockets, Bluetooth etc.
By layering Ockam Secure Channels and other higher level protocols over Ockam Routing, it is possible to build systems that provide end-to-end guarantees over arbitrary transport topologies that span many networks, connections, gateways, queues, and clouds.

Routing

Let's dive into how the routing protocol works. So far, in the section on Nodes and Workers, we've come across this simple message exchange:
Ockam Routing Protocol messages carry with them two metadata fields: an onward_route and a return_route. A route is an ordered list of addresses describing the path a message should travel. This information is carried with the message in compact binary form.
Pay close attention to the Sender, Hop, and Replier rules in the sequence diagrams below. Note how onward_route and return_route are handled as the message travels.
The above was one message hop. We may extend this to two hops:
This very simple protocol extends to any number of hops:

Routing over two hops

So far, we've created an "echoer" worker in our node, sent it a message, and received a reply. This worker was a simple one hop away from our "app" worker.
To achieve this, messages carry with them two metadata fields: onward_route and return_route, where a route is a list of addresses.
To get a sense of how that works, let's route a message over two hops.

Hop worker

For demonstration, we'll create a simple worker, called Hop, that takes every incoming message and forwards it to the next address in the onward_route of that message.
Just before forwarding the message, Hop's handle message function will:
  1. 1.
    Print the message
  2. 2.
    Remove its own address (first address) from the onward_route, by calling step()
  3. 3.
    Insert its own address as the first address in the return_route by calling prepend()
1
// src/hop.rs
2
use ockam::{Any, Context, LocalMessage, Result, Routed, Worker};
3
4
pub struct Hop;
5
6
#[ockam::worker]
7
impl Worker for Hop {
8
type Context = Context;
9
type Message = Any;
10
11
/// This handle function takes any incoming message and forwards
12
/// it to the next hop in it's onward route
13
async fn handle_message(&mut self, ctx: &mut Context, msg: Routed<Any>) -> Result<()> {
14
println!("Address: {}, Received: {}", ctx.address(), msg);
15
16
// Some type conversion
17
let mut transport_message = msg.into_local_message().into_transport_message();
18
19
// Remove my address from the onward_route
20
transport_message.onward_route.step()?;
21
22
// Insert my address at the beginning return_route
23
transport_message.return_route.modify().prepend(ctx.address());
24
25
// Wipe all local info (e.g. transport types)
26
let message = LocalMessage::new(transport_message, vec![]);
27
28
// Send the message on its onward_route
29
ctx.forward(message).await
30
}
31
}
32

App worker

Next, let's create our main "app" worker.
In the code below we start an Echoer worker at address "echoer" and a Hop worker at address "h1". Then, we send a message along the h1 => echoer route by passing route!["h1", "echoer"] to send(..).
1
// examples/03-routing.rs
2
// This node routes a message.
3
4
use hello_ockam::{Echoer, Hop};
5
use ockam::{node, route, Context, Result};
6
7
#[ockam::node]
8
async fn main(ctx: Context) -> Result<()> {
9
// Create a node with default implementations
10
let mut node = node(ctx).await?;
11
12
// Start a worker, of type Echoer, at address "echoer"
13
node.start_worker("echoer", Echoer).await?;
14
15
// Start a worker, of type Hop, at address "h1"
16
node.start_worker("h1", Hop).await?;
17
18
// Send a message to the worker at address "echoer",
19
// via the worker at address "h1"
20
node.send(route!["h1", "echoer"], "Hello Ockam!".to_string()).await?;
21
22
// Wait to receive a reply and print it.
23
let reply = node.receive::<String>().await?;
24
println!("App Received: {}", reply); // should print "Hello Ockam!"
25
26
// Stop all workers, stop the node, cleanup and return.
27
node.stop().await
28
}
29
To run this new node program:
cargo run --example 03-routing

Routing over many hops

Similarly, we can also route the message via many hop workers:
1
// examples/03-routing-many-hops.rs
2
// This node routes a message through many hops.
3
4
use hello_ockam::{Echoer, Hop};
5
use ockam::{node, route, Context, Result};
6
7
#[ockam::node]
8
async fn main(ctx: Context) -> Result<()> {
9
// Create a node with default implementations
10
let mut node = node(ctx).await?;
11
12
// Start an Echoer worker at address "echoer"
13
node.start_worker("echoer", Echoer).await?;
14
15
// Start 3 hop workers at addresses "h1", "h2" and "h3".
16
node.start_worker("h1", Hop).await?;
17
node.start_worker("h2", Hop).await?;
18
node.start_worker("h3", Hop).await?;
19
20
// Send a message to the echoer worker via the "h1", "h2", and "h3" workers
21
let r = route!["h1", "h2", "h3", "echoer"];
22
node.send(r, "Hello Ockam!".to_string()).await?;
23
24
// Wait to receive a reply and print it.
25
let reply = node.receive::<String>().await?;
26
println!("App Received: {}", reply); // should print "Hello Ockam!"
27
28
// Stop all workers, stop the node, cleanup and return.
29
node.stop().await
30
}
31
To run this new node program:
cargo run --example 03-routing-many-hops

Transport

An Ockam Transport is a plugin for Ockam Routing. It moves Ockam Routing messages using a specific transport protocol like TCP, UDP, WebSockets, Bluetooth etc.
In previous examples, we routed messages locally within one node. Routing messages over transport layer connections looks very similar.
Let's try the TcpTransport, we'll need to create two nodes: a responder and an initiator.

Responder node

1
// examples/04-routing-over-transport-responder.rs
2
// This node starts a tcp listener and an echoer worker.
3
// It then runs forever waiting for messages.
4
5
use hello_ockam::Echoer;
6
use ockam::{node, Context, Result, TcpListenerOptions, TcpTransportExtension};
7
8
#[ockam::node]
9
async fn main(ctx: Context) -> Result<()> {
10
// Create a node with default implementations
11
let node = node(ctx).await?;
12
13
// Initialize the TCP Transport
14
let tcp = node.create_tcp_transport().await?;
15
16
// Create an echoer worker
17
node.start_worker("echoer", Echoer).await?;
18
19
// Create a TCP listener and wait for incoming connections.
20
let listener = tcp.listen("127.0.0.1:4000", TcpListenerOptions::new()).await?;
21
22
// Allow access to the Echoer via TCP connections from the TCP listener
23
node.flow_controls().add_consumer("echoer", listener.flow_control_id());
24
25
// Don't call node.stop() here so this node runs forever.
26
Ok(())
27
}
28

Initiator node

1
// examples/04-routing-over-transport-initiator.rs
2
// This node routes a message, to a worker on a different node, over the tcp transport.
3
4
use ockam::{node, route, Context, Result, TcpConnectionOptions, TcpTransportExtension};
5
6
#[ockam::node]
7
async fn main(ctx: Context) -> Result<()> {
8
// Create a node with default implementations
9
let mut node = node(ctx).await?;
10
11
// Initialize the TCP Transport.
12
let tcp = node.create_tcp_transport().await?;
13
14
// Create a TCP connection to a different node.
15
let connection_to_responder = tcp.connect("localhost:4000", TcpConnectionOptions::new()).await?;
16
17
// Send a message to the "echoer" worker on a different node, over a tcp transport.
18
// Wait to receive a reply and print it.
19
let r = route![connection_to_responder, "echoer"];
20
let reply = node.send_and_receive::<String>(r, "Hello Ockam!".to_string()).await?;
21
22
println!("App Received: {}", reply); // should print "Hello Ockam!"
23
24
// Stop all workers, stop the node, cleanup and return.
25
node.stop().await
26
}
27

Run

Run the responder in a separate terminal tab and keep it running:
cargo run --example 04-routing-over-transport-responder
Run the initiator:
cargo run --example 04-routing-over-transport-initiator

Bridge

A common real world topology is a transport bridge.
Node n1 wishes to access a service on node n3, but it can't directly connect to n3. This can happen for many reasons, maybe because n3 is in a separate IP subnet, or it could be that the communication from n1 to n2 uses UDP while from n2 to n3 uses TCP or other similar constraints. The topology makes n2 a bridge or gateway between these two separate networks.
We can setup this topology with Ockam Routing as follows:

Responder node

1
// examples/04-routing-over-transport-two-hops-responder.rs
2
// This node starts a tcp listener and an echoer worker.
3
// It then runs forever waiting for messages.
4
5
use hello_ockam::Echoer;
6
use ockam::{node, Context, Result, TcpListenerOptions, TcpTransportExtension};
7
8
#[ockam::node]
9
async fn main(ctx: Context) -> Result<()> {
10
// Create a node with default implementations
11
let node = node(ctx).await?;
12
13
// Initialize the TCP Transport
14
let tcp = node.create_tcp_transport().await?;
15
16
// Create an echoer worker
17
node.start_worker("echoer", Echoer).await?;
18
19
// Create a TCP listener and wait for incoming connections.
20
let listener = tcp.listen("127.0.0.1:4000", TcpListenerOptions::new()).await?;
21
22
// Allow access to the Echoer via TCP connections from the TCP listener
23
node.flow_controls().add_consumer("echoer", listener.flow_control_id());
24
25
// Don't call node.stop() here so this node runs forever.
26
Ok(())
27
}
28

Middle node

Relay worker
We'll create a worker, called Relay, that takes every incoming message and forwards it to the predefined address.
1
// src/relay.rs
2
use ockam::{Address, Any, Context, LocalMessage, Result, Routed, Worker};
3
4
pub struct Relay(pub Address);
5
6
#[ockam::worker]
7
impl Worker for Relay {
8
type Context = Context;
9
type Message = Any;
10
11
/// This handle function takes any incoming message and forwards
12
/// it to the next hop in it's onward route
13
async fn handle_message(&mut self, ctx: &mut Context, msg: Routed<Any>) -> Result<()> {
14
println!("Address: {}, Received: {}", ctx.address(), msg);
15
16
// Some type conversion
17
let mut transport_message = msg.into_local_message().into_transport_message();
18
19
transport_message
20
.onward_route
21
.modify()
22
.pop_front() // Remove my address from the onward_route
23
.prepend(self.0.clone()); // Prepend predefined address to the onward_route
24
25
let prev_hop = transport_message.return_route.next()?.clone();
26
27
// Wipe all local info (e.g. transport types)
28
let message = LocalMessage::new(transport_message, vec![]);
29
30
if let Some(info) = ctx.flow_controls().find_flow_control_with_producer_address(&self.0) {
31
ctx.flow_controls()
32
.add_consumer(prev_hop.clone(), info.flow_control_id());
33
}
34
35
if let Some(info) = ctx.flow_controls().find_flow_control_with_producer_address(&prev_hop) {
36
ctx.flow_controls().add_consumer(self.0.clone(), info.flow_control_id());
37
}
38
39
// Send the message on its onward_route
40
ctx.forward(message).await
41
}
42
}
43
1
// examples/04-routing-over-transport-two-hops-middle.rs
2
// This node creates a tcp connection to a node at 127.0.0.1:4000
3
// Starts a relay worker to forward messages to 127.0.0.1:4000
4
// Starts a tcp listener at 127.0.0.1:3000
5
// It then runs forever waiting to route messages.
6
7
use hello_ockam::Relay;
8
use ockam::{node, Context, Result, TcpConnectionOptions, TcpListenerOptions, TcpTransportExtension};
9
10
#[ockam::node]
11
async fn main(ctx: Context) -> Result<()> {
12
// Create a node with default implementations
13
let node = node(ctx).await?;
14
15
// Initialize the TCP Transport
16
let tcp = node.create_tcp_transport().await?;
17
18
// Create a TCP connection to the responder node.
19
let connection_to_responder = tcp.connect("127.0.0.1:4000", TcpConnectionOptions::new()).await?;
20
21
// Create a Relay worker
22
node.start_worker("forward_to_responder", Relay(connection_to_responder.into()))
23
.await?;
24
25
// Create a TCP listener and wait for incoming connections.
26
let listener = tcp.listen("127.0.0.1:3000", TcpListenerOptions::new()).await?;
27
28
// Allow access to the Relay via TCP connections from the TCP listener
29
node.flow_controls()
30
.add_consumer("forward_to_responder", listener.flow_control_id());
31
32
// Don't call node.stop() here so this node runs forever.
33
Ok(())
34
}
35

Initiator node

1
// examples/04-routing-over-transport-two-hops-initiator.rs
2
// This node routes a message, to a worker on a different node, over two tcp transport hops.
3
4
use ockam::{node, route, Context, Result, TcpConnectionOptions, TcpTransportExtension};
5
6
#[ockam::node]
7
async fn main(ctx: Context) -> Result<()> {
8
// Create a node with default implementations
9
let mut node = node(ctx).await?;
10
11
// Initialize the TCP Transport
12
let tcp = node.create_tcp_transport().await?;
13
14
// Create a TCP connection to the middle node.
15
let connection_to_middle_node = tcp.connect("localhost:3000", TcpConnectionOptions::new()).await?;
16
17
// Send a message to the "echoer" worker, on a different node, over two tcp hops.
18
// Wait to receive a reply and print it.
19
let r = route![connection_to_middle_node, "forward_to_responder", "echoer"];
20
let reply = node.send_and_receive::<String>(r, "Hello Ockam!".to_string()).await?;
21
println!("App Received: {}", reply); // should print "Hello Ockam!"
22
23
// Stop all workers, stop the node, cleanup and return.
24
node.stop().await
25
}
26

Run

Run the responder in a separate terminal tab and keep it running:
cargo run --example 04-routing-over-transport-two-hops-responder
Run the middle node in a separate terminal tab and keep it running:
cargo run --example 04-routing-over-transport-two-hops-middle
Run the initiator:
cargo run --example 04-routing-over-transport-two-hops-initiator

Relay

It is common, however, to encounter communication topologies where the machine that provides a service is unwilling or is not allowed to open a listening port or expose a bridge node to other networks. This is a common security best practice in enterprise environments, home networks, OT networks, and VPCs across clouds. Application developers may not have control over these choices from the infrastructure / operations layer. This is where relays are useful.
Relays make it possible to establish end-to-end protocols with services operating in a remote private network, without requiring a remote service to expose listening ports to an outside hostile network like the Internet.

Serialization

Ockam Routing messages when transported over the wire have the following structure. TransportMessage is serialized using BARE Encoding. We intend to transition to CBOR in the near future since we already use CBOR for other protocols built on top of Ockam Routing.
1
pub struct TransportMessage {
2
pub version: u8,
3
pub onward_route: Route,
4
pub return_route: Route,
5
pub payload: Vec<u8>,
6
}
7
8
pub struct Route {
9
addresses: VecDeque<Address>
10
}
11
12
pub struct Address {
13
transport_type: TransportType,
14
transport_protocol_address: Vec<u8>,
15
}
16
17
pub struct TransportType(u8);
Each transport type has a conventional value. TCP has transport type 1. UDP has transport type 2 etc. Node local messages have transport type 0.
As message moves within a node it gathers additional metadata in structure like LocalMessage and RelayMessage that are used for a node's internal operation.

Access Control

Each Worker has one or more addresses that it uses to send and receive messages. We assign each Address an Incoming Access Control and an Outgoing Access Control.
1
#[async_trait]
2
pub trait IncomingAccessControl: Debug + Send + Sync + 'static {
3
/// Return true if the message is allowed to pass, and false if not.
4
async fn is_authorized(&self, relay_msg: &RelayMessage) -> Result<bool>;
5
}
6
7
#[async_trait]
8
pub trait OutgoingAccessControl: Debug + Send + Sync + 'static {
9
/// Return true if the message is allowed to pass, and false if not.
10
async fn is_authorized(&self, relay_msg: &RelayMessage) -> Result<bool>;
11
}
Concrete instances of these traits inspect a message's onward_route, return_route, metadata etc. along with other node local state to decide if a message should be allowed to be sent or received. Incoming Access Control filters which messages reach an address while Outgoing Access Control decides which messages can be sent.

Flow Control

In our threat model, we assume that Workers within a Node are not malicious against each other. If programmed correctly they intend no harm.
However, there are certain types of Workers that forward messages that were created on other nodes. We don't implicitly trust other Ockam Nodes so messages from them can be dangerous. Such workers that can receive messages from another node are implemented with an Outgoing Access Control that denies all messages by default.
For example, a TCP Transport Listener spawns TCP Receivers for every new TCP connection. These receivers are implemented with an Outgoing Access Control that denies all messages, by default, from entering the node that is running the receiver. We can then explicitly allow messages to flow to a specific addresses.
In the middle node example above, we do this by explicitly allowing flow of messages from the TCP Receivers (spawned by TCP Transport Listener) to the forward_to_responder worker.
1
// Create a TCP listener and wait for incoming connections.
2
let listener = tcp.listen("127.0.0.1:3000", TcpListenerOptions::new()).await?;
3
4
// Allow access to the Forwarder via TCP connections from the TCP listener
5
node.flow_controls()
6
.add_consumer("forward_to_responder", listener.flow_control_id());