diff --git a/CMakeLists.txt b/CMakeLists.txt index ada6d334..f0c57f77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ if (OPENDHT_TOOLS) find_package (Readline 6 REQUIRED) endif () -set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11 -Wno-return-type -Wall -Wextra -Wnon-virtual-dtor -pedantic-errors") +set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11 -Wno-return-type -Wall -Wextra -Wnon-virtual-dtor -pedantic-errors -fvisibility=hidden") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT") if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) @@ -131,6 +131,7 @@ if (OPENDHT_SHARED) ) set_target_properties (opendht PROPERTIES IMPORT_SUFFIX "_import.lib") set_target_properties (opendht PROPERTIES SOVERSION ${opendht_VERSION_MAJOR} VERSION ${opendht_VERSION}) + target_compile_definitions(opendht PRIVATE OPENDHT_BUILD) if (OPENDHT_LTO) target_link_libraries(opendht -flto -fuse-linker-plugin) endif () diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h index 22062a6f..cea81f1a 100644 --- a/include/opendht/callbacks.h +++ b/include/opendht/callbacks.h @@ -42,7 +42,7 @@ enum class NodeStatus { /** * Dht configuration. */ -struct Config { +struct OPENDHT_PUBLIC Config { /** DHT node ID */ InfoHash node_id; @@ -63,7 +63,7 @@ struct Config { /** * SecureDht configuration. */ -struct SecureDhtConfig +struct OPENDHT_PUBLIC SecureDhtConfig { Config node_config; crypto::Identity id; @@ -82,8 +82,8 @@ using CertificateStoreQuery = std::function, void *user_data); -GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data); -GetCallback bindGetCb(GetCallbackSimple cb); +OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data); +OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb); using DoneCallback = std::function>& nodes)>; typedef void (*DoneCallbackRaw)(bool, std::vector>*, void *user_data); @@ -92,9 +92,9 @@ typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data); using DoneCallbackSimple = std::function; -ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data); -DoneCallback bindDoneCb(DoneCallbackSimple donecb); -DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data); -DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data); +OPENDHT_PUBLIC ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data); +OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackSimple donecb); +OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data); +OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data); } diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 897abd25..f2276523 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -38,7 +38,7 @@ extern "C" { namespace dht { namespace crypto { -class CryptoException : public std::runtime_error { +class OPENDHT_PUBLIC CryptoException : public std::runtime_error { public: CryptoException(const std::string& str) : std::runtime_error(str) {}; }; @@ -46,7 +46,7 @@ class CryptoException : public std::runtime_error { /** * Exception thrown when a decryption error happened. */ -class DecryptError : public CryptoException { +class OPENDHT_PUBLIC DecryptError : public CryptoException { public: DecryptError(const std::string& str = "") : CryptoException(str) {}; }; @@ -59,7 +59,7 @@ using Identity = std::pair, std::shared_ptr +class OPENDHT_PUBLIC DhtMessage : public Value::Serializable { public: static const ValueType TYPE; @@ -61,7 +61,7 @@ public: }; template -class SignedValue : public Value::Serializable +class OPENDHT_PUBLIC SignedValue : public Value::Serializable { private: using BaseClass = Value::Serializable; @@ -81,7 +81,7 @@ public: }; template -class EncryptedValue : public SignedValue +class OPENDHT_PUBLIC EncryptedValue : public SignedValue { public: using BaseClass = SignedValue; @@ -105,7 +105,7 @@ public: -class ImMessage : public SignedValue +class OPENDHT_PUBLIC ImMessage : public SignedValue { private: using BaseClass = SignedValue; @@ -131,7 +131,7 @@ public: MSGPACK_DEFINE_MAP(id, msg, date, status) }; -class TrustRequest : public EncryptedValue +class OPENDHT_PUBLIC TrustRequest : public EncryptedValue { private: using BaseClass = EncryptedValue; @@ -152,7 +152,7 @@ public: MSGPACK_DEFINE(service, payload) }; -class IceCandidates : public EncryptedValue +class OPENDHT_PUBLIC IceCandidates : public EncryptedValue { private: using BaseClass = EncryptedValue; @@ -198,7 +198,7 @@ public: /* "Peer" announcement */ -class IpServiceAnnouncement : public Value::Serializable +class OPENDHT_PUBLIC IpServiceAnnouncement : public Value::Serializable { private: using BaseClass = Value::Serializable; @@ -273,8 +273,8 @@ private: }; -extern const std::array, 5> DEFAULT_TYPES; +OPENDHT_PUBLIC extern const std::array, 5> DEFAULT_TYPES; -extern const std::array, 1> DEFAULT_INSECURE_TYPES; +OPENDHT_PUBLIC extern const std::array, 1> DEFAULT_INSECURE_TYPES; } diff --git a/include/opendht/dht.h b/include/opendht/dht.h index 8e00c0a0..8d1303db 100644 --- a/include/opendht/dht.h +++ b/include/opendht/dht.h @@ -52,7 +52,7 @@ struct Request; * Must be given open UDP sockets and ::periodic must be * called regularly. */ -class Dht { +class OPENDHT_PUBLIC Dht { public: // [[deprecated]] diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index a5e96f28..c7c20dc2 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -26,6 +26,7 @@ #include "callbacks.h" #include "sockaddr.h" #include "log_enable.h" +#include "def.h" #include #include @@ -48,7 +49,7 @@ struct SecureDhtConfig; * either wait for (expectedly frequent) calls to ::loop() or start an internal * thread that will update the DHT when appropriate. */ -class DhtRunner { +class OPENDHT_PUBLIC DhtRunner { public: typedef std::function StatusCallback; diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index 534e7319..6131b83f 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -26,7 +26,7 @@ namespace indexation { * the node in question is a leaf, *the label is a prefix of all the keys * contained in the leaf*. */ -struct Prefix { +struct OPENDHT_PUBLIC Prefix { Prefix() {} Prefix(InfoHash h) : size_(h.size() * 8), content_(h.begin(), h.end()) { } Prefix(const Blob& d, const Blob& f={}) : size_(d.size()*8), flags_(f), content_(d) { } @@ -245,7 +245,7 @@ private: }; using Value = std::pair; -struct IndexEntry : public dht::Value::Serializable { +struct OPENDHT_PUBLIC IndexEntry : public dht::Value::Serializable { static const ValueType TYPE; virtual void unpackValue(const dht::Value& v) { @@ -265,7 +265,7 @@ struct IndexEntry : public dht::Value::Serializable { MSGPACK_DEFINE_MAP(prefix, value) }; -class Pht { +class OPENDHT_PUBLIC Pht { static constexpr const char* INVALID_KEY = "Key does not match the PHT key spec."; /* Prefixes the user_type for all dht values put on the DHT */ diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index 47d180e9..a8f6353a 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -19,6 +19,8 @@ #pragma once +#include "def.h" + #include #ifndef _WIN32 @@ -53,7 +55,7 @@ namespace dht { * An InfoHash is a byte array of HASH_LEN bytes. * InfoHashes identify nodes and values in the Dht. */ -class InfoHash final : public std::array { +class OPENDHT_PUBLIC InfoHash final : public std::array { public: constexpr InfoHash() : std::array() {} constexpr InfoHash(const std::array& h) : std::array(h) {} @@ -192,8 +194,8 @@ public: static InfoHash getRandom(); - friend std::ostream& operator<< (std::ostream& s, const InfoHash& h); - friend std::istream& operator>> (std::istream& s, InfoHash& h); + OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const InfoHash& h); + OPENDHT_PUBLIC friend std::istream& operator>> (std::istream& s, InfoHash& h); std::string toString() const; diff --git a/include/opendht/node.h b/include/opendht/node.h index 6cbfa9c8..638444a5 100644 --- a/include/opendht/node.h +++ b/include/opendht/node.h @@ -86,7 +86,7 @@ struct Node { std::string toString() const; - friend std::ostream& operator<< (std::ostream& s, const Node& h); + OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const Node& h); static constexpr const std::chrono::minutes NODE_GOOD_TIME {120}; diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h index 325615cd..a03b6f6f 100644 --- a/include/opendht/securedht.h +++ b/include/opendht/securedht.h @@ -40,7 +40,7 @@ namespace dht { -class SecureDht : public Dht { +class OPENDHT_PUBLIC SecureDht : public Dht { public: typedef std::function SignatureCheckCallback; diff --git a/include/opendht/sockaddr.h b/include/opendht/sockaddr.h index 4a3ccd40..2381920e 100644 --- a/include/opendht/sockaddr.h +++ b/include/opendht/sockaddr.h @@ -18,6 +18,8 @@ #pragma once +#include "def.h" + #ifndef _WIN32 #include #else @@ -30,10 +32,10 @@ typedef uint16_t in_port_t; namespace dht { -std::string print_addr(const sockaddr* sa, socklen_t slen); -std::string print_addr(const sockaddr_storage& ss, socklen_t sslen); +OPENDHT_PUBLIC std::string print_addr(const sockaddr* sa, socklen_t slen); +OPENDHT_PUBLIC std::string print_addr(const sockaddr_storage& ss, socklen_t sslen); -struct SockAddr : public std::pair { +struct OPENDHT_PUBLIC SockAddr : public std::pair { public: SockAddr() : pair::pair({},0) {} SockAddr(const SockAddr& o) : pair::pair({},o.second) { @@ -68,8 +70,8 @@ public: sa_family_t getFamily() const { return second > sizeof(sa_family_t) ? first.ss_family : AF_UNSPEC; } }; -bool operator==(const SockAddr& a, const SockAddr& b); +OPENDHT_PUBLIC bool operator==(const SockAddr& a, const SockAddr& b); -std::string printAddr(const SockAddr& addr); +OPENDHT_PUBLIC std::string printAddr(const SockAddr& addr); } diff --git a/include/opendht/utils.h b/include/opendht/utils.h index 92424cc8..6f830582 100644 --- a/include/opendht/utils.h +++ b/include/opendht/utils.h @@ -18,8 +18,7 @@ #pragma once -#define WANT4 1 -#define WANT6 2 +#include "def.h" #include @@ -30,6 +29,9 @@ #include +#define WANT4 1 +#define WANT6 2 + namespace dht { using NetId = uint32_t; @@ -45,7 +47,7 @@ void erase_if(std::map& map, const Condition& condition) } } -class DhtException : public std::runtime_error { +class OPENDHT_PUBLIC DhtException : public std::runtime_error { public: DhtException(const std::string &str = "") : std::runtime_error("DhtException occurred: " + str) {} @@ -93,7 +95,7 @@ using Blob = std::vector; /** * Provides backward compatibility with msgpack 1.0 */ -Blob unpackBlob(msgpack::object& o); +OPENDHT_PUBLIC Blob unpackBlob(msgpack::object& o); template Blob diff --git a/include/opendht/value.h b/include/opendht/value.h index bb574462..1e5c23d7 100644 --- a/include/opendht/value.h +++ b/include/opendht/value.h @@ -72,7 +72,7 @@ using EditPolicy = std::function static constexpr const size_t MAX_VALUE_SIZE {1024 * 64}; -struct ValueType { +struct OPENDHT_PUBLIC ValueType { typedef uint16_t Id; static bool DEFAULT_STORE_POLICY(InfoHash, std::shared_ptr& v, InfoHash, const sockaddr*, socklen_t); @@ -114,7 +114,7 @@ struct ValueType { * Values are stored at a given InfoHash in the Dht, but also have a * unique ID to distinguish between values stored at the same location. */ -struct Value +struct OPENDHT_PUBLIC Value { enum class Field : int { None = 0, @@ -409,7 +409,7 @@ struct Value } /** print value for debugging */ - friend std::ostream& operator<< (std::ostream& s, const Value& v); + OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const Value& v); std::string toString() const { std::stringstream ss; @@ -553,7 +553,7 @@ using ValuesExport = std::pair; * This structure holds the value for a specified field. It's type can either be * uint64_t, InfoHash or Blob. */ -struct FieldValue +struct OPENDHT_PUBLIC FieldValue { FieldValue() {} FieldValue(Value::Field f, uint64_t int_value) : field(f), intValue(int_value) {} @@ -638,7 +638,7 @@ private: * This is meant to narrow data to a set of specified fields. This structure is * used to construct a Select structure. */ -struct FieldSelectorDescription +struct OPENDHT_PUBLIC FieldSelectorDescription { FieldSelectorDescription() {} FieldSelectorDescription(Value::Field f) : field(f) {} @@ -661,7 +661,7 @@ private: * This is a container for a list of FieldSelectorDescription instances. It * describes a complete SELECT query for dht::Value. */ -struct Select +struct OPENDHT_PUBLIC Select { Select() { } Select(const std::string& q_str); @@ -700,7 +700,7 @@ struct Select fieldSelection_ = o.as(); } - friend std::ostream& operator<<(std::ostream& s, const dht::Select& q); + OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& s, const dht::Select& q); private: std::vector fieldSelection_ {}; }; @@ -712,7 +712,7 @@ private: * This is container for a list of FieldValue instances. It describes a * complete WHERE query for dht::Value. */ -struct Where +struct OPENDHT_PUBLIC Where { Where() { } Where(const std::string& q_str); @@ -802,7 +802,7 @@ struct Where filters_ = o.as(); } - friend std::ostream& operator<<(std::ostream& s, const dht::Where& q); + OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& s, const dht::Where& q); private: std::vector filters_; @@ -816,7 +816,7 @@ private: * itselves to include in the peer response to a GET operation. See * FieldValue. */ -struct Query +struct OPENDHT_PUBLIC Query { static const std::string QUERY_PARSE_ERROR; @@ -864,7 +864,7 @@ struct Query return ss.str(); } - friend std::ostream& operator<<(std::ostream& s, const dht::Query& q) { + OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& s, const dht::Query& q) { return s << "Query[" << q.select << " " << q.where << "]"; } @@ -880,7 +880,7 @@ struct Query * This structures is meant to manipulate a subset of fields normally contained * in Value. */ -struct FieldValueIndex { +struct OPENDHT_PUBLIC FieldValueIndex { FieldValueIndex() {} FieldValueIndex(const Value& v, Select s = {}); /** @@ -891,7 +891,7 @@ struct FieldValueIndex { */ bool containedIn(const FieldValueIndex& other) const; - friend std::ostream& operator<<(std::ostream& os, const FieldValueIndex& fvi); + OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& os, const FieldValueIndex& fvi); void msgpack_unpack_fields(const std::set& fields, const msgpack::object& o,