From 46f30e215c0187092af42ec74bffa5d32ca28f18 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Fri, 12 Jan 2024 23:08:57 +0300 Subject: [PATCH] common: replace netadr.h with compatible definition from GPL Quake-2 sources --- common/netadr.h | 110 ++++++++++++++++++------------------- engine/common/masterlist.c | 6 +- engine/common/net_ws.c | 22 +------- 3 files changed, 59 insertions(+), 79 deletions(-) diff --git a/common/netadr.h b/common/netadr.h index 69735d57..937b9ad1 100644 --- a/common/netadr.h +++ b/common/netadr.h @@ -1,76 +1,74 @@ -/*** -* -* Copyright (c) 1996-2002, Valve LLC. All rights reserved. -* -* This product contains software technology licensed from Id -* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. -* All Rights Reserved. -* -* Use, distribution, and modification of this source code and/or resulting -* object code is restricted to non-commercial enhancements to products from -* Valve LLC. All other use, distribution, or modification is prohibited -* without written permission from Valve LLC. -* -****/ - -#ifndef NETADR_H -#define NETADR_H - -#include "build.h" +/* +Copyright (C) 1997-2001 Id Software, Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifndef NET_ADR_H +#define NET_ADR_H + #include STDINT_H -typedef enum +// net.h -- quake's interface to the networking layer +// a1ba: copied from Quake-2/qcommon/qcommon.h and modified to support IPv6 + +#define PORT_ANY -1 + +typedef enum {NA_LOOPBACK = 1, NA_BROADCAST, NA_IP, NA_IPX, NA_BROADCAST_IPX, NA_IP6, NA_MULTICAST_IP6} netadrtype_t; + +/* +Original Quake-2 structure: +typedef struct { - NA_UNUSED = 0, - NA_LOOPBACK, - NA_BROADCAST, - NA_IP, - NA_IPX, - NA_BROADCAST_IPX, - NA_IP6, - NA_MULTICAST_IP6, // all nodes multicast -} netadrtype_t; - -// Original structure: -// typedef struct netadr_s -// { -// netadrtype_t type; -// unsigned char ip[4]; -// unsigned char ipx[10]; -// unsigned short port; -// } netadr_t; + netadrtype_t type; + + byte ip[4]; + byte ipx[10]; + + unsigned short port; +} netadr_t; +*/ #pragma pack( push, 1 ) typedef struct netadr_s { union { + // IPv6 struct struct { - uint32_t type; - union - { - uint8_t ip[4]; - uint32_t ip4; - }; - uint8_t ipx[10]; + uint16_t type6; + uint8_t ip6[16]; }; struct { -#if XASH_LITTLE_ENDIAN - uint16_t type6; - uint8_t ip6[16]; -#elif XASH_BIG_ENDIAN - uint8_t ip6_0[2]; - uint16_t type6; - uint8_t ip6_2[14]; -#endif + uint32_t type; // must be netadrtype_t but will break with short enums + union + { + uint8_t ip[4]; + uint32_t ip4; // for easier conversions + }; + uint8_t ipx[10]; }; }; - uint16_t port; + uint16_t port; } netadr_t; #pragma pack( pop ) -STATIC_ASSERT( sizeof( netadr_t ) == 20, "invalid netadr_t size" ); +STATIC_ASSERT( sizeof( netadr_t ) == 20, "netadr_t isn't 20 bytes!" ); -#endif//NETADR_H +#endif // NET_ADR_H diff --git a/engine/common/masterlist.c b/engine/common/masterlist.c index 895e6713..5bbdaa88 100644 --- a/engine/common/masterlist.c +++ b/engine/common/masterlist.c @@ -50,7 +50,7 @@ static net_gai_state_t NET_GetMasterHostByName( master_t *m ) if( res == NET_EAI_OK ) return res; - m->adr.type = NA_UNUSED; + m->adr.type = 0; if( res == NET_EAI_NONAME ) Con_Reportf( "Can't resolve adr: %s\n", m->address ); @@ -266,7 +266,7 @@ static void NET_AddMaster( const char *addr, qboolean save ) master->sent = false; master->save = save; master->next = NULL; - master->adr.type = NA_UNUSED; + master->adr.type = 0; // link in if( last ) @@ -322,7 +322,7 @@ static void NET_ListMasters_f( void ) for( i = 1, list = ml.list; list; i++, list = list->next ) { Msg( "%d\t%s", i, list->address ); - if( list->adr.type != NA_UNUSED ) + if( list->adr.type != 0 ) Msg( "\t%s\n", NET_AdrToString( list->adr )); else Msg( "\n" ); } diff --git a/engine/common/net_ws.c b/engine/common/net_ws.c index b03273e2..de05146a 100644 --- a/engine/common/net_ws.c +++ b/engine/common/net_ws.c @@ -32,7 +32,6 @@ static const struct in6_addr in6addr_any; #define NET_USE_FRAGMENTS -#define PORT_ANY -1 #define MAX_LOOPBACK 4 #define MASK_LOOPBACK (MAX_LOOPBACK - 1) @@ -132,7 +131,7 @@ static CVAR_DEFINE_AUTO( net6_address, "0", FCVAR_PRIVILEGED|FCVAR_READ_ONLY, "c NET_ErrorString ==================== */ -char *NET_ErrorString( void ) +static char *NET_ErrorString( void ) { #if XASH_WIN32 int err = WSANOTINITIALISED; @@ -226,34 +225,17 @@ _inline qboolean NET_IsSocketValid( int socket ) void NET_NetadrToIP6Bytes( uint8_t *ip6, const netadr_t *adr ) { -#if XASH_LITTLE_ENDIAN memcpy( ip6, adr->ip6, sizeof( adr->ip6 )); -#elif XASH_BIG_ENDIAN - memcpy( ip6, adr->ip6_0, sizeof( adr->ip6_0 )); - memcpy( ip6 + sizeof( adr->ip6_0 ), adr->ip6_2, sizeof( adr->ip6_2 )); -#endif } void NET_IP6BytesToNetadr( netadr_t *adr, const uint8_t *ip6 ) { -#if XASH_LITTLE_ENDIAN memcpy( adr->ip6, ip6, sizeof( adr->ip6 )); -#elif XASH_BIG_ENDIAN - memcpy( adr->ip6_0, ip6, sizeof( adr->ip6_0 )); - memcpy( adr->ip6_2, ip6 + sizeof( adr->ip6_0 ), sizeof( adr->ip6_2 )); -#endif } -_inline int NET_NetadrIP6Compare( const netadr_t *a, const netadr_t *b ) +static int NET_NetadrIP6Compare( const netadr_t *a, const netadr_t *b ) { -#if XASH_LITTLE_ENDIAN return memcmp( a->ip6, b->ip6, sizeof( a->ip6 )); -#elif XASH_BIG_ENDIAN - int ret = memcmp( a->ip6_0, b->ip6_0, sizeof( a->ip6_0 )); - if( !ret ) - return memcmp( a->ip6_2, b->ip6_2, sizeof( a->ip6_2 )); - return ret; -#endif } /*