modules_s/pike/pike.c

00001 /*
00002  * $Id$
00003  *
00004  * PIKE module
00005  *
00006  *
00007  * Copyright (C) 2001-2003 FhG Fokus
00008  *
00009  * This file is part of ser, a free SIP server.
00010  *
00011  * ser is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version
00015  *
00016  * For a license to use the ser software under conditions
00017  * other than those described here, or to purchase support for this
00018  * software, please contact iptel.org by e-mail at the following addresses:
00019  *    info@iptel.org
00020  *
00021  * ser is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  *
00026  * You should have received a copy of the GNU General Public License
00027  * along with this program; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00029  *
00030  * History:
00031  * --------
00032  *  2003-03-11  updated to the new module exports interface (andrei)
00033  *  2003-03-11  converted to the new locking interface: locking.h --
00034  *               major changes (andrei)
00035  *  2003-03-16  flags export parameter added (janakj)
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 /* parameters */
00067 static unsigned int time_unit = 2;
00068 static unsigned int max_reqs  = 30;
00069 unsigned int timeout   = 120;
00070 
00071 /* global variables */
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,           /* RPC methods */
00121         params,
00122 
00123         pike_init,   /* module initialization function */
00124         (response_function) 0,
00125         (destroy_function) pike_exit,   /* module exit function */
00126         0,
00127         0  /* per-child init function */
00128 };
00129 
00130 
00131 
00132 
00133 static int pike_init(void)
00134 {
00135         LOG(L_INFO,"PIKE - initializing\n");
00136 
00137         /* alloc the timer lock */
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         /* init the lock */
00144         if (lock_init(timer_lock)==0){
00145                 LOG(L_ERR, "ERROR:pike_init: init lock failed\n");
00146                 goto error1;
00147         }
00148 
00149         /* init the IP tree */
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         /* init timer list */
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         /* registering timing functions  */
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         /* destroy semaphore */
00185         if (timer_lock) {
00186                 lock_destroy(timer_lock);
00187                 lock_dealloc(timer_lock);
00188         }
00189 
00190         /* empty the timer list head */
00191         if (timer) {
00192                 shm_free(timer);
00193                 timer = 0;
00194         }
00195 
00196         /* destroy the IP tree */
00197         destroy_ip_tree();
00198 
00199         return 0;
00200 }
00201 
00202