Vidalia 0.2.15
Policy.h
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file Policy.h
00013 ** \brief Exit policy parsing
00014 */
00015 
00016 #ifndef _POLICY_H
00017 #define _POLICY_H
00018 
00019 #include <QCoreApplication>
00020 #include <QString>
00021 #include <QHostAddress>
00022 
00023 
00024 class Policy
00025 {
00026   Q_DECLARE_TR_FUNCTIONS(Policy)
00027 
00028 public:
00029   /** A set of possible actions for a policy */
00030   enum Action {
00031     Accept, /**< Connections matching this policy will be accepted. */
00032     Reject  /**< Connections matching this policy will be rejected. */
00033   };
00034   /** Special rule types. */
00035   enum SpecialPolicy {
00036     AcceptAll, /**< Accepts all connections. Equivalent to "accept *:*". */
00037     RejectAll  /**< Rejects all connections. Equivalent to "reject *:*". */
00038   };
00039   
00040   /** Default constructor. Creates an AcceptAll policy. */
00041   Policy();
00042   /** Parses the given policy, represented as a string. */
00043   Policy(QString policy);
00044   /** Parses the given portions of a policy string. */
00045   Policy(QString action, QString address, QString ports);
00046   /** Creates a policy of the given special type. */
00047   Policy(SpecialPolicy policy);
00048   /** Creates a policy using the specified information. */
00049   Policy(Action action, QHostAddress addr, uchar mask,
00050          quint16 fromPort, quint16 toPort = 0);
00051 
00052   /** Returns true if this policy matches <b>policy</b>. */
00053   bool matches(const Policy &policy) const;
00054   /** Returns true if this policy is identical to <b>policy</b>. */
00055   bool operator==(const Policy &policy) const;
00056 
00057   /** Parses the given policy string. */
00058   void fromString(QString policy);
00059   /** Converts this policy to a format Tor understands. */
00060   QString toString() const;
00061   /** Converts a string action to an Action enum value. */
00062   static Action toAction(QString action);
00063   
00064   /** Returns the action taken when this policy matches an address. */
00065   QString action() const;
00066   /** Returns the host address (including mask, if set) for this policy. */
00067   QString address() const;
00068   /** Returns the port or port range for this policy. */
00069   QString ports() const;
00070 
00071 private:
00072   Action _action; /**< The action to take for this policy. */
00073   QHostAddress _address; /**< Addresses to which this policy applies. */
00074   quint16 _fromPort;  /**< Start of a port range. */
00075   quint16 _toPort;  /**< End of a port range. */
00076   uchar _mask;  /**< Address mask. */
00077 };
00078 
00079 #endif
00080