]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
Cvar sys_usenoclockbutbenchmark for the dedicated server. Replaces the regular clock...
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index 65b9f8276b45b301be726d245ab5783fc209cde5..fde0f4e772663fda706ba93d931e3c56e4c1d4bb 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
@@ -1,6 +1,10 @@
 
 // Written by Forest Hale 2003-06-15 and placed into public domain.
 
+#ifndef STANDALONETEST
+#include "quakedef.h"
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
@@ -23,7 +27,6 @@
 
 // for Z_Malloc/Z_Free in quake
 #ifndef STANDALONETEST
-#include "quakedef.h"
 #include "zone.h"
 #include "sys.h"
 #include "netconn.h"
 
 #define SOCKETERRNO WSAGetLastError()
 
+#define IOC_VENDOR 0x18000000
+#define _WSAIOW(x,y) (IOC_IN|(x)|(y))
+#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
+
 #define SOCKLEN_T int
 #elif defined(__MORPHOS__)
 #define ioctlsocket IoctlSocket
@@ -136,6 +143,9 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def
        // try to parse as dotted decimal ipv4 address first
        // note this supports partial ip addresses
        d1 = d2 = d3 = d4 = 0;
+#if _MSC_VER >= 1400
+#define sscanf sscanf_s
+#endif
        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
@@ -246,7 +256,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
                {
                        if (stringbuffersize >= 12)
                        {
-                               sprintf(string, "local:%d", (int)address->addressdata.loop.port);
+                               dpsnprintf(string, stringbuffersize, "local:%d", (int)address->addressdata.loop.port);
                                return 1;
                        }
                }
@@ -264,7 +274,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
                {
                        if (stringbuffersize >= 22)
                        {
-                               sprintf(string, "%d.%d.%d.%d:%d", (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));
+                               dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d:%d", (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));
                                return 1;
                        }
                }
@@ -272,7 +282,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
                {
                        if (stringbuffersize >= 16)
                        {
-                               sprintf(string, "%d.%d.%d.%d", (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3]);
+                               dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d", (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3]);
                                return 1;
                        }
                }
@@ -282,7 +292,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
                {
                        if (stringbuffersize >= 88)
                        {
-                               sprintf(string, "[%x:%x:%x:%x:%x:%x:%x:%x]:%d", (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));
+                               dpsnprintf(string, stringbuffersize, "[%x:%x:%x:%x:%x:%x:%x:%x]:%d", (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));
                                return 1;
                        }
                }
@@ -290,7 +300,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin
                {
                        if (stringbuffersize >= 80)
                        {
-                               sprintf(string, "%x:%x:%x:%x:%x:%x:%x:%x", (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]);
+                               dpsnprintf(string, stringbuffersize, "%x:%x:%x:%x:%x:%x:%x:%x", (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]);
                                return 1;
                        }
                }
@@ -509,7 +519,7 @@ void LHNET_SleepUntilPacket_Microseconds(int microseconds)
                {
                        if (lastfd < s->inetsocket)
                                lastfd = s->inetsocket;
-                       FD_SET(s->inetsocket, &fdreadset);
+                       FD_SET((unsigned int)s->inetsocket, &fdreadset);
                }
        }
        tv.tv_sec = microseconds / 1000000;
@@ -571,6 +581,7 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                {
 #ifdef WIN32
                                        u_long _true = 1;
+                                       u_long _false = 0;
 #else
                                        char _true = 1;
 #endif
@@ -588,6 +599,10 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                                        lhnetsocket->prev = lhnetsocket->next->prev;
                                                        lhnetsocket->next->prev = lhnetsocket;
                                                        lhnetsocket->prev->next = lhnetsocket;
+#ifdef WIN32
+                                                       if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
+                                                               Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
+#endif
                                                        return lhnetsocket;
                                                }
                                                else
@@ -773,7 +788,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                {
                        if (SOCKETERRNO == EWOULDBLOCK)
                                return 0;
-                       Con_Printf("LHNET_Read: sendto returned error: %s\n", LHNETPRIVATE_StrError());
+                       Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
                }
        }
        else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
@@ -783,7 +798,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                {
                        if (SOCKETERRNO == EWOULDBLOCK)
                                return 0;
-                       Con_Printf("LHNET_Read: sendto returned error: %s\n", LHNETPRIVATE_StrError());
+                       Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
                }
        }
        return value;