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
00032
00033
00034
00035
00036
00037
00038
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <stdlib.h>
00042 #include <unistd.h>
00043 #include <fcntl.h>
00044
00045 #include "../../sr_module.h"
00046 #include "../../error.h"
00047 #include "../../dprint.h"
00048 #include "../../ut.h"
00049 #include "../../mem/shm_mem.h"
00050 #include "../../timer.h"
00051 #include "../../locking.h"
00052 #include "ip_tree.h"
00053 #include "timer.h"
00054 #include "pike_funcs.h"
00055 #include "rpc.h"
00056
00057 MODULE_VERSION
00058
00059
00060
00061 static int pike_init(void);
00062 static int pike_exit(void);
00063
00064
00065
00066
00067 static unsigned int time_unit = 2;
00068 static unsigned int max_reqs = 30;
00069 unsigned int timeout = 120;
00070
00071
00072 gen_lock_t* timer_lock=0;
00073 struct list_link* timer = 0;
00074
00075
00076 static int fixup_str2int( void** param, int param_no)
00077 {
00078 unsigned long go_to;
00079 int err;
00080
00081 if (param_no == 1) {
00082 go_to = str2s(*param, strlen(*param), &err);
00083 if (err == 0) {
00084 pkg_free(*param);
00085 *param = (void *)go_to;
00086 return 0;
00087 } else {
00088 LOG(L_ERR, "ERROR: fixup_str2int: bad number <%s>\n",
00089 (char *)(*param));
00090 return E_CFG;
00091 }
00092 }
00093 return 0;
00094 }
00095
00096
00097 static int pike_check_req_0(struct sip_msg* msg, char* foo, char* bar)
00098 {
00099 return pike_check_req(msg, (char*)(long)0,bar);
00100 }
00101
00102
00103 static cmd_export_t cmds[]={
00104 {"pike_check_req", pike_check_req_0, 0, 0, REQUEST_ROUTE},
00105 {"pike_check_req", pike_check_req, 1, fixup_str2int, REQUEST_ROUTE},
00106 {0,0,0,0,0}
00107 };
00108
00109 static param_export_t params[]={
00110 {"sampling_time_unit", PARAM_INT, &time_unit},
00111 {"reqs_density_per_unit", PARAM_INT, &max_reqs},
00112 {"remove_latency", PARAM_INT, &timeout},
00113 {0,0,0}
00114 };
00115
00116
00117 struct module_exports exports= {
00118 "pike",
00119 cmds,
00120 pike_rpc_methods,
00121 params,
00122
00123 pike_init,
00124 (response_function) 0,
00125 (destroy_function) pike_exit,
00126 0,
00127 0
00128 };
00129
00130
00131
00132
00133 static int pike_init(void)
00134 {
00135 LOG(L_INFO,"PIKE - initializing\n");
00136
00137
00138 timer_lock=lock_alloc();
00139 if (timer_lock==0) {
00140 LOG(L_ERR,"ERROR:pike_init: alloc locks failed!\n");
00141 goto error1;
00142 }
00143
00144 if (lock_init(timer_lock)==0){
00145 LOG(L_ERR, "ERROR:pike_init: init lock failed\n");
00146 goto error1;
00147 }
00148
00149
00150 if ( init_ip_tree(max_reqs)!=0 ) {
00151 LOG(L_ERR,"ERROR:pike_init: ip_tree creation failed!\n");
00152 goto error2;
00153 }
00154
00155
00156 timer = (struct list_link*)shm_malloc(sizeof(struct list_link));
00157 if (timer==0) {
00158 LOG(L_ERR,"ERROR:pike_init: cannot alloc shm mem for timer!\n");
00159 goto error3;
00160 }
00161 timer->next = timer->prev = timer;
00162
00163
00164 register_timer( clean_routine , 0, 1 );
00165 register_timer( swap_routine , 0, time_unit );
00166
00167 return 0;
00168 error3:
00169 destroy_ip_tree();
00170 error2:
00171 lock_destroy(timer_lock);
00172 error1:
00173 if (timer_lock) lock_dealloc(timer_lock);
00174 timer_lock = 0;
00175 return -1;
00176 }
00177
00178
00179
00180 static int pike_exit(void)
00181 {
00182 LOG(L_INFO,"PIKE - destroying module\n");
00183
00184
00185 if (timer_lock) {
00186 lock_destroy(timer_lock);
00187 lock_dealloc(timer_lock);
00188 }
00189
00190
00191 if (timer) {
00192 shm_free(timer);
00193 timer = 0;
00194 }
00195
00196
00197 destroy_ip_tree();
00198
00199 return 0;
00200 }
00201
00202