ipfilter.h

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 //! @file ipfilter.h Interface for IpFilterBase and IpFilter classes
00020 
00021 #ifndef __IPFILTER_H__
00022 #define __IPFILTER_H__
00023 
00024 #include <hn/osdep.h>
00025 #include <boost/function.hpp>
00026 
00027 template<typename T> class Range;
00028 template<typename T> class RangeList;
00029 
00030 /**
00031  * Abstract base class for IpFilter engine, declares a pure virtual function
00032  * isAllowed(), which derived classes must override and implement checks for
00033  * filtering ip addresses.
00034  */
00035 class IpFilterBase {
00036 public:
00037         IpFilterBase();
00038         virtual ~IpFilterBase();
00039 
00040         /**
00041          * Check if a given ip address is allowed to be connected to / to
00042          * connect us. The ip address is in host byte order.
00043          */
00044         virtual bool isAllowed(uint32_t ip) = 0;
00045 private:
00046         IpFilterBase(const IpFilterBase&);
00047         IpFilterBase& operator=(const IpFilterBase&);
00048 };
00049 
00050 class IpFilter : public IpFilterBase {
00051 public:
00052         IpFilter();
00053         ~IpFilter();
00054         virtual bool isAllowed(uint32_t ip);
00055         void load(const std::string &file);
00056 private:
00057         typedef std::runtime_error ParseError;
00058         typedef boost::function<void (const std::string&)> ParseFunc;
00059 
00060         void parseMldonkeyLine(const std::string &buf);
00061         void parseMuleLine(const std::string &buf);
00062         ParseFunc getParseFunc(const std::string &buf);
00063         std::auto_ptr<RangeList<Range<uint32_t> > > m_list;
00064 };
00065 
00066 #endif