# Copyright (c) 2014-2026 Savoir-faire Linux Inc. # SPDX-License-Identifier: MIT from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t from libcpp cimport bool, nullptr_t, nullptr from libcpp.string cimport string from libcpp.vector cimport vector from libcpp.utility cimport pair from libcpp.map cimport map from libcpp.memory cimport shared_ptr, make_shared from libc.string cimport const_char ctypedef uint16_t in_port_t ctypedef unsigned short int sa_family_t cdef extern from "" namespace "std::chrono" nogil: cdef cppclass duration[ulong]: duration() except + cdef cppclass seconds: duration seconds(uint64_t) except + duration seconds() cdef extern from "" namespace "std" nogil: cdef cppclass hash[T]: hash() except + size_t get "operator()"(T) cdef extern from "" namespace "std" nogil: cdef cppclass shared_future[T]: shared_future() except + bool valid() const cdef cppclass future[T]: future() except + bool valid() const shared_future[T] share() cdef extern from "opendht/utils.h" namespace "dht": const char* version() cdef cppclass time_point: @staticmethod time_point min() const @staticmethod time_point max() const cdef extern from "opendht/infohash.h" namespace "dht": cdef cppclass InfoHash: InfoHash() except + InfoHash(string s) except + string toString() const bool getBit(unsigned bit) const void setBit(unsigned bit, bool b) double toFloat() const @staticmethod unsigned commonBits(InfoHash a, InfoHash b) @staticmethod InfoHash get(string s) @staticmethod InfoHash getRandom() bool operator==(InfoHash) const bool operator<(InfoHash) const bool operator bool() const cdef cppclass PkId: PkId() except + PkId(string s) except + string toString() const bool getBit(unsigned bit) const void setBit(unsigned bit, bool b) double toFloat() const @staticmethod unsigned commonBits(PkId a, PkId b) @staticmethod PkId get(string s) @staticmethod PkId getRandom() bool operator==(PkId) const bool operator<(PkId) const bool operator bool() const cdef extern from "opendht/sockaddr.h" namespace "dht": cdef cppclass SockAddr: SockAddr() except + string toString() const in_port_t getPort() const void setPort(in_port_t p) sa_family_t getFamily() const void setFamily(sa_family_t f) bool isLoopback() const bool isPrivate() const bool isUnspecified() const @staticmethod vector[SockAddr] resolve(string host, string service) except + ctypedef vector[uint8_t] Blob cdef extern from "opendht/crypto.h" namespace "dht::crypto": ctypedef pair[shared_ptr[PrivateKey], shared_ptr[Certificate]] Identity cdef Identity generateIdentity(string name, Identity ca, unsigned bits) except + cdef Blob aesEncrypt(Blob data, string password) except + cdef Blob aesDecrypt(Blob encrypted, string password) except + cdef cppclass PrivateKey: PrivateKey() except + PrivateKey(Blob data, string password) except + shared_ptr[PublicKey] getSharedPublicKey() except + Blob serialize(string password) except + Blob decrypt(Blob data) except + Blob sign(Blob data) except + @staticmethod PrivateKey generate() except + @staticmethod PrivateKey generateEC() except + cdef cppclass PublicKey: PublicKey() except + PublicKey(Blob data) except + InfoHash getId() except + PkId getLongId() except + void pack(Blob& data) except + Blob encrypt(Blob data) except + bool checkSignature(Blob data, Blob signature) except + bool operator==(PublicKey) const cdef cppclass Certificate: Certificate() except + Certificate(string pem) except + InfoHash getId() except + PkId getLongId() except + string toString() except + string getName() except + void revoke(PrivateKey key, Certificate cert) except + @staticmethod Certificate generate(PrivateKey key, string name, Identity ca, bool is_ca) except + shared_ptr[Certificate] issuer cdef cppclass TrustList: cppclass VerifyResult: bool operator bool() const bool isValid() const string toString() except + TrustList() except + void add(Certificate) except + void remove(Certificate) except + VerifyResult verify(Certificate) except + ctypedef TrustList.VerifyResult TrustListVerifyResult cdef extern from "opendht/value.h" namespace "dht::Value": cdef cppclass Field: pass cdef extern from "opendht/value.h" namespace "dht::Value::Field": cdef Field None cdef Field Id cdef Field ValueTypeField "dht::Value::Field::ValueType" cdef Field OwnerPk cdef Field SeqNum cdef Field UserType cdef Field COUNT cdef extern from "opendht/value.h" namespace "dht": cdef cppclass Value: Value() except + Value(vector[uint8_t]) except + Value(const uint16_t t, const uint8_t* dat_ptr, size_t dat_len) except + string toString() const size_t size() const bool isSigned() const bool isEncrypted() const uint64_t id shared_ptr[PublicKey] owner InfoHash recipient vector[uint8_t] data string user_type uint16_t type uint16_t seq unsigned priority string pushType cdef cppclass ValueType: ValueType() except + ValueType(uint16_t id, string name, seconds expiration) except + uint16_t id string name seconds expiration cdef cppclass Query: Query() except + Query(Select s, Where w) except + Query(string q_str) except + bool isSatisfiedBy(const Query& q) const string toString() const cdef cppclass Select: Select() except + Select(const string& q_str) except + bool isSatisfiedBy(const Select& os) const Select& field(Field field) string toString() const cdef cppclass Where: Where() except + Where(const string& q_str) except + bool isSatisfiedBy(const Where& where) const Where& id(uint64_t id) Where& valueType(uint16_t type) Where& owner(InfoHash owner_pk_hash) Where& seq(uint16_t seq_no) Where& userType(string user_type) string toString() const cdef cppclass FieldValue: uint64_t getInt() const InfoHash getHash() const Blob getBlob() const cdef cppclass FieldValueIndex: map[Field, FieldValue] index cdef extern from "opendht/node.h" namespace "dht": cdef cppclass Node: Node() except + InfoHash getId() const string getAddrStr() const bool isExpired() const cdef extern from "opendht/callbacks.h" namespace "dht": cdef cppclass NodeStats: unsigned good_nodes unsigned dubious_nodes unsigned cached_nodes unsigned incoming_nodes unsigned table_depth unsigned searches unsigned node_cache_size unsigned getKnownNodes() const unsigned long getNetworkSizeEstimation() const cdef cppclass NodeInfo: InfoHash id InfoHash node_id NodeStats ipv4 NodeStats ipv6 size_t ongoing_ops size_t storage_values size_t storage_size size_t local_storage_values size_t local_storage_size in_port_t bound4 in_port_t bound6 ctypedef void (*ShutdownCallbackRaw)(void *user_data) ctypedef bool (*GetCallbackRaw)(shared_ptr[Value] values, void *user_data) ctypedef bool (*ValueCallbackRaw)(shared_ptr[Value] values, bool expired, void *user_data) ctypedef void (*DoneCallbackRaw)(bool done, vector[shared_ptr[Node]]* nodes, void *user_data) ctypedef void (*DoneCallbackSimpleRaw)(bool done, void *user_data) cppclass ShutdownCallback: ShutdownCallback() except + cppclass GetCallback: GetCallback() except + cppclass ValueCallback: ValueCallback() except + cppclass DoneCallback: DoneCallback() except + cppclass DoneCallbackSimple: DoneCallbackSimple() except + cdef ShutdownCallback bindShutdownCb(ShutdownCallbackRaw cb, void *user_data) cdef GetCallback bindGetCb(GetCallbackRaw cb, void *user_data) cdef ValueCallback bindValueCb(ValueCallbackRaw cb, void *user_data) cdef DoneCallback bindDoneCb(DoneCallbackRaw cb, void *user_data) cdef DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw cb, void *user_data) cppclass Config: InfoHash node_id uint32_t network bool is_bootstrap bool maintain_storage string persist_path size_t max_req_per_sec size_t max_peer_req_per_sec cppclass SecureDhtConfig: Config node_config Identity id cdef extern from *: """ #include #include static std::shared_ptr opendht_python_owner(PyObject* data) { auto state = PyGILState_Ensure(); Py_INCREF(data); PyGILState_Release(state); return std::shared_ptr(data, [](PyObject* object) { auto state = PyGILState_Ensure(); Py_DECREF(object); PyGILState_Release(state); }); } static dht::ValueCallback opendht_bind_python_value_cb( dht::ValueCallbackRaw callback, PyObject* data) { auto owner = opendht_python_owner(data); return [callback, owner = std::move(owner)]( const std::vector>& values, bool expired) { for (const auto& value : values) if (!callback(value, expired, owner.get())) return false; return true; }; } static dht::DhtRunner::Context opendht_python_context( void (*callback)(int, int, void*), PyObject* data) { dht::DhtRunner::Context context; auto owner = opendht_python_owner(data); context.statusChangedCallback = [callback, owner = std::move(owner)]( dht::NodeStatus ipv4, dht::NodeStatus ipv6) { callback(static_cast(ipv4), static_cast(ipv6), owner.get()); }; return context; } static void opendht_python_run(dht::DhtRunner& runner, in_port_t port, dht::DhtRunner::Config& config, void (*callback)(int, int, void*), PyObject* data) { runner.run(port, config, opendht_python_context(callback, data)); } static void opendht_python_run(dht::DhtRunner& runner, const char* ipv4, const char* ipv6, const char* port, dht::DhtRunner::Config& config, void (*callback)(int, int, void*), PyObject* data) { runner.run(ipv4, ipv6, port, config, opendht_python_context(callback, data)); } static void opendht_python_query(dht::DhtRunner& runner, const dht::InfoHash& key, bool (*callback)(std::shared_ptr, void*), dht::DoneCallbackRaw done, PyObject* data, dht::Query query) { auto owner = opendht_python_owner(data); runner.query(key, [callback, owner](const std::vector>& rows) { for (const auto& row : rows) if (!callback(row, owner.get())) return false; return true; }, [done, owner](bool ok, const std::vector>& nodes) { done(ok, const_cast>*>(&nodes), owner.get()); }, std::move(query)); } """ ValueCallback bindPythonValueCb "opendht_bind_python_value_cb"(ValueCallbackRaw callback, object data) except + ctypedef future[size_t] ListenToken ctypedef shared_future[size_t] SharedListenToken cdef extern from "opendht/dhtrunner.h" namespace "dht": cdef cppclass DhtRunner: DhtRunner() except + cppclass Config: SecureDhtConfig dht_config bool threaded string proxy_server string push_node_id string push_token string push_topic string push_platform string proxy_user_agent bool peer_discovery bool peer_publish shared_ptr[Certificate] server_ca Identity client_identity SockAddr bind4 SockAddr bind6 InfoHash getId() except + nogil shared_ptr[PublicKey] getPublicKey() except + nogil InfoHash getNodeId() except + nogil NodeInfo getNodeInfo() except + nogil vector[SockAddr] getPublicAddress(sa_family_t af) except + nogil void connectivityChanged() except + nogil void bootstrap(const_char*, const_char*) void bootstrap(const SockAddr&, DoneCallbackSimple done_cb) void run(in_port_t, Config config) except + void run(const_char*, const_char*, const_char*, Config config) except + void join() except + nogil void shutdown(ShutdownCallback) void registerType(const ValueType& value) except + nogil bool isRunning() SockAddr getBound(sa_family_t af) except + nogil string getStorageLog() except + nogil string getRoutingTablesLog(sa_family_t af) except + nogil string getSearchesLog(sa_family_t af) except + nogil void get(InfoHash key, GetCallback get_cb, DoneCallback done_cb, nullptr_t f, Where w) void put(InfoHash key, shared_ptr[Value] val, DoneCallback done_cb, time_point created, bool permanent) void putSigned(InfoHash key, shared_ptr[Value] val, DoneCallback done_cb, bool permanent) void putEncrypted(InfoHash key, PkId to, shared_ptr[Value] val, DoneCallback done_cb, bool permanent) void putEncrypted(InfoHash key, shared_ptr[PublicKey] to, shared_ptr[Value] val, DoneCallback done_cb, bool permanent) void cancelPut(InfoHash key, shared_ptr[Value] val) ListenToken listen(InfoHash key, ValueCallback get_cb, nullptr_t f, Where w) except + void cancelListen(InfoHash key, SharedListenToken token) except + vector[unsigned] getNodeMessageStats(bool i) except + nogil ctypedef DhtRunner.Config DhtRunnerConfig cdef extern from *: ctypedef void (*StatusCallbackRaw)(int ipv4, int ipv6, void* data) ctypedef bool (*QueryCallbackRaw)(shared_ptr[FieldValueIndex] row, void* data) void runPythonRunner "opendht_python_run"(DhtRunner& runner, in_port_t port, DhtRunnerConfig& config, StatusCallbackRaw callback, object data) except + nogil void runPythonRunnerAddress "opendht_python_run"(DhtRunner& runner, const_char* ipv4, const_char* ipv6, const_char* port, DhtRunnerConfig& config, StatusCallbackRaw callback, object data) except + nogil void queryPython "opendht_python_query"(DhtRunner& runner, InfoHash key, QueryCallbackRaw callback, DoneCallbackRaw done, object data, Query query) except + cdef extern from "opendht/log.h" namespace "dht::log": void enableLogging(DhtRunner& dht) except + nogil void disableLogging(DhtRunner& dht) except + nogil void enableFileLogging(DhtRunner& dht, const string& path) except + nogil cdef extern from "opendht/indexation/pht.h" namespace "dht::indexation": size_t PHT_MAX_NODE_ENTRY_COUNT "dht::indexation::Pht::MAX_NODE_ENTRY_COUNT" cdef cppclass Prefix: Prefix() except + Prefix(vector[uint8_t]) except + string toString() const ctypedef pair[InfoHash, uint64_t] IndexValue "dht::indexation::Value" ctypedef map[string, vector[uint8_t]] IndexKey "dht::indexation::Pht::Key" ctypedef map[string, uint32_t] IndexKeySpec "dht::indexation::Pht::KeySpec" ctypedef void (*LookupCallbackRaw)(vector[shared_ptr[IndexValue]]* values, Prefix* p, void* user_data); cdef cppclass Pht: cppclass LookupCallback: LookupCallback() except + Pht(string, IndexKeySpec, shared_ptr[DhtRunner]) except + void lookup(IndexKey k, LookupCallback cb, DoneCallbackSimple doneCb) except + void insert(IndexKey k, IndexValue v, DoneCallbackSimple cb) except + @staticmethod LookupCallback bindLookupCb(LookupCallbackRaw cb, void *user_data) cdef extern from "gnutls/gnutls.h" nogil: int gnutls_global_init() void gnutls_global_deinit() const char* gnutls_strerror(int error)