Skip to content

savoirfairelinux/opendht

Repository files navigation


OpenDHT

PyPI - Version

A lightweight C++17 Distributed Hash Table implementation.

OpenDHT provides an easy to use distributed in-memory data store. Every node in the network can read and write values to the store. Values are distributed over the network, with redundancy.

  • Lightweight and scalable, designed for large networks and small devices
  • High resilience to network disruption
  • Public key cryptography layer providing optional data signature and encryption (using GnuTLS)
  • IPv4 and IPv6 support
  • Clean and powerful C++17 map API
  • Bindings for C, Rust & Python 3
  • REST API with optional HTTP client+server with push notification support

Documentation

See the wiki: https://github.com/savoirfairelinux/opendht/wiki

How to run a node

You can help contribute to the public network by running a stable node with a public IP address. https://github.com/savoirfairelinux/opendht/wiki/Running-a-node-with-dhtnode

How-to build and install

Build instructions: see BUILD.md

Examples

C++ example

The tools directory includes simple example programs :

  • dhtnode, a command line tool, allowing to run a DHT node and perform operations supported by the library (get, put etc.) with text values.
  • dhtchat, a very simple IM client working over the dht.

Example program launching a DHT node, connecting to the network and performing some basic operations:

#include <opendht.h> #include <vector> int main() { dht::DhtRunner node; // Launch a dht node on a new thread, using a // generated RSA key pair, and listen on port 4222. node.run(4222, dht::crypto::generateIdentity(), true); // Join the network through any running node, // here using a known bootstrap node. node.bootstrap("bootstrap.jami.net", "4222"); // put some data on the dht std::vector<uint8_t> some_data(5, 10); node.put("unique_key", some_data); // put some data on the dht, signed with our generated private key node.putSigned("unique_key_42", some_data); // get data from the dht node.get("other_unique_key", [](const std::vector<std::shared_ptr<dht::Value>>& values) { // Callback called when values are found for (const auto& value : values) std::cout << "Found value: " << *value << std::endl; return true; // return false to stop the search }); // wait for dht threads to end node.join(); return 0; }

Python 3 example

Install on Linux, macOS and Windows with:

pip install opendht

Using the simple, blocking API:

import opendht as dht node = dht.DhtRunner() node.run() # Join the DHT network through any running node, # here using a known bootstrap node. node.bootstrap("bootstrap.jami.net", "4222") # blocking call (provide callback arguments to make the call non-blocking) node.put(dht.InfoHash.get("unique_key"), dht.Value(b'some binary data')) results = node.get(dht.InfoHash.get("unique_key")) for value in results: print(value)

Or using asyncio:

import asyncio import opendht.aio as dht async def dht_async_demo(key_str: str): # Start a new node using an async context manager. # It is also possible to call run()/await shutdown() manually. async with dht.DhtRunner( bootstrap=(("bootstrap.jami.net", "4222"),) ) as node: # compute key hash key = dht.InfoHash.get(key_str) # put data, waiting for completion await node.put(key, dht.Value(b'tata data')) # get all values at key results = await node.getAll(key) for value in results: print(value) # same operation, but stream values as they come from the network with node.get(key) as results: async for value in results: print(value) # listen for change of values at key with node.listen(key) as values: async for value, expired in values: print(value) if value.data == b'tata data': break asyncio.run(dht_async_demo("unique_key"))

Dependencies

Core dependencies:

  • msgpack-c 1.2+, used for data serialization.
  • GnuTLS 3.3+, used for cryptographic operations.
  • Nettle 2.4+, a GnuTLS dependency for crypto.
  • {fmt} 9.0+, for log formatting.

Optional dependencies for the REST API (DHT proxy client/server):

  • (optional) restinio
  • (optional) llhttp
  • (optional) jsoncpp 1.7.4-3+
  • (optional) simdutf

Build environment:

  • Build tested with GCC 9+ (GNU/Linux, Windows with MinGW), Clang/LLVM (GNU/Linux, Android, macOS, iOS).
  • Build tested with Microsoft Visual Studio 2019, 2022

License

Copyright (c) 2014-2026 Savoir-faire Linux Inc.

OpenDHT is released under the MIT License. See LICENSE for details.

Acknowledgements

This project was originally based on https://github.com/jech/dht by Juliusz Chroboczek.

About

OpenDHT: a C++17 Distributed Hash Table implementation

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages