engine: common: add network address comparator function

This commit is contained in:
Alibek Omarov 2022-12-12 00:17:28 +03:00
parent 8fa0290e25
commit 18c94b6ec4
2 changed files with 44 additions and 1 deletions

View File

@ -931,11 +931,53 @@ qboolean NET_CompareAdr( const netadr_t a, const netadr_t b )
return true;
}
Con_DPrintf( S_ERROR "NET_CompareAdr: bad address type\n" );
return false;
}
/*
====================
NET_CompareAdrSort
Network address sorting comparator
====================
*/
int NET_CompareAdrSort( const void *_a, const void *_b )
{
const netadr_t *a = _a;
const netadr_t *b = _b;
int portdiff;
if( a->type6 != b->type6 )
return (int)a->type6 - (int)b->type6;
portdiff = (int)a->port - (int)b->port;
switch( a->type6 )
{
case NA_IP6:
return NET_NetadrIP6Compare( a, b );
case NA_MULTICAST_IP6:
return portdiff;
}
if( a->type != b->type )
return (int)a->type - (int)b->type;
switch( a->type )
{
case NA_IP:
return memcmp( a->ip, b->ip, sizeof( a->ip ));
case NA_IPX:
return memcmp( a->ipx, b->ipx, sizeof( a->ipx ));
case NA_BROADCAST:
case NA_BROADCAST_IPX:
return portdiff;
}
return 0;
}
/*
====================
NET_IsLocalAddress

View File

@ -60,6 +60,7 @@ qboolean NET_CompareClassBAdr( const netadr_t a, const netadr_t b );
qboolean NET_StringToAdr( const char *string, netadr_t *adr );
qboolean NET_StringToFilterAdr( const char *s, netadr_t *adr, uint *prefixlen );
int NET_StringToAdrNB( const char *string, netadr_t *adr );
int NET_CompareAdrSort( const void *_a, const void *_b );
qboolean NET_CompareAdr( const netadr_t a, const netadr_t b );
qboolean NET_CompareBaseAdr( const netadr_t a, const netadr_t b );
qboolean NET_CompareAdrByMask( const netadr_t a, const netadr_t b, uint prefixlen );