From ddadef7a3cd39c13627e849b9bbf9cc7d52b866f Mon Sep 17 00:00:00 2001 From: thilo Date: Sun, 24 May 2009 16:58:08 +0000 Subject: - Introduce new NET_CompareBaseAdrMask for easy comparison of ip address ranges - Overhaul of the new banning functions: * basic check for redundant bans/exceptions * introduction of sv_banFile to make it possible to configure the file where to read bans and exceptions from * bans can now be deleted by giving address ranges, too. git-svn-id: svn://svn.icculus.org/quake3/trunk@1557 edf5b092-35ff-0310-97b2-ce42778d08ea --- code/qcommon/net_ip.c | 75 ++++++++++++++++++++++++++++++++++++++++++-------- code/qcommon/qcommon.h | 1 + 2 files changed, 64 insertions(+), 12 deletions(-) (limited to 'code/qcommon') diff --git a/code/qcommon/net_ip.c b/code/qcommon/net_ip.c index 848e8c5..b3fcfb0 100644 --- a/code/qcommon/net_ip.c +++ b/code/qcommon/net_ip.c @@ -382,39 +382,90 @@ qboolean Sys_StringToAdr( const char *s, netadr_t *a, netadrtype_t family ) { /* =================== -NET_CompareBaseAdr +NET_CompareBaseAdrMask -Compares without the port +Compare without port, and up to the bit number given in netmask. =================== */ -qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b) +qboolean NET_CompareBaseAdrMask(netadr_t a, netadr_t b, int netmask) { + qboolean differed; + byte cmpmask, *addra, *addrb; + int curbyte; + if (a.type != b.type) return qfalse; if (a.type == NA_LOOPBACK) return qtrue; - if (a.type == NA_IP) + if(a.type == NA_IP) { - if(!memcmp(a.ip, b.ip, sizeof(a.ip))) - return qtrue; + addra = (byte *) &a.ip; + addrb = (byte *) &b.ip; - return qfalse; + if(netmask < 0 || netmask > 32) + netmask = 32; } - - if (a.type == NA_IP6) + else if(a.type == NA_IP6) { - if(!memcmp(a.ip6, b.ip6, sizeof(a.ip6)) && a.scope_id == b.scope_id) - return qtrue; + addra = (byte *) &a.ip6; + addrb = (byte *) &b.ip6; + if(netmask < 0 || netmask > 128) + netmask = 128; + } + else + { + Com_Printf ("NET_CompareBaseAdr: bad address type\n"); return qfalse; } - Com_Printf ("NET_CompareBaseAdr: bad address type\n"); + differed = qfalse; + curbyte = 0; + + while(netmask > 7) + { + if(addra[curbyte] != addrb[curbyte]) + { + differed = qtrue; + break; + } + + curbyte++; + netmask -= 8; + } + + if(differed) + return qfalse; + + if(netmask) + { + cmpmask = (1 << netmask) - 1; + cmpmask <<= 8 - netmask; + + if((addra[curbyte] & cmpmask) == (addrb[curbyte] & cmpmask)) + return qtrue; + } + else + return qtrue; + return qfalse; } + +/* +=================== +NET_CompareBaseAdr + +Compares without the port +=================== +*/ +qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b) +{ + return NET_CompareBaseAdrMask(a, b, -1); +} + const char *NET_AdrToString (netadr_t a) { static char s[NET_ADDRSTRMAXLEN]; diff --git a/code/qcommon/qcommon.h b/code/qcommon/qcommon.h index 34cb2e9..34c8fac 100644 --- a/code/qcommon/qcommon.h +++ b/code/qcommon/qcommon.h @@ -168,6 +168,7 @@ void QDECL NET_OutOfBandPrint( netsrc_t net_socket, netadr_t adr, const char *f void QDECL NET_OutOfBandData( netsrc_t sock, netadr_t adr, byte *format, int len ); qboolean NET_CompareAdr (netadr_t a, netadr_t b); +qboolean NET_CompareBaseAdrMask(netadr_t a, netadr_t b, int netmask); qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b); qboolean NET_IsLocalAddress (netadr_t adr); const char *NET_AdrToString (netadr_t a); -- cgit v1.2.3