polserver/pol-core/pol/network/bannedips.cpp
turleypol dbb25bdb62
Include and buildsystem cleanup (#45)
Include cleanup
Reworked buildsystem (Linux only)
Removed dynamic libs, Linux now again uses static linking (like windows)
2018-01-29 21:55:48 +01:00

80 lines
1.9 KiB
C++

/** @file
*
* @par History
*/
#include "bannedips.h"
#include <string>
#include "../../clib/cfgelem.h"
#include "../../clib/cfgfile.h"
#include "../../clib/fileutil.h"
#include "../globals/network.h"
#include "client.h"
namespace Pol
{
namespace Network
{
bool is_banned_ip( Client* client )
{
if ( Core::networkManager.banned_ips.empty() )
return false;
for ( std::vector<IPRule>::iterator itr = Core::networkManager.banned_ips.begin();
itr != Core::networkManager.banned_ips.end(); ++itr )
{
unsigned int addr1part, addr2part;
struct sockaddr_in* sockin = reinterpret_cast<struct sockaddr_in*>( &client->ipaddr );
addr1part = itr->ipMatch & itr->ipMask;
#ifdef _WIN32
addr2part = sockin->sin_addr.S_un.S_addr & itr->ipMask;
#else
addr2part = sockin->sin_addr.s_addr & itr->ipMask;
#endif
if ( addr1part == addr2part )
return true;
}
return false;
}
void read_bannedips_config( bool initial_load )
{
if ( !initial_load )
{
Core::networkManager.banned_ips.clear();
}
if ( !Clib::FileExists( "config/bannedips.cfg" ) )
return;
Clib::ConfigFile cf( "config/bannedips.cfg" );
Clib::ConfigElem elem;
while ( cf.read( elem ) )
{
IPRule CurrentEntry;
std::string iptext = elem.remove_string( "IPMatch" );
std::string::size_type delim = iptext.find_first_of( '/' );
if ( delim != std::string::npos )
{
std::string ipaddr_str = iptext.substr( 0, delim );
std::string ipmask_str = iptext.substr( delim + 1 );
CurrentEntry.ipMatch = inet_addr( ipaddr_str.c_str() );
CurrentEntry.ipMask = inet_addr( ipmask_str.c_str() );
Core::networkManager.banned_ips.push_back( CurrentEntry );
}
else
{
std::string ipmask_str = "255.255.255.255";
CurrentEntry.ipMatch = inet_addr( iptext.c_str() );
CurrentEntry.ipMask = inet_addr( ipmask_str.c_str() );
Core::networkManager.banned_ips.push_back( CurrentEntry );
}
}
}
}
}