parse the IP only once when checking for bans

This commit is contained in:
Gunter Labes 2009-03-31 19:32:11 +00:00
parent 9833c0ed58
commit eea34de1c5
2 changed files with 13 additions and 13 deletions

View file

@ -143,7 +143,7 @@ namespace wesnothd {
read(cfg);
}
banned::ip_mask banned::parse_ip(const std::string& ip) const
ip_mask parse_ip(const std::string& ip)
{
// We use bit operations to construct the integer
// ip_mask is a pair: first is ip and second is mask
@ -272,13 +272,8 @@ namespace wesnothd {
return ip_ & mask & mask_;
}
bool banned::match_ip(const std::string& ip) const {
try {
ip_mask pair = parse_ip(ip);
return (ip_ & mask_) == (pair.first & mask_);
} catch (banned::error&) {
return false;
}
bool banned::match_ip(const ip_mask& pair) const {
return (ip_ & mask_) == (pair.first & mask_);
}
void ban_manager::read()
@ -609,7 +604,13 @@ namespace wesnothd {
std::string ban_manager::is_ip_banned(const std::string& ip) const
{
ban_set::const_iterator ban = std::find_if(bans_.begin(), bans_.end(), boost::bind(&banned::match_ip, boost::bind(&banned_ptr::get, _1), ip));
ip_mask pair;
try {
pair = parse_ip(ip);
} catch (banned::error&) {
return "";
}
ban_set::const_iterator ban = std::find_if(bans_.begin(), bans_.end(), boost::bind(&banned::match_ip, boost::bind(&banned_ptr::get, _1), pair));
if (ban == bans_.end()) return "";
return (*ban)->get_reason();
}

View file

@ -60,7 +60,9 @@ namespace wesnothd {
typedef std::list<banned_ptr> deleted_ban_list;
typedef std::priority_queue<banned_ptr,std::vector<banned_ptr>, banned_compare> ban_time_queue;
typedef std::map<std::string, size_t> default_ban_times;
typedef std::pair<unsigned int, unsigned int> ip_mask;
ip_mask parse_ip(const std::string&);
class banned {
unsigned int ip_;
@ -73,9 +75,6 @@ namespace wesnothd {
std::string group_;
std::string nick_;
static const std::string who_banned_default_;
typedef std::pair<unsigned int, unsigned int> ip_mask;
ip_mask parse_ip(const std::string&) const;
banned(const std::string& ip);
@ -110,7 +109,7 @@ namespace wesnothd {
bool match_group(const std::string& group) const
{ return group_ == group; }
bool match_ip(const std::string& ip) const;
bool match_ip(const ip_mask& ip) const;
unsigned int get_mask_ip(unsigned int) const;
unsigned int get_int_ip() const