sr_module.c 50 KB

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