select.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005-2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2005-12-19 select framework (mma)
  30. * 2006-01-19 multiple nested calls, IS_ALIAS -> NESTED flag renamed (mma)
  31. * DIVERSION flag checked
  32. * 2006-02-26 don't free str when changing type STR -> DIVERSION (mma)
  33. * it can't be freeable sometimes (e.g. xlog's select)
  34. * 2006-05-30 parse_select (mma)
  35. * 2006-06-02 shm_parse_select (mma)
  36. *
  37. */
  38. /*!
  39. * \file
  40. * \brief SIP-router core ::
  41. * \ingroup core
  42. * Module: \ref core
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <ctype.h>
  49. #include "select.h"
  50. #include "dprint.h"
  51. #include "select_core.h"
  52. #include "mem/mem.h"
  53. #include "mem/shm_mem.h"
  54. /*
  55. * The main parser table list placeholder
  56. * at startup use core table, modules can
  57. * add their own via register_select_table call
  58. */
  59. static select_table_t *select_list = &select_core_table;
  60. /* the level of the select call that is beeing evaluated
  61. * by the child process
  62. */
  63. int select_level = 0;
  64. /* pointer to the SIP uri beeing processed.
  65. * Nested function calls can pass information to each
  66. * other using this pointer. Only for performace reasons.
  67. * (Miklos)
  68. */
  69. struct sip_uri *select_uri_p = NULL;
  70. /** parse a select identifier (internal version)
  71. * Parse select string into select structure s
  72. * moves pointer p to the first unused char.
  73. *
  74. * The select identifier must be of the form:
  75. \verbatim
  76. * [@] <sel_id> [ '.' <sel_id> ...]
  77. *
  78. * Where
  79. * <sel_id> = <id> |
  80. * <id> '[' <idx> ']'
  81. * <id> = [a-zA-Z0-9_]+
  82. * <idx> = <number> | <string>
  83. * <string> = '"' <ascii> '"' |
  84. * '\"' <ascii> '\"'
  85. *
  86. * Examples:
  87. * @to.tag
  88. * @hf_value["contact"]
  89. * @msg.header["SER-Server-ID"]
  90. * @eval.pop[-1]
  91. * contact.uri.params.maddr
  92. * cfg_get.rtp_proxy.enabled
  93. \endverbatim
  94. *
  95. * @return -1 error
  96. * p points to the first unconsumed char
  97. * 0 success
  98. * p points to the first unconsumed char
  99. * s points to the select structure
  100. */
  101. int w_parse_select(char**p, select_t* sel)
  102. {
  103. str name;
  104. char* select_name;
  105. if (**p=='@') (*p)++;
  106. select_name=*p;
  107. sel->n=0;
  108. while (isalpha((unsigned char)*(*p))) {
  109. if (sel->n > MAX_SELECT_PARAMS -2) {
  110. ERR("parse_select: select depth exceeds max\n");
  111. goto error;
  112. }
  113. name.s=(*p);
  114. while (isalpha((unsigned char)*(*p)) ||
  115. isdigit((unsigned char)*(*p)) || (*(*p)=='_')) (*p)++;
  116. name.len=(*p)-name.s;
  117. sel->params[sel->n].type=SEL_PARAM_STR;
  118. sel->params[sel->n].v.s=name;
  119. DBG("parse_select: part %d: %.*s\n", sel->n, sel->params[sel->n].v.s.len, sel->params[sel->n].v.s.s);
  120. sel->n++;
  121. if (*(*p)=='[') {
  122. (*p)++;
  123. if (*(*p)=='\\') (*p)++;
  124. if (*(*p)=='"') {
  125. (*p)++;
  126. name.s=(*p);
  127. while ((*(*p)!='\0') && (*(*p)!='"')) (*p)++;
  128. if (*(*p)!='"') {
  129. ERR("parse_select: end of string is missing\n");
  130. goto error;
  131. }
  132. name.len=(*p)-name.s;
  133. if (*((*p)-1)=='\\') name.len--;
  134. (*p)++;
  135. if (*(*p)!=']') {
  136. ERR("parse_select: invalid string index, no closing ]\n");
  137. goto error;
  138. };
  139. (*p)++;
  140. sel->params[sel->n].type=SEL_PARAM_STR;
  141. sel->params[sel->n].v.s=name;
  142. DBG("parse_select: part %d: [\"%.*s\"]\n", sel->n, sel->params[sel->n].v.s.len, sel->params[sel->n].v.s.s);
  143. } else {
  144. name.s=(*p);
  145. if (*(*p)=='-') (*p)++;
  146. while (isdigit((unsigned char)*(*p))) (*p)++;
  147. name.len=(*p)-name.s;
  148. if (*(*p)!=']') {
  149. ERR("parse_select: invalid index, no closing ]\n");
  150. goto error;
  151. };
  152. (*p)++;
  153. sel->params[sel->n].type=SEL_PARAM_INT;
  154. sel->params[sel->n].v.i=atoi(name.s);
  155. DBG("parse_select: part %d: [%d]\n", sel->n, sel->params[sel->n].v.i);
  156. }
  157. sel->n++;
  158. }
  159. if (*(*p)!='.') break;
  160. (*p)++;
  161. };
  162. if (sel->n==0) {
  163. ERR("parse_select: invalid select '%.*s'\n", (int)(*p - select_name), select_name);
  164. goto error;
  165. };
  166. DBG("parse_select: end, total elements: %d, calling resolve_select\n", sel->n);
  167. if (resolve_select(sel)<0) {
  168. ERR("parse_select: error while resolve_select '%.*s'\n", (int)(*p - select_name), select_name);
  169. goto error;
  170. }
  171. return 0;
  172. error:
  173. return -1;
  174. }
  175. /** parse a select identifier.
  176. * Parse select string into select structure s and
  177. * moves pointer p to the first unused char.
  178. *
  179. \verbatim
  180. * The select identifier must be of the form:
  181. * [@] <sel_id> [ '.' <sel_id> ...]
  182. *
  183. * Where
  184. * <sel_id> = <id> |
  185. * <id> '[' <idx> ']'
  186. * <id> = [a-zA-Z0-9_]+
  187. * <idx> = <number> | '-' <number> | <string>
  188. * <string> = '"' <ascii> '"' |
  189. * '\"' <ascii> '\"'
  190. *
  191. * Examples:
  192. * @to.tag
  193. * @hf_value["contact"]
  194. * @msg.header["SER-Server-ID"]
  195. * @eval.pop[-1]
  196. * contact.uri.params.maddr
  197. * cfg_get.rtp_proxy.enabled
  198. \endverbatim
  199. *
  200. * @param p - double string (asciiz) pointer, *p is moved to the first char
  201. * after the select identifier
  202. * @param s - the result will be stored here
  203. * @return < 0 on error, 0 on success
  204. */
  205. int parse_select (char** p, select_t** s)
  206. {
  207. select_t* sel;
  208. sel=(select_t*)pkg_malloc(sizeof(select_t));
  209. if (!sel) {
  210. ERR("parse_select: no free memory\n");
  211. return -1;
  212. }
  213. memset(sel, 0, sizeof(select_t));
  214. if (w_parse_select(p, sel)<0) {
  215. pkg_free(sel);
  216. return -2;
  217. }
  218. *s=sel;
  219. return 0;
  220. }
  221. void free_select(select_t *s)
  222. {
  223. if (s)
  224. pkg_free(s);
  225. }
  226. void shm_free_select(select_t *s)
  227. {
  228. if (s)
  229. shm_free(s);
  230. }
  231. int shm_parse_select (char** p, select_t** s)
  232. {
  233. select_t* sel;
  234. sel=(select_t*)shm_malloc(sizeof(select_t));
  235. if (!sel) {
  236. ERR("parse_select: no free shared memory\n");
  237. return -1;
  238. }
  239. if (w_parse_select(p, sel)<0) {
  240. shm_free(sel);
  241. return -2;
  242. }
  243. *s=sel;
  244. return 0;
  245. }
  246. int resolve_select(select_t* s)
  247. {
  248. select_f f;
  249. int nested;
  250. int param_idx = 0;
  251. int table_idx = 0;
  252. select_table_t* t = NULL;
  253. int accept = 0;
  254. f = NULL;
  255. nested = 0;
  256. memset (s->f, 0, sizeof(s->f));
  257. while (param_idx<s->n) {
  258. accept = 0;
  259. switch (s->params[param_idx].type) {
  260. case SEL_PARAM_STR:
  261. DBG("resolve_select: '%.*s'\n", s->params[param_idx].v.s.len, s->params[param_idx].v.s.s);
  262. break;
  263. case SEL_PARAM_INT:
  264. DBG("resolve_select: [%d]\n", s->params[param_idx].v.i);
  265. break;
  266. default:
  267. /* just to avoid the warning */
  268. break;
  269. }
  270. for (t=select_list; t; t=t->next) {
  271. table_idx = 0;
  272. if (!t->table) continue;
  273. while (t->table[table_idx].curr_f || t->table[table_idx].new_f) {
  274. if (t->table[table_idx].curr_f == f) {
  275. if ((t->table[table_idx].flags & (NESTED | CONSUME_NEXT_INT | CONSUME_NEXT_STR)) == NESTED) {
  276. accept = 1;
  277. } else if (t->table[table_idx].type == s->params[param_idx].type) {
  278. switch (t->table[table_idx].type) {
  279. case SEL_PARAM_INT:
  280. accept = 1;
  281. break;
  282. case SEL_PARAM_STR:
  283. accept = (((t->table[table_idx].name.len == s->params[param_idx].v.s.len) || !t->table[table_idx].name.len)
  284. && (!t->table[table_idx].name.s || !strncasecmp(t->table[table_idx].name.s, s->params[param_idx].v.s.s, s->params[param_idx].v.s.len)));
  285. break;
  286. default:
  287. break;
  288. }
  289. };
  290. }
  291. if (accept) goto accepted;
  292. table_idx++;
  293. }
  294. }
  295. switch (s->params[param_idx].type) {
  296. case SEL_PARAM_STR:
  297. LM_ERR("Unable to resolve select '%.*s' at level %d\n", s->params[param_idx].v.s.len, s->params[param_idx].v.s.s, param_idx);
  298. break;
  299. case SEL_PARAM_INT:
  300. LM_ERR("Unable to resolve select [%d] at level %d\n", s->params[param_idx].v.i, param_idx);
  301. break;
  302. default:
  303. BUG ("Unable to resolve select at level %d\n", param_idx);
  304. break;
  305. break;
  306. }
  307. goto not_found;
  308. accepted:
  309. if (t->table[table_idx].flags & DIVERSION) {
  310. /* if (s->params[param_idx].type == SEL_PARAM_STR) pkg_free(s->params[param_idx].v.s.s); */
  311. /* don't free it (the mem can leak only once at startup)
  312. * the parsed string can live inside larger string block
  313. * e.g. when xlog's select is parsed
  314. */
  315. s->params[param_idx].type = SEL_PARAM_DIV;
  316. s->params[param_idx].v.i = t->table[table_idx].flags & DIVERSION_MASK;
  317. }
  318. if (t->table[table_idx].flags & CONSUME_NEXT_STR) {
  319. if ((param_idx<s->n-1) && (s->params[param_idx+1].type == SEL_PARAM_STR)) {
  320. param_idx++;
  321. } else if (!(t->table[table_idx].flags & OPTIONAL)) {
  322. BUG ("Mandatory STR parameter not found\n");
  323. goto not_found;
  324. }
  325. }
  326. if (t->table[table_idx].flags & CONSUME_NEXT_INT) {
  327. if ((param_idx<s->n-1) && (s->params[param_idx+1].type == SEL_PARAM_INT)) {
  328. param_idx++;
  329. } else if (!(t->table[table_idx].flags & OPTIONAL)) {
  330. BUG ("Mandatory INT parameter not found\n");
  331. goto not_found;
  332. }
  333. }
  334. if (t->table[table_idx].flags & NESTED) {
  335. if (nested < MAX_NESTED_CALLS-1) { /* need space for final function */
  336. s->f[nested++] = f;
  337. s->param_offset[nested] = param_idx;
  338. } else {
  339. BUG("MAX_NESTED_CALLS too small to resolve select\n");
  340. goto not_found;
  341. }
  342. } else {
  343. param_idx++;
  344. }
  345. if (t->table[table_idx].flags & FIXUP_CALL) {
  346. select_level = nested;
  347. s->param_offset[nested+1] = param_idx;
  348. if (t->table[table_idx].new_f(NULL, s, NULL)<0) goto not_found;
  349. }
  350. f = t->table[table_idx].new_f;
  351. if (t->table[table_idx].flags & CONSUME_ALL) {
  352. /* sanity checks */
  353. if (t->table[table_idx].flags & NESTED)
  354. WARN("resolve_select: CONSUME_ALL should not be set "
  355. "together with NESTED flag!\n");
  356. if ((t->table[table_idx].flags & FIXUP_CALL) == 0)
  357. WARN("resolve_select: FIXUP_CALL should be defined "
  358. "if CONSUME_ALL flag is set!\n");
  359. break;
  360. }
  361. }
  362. if (t==NULL) {
  363. BUG ("final node not found\n");
  364. goto not_found;
  365. }
  366. if (t->table[table_idx].flags & SEL_PARAM_EXPECTED) {
  367. BUG ("final node has SEL_PARAM_EXPECTED set (no more parameters available)\n");
  368. goto not_found;
  369. }
  370. if (nested >= MAX_NESTED_CALLS) {
  371. BUG("MAX_NESTED_CALLS too small, no space for finally resolved function\n");
  372. goto not_found;
  373. }
  374. if ((nested>0) && (s->f[nested-1] == f)) {
  375. BUG("Topmost nested function equals to final function, won't call it twice\n");
  376. } else {
  377. s->f[nested++] = f;
  378. }
  379. s->param_offset[nested] = s->n;
  380. return 0;
  381. not_found:
  382. return -1;
  383. }
  384. int run_select(str* res, select_t* s, struct sip_msg* msg)
  385. {
  386. int ret, orig_level;
  387. if (res == NULL) {
  388. BUG("Select unprepared result space\n");
  389. return -1;
  390. }
  391. if (s == 0) {
  392. BUG("Select structure is NULL\n");
  393. return -1;
  394. }
  395. if (s->f[0] == 0) {
  396. BUG("Select structure has not been resolved\n");
  397. return -1;
  398. }
  399. DBG("Calling SELECT %p \n", s->f);
  400. /* reset the uri pointer */
  401. select_uri_p = NULL;
  402. /* save and restore the original select_level
  403. * because of the nested selects */
  404. orig_level = select_level;
  405. ret = 0;
  406. for ( select_level=0;
  407. (ret == 0) && (select_level<MAX_NESTED_CALLS) && (s->f[select_level] !=0 );
  408. select_level++
  409. ) {
  410. ret = s->f[select_level](res, s, msg);
  411. }
  412. select_level = orig_level;
  413. return ret;
  414. }
  415. void print_select(select_t* s)
  416. {
  417. int i;
  418. DBG("select(");
  419. for(i = 0; i < s->n; i++) {
  420. if (s->params[i].type == SEL_PARAM_INT) {
  421. DBG("%d,", s->params[i].v.i);
  422. } else {
  423. DBG("%.*s,", s->params[i].v.s.len, s->params[i].v.s.s);
  424. }
  425. }
  426. DBG(")\n");
  427. }
  428. int register_select_table(select_row_t* mod_tab)
  429. {
  430. select_table_t* t;
  431. t=(select_table_t*)pkg_malloc(sizeof(select_table_t));
  432. if (!t) {
  433. ERR("No memory for new select_table structure\n");
  434. return -1;
  435. }
  436. t->table=mod_tab;
  437. t->next=select_list;
  438. select_list=t;
  439. return 0;
  440. }