00001 /* 00002 * $Id: sr_module.h 5265 2008-11-25 15:13:21Z miconda $ 00003 * 00004 * modules/plug-in structures declarations 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-10 changed module exports interface: added struct cmd_export 00027 * and param_export (andrei) 00028 * 2003-03-16 Added flags field to cmd_export_ (janakj) 00029 * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri) 00030 * 2004-03-12 extra flag USE_FUNC_PARAM added to modparam type - 00031 * instead of copying the param value, a func is called (bogdan) 00032 * 2004-09-19 switched to version.h for the module versions checks (andrei) 00033 * 2004-12-03 changed param_func_t to (modparam_t, void*), killed 00034 * param_func_param_t (andrei) 00035 * 2006-03-02 added find_cmd_export_t(), killed find_exportp() (bogdan) 00036 * 2006-11-28 added module_loaded() (Jeffrey Magder - SOMA Networks) 00037 */ 00038 00039 /*! 00040 * \file 00041 * \brief modules/plug-in structures declarations 00042 */ 00043 00044 00045 #ifndef sr_module_h 00046 #define sr_module_h 00047 00048 #include <dlfcn.h> 00049 00050 #include "parser/msg_parser.h" /* for sip_msg */ 00051 #include "statistics.h" 00052 #include "mi/mi.h" 00053 #include "pvar.h" 00054 #include "version.h" 00055 #include "route.h" 00056 00057 /** type used for the mod_register function export. 00058 * mod_register is a function called when loading a module 00059 * (if present), prior to registering the module exports. 00060 * @param path - path to the module, including file name 00061 * @param dlflags - pointer to the dlflags used when loading the module. 00062 * If the value is changed to a different and non-zero 00063 * value, the module will be reloaded with the new flags. 00064 * @param reserved1 - reserved for future use. 00065 * @param reserved2 - reserver for future use 00066 * @return 0 on success, -1 on error, all the other values are reserved 00067 * for future use (<0 meaning error and >0 success) 00068 */ 00069 typedef int (*mod_register_function)(char*, int*, void*, void*); 00070 00071 typedef struct module_exports* (*module_register)(void); 00072 typedef int (*cmd_function)(struct sip_msg*, char*, char*, char*, char*, char*, char*); 00073 typedef int (*fixup_function)(void** param, int param_no); 00074 typedef int (*free_fixup_function)(void** param, int param_no); 00075 typedef int (*response_function)(struct sip_msg*); 00076 typedef void (*destroy_function)(void); 00077 typedef int (*init_function)(void); 00078 typedef int (*child_init_function)(int rank); 00079 00080 00081 #define STR_PARAM (1U<<0) /* String parameter type */ 00082 #define INT_PARAM (1U<<1) /* Integer parameter type */ 00083 #define USE_FUNC_PARAM (1U<<(8*sizeof(int)-1)) 00084 #define PARAM_TYPE_MASK(_x) ((_x)&(~USE_FUNC_PARAM)) 00085 00086 typedef unsigned int modparam_t; 00087 00088 typedef int (*param_func_t)( modparam_t type, void* val); 00089 00090 typedef void (*mod_proc)(int no); 00091 00092 typedef int (*mod_proc_wrapper)(void); 00093 00094 /* Macros - used as rank in child_init function */ 00095 #define PROC_MAIN 0 /* Main Kamailio process */ 00096 #define PROC_TIMER -1 /* Timer attendant process */ 00097 #define PROC_TCP_MAIN -4 /* TCP main process */ 00098 00099 #define DEFAULT_DLFLAGS 0 /* value that signals to module loader to 00100 use default dlopen flags in Kamailio */ 00101 #ifndef RTLD_NOW 00102 /* for openbsd */ 00103 #define RTLD_NOW DL_LAZY 00104 #endif 00105 00106 #define KAMAILIO_DLFLAGS RTLD_NOW 00107 00108 #define MODULE_VERSION \ 00109 char *module_version=KAMAILIO_FULL_VERSION; \ 00110 char *module_flags=KAMAILIO_COMPILE_FLAGS; 00111 00112 00113 struct cmd_export_ { 00114 char* name; /* null terminated command name */ 00115 cmd_function function; /* pointer to the corresponding function */ 00116 int param_no; /* number of parameters used by the function */ 00117 fixup_function fixup; /* pointer to the function called to "fix" the 00118 parameters */ 00119 free_fixup_function 00120 free_fixup; /* pointer to the function called to free the 00121 "fixed" parameters */ 00122 int flags; /* Function flags */ 00123 }; 00124 00125 00126 struct param_export_ { 00127 char* name; /*!< null terminated param. name */ 00128 modparam_t type; /*!< param. type */ 00129 void* param_pointer; /*!< pointer to the param. memory location */ 00130 }; 00131 00132 00133 struct proc_export_ { 00134 char *name; 00135 mod_proc_wrapper pre_fork_function; 00136 mod_proc_wrapper post_fork_function; 00137 mod_proc function; 00138 unsigned int no; 00139 }; 00140 00141 00142 typedef struct cmd_export_ cmd_export_t; 00143 typedef struct param_export_ param_export_t; 00144 typedef struct proc_export_ proc_export_t; 00145 00146 struct module_exports{ 00147 char* name; /*!< null terminated module name */ 00148 unsigned int dlflags; /*!< flags for dlopen */ 00149 00150 cmd_export_t* cmds; /*!< null terminated array of the exported 00151 commands */ 00152 param_export_t* params; /*!< null terminated array of the exported 00153 module parameters */ 00154 00155 stat_export_t* stats; /*!< null terminated array of the exported 00156 module statistics */ 00157 00158 mi_export_t* mi_cmds; /*!< null terminated array of the exported 00159 MI functions */ 00160 00161 pv_export_t* items; /*!< null terminated array of the exported 00162 module items (pseudo-variables) */ 00163 00164 proc_export_t* procs; /*!< null terminated array of the additional 00165 processes reqired by the module */ 00166 00167 init_function init_f; /*!< Initialization function */ 00168 response_function response_f; /*!< function used for responses, 00169 returns yes or no; can be null */ 00170 destroy_function destroy_f; /*!< function called when the module should 00171 be "destroyed", e.g: on Kamailio exit */ 00172 child_init_function init_child_f;/*!< function called by all processes 00173 after the fork */ 00174 }; 00175 00176 00177 00178 00179 00180 struct sr_module{ 00181 char* path; 00182 void* handle; 00183 struct module_exports* exports; 00184 struct sr_module* next; 00185 }; 00186 00187 00188 struct sr_module* modules; /*!< global module list*/ 00189 00190 int register_module(struct module_exports*, char*, void*); 00191 int sr_load_module(char* path); 00192 cmd_export_t* find_cmd_export_t(char* name, int param_no, int flags); 00193 cmd_function find_export(char* name, int param_no, int flags); 00194 cmd_function find_mod_export(char* mod, char* name, int param_no, int flags); 00195 void destroy_modules(void); 00196 int init_child(int rank); 00197 int init_modules(void); 00198 00199 /*! \brief 00200 * Find a parameter with given type and return it's 00201 * address in memory 00202 * If there is no such parameter, NULL is returned 00203 */ 00204 void* find_param_export(char* mod, char* name, modparam_t type); 00205 00206 /* modules function prototypes: 00207 * struct module_exports* mod_register(); (type module_register) 00208 * int foo_cmd(struct sip_msg* msg, char* param); 00209 * - returns >0 if ok , <0 on error, 0 to stop processing (==DROP) 00210 * int response_f(struct sip_msg* msg) 00211 * - returns >0 if ok, 0 to drop message 00212 */ 00213 00214 /*! \brief Check if module is loaded 00215 * \return Returns 1 if the module with name 'name' is loaded, and zero otherwise. */ 00216 int module_loaded(char *name); 00217 00218 /*! \brief Counts the additional the number of processes requested by modules */ 00219 int count_module_procs(void); 00220 00221 /*! \brief Forks and starts the additional processes required by modules */ 00222 int start_module_procs(void); 00223 00224 00225 #endif
1.5.6