mirror of
https://github.com/eclipse-openvehicle-api/openvehicle-api.git
synced 2026-04-15 09:38:16 +00:00
tunnel component & update vehicle abstraction example (#8)
This commit is contained in:
40
sdv_services/uds_win_tunnel/CMakeLists.txt
Normal file
40
sdv_services/uds_win_tunnel/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
#
|
||||
# This program and the accompanying materials are made available under the
|
||||
# terms of the Apache License Version 2.0 which is available at
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#*******************************************************************************
|
||||
|
||||
# Define project
|
||||
project(uds_win_tunnel VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(uds_win_tunnel STATIC
|
||||
channel_mgnt.cpp
|
||||
connection.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(uds_win_tunnel
|
||||
PRIVATE
|
||||
uds_win_sockets
|
||||
Ws2_32.lib
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
|
||||
target_include_directories(uds_win_tunnel
|
||||
PRIVATE
|
||||
./include/
|
||||
../uds_win_sockets/
|
||||
)
|
||||
|
||||
set_target_properties(uds_win_tunnel PROPERTIES PREFIX "")
|
||||
set_target_properties(uds_win_tunnel PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
# Build dependencies
|
||||
add_dependencies(uds_win_tunnel CompileCoreIDL)
|
||||
|
||||
# Appending the service in the service list
|
||||
set(SDV_Service_List ${SDV_Service_List} uds_win_tunnel PARENT_SCOPE)
|
||||
414
sdv_services/uds_win_tunnel/channel_mgnt.cpp
Normal file
414
sdv_services/uds_win_tunnel/channel_mgnt.cpp
Normal file
@@ -0,0 +1,414 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#ifdef _WIN32
|
||||
#include "channel_mgnt.h"
|
||||
|
||||
#include "../../global/base64.h"
|
||||
#include <support/toml.h>
|
||||
#include <interfaces/process.h>
|
||||
|
||||
#include <future>
|
||||
|
||||
#pragma push_macro("interface")
|
||||
#undef interface
|
||||
|
||||
#pragma push_macro("GetObject")
|
||||
#undef GetObject
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <afunix.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
#pragma pop_macro("GetObject")
|
||||
#pragma pop_macro("interface")
|
||||
|
||||
extern int StartUpWinSock();
|
||||
|
||||
namespace
|
||||
{
|
||||
/**
|
||||
* @brief Parse a tunnel connect/config string and extract the path.
|
||||
*
|
||||
* Expected format:
|
||||
* "proto=tunnel;path=<something>;"
|
||||
*
|
||||
* Behavior:
|
||||
* - If "proto=tunnel" missing -> false
|
||||
* - If "path=" missing -> true and outPath.clear()
|
||||
*
|
||||
* @param[in] cs The connect/config string to parse.
|
||||
* @param[out] outPath The extracted path, or empty if not found.
|
||||
* @return true if parsing succeeded, false otherwise.
|
||||
*/
|
||||
static bool ParseTunnelPath(const std::string& cs, std::string& outPath)
|
||||
{
|
||||
constexpr const char* protoKey = "proto=tunnel";
|
||||
constexpr const char* pathKey = "path=";
|
||||
|
||||
if (cs.find(protoKey) == std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto p = cs.find(pathKey);
|
||||
if (p == std::string::npos)
|
||||
{
|
||||
outPath.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto start = p + std::strlen(pathKey);
|
||||
const auto end = cs.find(';', start);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
outPath = cs.substr(start);
|
||||
}
|
||||
else
|
||||
{
|
||||
outPath = cs.substr(start, end - start);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Expands Windows environment variables in a string (e.g., %TEMP%).
|
||||
* @param[in] in Input string possibly containing environment variables.
|
||||
* @return String with environment variables expanded, or original if expansion fails.
|
||||
*/
|
||||
static std::string ExpandEnvVars(const std::string& in)
|
||||
{
|
||||
if (in.find('%') == std::string::npos)
|
||||
{
|
||||
return in;
|
||||
}
|
||||
char buf[4096] = {};
|
||||
DWORD n = ExpandEnvironmentStringsA(in.c_str(), buf, static_cast<DWORD>(sizeof(buf)));
|
||||
if (n > 0 && n < sizeof(buf))
|
||||
{
|
||||
return std::string(buf);
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clamps a UDS path to the maximum allowed by SOCKADDR_UN.
|
||||
* @param[in] p The input path.
|
||||
* @return The clamped path.
|
||||
*/
|
||||
static std::string ClampUdsPath(const std::string& p)
|
||||
{
|
||||
SOCKADDR_UN tmp{};
|
||||
constexpr auto kMax = sizeof(tmp.sun_path) - 1;
|
||||
if (p.size() <= kMax)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
return p.substr(0, kMax);
|
||||
}
|
||||
|
||||
// Only for logging – basename
|
||||
/**
|
||||
* @brief Normalizes a raw UDS path for Windows, extracting the basename and ensuring a default if empty.
|
||||
* @param[in] raw The raw path string.
|
||||
* @return The normalized basename, clamped to max length.
|
||||
*/
|
||||
static std::string NormalizeUdsPathForWindows(const std::string& raw)
|
||||
{
|
||||
std::string p = ExpandEnvVars(raw);
|
||||
const size_t pos = p.find_last_of("/\\");
|
||||
std::string base = (pos == std::string::npos) ? p : p.substr(pos + 1);
|
||||
if (base.empty())
|
||||
{
|
||||
base = "sdv_tunnel.sock";
|
||||
}
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Normalize raw='", raw, "' -> base='", base, "'");
|
||||
return ClampUdsPath(base);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a short, safe UDS path in the Windows temp directory.
|
||||
* @param[in] raw The raw path string.
|
||||
* @return The full path in %TEMP%\sdv\, clamped to max length.
|
||||
*/
|
||||
static std::string MakeShortWinUdsPath(const std::string& raw)
|
||||
{
|
||||
std::string p = ExpandEnvVars(raw);
|
||||
const size_t pos = p.find_last_of("/\\");
|
||||
std::string base = (pos == std::string::npos) ? p : p.substr(pos + 1);
|
||||
if (base.empty())
|
||||
{
|
||||
base = "sdv_tunnel.sock";
|
||||
}
|
||||
|
||||
std::string dir = ExpandEnvVars("%TEMP%\\sdv\\");
|
||||
CreateDirectoryA(dir.c_str(), nullptr);
|
||||
const std::string full = dir + base;
|
||||
|
||||
return ClampUdsPath(full);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates an AF_UNIX listen socket at the specified path.
|
||||
* @param[in] rawPath The raw path for the socket.
|
||||
* @return The created socket handle, or INVALID_SOCKET on failure.
|
||||
*/
|
||||
static SOCKET CreateUnixListenSocket(const std::string& rawPath)
|
||||
{
|
||||
SOCKET s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] socket() FAIL (listen), WSA=", WSAGetLastError());
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
std::string udsPath = MakeShortWinUdsPath(rawPath);
|
||||
SOCKADDR_UN addr{};
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy_s(addr.sun_path, sizeof(addr.sun_path), udsPath.c_str());
|
||||
|
||||
const int addrlen = static_cast<int>(
|
||||
offsetof(SOCKADDR_UN, sun_path) + std::strlen(addr.sun_path) + 1);
|
||||
|
||||
::remove(udsPath.c_str());
|
||||
if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) == SOCKET_ERROR)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] bind FAIL, WSA=",
|
||||
WSAGetLastError(), ", path='", udsPath, "'");
|
||||
closesocket(s);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
if (listen(s, SOMAXCONN) == SOCKET_ERROR)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] listen FAIL, WSA=",
|
||||
WSAGetLastError(), ", path='", udsPath, "'");
|
||||
closesocket(s);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] bind+listen OK, path='", udsPath, "'");
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects to an AF_UNIX socket at the specified path, retrying until timeout.
|
||||
* @param[in] rawPath The raw path to connect to.
|
||||
* @param[in] totalTimeoutMs Total timeout in milliseconds.
|
||||
* @param[in] retryDelayMs Delay between retries in milliseconds.
|
||||
* @return The connected socket handle, or INVALID_SOCKET on failure.
|
||||
*/
|
||||
static SOCKET ConnectUnixSocket(
|
||||
const std::string& rawPath,
|
||||
uint32_t totalTimeoutMs,
|
||||
uint32_t retryDelayMs)
|
||||
{
|
||||
const std::string udsPath = MakeShortWinUdsPath(rawPath);
|
||||
SOCKADDR_UN addr{};
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy_s(addr.sun_path, sizeof(addr.sun_path), udsPath.c_str());
|
||||
|
||||
const int addrlen = static_cast<int>(
|
||||
offsetof(SOCKADDR_UN, sun_path) + std::strlen(addr.sun_path) + 1);
|
||||
|
||||
const auto deadline = std::chrono::steady_clock::now() +
|
||||
std::chrono::milliseconds(totalTimeoutMs);
|
||||
|
||||
int lastError = 0;
|
||||
while (true)
|
||||
{
|
||||
SOCKET s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
lastError = WSAGetLastError();
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] socket() FAIL (client), WSA=", lastError);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (connect(s, reinterpret_cast<const sockaddr*>(&addr), addrlen) == 0)
|
||||
{
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] connect OK, path='", udsPath, "'");
|
||||
return s;
|
||||
}
|
||||
|
||||
lastError = WSAGetLastError();
|
||||
closesocket(s);
|
||||
|
||||
if (std::chrono::steady_clock::now() >= deadline)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] connect TIMEOUT, last WSA=",
|
||||
lastError, ", path='", udsPath, "'");
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(retryDelayMs));
|
||||
}
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
bool CSocketsTunnelChannelMgnt::OnInitialize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void CSocketsTunnelChannelMgnt::OnShutdown()
|
||||
{}
|
||||
|
||||
// -------- Server bookkeeping (optional) --------
|
||||
void CSocketsTunnelChannelMgnt::OnServerClosed(const std::string& udsPath, CWinTunnelConnection* ptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
auto it = m_udsServers.find(udsPath);
|
||||
if (it != m_udsServers.end() && it->second.get() == ptr)
|
||||
{
|
||||
m_udsServers.erase(it);
|
||||
}
|
||||
m_udsServerClaimed.erase(udsPath);
|
||||
}
|
||||
|
||||
// -------- ICreateEndpoint --------
|
||||
sdv::ipc::SChannelEndpoint CSocketsTunnelChannelMgnt::CreateEndpoint(const sdv::u8string& cfgStr)
|
||||
{
|
||||
sdv::ipc::SChannelEndpoint ep{};
|
||||
|
||||
if (StartUpWinSock() != 0)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] WinSock startup failed in CreateEndpoint");
|
||||
return ep;
|
||||
}
|
||||
|
||||
// Optional TOML config: [IpcChannel] Path = "..."
|
||||
std::string udsRaw;
|
||||
if (!cfgStr.empty())
|
||||
{
|
||||
//for toml file
|
||||
bool isTOML = cfgStr.find('=') == std::string::npos;
|
||||
if(isTOML)
|
||||
{
|
||||
sdv::toml::CTOMLParser cfg(cfgStr.c_str());
|
||||
auto pathNode = cfg.GetDirect("IpcChannel.Path");
|
||||
if (pathNode.GetType() == sdv::toml::ENodeType::node_string)
|
||||
{
|
||||
udsRaw = static_cast<std::string>(pathNode.GetValue());
|
||||
}
|
||||
}
|
||||
|
||||
//for connect string
|
||||
if (udsRaw.empty())
|
||||
{
|
||||
const std::string s(cfgStr);
|
||||
const std::string key = "path=";
|
||||
auto pos = s.find(key);
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
auto end = s.find(';', pos + key.size());
|
||||
if (end == std::string::npos)
|
||||
udsRaw = s.substr(pos + key.size());
|
||||
else
|
||||
udsRaw = s.substr(pos + key.size(), end - pos - key.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (udsRaw.empty())
|
||||
{
|
||||
udsRaw = "%LOCALAPPDATA%/sdv/tunnel.sock";
|
||||
}
|
||||
|
||||
std::string udsPathBase = NormalizeUdsPathForWindows(udsRaw);
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] endpoint udsPath=", udsPathBase);
|
||||
|
||||
SOCKET listenSocket = CreateUnixListenSocket(udsPathBase);
|
||||
if (listenSocket == INVALID_SOCKET)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] Failed to create listen socket for endpoint: ", udsPathBase);
|
||||
return ep;
|
||||
}
|
||||
|
||||
auto serverTransport = std::make_shared<CWinsockConnection>( static_cast<unsigned long long>(listenSocket), true);
|
||||
auto serverTunnel = std::make_shared<CWinTunnelConnection>(serverTransport, /*channelId*/ static_cast<uint16_t>(0u));
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
m_udsServers[udsPathBase] = serverTunnel;
|
||||
m_udsServerClaimed.erase(udsPathBase);
|
||||
}
|
||||
|
||||
ep.pConnection = static_cast<sdv::IInterfaceAccess*>(serverTunnel.get());
|
||||
ep.ssConnectString = "proto=tunnel;role=server;path=" + udsPathBase + ";";
|
||||
|
||||
return ep;
|
||||
}
|
||||
|
||||
sdv::IInterfaceAccess* CSocketsTunnelChannelMgnt::Access(const sdv::u8string& cs)
|
||||
{
|
||||
if (StartUpWinSock() != 0)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] WinSock startup failed in Access()" );
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string connectStr = static_cast<std::string>(cs);
|
||||
std::string udsRaw;
|
||||
if (!ParseTunnelPath(connectStr, udsRaw))
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] Invalid tunnel connect string: ", connectStr);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (udsRaw.empty())
|
||||
{
|
||||
udsRaw = "%LOCALAPPDATA%/sdv/tunnel.sock";
|
||||
}
|
||||
|
||||
std::string udsPathBase = NormalizeUdsPathForWindows(udsRaw);
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Access udsPath=", udsPathBase);
|
||||
|
||||
const bool isServer =
|
||||
(connectStr.find("role=server") != std::string::npos);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
auto it = m_udsServers.find(udsPathBase);
|
||||
if (isServer && it != m_udsServers.end() && it->second != nullptr)
|
||||
{
|
||||
if (!m_udsServerClaimed.count(udsPathBase))
|
||||
{
|
||||
m_udsServerClaimed.insert(udsPathBase);
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Access -> RETURN SERVER for ", udsPathBase);
|
||||
return it->second.get(); // Ownership: managed by m_udsServers (do not delete)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CLIENT: create AF_UNIX client socket and wrap it in a tunnel
|
||||
SOCKET s = ConnectUnixSocket(udsPathBase, /*totalTimeoutMs*/ 5000, /*retryDelayMs*/ 50);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] Failed to connect client socket for ", udsPathBase);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Access -> CREATE CLIENT for ", udsPathBase);
|
||||
auto clientTransport = std::make_shared<CWinsockConnection>(s, /*acceptRequired*/ false);
|
||||
// Ownership: The returned pointer must be managed and deleted by the SDV framework via IObjectDestroy
|
||||
auto* tunnelClient = new CWinTunnelConnection(clientTransport, /*channelId*/ 0u);
|
||||
|
||||
return static_cast<sdv::IInterfaceAccess*>(tunnelClient);
|
||||
}
|
||||
#endif
|
||||
106
sdv_services/uds_win_tunnel/channel_mgnt.h
Normal file
106
sdv_services/uds_win_tunnel/channel_mgnt.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN_TUNNEL_CHANNEL_MGNT_H
|
||||
#define WIN_TUNNEL_CHANNEL_MGNT_H
|
||||
|
||||
#include <support/component_impl.h>
|
||||
#include <interfaces/ipc.h>
|
||||
#include "../sdv_services/uds_win_sockets/channel_mgnt.h"
|
||||
#include "../sdv_services/uds_win_sockets/connection.h"
|
||||
#include "connection.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
// Winsock headers are required for SOCKET / AF_UNIX / WSAStartup
|
||||
// NOTE: The actual initialization is done via StartUpWinSock()
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
class CWinTunnelConnection;
|
||||
|
||||
/**
|
||||
* @class CSocketsTunnelChannelMgnt
|
||||
* @brief IPC channel management class for Windows AF_UNIX tunnel communication.
|
||||
*
|
||||
* Similar to CSocketsChannelMgnt (proto=uds), but:
|
||||
* - uses CWinTunnelConnection (tunnel wrapper) on top of CWinsockConnection
|
||||
* - uses proto=tunnel in connect strings
|
||||
*
|
||||
* Provides creation and access to tunnel endpoints, manages server-side tunnel lifetimes,
|
||||
* and integrates with the SDV object/component framework.
|
||||
*/
|
||||
class CSocketsTunnelChannelMgnt :
|
||||
public sdv::CSdvObject,
|
||||
public sdv::ipc::ICreateEndpoint,
|
||||
public sdv::ipc::IChannelAccess
|
||||
{
|
||||
public:
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IChannelAccess)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::ICreateEndpoint)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::system_object)
|
||||
DECLARE_OBJECT_CLASS_NAME("WinTunnelChannelControl")
|
||||
DECLARE_OBJECT_CLASS_ALIAS("LocalChannelControl")
|
||||
DECLARE_DEFAULT_OBJECT_NAME("LocalChannelControl")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
virtual ~CSocketsTunnelChannelMgnt() = default;
|
||||
|
||||
/**
|
||||
* @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
|
||||
* @return Returns 'true' when the initialization was successful, 'false' when not.
|
||||
*/
|
||||
virtual bool OnInitialize() override;
|
||||
|
||||
/**
|
||||
* @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
|
||||
*/
|
||||
virtual void OnShutdown() override;
|
||||
|
||||
/**
|
||||
* @brief Creates a tunnel endpoint (server side) and returns endpoint info.
|
||||
* @param[in] cfgStr Optional config string (TOML or connect string).
|
||||
* @return The channel endpoint structure.
|
||||
*/
|
||||
sdv::ipc::SChannelEndpoint CreateEndpoint(const sdv::u8string& cfgStr) override;
|
||||
|
||||
/**
|
||||
* @brief Creates or accesses a connection object from the channel connect string.
|
||||
* @param[in] cs The channel connect string.
|
||||
* @return Pointer to connection access interface.
|
||||
*/
|
||||
sdv::IInterfaceAccess* Access(const sdv::u8string& cs) override;
|
||||
|
||||
/**
|
||||
* @brief Called by server tunnel when closing (bookkeeping).
|
||||
* @param[in] udsPath The UDS path for the server.
|
||||
* @param[in] ptr Pointer to the tunnel connection being closed.
|
||||
*/
|
||||
void OnServerClosed(const std::string& udsPath, CWinTunnelConnection* ptr);
|
||||
|
||||
private:
|
||||
std::mutex m_udsMtx;
|
||||
std::map<std::string, std::shared_ptr<CWinTunnelConnection>> m_udsServers;
|
||||
std::set<std::string> m_udsServerClaimed;
|
||||
};
|
||||
|
||||
DEFINE_SDV_OBJECT(CSocketsTunnelChannelMgnt)
|
||||
|
||||
#endif // ! defined WIN_TUNNEL_CHANNEL_MGNT_H
|
||||
#endif
|
||||
226
sdv_services/uds_win_tunnel/connection.cpp
Normal file
226
sdv_services/uds_win_tunnel/connection.cpp
Normal file
@@ -0,0 +1,226 @@
|
||||
/************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
************************************************************/
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "connection.h"
|
||||
|
||||
|
||||
CWinTunnelConnection::CWinTunnelConnection(
|
||||
std::shared_ptr<CWinsockConnection> transport,
|
||||
uint16_t channelId)
|
||||
: m_Transport(std::move(transport))
|
||||
, m_ChannelId(channelId)
|
||||
{
|
||||
// No additional initialization required; acts as a thin wrapper.
|
||||
}
|
||||
|
||||
bool CWinTunnelConnection::SendData(
|
||||
/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] SendData failed: transport is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build tunnel header buffer
|
||||
sdv::pointer<uint8_t> hdrBuf;
|
||||
hdrBuf.resize(sizeof(STunnelHeader));
|
||||
|
||||
STunnelHeader hdr{};
|
||||
hdr.uiChannelId = m_ChannelId; // Logical channel for this connection
|
||||
hdr.uiFlags = 0; // Reserved for future use
|
||||
|
||||
std::memcpy(hdrBuf.get(), &hdr, sizeof(STunnelHeader));
|
||||
|
||||
// Compose new sequence: [header] + original payload chunks
|
||||
sdv::sequence<sdv::pointer<uint8_t>> seqWithHdr;
|
||||
seqWithHdr.push_back(hdrBuf);
|
||||
for (auto& chunk : seqData)
|
||||
{
|
||||
seqWithHdr.push_back(chunk);
|
||||
}
|
||||
|
||||
bool result = m_Transport->SendData(seqWithHdr);
|
||||
if (!result) {
|
||||
SDV_LOG_ERROR("[WinTunnel] SendData failed in underlying transport");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CWinTunnelConnection::AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] AsyncConnect failed: transport is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store upper-layer callbacks (safe for null)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
sdv::TInterfaceAccessPtr acc(pReceiver);
|
||||
m_pUpperReceiver = acc.GetInterface<sdv::ipc::IDataReceiveCallback>();
|
||||
m_pUpperEvent = acc.GetInterface<sdv::ipc::IConnectEventCallback>();
|
||||
}
|
||||
|
||||
// Register this tunnel as the data/event receiver in the AF_UNIX transport.
|
||||
bool result = m_Transport->AsyncConnect(this);
|
||||
if (!result) {
|
||||
SDV_LOG_ERROR("[WinTunnel] AsyncConnect failed in underlying transport");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CWinTunnelConnection::WaitForConnection(/*in*/ uint32_t uiWaitMs)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] WaitForConnection failed: transport is null");
|
||||
return false;
|
||||
}
|
||||
return m_Transport->WaitForConnection(uiWaitMs);
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::CancelWait()
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] CancelWait failed: transport is null");
|
||||
return;
|
||||
}
|
||||
m_Transport->CancelWait();
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::Disconnect()
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] Disconnect failed: transport is null");
|
||||
return;
|
||||
}
|
||||
|
||||
m_Transport->Disconnect();
|
||||
|
||||
// Clear upper-layer callbacks (thread-safe)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
m_pUpperReceiver = nullptr;
|
||||
m_pUpperEvent = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t CWinTunnelConnection::RegisterStateEventCallback(
|
||||
/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] RegisterStateEventCallback failed: transport is null");
|
||||
return 0ULL;
|
||||
}
|
||||
|
||||
// Forward directly to underlying CWinsockConnection
|
||||
return m_Transport->RegisterStateEventCallback(pEventCallback);
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::UnregisterStateEventCallback(
|
||||
/*in*/ uint64_t uiCookie)
|
||||
{
|
||||
if (!m_Transport || uiCookie == 0ULL)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] UnregisterStateEventCallback failed: transport is null or cookie is 0");
|
||||
return;
|
||||
}
|
||||
|
||||
m_Transport->UnregisterStateEventCallback(uiCookie);
|
||||
}
|
||||
|
||||
sdv::ipc::EConnectState CWinTunnelConnection::GetConnectState() const
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return sdv::ipc::EConnectState::uninitialized;
|
||||
}
|
||||
return m_Transport->GetConnectState();
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::DestroyObject()
|
||||
{
|
||||
Disconnect();
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
m_Transport.reset();
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::ReceiveData(
|
||||
/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
// Expect at least one chunk (the tunnel header)
|
||||
if (seqData.empty())
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] ReceiveData: empty sequence");
|
||||
return; // nothing to do
|
||||
}
|
||||
|
||||
const auto& hdrChunk = seqData[0];
|
||||
if (hdrChunk.size() < sizeof(STunnelHeader))
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] ReceiveData: invalid tunnel header size");
|
||||
// Invalid tunnel frame; drop it for now (could set communication_error)
|
||||
return;
|
||||
}
|
||||
|
||||
STunnelHeader hdr{};
|
||||
std::memcpy(&hdr, hdrChunk.get(), sizeof(STunnelHeader));
|
||||
|
||||
|
||||
// TODO: use channelId for multiplexing later
|
||||
|
||||
// Build payload-only sequence: drop header chunk, keep others
|
||||
sdv::sequence<sdv::pointer<uint8_t>> payload;
|
||||
for (size_t i = 1; i < seqData.size(); ++i)
|
||||
{
|
||||
payload.push_back(seqData[i]);
|
||||
}
|
||||
|
||||
if (m_pUpperReceiver)
|
||||
{
|
||||
try {
|
||||
m_pUpperReceiver->ReceiveData(payload); // header stripped
|
||||
} catch (...) {
|
||||
SDV_LOG_ERROR("[WinTunnel] Exception in upper receiver's ReceiveData");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::SetConnectState(sdv::ipc::EConnectState state)
|
||||
{
|
||||
sdv::ipc::IConnectEventCallback* upper = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
upper = m_pUpperEvent;
|
||||
}
|
||||
|
||||
if (upper)
|
||||
{
|
||||
try
|
||||
{
|
||||
upper->SetConnectState(state);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] Exception in upper event callback's SetConnectState");
|
||||
// Never let user callback crash the transport.
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
168
sdv_services/uds_win_tunnel/connection.h
Normal file
168
sdv_services/uds_win_tunnel/connection.h
Normal file
@@ -0,0 +1,168 @@
|
||||
/************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
************************************************************/
|
||||
#ifdef _WIN32
|
||||
#ifndef UDS_WIN_TUNNEL_CONNECTION_H
|
||||
#define UDS_WIN_TUNNEL_CONNECTION_H
|
||||
|
||||
#include <interfaces/ipc.h>
|
||||
#include <support/component_impl.h>
|
||||
#include <support/interface_ptr.h>
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "../sdv_services/uds_win_sockets/connection.h" // existing AF_UNIX transport: CWinsockConnection
|
||||
|
||||
|
||||
/**
|
||||
* @brief Logical tunnel connection on top of a shared Windows AF_UNIX transport.
|
||||
*
|
||||
* This class does NOT create sockets by itself. It wraps an existing
|
||||
* CWinsockConnection (Winsock AF_UNIX) and adds:
|
||||
* - tunnel header (channelId, flags)
|
||||
* - (later) demultiplexing of incoming payloads per logical channel.
|
||||
*/
|
||||
class CWinTunnelConnection :
|
||||
public sdv::IInterfaceAccess,
|
||||
public sdv::IObjectDestroy,
|
||||
public sdv::ipc::IDataSend,
|
||||
public sdv::ipc::IConnect,
|
||||
public sdv::ipc::IDataReceiveCallback,
|
||||
public sdv::ipc::IConnectEventCallback
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* @struct STunnelHeader
|
||||
* @brief Header prepended to each tunneled SDV message for logical channel identification and flags.
|
||||
*/
|
||||
struct STunnelHeader
|
||||
{
|
||||
uint16_t uiChannelId; ///< Logical channel ID (IPC_x / REMOTE_IPC_x)
|
||||
uint16_t uiFlags; ///< Reserved for future use (QoS, direction, etc.)
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Construct a tunnel wrapper over an existing AF_UNIX transport.
|
||||
* @param[in] transport Shared pointer to the underlying AF_UNIX transport.
|
||||
* @param[in] channelId Logical channel ID for this tunnel instance.
|
||||
*/
|
||||
explicit CWinTunnelConnection(
|
||||
std::shared_ptr<CWinsockConnection> transport,
|
||||
uint16_t channelId);
|
||||
|
||||
/**
|
||||
* @brief Destructor.
|
||||
*/
|
||||
virtual ~CWinTunnelConnection() = default;
|
||||
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataSend)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IConnect)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataReceiveCallback)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IConnectEventCallback)
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectDestroy)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
// ---------- IDataSend ----------
|
||||
/**
|
||||
* @brief Send a sequence of buffers via the tunnel.
|
||||
* @param[in,out] seqData Sequence of message buffers (may be modified by callee).
|
||||
* @return true on successful send, false otherwise.
|
||||
*/
|
||||
bool SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData) override;
|
||||
|
||||
// ---------- IConnect ----------
|
||||
/**
|
||||
* @brief Start asynchronous connect and register this object as receiver.
|
||||
* @param[in] pReceiver Pointer to callback interface for data and state notifications.
|
||||
* @return true if connect started, false otherwise.
|
||||
*/
|
||||
bool AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver) override;
|
||||
|
||||
/**
|
||||
* @brief Wait until the underlying transport becomes 'connected'.
|
||||
* @param[in] uiWaitMs Timeout in milliseconds to wait.
|
||||
* @return true if connection established, false on timeout or error.
|
||||
*/
|
||||
bool WaitForConnection(/*in*/ uint32_t uiWaitMs) override;
|
||||
|
||||
/**
|
||||
* @brief Cancel any pending connect or wait operation.
|
||||
*/
|
||||
void CancelWait() override;
|
||||
|
||||
/**
|
||||
* @brief Disconnect the tunnel and underlying transport.
|
||||
*/
|
||||
void Disconnect() override;
|
||||
|
||||
/**
|
||||
* @brief Register a state event callback (forwards to transport).
|
||||
* @param[in] pEventCallback Pointer to event callback interface.
|
||||
* @return Registration cookie (nonzero) or 0 on failure.
|
||||
*/
|
||||
uint64_t RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
|
||||
/**
|
||||
* @brief Unregister a previously registered state event callback.
|
||||
* @param[in] uiCookie Registration cookie returned by RegisterStateEventCallback.
|
||||
*/
|
||||
void UnregisterStateEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
/**
|
||||
* @brief Get the current state from the underlying transport.
|
||||
* @return The current connection state.
|
||||
*/
|
||||
sdv::ipc::EConnectState GetConnectState() const override;
|
||||
|
||||
// ---------- IObjectDestroy ----------
|
||||
/**
|
||||
* @brief Release and clean up all resources associated with this object.
|
||||
*/
|
||||
void DestroyObject() override;
|
||||
|
||||
// ---------- IDataReceiveCallback ----------
|
||||
/**
|
||||
* @brief Receive data from the underlying AF_UNIX transport.
|
||||
* @param[in,out] seqData Sequence of received message buffers (header chunk is removed by this call).
|
||||
*/
|
||||
void ReceiveData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData) override;
|
||||
|
||||
// ---------- IConnectEventCallback ----------
|
||||
/**
|
||||
* @brief Forward state changes from the underlying transport to the upper layer.
|
||||
* @param[in] state New connection state.
|
||||
*/
|
||||
void SetConnectState(sdv::ipc::EConnectState state) override;
|
||||
|
||||
// Helpers
|
||||
void SetChannelId(uint16_t channelId) { m_ChannelId = channelId; }
|
||||
uint16_t GetChannelId() const noexcept { return m_ChannelId; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<CWinsockConnection> m_Transport; ///< shared physical tunnel port
|
||||
uint16_t m_ChannelId{0}; ///< default logical channel id
|
||||
|
||||
// Upper layer callbacks (original VAPI receiver)
|
||||
sdv::ipc::IDataReceiveCallback* m_pUpperReceiver{nullptr};
|
||||
sdv::ipc::IConnectEventCallback* m_pUpperEvent{nullptr};
|
||||
mutable std::mutex m_CallbackMtx;
|
||||
};
|
||||
|
||||
#endif // UDS_WIN_TUNNEL_CONNECTION_H
|
||||
#endif
|
||||
Reference in New Issue
Block a user