diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index 2056bd9..3a27e9c 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -127,17 +127,19 @@ void NetadrToSockadr(const netadr_t *a, struct sockaddr *s) { Q_memset(s, 0, sizeof(*s)); + auto s_in = (sockaddr_in *)s; + switch (a->type) { case NA_BROADCAST: - ((struct sockaddr_in *)s)->sin_family = AF_INET; - ((struct sockaddr_in *)s)->sin_addr.s_addr = INADDR_BROADCAST; - ((struct sockaddr_in *)s)->sin_port = a->port; + s_in->sin_family = AF_INET; + s_in->sin_addr.s_addr = INADDR_BROADCAST; + s_in->sin_port = a->port; break; case NA_IP: - ((struct sockaddr_in *)s)->sin_family = AF_INET; - ((struct sockaddr_in *)s)->sin_addr.s_addr = *(int *)&a->ip; - ((struct sockaddr_in *)s)->sin_port = a->port; + s_in->sin_family = AF_INET; + s_in->sin_addr.s_addr = *(int *)&a->ip; + s_in->sin_port = a->port; break; #ifdef _WIN32 case NA_IPX: @@ -349,68 +351,26 @@ char *NET_BaseAdrToString(netadr_t& a) qboolean NET_StringToSockaddr(const char *s, struct sockaddr *sadr) { - struct hostent *h; - char *colon; - int val; - char copy[128]; - Q_memset(sadr, 0, sizeof(*sadr)); + #ifdef _WIN32 + // IPX support. if (Q_strlen(s) >= 24 && s[8] == ':' && s[21] == ':') { sadr->sa_family = AF_IPX; - - copy[2] = 0; - - copy[0] = s[0]; - copy[1] = s[1]; - sscanf(copy, "%x", &val); - sadr->sa_data[0] = (char)val; - - copy[0] = s[2]; - copy[1] = s[3]; - sscanf(copy, "%x", &val); - sadr->sa_data[1] = (char)val; - - copy[0] = s[4]; - copy[1] = s[5]; - sscanf(copy, "%x", &val); - sadr->sa_data[2] = (char)val; - - copy[0] = s[6]; - copy[1] = s[7]; - sscanf(copy, "%x", &val); - sadr->sa_data[3] = (char)val; - - copy[0] = s[9]; - copy[1] = s[10]; - sscanf(copy, "%x", &val); - sadr->sa_data[4] = (char)val; - - copy[0] = s[11]; - copy[1] = s[12]; - sscanf(copy, "%x", &val); - sadr->sa_data[5] = (char)val; - - copy[0] = s[13]; - copy[1] = s[14]; - sscanf(copy, "%x", &val); - sadr->sa_data[6] = (char)val; - - copy[0] = s[15]; - copy[1] = s[16]; - sscanf(copy, "%x", &val); - sadr->sa_data[7] = (char)val; - - copy[0] = s[17]; - copy[1] = s[18]; - sscanf(copy, "%x", &val); - sadr->sa_data[8] = (char)val; - - copy[0] = s[19]; - copy[1] = s[20]; - sscanf(copy, "%x", &val); - sadr->sa_data[9] = (char)val; + int val = 0; + for(int i = 0; i < 20; i+=2) + { + if (s[i] == ':') + { + --i; // Skip one char on next iteration. + continue; + } + // Convert from hexademical represantation to sockaddr + char temp[3] = { s[i], s[i + 1], '\0' }; + sscanf(temp, "%x", &val); + sadr->sa_data[i / 2] = (char)val; + } sscanf(s + 22, "%u", &val); *(uint16 *)&sadr->sa_data[10] = htons(val); @@ -419,36 +379,38 @@ qboolean NET_StringToSockaddr(const char *s, struct sockaddr *sadr) } #endif // _WIN32 - ((sockaddr_in *)sadr)->sin_family = AF_INET; - ((sockaddr_in *)sadr)->sin_port = 0; + auto *sadr_in = (sockaddr_in *)sadr; + sadr_in->sin_family = AF_INET; + + char copy[128]; Q_strncpy(copy, s, sizeof(copy) - 1); copy[sizeof(copy) - 1] = 0; // Parse port - colon = copy; - while (*colon != 0) + char *colon = Q_strchr(copy, ':'); + if(colon != nullptr) { - if (*colon == ':') - { - *colon = 0; - val = Q_atoi(colon + 1); - ((sockaddr_in *)sadr)->sin_port = htons(val); - } - colon++; + *colon = '\0'; + sadr_in->sin_port = htons(Q_atoi(colon + 1)); + } + else + { + sadr_in->sin_port = 0; } // Parse address - ((sockaddr_in *)sadr)->sin_addr.s_addr = inet_addr(copy); - if (((sockaddr_in *)sadr)->sin_addr.s_addr == INADDR_NONE) + sadr_in->sin_addr.s_addr = inet_addr(copy); + if (sadr_in->sin_addr.s_addr == INADDR_NONE) { - h = CRehldsPlatformHolder::get()->gethostbyname(copy); + struct hostent *host = CRehldsPlatformHolder::get()->gethostbyname(copy); - if (h == NULL || h->h_addr == NULL) + if (host == nullptr || host->h_addr == nullptr) { return FALSE; } - ((sockaddr_in *)sadr)->sin_addr.s_addr = *(uint32 *)h->h_addr; + + sadr_in->sin_addr.s_addr = *(uint32 *)host->h_addr; } return TRUE; } @@ -563,10 +525,7 @@ void NET_TransferRawData(sizebuf_t *msg, unsigned char *pStart, int nSize) qboolean NET_GetLoopPacket(netsrc_t sock, netadr_t *in_from_, sizebuf_t *msg) { - int i; - loopback_t *loop; - - loop = &loopbacks[sock]; + loopback_t *loop = &loopbacks[sock]; if (loop->send - loop->get > 4) { @@ -575,7 +534,7 @@ qboolean NET_GetLoopPacket(netsrc_t sock, netadr_t *in_from_, sizebuf_t *msg) if (loop->get < loop->send) { - i = loop->get & (MAX_LOOPBACK - 1); + int i = loop->get & (MAX_LOOPBACK - 1); loop->get++; NET_TransferRawData(msg, loop->msgs[i].data, loop->msgs[i].datalen); @@ -590,14 +549,11 @@ qboolean NET_GetLoopPacket(netsrc_t sock, netadr_t *in_from_, sizebuf_t *msg) void NET_SendLoopPacket(netsrc_t sock, int length, void *data, const netadr_t& to) { - int i; - loopback_t *loop; - NET_ThreadLock(); - loop = &loopbacks[sock ^ 1]; + loopback_t *loop = &loopbacks[sock ^ 1]; - i = loop->send & (MAX_LOOPBACK - 1); + int i = loop->send & (MAX_LOOPBACK - 1); loop->send++; #ifdef REHLDS_CHECKS @@ -625,15 +581,10 @@ NOXREF int NET_CountLaggedList(packetlag_t *pList) { NOXREFCHECK; - int c; - packetlag_t *p; - - c = 0; - p = pList->pNext; - while (p && p != pList) + int c = 0; + for (packetlag_t* p = pList->pNext; p && p != pList; p = p->pNext) { c++; - p = p->pNext; } return c; @@ -641,12 +592,10 @@ NOXREF int NET_CountLaggedList(packetlag_t *pList) void NET_ClearLaggedList(packetlag_t *pList) { - packetlag_t *p, *n; - - p = pList->pNext; + packetlag_t *p = pList->pNext; while (p && p != pList) { - n = p->pNext; + packetlag_t *n = p->pNext; NET_RemoveFromPacketList(p); if (p->pPacketData) { @@ -687,11 +636,8 @@ void NET_AddToLagged(netsrc_t sock, packetlag_t *pList, packetlag_t *pPacket, ne void NET_AdjustLag() { static double lasttime = realtime; - double dt; - float diff; - float converge; - dt = realtime - lasttime; + double dt = realtime - lasttime; if (dt <= 0.0) { dt = 0.0; @@ -709,8 +655,8 @@ void NET_AdjustLag() { if (fakelag.value != gFakeLag) { - diff = fakelag.value - gFakeLag; - converge = dt * 200.0; + float diff = fakelag.value - gFakeLag; + float converge = dt * 200.0; if (fabs(diff) < converge) converge = fabs(diff); if (diff < 0.0) @@ -728,19 +674,13 @@ void NET_AdjustLag() qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_t *data) { - packetlag_t *pNewPacketLag; - packetlag_t *pPacket; - float curtime; - int ninterval; - static int losscount[NS_MAX]; - if (gFakeLag <= 0.0) { NET_ClearLagData(TRUE, TRUE); return newdata; } - curtime = realtime; + float curtime = realtime; #ifdef REHLDS_FIXES if (newdata && data) #else @@ -751,10 +691,11 @@ qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_ { if (allow_cheats) { + static int losscount[NS_MAX] = {}; ++losscount[sock]; if (fakeloss.value <= 0.0f) { - ninterval = fabs(fakeloss.value); + int ninterval = fabs(fakeloss.value); if (ninterval < 2) ninterval = 2; if ((losscount[sock] % ninterval) == 0) @@ -772,11 +713,11 @@ qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_ } } - pNewPacketLag = (packetlag_t *)Mem_ZeroMalloc(sizeof(packetlag_t)); + packetlag_t *pNewPacketLag = (packetlag_t *)Mem_ZeroMalloc(sizeof(packetlag_t)); NET_AddToLagged(sock, &g_pLagData[sock], pNewPacketLag, from, *data, curtime); } - pPacket = g_pLagData[sock].pNext; + packetlag_t *pPacket = g_pLagData[sock].pNext; while (pPacket != &g_pLagData[sock]) { @@ -801,14 +742,13 @@ qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_ void NET_FlushSocket(netsrc_t sock) { - struct sockaddr from; - socklen_t fromlen; - SOCKET net_socket; - unsigned char buf[MAX_UDP_PACKET]; - - net_socket = ip_sockets[sock]; + SOCKET net_socket = ip_sockets[sock]; if (net_socket != INV_SOCK) { + struct sockaddr from; + socklen_t fromlen; + unsigned char buf[MAX_UDP_PACKET]; + fromlen = 16; while (CRehldsPlatformHolder::get()->recvfrom(net_socket, (char*)buf, sizeof buf, 0, &from, &fromlen) > 0) ; @@ -817,23 +757,20 @@ void NET_FlushSocket(netsrc_t sock) qboolean NET_GetLong(unsigned char *pData, int size, int *outSize) { - unsigned int packetNumber; - unsigned int packetCount; - int sequenceNumber; - unsigned char packetID; static int gNetSplitFlags[NET_WS_MAX_FRAGMENTS]; SPLITPACKET *pHeader = (SPLITPACKET *) pData; - sequenceNumber = pHeader->sequenceNumber; - packetID = pHeader->packetID; - packetCount = packetID & 0xF; - packetNumber = (unsigned int)packetID >> 4; + int sequenceNumber = pHeader->sequenceNumber; + unsigned char packetID = pHeader->packetID; + unsigned int packetCount = packetID & 0xF; + unsigned int packetNumber = (unsigned int)packetID >> 4; if (packetNumber >= NET_WS_MAX_FRAGMENTS || packetCount > NET_WS_MAX_FRAGMENTS) { Con_NetPrintf("Malformed packet number (%i/%i)\n", packetNumber + 1, packetCount); return FALSE; } + if (gNetSplit.currentSequence == -1 || sequenceNumber != gNetSplit.currentSequence) { gNetSplit.currentSequence = pHeader->sequenceNumber; @@ -924,18 +861,16 @@ qboolean NET_GetLong(unsigned char *pData, int size, int *outSize) qboolean NET_QueuePacket(netsrc_t sock) { int ret = -1; - struct sockaddr from; - socklen_t fromlen; - SOCKET net_socket; - int protocol; unsigned char buf[MAX_UDP_PACKET]; #ifdef _WIN32 - for (protocol = 0; protocol < 2; protocol++) + for (int protocol = 0; protocol < 2; protocol++) #else - for (protocol = 0; protocol < 1; protocol++) + for (int protocol = 0; protocol < 1; protocol++) #endif // _WIN32 { + SOCKET net_socket; + if (protocol == 0) net_socket = ip_sockets[sock]; #ifdef _WIN32 @@ -946,7 +881,8 @@ qboolean NET_QueuePacket(netsrc_t sock) if (net_socket == INV_SOCK) continue; - fromlen = sizeof(from); + struct sockaddr from; + socklen_t fromlen = sizeof(from); ret = CRehldsPlatformHolder::get()->recvfrom(net_socket, (char *)buf, sizeof buf, 0, &from, &fromlen); if (ret == -1) { @@ -1082,12 +1018,9 @@ DLL_EXPORT int NET_Sleep_Timeout() int NET_Sleep() { fd_set fdset; - struct timeval tv; - SOCKET number; - FD_ZERO(&fdset); - number = 0; + SOCKET number = 0; for (int sock = 0; sock < NS_MAX; sock++) { SOCKET net_socket = ip_sockets[sock]; @@ -1111,6 +1044,7 @@ int NET_Sleep() #endif // _WIN32 } + struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 20 * 1000; @@ -1249,7 +1183,6 @@ void NET_FreeMsg(net_messages_t *pmsg) qboolean NET_GetPacket(netsrc_t sock) { - net_messages_t *pmsg; qboolean bret; NET_AdjustLag(); @@ -1281,7 +1214,7 @@ qboolean NET_GetPacket(netsrc_t sock) return bret; } - pmsg = messages[sock]; + net_messages_t *pmsg = messages[sock]; if (pmsg) { net_message.cursize = pmsg->buffersize; @@ -1298,10 +1231,9 @@ qboolean NET_GetPacket(netsrc_t sock) void NET_AllocateQueues() { - net_messages_t *p; for (int i = 0; i < NUM_MSG_QUEUES; i++) { - p = (net_messages_t *)Mem_ZeroMalloc(sizeof(net_messages_t)); + net_messages_t *p = (net_messages_t *)Mem_ZeroMalloc(sizeof(net_messages_t)); p->buffer = (unsigned char *)Mem_ZeroMalloc(MSG_QUEUE_SIZE); p->preallocated = TRUE; p->next = normalqueue; @@ -1348,26 +1280,23 @@ int NET_SendLong(netsrc_t sock, SOCKET s, const char *buf, int len, int flags, c if (sock == NS_SERVER && len > MAX_ROUTEABLE_PACKET) { // yep - char packet[MAX_ROUTEABLE_PACKET]; - int totalSent, ret, size, packetCount, packetNumber; - SPLITPACKET *pPacket; - gSequenceNumber++; if (gSequenceNumber < 0) { gSequenceNumber = 1; } - pPacket = (SPLITPACKET *)packet; + char packet[MAX_ROUTEABLE_PACKET]; + SPLITPACKET *pPacket = (SPLITPACKET *)packet; pPacket->netID = NET_HEADER_FLAG_SPLITPACKET; pPacket->sequenceNumber = gSequenceNumber; - packetNumber = 0; - totalSent = 0; - packetCount = (len + SPLIT_SIZE - 1) / SPLIT_SIZE; + int packetNumber = 0; + int totalSent = 0; + int packetCount = (len + SPLIT_SIZE - 1) / SPLIT_SIZE; while (len > 0) { - size = Q_min(int(SPLIT_SIZE), len); + int size = Q_min(int(SPLIT_SIZE), len); pPacket->packetID = (packetNumber << 4) + packetCount; @@ -1388,7 +1317,7 @@ int NET_SendLong(netsrc_t sock, SOCKET s, const char *buf, int len, int flags, c NET_AdrToString(adr)); } - ret = CRehldsPlatformHolder::get()->sendto(s, packet, size + sizeof(SPLITPACKET), flags, to, tolen); + int ret = CRehldsPlatformHolder::get()->sendto(s, packet, size + sizeof(SPLITPACKET), flags, to, tolen); if (ret < 0) { return ret; @@ -1410,6 +1339,7 @@ int NET_SendLong(netsrc_t sock, SOCKET s, const char *buf, int len, int flags, c return nSend; } + void EXT_FUNC NET_SendPacket_api(unsigned int length, void *data, const netadr_t &to) { NET_SendPacket(NS_SERVER, length, data, to); @@ -1417,16 +1347,13 @@ void EXT_FUNC NET_SendPacket_api(unsigned int length, void *data, const netadr_t void NET_SendPacket(netsrc_t sock, int length, void *data, const netadr_t& to) { - int ret; - struct sockaddr addr; - SOCKET net_socket; - if (to.type == NA_LOOPBACK) { NET_SendLoopPacket(sock, length, data, to); return; } + SOCKET net_socket; if (to.type == NA_BROADCAST) { net_socket = ip_sockets[sock]; @@ -1458,9 +1385,10 @@ void NET_SendPacket(netsrc_t sock, int length, void *data, const netadr_t& to) Sys_Error("%s: bad address type", __func__); } + struct sockaddr addr; NetadrToSockadr(&to, &addr); - ret = NET_SendLong(sock, net_socket, (const char *)data, length, 0, &addr, sizeof(addr)); + int ret = NET_SendLong(sock, net_socket, (const char *)data, length, 0, &addr, sizeof(addr)); if (ret == -1) { int err = NET_GetLastError(); @@ -1499,13 +1427,11 @@ void NET_SendPacket(netsrc_t sock, int length, void *data, const netadr_t& to) } } + SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) { SOCKET newsocket; - struct sockaddr_in address; qboolean _true = TRUE; - int i = 1; - int err; #ifdef _WIN32 if ((newsocket = CRehldsPlatformHolder::get()->socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) @@ -1513,7 +1439,7 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) if ((newsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) #endif // _WIN32 { - err = NET_GetLastError(); + int err = NET_GetLastError(); if (err != WSAEAFNOSUPPORT) { Con_Printf("WARNING: UDP_OpenSocket: port: %d socket: %s", port, NET_ErrorString(err)); @@ -1531,6 +1457,7 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) return INV_SOCK; } + qboolean i = TRUE; #ifdef _WIN32 if (CRehldsPlatformHolder::get()->setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) == SOCKET_ERROR) #else @@ -1557,11 +1484,11 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) #ifndef _WIN32 if (COM_CheckParm("-tos")) { - i = 16; + int i = IPTOS_LOWDELAY; Con_Printf("Enabling LOWDELAY TOS option\n"); if (setsockopt(newsocket, IPPROTO_IP, IP_TOS, (char *)&i, sizeof(i)) == SOCKET_ERROR) { - err = NET_GetLastError(); + int err = NET_GetLastError(); if (err != WSAENOPROTOOPT) Con_Printf("WARNING: UDP_OpenSocket: port: %d setsockopt IP_TOS: %s\n", port, NET_ErrorString(err)); return INV_SOCK; @@ -1569,12 +1496,14 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) } #endif // _WIN32 + struct sockaddr_in address; + if (net_interface && *net_interface && Q_stricmp(net_interface, "localhost")) NET_StringToSockaddr(net_interface, (sockaddr *)&address); else address.sin_addr.s_addr = INADDR_ANY; - if (port == -1) + if (port == -1) // TODO: Always false? address.sin_port = 0; else address.sin_port = htons((u_short)port); @@ -1596,19 +1525,19 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) return INV_SOCK; } - i = COM_CheckParm("-loopback") != 0; + qboolean bLoopBack = COM_CheckParm("-loopback") != 0; #ifdef _WIN32 - if (CRehldsPlatformHolder::get()->setsockopt(newsocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&i, sizeof(i)) == SOCKET_ERROR) + if (CRehldsPlatformHolder::get()->setsockopt(newsocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&bLoopBack, sizeof(bLoopBack)) == SOCKET_ERROR) #else - if (setsockopt(newsocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&i, sizeof(i)) == SOCKET_ERROR) + if (setsockopt(newsocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&bLoopBack, sizeof(bLoopBack)) == SOCKET_ERROR) #endif // _WIN32 { Con_DPrintf("WARNING: UDP_OpenSocket: port %d setsockopt IP_MULTICAST_LOOP: %s\n", port, NET_ErrorString(NET_GetLastError())); } #if !defined _WIN32 && defined REHLDS_FIXES - i = IP_PMTUDISC_DONT; - if (setsockopt(newsocket, IPPROTO_IP, IP_MTU_DISCOVER, (char *)&i, sizeof(i)) == SOCKET_ERROR) + int j = IP_PMTUDISC_DONT; + if (setsockopt(newsocket, IPPROTO_IP, IP_MTU_DISCOVER, (char *)&j, sizeof(j)) == SOCKET_ERROR) { Con_Printf("WARNING: UDP_OpenSocket: port %d setsockopt IP_MTU_DISCOVER: %s\n", port, NET_ErrorString(NET_GetLastError())); } @@ -1620,25 +1549,22 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast) void NET_OpenIP() { //cvar_t *ip;//unused? - int port; - int dedicated; int sv_port = 0; int cl_port = 0; //int mc_port;//unused? - static qboolean bFirst = TRUE; - dedicated = g_pcls.state == ca_dedicated; + int dedicated = g_pcls.state == ca_dedicated; NET_ThreadLock(); if (ip_sockets[NS_SERVER] == INV_SOCK) { - port = (int)iphostport.value; + int port = (int)iphostport.value; - if (!port) + if (!NET_CheckPort(port)) { port = (int)hostport.value; - if (!port) + if (!NET_CheckPort(port)) { port = (int)defport.value; hostport.value = defport.value; @@ -1662,12 +1588,12 @@ void NET_OpenIP() if (ip_sockets[NS_CLIENT] == INV_SOCK) { - port = (int)ip_clientport.value; + int port = (int)ip_clientport.value; - if (!port) + if (!NET_CheckPort(port)) { port = (int)clientport.value; - if (!port) + if (!NET_CheckPort(port)) port = -1; } ip_sockets[NS_CLIENT] = NET_IPSocket(ipname.string, port, FALSE); @@ -1685,6 +1611,7 @@ void NET_OpenIP() NET_ThreadUnlock(); + static qboolean bFirst = TRUE; if (bFirst) { bFirst = FALSE; @@ -1699,7 +1626,6 @@ SOCKET NET_IPXSocket(int hostshort) int err; u_long optval = 1; SOCKET newsocket; - SOCKADDR_IPX address; if((newsocket = CRehldsPlatformHolder::get()->socket(PF_IPX, SOCK_DGRAM, NSPROTO_IPX)) == INVALID_SOCKET) { @@ -1731,6 +1657,7 @@ SOCKET NET_IPXSocket(int hostshort) return INV_SOCK; } + SOCKADDR_IPX address; address.sa_family = AF_IPX; Q_memset(address.sa_netnum, 0, 4); Q_memset(address.sa_nodenum, 0, 6); @@ -1751,20 +1678,17 @@ SOCKET NET_IPXSocket(int hostshort) void NET_OpenIPX() { - int port; - int dedicated; - - dedicated = g_pcls.state == ca_dedicated; + int dedicated = g_pcls.state == ca_dedicated; NET_ThreadLock(); if (ipx_sockets[NS_SERVER] == INV_SOCK) { - port = ipx_hostport.value; - if (!port) + int port = ipx_hostport.value; + if (!NET_CheckPort(port)) { port = hostport.value; - if (!port) + if (!NET_CheckPort(port)) { hostport.value = defport.value; port = defport.value; @@ -1782,11 +1706,11 @@ void NET_OpenIPX() if (ipx_sockets[NS_CLIENT] == INV_SOCK) { - port = ipx_clientport.value; - if (!port) + int port = ipx_clientport.value; + if (!NET_CheckPort(port)) { port = clientport.value; - if (!port) + if (!NET_CheckPort(port)) port = -1; } ipx_sockets[NS_CLIENT] = NET_IPXSocket(port); @@ -1898,10 +1822,18 @@ int NET_IsConfigured() return net_configured; } +bool NET_CheckPort(int port) +{ +#ifdef REHLDS_FIXES + return port > 0 && port <= USHRT_MAX; +#else + return port != 0; +#endif +} + void NET_Config(qboolean multiplayer) { static qboolean old_config; - static qboolean bFirst = TRUE; if (old_config == multiplayer) { @@ -1918,6 +1850,8 @@ void NET_Config(qboolean multiplayer) if (!noipx) NET_OpenIPX(); #endif //_WIN32 + + static qboolean bFirst = TRUE; if (bFirst) { bFirst = FALSE; @@ -2085,13 +2019,15 @@ void NET_Shutdown() NET_FlushQueues(); } -qboolean NET_JoinGroup(netsrc_t sock, netadr_t& addr) +NOXREF qboolean NET_JoinGroup(netsrc_t sock, netadr_t& addr) { + NOXREFCHECK; + ip_mreq mreq; - SOCKET net_socket = ip_sockets[sock]; SIN_SET_ADDR(&mreq.imr_multiaddr, *(unsigned int*)&addr.ip[0]); SIN_SET_ADDR(&mreq.imr_interface, 0); + SOCKET net_socket = ip_sockets[sock]; if (CRehldsPlatformHolder::get()->setsockopt(net_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mreq, sizeof(mreq)) == SOCKET_ERROR) { int err = NET_GetLastError(); @@ -2105,17 +2041,18 @@ qboolean NET_JoinGroup(netsrc_t sock, netadr_t& addr) return TRUE; } -qboolean NET_LeaveGroup(netsrc_t sock, netadr_t& addr) +NOXREF qboolean NET_LeaveGroup(netsrc_t sock, netadr_t& addr) { + NOXREFCHECK; + ip_mreq mreq; - SOCKET net_socket = ip_sockets[sock]; SIN_SET_ADDR(&mreq.imr_multiaddr, *(unsigned int*)&addr.ip[0]); SIN_SET_ADDR(&mreq.imr_interface, 0); - if (CRehldsPlatformHolder::get()->setsockopt(net_socket, 0, 6, (char *)&mreq, sizeof(mreq)) != SOCKET_ERROR) + SOCKET net_socket = ip_sockets[sock]; + if (CRehldsPlatformHolder::get()->setsockopt(net_socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) != SOCKET_ERROR) { - int err = NET_GetLastError(); - if (err != WSAEAFNOSUPPORT) + if (NET_GetLastError() != WSAEAFNOSUPPORT) { return FALSE; } diff --git a/rehlds/engine/net_ws.h b/rehlds/engine/net_ws.h index 7f57ecb..70b42ab 100644 --- a/rehlds/engine/net_ws.h +++ b/rehlds/engine/net_ws.h @@ -207,10 +207,11 @@ SOCKET NET_IPXSocket(int hostshort); void NET_OpenIPX(); void NET_GetLocalAddress(); int NET_IsConfigured(); +bool NET_CheckPort(int port); void NET_Config(qboolean multiplayer); void MaxPlayers_f(); void NET_Init(); void NET_ClearLagData(qboolean bClient, qboolean bServer); void NET_Shutdown(); -qboolean NET_JoinGroup(netsrc_t sock, netadr_t& addr); -qboolean NET_LeaveGroup(netsrc_t sock, netadr_t& addr); +NOXREF qboolean NET_JoinGroup(netsrc_t sock, netadr_t& addr); +NOXREF qboolean NET_LeaveGroup(netsrc_t sock, netadr_t& addr); diff --git a/rehlds/public/rehlds/osconfig.h b/rehlds/public/rehlds/osconfig.h index 0e31743..9f70ec9 100644 --- a/rehlds/public/rehlds/osconfig.h +++ b/rehlds/public/rehlds/osconfig.h @@ -71,6 +71,7 @@ #include #include #include + #include #include #include #include