]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
implemented caching of DNS names in lhnet.c
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 8 May 2005 19:57:24 +0000 (19:57 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 8 May 2005 19:57:24 +0000 (19:57 +0000)
changed sv_masterextra* servers back to DNS names (but kept IP addresses as comments, note the dpmaster.deathmask.net IP has changed)

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5251 d7cf8633-e32d-0410-b094-e92efae38249

lhnet.c
netconn.c

diff --git a/lhnet.c b/lhnet.c
index f7de35abfa8bcda4d931eafc268319b46eda0d67..91805d6f130f1415759eae4b7d5f8ca587e51afe 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
 
 #include "lhnet.h"
 
+// to make LHNETADDRESS_FromString resolve repeated hostnames faster, cache them
+#define MAX_NAMECACHE 64
+static struct namecache_s
+{
+       lhnetaddress_t address;
+       char name[64];
+}
+namecache[MAX_NAMECACHE];
+static int namecacheposition = 0;
+
 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
 {
        if (!address)
@@ -64,7 +74,7 @@ int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
 
 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
 {
-       int port, namelen, d1, d2, d3, d4;
+       int i, port, namelen, d1, d2, d3, d4;
        struct hostent *hostentry;
        const char *colon;
        char name[128];
@@ -113,6 +123,24 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
 #endif
                return 1;
        }
+       for (i = 0;i < MAX_NAMECACHE;i++)
+               if (!strcmp(namecache[i].name, name))
+                       break;
+       if (i < MAX_NAMECACHE)
+       {
+               *address = namecache[i].address;
+               if (address->addresstype == LHNETADDRESSTYPE_INET6)
+               {
+                       address->addressdata.inet6.port = htons((unsigned short)port);
+                       return 1;
+               }
+               else if (address->addresstype == LHNETADDRESSTYPE_INET4)
+               {
+                       address->addressdata.inet4.port = htons((unsigned short)port);
+                       return 1;
+               }
+               return false;
+       }
        // try gethostbyname (handles dns and other ip formats)
        hostentry = gethostbyname(name);
        if (hostentry)
@@ -124,6 +152,11 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                        address->addressdata.inet6.family = hostentry->h_addrtype;
                        address->addressdata.inet6.port = htons((unsigned short)port);
                        memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
+                       for (i = 0;i < sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
+                               namecache[namecacheposition].name[i] = name[i];
+                       namecache[namecacheposition].name[i] = 0;
+                       namecache[namecacheposition].address = *address;
+                       namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
 #ifdef STANDALONETEST
                        printf("gethostbyname(\"%s\") returned ipv6 address [%x:%x:%x:%x:%x:%x:%x:%x]:%d\n", name, (int)address->addressdata.inet6.address[0], (int)address->addressdata.inet6.address[1], (int)address->addressdata.inet6.address[2], (int)address->addressdata.inet6.address[3], (int)address->addressdata.inet6.address[4], (int)address->addressdata.inet6.address[5], (int)address->addressdata.inet6.address[6], (int)address->addressdata.inet6.address[7], (int)ntohs(address->addressdata.inet6.port));
 #endif
@@ -136,6 +169,11 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                        address->addressdata.inet4.family = hostentry->h_addrtype;
                        address->addressdata.inet4.port = htons((unsigned short)port);
                        memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
+                       for (i = 0;i < sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
+                               namecache[namecacheposition].name[i] = name[i];
+                       namecache[namecacheposition].name[i] = 0;
+                       namecache[namecacheposition].address = *address;
+                       namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
 #ifdef STANDALONETEST
                        printf("gethostbyname(\"%s\") returned ipv4 address %d.%d.%d.%d:%d\n", name, (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port));
 #endif
@@ -145,6 +183,11 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
 #ifdef STANDALONETEST
        printf("gethostbyname failed on address \"%s\"\n", name);
 #endif
+       for (i = 0;i < sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
+               namecache[namecacheposition].name[i] = name[i];
+       namecache[namecacheposition].name[i] = 0;
+       namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
+       namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
        return 0;
 }
 
index 4e68c72a15ffc8013de416b27d789937341f6085..307ae43d861f4c26ec4962df857d22c4d8a3e86b 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -35,9 +35,9 @@ static cvar_t sv_masters [] =
        {CVAR_SAVE, "sv_master2", ""},
        {CVAR_SAVE, "sv_master3", ""},
        {CVAR_SAVE, "sv_master4", ""},
-       {0, "sv_masterextra1", "69.59.212.88"}, // ghdigital.com
-       {0, "sv_masterextra2", "66.169.205.13"}, // dpmaster.deathmask.net
-       {0, "sv_masterextra3", "12.166.196.192"}, // blaze.mindphukd.org
+       {0, "sv_masterextra1", "ghdigital.com"}, //69.59.212.88
+       {0, "sv_masterextra2", "dpmaster.deathmask.net"}, //209.164.24.243
+       {0, "sv_masterextra3", "blaze.mindphukd.org"}, //12.166.196.192
        {0, NULL, NULL}
 };