]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
made darkplaces compile successfully with g++ to test for errors C doesn't care about...
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index d9597f215469ef25fa17792299dd50dfe5d90fe4..89b47df48075874255fb358863215b64f4128115 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
@@ -8,25 +8,40 @@
 #ifdef WIN32
 #include <winsock.h>
 #else
-#include <netdb.h>
-//#include <netinet/in.h>
-//#include <arpa/inet.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <errno.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #endif
 
 // for Z_Malloc/Z_Free in quake
 #ifndef STANDALONETEST
+#include "quakedef.h"
 #include "zone.h"
+#include "sys.h"
+#include "netconn.h"
 #else
+#define Con_Print printf
+#define Con_Printf printf
 #define Z_Malloc malloc
 #define Z_Free free
 #endif
 
 #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)
@@ -59,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];
@@ -92,8 +107,41 @@ 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;
+       }
+       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)
        {
@@ -104,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 < (int)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
@@ -116,31 +169,25 @@ 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 < (int)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
                        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);
-#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));
+       printf("gethostbyname failed on address \"%s\"\n", name);
 #endif
-               return 1;
-       }
+       for (i = 0;i < (int)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;
 }
 
@@ -296,6 +343,9 @@ typedef struct lhnetpacket_s
        int sourceport;
        int destinationport;
        time_t timeout;
+#ifndef STANDALONETEST
+       double sentdoubletime;
+#endif
        struct lhnetpacket_s *next, *prev;
 }
 lhnetpacket_t;
@@ -334,12 +384,70 @@ 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));
+       lhnetsocket = (lhnetsocket_t *)Z_Malloc(sizeof(*lhnetsocket));
        if (lhnetsocket)
        {
                memset(lhnetsocket, 0, sizeof(*lhnetsocket));
@@ -381,17 +489,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;
@@ -401,22 +502,42 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                        if (ioctl(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
 #endif
                                        {
-                                               if (bind(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4)) != -1)
+#ifdef WIN32
+                                               int namelen;
+#else
+                                               socklen_t namelen;
+#endif
+                                               namelen = address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4);
+                                               if (bind(lhnetsocket->inetsocket, (struct sockaddr *)&lhnetsocket->address.addressdata, namelen) != -1)
                                                {
+                                                       int i = 1;
+                                                       getsockname(lhnetsocket->inetsocket, (struct sockaddr *)&lhnetsocket->address.addressdata, &namelen);
+                                                       // enable broadcast on this socket
+                                                       setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i));
                                                        lhnetsocket->next = &lhnet_socketlist;
                                                        lhnetsocket->prev = lhnetsocket->next->prev;
                                                        lhnetsocket->next->prev = lhnetsocket;
                                                        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_Print("LHNET_OpenSocket_Connectionless: WSAStartup failed\n");
+#endif
                        break;
                default:
                        break;
@@ -481,6 +602,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 (cl_netlocalping.value && (Sys_DoubleTime() - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
+                               continue;
+#endif
                        if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
                        {
                                if (p->length <= maxcontentlength)
@@ -497,18 +630,11 @@ 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)
        {
-               int inetaddresslength;
+               unsigned int inetaddresslength;
                address->addresstype = LHNETADDRESSTYPE_NONE;
                inetaddresslength = sizeof(address->addressdata.inet4);
                value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
@@ -521,17 +647,29 @@ 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_Print("Connection refused\n");
+                                       return 0;
+                       }
 #else
-                       if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
+                       if (errno == EWOULDBLOCK)
                                return 0;
+                       switch (errno)
+                       {
+                               case ECONNREFUSED:
+                                       Con_Print("Connection refused\n");
+                                       return 0;
+                       }
 #endif
                }
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
        {
-               int inetaddresslength;
+               unsigned int inetaddresslength;
                address->addresstype = LHNETADDRESSTYPE_NONE;
                inetaddresslength = sizeof(address->addressdata.inet6);
                value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
@@ -544,11 +682,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_Print("Connection refused\n");
+                                       return 0;
+                       }
 #else
-                       if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
+                       if (errno == EWOULDBLOCK)
                                return 0;
+                       switch (errno)
+                       {
+                               case ECONNREFUSED:
+                                       Con_Print("Connection refused\n");
+                                       return 0;
+                       }
 #endif
                }
        }
@@ -565,7 +715,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
        if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
        {
                lhnetpacket_t *p;
-               p = Z_Malloc(sizeof(*p) + contentlength);
+               p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength);
                p->data = (void *)(p + 1);
                memcpy(p->data, content, contentlength);
                p->length = contentlength;
@@ -576,6 +726,9 @@ 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
+               p->sentdoubletime = Sys_DoubleTime();
+#endif
                value = contentlength;
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)