setPushNotificationToken() stores the token and returns early when the
proxy connection is not established yet, without ever scheduling the
subscriptions it skipped. Nothing retries them afterwards:
restartListeners() only resubscribes listeners whose opstate is not ok,
so listeners that are healthy keep the subscription they were created
with and the server is never told the session id of the current run.
Clients set the token while the account starts, before the first proxy
reply arrives, so this path is the common one rather than a corner case.
The server then keeps the listener registered under the session of a
previous run and stamps every notification with that stale id. The
client compares it with pushSessionId_, which is drawn once per
DhtProxyClient, and discards the notification:
if (sessionId != notification.end() and sessionId->second != pushSessionId_)
return PushNotificationResult::IgnoredWrongSession;
Observed on an Android device: every push received was rejected with
IgnoredWrongSession, 83 out of 83, including calls and the diagnostic
ping. Restarting the proxy instance cleared the stale listeners, after
which the device received no push at all, confirming it never subscribes
for push with its current session.
Remember the skipped subscriptions and send them from restartListeners()
once the connection comes up. searchLock_ is already held there, so the
loop is inlined rather than calling resubscribeAll(), and
lockCurrentProxyInfos_ is released before searchLock_ is taken in
setPushNotificationToken() to keep the lock order consistent.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
OpenDHT
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.