Pol  Revision:cb584c9
bannedips.cpp
Go to the documentation of this file.
1 
7 #include "bannedips.h"
8 
9 #include <string>
10 
11 #include "../../clib/cfgelem.h"
12 #include "../../clib/cfgfile.h"
13 #include "../../clib/fileutil.h"
14 #include "../globals/network.h"
15 #include "client.h"
16 
17 namespace Pol
18 {
19 namespace Network
20 {
21 bool is_banned_ip( Client* client )
22 {
23  if ( Core::networkManager.banned_ips.empty() )
24  return false;
25 
26  for ( std::vector<IPRule>::iterator itr = Core::networkManager.banned_ips.begin();
27  itr != Core::networkManager.banned_ips.end(); ++itr )
28  {
29  unsigned int addr1part, addr2part;
30  struct sockaddr_in* sockin = reinterpret_cast<struct sockaddr_in*>( &client->ipaddr );
31  addr1part = itr->ipMatch & itr->ipMask;
32 
33 #ifdef _WIN32
34  addr2part = sockin->sin_addr.S_un.S_addr & itr->ipMask;
35 #else
36  addr2part = sockin->sin_addr.s_addr & itr->ipMask;
37 #endif
38 
39  if ( addr1part == addr2part )
40  return true;
41  }
42  return false;
43 }
44 
45 void read_bannedips_config( bool initial_load )
46 {
47  if ( !initial_load )
48  {
50  }
51  if ( !Clib::FileExists( "config/bannedips.cfg" ) )
52  return;
53 
54  Clib::ConfigFile cf( "config/bannedips.cfg" );
55  Clib::ConfigElem elem;
56 
57  while ( cf.read( elem ) )
58  {
59  IPRule CurrentEntry;
60  std::string iptext = elem.remove_string( "IPMatch" );
61  std::string::size_type delim = iptext.find_first_of( '/' );
62  if ( delim != std::string::npos )
63  {
64  std::string ipaddr_str = iptext.substr( 0, delim );
65  std::string ipmask_str = iptext.substr( delim + 1 );
66  CurrentEntry.ipMatch = inet_addr( ipaddr_str.c_str() );
67  CurrentEntry.ipMask = inet_addr( ipmask_str.c_str() );
68  Core::networkManager.banned_ips.push_back( CurrentEntry );
69  }
70  else
71  {
72  std::string ipmask_str = "255.255.255.255";
73  CurrentEntry.ipMatch = inet_addr( iptext.c_str() );
74  CurrentEntry.ipMask = inet_addr( ipmask_str.c_str() );
75  Core::networkManager.banned_ips.push_back( CurrentEntry );
76  }
77  }
78 }
79 }
80 }
std::string remove_string(const char *propname)
Definition: cfgfile.cpp:381
sockaddr ipaddr
Definition: client.h:213
unsigned int ipMask
Definition: bannedips.h:21
void read_bannedips_config(bool initial_load)
Definition: bannedips.cpp:45
NetworkManager networkManager
Definition: network.cpp:28
unsigned int ipMatch
Definition: bannedips.h:20
std::vector< Network::IPRule > banned_ips
Definition: network.h:101
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
bool read(ConfigElem &elem)
Definition: cfgfile.cpp:1015
Definition: berror.cpp:12
bool is_banned_ip(Client *client)
Definition: bannedips.cpp:21