Do actual domain resolution
authorhgn <hgodden00@gmail.com>
Wed, 10 Apr 2024 15:35:03 +0000 (16:35 +0100)
committerhgn <hgodden00@gmail.com>
Wed, 10 Apr 2024 15:35:03 +0000 (16:35 +0100)
network.c
network.h
skaterift.c

index 05af285462444c8b5a9dfbeeda028e97d941de84..4c9c61afd7424fb9e5d9fd66da47cacc3851e390 100644 (file)
--- a/network.c
+++ b/network.c
 #include "vg/vg_imgui.h"
 #include "gui.h"
 #include "ent_region.h"
+#include "vg/vg_loader.h"
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netdb.h>
 
 struct network_client network_client =
 {
    .auth_mode = eServerModeAuthentication,
    .state = k_ESteamNetworkingConnectionState_None,
-   .server_adress = "46.101.34.155",
    .last_intent_change = -99999.9
 };
 
@@ -328,8 +332,9 @@ void network_status_string( vg_str *str, u32 *colour )
 
          if( state == k_ESteamNetworkingConnectionState_None )
             vg_strcat( str, "No Connection" );
-         else if( state == k_ESteamNetworkingConnectionState_Connecting ){
-            vg_strcat( str, "Connecting to:\nskaterift.com" );
+         else if( state == k_ESteamNetworkingConnectionState_Connecting )
+         {
+            vg_strcatf( str, "Connecting...\n%s", network_client.host_adress );
 
             if( network_client.retries ){
                vg_strcat( str, "\n(" );
@@ -338,7 +343,7 @@ void network_status_string( vg_str *str, u32 *colour )
             }
          }
          else if( state == k_ESteamNetworkingConnectionState_Connected ){
-            vg_strcat( str, "Connected to:\nskaterift.com" );
+            vg_strcatf( str, "Connected to:\n%s", network_client.host_adress );
             *colour = 0xff00a020;
          }
          else if( state == k_ESteamNetworkingConnectionState_ClosedByPeer )
@@ -469,26 +474,37 @@ static void on_persona_state_change( CallbackMsg_t *msg ){
    }
 }
 
-static void network_connect(void){
-   char ip_buf[128];
-   vg_str str;
-   vg_strnull( &str, ip_buf, sizeof(ip_buf) );
-   vg_strcat( &str, network_client.server_adress );
-   vg_strcat( &str, ":" );
-   vg_strcati32( &str, NETWORK_PORT );
+void network_set_host( const char *host_str, const char *port_str )
+{
+   vg_strncpy( host_str, network_client.host_adress, 
+               sizeof(network_client.host_adress), k_strncpy_overflow_fatal );
 
-   if( !vg_strgood(&str) ) return;
+   memset( &network_client.ip, 0, sizeof(network_client.ip) );
+   network_client.ip_resolved = 0;
 
-   /* Connect to server if not connected */
-   SteamNetworkingIPAddr remoteAddr;
-   SteamAPI_SteamNetworkingIPAddr_ParseString( &remoteAddr, str.buffer );
+   if( port_str )
+   {
+      vg_strncpy( port_str, network_client.host_port, 
+                  sizeof(network_client.host_port), k_strncpy_overflow_fatal );
+   }
+   else
+   {
+      vg_str str;
+      vg_strnull( &str, network_client.host_port, 
+                  sizeof(network_client.host_port) );
+      vg_strcati32( &str, NETWORK_PORT );
+   }
 
-   char buf[256];
-   SteamAPI_SteamNetworkingIPAddr_ToString( &remoteAddr, buf, 256, 1 );
-   vg_info( "connect to: %s\n", buf );
+   network_client.ip.m_port = atoi( network_client.host_port );
+}
+
+static void network_connect(void)
+{
+   VG_ASSERT( network_client.ip_resolved );
 
+   vg_info( "connecting...\n" );
    network_client.remote = SteamAPI_ISteamNetworkingSockets_ConnectByIPAddress( 
-                  hSteamNetworkingSockets, &remoteAddr, 0, NULL );
+                  hSteamNetworkingSockets, &network_client.ip, 0, NULL );
 }
 
 static void network_sign_on_complete(void){
@@ -552,6 +568,64 @@ static void poll_remote_connection(void){
    }
 }
 
+static void network_resolve_host_async( void *payload, u32 size )
+{
+   u32 *status = payload;
+   network_client.ip_resolved = *status;
+
+   char buf[256];
+   SteamAPI_SteamNetworkingIPAddr_ToString( &network_client.ip, buf, 256, 1 );
+   vg_info( "Resolved host address to: %s\n", buf );
+}
+
+static void network_resolve_host_thread( void *_ )
+{
+   vg_async_item *call = vg_async_alloc(8);
+   u32 *status = call->payload;
+   *status = 0;
+
+   if( (network_client.host_adress[0] >= '0') && 
+       (network_client.host_adress[0] <= '9') )
+   {
+      SteamAPI_SteamNetworkingIPAddr_ParseString( 
+            &network_client.ip, 
+            network_client.host_adress );
+      network_client.ip.m_port = atoi( network_client.host_port );
+      *status = 1;
+      goto end;
+   }
+
+   vg_info( "Resolving host.. %s (:%s)\n",
+             network_client.host_adress, network_client.host_port );
+
+   struct addrinfo  hints;
+   struct addrinfo  *result;
+
+   /* Obtain address(es) matching host/port. */
+
+   memset( &hints, 0, sizeof(hints) );
+   hints.ai_family = AF_INET6;
+   hints.ai_socktype = SOCK_DGRAM;
+   hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
+   hints.ai_protocol = 0;
+
+   int s = getaddrinfo( network_client.host_adress, network_client.host_port, 
+                        &hints, &result);
+   if( s != 0 ) 
+   {
+      vg_error( "getaddrinfo: %s\n", gai_strerror(s) );
+      goto end;
+   }
+
+   struct sockaddr_in6 *inaddr = (struct sockaddr_in6 *)result->ai_addr;
+   memcpy( network_client.ip.m_ipv6, &inaddr->sin6_addr, 16 );
+   freeaddrinfo( result );
+
+   *status = 1;
+
+end: vg_async_dispatch( call, network_resolve_host_async );
+}
+
 void network_update(void)
 {
    if( !steam_ready )
@@ -559,18 +633,21 @@ void network_update(void)
 
    ESteamNetworkingConnectionState state = network_client.state;
 
-   if( network_client.user_intent == k_server_intent_offline ){
+   if( network_client.user_intent == k_server_intent_offline )
+   {
       if( state != k_ESteamNetworkingConnectionState_None )
          network_disconnect();
 
       return;
    }
 
-   if( state == k_ESteamNetworkingConnectionState_Connected ){
+   if( state == k_ESteamNetworkingConnectionState_Connected )
+   {
       poll_remote_connection();
       f64 frame_delta = vg.time_real - network_client.last_frame;
 
-      if( frame_delta > NETWORK_FRAMERATE ){
+      if( frame_delta > NETWORK_FRAMERATE )
+      {
          network_client.last_frame = vg.time_real;
          remote_player_send_playerframe();
          localplayer.sfx_buffer_count = 0;
@@ -578,12 +655,15 @@ void network_update(void)
 
       remote_player_debug_update();
    }
-   else {
+   else 
+   {
       if( (state == k_ESteamNetworkingConnectionState_Connecting) ||
-          (state == k_ESteamNetworkingConnectionState_FindingRoute) ){
+          (state == k_ESteamNetworkingConnectionState_FindingRoute) )
+      {
          return;
       }
-      else {
+      else 
+      {
          f64 waited = vg.time_real - network_client.last_attempt,
              min_wait = 1.0;
 
@@ -592,8 +672,18 @@ void network_update(void)
 
          if( waited < min_wait )
             return;
+
+         if( !network_client.ip_resolved )
+         {
+            if( vg_loader_availible() )
+            {
+               vg_loader_start( network_resolve_host_thread, NULL );
+            }
+            else return;
+         }
+         else
+            network_connect();
          
-         network_connect();
          network_client.retries ++;
          network_client.last_attempt = vg.time_real;
       }
index bf2ccf8983fd88897863a0a85fac2a710a24b206..4bd5a37ddcc6dbcad21e9b0022c60c8ed17e8199 100644 (file)
--- a/network.h
+++ b/network.h
@@ -62,7 +62,9 @@ struct network_client
    *request_buffer;
    vg_pool request_pool;
 
-   char server_adress[64];
+   SteamNetworkingIPAddr ip;
+   char host_port[8], host_adress[256];
+   bool ip_resolved;
 
    enum server_intent {
       k_server_intent_offline,
@@ -85,6 +87,7 @@ void chat_send_message( const char *message );
 void render_server_status_gui(void);
 void network_status_string( vg_str *str, u32 *colour );
 void network_send_region(void);
+void network_set_host( const char *host_str, const char *port_str );
 
 static inline int network_connected(void)
 {
index dd082041aa67164384e9e02273bd2db7d1ab6c47..27af1d10b9885d275a465fec250f3181b387a89b 100644 (file)
@@ -56,7 +56,9 @@ struct skaterift_globals skaterift =
 
 static int k_tools_mode = 0;
 
-int main( int argc, char *argv[] ){
+int main( int argc, char *argv[] )
+{
+   network_set_host( "skaterift.com", NULL );
    vg_mem.use_libc_malloc = 0;
    vg_set_mem_quota( 160*1024*1024 );
    vg_enter( argc, argv, "Voyager Game Engine" ); 
@@ -70,9 +72,9 @@ void vg_launch_opt(void)
       network_client.auth_mode = eServerModeNoAuthentication;
    }
 
-   if( (arg = vg_long_opt_arg( "server" )) ){
-      vg_strncpy( arg, network_client.server_adress, 64, 
-                  k_strncpy_overflow_fatal );
+   if( (arg = vg_long_opt_arg( "server" )) )
+   {
+      network_set_host( arg, NULL );
    }
 
    if( vg_long_opt( "tools" ) ){
@@ -317,7 +319,12 @@ void vg_pre_update(void)
    /* TODO: how can we compress this? */
    ent_miniworld_preupdate();
    world_entity_focus_preupdate();
-   player__pre_update();
+
+   if( skaterift.activity != k_skaterift_menu )
+   {
+      player__pre_update();
+   }
+
    skaterift_replay_pre_update();
    remote_sfx_pre_update();
    skateshop_world_preupdate( world_current_instance() );