sr_module.c 47 KB

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