diff --git a/include/connectionmanager.h b/include/connectionmanager.h index e459eb3f873b3a91b46b314a3a2b3be4fa3e7269..17300dcfefa7788881a189e456539783a5f2ad14 100644 --- a/include/connectionmanager.h +++ b/include/connectionmanager.h @@ -72,6 +72,8 @@ using ChannelRequestCallback = std::function<bool(const std::shared_ptr<dht::cry * Used by connectDevice, when the socket is ready */ using ConnectCallback = std::function<void(const std::shared_ptr<ChannelSocket>&, const DeviceId&)>; +using ConnectCallbackLegacy = std::function<void(const std::shared_ptr<ChannelSocket>&, const dht::InfoHash&)>; + /** * Used when an incoming connection is ready */ @@ -111,6 +113,13 @@ public: bool noNewSocket = false, bool forceNewSocket = false, const std::string& connType = ""); + void connectDevice(const dht::InfoHash& deviceId, + const std::string& name, + ConnectCallbackLegacy cb, + bool noNewSocket = false, + bool forceNewSocket = false, + const std::string& connType = ""); + void connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name, ConnectCallback cb, diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp index 6c6de311d58fa40159e46e6fd65a949b0d4b5736..507a297e5ea496c8557c4d78c441a2eac10b3bd8 100644 --- a/src/connectionmanager.cpp +++ b/src/connectionmanager.cpp @@ -148,6 +148,13 @@ public: bool noNewSocket = false, bool forceNewSocket = false, const std::string& connType = ""); + void connectDevice(const dht::InfoHash& deviceId, + const std::string& uri, + ConnectCallbackLegacy cb, + bool noNewSocket = false, + bool forceNewSocket = false, + const std::string& connType = ""); + void connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name, ConnectCallback cb, @@ -602,6 +609,53 @@ ConnectionManager::Impl::connectDevice(const DeviceId& deviceId, }); } +void +ConnectionManager::Impl::connectDevice(const dht::InfoHash& deviceId, + const std::string& name, + ConnectCallbackLegacy cb, + bool noNewSocket, + bool forceNewSocket, + const std::string& connType) +{ + if (!dht()) { + cb(nullptr, deviceId); + return; + } + if (deviceId.toString() == identity().second->getLongId().toString()) { + cb(nullptr, deviceId); + return; + } + findCertificate(deviceId, + [w = weak(), + deviceId, + name, + cb = std::move(cb), + noNewSocket, + forceNewSocket, + connType](const std::shared_ptr<dht::crypto::Certificate>& cert) { + if (!cert) { + if (auto shared = w.lock()) + if (shared->config_->logger) + shared->config_->logger->error( + "No valid certificate found for device {}", + deviceId); + cb(nullptr, deviceId); + return; + } + if (auto shared = w.lock()) { + shared->connectDevice(cert, + name, + [cb, deviceId](const std::shared_ptr<ChannelSocket>& sock, const DeviceId& did){ + cb(sock, deviceId); + }, + noNewSocket, + forceNewSocket, + connType); + } else + cb(nullptr, deviceId); + }); +} + void ConnectionManager::Impl::connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name, @@ -1534,6 +1588,18 @@ ConnectionManager::connectDevice(const DeviceId& deviceId, pimpl_->connectDevice(deviceId, name, std::move(cb), noNewSocket, forceNewSocket, connType); } +void +ConnectionManager::connectDevice(const dht::InfoHash& deviceId, + const std::string& name, + ConnectCallbackLegacy cb, + bool noNewSocket, + bool forceNewSocket, + const std::string& connType) +{ + pimpl_->connectDevice(deviceId, name, std::move(cb), noNewSocket, forceNewSocket, connType); +} + + void ConnectionManager::connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name,