route_struct.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * $Id$
  3. *
  4. * route structures helping functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /* History:
  30. * --------
  31. * 2003-01-29 src_port introduced (jiri)
  32. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  33. * 2003-04-12 FORCE_RPORT_T added (andrei)
  34. * 2003-10-02 added SET_ADV_ADDRESS & SET_ADV_PORT (andrei)
  35. * 2004-02-24 added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
  36. * 2005-12-19 select framework added SELECT_O and SELECT_ST (mma)
  37. */
  38. /*!
  39. * \file
  40. * \brief SIP-router core ::
  41. * \ingroup core
  42. * Module: \ref core
  43. */
  44. #include "route_struct.h"
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <stdarg.h>
  49. #include "dprint.h"
  50. #include "ip_addr.h"
  51. #include "mem/mem.h"
  52. #include "usr_avp.h"
  53. #include "ut.h" /* ZSW() */
  54. /** joins to cfg file positions into a new one. */
  55. void cfg_pos_join(struct cfg_pos* res,
  56. struct cfg_pos* pos1, struct cfg_pos* pos2)
  57. {
  58. struct cfg_pos ret;
  59. ret=*pos1;
  60. if ((ret.s_line == 0) || (ret.s_line > pos2->s_line)){
  61. ret.s_line=pos2->s_line;
  62. ret.s_col=pos2->s_col;
  63. }else if ((ret.s_line == pos2->s_line) && (ret.s_col > pos2->s_col)){
  64. ret.s_col=pos2->s_col;
  65. }
  66. if ((ret.e_line == 0) || (ret.e_line < pos2->e_line)){
  67. ret.e_line=pos2->e_line;
  68. ret.e_col=pos2->e_col;
  69. }else if ((ret.e_line == pos2->e_line) && (ret.e_col < pos2->e_col)){
  70. ret.e_col=pos2->e_col;
  71. }
  72. *res=ret;
  73. }
  74. struct expr* mk_exp(int op, struct expr* left, struct expr* right)
  75. {
  76. struct expr * e;
  77. e=(struct expr*)pkg_malloc(sizeof (struct expr));
  78. if (e==0) goto error;
  79. e->type=EXP_T;
  80. e->op=op;
  81. e->l.expr=left;
  82. e->r.expr=right;
  83. return e;
  84. error:
  85. LM_CRIT("memory allocation failure\n");
  86. return 0;
  87. }
  88. struct expr* mk_exp_rve(int op, void* left, void* right)
  89. {
  90. struct expr * e;
  91. e=(struct expr*)pkg_malloc(sizeof (struct expr));
  92. if (e==0) goto error;
  93. e->type=EXP_T;
  94. e->op=op;
  95. e->l.param=mk_elem(RVEXP_O, RVE_ST, left, 0, 0);
  96. e->r.param=mk_elem(RVEXP_O, RVE_ST, right, 0, 0);
  97. if (e->l.param==0 || e->r.param==0){
  98. if (e->l.param) pkg_free(e->l.param);
  99. if (e->r.param) pkg_free(e->r.param);
  100. pkg_free(e);
  101. goto error;
  102. }
  103. return e;
  104. error:
  105. LM_CRIT("memory allocation failure\n");
  106. return 0;
  107. }
  108. struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
  109. expr_r_type rtype, void* rparam)
  110. {
  111. struct expr * e;
  112. e=(struct expr*)pkg_malloc(sizeof (struct expr));
  113. if (e==0) goto error;
  114. e->type=ELEM_T;
  115. e->op=op;
  116. e->l_type=ltype;
  117. e->l.param=lparam;
  118. e->r_type = rtype;
  119. e->r.param=rparam;
  120. return e;
  121. error:
  122. LM_CRIT("memory allocation failure\n");
  123. return 0;
  124. }
  125. /** create an action structure (parser use).
  126. * @param type - type of the action
  127. * @param count - count of couples {param_type,val}
  128. * @param ... - count {param_type, val} pairs, where param_type is
  129. * action_param_type.
  130. * @return new action structure on success (pkg_malloc'ed) or 0 on error.
  131. */
  132. struct action* mk_action(enum action_type type, int count, ...)
  133. {
  134. va_list args;
  135. int i;
  136. struct action* a;
  137. a = (struct action*)pkg_malloc(sizeof(struct action));
  138. if (a==0) goto error;
  139. memset(a, 0, sizeof(struct action));
  140. a->type=type;
  141. a->count = (count > MAX_ACTIONS)?MAX_ACTIONS:count;
  142. va_start(args, count);
  143. for (i=0; i<a->count; i++) {
  144. a->val[i].type = va_arg(args, int);
  145. a->val[i].u.data = va_arg(args, void *);
  146. DBG("ACTION_#%d #%d/%d: %d(%x)/ %p\n", a->type, i, a->count, a->val[i].type, a->val[i].type, a->val[i].u.data);
  147. }
  148. va_end(args);
  149. a->next=0;
  150. return a;
  151. error:
  152. LM_CRIT("memory allocation failure\n");
  153. return 0;
  154. }
  155. struct action* append_action(struct action* a, struct action* b)
  156. {
  157. struct action *t;
  158. if (b==0) return a;
  159. if (a==0) return b;
  160. for(t=a; t->next; t=t->next);
  161. t->next=b;
  162. return a;
  163. }
  164. void print_expr(struct expr* exp)
  165. {
  166. if (exp==0){
  167. LM_CRIT("null expression!\n");
  168. return;
  169. }
  170. if (exp->type==ELEM_T){
  171. switch(exp->l_type){
  172. case METHOD_O:
  173. DBG("method");
  174. break;
  175. case URI_O:
  176. DBG("uri");
  177. break;
  178. case FROM_URI_O:
  179. DBG("from_uri");
  180. break;
  181. case TO_URI_O:
  182. DBG("to_uri");
  183. break;
  184. case SRCIP_O:
  185. DBG("srcip");
  186. break;
  187. case SRCPORT_O:
  188. DBG("srcport");
  189. break;
  190. case DSTIP_O:
  191. DBG("dstip");
  192. break;
  193. case DSTPORT_O:
  194. DBG("dstport");
  195. break;
  196. case PROTO_O:
  197. DBG("proto");
  198. break;
  199. case AF_O:
  200. DBG("af");
  201. break;
  202. case MSGLEN_O:
  203. DBG("msglen");
  204. break;
  205. case ACTION_O:
  206. break;
  207. case NUMBER_O:
  208. break;
  209. case AVP_O:
  210. DBG("avp");
  211. break;
  212. case SNDIP_O:
  213. DBG("sndip");
  214. break;
  215. case SNDPORT_O:
  216. DBG("sndport");
  217. break;
  218. case TOIP_O:
  219. DBG("toip");
  220. break;
  221. case TOPORT_O:
  222. DBG("toport");
  223. break;
  224. case SNDPROTO_O:
  225. DBG("sndproto");
  226. break;
  227. case SNDAF_O:
  228. DBG("sndaf");
  229. break;
  230. case RETCODE_O:
  231. DBG("retcode");
  232. break;
  233. case SELECT_O:
  234. DBG("select");
  235. break;
  236. case RVEXP_O:
  237. DBG("rval");
  238. break;
  239. default:
  240. DBG("UNKNOWN");
  241. }
  242. switch(exp->op){
  243. case EQUAL_OP:
  244. DBG("==");
  245. break;
  246. case MATCH_OP:
  247. DBG("=~");
  248. break;
  249. case NO_OP:
  250. break;
  251. case GT_OP:
  252. DBG(">");
  253. break;
  254. case GTE_OP:
  255. DBG(">=");
  256. break;
  257. case LT_OP:
  258. DBG("<");
  259. break;
  260. case LTE_OP:
  261. DBG("<=");
  262. break;
  263. case DIFF_OP:
  264. DBG("!=");
  265. break;
  266. default:
  267. DBG("<UNKNOWN>");
  268. }
  269. switch(exp->r_type){
  270. case NOSUBTYPE:
  271. DBG("N/A");
  272. break;
  273. case STRING_ST:
  274. DBG("\"%s\"", ZSW((char*)exp->r.param));
  275. break;
  276. case NET_ST:
  277. print_net((struct net*)exp->r.param);
  278. break;
  279. case IP_ST:
  280. print_ip("", (struct ip_addr*)exp->r.param, "");
  281. break;
  282. case ACTIONS_ST:
  283. print_actions((struct action*)exp->r.param);
  284. break;
  285. case NUMBER_ST:
  286. DBG("%ld",exp->r.numval);
  287. break;
  288. case MYSELF_ST:
  289. DBG("_myself_");
  290. break;
  291. case AVP_ST:
  292. DBG("attr");
  293. break;
  294. case SELECT_ST:
  295. DBG("select");
  296. break;
  297. default:
  298. DBG("type<%d>", exp->r_type);
  299. }
  300. }else if (exp->type==EXP_T){
  301. switch(exp->op){
  302. case LOGAND_OP:
  303. DBG("AND( ");
  304. print_expr(exp->l.expr);
  305. DBG(", ");
  306. print_expr(exp->r.expr);
  307. DBG(" )");
  308. break;
  309. case LOGOR_OP:
  310. DBG("OR( ");
  311. print_expr(exp->l.expr);
  312. DBG(", ");
  313. print_expr(exp->r.expr);
  314. DBG(" )");
  315. break;
  316. case NOT_OP:
  317. DBG("NOT( ");
  318. print_expr(exp->l.expr);
  319. DBG(" )");
  320. break;
  321. default:
  322. DBG("UNKNOWN_EXP ");
  323. }
  324. }else{
  325. DBG("ERROR:print_expr: unknown type\n");
  326. }
  327. }
  328. void print_action(struct action* t)
  329. {
  330. switch(t->type){
  331. case FORWARD_T:
  332. DBG("forward(");
  333. break;
  334. case FORWARD_TCP_T:
  335. DBG("forward_tcp(");
  336. break;
  337. case FORWARD_UDP_T:
  338. DBG("forward_udp(");
  339. break;
  340. case DROP_T:
  341. DBG("drop(");
  342. break;
  343. case LOG_T:
  344. DBG("log(");
  345. break;
  346. case ERROR_T:
  347. DBG("error(");
  348. break;
  349. case ROUTE_T:
  350. DBG("route(");
  351. break;
  352. case EXEC_T:
  353. DBG("exec(");
  354. break;
  355. case REVERT_URI_T:
  356. DBG("revert_uri(");
  357. break;
  358. case STRIP_T:
  359. DBG("strip(");
  360. break;
  361. case APPEND_BRANCH_T:
  362. DBG("append_branch(");
  363. break;
  364. case PREFIX_T:
  365. DBG("prefix(");
  366. break;
  367. case LEN_GT_T:
  368. DBG("len_gt(");
  369. break;
  370. case SETFLAG_T:
  371. DBG("setflag(");
  372. break;
  373. case RESETFLAG_T:
  374. DBG("resetflag(");
  375. break;
  376. case ISFLAGSET_T:
  377. DBG("isflagset(");
  378. break;
  379. case AVPFLAG_OPER_T:
  380. DBG("avpflagoper(");
  381. break;
  382. case SET_HOST_T:
  383. DBG("sethost(");
  384. break;
  385. case SET_HOSTPORT_T:
  386. DBG("sethostport(");
  387. break;
  388. case SET_HOSTPORTTRANS_T:
  389. DBG("sethostporttrans(");
  390. break;
  391. case SET_USER_T:
  392. DBG("setuser(");
  393. break;
  394. case SET_USERPASS_T:
  395. DBG("setuserpass(");
  396. break;
  397. case SET_PORT_T:
  398. DBG("setport(");
  399. break;
  400. case SET_URI_T:
  401. DBG("seturi(");
  402. break;
  403. case IF_T:
  404. DBG("if (");
  405. break;
  406. case MODULE0_T:
  407. case MODULE1_T:
  408. case MODULE2_T:
  409. case MODULE3_T:
  410. case MODULE4_T:
  411. case MODULE5_T:
  412. case MODULE6_T:
  413. case MODULEX_T:
  414. case MODULE1_RVE_T:
  415. case MODULE2_RVE_T:
  416. case MODULE3_RVE_T:
  417. case MODULE4_RVE_T:
  418. case MODULE5_RVE_T:
  419. case MODULE6_RVE_T:
  420. case MODULEX_RVE_T:
  421. DBG(" external_module_call(");
  422. break;
  423. case FORCE_RPORT_T:
  424. DBG("force_rport(");
  425. break;
  426. case SET_ADV_ADDR_T:
  427. DBG("set_advertised_address(");
  428. break;
  429. case SET_ADV_PORT_T:
  430. DBG("set_advertised_port(");
  431. break;
  432. case FORCE_TCP_ALIAS_T:
  433. DBG("force_tcp_alias(");
  434. break;
  435. case LOAD_AVP_T:
  436. DBG("load_avp(");
  437. break;
  438. case AVP_TO_URI_T:
  439. DBG("avp_to_attr");
  440. break;
  441. case FORCE_SEND_SOCKET_T:
  442. DBG("force_send_socket");
  443. break;
  444. case ASSIGN_T
  445. : DBG("assign(");
  446. break;
  447. case ADD_T:
  448. DBG("assign_add(");
  449. break;
  450. default:
  451. DBG("UNKNOWN(");
  452. }
  453. switch(t->val[0].type){
  454. case STRING_ST:
  455. DBG("\"%s\"", ZSW(t->val[0].u.string));
  456. break;
  457. case NUMBER_ST:
  458. DBG("%lu",t->val[0].u.number);
  459. break;
  460. case IP_ST:
  461. print_ip("", (struct ip_addr*)t->val[0].u.data, "");
  462. break;
  463. case EXPR_ST:
  464. print_expr((struct expr*)t->val[0].u.data);
  465. break;
  466. case ACTIONS_ST:
  467. print_actions((struct action*)t->val[0].u.data);
  468. break;
  469. case MODEXP_ST:
  470. DBG("f_ptr<%p>",t->val[0].u.data);
  471. break;
  472. case SOCKID_ST:
  473. DBG("%d:%s:%d",
  474. ((struct socket_id*)t->val[0].u.data)->proto,
  475. ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
  476. ((struct socket_id*)t->val[0].u.data)->port
  477. );
  478. break;
  479. case AVP_ST:
  480. DBG("avp(%u,%.*s)", t->val[0].u.attr->type, t->val[0].u.attr->name.s.len, ZSW(t->val[0].u.attr->name.s.s));
  481. break;
  482. case SELECT_ST:
  483. DBG("select");
  484. break;
  485. default:
  486. DBG("type<%d>", t->val[0].type);
  487. }
  488. if (t->type==IF_T) DBG(") {");
  489. switch(t->val[1].type){
  490. case NOSUBTYPE:
  491. break;
  492. case STRING_ST:
  493. DBG(", \"%s\"", ZSW(t->val[1].u.string));
  494. break;
  495. case NUMBER_ST:
  496. DBG(", %lu",t->val[1].u.number);
  497. break;
  498. case EXPR_ST:
  499. print_expr((struct expr*)t->val[1].u.data);
  500. break;
  501. case ACTION_ST:
  502. case ACTIONS_ST:
  503. print_actions((struct action*)t->val[1].u.data);
  504. break;
  505. case SOCKID_ST:
  506. DBG("%d:%s:%d",
  507. ((struct socket_id*)t->val[0].u.data)->proto,
  508. ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
  509. ((struct socket_id*)t->val[0].u.data)->port
  510. );
  511. break;
  512. case AVP_ST:
  513. DBG(", avp(%u,%.*s)", t->val[1].u.attr->type, t->val[1].u.attr->name.s.len, ZSW(t->val[1].u.attr->name.s.s));
  514. break;
  515. case SELECT_ST:
  516. DBG("select");
  517. break;
  518. default:
  519. DBG(", type<%d>", t->val[1].type);
  520. }
  521. if (t->type==IF_T) DBG("} else {");
  522. switch(t->val[2].type){
  523. case NOSUBTYPE:
  524. break;
  525. case STRING_ST:
  526. DBG(", \"%s\"", ZSW(t->val[2].u.string));
  527. break;
  528. case NUMBER_ST:
  529. DBG(", %lu",t->val[2].u.number);
  530. break;
  531. case EXPR_ST:
  532. print_expr((struct expr*)t->val[2].u.data);
  533. break;
  534. case ACTIONS_ST:
  535. print_actions((struct action*)t->val[2].u.data);
  536. break;
  537. case SOCKID_ST:
  538. DBG("%d:%s:%d",
  539. ((struct socket_id*)t->val[0].u.data)->proto,
  540. ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
  541. ((struct socket_id*)t->val[0].u.data)->port
  542. );
  543. break;
  544. default:
  545. DBG(", type<%d>", t->val[2].type);
  546. }
  547. if (t->type==IF_T) DBG("}; ");
  548. else DBG("); ");
  549. }
  550. void print_actions(struct action* a)
  551. {
  552. while(a) {
  553. print_action(a);
  554. a = a->next;
  555. }
  556. }
  557. /**
  558. * get the pointer to action structure from parameter
  559. */
  560. struct action *get_action_from_param(void **param, int param_no)
  561. {
  562. struct action *ac, ac2;
  563. action_u_t *au, au2;
  564. /* param points to au->u.string, get pointer to au */
  565. au = (void*) ((char *)param - ((char *)&au2.u.string-(char *)&au2));
  566. au = au - 1 - param_no;
  567. ac = (void*) ((char *)au - ((char *)&ac2.val-(char *)&ac2));
  568. return ac;
  569. }