From 8090b8da9ce1397ba47d7e2d91b4f1716f708f25 Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 13 Nov 2023 02:16:42 +0000 Subject: [PATCH] server version check --- gameserver.c | 80 ++++++++++++++++++++++++++++++++++++++++++------ gameserver.h | 2 ++ network.c | 46 +++++++++++++++++++++++----- network.h | 3 ++ network_common.h | 1 + network_msg.h | 13 ++++++-- 6 files changed, 126 insertions(+), 19 deletions(-) diff --git a/gameserver.c b/gameserver.c index 3ff58f1..9116a34 100644 --- a/gameserver.c +++ b/gameserver.c @@ -54,6 +54,18 @@ static void gameserver_send_to_all( int ignore, } } +static void gameserver_send_version_to_client( int index ){ + struct gameserver_client *client = &gameserver.clients[index]; + + netmsg_version version; + version.inetmsg_id = k_inetmsg_version; + version.version = NETWORK_SKATERIFT_VERSION; + SteamAPI_ISteamNetworkingSockets_SendMessageToConnection( + hSteamNetworkingSockets, client->connection, + &version, sizeof(netmsg_version), + k_nSteamNetworkingSend_Reliable, NULL ); +} + /* * handle server update that client #'index' has joined */ @@ -68,8 +80,10 @@ static void gameserver_player_join( int index ){ k_nSteamNetworkingSend_Reliable ); /* - * update the joining user about current connections + * update the joining user about current connections and our version */ + gameserver_send_version_to_client( index ); + netmsg_playerusername *username = alloca( sizeof(netmsg_playerusername) + NETWORK_USERNAME_MAX ); username->inetmsg_id = k_inetmsg_playerusername; @@ -189,11 +203,6 @@ static void handle_new_connection( HSteamNetConnection conn ){ SteamAPI_ISteamNetworkingSockets_SetConnectionUserData( hSteamNetworkingSockets, conn, index ); - - if( gameserver.auth_mode != eServerModeAuthentication ){ - client->steamid = k_steamid_max; - gameserver_player_join( index ); - } } else{ vg_warn( "Error accepting connection (id: %u)\n", conn ); @@ -263,14 +272,52 @@ static void on_connect_status( CallbackMsg_t *msg ){ } } +static void gameserver_rx_version( SteamNetworkingMessage_t *msg ){ + netmsg_version *version = msg->m_pData; + + int client_id = gameserver_conid( msg->m_conn ); + if( client_id == -1 ) { + vg_warn( "Recieved version from unkown connection (%u)\n", msg->m_conn ); + SteamAPI_ISteamNetworkingSockets_CloseConnection( + hSteamNetworkingSockets, msg->m_conn, + k_ESteamNetConnectionEnd_Misc_InternalError, + NULL, 1 ); + return; + } + + struct gameserver_client *client = &gameserver.clients[ client_id ]; + + if( client->version ){ + vg_warn( "Already have version for this client (%d conn: %u)", + client_id, msg->m_conn ); + return; + } + + client->version = version->version; + + if( client->version != NETWORK_SKATERIFT_VERSION ){ + gameserver_send_version_to_client( client_id ); + remove_client( client_id ); + return; + } + + /* this is the sign on point for non-auth servers, + * for auth servers it comes at the end of rx_auth + */ + if( gameserver.auth_mode != eServerModeAuthentication ){ + client->steamid = k_steamid_max; + gameserver_player_join( client_id ); + } +} + /* * recieve auth ticket from connection. will only accept it if we've added them * to the client list first. */ static void gameserver_rx_auth( SteamNetworkingMessage_t *msg ){ if( gameserver.auth_mode != eServerModeAuthentication ){ - vg_error( "Running server without authentication. " - "Connection %u tried to authenticate.\n", msg->m_conn ); + vg_warn( "Running server without authentication. " + "Connection %u tried to authenticate.\n", msg->m_conn ); return; } @@ -278,21 +325,30 @@ static void gameserver_rx_auth( SteamNetworkingMessage_t *msg ){ if( client_id == -1 ) { vg_warn( "Recieved auth ticket from unkown connection (%u)\n", msg->m_conn ); + SteamAPI_ISteamNetworkingSockets_CloseConnection( + hSteamNetworkingSockets, msg->m_conn, + k_ESteamNetConnectionEnd_Misc_InternalError, NULL, 1 ); return; } struct gameserver_client *client = &gameserver.clients[ client_id ]; - if( client->steamid ){ vg_warn( "Already authorized this user but another app ticket was sent" " again (%d conn: %u)\n", client_id, msg->m_conn ); return; } + if( client->version == 0 ){ + vg_error( "Client has not sent their version yet (%u)\n", msg->m_conn ); + remove_client( client_id ); + return; + } + vg_low( "Attempting to verify user\n" ); if( msg->m_cbSize < sizeof(netmsg_auth) ){ vg_error( "Malformed auth ticket, too small (%u)\n", msg->m_conn ); + remove_client( client_id ); return; } @@ -301,7 +357,8 @@ static void gameserver_rx_auth( SteamNetworkingMessage_t *msg ){ if( msg->m_cbSize < sizeof(netmsg_auth)+auth->ticket_length || auth->ticket_length > 1024 ){ vg_error( "Malformed auth ticket, ticket_length incorrect (%u)\n", - auth->ticket_length ); + auth->ticket_length ); + remove_client( client_id ); return; } @@ -717,6 +774,9 @@ static void poll_connections(void){ else{ if( tmp->inetmsg_id == k_inetmsg_auth ) gameserver_rx_auth( msg ); + else if( tmp->inetmsg_id == k_inetmsg_version ){ + gameserver_rx_version( msg ); + } else { vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n", tmp->inetmsg_id ); diff --git a/gameserver.h b/gameserver.h index d8e0d38..fa856ce 100644 --- a/gameserver.h +++ b/gameserver.h @@ -2,6 +2,7 @@ #define GAMESERVER_H #define VG_SERVER + #include "vg/vg.h" #include "vg/vg_steam.h" #include "vg/vg_steam_networking.h" @@ -18,6 +19,7 @@ struct { struct gameserver_client { int active; + u32 version; int authenticated; HSteamNetConnection connection; char username[ NETWORK_USERNAME_MAX ]; diff --git a/network.c b/network.c index 2219a1c..9238901 100644 --- a/network.c +++ b/network.c @@ -58,6 +58,9 @@ static void request_auth_ticket(void){ } static void network_send_username(void){ + if( !network_connected() ) + return; + netmsg_playerusername *update = alloca( sizeof(netmsg_playerusername)+ NETWORK_USERNAME_MAX ); update->inetmsg_id = k_inetmsg_playerusername; @@ -267,6 +270,7 @@ static void network_disconnect(void){ SteamAPI_ISteamNetworkingSockets_CloseConnection( hSteamNetworkingSockets, network_client.remote, 0, NULL, 0 ); network_client.remote = 0; + network_client.state = k_ESteamNetworkingConnectionState_None; for( int i=0; im_info.m_eState == k_ESteamNetworkingConnectionState_ClosedByPeer ){ if( info->m_info.m_eEndReason == - k_ESteamNetConnectionEnd_Remote_Max ){ + k_ESteamNetConnectionEnd_Misc_InternalError ){ network_client.retries = 40; } network_disconnect(); @@ -373,6 +379,16 @@ static void network_connect(void){ hSteamNetworkingSockets, &remoteAddr, 0, NULL ); } +static void network_sign_on_complete(void){ + vg_success( "Sign on completed\n" ); + + /* send our init info */ + network_send_username(); + for( u32 i=0; iinetmsg_id >= 300) && (tmp->inetmsg_id < 400) ){ network_request_rx_300_400( msg ); } + else { + if( tmp->inetmsg_id == k_inetmsg_version ){ + netmsg_version *version = msg->m_pData; + if( version->version != NETWORK_SKATERIFT_VERSION ){ + network_disconnect(); + /* we dont want to connect to this server ever */ + network_client.retries = 999; + network_client.last_attempt = 999999999.9; + vg_error( "version mismatch with server\n" ); + } + else { + network_client.remote_version = version->version; + network_sign_on_complete(); + } + } + } SteamAPI_SteamNetworkingMessage_t_Release( msg ); } diff --git a/network.h b/network.h index 3b445d5..b643a1b 100644 --- a/network.h +++ b/network.h @@ -8,6 +8,7 @@ #include "vg/vg_stdint.h" #include "steam.h" +#include "network_common.h" #include "network_msg.h" #include "highscores.h" #include "addon_types.h" @@ -45,6 +46,7 @@ struct { HSteamNetConnection remote; ESteamNetworkingConnectionState state; + u32 remote_version; f64 last_attempt, last_frame; u32 retries; @@ -78,6 +80,7 @@ static void network_publish_laptime( const char *mod_uid, static void chat_send_message( const char *message ); static int network_connected(void){ + if( network_client.remote_version != NETWORK_SKATERIFT_VERSION ) return 0; return network_client.state == k_ESteamNetworkingConnectionState_Connected; } diff --git a/network_common.h b/network_common.h index 5191c95..da197d8 100644 --- a/network_common.h +++ b/network_common.h @@ -10,6 +10,7 @@ #define NETWORK_BUFFERFRAMES 6 #define NETWORK_LEADERBOARD_MAX_SIZE 1024 #define NETWORK_MAX_CHAT 128 +#define NETWORK_SKATERIFT_VERSION 8 #define NETWORK_LEADERBOARD_ALLTIME 0 #define NETWORK_LEADERBOARD_CURRENT_WEEK 1 diff --git a/network_msg.h b/network_msg.h index d3b49fd..1cd3007 100644 --- a/network_msg.h +++ b/network_msg.h @@ -13,12 +13,14 @@ #pragma pack(push,1) typedef struct netmsg_blank netmsg_blank; +enum{ k_inetmsg_blank = 0 }; struct netmsg_blank{ u16 inetmsg_id; }; -enum{ k_inetmsg_blank = 0 }; +/* send after version */ typedef struct netmsg_auth netmsg_auth; +enum{ k_inetmsg_auth = 1 }; struct netmsg_auth { u16 inetmsg_id; @@ -26,7 +28,14 @@ struct netmsg_auth u32 ticket_length; u8 ticket[]; }; -enum{ k_inetmsg_auth = 1 }; + +/* version should be sent before auth */ +typedef struct netmsg_version netmsg_version; +enum{ k_inetmsg_version = 2 }; +struct netmsg_version{ + u16 inetmsg_id; + u32 version; +}; /* server control 100 */ -- 2.25.1