3 * modules/plug-in structures declarations
6 * Copyright (C) 2001-2003 FhG Fokus
8 * This file is part of ser, a free SIP server.
10 * ser is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version
15 * For a license to use the ser software under conditions
16 * other than those described here, or to purchase support for this
17 * software, please contact iptel.org by e-mail at the following addresses:
20 * ser is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 * 2003-03-10 changed module exports interface: added struct cmd_export
33 * and param_export (andrei)
34 * 2003-03-16 Added flags field to cmd_export_ (janakj)
35 * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
36 * 2004-03-12 extra flag USE_FUNC_PARAM added to modparam type -
37 * instead of copying the param value, a func is called (bogdan)
38 * 2004-09-19 switched to version.h for the module versions checks (andrei)
39 * 2004-12-03 changed param_func_t to (modparam_t, void*), killed
40 * param_func_param_t (andrei)
41 * 2007-06-07 added PROC_INIT, called in the main process context
42 * (same as PROC_MAIN), buf guaranteed to be called before
43 * any other process is forked (andrei)
44 * 2008-11-17 sip-router version: includes some of the openser/kamailio
45 * changes: f(void) instead of f(), free_fixup_function()
46 * dual module interface support: ser & kamailio (andrei)
47 * 2008-11-18 prototypes for various fixed parameters numbers module
48 * functions (3, 4, 5 & 6) and variable parameters (andrei)
49 * 2008-11-26 added fparam_free_contents() and fix_param_types (andrei)
52 /** modules structures/exports declarations and utilities (fixups a.s.o).
62 #include "parser/msg_parser.h" /* for sip_msg */
65 #include "route_struct.h"
70 #include "kstats_types.h"
71 #include "mi/mi_types.h"
76 #if defined KAMAILIO_MOD_INTERFACE || defined OPENSER_MOD_INTERFACE || \
77 defined MOD_INTERFACE_V1
79 #define MODULE_INTERFACE_VER 1
80 #define cmd_export_t kam_cmd_export_t
81 #define module_exports kam_module_exports
83 #elif defined SER_MOD_INTERFACE || defined MOD_INTERFACE_V0
85 #define MODULE_INTERFACE_VER 0
86 #define cmd_export_t ser_cmd_export_t
87 #define module_exports ser_module_exports
91 /* do nothing for core */
95 /** type used for the mod_register function export.
96 * mod_register is a function called when loading a module
97 * (if present), prior to registering the module exports.
98 * @param path - path to the module, including file name
99 * @param dlflags - pointer to the dlflags used when loading the module.
100 * If the value is changed to a different and non-zero
101 * value, the module will be reloaded with the new flags.
102 * @param reserved1 - reserved for future use.
103 * @param reserved2 - reserver for future use
104 * @return 0 on success, -1 on error, all the other values are reserved
105 * for future use (<0 meaning error and >0 success)
107 typedef int (*mod_register_function)(char*, int*, void*, void*);
109 typedef struct module_exports* (*module_register)(void);
110 typedef int (*cmd_function)(struct sip_msg*, char*, char*);
111 typedef int (*cmd_function3)(struct sip_msg*, char*, char*, char*);
112 typedef int (*cmd_function4)(struct sip_msg*, char*, char*, char*, char*);
113 typedef int (*cmd_function5)(struct sip_msg*, char*, char*, char*,
115 typedef int (*cmd_function6)(struct sip_msg*, char*, char*, char*,
116 char*, char*, char*);
117 /* variable number of param module function, takes as param the sip_msg,
118 extra paremeters number and a pointer to an array of parameters */
119 typedef int (*cmd_function_var)(struct sip_msg*, int no, action_u_t* vals );
120 typedef int (*fixup_function)(void** param, int param_no);
121 typedef int (*free_fixup_function)(void** param, int param_no);
122 typedef int (*response_function)(struct sip_msg*);
123 typedef void (*onbreak_function)(struct sip_msg*);
124 typedef void (*destroy_function)(void);
126 typedef int (*init_function)(void);
127 typedef int (*child_init_function)(int rank);
130 #define PARAM_STRING (1U<<0) /* String (char *) parameter type */
131 #define PARAM_INT (1U<<1) /* Integer parameter type */
132 #define PARAM_STR (1U<<2) /* struct str parameter type */
133 #define PARAM_USE_FUNC (1U<<(8*sizeof(int)-1))
134 #define PARAM_TYPE_MASK(_x) ((_x)&(~PARAM_USE_FUNC))
136 /* temporary, for backward compatibility only until all modules adjust it */
137 #define STR_PARAM PARAM_STRING
138 #define INT_PARAM PARAM_INT
139 #define USE_FUNC_PARAM PARAM_USE_FUNC
141 typedef unsigned int modparam_t;
143 typedef int (*param_func_t)( modparam_t type, void* val);
145 /* magic parameter number values */
147 #define NO_SCRIPT -1 /* export not usable from scripts */
148 #define VAR_PARAM_NO -128 /* function has variable number of parameters
149 (see cmd_function_var for the prototype) */
151 /* special fixup function flags.
152 * They are kept in the first 2 bits inside the pointer
154 #define FIXUP_F_FPARAM_RVE (unsigned long)1 /* fparam fixup, rve ready */
156 #define call_fixup(fixup, param, param_no) \
157 ((fixup) ? (fixup)(param, param_no) : 0)
159 /* Macros - used as rank in child_init function */
160 #define PROC_MAIN 0 /* Main ser process */
161 #define PROC_TIMER -1 /* Timer attendant process */
162 #define PROC_RPC -2 /* RPC type process */
163 #define PROC_FIFO PROC_RPC /* FIFO attendant process */
164 #define PROC_TCP_MAIN -4 /* TCP main process */
165 #define PROC_UNIXSOCK -5 /* Unix socket server */
166 #define PROC_ATTENDANT -10 /* main "attendant process */
167 #define PROC_INIT -127 /* special rank, the context is the main ser
168 process, but this is guaranteed to be executed
169 before any process is forked, so it can be used
170 to setup shared variables that depend on some
171 after mod_init available information (e.g.
172 total number of processes).
173 WARNING: child_init(PROC_MAIN) is again called
174 in the same process (main), but latter
175 (before tcp), so make sure you don't init things
176 twice, bot in PROC_MAIN and PROC_INT */
177 #define PROC_NOCHLDINIT -128 /* no child init functions will be called
178 if this rank is used in fork_process() */
180 #define PROC_MIN PROC_NOCHLDINIT /* Minimum process rank */
183 #define DEFAULT_DLFLAGS 0 /* value that signals to module loader to
184 use default dlopen flags in Kamailio */
187 #define RTLD_NOW DL_LAZY
190 #define KAMAILIO_DLFLAGS RTLD_NOW
193 #define MODULE_VERSION \
194 char *module_version=SER_FULL_VERSION; \
195 char *module_flags=SER_COMPILE_FLAGS; \
196 unsigned int module_interface_ver=MODULE_INTERFACE_VER;
199 struct ser_cmd_export_ {
200 char* name; /* null terminated command name */
201 cmd_function function; /* pointer to the corresponding function */
202 int param_no; /* number of parameters used by the function */
203 fixup_function fixup; /* pointer to the function called to "fix" the
205 int flags; /* Function flags */
209 /* kamailo/openser version */
210 struct kam_cmd_export_ {
211 char* name; /* null terminated command name */
212 cmd_function function; /* pointer to the corresponding function */
213 int param_no; /* number of parameters used by the function */
214 fixup_function fixup; /* pointer to the function called to "fix" the
216 free_fixup_function free_fixup; /* function called to free the "fixed"
218 int flags; /* Function flags */
222 struct sr31_cmd_export_ {
223 char* name; /* null terminated command name */
224 cmd_function function; /* pointer to the corresponding function */
225 int param_no; /* number of parameters used by the function */
226 fixup_function fixup; /* pointer to the function called to "fix" the
228 free_fixup_function free_fixup; /* function called to free the "fixed"
230 int flags; /* Function flags */
232 void* module_exports; /* pointer to module structure */
236 /* members situated at the same place in memory in both ser & kamailio
238 struct cmd_export_common_ {
240 cmd_function function;
242 fixup_function fixup;
246 struct param_export_ {
247 char* name; /* null terminated param. name */
248 modparam_t type; /* param. type */
249 void* param_pointer; /* pointer to the param. memory location */
254 /** allowed parameter types.
255 * the types _must_ be in "fallback" order,
256 * e.g. FPARAM_STR should be the last to allow fallback to it,
257 * F_PARAM_PVS should be in front of F_PARAM_AVP (so that
258 * for fix_param_types(FPARAM_AVP|FPARAM_PVS|FPARAM_STR, param) and $foo
259 * the pvars will be checked first and only if no pvar is found the
260 * param will be resolved to an avp)
264 FPARAM_INT = (1 << 0),
265 FPARAM_SELECT = (1 << 1),
266 FPARAM_PVS = (1 << 2),
267 FPARAM_AVP = (1 << 3),
268 FPARAM_STRING = (1 << 4),
269 FPARAM_STR = (1 << 5),
270 /* special types: no fallback between them possible */
271 FPARAM_REGEX = (1 << 6),
272 FPARAM_SUBST = (1 << 7),
273 FPARAM_PVE = (1 << 8)
279 typedef struct fparam {
280 char* orig; /* The original value */
281 int type; /* Type of parameter */
283 char* asciiz; /* Zero terminated ASCII string */
284 struct _str str; /* pointer/len string */
285 int i; /* Integer value */
286 regex_t* regex; /* Compiled regular expression */
287 avp_ident_t avp; /* AVP identifier */
288 select_t* select; /* select structure */
289 struct subst_expr* subst; /* Regex substitution */
290 pv_spec_t* pvs; /* kamailio pseudo-vars */
291 pv_elem_t* pve; /* kamailio pseudo-vars in a string */
297 typedef struct param_export_ param_export_t;
298 typedef struct ser_cmd_export_ ser_cmd_export_t;
299 typedef struct kam_cmd_export_ kam_cmd_export_t;
300 typedef struct cmd_export_common_ cmd_export_common_t;
301 typedef struct sr31_cmd_export_ sr31_cmd_export_t;
305 cmd_export_common_t c; /* common members for everybody */
312 /* ser module exports version */
313 struct ser_module_exports {
314 char* name; /* null terminated module name */
315 ser_cmd_export_t* cmds; /* null terminated array of the exported
317 rpc_export_t* rpc_methods; /* null terminated array of exported rpc methods */
318 param_export_t* params; /* null terminated array of the exported
320 init_function init_f; /* Initialization function */
321 response_function response_f; /* function used for responses,
322 returns yes or no; can be null */
323 destroy_function destroy_f; /* function called when the module should
324 be "destroyed", e.g: on ser exit;
326 onbreak_function onbreak_f;
327 child_init_function init_child_f; /* function called by all processes
332 /* kamailio/openser proc_export (missing from ser) */
333 typedef void (*mod_proc)(int no);
335 typedef int (*mod_proc_wrapper)(void);
337 struct proc_export_ {
339 mod_proc_wrapper pre_fork_function;
340 mod_proc_wrapper post_fork_function;
345 typedef struct proc_export_ proc_export_t;
348 /* kamailio/openser module exports version */
349 struct kam_module_exports {
350 char* name; /* null terminated module name */
351 unsigned int dlflags; /*!< flags for dlopen */
352 kam_cmd_export_t* cmds; /* null terminated array of the exported
354 param_export_t* params; /* null terminated array of the exported
356 stat_export_t* stats; /*!< null terminated array of the exported
358 mi_export_t* mi_cmds; /*!< null terminated array of the exported
360 pv_export_t* items; /*!< null terminated array of the exported
361 module items (pseudo-variables) */
362 proc_export_t* procs; /*!< null terminated array of the
363 additional processes required by the
365 init_function init_f; /* Initialization function */
366 response_function response_f; /* function used for responses,
367 returns yes or no; can be null */
368 destroy_function destroy_f; /* function called when the module should
369 be "destroyed", e.g: on ser exit;
371 child_init_function init_child_f; /* function called by all processes
377 /** sr/ser 3.1+ module exports version.
378 * Includes ser and kamailio versions, re-arraranged + some extras.
379 * Note: some of the members will be obsoleted and are kept only for
380 * backward compatibility (avoid re-writing all the modules exports
383 struct sr31_module_exports {
384 char* name; /* null terminated module name */
385 sr31_cmd_export_t* cmds; /* null terminated array of the exported
387 param_export_t* params; /* null terminated array of the exported
389 init_function init_f; /* Initialization function */
390 response_function response_f; /* function used for responses,
391 returns yes or no; can be null */
392 destroy_function destroy_f; /* function called when the module should
393 be "destroyed", e.g: on ser exit;
395 onbreak_function onbreak_f;
396 child_init_function init_child_f; /* function called by all processes
398 unsigned int dlflags; /**< flags for dlopen */
399 /* ser specific exports
400 (to be obsoleted and replaced by register_...) */
401 rpc_export_t* rpc_methods; /* null terminated array of exported
403 /* kamailio specific exports
404 (to be obsoleted and replaced by register_...) */
405 stat_export_t* stats; /*!< null terminated array of the exported
407 mi_export_t* mi_cmds; /*!< null terminated array of the exported
409 pv_export_t* items; /*!< null terminated array of the exported
410 module items (pseudo-variables) */
411 proc_export_t* procs; /*!< null terminated array of the
412 additional processes required by the
418 /* module exports in the same place in memory in both ser & kamailio */
419 struct module_exports_common{
424 union module_exports_u {
425 struct module_exports_common c; /*common members for all the versions*/
426 struct ser_module_exports v0;
427 struct kam_module_exports v1;
434 unsigned int orig_mod_interface_ver;
435 struct sr31_module_exports exports;
436 struct sr_module* next;
440 extern struct sr_module* modules; /* global module list*/
441 extern response_function* mod_response_cbks;/* response callback array */
442 extern int mod_response_cbk_no; /* size of reponse callbacks array */
444 int register_builtin_modules(void);
445 int load_module(char* path);
446 sr31_cmd_export_t* find_export_record(char* name, int param_no, int flags,
448 cmd_function find_export(char* name, int param_no, int flags);
449 cmd_function find_mod_export(char* mod, char* name, int param_no, int flags);
450 rpc_export_t* find_rpc_export(char* name, int flags);
451 void destroy_modules(void);
452 int init_child(int rank);
453 int init_modules(void);
454 struct sr_module* find_module_by_name(char* mod);
456 /* true if the module with name 'mod_name' is loaded */
457 #define module_loaded(mod_name) (find_module_by_name(mod_name)!=0)
461 * Find a parameter with given type and return it's
463 * If there is no such parameter, NULL is returned
465 void* find_param_export(struct sr_module* mod, char* name, modparam_t type_mask, modparam_t *param_type);
467 /* modules function prototypes:
468 * struct module_exports* mod_register(); (type module_register)
469 * int foo_cmd(struct sip_msg* msg, char* param);
470 * - returns >0 if ok , <0 on error, 0 to stop processing (==DROP)
471 * int response_f(struct sip_msg* msg)
472 * - returns >0 if ok, 0 to drop message
476 /* API function to get other parameters from fixup */
477 action_u_t *fixup_get_param(void **cur_param, int cur_param_no, int required_param_no);
478 int fixup_get_param_count(void **cur_param, int cur_param_no);
480 int fix_flag( modparam_t type, void* val,
481 char* mod_name, char* param_name, int* flag);
485 * Common function parameter fixups
489 * Generic parameter fixup function which creates
490 * fparam_t structure. type parameter contains allowed
493 int fix_param(int type, void** param);
494 void fparam_free_contents(fparam_t* fp);
496 /** fix a param to one of the given types (mask).
498 int fix_param_types(int types, void** param);
501 * Fixup variable string, the parameter can be
502 * AVP, SELECT, or ordinary string. AVP and select
503 * identifiers will be resolved to their values during
506 * The parameter value will be converted to fparam structure
507 * This function returns -1 on an error
509 int fixup_var_str_12(void** param, int param_no);
511 /* Same as fixup_var_str_12 but applies to the 1st parameter only */
512 int fixup_var_str_1(void** param, int param_no);
514 /* Same as fixup_var_str_12 but applies to the 2nd parameter only */
515 int fixup_var_str_2(void** param, int param_no);
517 /** fixup variable-pve-string.
518 * The parameter can be a PVAR, AVP, SELECT, PVE (pv based format string)
521 int fixup_var_pve_str_12(void** param, int param_no);
523 /* same as fixup_var_pve_str_12 but applies to the 1st parameter only */
524 int fixup_var_pve_str_1(void** param, int param_no);
526 /* same as fixup_var_pve_str_12 but applies to the 2nd parameter only */
527 int fixup_var_pve_str_2(void** param, int param_no);
530 * Fixup variable integer, the parameter can be
531 * AVP, SELECT, or ordinary integer. AVP and select
532 * identifiers will be resolved to their values and
533 * converted to int if necessary during runtime
535 * The parameter value will be converted to fparam structure
536 * This function returns -1 on an error
538 int fixup_var_int_12(void** param, int param_no);
540 /* Same as fixup_var_int_12 but applies to the 1st parameter only */
541 int fixup_var_int_1(void** param, int param_no);
543 /* Same as fixup_var_int_12 but applies to the 2nd parameter only */
544 int fixup_var_int_2(void** param, int param_no);
547 * The parameter must be a regular expression which must compile, the
548 * parameter will be converted to compiled regex
550 int fixup_regex_12(void** param, int param_no);
552 /* Same as fixup_regex_12 but applies to the 1st parameter only */
553 int fixup_regex_1(void** param, int param_no);
555 /* Same as fixup_regex_12 but applies to the 2nd parameter only */
556 int fixup_regex_2(void** param, int param_no);
559 * The string parameter will be converted to integer
561 int fixup_int_12(void** param, int param_no);
563 /* Same as fixup_int_12 but applies to the 1st parameter only */
564 int fixup_int_1(void** param, int param_no);
566 /* Same as fixup_int_12 but applies to the 2nd parameter only */
567 int fixup_int_2(void** param, int param_no);
570 * Parse the parameter as static string, do not resolve
571 * AVPs or selects, convert the parameter to str structure
573 int fixup_str_12(void** param, int param_no);
575 /* Same as fixup_str_12 but applies to the 1st parameter only */
576 int fixup_str_1(void** param, int param_no);
578 /* Same as fixup_str_12 but applies to the 2nd parameter only */
579 int fixup_str_2(void** param, int param_no);
582 * Get the function parameter value as string
583 * Return values: 0 - Success
584 * -1 - Cannot get value
586 int get_str_fparam(str* dst, struct sip_msg* msg, fparam_t* param);
589 * Get the function parameter value as integer
590 * Return values: 0 - Success
591 * -1 - Cannot get value
593 int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param);
597 * Retrieve the compiled RegExp.
598 * @return: 0 for success, negative on error.
600 int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param);
603 int is_fparam_rve_fixup(fixup_function f);
606 /** generic free fixup type function for a fixed fparam.
607 * It will free whatever was allocated during the initial fparam fixup
608 * and restore the original param value.
610 void fparam_free_restore(void** param);
611 int fixup_free_fparam_all(void** param, int param_no);
612 int fixup_free_fparam_1(void** param, int param_no);
613 int fixup_free_fparam_2(void** param, int param_no);
615 free_fixup_function get_fixup_free(fixup_function f);
617 #endif /* sr_module_h */