sr_module.c 44 KB

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