Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 #ifndef _IP_ALLOW_H_
00032 #define _IP_ALLOW_H_
00033 
00034 #include "Main.h"
00035 #include "hdrs/HTTP.h"
00036 #include "ts/IpMap.h"
00037 #include "ts/Vec.h"
00038 #include "ProxyConfig.h"
00039 
00040 #include <string>
00041 #include <set>
00042 
00043 
00044 struct IpAllowUpdate;
00045 
00046 
00047 
00048 
00049 
00050 
00051 static uint64_t const IP_ALLOW_TIMEOUT = HRTIME_HOUR;
00052 
00053 
00054 
00055 
00056 struct AclRecord {
00057   uint32_t _method_mask;
00058   int _src_line;
00059   typedef std::set<std::string> MethodSet;
00060   MethodSet _nonstandard_methods;
00061   bool _deny_nonstandard_methods;
00062   static const uint32_t ALL_METHOD_MASK = ~0; 
00063 
00064 
00065 
00066   AclRecord() : _method_mask(0), _src_line(0), _deny_nonstandard_methods(false) { }
00067 
00068   AclRecord(uint32_t method_mask) : _method_mask(method_mask), _src_line(0),
00069                                     _deny_nonstandard_methods(false) { }
00070 
00071   AclRecord(uint32_t method_mask, int ln, const MethodSet &nonstandard_methods, bool deny_nonstandard_methods)
00072     : _method_mask(method_mask), _src_line(ln), _nonstandard_methods(nonstandard_methods),
00073       _deny_nonstandard_methods(deny_nonstandard_methods) { }
00074 
00075   static uint32_t MethodIdxToMask(int wksidx) { return 1 << (wksidx - HTTP_WKSIDX_CONNECT); }
00076 
00077   bool isEmpty() const {
00078     return (_method_mask == 0) && _nonstandard_methods.empty();
00079   }
00080 
00081   bool isMethodAllowed(int method_wksidx) const {
00082     return _method_mask & MethodIdxToMask(method_wksidx);
00083   }
00084   
00085   bool isNonstandardMethodAllowed(const std::string &method_str) const {
00086     if (_method_mask == ALL_METHOD_MASK) {
00087       return true;
00088     }
00089     bool method_in_set = _nonstandard_methods.count(method_str);
00090     return _deny_nonstandard_methods ? !method_in_set : method_in_set;
00091   }
00092 };
00093 
00094 
00095 
00096 class IpAllow : public ConfigInfo
00097 {
00098   friend int main(int, char**);
00099   friend struct IpAllowUpdate;
00100 
00101 public:
00102   typedef IpAllow self; 
00103 
00104   IpAllow(const char *config_var, const char *name, const char *action_val);
00105    ~IpAllow();
00106   void Print();
00107   AclRecord *match(IpEndpoint const* ip) const;
00108   AclRecord *match(sockaddr const* ip) const;
00109 
00110   static void startup();
00111   static void reconfigure();
00112 
00113   static IpAllow * acquire();
00114   static void release(IpAllow * params);
00115 
00116 
00117   static const AclRecord *AllMethodAcl() {
00118     return &ALL_METHOD_ACL;
00119   }
00120 
00121   typedef ConfigProcessor::scoped_config<IpAllow, IpAllow> scoped_config;
00122 
00123 private:
00124   static int configid;
00125   static const AclRecord ALL_METHOD_ACL;
00126 
00127   int BuildTable();
00128 
00129   char config_file_path[PATH_NAME_MAX];
00130   const char *module_name;
00131   const char *action;
00132   IpMap _map;
00133   Vec<AclRecord> _acls;
00134 };
00135 
00136 inline AclRecord *
00137 IpAllow::match(IpEndpoint const* ip) const {
00138   return this->match(&ip->sa);
00139 }
00140 
00141 inline AclRecord *
00142 IpAllow::match(sockaddr const* ip) const {
00143   void *raw;
00144   if (_map.contains(ip, &raw)) {
00145     return static_cast<AclRecord*>(raw);
00146   }
00147   return NULL;
00148 }
00149 
00150 #endif