00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __TYPES__HPP__
00022 #define __TYPES__HPP__
00023
00024 #include <stdint.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <cstdio>
00028 #include <event.h>
00029 #include <time.h>
00030 #include <list>
00031
00035 #ifdef DEBUG
00036 #define debug(...) fprintf(stderr, __VA_ARGS__ )
00037 #else
00038 #define debug(...) ;
00039 #endif
00040
00041 #define MAX_CLIENT_RECV 4024
00042 #define MAX_LIMIT_SERVER_RECV 4024
00043 #define RECV_THREAD_AMOUNT 1
00044
00045 enum AUTH_STATE
00046 {
00047 AUTH_STATE_UNKNOWN = 0,
00048 AUTH_STATE_BANNED = 1,
00049 AUTH_STATE_IGNORE_BANNED = 2
00050 };
00051
00052
00053
00054 #define t_int int
00055 extern int curr_month;
00057 enum CONFIG_ENTRY_CHECK_MODE {
00058 CHECK_TRAFFIC = 1,
00059 CHECK_HOSTING = 2,
00060 CHECK_BOTH = 3
00061 };
00062
00063 struct config_entry {
00064 char check_mode;
00065 unsigned char zone_index;
00066
00067
00068 int ttl;
00069 long long traffic_month;
00070 long long traffic_day;
00071 long long traffic_quarter;
00072
00073
00074 long long hosting_traffic_month;
00075 long long hosting_traffic_day;
00076 long long hosting_traffic_quarter;
00077
00078 char ban;
00079 char allow;
00080 };
00081
00085 struct buf_el{
00086 int to_send;
00087 int pos;
00088 char *buf;
00089 int do_not_delete;
00090 int *count;
00091 buf_el():do_not_delete(0){
00092 count = NULL;
00093 buf = NULL;
00094 };
00095 };
00096
00100 struct t_uid
00101 {
00102 uint32_t length;
00103 char *data;
00105 bool operator==(const t_uid& other) const {
00106 if(this->length != other.length) {
00107 return false;
00108 }
00109 return memcmp(this->data, other.data, this->length) == 0;
00110 }
00111 bool operator<(const t_uid& other) const {
00112 int check_size = (this->length < other.length) ? this->length : other.length;
00113 int rc = memcmp(this->data, other.data, check_size);
00114 if(rc == 0) {
00115 return this->length < other.length;
00116 } else if(rc < 0) {
00117 return true;
00118 } else {
00119 return false;
00120 }
00121
00122 }
00123 void allocate(uint32_t length_new){
00124 if(length>0) delete[] data;
00125 length = length_new;
00126 data = new char[length];
00127 }
00128 t_uid(const char * dest){
00129 length = strlen(dest);
00130 data = new char[length];
00131 memcpy(data, dest, length);
00132 }
00133 t_uid(){ length=0; data=NULL; }
00134 t_uid(const t_uid &ref){
00135 length = ref.length;
00136 if(ref.length != 0) {
00137 data = new char[length];
00138 memcpy(data, ref.data, length);
00139 }
00140 }
00141 t_uid& operator=(const t_uid &ref){
00142 if( &ref == this ) return *this;
00143 if( length > 0 ) {
00144 length = 0;
00145 delete[] data;
00146 }
00147 allocate(ref.length);
00148 memcpy(data, ref.data, length);
00149 return *this;
00150 }
00151 ~t_uid(){
00152
00153 if(length>0) delete[] data;
00154 }
00155 };
00156 #define t_traffic_int long long
00157 #define QUARTER 900
00158 #define HOUR 3600
00159 #define DAY 86400
00160 #define MONTH 2592000
00165 struct t_traffic
00166 {
00167 long long traffic_month;
00168 long long traffic_day;
00169 long long traffic_quarter;
00170 time_t timestamp;
00171 char ban;
00172 char allow;
00173 char _set;
00174 t_traffic() {
00175 traffic_month=traffic_day=traffic_quarter=_set=0;
00176 timestamp=time(NULL);
00177 }
00178 t_traffic(t_traffic_int diff) {
00179 _set=0;
00180 timestamp = time(NULL);
00181 traffic_month=traffic_day=traffic_quarter=diff;
00182 }
00186 inline void update_() {
00187 time_t curr_time = time(NULL);
00188 time_t interval = curr_time - timestamp;
00189 tm info;
00190 localtime_r(×tamp,&info);
00191 if(info.tm_mon != curr_month) {
00192 traffic_month=0;
00193 }
00194 traffic_day = traffic_day - traffic_day * interval / DAY;
00195 traffic_quarter = traffic_quarter - traffic_quarter * interval / QUARTER;
00196 traffic_quarter = traffic_quarter>0?traffic_quarter:0;
00197 traffic_day = traffic_day>0?traffic_day:0;
00198 timestamp = curr_time;
00199 }
00200 t_traffic& operator+=(const t_traffic_int &add) {
00201 update_();
00202 traffic_day += add;
00203 traffic_quarter += add;
00204 traffic_month += add;
00205 return (*this);
00206 }
00207 t_traffic& operator+=(const t_traffic &add) {
00208 update_();
00209 traffic_day+=add.traffic_day;
00210 traffic_month+=add.traffic_month;
00211 traffic_quarter+=add.traffic_quarter;
00212 return (*this);
00213 }
00214 t_traffic(const t_traffic &ref) {
00215 traffic_month = ref.traffic_month;
00216 traffic_day = ref.traffic_day;
00217 traffic_quarter = ref.traffic_quarter;
00218 timestamp = ref.timestamp;
00219 ban = ref.ban;
00220 allow = ref.allow;
00221 _set = ref._set;
00222 }
00223 int set() {
00224 return _set;
00225 }
00226 bool operator<(const t_traffic &x) {
00227
00228 update_();
00229
00230 if( traffic_month > x.traffic_month ) return false;
00231 if( traffic_day > x.traffic_day ) return false;
00232 if( traffic_quarter > x.traffic_quarter ) return false;
00233 return true;
00234 }
00235 };
00236
00237 enum conn_state { opened, closed };
00238 class proxy_server;
00242 struct client_s {
00243 public:
00244 ssize_t read_position;
00245 char read_buffer[MAX_CLIENT_RECV];
00246 t_uid uid;
00247 event * read;
00248 event * write;
00249 int fd;
00250 conn_state state;
00251 int thread_index;
00253 char auth_result;
00254
00255 std::list<buf_el> packets_to_send;
00257 proxy_server * proxy;
00258 client_s() {
00259 read_position = 0;
00260 state = closed;
00261 proxy = 0;
00262 write = 0;
00263 read = 0;
00264 }
00265 ~client_s() {
00266 for(std::list<buf_el>::iterator it = packets_to_send.begin(); it != packets_to_send.end(); ++it) {
00267 if(it->do_not_delete) {
00268 *(it->count) = *(it->count) - 1;
00269 }
00270 if(!it->do_not_delete) {
00271 delete[] it->buf;
00272 } else if(*(it->count)==0 ) {
00273 delete[] it->buf;
00274 delete it->count;
00275 }
00276 }
00277 }
00278 };
00279 typedef client_s client_t;
00283 class proxy_server{
00284 public:
00285 int port;
00286 char *host;
00287 client_t *cli;
00288 int active;
00289
00290 ~proxy_server() {
00291 delete[] host;
00292 }
00293 };
00294
00298 struct thread_s{
00299 public:
00300 int i;
00301 event * pipe_ev;
00302 event * ev;
00303 };
00304
00305
00306 extern bool pending_reload;
00307
00308 void reload_config();
00309 #endif