server version check
authorhgn <hgodden00@gmail.com>
Mon, 13 Nov 2023 02:16:42 +0000 (02:16 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 13 Nov 2023 02:16:42 +0000 (02:16 +0000)
gameserver.c
gameserver.h
network.c
network.h
network_common.h
network_msg.h

index 3ff58f1d40936e2abea9daf5923691f8318b7571..9116a34eb3aa240fc4e556fbb54471496bd620ab 100644 (file)
@@ -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 );
index d8e0d38c018d929938291de77d012c24664bbb0f..fa856cee1785aed63b10e9112a84d411cd58dec2 100644 (file)
@@ -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 ];
index 2219a1ce8cedd8b3b8e9999ad1f3c2fcee1ca7cd..92389016c49a7a5240e03ff0794bc674ac6bfd05 100644 (file)
--- 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; i<vg_list_size(netplayers.list); i++ ){
       netplayers.list[i].active = 0;
@@ -287,6 +291,14 @@ static void on_server_connect_status( CallbackMsg_t *msg ){
             k_ESteamNetworkingConnectionState_Connected ){
          vg_success("  Connected to remote server.. authenticating\n");
 
+         /* send version info to server */
+         netmsg_version version;
+         version.inetmsg_id = k_inetmsg_version;
+         version.version = NETWORK_SKATERIFT_VERSION;
+         SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
+               hSteamNetworkingSockets, network_client.remote, &version, 
+               sizeof(netmsg_version), k_nSteamNetworkingSend_Reliable, NULL );
+
          /* TODO: We should really wait to see if the server is in auth mode
           * first... */
          u32 size = sizeof(netmsg_auth) + network_client.app_key_length;
@@ -299,18 +311,12 @@ static void on_server_connect_status( CallbackMsg_t *msg ){
          SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
                hSteamNetworkingSockets, network_client.remote, auth, size,
                k_nSteamNetworkingSend_Reliable, NULL );
-
-         network_send_username();
-
-         for( u32 i=0; i<k_netmsg_playeritem_max; i ++ ){
-            network_send_item(i);
-         }
       }
       else if( info->m_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; i<k_netmsg_playeritem_max; i ++ ){
+      network_send_item(i);
+   }
+}
+
 static void poll_remote_connection(void){
    SteamNetworkingMessage_t *messages[32];
    int len;
@@ -401,6 +417,22 @@ static void poll_remote_connection(void){
          else if( (tmp->inetmsg_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 );
       }
index 3b445d5639a7c4106a380a3c05b83d97624f9d7b..b643a1b7da9f96641972d352417c3569d3ba2577 100644 (file)
--- 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;
 }
 
index 5191c95b43ac26fc6bc4d35e696a351a9d8aafa9..da197d8d02ad993c0423e1d354cdd4d7cf7fa5a9 100644 (file)
@@ -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
index d3b49fd26e3101a6321a2a51567a5e737b0790eb..1cd3007ee2da1872e81ec56bef2a9a05a0679e1f 100644 (file)
 #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 */