sr_module.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. */
  41. #include "sr_module.h"
  42. #include "dprint.h"
  43. #include "error.h"
  44. #include "mem/mem.h"
  45. #include "core_cmd.h"
  46. #include "ut.h"
  47. #include "re.h"
  48. #include "route_struct.h"
  49. #include "flags.h"
  50. #include "trim.h"
  51. #include "globals.h"
  52. #include <sys/stat.h>
  53. #include <regex.h>
  54. #include <dlfcn.h>
  55. #include <strings.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. struct sr_module* modules=0;
  59. #ifdef STATIC_EXEC
  60. extern struct module_exports* exec_exports();
  61. #endif
  62. #ifdef STATIC_TM
  63. extern struct module_exports* tm_exports();
  64. #endif
  65. #ifdef STATIC_MAXFWD
  66. extern struct module_exports* maxfwd_exports();
  67. #endif
  68. #ifdef STATIC_AUTH
  69. extern struct module_exports* auth_exports();
  70. #endif
  71. #ifdef STATIC_RR
  72. extern struct module_exports* rr_exports();
  73. #endif
  74. #ifdef STATIC_USRLOC
  75. extern struct module_exports* usrloc_exports();
  76. #endif
  77. #ifdef STATIC_SL
  78. extern struct module_exports* sl_exports();
  79. #endif
  80. int mod_response_cbk_no=0;
  81. response_function* mod_response_cbks=0;
  82. /* initializes statically built (compiled in) modules*/
  83. int register_builtin_modules()
  84. {
  85. int ret;
  86. ret=0;
  87. #ifdef STATIC_TM
  88. ret=register_module(tm_exports,"built-in", 0);
  89. if (ret<0) return ret;
  90. #endif
  91. #ifdef STATIC_EXEC
  92. ret=register_module(exec_exports,"built-in", 0);
  93. if (ret<0) return ret;
  94. #endif
  95. #ifdef STATIC_MAXFWD
  96. ret=register_module(maxfwd_exports, "built-in", 0);
  97. if (ret<0) return ret;
  98. #endif
  99. #ifdef STATIC_AUTH
  100. ret=register_module(auth_exports, "built-in", 0);
  101. if (ret<0) return ret;
  102. #endif
  103. #ifdef STATIC_RR
  104. ret=register_module(rr_exports, "built-in", 0);
  105. if (ret<0) return ret;
  106. #endif
  107. #ifdef STATIC_USRLOC
  108. ret=register_module(usrloc_exports, "built-in", 0);
  109. if (ret<0) return ret;
  110. #endif
  111. #ifdef STATIC_SL
  112. ret=register_module(sl_exports, "built-in", 0);
  113. if (ret<0) return ret;
  114. #endif
  115. return ret;
  116. }
  117. /* registers a module, register_f= module register functions
  118. * returns <0 on error, 0 on success */
  119. int register_module(struct module_exports* e, char* path, void* handle)
  120. {
  121. int ret;
  122. struct sr_module* mod;
  123. ret=-1;
  124. /* add module to the list */
  125. if ((mod=pkg_malloc(sizeof(struct sr_module)))==0){
  126. LOG(L_ERR, "load_module: memory allocation failure\n");
  127. ret=E_OUT_OF_MEM;
  128. goto error;
  129. }
  130. memset(mod,0, sizeof(struct sr_module));
  131. mod->path=path;
  132. mod->handle=handle;
  133. mod->exports=e;
  134. mod->next=modules;
  135. modules=mod;
  136. return 0;
  137. error:
  138. return ret;
  139. }
  140. #ifndef DLSYM_PREFIX
  141. /* define it to null */
  142. #define DLSYM_PREFIX
  143. #endif
  144. static inline int version_control(void *handle, char *path)
  145. {
  146. char **m_ver;
  147. char **m_flags;
  148. char* error;
  149. m_ver=(char **)dlsym(handle, DLSYM_PREFIX "module_version");
  150. if ((error=(char *)dlerror())!=0) {
  151. LOG(L_ERR, "ERROR: no version info in module <%s>: %s\n",
  152. path, error );
  153. return 0;
  154. }
  155. m_flags=(char **)dlsym(handle, DLSYM_PREFIX "module_flags");
  156. if ((error=(char *)dlerror())!=0) {
  157. LOG(L_ERR, "ERROR: no compile flags info in module <%s>: %s\n",
  158. path, error );
  159. return 0;
  160. }
  161. if (!m_ver || !(*m_ver)) {
  162. LOG(L_ERR, "ERROR: no version in module <%s>\n", path );
  163. return 0;
  164. }
  165. if (!m_flags || !(*m_flags)) {
  166. LOG(L_ERR, "ERROR: no compile flags in module <%s>\n", path );
  167. return 0;
  168. }
  169. if (strcmp(SER_FULL_VERSION, *m_ver)==0){
  170. if (strcmp(SER_COMPILE_FLAGS, *m_flags)==0)
  171. return 1;
  172. else {
  173. LOG(L_ERR, "ERROR: module compile flags mismatch for %s "
  174. " \ncore: %s \nmodule: %s\n",
  175. path, SER_COMPILE_FLAGS, *m_flags);
  176. return 0;
  177. }
  178. }
  179. LOG(L_ERR, "ERROR: module version mismatch for %s; "
  180. "core: %s; module: %s\n", path, SER_FULL_VERSION, *m_ver );
  181. return 0;
  182. }
  183. /* returns 0 on success , <0 on error */
  184. int load_module(char* path)
  185. {
  186. void* handle;
  187. char* error;
  188. struct module_exports* exp;
  189. struct sr_module* t;
  190. struct stat stat_buf;
  191. char* modname;
  192. int len;
  193. #ifndef RTLD_NOW
  194. /* for openbsd */
  195. #define RTLD_NOW DL_LAZY
  196. #endif
  197. if (!strchr(path, '/') && !strchr(path, '.')) {
  198. /* module name was given, we try to construct the path */
  199. modname = path;
  200. /* try path <MODS_DIR>/<modname>.so */
  201. path = (char*)pkg_malloc(
  202. strlen(mods_dir) + 1 /* "/" */ +
  203. strlen(modname) + 3 /* ".so" */ + 1);
  204. strcpy(path, mods_dir);
  205. len = strlen(path);
  206. if (len != 0 && path[len - 1] != '/') {
  207. strcat(path, "/");
  208. }
  209. strcat(path, modname);
  210. strcat(path, ".so");
  211. #ifdef EXTRA_DEBUG
  212. if (stat(path, &stat_buf) == -1) {
  213. DBG("load_module: module file not found <%s>\n", path);
  214. pkg_free(path);
  215. /* try path <MODS_DIR>/<modname>/<modname>.so */
  216. path = (char*)pkg_malloc(
  217. strlen(mods_dir) + 1 /* "/" */ +
  218. strlen(modname) + 1 /* "/" */ +
  219. strlen(modname) + 3 /* ".so" */ + 1);
  220. strcpy(path, mods_dir);
  221. len = strlen(path);
  222. if (len != 0 && path[len - 1] != '/') {
  223. strcat(path, "/");
  224. }
  225. strcat(path, modname);
  226. strcat(path, "/");
  227. strcat(path, modname);
  228. strcat(path, ".so");
  229. if (stat(path, &stat_buf) == -1) {
  230. DBG("load_module: module file not found <%s>\n", path);
  231. pkg_free(path);
  232. LOG(L_ERR, "ERROR: load_module: could not find module <%s>\n",
  233. modname);
  234. goto error;
  235. }
  236. }
  237. #else /* !EXTRA_DEBUG */
  238. if (stat(path, &stat_buf) == -1) {
  239. DBG("load_module: module file not found <%s>\n", path);
  240. pkg_free(path);
  241. LOG(L_ERR, "ERROR: load_module: could not find module <%s>\n",
  242. modname);
  243. goto error;
  244. }
  245. #endif /* !EXTRA_DEBUG */
  246. }
  247. handle=dlopen(path, RTLD_NOW); /* resolve all symbols now */
  248. if (handle==0){
  249. LOG(L_ERR, "ERROR: load_module: could not open module <%s>: %s\n",
  250. path, dlerror());
  251. goto error;
  252. }
  253. for(t=modules;t; t=t->next){
  254. if (t->handle==handle){
  255. LOG(L_WARN, "WARNING: load_module: attempting to load the same"
  256. " module twice (%s)\n", path);
  257. goto skip;
  258. }
  259. }
  260. /* version control */
  261. if (!version_control(handle, path)) {
  262. exit(0);
  263. }
  264. /* launch register */
  265. exp = (struct module_exports*)dlsym(handle, DLSYM_PREFIX "exports");
  266. if ( (error =(char*)dlerror())!=0 ){
  267. LOG(L_ERR, "ERROR: load_module: %s\n", error);
  268. goto error1;
  269. }
  270. if (register_module(exp, path, handle)<0) goto error1;
  271. return 0;
  272. error1:
  273. dlclose(handle);
  274. error:
  275. skip:
  276. return -1;
  277. }
  278. /* searches the module list and returns pointer to the "name" function record or
  279. * 0 if not found
  280. * flags parameter is OR value of all flags that must match
  281. */
  282. cmd_export_t* find_export_record(char* name, int param_no, int flags)
  283. {
  284. struct sr_module* t;
  285. cmd_export_t* cmd;
  286. for(t=modules;t;t=t->next){
  287. for(cmd=t->exports->cmds; cmd && cmd->name; cmd++){
  288. if((strcmp(name, cmd->name)==0)&&
  289. (cmd->param_no==param_no) &&
  290. ((cmd->flags & flags) == flags)
  291. ){
  292. DBG("find_export_record: found <%s> in module %s [%s]\n",
  293. name, t->exports->name, t->path);
  294. return cmd;
  295. }
  296. }
  297. }
  298. DBG("find_export_record: <%s> not found \n", name);
  299. return 0;
  300. }
  301. cmd_function find_export(char* name, int param_no, int flags)
  302. {
  303. cmd_export_t* cmd;
  304. cmd = find_export_record(name, param_no, flags);
  305. return cmd?cmd->function:0;
  306. }
  307. rpc_export_t* find_rpc_export(char* name, int flags)
  308. {
  309. struct sr_module* t;
  310. rpc_export_t* rpc;
  311. /* Scan the list of core methods first, they are always
  312. * present
  313. */
  314. for(rpc = core_rpc_methods; rpc && rpc->name; rpc++) {
  315. if ((strcmp(name, rpc->name) == 0) &&
  316. ((rpc->flags & flags) == flags)
  317. ) {
  318. return rpc;
  319. }
  320. }
  321. /* Continue with modules if not found */
  322. for(t = modules; t; t = t->next) {
  323. for(rpc = t->exports->rpc_methods; rpc && rpc->name; rpc++) {
  324. if ((strcmp(name, rpc->name) == 0) &&
  325. ((rpc->flags & flags) == flags)
  326. ) {
  327. return rpc;
  328. }
  329. }
  330. }
  331. return 0;
  332. }
  333. /*
  334. * searches the module list and returns pointer to "name" function in module "mod"
  335. * 0 if not found
  336. * flags parameter is OR value of all flags that must match
  337. */
  338. cmd_function find_mod_export(char* mod, char* name, int param_no, int flags)
  339. {
  340. struct sr_module* t;
  341. cmd_export_t* cmd;
  342. for (t = modules; t; t = t->next) {
  343. if (strcmp(t->exports->name, mod) == 0) {
  344. for (cmd = t->exports->cmds; cmd && cmd->name; cmd++) {
  345. if ((strcmp(name, cmd->name) == 0) &&
  346. (cmd->param_no == param_no) &&
  347. ((cmd->flags & flags) == flags)
  348. ){
  349. DBG("find_mod_export: found <%s> in module %s [%s]\n",
  350. name, t->exports->name, t->path);
  351. return cmd->function;
  352. }
  353. }
  354. }
  355. }
  356. DBG("find_mod_export: <%s> in module <%s> not found\n", name, mod);
  357. return 0;
  358. }
  359. struct sr_module* find_module_by_name(char* mod) {
  360. struct sr_module* t;
  361. for(t = modules; t; t = t->next) {
  362. if (strcmp(mod, t->exports->name) == 0) {
  363. return t;
  364. }
  365. }
  366. DBG("find_module_by_name: module <%s> not found\n", mod);
  367. return 0;
  368. }
  369. void* find_param_export(struct sr_module* mod, char* name, modparam_t type_mask, modparam_t *param_type)
  370. {
  371. param_export_t* param;
  372. if (!mod)
  373. return 0;
  374. for(param=mod->exports->params;param && param->name ; param++) {
  375. if ((strcmp(name, param->name) == 0) &&
  376. ((param->type & PARAM_TYPE_MASK(type_mask)) != 0)) {
  377. DBG("find_param_export: found <%s> in module %s [%s]\n",
  378. name, mod->exports->name, mod->path);
  379. *param_type = param->type;
  380. return param->param_pointer;
  381. }
  382. }
  383. DBG("find_param_export: parameter <%s> not found in module <%s>\n",
  384. name, mod->exports->name);
  385. return 0;
  386. }
  387. void destroy_modules()
  388. {
  389. struct sr_module* t, *foo;
  390. t=modules;
  391. while(t) {
  392. foo=t->next;
  393. if ((t->exports)&&(t->exports->destroy_f)) t->exports->destroy_f();
  394. pkg_free(t);
  395. t=foo;
  396. }
  397. modules=0;
  398. if (mod_response_cbks){
  399. pkg_free(mod_response_cbks);
  400. mod_response_cbks=0;
  401. }
  402. }
  403. #ifdef NO_REVERSE_INIT
  404. /*
  405. * Initialize all loaded modules, the initialization
  406. * is done *AFTER* the configuration file is parsed
  407. */
  408. int init_modules(void)
  409. {
  410. struct sr_module* t;
  411. for(t = modules; t; t = t->next) {
  412. if ((t->exports) && (t->exports->init_f))
  413. if (t->exports->init_f() != 0) {
  414. LOG(L_ERR, "init_modules(): Error while initializing"
  415. " module %s\n", t->exports->name);
  416. return -1;
  417. }
  418. if ( t->exports && t->exports->response_f)
  419. mod_response_cbk_no++;
  420. }
  421. mod_response_cbks=pkg_malloc(mod_response_cbk_no *
  422. sizeof(response_function));
  423. if (mod_response_cbks==0){
  424. LOG(L_ERR, "init_modules(): memory allocation failure"
  425. " for %d response_f callbacks\n", mod_response_cbk_no);
  426. return -1;
  427. }
  428. for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next){
  429. if (t->exports && t->exports->response_f){
  430. mod_response_cbks[i]=t->exports->response_f;
  431. i++;
  432. }
  433. }
  434. return 0;
  435. }
  436. /*
  437. * per-child initialization
  438. */
  439. int init_child(int rank)
  440. {
  441. struct sr_module* t;
  442. char* type;
  443. switch(rank) {
  444. case PROC_MAIN: type = "PROC_MAIN"; break;
  445. case PROC_TIMER: type = "PROC_TIMER"; break;
  446. case PROC_FIFO: type = "PROC_FIFO"; break;
  447. case PROC_TCP_MAIN: type = "PROC_TCP_MAIN"; break;
  448. default: type = "CHILD"; break;
  449. }
  450. DBG("init_child: initializing %s with rank %d\n", type, rank);
  451. for(t = modules; t; t = t->next) {
  452. if (t->exports->init_child_f) {
  453. if ((t->exports->init_child_f(rank)) < 0) {
  454. LOG(L_ERR, "init_child(): Initialization of child %d failed\n",
  455. rank);
  456. return -1;
  457. }
  458. }
  459. }
  460. return 0;
  461. }
  462. #else
  463. /* recursive module child initialization; (recursion is used to
  464. process the module linear list in the same order in
  465. which modules are loaded in config file
  466. */
  467. static int init_mod_child( struct sr_module* m, int rank )
  468. {
  469. if (m) {
  470. /* iterate through the list; if error occurs,
  471. propagate it up the stack
  472. */
  473. if (init_mod_child(m->next, rank)!=0) return -1;
  474. if (m->exports && m->exports->init_child_f) {
  475. DBG("DEBUG: init_mod_child (%d): %s\n",
  476. rank, m->exports->name);
  477. if (m->exports->init_child_f(rank)<0) {
  478. LOG(L_ERR, "init_mod_child(): Error while initializing"
  479. " module %s\n", m->exports->name);
  480. return -1;
  481. } else {
  482. /* module correctly initialized */
  483. return 0;
  484. }
  485. }
  486. /* no init function -- proceed with success */
  487. return 0;
  488. } else {
  489. /* end of list */
  490. return 0;
  491. }
  492. }
  493. /*
  494. * per-child initialization
  495. */
  496. int init_child(int rank)
  497. {
  498. return init_mod_child(modules, rank);
  499. }
  500. /* recursive module initialization; (recursion is used to
  501. process the module linear list in the same order in
  502. which modules are loaded in config file
  503. */
  504. static int init_mod( struct sr_module* m )
  505. {
  506. if (m) {
  507. /* iterate through the list; if error occurs,
  508. propagate it up the stack
  509. */
  510. if (init_mod(m->next)!=0) return -1;
  511. if (m->exports && m->exports->init_f) {
  512. DBG("DEBUG: init_mod: %s\n", m->exports->name);
  513. if (m->exports->init_f()!=0) {
  514. LOG(L_ERR, "init_mod(): Error while initializing"
  515. " module %s\n", m->exports->name);
  516. return -1;
  517. } else {
  518. /* module correctly initialized */
  519. return 0;
  520. }
  521. }
  522. /* no init function -- proceed with success */
  523. return 0;
  524. } else {
  525. /* end of list */
  526. return 0;
  527. }
  528. }
  529. /*
  530. * Initialize all loaded modules, the initialization
  531. * is done *AFTER* the configuration file is parsed
  532. */
  533. int init_modules(void)
  534. {
  535. struct sr_module* t;
  536. int i;
  537. for(t = modules; t; t = t->next)
  538. if ( t->exports && t->exports->response_f)
  539. mod_response_cbk_no++;
  540. mod_response_cbks=pkg_malloc(mod_response_cbk_no *
  541. sizeof(response_function));
  542. if (mod_response_cbks==0){
  543. LOG(L_ERR, "init_modules(): memory allocation failure"
  544. " for %d response_f callbacks\n", mod_response_cbk_no);
  545. return -1;
  546. }
  547. for (t=modules, i=0; t && (i<mod_response_cbk_no); t=t->next){
  548. if (t->exports && t->exports->response_f){
  549. mod_response_cbks[i]=t->exports->response_f;
  550. i++;
  551. }
  552. }
  553. return init_mod(modules);
  554. }
  555. #endif
  556. action_u_t *fixup_get_param(void **cur_param, int cur_param_no, int required_param_no) {
  557. action_u_t *a, a2;
  558. /* cur_param points to a->u.string, get pointer to a */
  559. a = (void*) ((char *)cur_param - ((char *)&a2.u.string-(char *)&a2));
  560. return a + required_param_no - cur_param_no;
  561. }
  562. int fixup_get_param_count(void **cur_param, int cur_param_no) {
  563. action_u_t *a;
  564. a = fixup_get_param(cur_param, cur_param_no, 0);
  565. if (a)
  566. return a->u.number;
  567. else
  568. return -1;
  569. }
  570. /* fixes flag params (resolves possible named flags)
  571. * use PARAM_USE_FUNC|PARAM_STRING as a param. type and create
  572. * a wrapper function that does just:
  573. * return fix_flag(type, val, "my_module", "my_param", &flag_var)
  574. * see also param_func_t.
  575. */
  576. int fix_flag( modparam_t type, void* val,
  577. char* mod_name, char* param_name, int* flag)
  578. {
  579. int num;
  580. int err;
  581. int f, len;
  582. char* s;
  583. char *p;
  584. if ((type & PARAM_STRING)==0){
  585. LOG(L_CRIT, "BUG: %s: fix_flag(%s): bad parameter type\n",
  586. mod_name, param_name);
  587. return -1;
  588. }
  589. s=(char*)val;
  590. len=strlen(s);
  591. f=-1;
  592. /* try to see if it's a number */
  593. num = str2s(s, len, &err);
  594. if (err != 0) {
  595. /* see if it's in the name:<no> format */
  596. p=strchr(s, ':');
  597. if (p){
  598. f= str2s(p+1, strlen(p+1), &err);
  599. if (err!=0){
  600. LOG(L_ERR, "ERROR: %s: invalid %s format:"
  601. " \"%s\"", mod_name, param_name, s);
  602. return -1;
  603. }
  604. *p=0;
  605. }
  606. if ((num=get_flag_no(s, len))<0){
  607. /* not declared yet, declare it */
  608. num=register_flag(s, f);
  609. }
  610. if (num<0){
  611. LOG(L_ERR, "ERROR: %s: bad %s %s\n", mod_name, param_name, s);
  612. return -1;
  613. } else if ((f>0) && (num!=f)){
  614. LOG(L_ERR, "WARNING: %s: flag %s already defined"
  615. " as %d (and not %d), using %s:%d\n",
  616. mod_name, s, num, f, s, num);
  617. }
  618. }
  619. *flag=num;
  620. return 0;
  621. }
  622. /*
  623. * Common function parameter fixups
  624. */
  625. /*
  626. * Generic parameter fixup function which creates
  627. * fparam_t structure. type parameter contains allowed
  628. * parameter types
  629. *
  630. * Returns:
  631. * 0 on success,
  632. * 1 if the param doesn't match the specified type
  633. * <0 on failure
  634. */
  635. int fix_param(int type, void** param)
  636. {
  637. fparam_t* p;
  638. str name, s;
  639. unsigned int num;
  640. int err;
  641. p = (fparam_t*)pkg_malloc(sizeof(fparam_t));
  642. if (!p) {
  643. ERR("No memory left\n");
  644. return E_OUT_OF_MEM;
  645. }
  646. memset(p, 0, sizeof(fparam_t));
  647. p->orig = *param;
  648. switch(type) {
  649. case FPARAM_UNSPEC:
  650. ERR("Invalid type value\n");
  651. goto error;
  652. case FPARAM_STRING:
  653. p->v.asciiz = *param;
  654. break;
  655. case FPARAM_STR:
  656. p->v.str.s = (char*)*param;
  657. p->v.str.len = strlen(p->v.str.s);
  658. break;
  659. case FPARAM_INT:
  660. s.s = (char*)*param;
  661. s.len = strlen(s.s);
  662. err = str2int(&s, &num);
  663. if (err == 0) {
  664. p->v.i = (int)num;
  665. } else {
  666. /* Not a number */
  667. pkg_free(p);
  668. return 1;
  669. }
  670. break;
  671. case FPARAM_REGEX:
  672. if ((p->v.regex = pkg_malloc(sizeof(regex_t))) == 0) {
  673. ERR("No memory left\n");
  674. goto error;
  675. }
  676. if (regcomp(p->v.regex, *param, REG_EXTENDED|REG_ICASE|REG_NEWLINE)) {
  677. pkg_free(p->v.regex);
  678. ERR("Bad regular expression '%s'\n", (char*)*param);
  679. goto error;
  680. }
  681. break;
  682. case FPARAM_AVP:
  683. name.s = (char*)*param;
  684. name.len = strlen(name.s);
  685. trim(&name);
  686. if (!name.len || name.s[0] != '$') {
  687. /* Not an AVP identifier */
  688. pkg_free(p);
  689. return 1;
  690. }
  691. name.s++;
  692. name.len--;
  693. if (parse_avp_ident(&name, &p->v.avp) < 0) {
  694. ERR("Error while parsing attribute name\n");
  695. goto error;
  696. }
  697. break;
  698. case FPARAM_SELECT:
  699. name.s = (char*)*param;
  700. name.len = strlen(name.s);
  701. trim(&name);
  702. if (!name.len || name.s[0] != '@') {
  703. /* Not a select identifier */
  704. pkg_free(p);
  705. return 1;
  706. }
  707. if (parse_select(&name.s, &p->v.select) < 0) {
  708. ERR("Error while parsing select identifier\n");
  709. goto error;
  710. }
  711. break;
  712. case FPARAM_SUBST:
  713. s.s = *param;
  714. s.len = strlen(s.s);
  715. p->v.subst = subst_parser(&s);
  716. if (!p->v.subst) {
  717. ERR("Error while parsing regex substitution\n");
  718. goto error;
  719. }
  720. break;
  721. }
  722. p->type = type;
  723. *param = (void*)p;
  724. return 0;
  725. error:
  726. pkg_free(p);
  727. return E_UNSPEC;
  728. }
  729. /*
  730. * Fixup variable string, the parameter can be
  731. * AVP, SELECT, or ordinary string. AVP and select
  732. * identifiers will be resolved to their values during
  733. * runtime
  734. *
  735. * The parameter value will be converted to fparam structure
  736. * This function returns -1 on an error
  737. */
  738. int fixup_var_str_12(void** param, int param_no)
  739. {
  740. int ret;
  741. if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
  742. if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
  743. if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
  744. ERR("Error while fixing parameter, AVP, SELECT, and str conversions failed\n");
  745. return -1;
  746. }
  747. /* Same as fixup_var_str_12 but applies to the 1st parameter only */
  748. int fixup_var_str_1(void** param, int param_no)
  749. {
  750. if (param_no == 1) return fixup_var_str_12(param, param_no);
  751. else return 0;
  752. }
  753. /* Same as fixup_var_str_12 but applies to the 2nd parameter only */
  754. int fixup_var_str_2(void** param, int param_no)
  755. {
  756. if (param_no == 2) return fixup_var_str_12(param, param_no);
  757. else return 0;
  758. }
  759. /*
  760. * Fixup variable integer, the parameter can be
  761. * AVP, SELECT, or ordinary integer. AVP and select
  762. * identifiers will be resolved to their values and
  763. * converted to int if necessary during runtime
  764. *
  765. * The parameter value will be converted to fparam structure
  766. * This function returns -1 on an error
  767. */
  768. int fixup_var_int_12(void** param, int param_no)
  769. {
  770. int ret;
  771. if ((ret = fix_param(FPARAM_AVP, param)) <= 0) return ret;
  772. if ((ret = fix_param(FPARAM_SELECT, param)) <= 0) return ret;
  773. if ((ret = fix_param(FPARAM_INT, param)) <= 0) return ret;
  774. ERR("Error while fixing parameter, AVP, SELECT, and int conversions failed\n");
  775. return -1;
  776. }
  777. /* Same as fixup_var_int_12 but applies to the 1st parameter only */
  778. int fixup_var_int_1(void** param, int param_no)
  779. {
  780. if (param_no == 1) return fixup_var_int_12(param, param_no);
  781. else return 0;
  782. }
  783. /* Same as fixup_var_int_12 but applies to the 2nd parameter only */
  784. int fixup_var_int_2(void** param, int param_no)
  785. {
  786. if (param_no == 2) return fixup_var_int_12(param, param_no);
  787. else return 0;
  788. }
  789. /*
  790. * The parameter must be a regular expression which must compile, the
  791. * parameter will be converted to compiled regex
  792. */
  793. int fixup_regex_12(void** param, int param_no)
  794. {
  795. int ret;
  796. if ((ret = fix_param(FPARAM_REGEX, param)) <= 0) return ret;
  797. ERR("Error while compiling regex in function parameter\n");
  798. return -1;
  799. }
  800. /* Same as fixup_regex_12 but applies to the 1st parameter only */
  801. int fixup_regex_1(void** param, int param_no)
  802. {
  803. if (param_no == 1) return fixup_regex_12(param, param_no);
  804. else return 0;
  805. }
  806. /* Same as fixup_regex_12 but applies to the 2nd parameter only */
  807. int fixup_regex_2(void** param, int param_no)
  808. {
  809. if (param_no == 2) return fixup_regex_12(param, param_no);
  810. else return 0;
  811. }
  812. /*
  813. * The string parameter will be converted to integer
  814. */
  815. int fixup_int_12(void** param, int param_no)
  816. {
  817. int ret;
  818. if ((ret = fix_param(FPARAM_INT, param)) <= 0) return ret;
  819. ERR("Cannot function parameter to integer\n");
  820. return -1;
  821. }
  822. /* Same as fixup_int_12 but applies to the 1st parameter only */
  823. int fixup_int_1(void** param, int param_no)
  824. {
  825. if (param_no == 1) return fixup_int_12(param, param_no);
  826. else return 0;
  827. }
  828. /* Same as fixup_int_12 but applies to the 2nd parameter only */
  829. int fixup_int_2(void** param, int param_no)
  830. {
  831. if (param_no == 2) return fixup_int_12(param, param_no);
  832. else return 0;
  833. }
  834. /*
  835. * Parse the parameter as static string, do not resolve
  836. * AVPs or selects, convert the parameter to str structure
  837. */
  838. int fixup_str_12(void** param, int param_no)
  839. {
  840. int ret;
  841. if ((ret = fix_param(FPARAM_STR, param)) <= 0) return ret;
  842. ERR("Cannot function parameter to string\n");
  843. return -1;
  844. }
  845. /* Same as fixup_str_12 but applies to the 1st parameter only */
  846. int fixup_str_1(void** param, int param_no)
  847. {
  848. if (param_no == 1) return fixup_str_12(param, param_no);
  849. else return 0;
  850. }
  851. /* Same as fixup_str_12 but applies to the 2nd parameter only */
  852. int fixup_str_2(void** param, int param_no)
  853. {
  854. if (param_no == 2) return fixup_str_12(param, param_no);
  855. else return 0;
  856. }
  857. /*
  858. * Get the function parameter value as string
  859. * Return values: 0 - Success
  860. * -1 - Cannot get value
  861. */
  862. int get_str_fparam(str* dst, struct sip_msg* msg, fparam_t* param)
  863. {
  864. int_str val;
  865. int ret;
  866. avp_t* avp;
  867. switch(param->type) {
  868. case FPARAM_REGEX:
  869. case FPARAM_UNSPEC:
  870. case FPARAM_INT:
  871. return -1;
  872. case FPARAM_STRING:
  873. dst->s = param->v.asciiz;
  874. dst->len = strlen(param->v.asciiz);
  875. break;
  876. case FPARAM_STR:
  877. *dst = param->v.str;
  878. break;
  879. case FPARAM_AVP:
  880. avp = search_first_avp(param->v.avp.flags, param->v.avp.name, &val, 0);
  881. if (!avp) {
  882. DBG("Could not find AVP from function parameter '%s'\n", param->orig);
  883. return -1;
  884. }
  885. if (avp->flags & AVP_VAL_STR) {
  886. *dst = val.s;
  887. } else {
  888. /* The caller does not know of what type the AVP will be so
  889. * convert int AVPs into string here
  890. */
  891. dst->s = int2str(val.n, &dst->len);
  892. }
  893. break;
  894. case FPARAM_SELECT:
  895. ret = run_select(dst, param->v.select, msg);
  896. if (ret < 0 || ret > 0) return -1;
  897. break;
  898. }
  899. return 0;
  900. }
  901. /*
  902. * Get the function parameter value as integer
  903. * Return values: 0 - Success
  904. * -1 - Cannot get value
  905. */
  906. int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param)
  907. {
  908. int_str val;
  909. int ret;
  910. avp_t* avp;
  911. str tmp;
  912. switch(param->type) {
  913. case FPARAM_INT:
  914. *dst = param->v.i;
  915. return 0;
  916. case FPARAM_REGEX:
  917. case FPARAM_UNSPEC:
  918. case FPARAM_STRING:
  919. case FPARAM_STR:
  920. return -1;
  921. case FPARAM_AVP:
  922. avp = search_first_avp(param->v.avp.flags, param->v.avp.name, &val, 0);
  923. if (!avp) {
  924. DBG("Could not find AVP from function parameter '%s'\n", param->orig);
  925. return -1;
  926. }
  927. if (avp->flags & AVP_VAL_STR) {
  928. if (str2int(&val.s, (unsigned int*)dst) < 0) {
  929. ERR("Could not convert AVP string value to int\n");
  930. return -1;
  931. }
  932. } else {
  933. *dst = val.n;
  934. }
  935. break;
  936. case FPARAM_SELECT:
  937. ret = run_select(&tmp, param->v.select, msg);
  938. if (ret < 0 || ret > 0) return -1;
  939. if (str2int(&tmp, (unsigned int*)dst) < 0) {
  940. ERR("Could not convert select result to int\n");
  941. return -1;
  942. }
  943. break;
  944. }
  945. return 0;
  946. }
  947. /**
  948. * Retrieve the compiled RegExp.
  949. * @return: 0 for success, negative on error.
  950. */
  951. int get_regex_fparam(regex_t *dst, struct sip_msg* msg, fparam_t* param)
  952. {
  953. switch (param->type) {
  954. case FPARAM_REGEX:
  955. *dst = *param->v.regex;
  956. return 0;
  957. default:
  958. ERR("unexpected parameter type (%d), instead of regexp.\n",
  959. param->type);
  960. }
  961. return -1;
  962. }