]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - lhnet.c
FreeBSD support
[xonotic/darkplaces.git] / lhnet.c
diff --git a/lhnet.c b/lhnet.c
index 34052e14c55708895cafead6f5e51c1b5aaed1d8..1060341f10abbbaa71d5f3b3e4fde84814288647 100644 (file)
--- a/lhnet.c
+++ b/lhnet.c
@@ -8,13 +8,13 @@
 #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
@@ -24,6 +24,8 @@
 #include "sys.h"
 #include "netconn.h"
 #else
+#define Con_Print printf
+#define Con_Printf printf
 #define Z_Malloc malloc
 #define Z_Free free
 #endif
@@ -444,17 +446,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;
@@ -464,8 +459,15 @@ 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, (void *)&lhnetsocket->address.addressdata, namelen) != -1)
                                                {
+                                                       getsockname(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, &namelen);
                                                        lhnetsocket->next = &lhnet_socketlist;
                                                        lhnetsocket->prev = lhnetsocket->next->prev;
                                                        lhnetsocket->next->prev = lhnetsocket;
@@ -485,7 +487,11 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
                                }
                                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;
@@ -559,7 +565,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                                continue;
                        }
 #ifndef STANDALONETEST
-                       if (p->sentdoubletime && Sys_DoubleTime() < p->sentdoubletime)
+                       if (cl_netlocalping.value && (Sys_DoubleTime() - cl_netlocalping.value * 1000.0) < p->sentdoubletime)
                                continue;
 #endif
                        if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
@@ -600,7 +606,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                        switch (e)
                        {
                                case WSAECONNREFUSED:
-                                       Con_Printf("Connection refused\n");
+                                       Con_Print("Connection refused\n");
                                        return 0;
                        }
 #else
@@ -609,7 +615,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                        switch (errno)
                        {
                                case ECONNREFUSED:
-                                       Con_Printf("Connection refused\n");
+                                       Con_Print("Connection refused\n");
                                        return 0;
                        }
 #endif
@@ -635,7 +641,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                        switch (e)
                        {
                                case WSAECONNREFUSED:
-                                       Con_Printf("Connection refused\n");
+                                       Con_Print("Connection refused\n");
                                        return 0;
                        }
 #else
@@ -644,7 +650,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength,
                        switch (errno)
                        {
                                case ECONNREFUSED:
-                                       Con_Printf("Connection refused\n");
+                                       Con_Print("Connection refused\n");
                                        return 0;
                        }
 #endif
@@ -675,8 +681,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng
                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;
+               p->sentdoubletime = Sys_DoubleTime();
 #endif
                value = contentlength;
        }