]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
fix a thinko in big endian support (forgot to use defined() in a #elif)
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index 742e2d5db1016006c6b2366827b66f8760745185..16cf41748734bcac2fded46bad9e4db14f4b3e08 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
@@ -9,7 +9,7 @@
 #include <winsock.h>
 #else
 #include <netdb.h>
-//#include <netinet/in.h>
+#include <netinet/in.h>
 //#include <arpa/inet.h>
 #include <unistd.h>
 #include <sys/socket.h>
 
 // for Z_Malloc/Z_Free in quake
 #ifndef STANDALONETEST
+#include "quakedef.h"
 #include "zone.h"
+#include "sys.h"
+#include "netconn.h"
 #else
 #define Z_Malloc malloc
 #define Z_Free free
@@ -29,6 +32,8 @@
 
 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
 {
+       if (!address)
+               return 0;
        switch(addresstype)
        {
        case LHNETADDRESSTYPE_LOOP:
@@ -61,6 +66,8 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
        struct hostent *hostentry;
        const char *colon;
        char name[128];
+       if (!address || !string)
+               return 0;
        memset(address, 0, sizeof(*address));
        address->addresstype = LHNETADDRESSTYPE_NONE;
        port = 0;
@@ -143,6 +150,8 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
 {
        *string = 0;
+       if (!address || !string || stringbuffersize < 1)
+               return 0;
        switch(address->addresstype)
        {
        default:
@@ -207,11 +216,16 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
 
 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
 {
-       return address->addresstype;
+       if (address)
+               return address->addresstype;
+       else
+               return LHNETADDRESSTYPE_NONE;
 }
 
 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
 {
+       if (!address)
+               return -1;
        switch(address->addresstype)
        {
        case LHNETADDRESSTYPE_LOOP:
@@ -227,6 +241,8 @@ int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
 
 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
 {
+       if (!address)
+               return 0;
        switch(address->addresstype)
        {
        case LHNETADDRESSTYPE_LOOP:
@@ -245,6 +261,8 @@ int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
 
 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
 {
+       if (!address1 || !address2)
+               return 1;
        if (address1->addresstype != address2->addresstype)
                return 1;
        switch(address1->addresstype)
@@ -281,6 +299,9 @@ typedef struct lhnetpacket_s
        int sourceport;
        int destinationport;
        time_t timeout;
+#ifndef STANDALONETEST
+       double sentdoubletime;
+#endif
        struct lhnetpacket_s *next, *prev;
 }
 lhnetpacket_t;
@@ -322,6 +343,8 @@ void LHNET_Shutdown(void)
 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
 {
        lhnetsocket_t *lhnetsocket, *s;
+       if (!address)
+               return NULL;
        lhnetsocket = Z_Malloc(sizeof(*lhnetsocket));
        if (lhnetsocket)
        {
@@ -443,12 +466,17 @@ void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
 
 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
 {
-       return &sock->address;
+       if (sock)
+               return &sock->address;
+       else
+               return NULL;
 }
 
 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
 {
        int value = 0;
+       if (!lhnetsocket || !address || !content || maxcontentlength < 1)
+               return -1;
        if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
        {
                time_t currenttime;
@@ -459,6 +487,18 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
                {
                        pnext = p->next;
+                       if (p->timeout < currenttime)
+                       {
+                               // unlink and free
+                               p->next->prev = p->prev;
+                               p->prev->next = p->next;
+                               Z_Free(p);
+                               continue;
+                       }
+#ifndef STANDALONETEST
+                       if (p->sentdoubletime && Sys_DoubleTime() < p->sentdoubletime)
+                               continue;
+#endif
                        if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
                        {
                                if (p->length <= maxcontentlength)
@@ -475,13 +515,6 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                                p->prev->next = p->next;
                                Z_Free(p);
                        }
-                       else if (p->timeout < currenttime)
-                       {
-                               // unlink and free
-                               p->next->prev = p->prev;
-                               p->prev->next = p->next;
-                               Z_Free(p);
-                       }
                }
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
@@ -499,11 +532,23 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                {
 #ifdef WIN32
                        int e = WSAGetLastError();
-                       if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
+                       if (e == WSAEWOULDBLOCK)
                                return 0;
+                       switch (e)
+                       {
+                               case WSAECONNREFUSED:
+                                       Con_Printf("Connection refused\n");
+                                       return 0;
+                       }
 #else
-                       if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
+                       if (errno == EWOULDBLOCK)
                                return 0;
+                       switch (errno)
+                       {
+                               case ECONNREFUSED:
+                                       Con_Printf("Connection refused\n");
+                                       return 0;
+                       }
 #endif
                }
        }
@@ -522,11 +567,23 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                {
 #ifdef WIN32
                        int e = WSAGetLastError();
-                       if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
+                       if (e == WSAEWOULDBLOCK)
                                return 0;
+                       switch (e)
+                       {
+                               case WSAECONNREFUSED:
+                                       Con_Printf("Connection refused\n");
+                                       return 0;
+                       }
 #else
-                       if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
+                       if (errno == EWOULDBLOCK)
                                return 0;
+                       switch (errno)
+                       {
+                               case ECONNREFUSED:
+                                       Con_Printf("Connection refused\n");
+                                       return 0;
+                       }
 #endif
                }
        }
@@ -536,6 +593,8 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
 {
        int value = -1;
+       if (!lhnetsocket || !address || !content || contentlength < 1)
+               return -1;
        if (lhnetsocket->address.addresstype != address->addresstype)
                return -1;
        if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
@@ -552,6 +611,10 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                p->prev = p->next->prev;
                p->next->prev = p;
                p->prev->next = p;
+#ifndef STANDALONETEST
+               if (cl_fakelocalping_min.integer || cl_fakelocalping_max.integer)
+                       p->sentdoubletime = Sys_DoubleTime() + (cl_fakelocalping_min.integer + ((cl_fakelocalping_max.integer - cl_fakelocalping_min.integer) * (rand() & 255) / 256)) / 1000.0;
+#endif
                value = contentlength;
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)