modules_k/pike/pike.c

00001 /*
00002  * $Id$
00003  *
00004  * PIKE module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2003-03-11  updated to the new module exports interface (andrei)
00027  *  2003-03-11  converted to the new locking interface: locking.h --
00028  *               major changes (andrei)
00029  *  2003-03-16  flags export parameter added (janakj)
00030  *  2008-04-17  new parameter to control the module's log regarding the
00031  *               blocking/unblocking of IPs (bogdan)
00032  */
00033 
00034 
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <fcntl.h>
00040 
00041 #include "../../sr_module.h"
00042 #include "../../error.h"
00043 #include "../../dprint.h"
00044 #include "../../ut.h"
00045 #include "../../mem/shm_mem.h"
00046 #include "../../timer.h"
00047 #include "../../locking.h"
00048 #include "ip_tree.h"
00049 #include "timer.h"
00050 #include "pike_mi.h"
00051 #include "pike_funcs.h"
00052 
00053 MODULE_VERSION
00054 
00055 
00056 
00057 static int pike_init(void);
00058 static int pike_exit(void);
00059 
00060 
00061 
00062 /* parameters */
00063 static int time_unit = 2;
00064 static int max_reqs  = 30;
00065 int timeout   = 120;
00066 int pike_log_level = L_WARN;
00067 
00068 /* global variables */
00069 gen_lock_t*             timer_lock=0;
00070 struct list_link*       timer = 0;
00071 
00072 
00073 static cmd_export_t cmds[]={
00074         {"pike_check_req", (cmd_function)pike_check_req,  0,  0, 0, REQUEST_ROUTE},
00075         {0,0,0,0,0,0}
00076 };
00077 
00078 static param_export_t params[]={
00079         {"sampling_time_unit",    INT_PARAM,  &time_unit},
00080         {"reqs_density_per_unit", INT_PARAM,  &max_reqs},
00081         {"remove_latency",        INT_PARAM,  &timeout},
00082         {"pike_log_level",        INT_PARAM, &pike_log_level},
00083         {0,0,0}
00084 };
00085 
00086 
00087 static mi_export_t mi_cmds [] = {
00088         {MI_PIKE_LIST, mi_pike_list,   MI_NO_INPUT_FLAG,  0,  0 },
00089         {0,0,0,0,0}
00090 };
00091 
00092 
00093 struct module_exports exports= {
00094         "pike",
00095         DEFAULT_DLFLAGS, /* dlopen flags */
00096         cmds,
00097         params,
00098         0,           /* exported statistics */
00099         mi_cmds,     /* exported MI functions */
00100         0,           /* exported pseudo-variables */
00101         0,           /* extra processes */
00102         pike_init,   /* module initialization function */
00103         0,
00104         (destroy_function) pike_exit,   /* module exit function */
00105         0  /* per-child init function */
00106 };
00107 
00108 
00109 
00110 
00111 static int pike_init(void)
00112 {
00113         if(register_mi_mod(exports.name, mi_cmds)!=0)
00114         {
00115                 LM_ERR("failed to register MI commands\n");
00116                 return -1;
00117         }
00118 
00119         /* alloc the timer lock */
00120         timer_lock=lock_alloc();
00121         if (timer_lock==0) {
00122                 LM_ERR(" alloc locks failed!\n");
00123                 goto error1;
00124         }
00125         /* init the lock */
00126         if (lock_init(timer_lock)==0){
00127                 LM_ERR(" init lock failed\n");
00128                 goto error1;
00129         }
00130 
00131         /* init the IP tree */
00132         if ( init_ip_tree(max_reqs)!=0 ) {
00133                 LM_ERR(" ip_tree creation failed!\n");
00134                 goto error2;
00135         }
00136 
00137         /* init timer list */
00138         timer = (struct list_link*)shm_malloc(sizeof(struct list_link));
00139         if (timer==0) {
00140                 LM_ERR(" cannot alloc shm mem for timer!\n");
00141                 goto error3;
00142         }
00143         timer->next = timer->prev = timer;
00144 
00145         /* registering timing functions  */
00146         register_timer( clean_routine , 0, 1 );
00147         register_timer( swap_routine , 0, time_unit );
00148 
00149         return 0;
00150 error3:
00151         destroy_ip_tree();
00152 error2:
00153         lock_destroy(timer_lock);
00154 error1:
00155         if (timer_lock) lock_dealloc(timer_lock);
00156         timer_lock = 0;
00157         return -1;
00158 }
00159 
00160 
00161 
00162 static int pike_exit(void)
00163 {
00164         /* destroy semaphore */
00165         if (timer_lock) {
00166                 lock_destroy(timer_lock);
00167                 lock_dealloc(timer_lock);
00168         }
00169 
00170         /* empty the timer list head */
00171         if (timer) {
00172                 shm_free(timer);
00173                 timer = 0;
00174         }
00175 
00176         /* destroy the IP tree */
00177         destroy_ip_tree();
00178 
00179         return 0;
00180 }
00181 
00182