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
00031 #include <list>
00032
00033 #define MAX_CLIENT_RECV 4024
00034 #define MAX_SERVER_RECV 4024
00036 #define RECV_THREAD_AMOUNT 1024
00041 enum AUTH_STATE
00042 {
00043 AUTH_STATE_UNKNOWN = 0,
00044 AUTH_STATE_BANNED = 1,
00045 AUTH_STATE_IGNORE_BANNED = 2
00046 };
00050 enum CONFIG_ENTRY_CHECK_MODE {
00051 CHECK_TRAFFIC = 1,
00052 CHECK_HOSTING = 2,
00053 CHECK_BOTH = 3
00054 };
00055
00056
00057
00061 #ifdef DEBUG
00062 #define debug(...) fprintf(stderr, __VA_ARGS__ )
00063 #else
00064 #define debug(...) ;
00065 #endif
00066
00067 #define log_error(...) fprintf(stderr, __VA_ARGS__ )
00068
00069 #define t_int int
00070
00071 extern int curr_month;
00076 struct config_entry
00077 {
00078 unsigned char zone_index;
00079
00080 long long traffic_month;
00081 long long traffic_day;
00082 long long traffic_quarter;
00083 char ban;
00084 char allow;
00085 };
00086
00090 struct buf_el{
00091 int to_send;
00092 int pos;
00093 char *buf;
00094 int do_not_delete;
00095 int *count;
00096 buf_el():do_not_delete(0){
00097 count = NULL;
00098 buf = NULL;
00099 };
00100 };
00101
00105 struct t_uid
00106 {
00107 uint32_t length;
00108 char *data;
00110 bool operator==(const t_uid& other) const {
00111 if(this->length != other.length) {
00112 return false;
00113 }
00114 return memcmp(this->data, other.data, this->length) == 0;
00115 }
00116 bool operator!=(const t_uid& other) const {
00117 return !(*this == other);
00118 }
00119 void allocate(uint32_t length_new){
00120 if(length>0) delete[] data;
00121 length = length_new;
00122 if(length_new > 0) {
00123 data = new char[length];
00124 }
00125 }
00126 bool operator<(const t_uid& other) const {
00127 int check_size = (this->length < other.length) ? this->length : other.length;
00128 int rc = memcmp(this->data, other.data, check_size);
00129 if(rc == 0) {
00130 return this->length < other.length;
00131 } else if(rc < 0) {
00132 return true;
00133 } else {
00134 return false;
00135 }
00136
00137 }
00138 t_uid(const char * dest){
00139 length = strlen(dest);
00140 data = new char[length];
00141 memcpy(data, dest, length);
00142 }
00143 t_uid(){ length=0; data=NULL; }
00144 t_uid(const t_uid &ref){
00145 length = ref.length;
00146 if(ref.length != 0) {
00147 data = new char[length];
00148 memcpy(data, ref.data, length);
00149 }
00150 }
00151 t_uid& operator=(const t_uid &ref){
00152 if( &ref == this ) return *this;
00153 if( length > 0 ) {
00154 length = 0;
00155 delete[] data;
00156 }
00157 allocate(ref.length);
00158 memcpy(data, ref.data, length);
00159 return *this;
00160 }
00161 ~t_uid(){
00162
00163 if(length>0) delete[] data;
00164 }
00165 };
00166 #define t_traffic_int long long
00167 #define QUARTER 900
00168 #define HOUR 3600
00169 #define DAY 86400
00170 #define MONTH 2592000
00175 struct t_traffic
00176 {
00177 long long traffic_month;
00178 long long traffic_day;
00179 long long traffic_quarter;
00180 time_t timestamp;
00181 char ban;
00182 char allow;
00183 char _set;
00184 t_traffic() {
00185 ban = allow = false;
00186 traffic_month=traffic_day=traffic_quarter=_set=0;
00187 timestamp=time(NULL);
00188 }
00189 t_traffic(t_traffic_int diff){
00190 _set=0;
00191 timestamp = time(NULL);
00192 ban = allow = false;
00193 traffic_month=traffic_day=traffic_quarter=diff;
00194 }
00198 inline void update_() {
00199 time_t curr_time = time(NULL);
00200 time_t interval = curr_time - timestamp;
00201 tm *info = new tm;
00202 localtime_r(×tamp,info);
00203 if(info->tm_mon != curr_month) {
00204 traffic_month=0;
00205 }
00206 delete info;
00207
00208 traffic_day = traffic_day - traffic_day * interval / DAY;
00209 traffic_quarter = traffic_quarter - traffic_quarter * interval / QUARTER;
00210 traffic_quarter = traffic_quarter>0?traffic_quarter:0;
00211 traffic_day = traffic_day>0?traffic_day:0;
00212
00213 timestamp = curr_time;
00214 }
00215 t_traffic& operator+=(const t_traffic_int &add) {
00216 update_();
00217 traffic_day += add;
00218 traffic_quarter += add;
00219 traffic_month += add;
00220 return (*this);
00221 }
00222 t_traffic& operator+=(const t_traffic &add) {
00223 update_();
00224 traffic_day+=add.traffic_day;
00225 traffic_month+=add.traffic_month;
00226 traffic_quarter+=add.traffic_quarter;
00227 return (*this);
00228 }
00229 t_traffic(const t_traffic &ref) {
00230 traffic_month = ref.traffic_month;
00231 traffic_day = ref.traffic_day;
00232 traffic_quarter = ref.traffic_quarter;
00233 timestamp = ref.timestamp;
00234 ban = ref.ban;
00235 allow = ref.allow;
00236 _set = ref._set;
00237 }
00238 int set() {
00239 return _set;
00240 }
00241 bool operator<(const t_traffic &x) {
00242
00243 update_();
00244
00245
00246 if( traffic_month > x.traffic_month ) return false;
00247
00248 if( traffic_day > x.traffic_day ) return false;
00249
00250 if( traffic_quarter > x.traffic_quarter ) return false;
00251 return true;
00252 }
00253 };
00254
00255
00256 enum conn_state { opened, closed };
00260 struct client_s {
00261 public:
00262 ssize_t read_position;
00263 char read_buffer[MAX_CLIENT_RECV];
00264 t_uid uid;
00265 event * read;
00266 event * write;
00267 int fd;
00268 conn_state state;
00269 int thread_index;
00271 char auth_result;
00272 unsigned char auth_zone;
00273
00274 bool is_mainserver;
00275 std::list<buf_el> packets_to_send;
00277 client_s() {
00278 read_position = 0;
00279 state = closed;
00280 is_mainserver = false;
00281 read = 0;
00282 write = 0;
00283 }
00284
00285 ~client_s() {
00286 for(std::list<buf_el>::iterator it = packets_to_send.begin(); it != packets_to_send.end(); ++it) {
00287 if(it->do_not_delete) {
00288 *(it->count) = *(it->count) - 1;
00289 }
00290 if(!it->do_not_delete) {
00291 delete[] it->buf;
00292 } else if(*(it->count)==0 ) {
00293 delete[] it->buf;
00294 delete it->count;
00295 }
00296 }
00297 }
00298 };
00299 typedef client_s client_t;
00303 class proxy_server{
00304 public:
00305 int port;
00306 char *host;
00307 client_t *cli;
00308 int active;
00309 };
00310
00314 struct thread_s{
00315 public:
00316 int i;
00317 event * pipe_ev;
00318 event * ev;
00319 };
00320 extern struct event_base* thread_loops[RECV_THREAD_AMOUNT];
00321 extern pthread_t thread_ids[RECV_THREAD_AMOUNT];
00322 extern int thread_usage[RECV_THREAD_AMOUNT];
00323 extern pthread_mutex_t thread_usage_guard;
00324 extern pthread_mutex_t mutex_queue[RECV_THREAD_AMOUNT];
00325 extern int pipe_queue[RECV_THREAD_AMOUNT][2];
00326 extern int last_thread;
00328 extern pthread_mutex_t master_server_connecting;
00329 extern bool master_server_connected;
00330 extern client_t *master_server_cli;
00331
00332
00333 extern const char *NOTIFY;
00334 extern std::list<client_t *> queue[RECV_THREAD_AMOUNT];
00335 extern pthread_mutex_t mutex_queue[RECV_THREAD_AMOUNT];
00336
00337 extern pthread_mutex_t master_guard;
00338 #endif