]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
changed entity networking prioritization code to use the center of the model rather...
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index 5beafc77a41c6a2ea5a1d759e7353f7e2b9cd680..4f820099e2e6e2efe3e6707a359fa6ea064d9552 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
@@ -104,7 +104,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
        struct hostent *hostentry;
        const char *colon;
        char name[128];
-       if (!address || !string)
+       if (!address || !string || !*string)
                return 0;
        memset(address, 0, sizeof(*address));
        address->addresstype = LHNETADDRESSTYPE_NONE;
@@ -134,7 +134,9 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                return 1;
        }
        // try to parse as dotted decimal ipv4 address first
-       if (sscanf(name, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) == 4 && (unsigned int)d1 < 256 && (unsigned int)d2 < 256 && (unsigned int)d3 < 256 && (unsigned int)d4 < 256)
+       // note this supports partial ip addresses
+       d1 = d2 = d3 = d4 = 0;
+       if (sscanf(name, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) >= 1 && (unsigned int)d1 < 256 && (unsigned int)d2 < 256 && (unsigned int)d3 < 256 && (unsigned int)d4 < 256)
        {
                // parsed a valid ipv4 address
                address->addresstype = LHNETADDRESSTYPE_INET4;
@@ -252,7 +254,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
                {
                        if (stringbuffersize >= 6)
                        {
-                               strcpy(string, "local");
+                               memcpy(string, "local", 6);
                                return 1;
                        }
                }
@@ -769,7 +771,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
 int main(int argc, char **argv)
 {
 #if 1
-       char *buffer = "socket to socket test successful", buffer2[1024];
+       char *buffer = "test", buffer2[1024];
        int blen = strlen(buffer);
        int b2len = 1024;
        lhnetsocket_t *sock1;
@@ -777,24 +779,26 @@ int main(int argc, char **argv)
        lhnetaddress_t myaddy1;
        lhnetaddress_t myaddy2;
        lhnetaddress_t myaddy3;
+       lhnetaddress_t localhostaddy1;
+       lhnetaddress_t localhostaddy2;
        int test1;
        int test2;
 
-       strcpy(buffer2, "socket to socket test failed");
-
        printf("calling LHNET_Init\n");
        LHNET_Init();
 
        printf("calling LHNET_FromPort twice to create two local addresses\n");
        LHNETADDRESS_FromPort(&myaddy1, LHNETADDRESSTYPE_INET4, 4000);
        LHNETADDRESS_FromPort(&myaddy2, LHNETADDRESSTYPE_INET4, 4001);
+       LHNETADDRESS_FromString(&localhostaddy1, "127.0.0.1", 4000);
+       LHNETADDRESS_FromString(&localhostaddy2, "127.0.0.1", 4001);
 
        printf("calling LHNET_OpenSocket_Connectionless twice to create two local sockets\n");
        sock1 = LHNET_OpenSocket_Connectionless(&myaddy1);
        sock2 = LHNET_OpenSocket_Connectionless(&myaddy2);
 
        printf("calling LHNET_Write to send a packet from the first socket to the second socket\n");
-       test1 = LHNET_Write(sock1, buffer, blen, &myaddy2);
+       test1 = LHNET_Write(sock1, buffer, blen, &localhostaddy2);
        printf("sleeping briefly\n");
 #ifdef WIN32
        Sleep (100);
@@ -802,8 +806,12 @@ int main(int argc, char **argv)
        usleep (100000);
 #endif
        printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n");
-       test2 = LHNET_Read(sock2, buffer2, b2len, &myaddy3);
-       Con_Printf("%s\n", buffer2);
+       test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3);
+       if (test2 > 0)
+               Con_Printf("socket to socket test succeeded\n");
+       else
+               Con_Printf("socket to socket test failed\n");
+
 #ifdef WIN32
        printf("press any key to exit\n");
        getchar();