sr_module.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* $Id$
  2. *
  3. * modules/plug-in structures declarations
  4. *
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  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
  14. *
  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:
  18. * [email protected]
  19. *
  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.
  24. *
  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
  28. */
  29. /*
  30. * History:
  31. * --------
  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. */
  45. #ifndef sr_module_h
  46. #define sr_module_h
  47. #include "parser/msg_parser.h" /* for sip_msg */
  48. #include "version.h"
  49. #include "rpc.h"
  50. #include "route_struct.h"
  51. #include "str.h"
  52. typedef struct module_exports* (*module_register)();
  53. typedef int (*cmd_function)(struct sip_msg*, char*, char*);
  54. typedef int (*fixup_function)(void** param, int param_no);
  55. typedef int (*response_function)(struct sip_msg*);
  56. typedef void (*onbreak_function)(struct sip_msg*);
  57. typedef void (*destroy_function)();
  58. typedef int (*init_function)(void);
  59. typedef int (*child_init_function)(int rank);
  60. #define PARAM_STRING (1U<<0) /* String (char *) parameter type */
  61. #define PARAM_INT (1U<<1) /* Integer parameter type */
  62. #define PARAM_STR (1U<<2) /* struct str parameter type */
  63. #define PARAM_USE_FUNC (1U<<(8*sizeof(int)-1))
  64. #define PARAM_TYPE_MASK(_x) ((_x)&(~PARAM_USE_FUNC))
  65. /* temporary, for backward compatibility only until all modules adjust it */
  66. #define STR_PARAM PARAM_STRING
  67. #define INT_PARAM PARAM_INT
  68. #define USE_FUNC_PARAM PARAM_USE_FUNC
  69. typedef unsigned int modparam_t;
  70. typedef int (*param_func_t)( modparam_t type, void* val);
  71. #define REQUEST_ROUTE 1 /* Function can be used in request route blocks */
  72. #define FAILURE_ROUTE 2 /* Function can be used in reply route blocks */
  73. #define ONREPLY_ROUTE 4 /* Function can be used in on_reply */
  74. #define BRANCH_ROUTE 8 /* Function can be used in branch_route blocks */
  75. #define ONSEND_ROUTE 16 /* Function can be used in onsend_route blocks */
  76. /* Macros - used as rank in child_init function */
  77. #define PROC_MAIN 0 /* Main ser process */
  78. #define PROC_TIMER -1 /* Timer attendant process */
  79. #define PROC_RPC -2 /* RPC type process */
  80. #define PROC_FIFO PROC_RPC /* FIFO attendant process */
  81. #define PROC_TCP_MAIN -4 /* TCP main process */
  82. #define PROC_UNIXSOCK -5 /* Unix socket server */
  83. #define PROC_ATTENDANT -10 /* main "attendant process */
  84. #define PROC_INIT -127 /* special rank, the context is the main ser
  85. process, but this is guaranteed to be executed
  86. before any process is forked, so it can be used
  87. to setup shared variables that depend on some
  88. after mod_init available information (e.g.
  89. total number of processes).
  90. WARNING: child_init(PROC_MAIN) is again called
  91. in the same process (main), but latter
  92. (before tcp), so make sure you don't init things
  93. twice, bot in PROC_MAIN and PROC_INT */
  94. #define PROC_NOCHLDINIT -128 /* no child init functions will be called
  95. if this rank is used in fork_process() */
  96. #define PROC_MIN PROC_NOCHLDINIT /* Minimum process rank */
  97. #define MODULE_VERSION \
  98. char *module_version=SER_FULL_VERSION; \
  99. char *module_flags=SER_COMPILE_FLAGS;
  100. struct cmd_export_ {
  101. char* name; /* null terminated command name */
  102. cmd_function function; /* pointer to the corresponding function */
  103. int param_no; /* number of parameters used by the function */
  104. fixup_function fixup; /* pointer to the function called to "fix" the
  105. parameters */
  106. int flags; /* Function flags */
  107. };
  108. struct param_export_ {
  109. char* name; /* null terminated param. name */
  110. modparam_t type; /* param. type */
  111. void* param_pointer; /* pointer to the param. memory location */
  112. };
  113. enum {
  114. FPARAM_UNSPEC = 0,
  115. FPARAM_STRING = (1 << 0),
  116. FPARAM_STR = (1 << 1),
  117. FPARAM_INT = (1 << 2),
  118. FPARAM_REGEX = (1 << 3),
  119. FPARAM_AVP = (1 << 5),
  120. FPARAM_SELECT = (1 << 6),
  121. FPARAM_SUBST = (1 << 7)
  122. };
  123. /*
  124. * Function parameter
  125. */
  126. typedef struct fparam {
  127. char* orig; /* The original value */
  128. int type; /* Type of parameter */
  129. union {
  130. char* asciiz; /* Zero terminated ASCII string */
  131. struct _str str; /* pointer/len string */
  132. int i; /* Integer value */
  133. regex_t* regex; /* Compiled regular expression */
  134. avp_ident_t avp; /* AVP identifier */
  135. select_t* select; /* select structure */
  136. struct subst_expr* subst; /* Regex substitution */
  137. } v;
  138. } fparam_t;
  139. typedef struct cmd_export_ cmd_export_t;
  140. typedef struct param_export_ param_export_t;
  141. struct module_exports {
  142. char* name; /* null terminated module name */
  143. cmd_export_t* cmds; /* null terminated array of the exported
  144. commands */
  145. rpc_export_t* rpc_methods; /* null terminated array of exported rpc methods */
  146. param_export_t* params; /* null terminated array of the exported
  147. module parameters */
  148. init_function init_f; /* Initialization function */
  149. response_function response_f; /* function used for responses,
  150. returns yes or no; can be null */
  151. destroy_function destroy_f; /* function called when the module should
  152. be "destroyed", e.g: on ser exit;
  153. can be null */
  154. onbreak_function onbreak_f;
  155. child_init_function init_child_f; /* function called by all processes
  156. after the fork */
  157. };
  158. struct sr_module{
  159. char* path;
  160. void* handle;
  161. struct module_exports* exports;
  162. struct sr_module* next;
  163. };
  164. extern struct sr_module* modules; /* global module list*/
  165. extern response_function* mod_response_cbks;/* response callback array */
  166. extern int mod_response_cbk_no; /* size of reponse callbacks array */
  167. int register_builtin_modules();
  168. int register_module(struct module_exports*, char*, void*);
  169. int load_module(char* path);
  170. cmd_export_t* find_export_record(char* name, int param_no, int flags);
  171. cmd_function find_export(char* name, int param_no, int flags);
  172. cmd_function find_mod_export(char* mod, char* name, int param_no, int flags);
  173. rpc_export_t* find_rpc_export(char* name, int flags);
  174. void destroy_modules();
  175. int init_child(int rank);
  176. int init_modules(void);
  177. struct sr_module* find_module_by_name(char* mod);
  178. /*
  179. * Find a parameter with given type and return it's
  180. * address in memory
  181. * If there is no such parameter, NULL is returned
  182. */
  183. void* find_param_export(struct sr_module* mod, char* name, modparam_t type_mask, modparam_t *param_type);
  184. /* modules function prototypes:
  185. * struct module_exports* mod_register(); (type module_register)
  186. * int foo_cmd(struct sip_msg* msg, char* param);
  187. * - returns >0 if ok , <0 on error, 0 to stop processing (==DROP)
  188. * int response_f(struct sip_msg* msg)
  189. * - returns >0 if ok, 0 to drop message
  190. */
  191. /* API function to get other parameters from fixup */
  192. action_u_t *fixup_get_param(void **cur_param, int cur_param_no, int required_param_no);
  193. int fixup_get_param_count(void **cur_param, int cur_param_no);
  194. int fix_flag( modparam_t type, void* val,
  195. char* mod_name, char* param_name, int* flag);
  196. /*
  197. * Common function parameter fixups
  198. */
  199. /*
  200. * Generic parameter fixup function which creates
  201. * fparam_t structure. type parameter contains allowed
  202. * parameter types
  203. */
  204. int fix_param(int type, void** param);
  205. /*
  206. * Fixup variable string, the parameter can be
  207. * AVP, SELECT, or ordinary string. AVP and select
  208. * identifiers will be resolved to their values during
  209. * runtime
  210. *
  211. * The parameter value will be converted to fparam structure
  212. * This function returns -1 on an error
  213. */
  214. int fixup_var_str_12(void** param, int param_no);
  215. /* Same as fixup_var_str_12 but applies to the 1st parameter only */
  216. int fixup_var_str_1(void** param, int param_no);
  217. /* Same as fixup_var_str_12 but applies to the 2nd parameter only */
  218. int fixup_var_str_2(void** param, int param_no);
  219. /*
  220. * Fixup variable integer, the parameter can be
  221. * AVP, SELECT, or ordinary integer. AVP and select
  222. * identifiers will be resolved to their values and
  223. * converted to int if necessary during runtime
  224. *
  225. * The parameter value will be converted to fparam structure
  226. * This function returns -1 on an error
  227. */
  228. int fixup_var_int_12(void** param, int param_no);
  229. /* Same as fixup_var_int_12 but applies to the 1st parameter only */
  230. int fixup_var_int_1(void** param, int param_no);
  231. /* Same as fixup_var_int_12 but applies to the 2nd parameter only */
  232. int fixup_var_int_2(void** param, int param_no);
  233. /*
  234. * The parameter must be a regular expression which must compile, the
  235. * parameter will be converted to compiled regex
  236. */
  237. int fixup_regex_12(void** param, int param_no);
  238. /* Same as fixup_regex_12 but applies to the 1st parameter only */
  239. int fixup_regex_1(void** param, int param_no);
  240. /* Same as fixup_regex_12 but applies to the 2nd parameter only */
  241. int fixup_regex_2(void** param, int param_no);
  242. /*
  243. * The string parameter will be converted to integer
  244. */
  245. int fixup_int_12(void** param, int param_no);
  246. /* Same as fixup_int_12 but applies to the 1st parameter only */
  247. int fixup_int_1(void** param, int param_no);
  248. /* Same as fixup_int_12 but applies to the 2nd parameter only */
  249. int fixup_int_2(void** param, int param_no);
  250. /*
  251. * Parse the parameter as static string, do not resolve
  252. * AVPs or selects, convert the parameter to str structure
  253. */
  254. int fixup_str_12(void** param, int param_no);
  255. /* Same as fixup_str_12 but applies to the 1st parameter only */
  256. int fixup_str_1(void** param, int param_no);
  257. /* Same as fixup_str_12 but applies to the 2nd parameter only */
  258. int fixup_str_2(void** param, int param_no);
  259. /*
  260. * Get the function parameter value as string
  261. * Return values: 0 - Success
  262. * -1 - Cannot get value
  263. */
  264. int get_str_fparam(str* dst, struct sip_msg* msg, fparam_t* param);
  265. /*
  266. * Get the function parameter value as integer
  267. * Return values: 0 - Success
  268. * -1 - Cannot get value
  269. */
  270. int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param);
  271. /**
  272. * Retrieve the compiled RegExp.
  273. * @return: 0 for success, negative on error.
  274. */
  275. int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param);
  276. #endif /* sr_module_h */