Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,23 @@ only uses the game server functions.
heartbeats, ticket authentication (`SendUserConnectAndAuthenticate_DEPRECATED`),
bots and shared-socket master server packets. The interface version list matches
`SteamGameServer_InitEx`.
* FakeIP: fake UDP ports (`CreateFakeUDPPort`, SteamNetworkingSockets013) on the client or
the game server session, the game server's FakeIP request (`BeginAsyncRequestFakeIP`,
`GetFakeIP`), FakeIP address checks and SteamNetworkingSockets diagnostics
(SteamNetworkingUtils004: the client's, or the game server's without a client). Ports
are bridge handles; a session's shutdown destroys the ports it still holds. Received
datagrams are copied out and released inside the bridge; one that does not fit, or does
not come from an IP address, is dropped and described.
* Events: ClientGameServerDeny, GameServerChangeRequested and GameOverlayActivated
reach the client callback during `SteamBridge_HostRunCallbacks`; SteamServersConnected,
SteamServerConnectFailure, GSPolicyResponse and GSClientApprove/Deny/Kick reach the
server callback during `SteamBridge_ServerRunCallbacks`. Fields are copied out of
SteamServerConnectFailure, GSPolicyResponse, GSClientApprove/Deny/Kick and
SteamNetworkingFakeIPResult reach the server callback during
`SteamBridge_ServerRunCallbacks`. Fields are copied out of
Steam's payloads; IP addresses and ports pass through unchanged.

Host functions are not thread-safe; call them from the host's main thread. A callback
Host functions are not thread-safe; call them from the host's main thread. FakeIP port
send, receive and cleanup may run on any thread while the port exists, and the networking
diagnostics callback may run on any thread. A callback
may call `SteamBridge_HostShutdown` or `SteamBridge_ServerShutdown`; the shutdown runs
after the native dispatcher returns. `SteamBridge_HostShutdown` shuts down an active
game server first. Plugin contexts may use the runtime that host mode loaded; destroy
Expand Down Expand Up @@ -123,7 +133,9 @@ CTest covers, on every supported platform, absent runtimes and host mode against
fixture runtime: client initialization failure and retry, connection tickets, voice
capture/decoding and non-OK voice results, client and game server event translation
and dispatcher separation, deferred shutdown from callbacks, game server setters and
authentication, packets, and dedicated (server-only) sessions. On Windows it also
authentication, packets, dedicated (server-only) sessions, and FakeIP ports: datagram
send results, delivery and dropping, cleanup, session-owned port destruction, the game
server's FakeIP request and result event, and diagnostics forwarding. On Windows it also
covers plugin mode: absent/uninitialized Steam, legacy (x86) and intermediate-modern
exports, exact-version availability and retry, SteamID/screenshots, cookie/certificate
forwarding, stream events, context isolation, cancellation and reentrant destruction.
Expand All @@ -134,8 +146,9 @@ accepts real Steam DLL and consumer DLL paths for loader-only checks.
`SteamBridgeHostSmoke <runtime> [seconds]` needs a running, logged-in Steam client and
`steam_appid.txt` in the working directory: it initializes the client, logs a game
server on anonymously, authenticates the client's ticket and exits zero on
GSClientApprove. Neither validates screenshot submission, real HTTP traffic, engine
hooks or gameplay; those require running each target game.
GSClientApprove. Neither validates screenshot submission, real HTTP traffic, FakeIP
traffic over the Steam relay, engine hooks or gameplay; those require running each target
game (FakeIP traffic also needs a second Steam account).

The SDK notice is installed with the runtime. Historical ABI declarations derive
from Valve Steamworks headers; see their provenance in `src/LegacyInterfaces.h`.
57 changes: 55 additions & 2 deletions include/SteamAPIBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ extern "C"
SB_SERVER_POLICY = 3,
SB_SERVER_CLIENT_APPROVE = 4,
SB_SERVER_CLIENT_DENY = 5,
SB_SERVER_CLIENT_KICK = 6
SB_SERVER_CLIENT_KICK = 6,
SB_SERVER_FAKE_IP_RESULT = 7
};
// Values of Steamworks EServerMode.
enum
Expand All @@ -143,7 +144,7 @@ extern "C"
char server[SB_CLIENT_SERVER_BYTES];
char password[SB_CLIENT_SERVER_BYTES];
} SteamBridgeClientEvent;
// result is the Steam EResult of a logon failure; reason is the EDenyReason of a deny or kick.
// result is the Steam EResult of a logon failure or a FakeIP request; reason is the EDenyReason of a deny or kick.
typedef struct SteamBridgeServerEvent
{
uint32_t kind;
Expand Down Expand Up @@ -225,6 +226,58 @@ extern "C"
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_ServerHandleIncomingPacket(const void* data, uint32_t bytes, uint32_t ip, uint16_t port);
// Returns the packet length, or zero when no packet is pending.
STEAMBRIDGE_API uint32_t STEAMBRIDGE_CALL SteamBridge_ServerGetNextOutgoingPacket(void* data, uint32_t capacity, uint32_t* ip, uint16_t* port);
// Asks Steam for a FakeIP with ports fake ports; SB_SERVER_FAKE_IP_RESULT reports the outcome.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_ServerBeginAsyncRequestFakeIP(int32_t ports);
// *result receives the Steam EResult of the request (k_EResultBusy while it is pending). On
// k_EResultOK, *ip and up to count ports from firstPort are written; otherwise they are cleared.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_ServerGetFakeIP(int32_t firstPort, int32_t* result, uint32_t* ip, uint16_t* ports, uint32_t count);

// FakeIP datagrams (ISteamNetworkingFakeUDPPort), host mode. IPv4 addresses and ports are in
// host byte order, as in Steam's SteamNetworkingIPAddr. A client port belongs to the client
// session and a server port to the game server session; shutting a session down destroys its
// remaining ports and invalidates their handles. Create and destroy ports on the host's main
// thread; Send, Receive and ScheduleCleanup may run on any thread while the port exists.
typedef struct SteamBridgeFakeUDPPort* SteamBridgeFakeUDPPortHandle;
enum
{
SB_FAKE_UDP_NONE = 0,
SB_FAKE_UDP_MESSAGE = 1,
SB_FAKE_UDP_TOO_LARGE = 2,
SB_FAKE_UDP_NOT_IP = 3
};
enum
{
SB_FAKE_UDP_PEER_BYTES = 128
};
// kind is an SB_FAKE_UDP_* value. bytes is the message size, also when it did not fit. ip
// and port are the sender of a received message. peer is the sender as Steam prints it, for
// a message that was dropped (SB_FAKE_UDP_TOO_LARGE or SB_FAKE_UDP_NOT_IP).
typedef struct SteamBridgeFakeUDPMessage
{
uint32_t kind;
uint32_t bytes;
uint32_t ip;
uint16_t port;
char peer[SB_FAKE_UDP_PEER_BYTES];
} SteamBridgeFakeUDPMessage;
typedef void(STEAMBRIDGE_CALL* SteamBridgeNetworkingDebugCallback)(int32_t level, const char* message);

// gameServer selects the game server session. fakeServerPort is the index of a fake port the
// game server requested, or -1 for a port that sends to FakeIPs from a local FakeIP.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_FakeUDPPortCreate(uint32_t gameServer, int32_t fakeServerPort, SteamBridgeFakeUDPPortHandle* port);
STEAMBRIDGE_API void STEAMBRIDGE_CALL SteamBridge_FakeUDPPortDestroy(SteamBridgeFakeUDPPortHandle port);
// flags are Steam k_nSteamNetworkingSend_* flags; *result receives the Steam EResult, and
// anything but k_EResultOK returns SB_FAILED.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_FakeUDPPortSend(SteamBridgeFakeUDPPortHandle port, uint32_t ip, uint16_t remotePort, const void* data, uint32_t bytes, int32_t flags, int32_t* result);
// Takes at most one pending message. A message from an IP address that fits capacity is
// copied into data; any other message is dropped and only described.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_FakeUDPPortReceive(SteamBridgeFakeUDPPortHandle port, void* data, uint32_t capacity, SteamBridgeFakeUDPMessage* message);
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_FakeUDPPortScheduleCleanup(SteamBridgeFakeUDPPortHandle port, uint32_t ip, uint16_t remotePort);
// Nonzero when ip is a global or local FakeIP.
STEAMBRIDGE_API uint32_t STEAMBRIDGE_CALL SteamBridge_IsFakeIPv4(uint32_t ip);
// Forwards SteamNetworkingSockets diagnostics up to level (ESteamNetworkingSocketsDebugOutputType
// values). The callback may run on any thread; NULL stops forwarding.
STEAMBRIDGE_API SteamBridgeStatus STEAMBRIDGE_CALL SteamBridge_NetworkingSetDebugOutput(int32_t level, SteamBridgeNetworkingDebugCallback callback);

#ifdef __cplusplus
}
Expand Down
174 changes: 172 additions & 2 deletions src/Host.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#include "Platform.h"
#include <steam/steam_api.h>
#include <steam/steam_gameserver.h>
#include <steam/steamnetworkingfakeip.h>
#include <SteamAPIBridge.h>
#include <algorithm>
#include <atomic>
#include <cstring>
#include <memory>
#include <vector>

// The object behind a public FakeIP port handle.
struct SteamBridgeFakeUDPPort
{
ISteamNetworkingFakeUDPPort* port;
bool gameServer;
};

namespace
{
using platform::Resolve;
Expand Down Expand Up @@ -46,6 +56,12 @@ struct Host
// Shutdown requested by a host callback runs after the native dispatcher returns.
unsigned dispatchDepth{};
bool pendingServerShutdown{}, pendingHostShutdown{};
// FakeIP networking. utils is the client's, or the game server's when no client is initialized.
ISteamNetworkingSockets* sockets{};
ISteamNetworkingSockets* serverSockets{};
ISteamNetworkingUtils* utils{};
bool utilsFromServer{};
std::vector<std::unique_ptr<SteamBridgeFakeUDPPort>> ports;
} host;

// Copies a truncated, NUL-terminated string; capacity must be nonzero.
Expand Down Expand Up @@ -123,6 +139,10 @@ struct HostCallback final : CCallbackBase
event.ownerSteamID = p->m_OwnerSteamID.ConvertToUint64();
break;
}
case SteamNetworkingFakeIPResult_t::k_iCallback:
event.kind = SB_SERVER_FAKE_IP_RESULT;
event.result = static_cast<SteamNetworkingFakeIPResult_t*>(payload)->m_eResult;
break;
case GSClientDeny_t::k_iCallback:
{
auto p = static_cast<GSClientDeny_t*>(payload);
Expand Down Expand Up @@ -169,17 +189,34 @@ void Unregister(std::vector<std::unique_ptr<HostCallback>>& list)
host.unregisterCallback(callback.get());
list.clear();
}
// Destroys the FakeIP ports a session still holds before the session shuts down.
void DestroyPorts(bool gameServer)
{
std::erase_if(host.ports, [&](const std::unique_ptr<SteamBridgeFakeUDPPort>& port) {
if (port->gameServer != gameServer) return false;
port->port->DestroyFakeUDPPort();
return true;
});
}
void ShutdownServer()
{
if (!host.server) return;
DestroyPorts(true);
Unregister(host.serverCallbacks);
host.server = nullptr;
host.server = nullptr;
host.serverSockets = nullptr;
if (host.utilsFromServer)
{
host.utils = nullptr;
host.utilsFromServer = false;
}
host.serverShutdown();
}
void ShutdownHost()
{
if (!host.module) return;
ShutdownServer();
DestroyPorts(false);
Unregister(host.clientCallbacks);
if (host.client) host.shutdown();
platform::UnloadRuntime(host.module);
Expand Down Expand Up @@ -213,6 +250,26 @@ template <class F> SteamBridgeStatus Server(F&& fn)
fn(*host.server);
return SB_OK;
}
SteamNetworkingIPAddr IPv4(uint32_t ip, uint16_t port)
{
SteamNetworkingIPAddr address;
address.SetIPv4(ip, port);
return address;
}
// Steam calls the debug output function without user data, so the consumer's callback is global.
std::atomic<SteamBridgeNetworkingDebugCallback> networkingDebugOutput{};
void NetworkingDebugOutput(ESteamNetworkingSocketsDebugOutputType level, const char* message)
{
auto callback = networkingDebugOutput.load();
if (!callback || !message) return;
// Exceptions must not cross the C callback boundary.
try
{
callback(int32_t(level), message);
}
catch (...)
{}
}
} // namespace

SteamBridgeStatus SteamBridge_HostInit(const char* runtimePath, uint32_t initializeClient)
Expand Down Expand Up @@ -254,6 +311,9 @@ SteamBridgeStatus SteamBridge_HostInit(const char* runtimePath, uint32_t initial
h.user = static_cast<ISteamUser*>(h.findUser(steamUser, STEAMUSER_INTERFACE_VERSION));
h.friends = static_cast<ISteamFriends*>(h.findUser(steamUser, STEAMFRIENDS_INTERFACE_VERSION));
h.apps = static_cast<ISteamApps*>(h.findUser(steamUser, STEAMAPPS_INTERFACE_VERSION));
h.sockets = static_cast<ISteamNetworkingSockets*>(h.findUser(steamUser, STEAMNETWORKINGSOCKETS_INTERFACE_VERSION));
// The networking utilities are global, not per user (SteamNetworkingUtils_SteamAPI).
h.utils = static_cast<ISteamNetworkingUtils*>(h.findUser(0, STEAMNETWORKINGUTILS_INTERFACE_VERSION));
}
h.clientFn = host.clientFn;
h.clientUser = host.clientUser;
Expand Down Expand Up @@ -416,14 +476,21 @@ SteamBridgeStatus SteamBridge_ServerInit(uint32_t serverIP, uint16_t gamePort, u
Register<GSClientApprove_t>(host.serverCallbacks, true);
Register<GSClientDeny_t>(host.serverCallbacks, true);
Register<GSClientKick_t>(host.serverCallbacks, true);
Register<SteamNetworkingFakeIPResult_t>(host.serverCallbacks, true);
}
catch (...)
{
Unregister(host.serverCallbacks);
host.serverShutdown();
return SB_FAILED;
}
host.server = server;
host.server = server;
host.serverSockets = static_cast<ISteamNetworkingSockets*>(host.findServer(host.serverGetUser(), STEAMNETWORKINGSOCKETS_INTERFACE_VERSION));
if (!host.utils)
{
host.utils = static_cast<ISteamNetworkingUtils*>(host.findServer(0, STEAMNETWORKINGUTILS_INTERFACE_VERSION));
host.utilsFromServer = host.utils != nullptr;
}
return SB_OK;
}
void SteamBridge_ServerShutdown()
Expand Down Expand Up @@ -554,3 +621,106 @@ uint32_t SteamBridge_ServerGetNextOutgoingPacket(void* data, uint32_t capacity,
int bytes = host.server->GetNextOutgoingPacket(data, int(capacity), ip, port);
return bytes > 0 && uint32_t(bytes) <= capacity ? uint32_t(bytes) : 0;
}
SteamBridgeStatus SteamBridge_ServerBeginAsyncRequestFakeIP(int32_t ports)
{
if (ports <= 0) return SB_INVALID_ARGUMENT;
if (!host.serverSockets) return SB_UNAVAILABLE;
return Result(host.serverSockets->BeginAsyncRequestFakeIP(ports));
}
SteamBridgeStatus SteamBridge_ServerGetFakeIP(int32_t firstPort, int32_t* result, uint32_t* ip, uint16_t* ports, uint32_t count)
{
if (!result || !ip || (count && !ports)) return SB_INVALID_ARGUMENT;
*result = 0;
*ip = 0;
std::fill_n(ports, count, uint16_t(0));
if (firstPort < 0) return SB_INVALID_ARGUMENT;
if (!host.serverSockets) return SB_UNAVAILABLE;
SteamNetworkingFakeIPResult_t info{};
host.serverSockets->GetFakeIP(firstPort, &info);
*result = info.m_eResult;
if (info.m_eResult != k_EResultOK) return SB_OK;
*ip = info.m_unIP;
std::copy_n(info.m_unPorts, std::min<uint32_t>(count, SteamNetworkingFakeIPResult_t::k_nMaxReturnPorts), ports);
return SB_OK;
}

SteamBridgeStatus SteamBridge_FakeUDPPortCreate(uint32_t gameServer, int32_t fakeServerPort, SteamBridgeFakeUDPPortHandle* port)
{
if (!port) return SB_INVALID_ARGUMENT;
*port = nullptr;
if (fakeServerPort < -1) return SB_INVALID_ARGUMENT;
ISteamNetworkingSockets* sockets = gameServer ? host.serverSockets : host.sockets;
if (!sockets) return SB_UNAVAILABLE;
ISteamNetworkingFakeUDPPort* native = sockets->CreateFakeUDPPort(fakeServerPort);
if (!native) return SB_FAILED;
try
{
host.ports.push_back(std::make_unique<SteamBridgeFakeUDPPort>(SteamBridgeFakeUDPPort{native, gameServer != 0}));
}
catch (...)
{
native->DestroyFakeUDPPort();
return SB_FAILED;
}
*port = host.ports.back().get();
return SB_OK;
}
void SteamBridge_FakeUDPPortDestroy(SteamBridgeFakeUDPPortHandle port)
{
auto found = std::find_if(host.ports.begin(), host.ports.end(), [&](const std::unique_ptr<SteamBridgeFakeUDPPort>& owned) { return owned.get() == port; });
if (found == host.ports.end()) return;
(*found)->port->DestroyFakeUDPPort();
host.ports.erase(found);
}
SteamBridgeStatus SteamBridge_FakeUDPPortSend(SteamBridgeFakeUDPPortHandle port, uint32_t ip, uint16_t remotePort, const void* data, uint32_t bytes, int32_t flags, int32_t* result)
{
if (!result) return SB_INVALID_ARGUMENT;
*result = 0;
if (!port || !data || !bytes) return SB_INVALID_ARGUMENT;
*result = port->port->SendMessageToFakeIP(IPv4(ip, remotePort), data, bytes, flags);
return *result == k_EResultOK ? SB_OK : SB_FAILED;
}
SteamBridgeStatus SteamBridge_FakeUDPPortReceive(SteamBridgeFakeUDPPortHandle port, void* data, uint32_t capacity, SteamBridgeFakeUDPMessage* message)
{
if (!message) return SB_INVALID_ARGUMENT;
*message = {};
if (!port || (capacity && !data)) return SB_INVALID_ARGUMENT;
SteamNetworkingMessage_t* received{};
if (port->port->ReceiveMessages(&received, 1) != 1 || !received) return SB_OK;
message->bytes = uint32_t(received->m_cbSize);
const SteamNetworkingIPAddr* address = received->m_identityPeer.GetIPAddr();
if (!address)
{
message->kind = SB_FAKE_UDP_NOT_IP;
if (host.utils) host.utils->SteamNetworkingIdentity_ToString(received->m_identityPeer, message->peer, sizeof(message->peer));
}
else if (message->bytes > capacity)
{
message->kind = SB_FAKE_UDP_TOO_LARGE;
if (host.utils) host.utils->SteamNetworkingIPAddr_ToString(*address, message->peer, sizeof(message->peer), true);
}
else
{
message->kind = SB_FAKE_UDP_MESSAGE;
message->ip = address->GetIPv4();
message->port = address->m_port;
std::memcpy(data, received->m_pData, message->bytes);
}
received->Release();
return SB_OK;
}
SteamBridgeStatus SteamBridge_FakeUDPPortScheduleCleanup(SteamBridgeFakeUDPPortHandle port, uint32_t ip, uint16_t remotePort)
{
if (!port) return SB_INVALID_ARGUMENT;
port->port->ScheduleCleanup(IPv4(ip, remotePort));
return SB_OK;
}
uint32_t SteamBridge_IsFakeIPv4(uint32_t ip) { return host.utils && host.utils->GetIPv4FakeIPType(ip) > k_ESteamNetworkingFakeIPType_NotFake; }
SteamBridgeStatus SteamBridge_NetworkingSetDebugOutput(int32_t level, SteamBridgeNetworkingDebugCallback callback)
{
if (level < k_ESteamNetworkingSocketsDebugOutputType_None || level > k_ESteamNetworkingSocketsDebugOutputType_Everything) return SB_INVALID_ARGUMENT;
if (!host.utils) return SB_UNAVAILABLE;
networkingDebugOutput = callback;
host.utils->SetDebugOutputFunction(callback ? ESteamNetworkingSocketsDebugOutputType(level) : k_ESteamNetworkingSocketsDebugOutputType_None, callback ? NetworkingDebugOutput : nullptr);
return SB_OK;
}
Loading
Loading