sr_module.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /*
  26. * History:
  27. * --------
  28. * 2003-03-10 switched to new module_exports format: updated find_export,
  29. * find_export_param, find_module (andrei)
  30. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  31. * 2003-03-19 Support for flags in find_export (janakj)
  32. * 2003-03-29 cleaning pkg_mallocs introduced (jiri)
  33. * 2003-04-24 module version checking introduced (jiri)
  34. * 2004-09-19 compile flags are checked too (andrei)
  35. * 2005-01-07 removed find_module-overloading problems, added
  36. * find_export_record
  37. * 2006-02-07 added fix_flag (andrei)
  38. * 2008-02-29 store all the reponse callbacks in their own array (andrei)
  39. * 2008-11-17 support dual module interface: ser & kamailio (andrei)
  40. * 2008-11-26 added fparam_free_contents() and fix_param_types (andrei)
  41. */
  42. /**
  43. * @file
  44. * @brief SIP-Router core :: modules loading, structures declarations and utilities
  45. * @ingroup core
  46. * Module: \ref core
  47. */
  48. #include "sr_module.h"
  49. #include "mod_fix.h"
  50. #include "dprint.h"
  51. #include "error.h"
  52. #include "mem/mem.h"
  53. #include "core_cmd.h"
  54. #include "ut.h"
  55. #include "re.h"
  56. #include "route_struct.h"
  57. #include "flags.h"
  58. #include "trim.h"
  59. #include "pvapi.h"
  60. #include "globals.h"
  61. #include "rpc_lookup.h"
  62. #include "sr_compat.h"
  63. #include <sys/stat.h>
  64. #include <regex.h>
  65. #include <dlfcn.h>
  66. #include <strings.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <stddef.h> /* for offsetof */
  70. struct sr_module* modules=0;
  71. /*We need to define this symbol on Solaris becuase libcurl relies on libnspr which looks for this symbol.
  72. If it is not defined, dynamic module loading (dlsym) fails */
  73. #ifdef __OS_solaris
  74. int nspr_use_zone_allocator = 0;
  75. #endif
  76. #ifdef STATIC_EXEC
  77. extern struct module_exports exec_exports;
  78. #endif
  79. #ifdef STATIC_TM
  80. extern struct module_exports tm_exports;
  81. #endif
  82. #ifdef STATIC_MAXFWD
  83. extern struct module_exports maxfwd_exports;
  84. #endif
  85. #ifdef STATIC_AUTH
  86. extern struct module_exports auth_exports;
  87. #endif
  88. #ifdef STATIC_RR
  89. extern struct module_exports rr_exports;
  90. #endif
  91. #ifdef STATIC_USRLOC
  92. extern struct module_exports usrloc_exports;
  93. #endif
  94. #ifdef STATIC_SL
  95. extern struct module_exports sl_exports;
  96. #endif
  97. #ifndef offsetof
  98. #warning "use null pointer dereference for offsetof"
  99. #define offsetof(st, m) \
  100. ((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))
  101. #endif
  102. int mod_response_cbk_no=0;
  103. response_function* mod_response_cbks=0;
  104. /**
  105. * if bit 1 set, SIP worker processes handle RPC commands as well
  106. * if bit 2 set, RPC worker processes handle SIP commands as well
  107. */
  108. static int child_sip_rpc_mode = 0;
  109. #define CHILD_SIP_RPC 1<<0
  110. #define CHILD_RPC_SIP 1<<1
  111. void set_child_sip_rpc_mode(void)
  112. {
  113. child_sip_rpc_mode |= CHILD_SIP_RPC;
  114. }
  115. void set_child_rpc_sip_mode(void)
  116. {
  117. child_sip_rpc_mode |= CHILD_RPC_SIP;
  118. }
  119. int is_rpc_worker(int rank)
  120. {
  121. if(rank==PROC_RPC
  122. || (rank>PROC_MAIN && (child_sip_rpc_mode&CHILD_SIP_RPC)!=0))
  123. return 1;
  124. return 0;
  125. }
  126. int is_sip_worker(int rank)
  127. {
  128. if(rank>PROC_MAIN
  129. || ((rank==PROC_RPC || rank==PROC_NOCHLDINIT)
  130. && (child_sip_rpc_mode&CHILD_RPC_SIP)!=0))
  131. return 1;
  132. return 0;
  133. }
  134. /* initializes statically built (compiled in) modules*/
  135. int register_builtin_modules()
  136. {
  137. int ret;
  138. ret=0;
  139. #ifdef STATIC_TM
  140. ret=register_module(MODULE_INTERFACE_VER, &tm_exports,"built-in", 0);
  141. if (ret<0) return ret;
  142. #endif
  143. #ifdef STATIC_EXEC
  144. ret=register_module(MODULE_INTERFACE_VER, &exec_exports,"built-in", 0);
  145. if (ret<0) return ret;
  146. #endif
  147. #ifdef STATIC_MAXFWD
  148. ret=register_module(MODULE_INTERFACE_VER, &maxfwd_exports, "built-in", 0);
  149. if (ret<0) return ret;
  150. #endif
  151. #ifdef STATIC_AUTH
  152. ret=register_module(MODULE_INTERFACE_VER, &auth_exports, "built-in", 0);
  153. if (ret<0) return ret;
  154. #endif
  155. #ifdef STATIC_RR
  156. ret=register_module(MODULE_INTERFACE_VER, &rr_exports, "built-in", 0);
  157. if (ret<0) return ret;
  158. #endif
  159. #ifdef STATIC_USRLOC
  160. ret=register_module(MODULE_INTERFACE_VER, &usrloc_exports, "built-in", 0);
  161. if (ret<0) return ret;
  162. #endif
  163. #ifdef STATIC_SL
  164. ret=register_module(MODULE_INTERFACE_VER, &sl_exports, "built-in", 0);
  165. if (ret<0) return ret;
  166. #endif
  167. return ret;
  168. }
  169. /** convert cmd exports to current format.
  170. * @param ver - module interface versions (0 == ser, 1 == kam).
  171. * @param src - null terminated array of cmd exports
  172. * (either ser_cmd_export_t or kam_cmd_export_t, depending
  173. * on ver).
  174. * @param mod - pointer to module exports structure.
  175. * @return - pkg_malloc'ed null terminated sr_cmd_export_v31_t array with
  176. * the converted cmd exports or 0 on error.
  177. */
  178. static sr31_cmd_export_t* sr_cmd_exports_convert(unsigned ver,
  179. void* src, void* mod)
  180. {
  181. int i, n;
  182. ser_cmd_export_t* ser_cmd;
  183. kam_cmd_export_t* kam_cmd;
  184. sr31_cmd_export_t* ret;
  185. ser_cmd = 0;
  186. kam_cmd = 0;
  187. ret = 0;
  188. n = 0;
  189. /* count the number of elements */
  190. if (ver == 0) {
  191. ser_cmd = src;
  192. for (; ser_cmd[n].name; n++);
  193. } else if (ver == 1) {
  194. kam_cmd = src;
  195. for (; kam_cmd[n].name; n++);
  196. } else goto error; /* unknown interface version */
  197. /* alloc & init new array */
  198. ret = pkg_malloc(sizeof(*ret)*(n+1));
  199. memset(ret, 0, sizeof(*ret)*(n+1));
  200. /* convert/copy */
  201. for (i=0; i < n; i++) {
  202. if (ver == 0) {
  203. ret[i].name = ser_cmd[i].name;
  204. ret[i].function = ser_cmd[i].function;
  205. ret[i].param_no = ser_cmd[i].param_no;
  206. ret[i].fixup = ser_cmd[i].fixup;
  207. ret[i].free_fixup = 0; /* no present in ser <= 2.1 */
  208. ret[i].flags = ser_cmd[i].flags;
  209. } else {
  210. ret[i].name = kam_cmd[i].name;
  211. ret[i].function = kam_cmd[i].function;
  212. ret[i].param_no = kam_cmd[i].param_no;
  213. ret[i].fixup = kam_cmd[i].fixup;
  214. ret[i].free_fixup = kam_cmd[i].free_fixup;
  215. ret[i].flags = kam_cmd[i].flags;
  216. }
  217. /* 3.1+ specific stuff */
  218. ret[i].fixup_flags = 0;
  219. ret[i].module_exports = mod;
  220. /* fill known free fixups */
  221. if (ret[i].fixup && ret[i].free_fixup == 0)
  222. ret[i].free_fixup = get_fixup_free(ret[i].fixup);
  223. }
  224. return ret;
  225. error:
  226. return 0;
  227. }
  228. /* registers a module, register_f= module register functions
  229. * returns <0 on error, 0 on success */
  230. static int register_module(unsigned ver, union module_exports_u* e,
  231. char* path, void* handle)
  232. {
  233. int ret, i;
  234. struct sr_module* mod;
  235. ret=-1;
  236. /* add module to the list */
  237. if ((mod=pkg_malloc(sizeof(struct sr_module)))==0){
  238. LOG(L_ERR, "load_module: memory allocation failure\n");
  239. ret=E_OUT_OF_MEM;
  240. goto error;
  241. }
  242. memset(mod,0, sizeof(struct sr_module));
  243. mod->path=path;
  244. mod->handle=handle;
  245. mod->orig_mod_interface_ver=ver;
  246. /* convert exports to sr31 format */
  247. if (ver == 0) {
  248. /* ser <= 3.0 */
  249. mod->exports.name = e->v0.name;
  250. if (e->v0.cmds) {
  251. mod->exports.cmds = sr_cmd_exports_convert(ver, e->v0.cmds, mod);
  252. if (mod->exports.cmds == 0) {
  253. ERR("failed to convert module command exports to 3.1 format"
  254. " for module \"%s\" (%s), interface version %d\n",
  255. mod->exports.name, mod->path, ver);
  256. ret = E_UNSPEC;
  257. goto error;
  258. }
  259. }
  260. mod->exports.params = e->v0.params;
  261. mod->exports.init_f = e->v0.init_f;
  262. mod->exports.response_f = e->v0.response_f;
  263. mod->exports.destroy_f = e->v0.destroy_f;
  264. mod->exports.onbreak_f = e->v0.onbreak_f;
  265. mod->exports.init_child_f = e->v0.init_child_f;
  266. mod->exports.dlflags = 0; /* not used in ser <= 3.0 */
  267. mod->exports.rpc_methods = e->v0.rpc_methods;
  268. /* the rest are 0, not used in ser */
  269. } else if (ver == 1) {
  270. /* kamailio <= 3.0 */
  271. mod->exports.name = e->v1.name;
  272. if (e->v1.cmds) {
  273. mod->exports.cmds = sr_cmd_exports_convert(ver, e->v1.cmds, mod);
  274. if (mod->exports.cmds == 0) {
  275. ERR("failed to convert module command exports to 3.1 format"
  276. " for module \"%s\" (%s), interface version %d\n",
  277. mod->exports.name, mod->path, ver);
  278. ret = E_UNSPEC;
  279. goto error;
  280. }
  281. }
  282. mod->exports.params = e->v1.params;
  283. mod->exports.init_f = e->v1.init_f;
  284. mod->exports.response_f = e->v1.response_f;
  285. mod->exports.destroy_f = e->v1.destroy_f;
  286. mod->exports.onbreak_f = 0; /* not used in k <= 3.0 */
  287. mod->exports.init_child_f = e->v1.init_child_f;
  288. mod->exports.dlflags = e->v1.dlflags;
  289. mod->exports.rpc_methods = 0; /* not used in k <= 3.0 */
  290. mod->exports.stats = e->v1.stats;
  291. mod->exports.mi_cmds = e->v1.mi_cmds;
  292. mod->exports.items = e->v1.items;
  293. mod->exports.procs = e->v1.procs;
  294. } else {
  295. ERR("unsupported module interface version %d\n", ver);
  296. ret = E_UNSPEC;
  297. goto error;
  298. }
  299. if (mod->exports.items) {
  300. /* register module pseudo-variables for kamailio modules */
  301. LM_DBG("register PV from: %s\n", mod->exports.name);
  302. if (register_pvars_mod(mod->exports.name, mod->exports.items)!=0) {
  303. LM_ERR("failed to register pseudo-variables for module %s (%s)\n",
  304. mod->exports.name, path);
  305. ret = E_UNSPEC;
  306. goto error;
  307. }
  308. }
  309. if (mod->exports.rpc_methods){
  310. /* register rpcs for ser modules */
  311. i=rpc_register_array(mod->exports.rpc_methods);
  312. if (i<0){
  313. ERR("failed to register RPCs for module %s (%s)\n",
  314. mod->exports.name, path);
  315. ret = E_UNSPEC;
  316. goto error;
  317. }else if (i>0){
  318. ERR("%d duplicate RPCs name detected while registering RPCs"
  319. " declared in module %s (%s)\n",
  320. i, mod->exports.name, path);
  321. ret = E_UNSPEC;
  322. goto error;
  323. }
  324. /* i==0 => success */
  325. }
  326. /* link module in the list */
  327. mod->next=modules;
  328. modules=mod;
  329. return 0;
  330. error:
  331. if (mod)
  332. pkg_free(mod);
  333. return ret;
  334. }
  335. #ifndef DLSYM_PREFIX
  336. /* define it to null */
  337. #define DLSYM_PREFIX
  338. #endif
  339. static inline int version_control(void *handle, char *path)
  340. {
  341. char **m_ver;
  342. char **m_flags;
  343. char* error;
  344. m_ver=(char **)dlsym(handle, DLSYM_PREFIX "module_version");
  345. if ((error=(char *)dlerror())!=0) {
  346. LOG(L_ERR, "ERROR: no version info in module <%s>: %s\n",
  347. path, error );
  348. return 0;
  349. }
  350. m_flags=(char **)dlsym(handle, DLSYM_PREFIX "module_flags");
  351. if ((error=(char *)dlerror())!=0) {
  352. LOG(L_ERR, "ERROR: no compile flags info in module <%s>: %s\n",
  353. path, error );
  354. return 0;
  355. }
  356. if (!m_ver || !(*m_ver)) {
  357. LOG(L_ERR, "ERROR: no version in module <%s>\n", path );
  358. return 0;
  359. }
  360. if (!m_flags || !(*m_flags)) {
  361. LOG(L_ERR, "ERROR: no compile flags in module <%s>\n", path );
  362. return 0;
  363. }
  364. if (strcmp(SER_FULL_VERSION, *m_ver)==0){
  365. if (strcmp(SER_COMPILE_FLAGS, *m_flags)==0)
  366. return 1;
  367. else {
  368. LOG(L_ERR, "ERROR: module compile flags mismatch for %s "
  369. " \ncore: %s \nmodule: %s\n",
  370. path, SER_COMPILE_FLAGS, *m_flags);
  371. return 0;
  372. }
  373. }
  374. LOG(L_ERR, "ERROR: module version mismatch for %s; "
  375. "core: %s; module: %s\n", path, SER_FULL_VERSION, *m_ver );
  376. return 0;
  377. }
  378. /**
  379. * \brief load a sr module
  380. *
  381. * tries to load the module specified by mod_path.
  382. * If mod_path is 'modname' or 'modname.so' then
  383. * \<MODS_DIR\>/\<modname\>.so will be tried and if this fails
  384. * \<MODS_DIR\>/\<modname\>/\<modname\>.so
  385. * If mod_path contain a '/' it is assumed to be the
  386. * path to the module and tried first. If fails and mod_path is not
  387. * absolute path (not starting with '/') then will try:
  388. * \<MODS_DIR\>/mod_path
  389. * @param mod_path path or module name
  390. * @return 0 on success , <0 on error
  391. */
  392. int load_module(char* mod_path)
  393. {
  394. void* handle;
  395. char* error;
  396. mod_register_function mr;
  397. union module_exports_u* exp;
  398. unsigned* mod_if_ver;
  399. struct sr_module* t;
  400. struct stat stat_buf;
  401. str modname;
  402. char* mdir;
  403. char* nxt_mdir;
  404. char* path;
  405. int mdir_len;
  406. int len;
  407. int dlflags;
  408. int new_dlflags;
  409. int retries;
  410. int path_type;
  411. #ifndef RTLD_NOW
  412. /* for openbsd */
  413. #define RTLD_NOW DL_LAZY
  414. #endif
  415. path=mod_path;
  416. path_type = 0;
  417. modname.s = path;
  418. modname.len = strlen(mod_path);
  419. if(modname.len>3 && strcmp(modname.s+modname.len-3, ".so")==0) {
  420. path_type = 1;
  421. modname.len -= 3;
  422. }
  423. if (!strchr(path, '/'))
  424. path_type |= 2;
  425. if((path_type&2) || path[0] != '/') {
  426. /* module name was given, we try to construct the path */
  427. mdir=mods_dir; /* search path */
  428. do{
  429. nxt_mdir=strchr(mdir, ':');
  430. if (nxt_mdir) mdir_len=(int)(nxt_mdir-mdir);
  431. else mdir_len=strlen(mdir);
  432. if(path_type&2) {
  433. /* try path <MODS_DIR>/<modname>.so */
  434. path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
  435. modname.len + 3 /* ".so" */ + 1);
  436. if (path==0) goto error;
  437. memcpy(path, mdir, mdir_len);
  438. len = mdir_len;
  439. if (len != 0 && path[len - 1] != '/'){
  440. path[len]='/';
  441. len++;
  442. }
  443. path[len]=0;
  444. strcat(path, modname.s);
  445. if(!(path_type&1))
  446. strcat(path, ".so");
  447. if (stat(path, &stat_buf) == -1) {
  448. DBG("load_module: module file not found <%s>\n", path);
  449. pkg_free(path);
  450. /* try path <MODS_DIR>/<modname>/<modname>.so */
  451. path = (char*)pkg_malloc(
  452. mdir_len + 1 /* "/" */ +
  453. modname.len + 1 /* "/" */ +
  454. modname.len + 3 /* ".so" */ + 1);
  455. if (path==0) goto error;
  456. memcpy(path, mdir, mdir_len);
  457. len = mdir_len;
  458. if (len != 0 && path[len - 1] != '/') {
  459. path[len]='/';
  460. len++;
  461. }
  462. path[len]=0;
  463. strncat(path, modname.s, modname.len);
  464. strcat(path, "/");
  465. strcat(path, modname.s);
  466. if(!(path_type&1))
  467. strcat(path, ".so");
  468. if (stat(path, &stat_buf) == -1) {
  469. DBG("load_module: module file not found <%s>\n", path);
  470. pkg_free(path);
  471. path=0;
  472. }
  473. }
  474. } else {
  475. /* try mod_path - S compat */
  476. if(path==mod_path) {
  477. if (stat(path, &stat_buf) == -1) {
  478. DBG("load_module: module file not found <%s>\n", path);
  479. path=0;
  480. }
  481. }
  482. if(path==0) {
  483. /* try path <MODS_DIR>/mod_path - K compat */
  484. path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
  485. strlen(mod_path) + 1);
  486. if (path==0) goto error;
  487. memcpy(path, mdir, mdir_len);
  488. len = mdir_len;
  489. if (len != 0 && path[len - 1] != '/'){
  490. path[len]='/';
  491. len++;
  492. }
  493. path[len]=0;
  494. strcat(path, mod_path);
  495. if (stat(path, &stat_buf) == -1) {
  496. DBG("load_module: module file not found <%s>\n", path);
  497. pkg_free(path);
  498. path=0;
  499. }
  500. }
  501. }
  502. mdir=nxt_mdir?nxt_mdir+1:0;
  503. }while(path==0 && mdir);
  504. if (path==0){
  505. LOG(L_ERR, "ERROR: load_module: could not find module <%.*s> in"
  506. " <%s>\n", modname.len, modname.s, mods_dir);
  507. goto error;
  508. }
  509. }
  510. DBG("load_module: trying to load <%s>\n", path);
  511. retries=2;
  512. dlflags=RTLD_NOW;
  513. reload:
  514. handle=dlopen(path, dlflags); /* resolve all symbols now */
  515. if (handle==0){
  516. LOG(L_ERR, "ERROR: load_module: could not open module <%s>: %s\n",
  517. path, dlerror());
  518. goto error;
  519. }
  520. for(t=modules;t; t=t->next){
  521. if (t->handle==handle){
  522. LOG(L_WARN, "WARNING: load_module: attempting to load the same"
  523. " module twice (%s)\n", path);
  524. goto skip;
  525. }
  526. }
  527. /* version control */
  528. if (!version_control(handle, path)) {
  529. exit(0);
  530. }
  531. mod_if_ver = (unsigned *)dlsym(handle,
  532. DLSYM_PREFIX "module_interface_ver");
  533. if ( (error =(char*)dlerror())!=0 ){
  534. LOG(L_ERR, "ERROR: no module interface version in module <%s>\n",
  535. path );
  536. goto error1;
  537. }
  538. /* launch register */
  539. mr = (mod_register_function)dlsym(handle, DLSYM_PREFIX "mod_register");
  540. if (((error =(char*)dlerror())==0) && mr) {
  541. /* no error call it */
  542. new_dlflags=dlflags;
  543. if (mr(path, &new_dlflags, 0, 0)!=0) {
  544. LOG(L_ERR, "ERROR: load_module: %s: mod_register failed\n", path);
  545. goto error1;
  546. }
  547. if (new_dlflags!=dlflags && new_dlflags!=0) {
  548. /* we have to reload the module */
  549. dlclose(handle);
  550. dlflags=new_dlflags;
  551. retries--;
  552. if (retries>0) goto reload;
  553. LOG(L_ERR, "ERROR: load_module: %s: cannot agree"
  554. " on the dlflags\n", path);
  555. goto error;
  556. }
  557. }
  558. exp = (union module_exports_u*)dlsym(handle, DLSYM_PREFIX "exports");
  559. if ( (error =(char*)dlerror())!=0 ){
  560. LOG(L_ERR, "ERROR: load_module: %s\n", error);
  561. goto error1;
  562. }
  563. /* hack to allow for kamailio style dlflags inside exports */
  564. if (*mod_if_ver == 1) {
  565. new_dlflags = exp->v1.dlflags;
  566. if (new_dlflags!=dlflags && new_dlflags!=DEFAULT_DLFLAGS) {
  567. /* we have to reload the module */
  568. dlclose(handle);
  569. NOTICE("%s: exports dlflags interface is deprecated and it will not"
  570. " be supported in newer versions; consider using"
  571. " mod_register() instead\n", path);
  572. dlflags=new_dlflags;
  573. retries--;
  574. if (retries>0) goto reload;
  575. LOG(L_ERR, "ERROR: load_module: %s: cannot agree"
  576. " on the dlflags\n", path);
  577. goto error;
  578. }
  579. }
  580. if (register_module(*mod_if_ver, exp, path, handle)<0) goto error1;
  581. return 0;
  582. error1:
  583. dlclose(handle);
  584. error:
  585. skip:
  586. if (path && path!=mod_path)
  587. pkg_free(path);
  588. return -1;
  589. }
  590. /* searches the module list for function name in module mod and returns
  591. * a pointer to the "name" function record union or 0 if not found
  592. * sets also *mod_if_ver to the original module interface version.
  593. * mod==0 is a wildcard matching all modules
  594. * flags parameter is OR value of all flags that must match
  595. */
  596. sr31_cmd_export_t* find_mod_export_record(char* mod, char* name,
  597. int param_no, int flags,
  598. unsigned* mod_if_ver)
  599. {
  600. struct sr_module* t;
  601. sr31_cmd_export_t* cmd;
  602. for(t=modules;t;t=t->next){
  603. if (mod!=0 && (strcmp(t->exports.name, mod) !=0))
  604. continue;
  605. if (t->exports.cmds)
  606. for(cmd=&t->exports.cmds[0]; cmd->name; cmd++) {
  607. if((strcmp(name, cmd->name) == 0) &&
  608. ((cmd->param_no == param_no) ||
  609. (cmd->param_no==VAR_PARAM_NO)) &&
  610. ((cmd->flags & flags) == flags)
  611. ){
  612. DBG("find_export_record: found <%s> in module %s [%s]\n",
  613. name, t->exports.name, t->path);
  614. *mod_if_ver=t->orig_mod_interface_ver;
  615. return cmd;
  616. }
  617. }
  618. }
  619. DBG("find_export_record: <%s> not found \n", name);
  620. return 0;
  621. }
  622. /* searches the module list for function name and returns
  623. * a pointer to the "name" function record union or 0 if not found
  624. * sets also *mod_if_ver to the module interface version (needed to know
  625. * which member of the union should be accessed v0 or v1)
  626. * mod==0 is a wildcard matching all modules
  627. * flags parameter is OR value of all flags that must match
  628. */
  629. sr31_cmd_export_t* find_export_record(char* name,
  630. int param_no, int flags,
  631. unsigned* mod_if_ver)
  632. {
  633. return find_mod_export_record(0, name, param_no, flags, mod_if_ver);
  634. }
  635. cmd_function find_export(char* name, int param_no, int flags)
  636. {
  637. sr31_cmd_export_t* cmd;
  638. unsigned mver;
  639. cmd = find_export_record(name, param_no, flags, &mver);
  640. return cmd?cmd->function:0;
  641. }
  642. rpc_export_t* find_rpc_export(char* name, int flags)
  643. {
  644. return rpc_lookup((char*)name, strlen(name));
  645. }
  646. /*
  647. * searches the module list and returns pointer to "name" function in module
  648. * "mod"
  649. * 0 if not found
  650. * flags parameter is OR value of all flags that must match
  651. */
  652. cmd_function find_mod_export(char* mod, char* name, int param_no, int flags)
  653. {
  654. sr31_cmd_export_t* cmd;
  655. unsigned mver;
  656. cmd=find_mod_export_record(mod, name, param_no, flags, &mver);
  657. if (cmd)
  658. return cmd->function;
  659. DBG("find_mod_export: <%s> in module <%s> not found\n", name, mod);
  660. return 0;
  661. }
  662. struct sr_module* find_module_by_name(char* mod) {
  663. struct sr_module* t;
  664. for(t = modules; t; t = t->next) {
  665. if (strcmp(mod, t->exports.name) == 0) {
  666. return t;
  667. }
  668. }
  669. DBG("find_module_by_name: module <%s> not found\n", mod);
  670. return 0;
  671. }
  672. /*!
  673. * \brief Find a parameter with given type
  674. * \param mod module
  675. * \param name parameter name
  676. * \param type_mask parameter mask
  677. * \param param_type parameter type
  678. * \return parameter address in memory, if there is no such parameter, NULL is returned
  679. */
  680. void* find_param_export(struct sr_module* mod, char* name,
  681. modparam_t type_mask, modparam_t *param_type)
  682. {
  683. param_export_t* param;
  684. if (!mod)
  685. return 0;
  686. for(param = mod->exports.params ;param && param->name ; param++) {
  687. if ((strcmp(name, param->name) == 0) &&
  688. ((param->type & PARAM_TYPE_MASK(type_mask)) != 0)) {
  689. DBG("find_param_export: found <%s> in module %s [%s]\n",
  690. name, mod->exports.name, mod->path);
  691. *param_type = param->type;
  692. return param->param_pointer;
  693. }
  694. }
  695. DBG("find_param_export: parameter <%s> not found in module <%s>\n",
  696. name, mod->exports.name);
  697. return 0;
  698. }
  699. void destroy_modules()
  700. {
  701. struct sr_module* t, *foo;
  702. /* call first destroy function from each module */
  703. t=modules;
  704. while(t) {
  705. foo=t->next;
  706. if (t->exports.destroy_f){
  707. t->exports.destroy_f();
  708. }
  709. t=foo;
  710. }
  711. /* free module exports structures */
  712. t=modules;
  713. while(t) {
  714. foo=t->next;
  715. pkg_free(t);
  716. t=foo;
  717. }
  718. modules=0;
  719. if (mod_response_cbks){
  720. pkg_free(mod_response_cbks);
  721. mod_response_cbks=0;
  722. }
  723. }
  724. #ifdef NO_REVERSE_INIT
  725. /*
  726. * Initialize all loaded modules, the initialization
  727. * is done *AFTER* the configuration file is parsed
  728. */
  729. int init_modules(void)
  730. {
  731. struct sr_module* t;
  732. for(t = modules; t; t = t->next) {
  733. if (t->exports.init_f)
  734. if (t->exports.init_f() != 0) {
  735. LOG(L_ERR, "init_modules(): Error while"
  736. " initializing module %s\n", t->exports.name);
  737. return -1;
  738. }
  739. if (t->exports.response_f)
  740. mod_response_cbk_no++;
  741. }
  742. mod_response_cbks=pkg_malloc(mod_response_cbk_no *
  743. sizeof(response_function));
  744. if (mod_response_cbks==0){
  745. LOG(L_ERR, "init_modules(): memory allocation failure"
  746. " for %d response_f callbacks\n", mod_response_cbk_no);
  747. return -1;
  748. }
  749. for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next) {
  750. if (t->exports.response_f) {
  751. mod_response_cbks[i]=t->exports.response_f;
  752. i++;
  753. }
  754. }
  755. return 0;
  756. }
  757. /*
  758. * per-child initialization
  759. */
  760. int init_child(int rank)
  761. {
  762. struct sr_module* t;
  763. char* type;
  764. switch(rank) {
  765. case PROC_MAIN: type = "PROC_MAIN"; break;
  766. case PROC_TIMER: type = "PROC_TIMER"; break;
  767. case PROC_FIFO: type = "PROC_FIFO"; break;
  768. case PROC_TCP_MAIN: type = "PROC_TCP_MAIN"; break;
  769. default: type = "CHILD"; break;
  770. }
  771. DBG("init_child: initializing %s with rank %d\n", type, rank);
  772. for(t = modules; t; t = t->next) {
  773. if (t->exports.init_child_f) {
  774. if ((t->exports.init_child_f(rank)) < 0) {
  775. LOG(L_ERR, "init_child(): Initialization of child"
  776. " %d failed\n", rank);
  777. return -1;
  778. }
  779. }
  780. }
  781. return 0;
  782. }
  783. #else
  784. /* recursive module child initialization; (recursion is used to
  785. process the module linear list in the same order in
  786. which modules are loaded in config file
  787. */
  788. static int init_mod_child( struct sr_module* m, int rank )
  789. {
  790. if (m) {
  791. /* iterate through the list; if error occurs,
  792. propagate it up the stack
  793. */
  794. if (init_mod_child(m->next, rank)!=0) return -1;
  795. if (m->exports.init_child_f) {
  796. DBG("DEBUG: init_mod_child (%d): %s\n", rank, m->exports.name);
  797. if (m->exports.init_child_f(rank)<0) {
  798. LOG(L_ERR, "init_mod_child(): Error while"
  799. " initializing module %s (%s)\n",
  800. m->exports.name, m->path);
  801. return -1;
  802. } else {
  803. /* module correctly initialized */
  804. return 0;
  805. }
  806. }
  807. /* no init function -- proceed with success */
  808. return 0;
  809. } else {
  810. /* end of list */
  811. return 0;
  812. }
  813. }
  814. /*
  815. * per-child initialization
  816. */
  817. int init_child(int rank)
  818. {
  819. return init_mod_child(modules, rank);
  820. }
  821. /* recursive module initialization; (recursion is used to
  822. process the module linear list in the same order in
  823. which modules are loaded in config file
  824. */
  825. static int init_mod( struct sr_module* m )
  826. {
  827. if (m) {
  828. /* iterate through the list; if error occurs,
  829. propagate it up the stack
  830. */
  831. if (init_mod(m->next)!=0) return -1;
  832. if (m->exports.init_f) {
  833. DBG("DEBUG: init_mod: %s\n", m->exports.name);
  834. if (m->exports.init_f()!=0) {
  835. LOG(L_ERR, "init_mod(): Error while initializing"
  836. " module %s (%s)\n",
  837. m->exports.name, m->path);
  838. return -1;
  839. } else {
  840. /* module correctly initialized */
  841. return 0;
  842. }
  843. }
  844. /* no init function -- proceed with success */
  845. return 0;
  846. } else {
  847. /* end of list */
  848. return 0;
  849. }
  850. }
  851. /*
  852. * Initialize all loaded modules, the initialization
  853. * is done *AFTER* the configuration file is parsed
  854. */
  855. int init_modules(void)
  856. {
  857. struct sr_module* t;
  858. int i;
  859. i = init_mod(modules);
  860. if(i!=0)
  861. return i;
  862. for(t = modules; t; t = t->next)
  863. if (t->exports.response_f)
  864. mod_response_cbk_no++;
  865. mod_response_cbks=pkg_malloc(mod_response_cbk_no *
  866. sizeof(response_function));
  867. if (mod_response_cbks==0){
  868. LOG(L_ERR, "init_modules(): memory allocation failure"
  869. " for %d response_f callbacks\n", mod_response_cbk_no);
  870. return -1;
  871. }
  872. for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next)
  873. if (t->exports.response_f) {
  874. mod_response_cbks[i]=t->exports.response_f;
  875. i++;
  876. }
  877. return 0;
  878. }
  879. #endif
  880. action_u_t *fixup_get_param(void **cur_param, int cur_param_no,
  881. int required_param_no)
  882. {
  883. action_u_t *a;
  884. /* cur_param points to a->u.string, get pointer to a */
  885. a = (void*) ((char *)cur_param - offsetof(action_u_t, u.string));
  886. return a + required_param_no - cur_param_no;
  887. }
  888. int fixup_get_param_count(void **cur_param, int cur_param_no)
  889. {
  890. action_u_t *a;
  891. a = fixup_get_param(cur_param, cur_param_no, 0);
  892. if (a)
  893. return a->u.number;
  894. else
  895. return -1;
  896. }
  897. /** get a pointer to a parameter internal type.
  898. * @param param
  899. * @return pointer to the parameter internal type.
  900. */
  901. action_param_type* fixup_get_param_ptype(void** param)
  902. {
  903. action_u_t* a;
  904. a = (void*)((char*)param - offsetof(action_u_t, u.string));
  905. return &a->type;
  906. }
  907. /** get a parameter internal type.
  908. * @see fixup_get_param_ptype().
  909. * @return paramter internal type.
  910. */
  911. action_param_type fixup_get_param_type(void** param)
  912. {
  913. return *fixup_get_param_ptype(param);
  914. }
  915. /* fixes flag params (resolves possible named flags)
  916. * use PARAM_USE_FUNC|PARAM_STRING as a param. type and create
  917. * a wrapper function that does just:
  918. * return fix_flag(type, val, "my_module", "my_param", &flag_var)
  919. * see also param_func_t.
  920. */
  921. int fix_flag( modparam_t type, void* val,
  922. char* mod_name, char* param_name, int* flag)
  923. {
  924. int num;
  925. int err;
  926. int f, len;
  927. char* s;
  928. char *p;
  929. if ((type & PARAM_STRING)==0){
  930. LOG(L_CRIT, "BUG: %s: fix_flag(%s): bad parameter type\n",
  931. mod_name, param_name);
  932. return -1;
  933. }
  934. s=(char*)val;
  935. len=strlen(s);
  936. f=-1;
  937. /* try to see if it's a number */
  938. num = str2s(s, len, &err);
  939. if (err != 0) {
  940. /* see if it's in the name:<no> format */
  941. p=strchr(s, ':');
  942. if (p){
  943. f= str2s(p+1, strlen(p+1), &err);
  944. if (err!=0){
  945. LOG(L_ERR, "ERROR: %s: invalid %s format:"
  946. " \"%s\"", mod_name, param_name, s);
  947. return -1;
  948. }
  949. *p=0;
  950. }
  951. if ((num=get_flag_no(s, len))<0){
  952. /* not declared yet, declare it */
  953. num=register_flag(s, f);
  954. }
  955. if (num<0){
  956. LOG(L_ERR, "ERROR: %s: bad %s %s\n", mod_name, param_name, s);
  957. return -1;
  958. } else if ((f>0) && (num!=f)){
  959. LOG(L_ERR, "WARNING: %s: flag %s already defined"
  960. " as %d (and not %d), using %s:%d\n",
  961. mod_name, s, num, f, s, num);
  962. }
  963. }
  964. *flag=num;
  965. return 0;
  966. }
  967. /*
  968. * Common function parameter fixups
  969. */
  970. /** Generic parameter fixup function.
  971. * Creates a fparam_t structure.
  972. * @param type contains allowed parameter types
  973. * @param param is the parameter that will be fixed-up
  974. *
  975. * @return
  976. * 0 on success,
  977. * 1 if the param doesn't match the specified type
  978. * <0 on failure
  979. */
  980. int fix_param(int type, void** param)
  981. {
  982. fparam_t* p;
  983. str name, s;
  984. int num;
  985. int err;
  986. p = (fparam_t*)pkg_malloc(sizeof(fparam_t));
  987. if (!p) {
  988. ERR("No memory left\n");
  989. return E_OUT_OF_MEM;
  990. }
  991. memset(p, 0, sizeof(fparam_t));
  992. p->orig = *param;
  993. switch(type) {
  994. case FPARAM_UNSPEC:
  995. ERR("Invalid type value\n");
  996. goto error;
  997. case FPARAM_STRING:
  998. p->v.asciiz = *param;
  999. /* no break */
  1000. case FPARAM_STR:
  1001. p->v.str.s = (char*)*param;
  1002. p->v.str.len = strlen(p->v.str.s);
  1003. p->fixed = &p->v;
  1004. break;
  1005. case FPARAM_INT:
  1006. s.s = (char*)*param;
  1007. s.len = strlen(s.s);
  1008. err = str2sint(&s, &num);
  1009. if (err == 0) {
  1010. p->v.i = (int)num;
  1011. } else {
  1012. /* Not a number */
  1013. pkg_free(p);
  1014. return 1;
  1015. }
  1016. p->fixed = (void*)(long)num;
  1017. break;
  1018. case FPARAM_REGEX:
  1019. if ((p->v.regex = pkg_malloc(sizeof(regex_t))) == 0) {
  1020. ERR("No memory left\n");
  1021. goto error;
  1022. }
  1023. if (regcomp(p->v.regex, *param,
  1024. REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
  1025. pkg_free(p->v.regex);
  1026. p->v.regex=0;
  1027. /* not a valid regex */
  1028. goto no_match;
  1029. }
  1030. p->fixed = p->v.regex;
  1031. break;
  1032. case FPARAM_AVP:
  1033. name.s = (char*)*param;
  1034. name.len = strlen(name.s);
  1035. trim(&name);
  1036. if (!name.len || name.s[0] != '$') {
  1037. /* Not an AVP identifier */
  1038. goto no_match;
  1039. }
  1040. name.s++;
  1041. name.len--;
  1042. if (parse_avp_ident(&name, &p->v.avp) < 0) {
  1043. /* invalid avp identifier (=> no match) */
  1044. goto no_match;
  1045. }
  1046. p->fixed = &p->v;
  1047. break;
  1048. case FPARAM_SELECT:
  1049. name.s = (char*)*param;
  1050. name.len = strlen(name.s);
  1051. trim(&name);
  1052. if (!name.len || name.s[0] != '@') {
  1053. /* Not a select identifier */
  1054. goto no_match;
  1055. }
  1056. if (parse_select(&name.s, &p->v.select) < 0) {
  1057. ERR("Error while parsing select identifier\n");
  1058. goto error;
  1059. }
  1060. p->fixed = &p->v;
  1061. break;
  1062. case FPARAM_SUBST:
  1063. s.s = *param;
  1064. s.len = strlen(s.s);
  1065. p->v.subst = subst_parser(&s);
  1066. if (!p->v.subst) {
  1067. ERR("Error while parsing regex substitution\n");
  1068. goto error;
  1069. }
  1070. p->fixed = &p->v;
  1071. break;
  1072. case FPARAM_PVS:
  1073. name.s = (char*)*param;
  1074. name.len = strlen(name.s);
  1075. trim(&name);
  1076. if (!name.len || name.s[0] != '$'){
  1077. /* not a pvs identifier */
  1078. goto no_match;
  1079. }
  1080. p->v.pvs=pkg_malloc(sizeof(pv_spec_t));
  1081. if (p->v.pvs==0){
  1082. ERR("out of memory while parsing pv_spec_t\n");
  1083. goto error;
  1084. }
  1085. if (pv_parse_spec2(&name, p->v.pvs, 1)==0){
  1086. /* not a valid pvs identifier (but it might be an avp) */
  1087. pkg_free(p->v.pvs);
  1088. p->v.pvs=0;
  1089. goto no_match;
  1090. }
  1091. p->fixed = p->v.pvs;
  1092. break;
  1093. case FPARAM_PVE:
  1094. name.s = (char*)*param;
  1095. name.len = strlen(name.s);
  1096. if (pv_parse_format(&name, &p->v.pve)<0){
  1097. ERR("bad PVE format: \"%.*s\"\n", name.len, name.s);
  1098. goto error;
  1099. }
  1100. p->fixed = &p->v;
  1101. break;
  1102. }
  1103. p->type = type;
  1104. *param = (void*)p;
  1105. return 0;
  1106. no_match:
  1107. pkg_free(p);
  1108. return 1;
  1109. error:
  1110. pkg_free(p);
  1111. return E_UNSPEC;
  1112. }
  1113. /** fparam_t free function.
  1114. * Frees the "content" of a fparam, but not the fparam itself.
  1115. * Note: it doesn't free fp->orig!
  1116. * Assumes pkg_malloc'ed content.
  1117. * @param fp - fparam to be freed
  1118. *
  1119. */
  1120. void fparam_free_contents(fparam_t* fp)
  1121. {
  1122. if (fp==0)
  1123. return;
  1124. switch(fp->type) {
  1125. case FPARAM_UNSPEC:
  1126. case FPARAM_STRING: /* asciiz string, not str */
  1127. case FPARAM_INT:
  1128. case FPARAM_STR:
  1129. /* nothing to do */
  1130. break;
  1131. case FPARAM_REGEX:
  1132. if (fp->v.regex){
  1133. regfree(fp->v.regex);
  1134. pkg_free(fp->v.regex);
  1135. fp->v.regex=0;
  1136. }
  1137. break;
  1138. case FPARAM_AVP:
  1139. free_avp_name(&fp->v.avp.flags, &fp->v.avp.name);
  1140. break;
  1141. case FPARAM_SELECT:
  1142. if (fp->v.select){
  1143. free_select(fp->v.select);
  1144. fp->v.select=0;
  1145. }
  1146. break;
  1147. case FPARAM_SUBST:
  1148. if (fp->v.subst){
  1149. subst_expr_free(fp->v.subst);
  1150. fp->v.subst=0;
  1151. }
  1152. break;
  1153. case FPARAM_PVS:
  1154. if (fp->v.pvs){
  1155. pv_spec_free(fp->v.pvs);
  1156. fp->v.pvs=0;
  1157. }
  1158. break;
  1159. case FPARAM_PVE:
  1160. if (fp->v.pve){
  1161. pv_elem_free_all(fp->v.pve);
  1162. fp->v.pve=0;
  1163. }
  1164. break;
  1165. }
  1166. }
  1167. /**
  1168. * @brief Generic free fixup type function for a fixed fparam
  1169. *
  1170. * Generic free fixup type function for a fixed fparam. It will free whatever
  1171. * was allocated during the initial fparam fixup and restore the original param
  1172. * value.
  1173. * @param param freed parameters
  1174. */
  1175. void fparam_free_restore(void** param)
  1176. {
  1177. fparam_t *fp;
  1178. void *orig;
  1179. fp = *param;
  1180. orig = fp->orig;
  1181. fp->orig = 0;
  1182. fparam_free_contents(fp);
  1183. pkg_free(fp);
  1184. *param = orig;
  1185. }
  1186. /** fix a param to one of the given types (mask).
  1187. *
  1188. * @param types - bitmap of the allowed types (e.g. FPARAM_INT|FPARAM_STR)
  1189. * @param param - value/result
  1190. * @return - 0 on success, -1 on error, 1 if param doesn't
  1191. * match any of the types
  1192. */
  1193. int fix_param_types(int types, void** param)
  1194. {
  1195. int ret;
  1196. int t;
  1197. if (fixup_get_param_type(param) == STRING_RVE_ST &&
  1198. (types & (FPARAM_INT|FPARAM_STR|FPARAM_STRING))) {
  1199. /* if called with a RVE already converted to string =>
  1200. don't try AVP, PVAR or SELECT (to avoid double
  1201. deref., e.g.: $foo="$bar"; f($foo) ) */
  1202. types &= ~ (FPARAM_AVP|FPARAM_PVS|FPARAM_SELECT|FPARAM_PVE);
  1203. }
  1204. for (t=types & ~(types-1); types; types&=(types-1), t=types & ~(types-1)){
  1205. if ((ret=fix_param(t, param))<=0) return ret;
  1206. }
  1207. return E_UNSPEC;
  1208. }
  1209. /*
  1210. * Fixup variable string, the parameter can be
  1211. * AVP, SELECT, or ordinary string. AVP and select
  1212. * identifiers will be resolved to their values during
  1213. * runtime
  1214. *
  1215. * The parameter value will be converted to fparam structure
  1216. * This function returns -1 on an error
  1217. */
  1218. int fixup_var_str_12(void** param, int param_no)
  1219. {
  1220. int ret;
  1221. if (fixup_get_param_type(param) != STRING_RVE_ST) {
  1222. /* if called with a RVE already converted to string =>
  1223. don't try AVP, PVAR or SELECT (to avoid double
  1224. deref., e.g.: $foo="$bar"; f($foo) ) */
  1225. if ((ret = fix_param(FPARAM_PVS, param)) <= 0) return ret;
  1226. if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
  1227. if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
  1228. }
  1229. if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
  1230. ERR("Error while fixing parameter, PV, AVP, SELECT, and str conversions"
  1231. " failed\n");
  1232. return -1;
  1233. }
  1234. /* Same as fixup_var_str_12 but applies to the 1st parameter only */
  1235. int fixup_var_str_1(void** param, int param_no)
  1236. {
  1237. if (param_no == 1) return fixup_var_str_12(param, param_no);
  1238. else return 0;
  1239. }
  1240. /* Same as fixup_var_str_12 but applies to the 2nd parameter only */
  1241. int fixup_var_str_2(void** param, int param_no)
  1242. {
  1243. if (param_no == 2) return fixup_var_str_12(param, param_no);
  1244. else return 0;
  1245. }
  1246. /** fixup variable-pve-only-string.
  1247. * The parameter can be a PVE (pv based format string)
  1248. * or string.
  1249. * non-static PVEs identifiers will be resolved to
  1250. * their values during runtime.
  1251. * The parameter value will be converted to fparam structure
  1252. * @param param - double pointer to param, as for normal fixup functions.
  1253. * @param param_no - parameter number, ignored.
  1254. * @return -1 on an error, 0 on success.
  1255. */
  1256. int fixup_var_pve_12(void** param, int param_no)
  1257. {
  1258. int ret;
  1259. fparam_t* fp;
  1260. if (fixup_get_param_type(param) != STRING_RVE_ST) {
  1261. /* if called with a RVE already converted to string =>
  1262. don't try PVE again (to avoid double
  1263. deref., e.g.: $foo="$bar"; f($foo) ) */
  1264. if ((ret = fix_param(FPARAM_PVE, param)) <= 0) {
  1265. if (ret < 0)
  1266. return ret;
  1267. /* check if it resolved to a dynamic or "static" PVE.
  1268. If the resulting PVE is static (normal string), discard
  1269. it and use the normal string fixup (faster at runtime) */
  1270. fp = (fparam_t*)*param;
  1271. if (fp->v.pve->spec.getf == 0)
  1272. fparam_free_restore(param); /* fallback to STR below */
  1273. else
  1274. return ret; /* dynamic PVE => return */
  1275. }
  1276. }
  1277. if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
  1278. ERR("Error while fixing parameter - PVE or str conversions failed\n");
  1279. return -1;
  1280. }
  1281. /** fixup variable-pve-string.
  1282. * The parameter can be a PVAR, AVP, SELECT, PVE (pv based format string)
  1283. * or string.
  1284. * PVAR, AVP and select and non-static PVEs identifiers will be resolved to
  1285. * their values during runtime.
  1286. * The parameter value will be converted to fparam structure
  1287. * @param param - double pointer to param, as for normal fixup functions.
  1288. * @param param_no - parameter number, ignored.
  1289. * @return -1 on an error, 0 on success.
  1290. */
  1291. int fixup_var_pve_str_12(void** param, int param_no)
  1292. {
  1293. int ret;
  1294. fparam_t* fp;
  1295. if (fixup_get_param_type(param) != STRING_RVE_ST) {
  1296. /* if called with a RVE already converted to string =>
  1297. don't try AVP, PVAR, SELECT or PVE again (to avoid double
  1298. deref., e.g.: $foo="$bar"; f($foo) ) */
  1299. if ((ret = fix_param(FPARAM_PVS, param)) <= 0) return ret;
  1300. if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
  1301. if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
  1302. if ((ret = fix_param(FPARAM_PVE, param)) <= 0) {
  1303. if (ret < 0)
  1304. return ret;
  1305. /* check if it resolved to a dynamic or "static" PVE.
  1306. If the resulting PVE is static (normal string), discard
  1307. it and use the normal string fixup (faster at runtime) */
  1308. fp = (fparam_t*)*param;
  1309. if (fp->v.pve->spec.getf == 0)
  1310. fparam_free_restore(param); /* fallback to STR below */
  1311. else
  1312. return ret; /* dynamic PVE => return */
  1313. }
  1314. }
  1315. if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
  1316. ERR("Error while fixing parameter, PV, AVP, SELECT, and str conversions"
  1317. " failed\n");
  1318. return -1;
  1319. }
  1320. /* Same as fixup_var_pve_str_12 but applies to the 1st parameter only */
  1321. int fixup_var_pve_str_1(void** param, int param_no)
  1322. {
  1323. if (param_no == 1) return fixup_var_pve_str_12(param, param_no);
  1324. else return 0;
  1325. }
  1326. /* Same as fixup_var_pve_str_12 but applies to the 2nd parameter only */
  1327. int fixup_var_pve_str_2(void** param, int param_no)
  1328. {
  1329. if (param_no == 2) return fixup_var_pve_str_12(param, param_no);
  1330. else return 0;
  1331. }
  1332. /*
  1333. * Fixup variable integer, the parameter can be
  1334. * AVP, SELECT, or ordinary integer. AVP and select
  1335. * identifiers will be resolved to their values and
  1336. * converted to int if necessary during runtime
  1337. *
  1338. * The parameter value will be converted to fparam structure
  1339. * This function returns -1 on an error
  1340. */
  1341. int fixup_var_int_12(void** param, int param_no)
  1342. {
  1343. int ret;
  1344. if (fixup_get_param_type(param) != STRING_RVE_ST) {
  1345. /* if called with a RVE already converted to string =>
  1346. don't try AVP, PVAR or SELECT (to avoid double
  1347. deref., e.g.: $foo="$bar"; f($foo) ) */
  1348. if ((ret = fix_param(FPARAM_PVS, param)) <= 0) return ret;
  1349. if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
  1350. if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
  1351. }
  1352. if ((ret = fix_param(FPARAM_INT, param)) <= 0) return ret;
  1353. ERR("Error while fixing parameter, PV, AVP, SELECT, and int conversions"
  1354. " failed\n");
  1355. return -1;
  1356. }
  1357. /* Same as fixup_var_int_12 but applies to the 1st parameter only */
  1358. int fixup_var_int_1(void** param, int param_no)
  1359. {
  1360. if (param_no == 1) return fixup_var_int_12(param, param_no);
  1361. else return 0;
  1362. }
  1363. /* Same as fixup_var_int_12 but applies to the 2nd parameter only */
  1364. int fixup_var_int_2(void** param, int param_no)
  1365. {
  1366. if (param_no == 2) return fixup_var_int_12(param, param_no);
  1367. else return 0;
  1368. }
  1369. /*
  1370. * The parameter must be a regular expression which must compile, the
  1371. * parameter will be converted to compiled regex
  1372. */
  1373. int fixup_regex_12(void** param, int param_no)
  1374. {
  1375. int ret;
  1376. if ((ret = fix_param(FPARAM_REGEX, param)) <= 0) return ret;
  1377. ERR("Error while compiling regex in function parameter\n");
  1378. return -1;
  1379. }
  1380. /* Same as fixup_regex_12 but applies to the 1st parameter only */
  1381. int fixup_regex_1(void** param, int param_no)
  1382. {
  1383. if (param_no == 1) return fixup_regex_12(param, param_no);
  1384. else return 0;
  1385. }
  1386. /* Same as fixup_regex_12 but applies to the 2nd parameter only */
  1387. int fixup_regex_2(void** param, int param_no)
  1388. {
  1389. if (param_no == 2) return fixup_regex_12(param, param_no);
  1390. else return 0;
  1391. }
  1392. /*
  1393. * The string parameter will be converted to integer
  1394. */
  1395. int fixup_int_12(void** param, int param_no)
  1396. {
  1397. int ret;
  1398. if ((ret = fix_param(FPARAM_INT, param)) <= 0) return ret;
  1399. ERR("Cannot function parameter to integer\n");
  1400. return -1;
  1401. }
  1402. /* Same as fixup_int_12 but applies to the 1st parameter only */
  1403. int fixup_int_1(void** param, int param_no)
  1404. {
  1405. if (param_no == 1) return fixup_int_12(param, param_no);
  1406. else return 0;
  1407. }
  1408. /* Same as fixup_int_12 but applies to the 2nd parameter only */
  1409. int fixup_int_2(void** param, int param_no)
  1410. {
  1411. if (param_no == 2) return fixup_int_12(param, param_no);
  1412. else return 0;
  1413. }
  1414. /*
  1415. * Parse the parameter as static string, do not resolve
  1416. * AVPs or selects, convert the parameter to str structure
  1417. */
  1418. int fixup_str_12(void** param, int param_no)
  1419. {
  1420. int ret;
  1421. if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
  1422. ERR("Cannot function parameter to string\n");
  1423. return -1;
  1424. }
  1425. /* Same as fixup_str_12 but applies to the 1st parameter only */
  1426. int fixup_str_1(void** param, int param_no)
  1427. {
  1428. if (param_no == 1) return fixup_str_12(param, param_no);
  1429. else return 0;
  1430. }
  1431. /* Same as fixup_str_12 but applies to the 2nd parameter only */
  1432. int fixup_str_2(void** param, int param_no)
  1433. {
  1434. if (param_no == 2) return fixup_str_12(param, param_no);
  1435. else return 0;
  1436. }
  1437. /** Get the function parameter value as string.
  1438. * @return 0 - Success
  1439. * -1 - Cannot get value
  1440. */
  1441. int get_str_fparam(str* dst, struct sip_msg* msg, fparam_t* param)
  1442. {
  1443. int_str val;
  1444. int ret;
  1445. avp_t* avp;
  1446. pv_value_t pv_val;
  1447. switch(param->type) {
  1448. case FPARAM_REGEX:
  1449. case FPARAM_UNSPEC:
  1450. case FPARAM_INT:
  1451. return -1;
  1452. case FPARAM_STRING:
  1453. dst->s = param->v.asciiz;
  1454. dst->len = strlen(param->v.asciiz);
  1455. break;
  1456. case FPARAM_STR:
  1457. *dst = param->v.str;
  1458. break;
  1459. case FPARAM_AVP:
  1460. avp = search_first_avp(param->v.avp.flags, param->v.avp.name,
  1461. &val, 0);
  1462. if (unlikely(!avp)) {
  1463. DBG("Could not find AVP from function parameter '%s'\n",
  1464. param->orig);
  1465. return -1;
  1466. }
  1467. if (likely(avp->flags & AVP_VAL_STR)) {
  1468. *dst = val.s;
  1469. } else {
  1470. /* The caller does not know of what type the AVP will be so
  1471. * convert int AVPs into string here
  1472. */
  1473. dst->s = int2str(val.n, &dst->len);
  1474. }
  1475. break;
  1476. case FPARAM_SELECT:
  1477. ret = run_select(dst, param->v.select, msg);
  1478. if (unlikely(ret < 0 || ret > 0)) return -1;
  1479. break;
  1480. case FPARAM_PVS:
  1481. if (likely((pv_get_spec_value(msg, param->v.pvs, &pv_val)==0) &&
  1482. ((pv_val.flags&(PV_VAL_NULL|PV_VAL_STR))==PV_VAL_STR))){
  1483. *dst=pv_val.rs;
  1484. }else{
  1485. ERR("Could not convert PV to str\n");
  1486. return -1;
  1487. }
  1488. break;
  1489. case FPARAM_PVE:
  1490. dst->s=pv_get_buffer();
  1491. dst->len=pv_get_buffer_size();
  1492. if (unlikely(pv_printf(msg, param->v.pve, dst->s, &dst->len)!=0)){
  1493. ERR("Could not convert the PV-formated string to str\n");
  1494. dst->len=0;
  1495. return -1;
  1496. };
  1497. break;
  1498. }
  1499. return 0;
  1500. }
  1501. /** Get the function parameter value as integer.
  1502. * @return 0 - Success
  1503. * -1 - Cannot get value
  1504. */
  1505. int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param)
  1506. {
  1507. int_str val;
  1508. int ret;
  1509. avp_t* avp;
  1510. str tmp;
  1511. pv_value_t pv_val;
  1512. switch(param->type) {
  1513. case FPARAM_INT:
  1514. *dst = param->v.i;
  1515. return 0;
  1516. case FPARAM_REGEX:
  1517. case FPARAM_UNSPEC:
  1518. case FPARAM_STRING:
  1519. case FPARAM_STR:
  1520. return -1;
  1521. case FPARAM_AVP:
  1522. avp = search_first_avp(param->v.avp.flags, param->v.avp.name,
  1523. &val, 0);
  1524. if (unlikely(!avp)) {
  1525. DBG("Could not find AVP from function parameter '%s'\n",
  1526. param->orig);
  1527. return -1;
  1528. }
  1529. if (avp->flags & AVP_VAL_STR) {
  1530. if (str2int(&val.s, (unsigned int*)dst) < 0) {
  1531. ERR("Could not convert AVP string value to int\n");
  1532. return -1;
  1533. }
  1534. } else {
  1535. *dst = val.n;
  1536. }
  1537. break;
  1538. case FPARAM_SELECT:
  1539. ret = run_select(&tmp, param->v.select, msg);
  1540. if (unlikely(ret < 0 || ret > 0)) return -1;
  1541. if (unlikely(str2int(&tmp, (unsigned int*)dst) < 0)) {
  1542. ERR("Could not convert select result to int\n");
  1543. return -1;
  1544. }
  1545. break;
  1546. case FPARAM_PVS:
  1547. if (likely((pv_get_spec_value(msg, param->v.pvs, &pv_val)==0) &&
  1548. ((pv_val.flags&(PV_VAL_NULL|PV_VAL_INT))==PV_VAL_INT))){
  1549. *dst=pv_val.ri;
  1550. }else{
  1551. ERR("Could not convert PV to int\n");
  1552. return -1;
  1553. }
  1554. break;
  1555. case FPARAM_PVE:
  1556. return -1;
  1557. }
  1558. return 0;
  1559. }
  1560. /**
  1561. * Retrieve the compiled RegExp.
  1562. * @return: 0 for success, negative on error.
  1563. */
  1564. int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param)
  1565. {
  1566. switch (param->type) {
  1567. case FPARAM_REGEX:
  1568. *dst = *param->v.regex;
  1569. return 0;
  1570. default:
  1571. ERR("unexpected parameter type (%d), instead of regexp.\n",
  1572. param->type);
  1573. }
  1574. return -1;
  1575. }
  1576. /** generic free fixup function for "pure" fparam type fixups.
  1577. * @param param - double pointer to param, as for normal fixup functions.
  1578. * @param param_no - parameter number, ignored.
  1579. * @return 0 on success (always).
  1580. */
  1581. int fixup_free_fparam_all(void** param, int param_no)
  1582. {
  1583. fparam_free_restore(param);
  1584. return 0;
  1585. }
  1586. /** generic free fixup function for "pure" first parameter fparam type fixups.
  1587. * @param param - double pointer to param, as for normal fixup functions.
  1588. * @param param_no - parameter number: the function will work only for
  1589. * param_no == 1 (first parameter).
  1590. * @return 0 on success (always).
  1591. */
  1592. int fixup_free_fparam_1(void** param, int param_no)
  1593. {
  1594. if (param_no == 1)
  1595. fparam_free_restore(param);
  1596. return 0;
  1597. }
  1598. /** generic free fixup function for "pure" 2nd parameter fparam type fixups.
  1599. * @param param - double pointer to param, as for normal fixup functions.
  1600. * @param param_no - parameter number: the function will work only for
  1601. * param_no == 2 (2nd parameter).
  1602. * @return 0 on success (always).
  1603. */
  1604. int fixup_free_fparam_2(void** param, int param_no)
  1605. {
  1606. if (param_no == 2)
  1607. fparam_free_restore(param);
  1608. return 0;
  1609. }
  1610. /** returns true if a fixup is a fparam_t* one.
  1611. * Used to automatically detect "pure" fparam fixups that can be used with non
  1612. * contant RVEs.
  1613. * @param f - function pointer
  1614. * @return 1 for fparam fixups, 0 for others.
  1615. */
  1616. int is_fparam_rve_fixup(fixup_function f)
  1617. {
  1618. if (f == fixup_var_str_12 ||
  1619. f == fixup_var_str_1 ||
  1620. f == fixup_var_str_2 ||
  1621. f == fixup_var_pve_str_12 ||
  1622. f == fixup_var_pve_str_1 ||
  1623. f == fixup_var_pve_str_2 ||
  1624. f == fixup_var_int_12 ||
  1625. f == fixup_var_int_1 ||
  1626. f == fixup_var_int_2 ||
  1627. f == fixup_int_12 ||
  1628. f == fixup_int_1 ||
  1629. f == fixup_int_2 ||
  1630. f == fixup_str_12 ||
  1631. f == fixup_str_1 ||
  1632. f == fixup_str_2 ||
  1633. f == fixup_regex_12 ||
  1634. f == fixup_regex_1 ||
  1635. f == fixup_regex_2
  1636. )
  1637. return 1;
  1638. return 0;
  1639. }
  1640. /**
  1641. * @brief returns the corresponding fixup_free* for various known fixup types
  1642. *
  1643. * Returns the corresponding fixup_free* for various known fixup types.
  1644. * Used to automatically fill in free_fixup* functions.
  1645. * @param f fixup function pointer
  1646. * @return free fixup function pointer on success, 0 on failure (unknown
  1647. * fixup or no free fixup function).
  1648. */
  1649. free_fixup_function get_fixup_free(fixup_function f)
  1650. {
  1651. free_fixup_function ret;
  1652. /* "pure" fparam, all parameters */
  1653. if (f == fixup_var_str_12 ||
  1654. f == fixup_var_pve_str_12 ||
  1655. f == fixup_var_int_12 ||
  1656. f == fixup_int_12 ||
  1657. f == fixup_str_12 ||
  1658. f == fixup_regex_12)
  1659. return fixup_free_fparam_all;
  1660. /* "pure" fparam, 1st parameter */
  1661. if (f == fixup_var_str_1 ||
  1662. f == fixup_var_pve_str_1 ||
  1663. f == fixup_var_int_1 ||
  1664. f == fixup_int_1 ||
  1665. f == fixup_str_1 ||
  1666. f == fixup_regex_1)
  1667. return fixup_free_fparam_1;
  1668. /* "pure" fparam, 2nd parameters */
  1669. if (f == fixup_var_str_2 ||
  1670. f == fixup_var_pve_str_2 ||
  1671. f == fixup_var_int_2 ||
  1672. f == fixup_int_2 ||
  1673. f == fixup_str_2 ||
  1674. f == fixup_regex_2)
  1675. return fixup_free_fparam_2;
  1676. /* mod_fix.h kamailio style fixups */
  1677. if ((ret = mod_fix_get_fixup_free(f)) != 0)
  1678. return ret;
  1679. /* unknown */
  1680. return 0;
  1681. }