]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
handle funky ToAscii values (shift/ctrl/alt sometimes produce ascii values for no...
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index 742e2d5db1016006c6b2366827b66f8760745185..998c7b08686c43fd5e65fcea977b1a2511347e00 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;
@@ -88,8 +95,23 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                address->addressdata.loop.port = port;
                return 1;
        }
-       // try to parse with gethostbyname first, because it can handle ipv4 and
-       // ipv6 (in various address formats), as well as dns names
+       // 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)
+       {
+               // parsed a valid ipv4 address
+               address->addresstype = LHNETADDRESSTYPE_INET4;
+               address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
+               address->addressdata.inet4.port = htons((unsigned short)port);
+               address->addressdata.inet4.address[0] = (unsigned char)d1;
+               address->addressdata.inet4.address[1] = (unsigned char)d2;
+               address->addressdata.inet4.address[2] = (unsigned char)d3;
+               address->addressdata.inet4.address[3] = (unsigned char)d4;
+#ifdef STANDALONETEST
+               printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %d.%d.%d.%d:%d\n", string, (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
+               return 1;
+       }
+       // try gethostbyname (handles dns and other ip formats)
        hostentry = gethostbyname(name);
        if (hostentry)
        {
@@ -118,31 +140,17 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
                        return 1;
                }
        }
-       // failed, try to parse as an ipv4 address as a fallback (is this needed?)
 #ifdef STANDALONETEST
-       printf("gethostbyname and gethostbyaddr failed on address \"%s\"\n", name);
+       printf("gethostbyname failed on address \"%s\"\n", name);
 #endif
-       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)
-       {
-               // parsed a valid ipv4 address
-               address->addresstype = LHNETADDRESSTYPE_INET4;
-               address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
-               address->addressdata.inet4.port = htons((unsigned short)port);
-               address->addressdata.inet4.address[0] = (unsigned char)d1;
-               address->addressdata.inet4.address[1] = (unsigned char)d2;
-               address->addressdata.inet4.address[2] = (unsigned char)d3;
-               address->addressdata.inet4.address[3] = (unsigned char)d4;
-#ifdef STANDALONETEST
-               printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %d.%d.%d.%d:%d\n", string, (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
-               return 1;
-       }
        return 0;
 }
 
 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 +215,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 +240,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 +260,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 +298,9 @@ typedef struct lhnetpacket_s
        int sourceport;
        int destinationport;
        time_t timeout;
+#ifndef STANDALONETEST
+       double sentdoubletime;
+#endif
        struct lhnetpacket_s *next, *prev;
 }
 lhnetpacket_t;
@@ -319,9 +339,69 @@ void LHNET_Shutdown(void)
        lhnet_active = 0;
 }
 
+static const char *LHNETPRIVATE_StrError(void)
+{
+#ifdef WIN32
+       int i = WSAGetLastError();
+       switch (i)
+       {
+               case WSAEINTR:           return "WSAEINTR";                                     
+               case WSAEBADF:           return "WSAEBADF";
+               case WSAEACCES:          return "WSAEACCES";          
+               case WSAEFAULT:          return "WSAEFAULT";
+               case WSAEINVAL:          return "WSAEINVAL";
+               case WSAEMFILE:          return "WSAEMFILE";
+               case WSAEWOULDBLOCK:     return "WSAEWOULDBLOCK";
+               case WSAEINPROGRESS:     return "WSAEINPROGRESS";
+               case WSAEALREADY:        return "WSAEALREADY";
+               case WSAENOTSOCK:        return "WSAENOTSOCK";
+               case WSAEDESTADDRREQ:    return "WSAEDESTADDRREQ";
+               case WSAEMSGSIZE:        return "WSAEMSGSIZE";
+               case WSAEPROTOTYPE:      return "WSAEPROTOTYPE";
+               case WSAENOPROTOOPT:     return "WSAENOPROTOOPT";
+               case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
+               case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
+               case WSAEOPNOTSUPP:      return "WSAEOPNOTSUPP";
+               case WSAEPFNOSUPPORT:    return "WSAEPFNOSUPPORT";
+               case WSAEAFNOSUPPORT:    return "WSAEAFNOSUPPORT";
+               case WSAEADDRINUSE:      return "WSAEADDRINUSE";
+               case WSAEADDRNOTAVAIL:   return "WSAEADDRNOTAVAIL";
+               case WSAENETDOWN:        return "WSAENETDOWN";
+               case WSAENETUNREACH:     return "WSAENETUNREACH";
+               case WSAENETRESET:       return "WSAENETRESET";
+               case WSAECONNABORTED:    return "WSAECONNABORTED";
+               case WSAECONNRESET:      return "WSAECONNRESET";
+               case WSAENOBUFS:         return "WSAENOBUFS";
+               case WSAEISCONN:         return "WSAEISCONN";
+               case WSAENOTCONN:        return "WSAENOTCONN";
+               case WSAESHUTDOWN:       return "WSAESHUTDOWN";
+               case WSAETOOMANYREFS:    return "WSAETOOMANYREFS";
+               case WSAETIMEDOUT:       return "WSAETIMEDOUT";
+               case WSAECONNREFUSED:    return "WSAECONNREFUSED";
+               case WSAELOOP:           return "WSAELOOP";
+               case WSAENAMETOOLONG:    return "WSAENAMETOOLONG";
+               case WSAEHOSTDOWN:       return "WSAEHOSTDOWN";
+               case WSAEHOSTUNREACH:    return "WSAEHOSTUNREACH";
+               case WSAENOTEMPTY:       return "WSAENOTEMPTY";
+               case WSAEPROCLIM:        return "WSAEPROCLIM";
+               case WSAEUSERS:          return "WSAEUSERS";
+               case WSAEDQUOT:          return "WSAEDQUOT";
+               case WSAESTALE:          return "WSAESTALE";
+               case WSAEREMOTE:         return "WSAEREMOTE";
+               case WSAEDISCON:         return "WSAEDISCON";
+               case 0:                  return "no error";
+               default:                 return "unknown WSAE error";  
+       }
+#else
+       return strerror(errno);
+#endif
+}
+
 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
 {
        lhnetsocket_t *lhnetsocket, *s;
+       if (!address)
+               return NULL;
        lhnetsocket = Z_Malloc(sizeof(*lhnetsocket));
        if (lhnetsocket)
        {
@@ -364,17 +444,10 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                case LHNETADDRESSTYPE_INET4:
                case LHNETADDRESSTYPE_INET6:
 #ifdef WIN32
-                       if (!lhnet_didWSAStartup && !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata))
-                       {
-                               lhnet_didWSAStartup = 1;
-#else
+                       if (lhnet_didWSAStartup || (lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata)))
                        {
 #endif
-                               if (address->addresstype == LHNETADDRESSTYPE_INET6)
-                                       lhnetsocket->inetsocket = socket(LHNETADDRESSTYPE_INET6_FAMILY, SOCK_DGRAM, IPPROTO_UDP);
-                               else
-                                       lhnetsocket->inetsocket = socket(LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP);
-                               if (lhnetsocket->inetsocket != -1)
+                               if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? LHNETADDRESSTYPE_INET6_FAMILY : LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP)) != -1)
                                {
 #ifdef WIN32
                                        u_long _true = 1;
@@ -392,14 +465,24 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                                        lhnetsocket->prev->next = lhnetsocket;
                                                        return lhnetsocket;
                                                }
+                                               else
+                                                       Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
                                        }
+                                       else
+                                               Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
 #ifdef WIN32
                                        closesocket(lhnetsocket->inetsocket);
 #else
                                        close(lhnetsocket->inetsocket);
 #endif
                                }
+                               else
+                                       Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
+#ifdef WIN32
                        }
+                       else
+                               Con_Printf("LHNET_OpenSocket_Connectionless: WSAStartup failed\n");
+#endif
                        break;
                default:
                        break;
@@ -443,12 +526,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 +547,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 +575,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 +592,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 +627,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 +653,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 +671,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)