X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=lhnet.c;h=cb87b6d319e2ae866f94ca9bf11d1856120aa9cd;hb=cfee52a1ec9db338098789cae89ae5cf1f7a6fbf;hp=3ee6267d729802dc65bbd973adf7445ae89a52ed;hpb=fb6f77ec0022e825089b226e2daa3870b4ceeb32;p=xonotic%2Fdarkplaces.git diff --git a/lhnet.c b/lhnet.c index 3ee6267d..cb87b6d3 100644 --- a/lhnet.c +++ b/lhnet.c @@ -1,17 +1,20 @@ // Written by Forest Hale 2003-06-15 and placed into public domain. -#ifdef SUPPORTIPV6 #ifdef WIN32 +#ifdef _MSC_VER +#pragma comment(lib, "ws2_32.lib") +#endif +# ifdef SUPPORTIPV6 // Windows XP or higher is required for getaddrinfo, but the inclusion of wspiapi provides fallbacks for older versions # define _WIN32_WINNT 0x0501 +# endif # include # include # ifdef USE_WSPIAPI_H # include # endif #endif -#endif #ifndef STANDALONETEST #include "quakedef.h" @@ -21,10 +24,7 @@ #include #include #include -#ifdef WIN32 -#include -#include -#else +#ifndef WIN32 #include #include #include @@ -124,7 +124,7 @@ int LHNETADDRESS_FromPort(lhnetaddress_t *vaddress, lhnetaddresstype_t addressty switch(addresstype) { default: - return 0; + break; case LHNETADDRESSTYPE_LOOP: // local:port (loopback) memset(address, 0, sizeof(*address)); @@ -389,8 +389,12 @@ int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int de address->addresstype = LHNETADDRESSTYPE_NONE; port = 0; colon = strrchr(string, ':'); - if (colon) + if (colon && (colon == strchr(string, ':') || (string[0] == '[' && colon - string > 0 && colon[-1] == ']'))) + // EITHER: colon is the ONLY colon OR: colon comes after [...] delimited IPv6 address + // fixes misparsing of IPv6 addresses without port + { port = atoi(colon + 1); + } else colon = string + strlen(string); if (port == 0) @@ -608,7 +612,7 @@ int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address) return LHNETADDRESSTYPE_NONE; } -const char *LHNETADDRESS_GetInterfaceName(const lhnetaddress_t *vaddress) +const char *LHNETADDRESS_GetInterfaceName(const lhnetaddress_t *vaddress, char *ifname, size_t ifnamelength) { #ifdef SUPPORTIPV6 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress; @@ -617,8 +621,6 @@ const char *LHNETADDRESS_GetInterfaceName(const lhnetaddress_t *vaddress) { #ifndef _WIN32 - static char ifname [IF_NAMESIZE]; - if (if_indextoname(address->addr.in6.sin6_scope_id, ifname) == ifname) return ifname; @@ -627,9 +629,7 @@ const char *LHNETADDRESS_GetInterfaceName(const lhnetaddress_t *vaddress) // The Win32 API doesn't have if_indextoname() until Windows Vista, // but luckily it just uses the interface ID as the interface name - static char ifname [16]; - - if (dpsnprintf(ifname, sizeof(ifname), "%lu", address->addr.in6.sin6_scope_id) > 0) + if (dpsnprintf(ifname, ifnamelength, "%lu", address->addr.in6.sin6_scope_id) > 0) return ifname; #endif @@ -839,7 +839,11 @@ void LHNET_SleepUntilPacket_Microseconds(int microseconds) { if (lastfd < s->inetsocket) lastfd = s->inetsocket; +#if defined(WIN32) && !defined(_MSC_VER) + FD_SET((int)s->inetsocket, &fdreadset); +#else FD_SET((unsigned int)s->inetsocket, &fdreadset); +#endif } } tv.tv_sec = microseconds / 1000000; @@ -1088,7 +1092,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, address->port = ntohs(address->addr.in.sin_port); return value; } - else if (value == -1) + else if (value < 0) { int e = SOCKETERRNO; if (e == EWOULDBLOCK) @@ -1099,7 +1103,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, Con_Print("Connection refused\n"); return 0; } - Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError()); + Con_DPrintf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError()); } } #ifdef SUPPORTIPV6 @@ -1108,7 +1112,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, SOCKLEN_T inetaddresslength; address->addresstype = LHNETADDRESSTYPE_NONE; inetaddresslength = sizeof(address->addr.in6); - value = recvfrom(lhnetsocket->inetsocket, (char *)content, maxcontentlength, 0, &address->addr.sock, &inetaddresslength); + value = recvfrom(lhnetsocket->inetsocket, (char *)content, maxcontentlength, LHNET_RECVFROM_FLAGS, &address->addr.sock, &inetaddresslength); if (value > 0) { address->addresstype = LHNETADDRESSTYPE_INET6; @@ -1126,7 +1130,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, Con_Print("Connection refused\n"); return 0; } - Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError()); + Con_DPrintf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError()); } } #endif @@ -1167,7 +1171,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng { if (SOCKETERRNO == EWOULDBLOCK) return 0; - Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError()); + Con_DPrintf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError()); } } #ifdef SUPPORTIPV6 @@ -1178,7 +1182,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng { if (SOCKETERRNO == EWOULDBLOCK) return 0; - Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError()); + Con_DPrintf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError()); } } #endif