modules/pike/pike.c
31ccf6a2
 /*
  * PIKE module
  *
  * Copyright (C) 2001-2003 FhG Fokus
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
31ccf6a2
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
31ccf6a2
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
31ccf6a2
  *
  */
 
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 
 #include "../../sr_module.h"
 #include "../../error.h"
 #include "../../dprint.h"
 #include "../../ut.h"
 #include "../../mem/shm_mem.h"
 #include "../../timer.h"
 #include "../../locking.h"
 #include "ip_tree.h"
 #include "timer.h"
5e95415f
 #include "pike_mi.h"
31ccf6a2
 #include "pike_funcs.h"
3ad60fe1
 #include "../../rpc_lookup.h"
 #include "pike_rpc.h"
31ccf6a2
 
 MODULE_VERSION
 
 
 
 static int pike_init(void);
 static int pike_exit(void);
 
 
 
 /* parameters */
 static int time_unit = 2;
 static int max_reqs  = 30;
 int timeout   = 120;
470b3737
 int pike_log_level = L_WARN;
31ccf6a2
 
 /* global variables */
 gen_lock_t*             timer_lock=0;
 struct list_link*       timer = 0;
 
 
 static cmd_export_t cmds[]={
470b3737
 	{"pike_check_req", (cmd_function)pike_check_req,  0,  0, 0, REQUEST_ROUTE},
80998a7f
 	{0,0,0,0,0,0}
31ccf6a2
 };
 
 static param_export_t params[]={
 	{"sampling_time_unit",    INT_PARAM,  &time_unit},
 	{"reqs_density_per_unit", INT_PARAM,  &max_reqs},
 	{"remove_latency",        INT_PARAM,  &timeout},
470b3737
 	{"pike_log_level",        INT_PARAM, &pike_log_level},
31ccf6a2
 	{0,0,0}
 };
 
 
5e95415f
 static mi_export_t mi_cmds [] = {
c4d2e802
 	{MI_PIKE_LIST, mi_pike_list,   MI_NO_INPUT_FLAG,  0,  0 },
fcb8aeaf
 	{0,0,0,0,0}
5e95415f
 };
 
 
31ccf6a2
 struct module_exports exports= {
 	"pike",
51716422
 	DEFAULT_DLFLAGS, /* dlopen flags */
31ccf6a2
 	cmds,
 	params,
739aec8b
 	0,           /* exported statistics */
5e95415f
 	mi_cmds,     /* exported MI functions */
71c26efd
 	0,           /* exported pseudo-variables */
3eee3a4e
 	0,           /* extra processes */
31ccf6a2
 	pike_init,   /* module initialization function */
3ed44241
 	0,
31ccf6a2
 	(destroy_function) pike_exit,   /* module exit function */
 	0  /* per-child init function */
 };
 
 
 
 
 static int pike_init(void)
 {
3ad60fe1
 	LOG(L_INFO, "PIKE - initializing\n");
 
6635cb9f
 	if(register_mi_mod(exports.name, mi_cmds)!=0)
 	{
 		LM_ERR("failed to register MI commands\n");
 		return -1;
 	}
3ad60fe1
 	if (rpc_register_array(pike_rpc_methods)!=0) {
 		LM_ERR("failed to register RPC commands\n");
 		return -1;
 	}
6635cb9f
 
31ccf6a2
 	/* alloc the timer lock */
 	timer_lock=lock_alloc();
 	if (timer_lock==0) {
387847d7
 		LM_ERR(" alloc locks failed!\n");
31ccf6a2
 		goto error1;
 	}
 	/* init the lock */
 	if (lock_init(timer_lock)==0){
387847d7
 		LM_ERR(" init lock failed\n");
31ccf6a2
 		goto error1;
 	}
 
 	/* init the IP tree */
 	if ( init_ip_tree(max_reqs)!=0 ) {
387847d7
 		LM_ERR(" ip_tree creation failed!\n");
31ccf6a2
 		goto error2;
 	}
 
 	/* init timer list */
 	timer = (struct list_link*)shm_malloc(sizeof(struct list_link));
 	if (timer==0) {
387847d7
 		LM_ERR(" cannot alloc shm mem for timer!\n");
31ccf6a2
 		goto error3;
 	}
 	timer->next = timer->prev = timer;
 
 	/* registering timing functions  */
 	register_timer( clean_routine , 0, 1 );
 	register_timer( swap_routine , 0, time_unit );
 
7fffcebb
 	/* Register counter */
 	pike_counter_init();
 
31ccf6a2
 	return 0;
 error3:
 	destroy_ip_tree();
 error2:
 	lock_destroy(timer_lock);
 error1:
 	if (timer_lock) lock_dealloc(timer_lock);
 	timer_lock = 0;
 	return -1;
 }
 
 
 
 static int pike_exit(void)
 {
 	/* destroy semaphore */
 	if (timer_lock) {
 		lock_destroy(timer_lock);
 		lock_dealloc(timer_lock);
 	}
 
 	/* empty the timer list head */
 	if (timer) {
 		shm_free(timer);
 		timer = 0;
 	}
 
 	/* destroy the IP tree */
 	destroy_ip_tree();
 
 	return 0;
 }