diff --git a/python/opendht.pyx b/python/opendht.pyx index 04f51803..713ad00b 100644 --- a/python/opendht.pyx +++ b/python/opendht.pyx @@ -34,6 +34,20 @@ cimport opendht_cpp as cpp import threading +cdef inline void _initialize_gnutls(): + cdef int err = cpp.gnutls_global_init() + if err != 0: + print(f"Failed to initialize GnuTLS: {cpp.gnutls_strerror(err).decode()}") + else: + import atexit + atexit.register(_deinitialize_gnutls) + +cdef inline _deinitialize_gnutls(): + cpp.gnutls_global_deinit() + +# Initialize GnuTLS when this module is imported +_initialize_gnutls() + cdef inline void lookup_callback(cpp.vector[cpp.shared_ptr[cpp.IndexValue]]* values, cpp.Prefix* p, void *user_data) noexcept with gil: cbs = user_data if 'lookup' in cbs and cbs['lookup']: diff --git a/python/opendht_cpp.pxd b/python/opendht_cpp.pxd index 01c561a0..c1cdc94a 100644 --- a/python/opendht_cpp.pxd +++ b/python/opendht_cpp.pxd @@ -326,3 +326,8 @@ cdef extern from "opendht/indexation/pht.h" namespace "dht::indexation": void insert(IndexKey k, IndexValue v, DoneCallbackSimple cb) @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) diff --git a/python/setup.py.in b/python/setup.py.in index 3b20ef14..e61ff800 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -42,7 +42,7 @@ setup(name="opendht", language="c++", extra_compile_args=["-std=c++17", "-DMSGPACK_NO_BOOST", "-DMSGPACK_DISABLE_LEGACY_NIL", "-DMSGPACK_DISABLE_LEGACY_CONVERT", "-DOPENDHT_INDEXATION", "-Dopendht_EXPORTS"], extra_link_args=["-std=c++17"], - libraries=["opendht"], + libraries=["opendht", "gnutls"], include_dirs = ['@PROJECT_SOURCE_DIR@/include'@OPENDHT_PYTHON_INCLUDE_DIRS@], library_dirs = ['@CURRENT_BINARY_DIR@', '@PROJECT_BINARY_DIR@'@OPENDHT_PYTHON_LIBRARY_DIRS@] )]