---
title: llarp::quic::TunnelManager
summary: Manager class for incoming and outgoing QUIC tunnels. 

---

# llarp::quic::TunnelManager



Manager class for incoming and outgoing QUIC tunnels. 


`#include <tunnel.hpp>`

## Public Types

|                | Name           |
| -------------- | -------------- |
| using std::function< std::optional< [SockAddr](Classes/structllarp_1_1SockAddr.md) >(std::string_view lokinet_addr, uint16_t port)> | **[ListenHandler](Classes/classllarp_1_1quic_1_1TunnelManager.md#using-listenhandler)**  |
| using std::function< void(bool success)> | **[OpenCallback](Classes/classllarp_1_1quic_1_1TunnelManager.md#using-opencallback)** <br>Called when open succeeds or times out.  |

## Public Functions

|                | Name           |
| -------------- | -------------- |
| | **[TunnelManager](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-tunnelmanager)**([EndpointBase](Classes/classllarp_1_1EndpointBase.md) & endpoint) |
| int | **[listen](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-listen)**(ListenHandler handler)<br>Adds an incoming listener callback.  |
| int | **[listen](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-listen)**([SockAddr](Classes/structllarp_1_1SockAddr.md) port)<br>Simple wrapper around `listen(...)` that adds a handler that accepts all incoming connections trying to tunnel to port `port` and maps them to `localhost:port`.  |
| void | **[forget](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-forget)**(int id)<br>Removes an incoming connection handler; takes the ID returned by `[listen()](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-listen)`.  |
| std::pair< [SockAddr](Classes/structllarp_1_1SockAddr.md), uint16_t > | **[open](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-open)**(std::string_view remote_addr, uint16_t port, [OpenCallback](Classes/classllarp_1_1quic_1_1TunnelManager.md#using-opencallback) on_open ={}, [SockAddr](Classes/structllarp_1_1SockAddr.md) bind_addr ={127, 0, 0, 1})<br>Opens a quic tunnel to some remote lokinet address.  |
| void | **[close](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-close)**(int id)<br>Start closing an outgoing tunnel; takes the ID returned by `[open()](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-open)`.  |
| void | **[receive_packet](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-receive-packet)**(const [service::ConvoTag](Classes/structllarp_1_1service_1_1ConvoTag.md) & tag, const [llarp_buffer_t](Classes/structllarp__buffer__t.md) & buf)<br>Called from tun code to deliver a quic packet.  |
| bool | **[hasListeners](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-haslisteners)**() const<br>return true if we have any listeners added  |

## Public Attributes

|                | Name           |
| -------------- | -------------- |
| std::chrono::milliseconds | **[open_timeout](Classes/classllarp_1_1quic_1_1TunnelManager.md#variable-open-timeout)**  |

## Public Types Documentation

### using ListenHandler

```cpp
using llarp::quic::TunnelManager::ListenHandler =  std::function<std::optional<SockAddr>( std::string_view lokinet_addr, uint16_t port )>;
```


### using OpenCallback

```cpp
using llarp::quic::TunnelManager::OpenCallback =  std::function<void(bool success)>;
```

Called when open succeeds or times out. 

## Public Functions Documentation

### function TunnelManager

```cpp
TunnelManager(
    EndpointBase & endpoint
)
```


### function listen

```cpp
int listen(
    ListenHandler handler
)
```

Adds an incoming listener callback. 

When a new incoming quic connection is initiated to us by some remote we invoke these callback(s) in order of registration. Each one has three options:

* return a concrete [llarp::SockAddr](Classes/structllarp_1_1SockAddr.md) giving the TCP address/port to which we should connect new incoming streams over the connection.
* returns std::nullopt to decline handling the connection (we will try the next listen handler, in order of registration).
* throws an exception (derived from std::exception) in which case we refuse the connection without trying any additional handlers.
If `[listen()](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-listen)` is not called at all then new incoming connections will be immediately dropped.

For plain-C wrappers around this see [FIXME]. 


### function listen

```cpp
int listen(
    SockAddr port
)
```

Simple wrapper around `listen(...)` that adds a handler that accepts all incoming connections trying to tunnel to port `port` and maps them to `localhost:port`. 

### function forget

```cpp
void forget(
    int id
)
```

Removes an incoming connection handler; takes the ID returned by `[listen()](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-listen)`. 

### function open

```cpp
std::pair< SockAddr, uint16_t > open(
    std::string_view remote_addr,
    uint16_t port,
    OpenCallback on_open ={},
    SockAddr bind_addr ={127, 0, 0, 1}
)
```

Opens a quic tunnel to some remote lokinet address. 

**Parameters**: 

  * **remote_addr** is the lokinet address or ONS name (e.g. `azfojblahblahblah.loki` or `blocks.loki`) that the tunnel should connect to. 
  * **port** is the tunneled port on the remote that the client wants to reach. (This is _not_ the quic pseudo-port, which is always 0). 
  * **callback** callback invoked when the quic connection has been established, or has timed out. 
  * **bind_addr** is the bind address and port that we should use for the localhost TCP connection. Use port 0 to let the OS choose a random high port. Defaults to `127.0.0.1:0`.


**Return**: a pair:

* [SockAddr](Classes/structllarp_1_1SockAddr.md) containing the just-opened localhost socket that tunnels to the remote. This is typically the same IP as `bind_addr`, with the port filled in (if bind_addr had a 0 port). Note that, while you can connect to this socket immediately, it will block until the actual connection and streams are established (and will be closed if they fail).
* unique integer that can be passed to [close()](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-close) to stop listening for new connections. This also serves as a unique internal "pseudo-port" number to route returned quic packets to the right connection.

(Should only be called from the event loop thread.)


This call immediately opens the local TCP socket, and initiates the lokinet connection and QUIC tunnel to the remote. If the connection fails, the TCP socket will be closed. Note, however, that this TCP socket will block until the underlying quic connection is established.

Each connection to the local TCP socket establishes a new stream over the QUIC connection.


TODO: add a callback to invoke when QUIC connection succeeds or fails. TODO: add a plain C wrapper around this 


### function close

```cpp
void close(
    int id
)
```

Start closing an outgoing tunnel; takes the ID returned by `[open()](Classes/classllarp_1_1quic_1_1TunnelManager.md#function-open)`. 

Note that an existing established tunneled connections will not be forcibly closed; this simply stops accepting new tunnel connections. 


### function receive_packet

```cpp
void receive_packet(
    const service::ConvoTag & tag,
    const llarp_buffer_t & buf
)
```

Called from tun code to deliver a quic packet. 

**Parameters**: 

  * **dest** - the convotag for which the packet arrived 
  * **buf** - the raw arriving packet 


### function hasListeners

```cpp
inline bool hasListeners() const
```

return true if we have any listeners added 

## Public Attributes Documentation

### variable open_timeout

```cpp
std::chrono::milliseconds open_timeout = 4s;
```


-------------------------------

Updated on 2022-09-10 at 18:17:14 +0000