engine: network: fix address comparator

This commit is contained in:
Alibek Omarov 2022-12-15 12:24:56 +03:00
parent 9397301a73
commit f3400c983e

View File

@ -940,37 +940,53 @@ qboolean NET_CompareAdr( const netadr_t a, const netadr_t b )
NET_CompareAdrSort NET_CompareAdrSort
Network address sorting comparator Network address sorting comparator
guaranteed to return -1, 0 or 1
==================== ====================
*/ */
int NET_CompareAdrSort( const void *_a, const void *_b ) int NET_CompareAdrSort( const void *_a, const void *_b )
{ {
const netadr_t *a = _a; const netadr_t *a = _a, *b = _b;
const netadr_t *b = _b; int porta, portb, portdiff, addrdiff;
int portdiff;
if( a->type6 != b->type6 ) if( a->type6 != b->type6 )
return (int)a->type6 - (int)b->type6; return bound( -1, (int)a->type6 - (int)b->type6, 1 );
portdiff = (int)a->port - (int)b->port; porta = ntohs( a->port );
portb = ntohs( b->port );
if( porta < portb )
portdiff = -1;
else if( porta > portb )
portdiff = 1;
else
portdiff = 0;
switch( a->type6 ) switch( a->type6 )
{ {
case NA_IP6: case NA_IP6:
return NET_NetadrIP6Compare( a, b ); if(( addrdiff = NET_NetadrIP6Compare( a, b )))
return addrdiff;
// fallthrough
case NA_MULTICAST_IP6: case NA_MULTICAST_IP6:
return portdiff; return portdiff;
} }
// don't check for full type earlier, as it's value depends on v6 address
if( a->type != b->type ) if( a->type != b->type )
return (int)a->type - (int)b->type; return bound( -1, (int)a->type - (int)b->type, 1 );
switch( a->type ) switch( a->type )
{ {
case NA_IP: case NA_IP:
return memcmp( a->ip, b->ip, sizeof( a->ip )); if(( addrdiff = memcmp( a->ip, b->ip, sizeof( a->ipx ))))
case NA_IPX: return addrdiff;
return memcmp( a->ipx, b->ipx, sizeof( a->ipx )); // fallthrough
case NA_BROADCAST: case NA_BROADCAST:
return portdiff;
case NA_IPX:
if(( addrdiff = memcmp( a->ipx, b->ipx, sizeof( a->ipx ))))
return addrdiff;
// fallthrough
case NA_BROADCAST_IPX: case NA_BROADCAST_IPX:
return portdiff; return portdiff;
} }