route.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. * $Id$
  3. *
  4. * SIP routing engine
  5. *
  6. *
  7. * Copyright (C) 2001-2003 FhG Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. * History:
  31. * --------
  32. * 2003-01-28 scratchpad removed, src_port introduced (jiri)
  33. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  34. * 2003-03-10 updated to the new module exports format (andrei)
  35. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  36. * 2003-04-01 added dst_port, proto, af; renamed comp_port to comp_no,
  37. * inlined all the comp_* functions (andrei)
  38. * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
  39. * 2003-05-23 comp_ip fixed, now it will resolve its operand and compare
  40. * the ip with all the addresses (andrei)
  41. * 2003-10-10 added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
  42. * 2004-10-19 added from_uri & to_uri (andrei)
  43. * 2005-12-12 added retcode support (anrei)
  44. * 2005-12-19 select framework (mma)
  45. * 2006-01-30 removed rec. protection from eval_expr (andrei)
  46. * 2006-02-06 added named route tables (andrei)
  47. */
  48. #include <stdlib.h>
  49. #include <sys/types.h>
  50. #include <regex.h>
  51. #include <netdb.h>
  52. #include <string.h>
  53. #include <sys/socket.h>
  54. #include <netinet/in.h>
  55. #include <arpa/inet.h>
  56. #include <netdb.h>
  57. #include "route.h"
  58. #include "forward.h"
  59. #include "dprint.h"
  60. #include "proxy.h"
  61. #include "action.h"
  62. #include "sr_module.h"
  63. #include "ip_addr.h"
  64. #include "resolve.h"
  65. #include "socket_info.h"
  66. #include "parser/parse_uri.h"
  67. #include "parser/parse_from.h"
  68. #include "parser/parse_to.h"
  69. #include "mem/mem.h"
  70. #include "select.h"
  71. #include "onsend.h"
  72. #include "str_hash.h"
  73. #include "ut.h"
  74. #define RT_HASH_SIZE 8 /* route names hash */
  75. /* main routing script table */
  76. struct route_list main_rt;
  77. struct route_list onreply_rt;
  78. struct route_list failure_rt;
  79. struct route_list branch_rt;
  80. struct route_list onsend_rt;
  81. inline static void destroy_rlist(struct route_list* rt)
  82. {
  83. struct str_hash_entry* e;
  84. struct str_hash_entry* tmp;
  85. if (rt->rlist){
  86. pkg_free(rt->rlist);
  87. rt->rlist=0;
  88. rt->entries=0;
  89. }
  90. if (rt->names.table){
  91. clist_foreach_safe(rt->names.table, e, tmp, next){
  92. pkg_free(e);
  93. }
  94. pkg_free(rt->names.table);
  95. rt->names.table=0;
  96. rt->names.size=0;
  97. }
  98. }
  99. void destroy_routes()
  100. {
  101. destroy_rlist(&main_rt);
  102. destroy_rlist(&onreply_rt);
  103. destroy_rlist(&failure_rt);
  104. destroy_rlist(&branch_rt);
  105. }
  106. /* adds route name -> i mapping
  107. * WARNING: it doesn't check for pre-existing routes
  108. * return -1 on error, route index on success
  109. */
  110. static int route_add(struct route_list* rt, char* name, int i)
  111. {
  112. struct str_hash_entry* e;
  113. e=pkg_malloc(sizeof(struct str_hash_entry));
  114. if (e==0){
  115. LOG(L_CRIT, "ERROR: route_add: out of memory\n");
  116. goto error;
  117. }
  118. e->key.s=name;
  119. e->key.len=strlen(name);
  120. e->flags=0;
  121. e->u.n=i;
  122. str_hash_add(&rt->names, e);
  123. return 0;
  124. error:
  125. return -1;
  126. }
  127. /* returns -1 on error, 0 on success */
  128. inline static int init_rlist(char* r_name, struct route_list* rt,
  129. int n_entries, int hash_size)
  130. {
  131. rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
  132. if (rt->rlist==0){
  133. LOG(L_CRIT, "ERROR: failed to allocate \"%s\" route tables: "
  134. "out of memory\n", r_name);
  135. goto error;
  136. }
  137. memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
  138. rt->idx=1; /* idx=0 == default == reserved */
  139. rt->entries=n_entries;
  140. if (str_hash_alloc(&rt->names, hash_size)<0){
  141. LOG(L_CRIT, "ERROR: \"%s\" route table: failed to alloc hash\n",
  142. r_name);
  143. goto error;
  144. }
  145. str_hash_init(&rt->names);
  146. route_add(rt, "0", 0); /* default route */
  147. return 0;
  148. error:
  149. return -1;
  150. }
  151. /* init route tables */
  152. int init_routes()
  153. {
  154. if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
  155. goto error;
  156. if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
  157. goto error;
  158. if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
  159. goto error;
  160. if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
  161. goto error;
  162. if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
  163. goto error;
  164. return 0;
  165. error:
  166. destroy_routes();
  167. return -1;
  168. }
  169. static inline int route_new_list(struct route_list* rt)
  170. {
  171. int ret;
  172. struct action** tmp;
  173. ret=-1;
  174. if (rt->idx >= rt->entries){
  175. tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
  176. if (tmp==0){
  177. LOG(L_CRIT, "ERROR: route_new_list: out of memory\n");
  178. goto end;
  179. }
  180. /* init the newly allocated memory chunk */
  181. memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
  182. rt->rlist=tmp;
  183. rt->entries*=2;
  184. }
  185. if (rt->idx<rt->entries){
  186. ret=rt->idx;
  187. rt->idx++;
  188. }
  189. end:
  190. return ret;
  191. }
  192. /*
  193. * if the "name" route already exists, return its index, else
  194. * create a new empty route
  195. * return route index in rt->rlist or -1 on error
  196. */
  197. int route_get(struct route_list* rt, char* name)
  198. {
  199. int len;
  200. struct str_hash_entry* e;
  201. int i;
  202. len=strlen(name);
  203. /* check if exists an non empty*/
  204. e=str_hash_get(&rt->names, name, len);
  205. if (e){
  206. i=e->u.n;
  207. }else{
  208. i=route_new_list(rt);
  209. if (i==-1) goto error;
  210. if (route_add(rt, name, i)<0){
  211. goto error;
  212. }
  213. }
  214. return i;
  215. error:
  216. return -1;
  217. }
  218. /*
  219. * if the "name" route already exists, return its index, else
  220. * return error
  221. * return route index in rt->rlist or -1 on error
  222. */
  223. int route_lookup(struct route_list* rt, char* name)
  224. {
  225. int len;
  226. struct str_hash_entry* e;
  227. len=strlen(name);
  228. /* check if exists an non empty*/
  229. e=str_hash_get(&rt->names, name, len);
  230. if (e){
  231. return e->u.n;
  232. }else{
  233. return -1;
  234. }
  235. }
  236. static int fix_actions(struct action* a); /*fwd declaration*/
  237. /* traverses an expr tree and compiles the REs where necessary)
  238. * returns: 0 for ok, <0 if errors */
  239. static int fix_expr(struct expr* exp)
  240. {
  241. regex_t* re;
  242. int ret;
  243. ret=E_BUG;
  244. if (exp==0){
  245. LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
  246. return E_BUG;
  247. }
  248. if (exp->type==EXP_T){
  249. switch(exp->op){
  250. case LOGAND_OP:
  251. case LOGOR_OP:
  252. if ((ret=fix_expr(exp->l.expr))!=0)
  253. return ret;
  254. ret=fix_expr(exp->r.expr);
  255. break;
  256. case NOT_OP:
  257. ret=fix_expr(exp->l.expr);
  258. break;
  259. default:
  260. LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
  261. exp->op);
  262. }
  263. }else if (exp->type==ELEM_T){
  264. if (exp->op==MATCH_OP){
  265. /* right side either has to be string, in which case
  266. * we turn it into regular expression, or it is regular
  267. * expression already. In that case we do nothing
  268. */
  269. if (exp->r_type==STRING_ST){
  270. re=(regex_t*)pkg_malloc(sizeof(regex_t));
  271. if (re==0){
  272. LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
  273. " failure\n");
  274. return E_OUT_OF_MEM;
  275. }
  276. if (regcomp(re, (char*) exp->r.param,
  277. REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
  278. LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
  279. (char*) exp->r.param);
  280. pkg_free(re);
  281. return E_BAD_RE;
  282. }
  283. /* replace the string with the re */
  284. pkg_free(exp->r.param);
  285. exp->r.re=re;
  286. exp->r_type=RE_ST;
  287. }else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST && exp->r_type != SELECT_ST){
  288. LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
  289. return E_BUG;
  290. }
  291. }
  292. if (exp->l_type==ACTION_O){
  293. ret=fix_actions((struct action*)exp->r.param);
  294. if (ret!=0){
  295. LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
  296. return ret;
  297. }
  298. }
  299. /* Calculate lengths of strings */
  300. if (exp->l_type==STRING_ST) {
  301. int len;
  302. if (exp->l.string) len = strlen(exp->l.string);
  303. else len = 0;
  304. exp->l.str.s = exp->l.string;
  305. exp->l.str.len = len;
  306. }
  307. if (exp->r_type==STRING_ST) {
  308. int len;
  309. if (exp->r.string) len = strlen(exp->r.string);
  310. else len = 0;
  311. exp->r.str.s = exp->r.string;
  312. exp->r.str.len = len;
  313. }
  314. if (exp->l_type==SELECT_O) {
  315. if ((ret=resolve_select(exp->l.select)) < 0) {
  316. BUG("Unable to resolve select\n");
  317. print_select(exp->l.select);
  318. return ret;
  319. }
  320. }
  321. if ((exp->r_type==SELECT_O)||(exp->r_type==SELECT_ST)) {
  322. if ((ret=resolve_select(exp->r.select)) < 0) {
  323. BUG("Unable to resolve select\n");
  324. print_select(exp->l.select);
  325. return ret;
  326. }
  327. }
  328. ret=0;
  329. }
  330. return ret;
  331. }
  332. /* adds the proxies in the proxy list & resolves the hostnames */
  333. /* returns 0 if ok, <0 on error */
  334. static int fix_actions(struct action* a)
  335. {
  336. struct action *t;
  337. struct proxy_l* p;
  338. char *tmp;
  339. int ret;
  340. cmd_export_t* cmd;
  341. str s;
  342. struct hostent* he;
  343. struct ip_addr ip;
  344. struct socket_info* si;
  345. if (a==0){
  346. LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
  347. return E_BUG;
  348. }
  349. for(t=a; t!=0; t=t->next){
  350. switch(t->type){
  351. case FORWARD_T:
  352. case FORWARD_TLS_T:
  353. case FORWARD_TCP_T:
  354. case FORWARD_UDP_T:
  355. case SEND_T:
  356. case SEND_TCP_T:
  357. switch(t->val[0].type){
  358. case IP_ST:
  359. tmp=strdup(ip_addr2a(
  360. (struct ip_addr*)t->val[0].u.data));
  361. if (tmp==0){
  362. LOG(L_CRIT, "ERROR: fix_actions:"
  363. "memory allocation failure\n");
  364. return E_OUT_OF_MEM;
  365. }
  366. t->val[0].type=STRING_ST;
  367. t->val[0].u.string=tmp;
  368. /* no break */
  369. case STRING_ST:
  370. s.s = t->val[0].u.string;
  371. s.len = strlen(s.s);
  372. p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
  373. if (p==0) return E_BAD_ADDRESS;
  374. t->val[0].u.data=p;
  375. t->val[0].type=PROXY_ST;
  376. break;
  377. case URIHOST_ST:
  378. break;
  379. default:
  380. LOG(L_CRIT, "BUG: fix_actions: invalid type"
  381. "%d (should be string or number)\n",
  382. t->type);
  383. return E_BUG;
  384. }
  385. break;
  386. case IF_T:
  387. if (t->val[0].type!=EXPR_ST){
  388. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  389. "%d for if (should be expr)\n",
  390. t->val[0].type);
  391. return E_BUG;
  392. }else if( (t->val[1].type!=ACTIONS_ST)&&(t->val[1].type!=NOSUBTYPE) ){
  393. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  394. "%d for if() {...} (should be action)\n",
  395. t->val[1].type);
  396. return E_BUG;
  397. }else if( (t->val[2].type!=ACTIONS_ST)&&(t->val[2].type!=NOSUBTYPE) ){
  398. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  399. "%d for if() {} else{...}(should be action)\n",
  400. t->val[2].type);
  401. return E_BUG;
  402. }
  403. if (t->val[0].u.data){
  404. if ((ret=fix_expr((struct expr*)t->val[0].u.data))<0)
  405. return ret;
  406. }
  407. if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
  408. if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
  409. return ret;
  410. }
  411. if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
  412. if ((ret=fix_actions((struct action*)t->val[2].u.data))<0)
  413. return ret;
  414. }
  415. break;
  416. case ASSIGN_T:
  417. case ADD_T:
  418. if (t->val[0].type != AVP_ST) {
  419. LOG(L_CRIT, "BUG: fix_actions: Invalid left side of assignment\n");
  420. return E_BUG;
  421. }
  422. if (t->val[0].u.attr->type & AVP_CLASS_DOMAIN) {
  423. LOG(L_ERR, "ERROR: You cannot change domain attributes from the script, they are read-only\n");
  424. return E_BUG;
  425. } else if (t->val[0].u.attr->type & AVP_CLASS_GLOBAL) {
  426. LOG(L_ERR, "ERROR: You cannot change global attributes from the script, they are read-only\n");
  427. return E_BUG;
  428. }
  429. if (t->val[1].type == ACTION_ST && t->val[1].u.data) {
  430. if ((ret = fix_actions((struct action*)t->val[1].u.data)) < 0) {
  431. return ret;
  432. }
  433. } else if (t->val[1].type == EXPR_ST && t->val[1].u.data) {
  434. if ((ret = fix_expr((struct expr*)t->val[1].u.data)) < 0) {
  435. return ret;
  436. }
  437. } else if (t->val[1].type == STRING_ST) {
  438. int len;
  439. len = strlen(t->val[1].u.data);
  440. t->val[1].u.str.s = t->val[1].u.data;
  441. t->val[1].u.str.len = len;
  442. } else if (t->val[1].type == SELECT_ST) {
  443. if ((ret=resolve_select(t->val[1].u.select)) < 0) {
  444. BUG("Unable to resolve select\n");
  445. print_select(t->val[1].u.select);
  446. return ret;
  447. }
  448. }
  449. break;
  450. case MODULE_T:
  451. cmd = t->val[0].u.data;
  452. if (cmd && cmd->fixup) {
  453. int i;
  454. DBG("fixing %s()\n", cmd->name);
  455. /* type cast NUMBER to STRING, old modules may expect all STRING params during fixup */
  456. for (i=0; i<t->val[1].u.number; i++) {
  457. if (t->val[i+2].type == NUMBER_ST) {
  458. char buf[30];
  459. snprintf(buf, sizeof(buf)-1, "%ld", t->val[i+2].u.number);
  460. /* fixup currently requires string pkg_malloc-aed */
  461. t->val[i+2].u.string = pkg_malloc(strlen(buf)+1);
  462. if (!t->val[i+2].u.string) {
  463. LOG(L_CRIT, "ERROR: cannot translate NUMBER to STRING\n");
  464. return E_OUT_OF_MEM;
  465. }
  466. strcpy(t->val[i+2].u.string, buf);
  467. t->val[i+2].type = STRING_ST;
  468. }
  469. }
  470. for (i=0; i<t->val[1].u.number; i++) {
  471. void *p;
  472. p = t->val[i+2].u.data;
  473. ret = cmd->fixup(&t->val[i+2].u.data, i+1);
  474. if (t->val[i+2].u.data != p)
  475. t->val[i+2].type = MODFIXUP_ST;
  476. if (ret < 0)
  477. return ret;
  478. }
  479. }
  480. break;
  481. case FORCE_SEND_SOCKET_T:
  482. if (t->val[0].type!=SOCKID_ST){
  483. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  484. "%d for force_send_socket\n",
  485. t->val[0].type);
  486. return E_BUG;
  487. }
  488. he=resolvehost(((struct socket_id*)t->val[0].u.data)->name);
  489. if (he==0){
  490. LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
  491. " could not resolve %s\n",
  492. ((struct socket_id*)t->val[0].u.data)->name);
  493. return E_BAD_ADDRESS;
  494. }
  495. hostent2ip_addr(&ip, he, 0);
  496. si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
  497. ((struct socket_id*)t->val[0].u.data)->proto);
  498. if (si==0){
  499. LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
  500. " argument: %s:%d (ser doesn't listen on it)\n",
  501. ((struct socket_id*)t->val[0].u.data)->name,
  502. ((struct socket_id*)t->val[0].u.data)->port);
  503. return E_BAD_ADDRESS;
  504. }
  505. t->val[0].u.data=si;
  506. t->val[0].type=SOCKETINFO_ST;
  507. break;
  508. }
  509. }
  510. return 0;
  511. }
  512. /* Compare parameters as ordinary numbers
  513. *
  514. * Left and right operands can be either numbers or
  515. * attributes. If either of the attributes if of string type then the length of
  516. * its value will be used.
  517. */
  518. inline static int comp_num(int op, long left, int rtype, union exp_op* r)
  519. {
  520. int_str val;
  521. avp_t* avp;
  522. long right;
  523. if (rtype == AVP_ST) {
  524. avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
  525. if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
  526. else return 0; /* Always fail */
  527. } else if (rtype == NUMBER_ST) {
  528. right = r->intval;
  529. } else {
  530. LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
  531. return E_BUG;
  532. }
  533. switch (op){
  534. case EQUAL_OP: return (long)left == (long)right;
  535. case DIFF_OP: return (long)left != (long)right;
  536. case GT_OP: return (long)left > (long)right;
  537. case LT_OP: return (long)left < (long)right;
  538. case GTE_OP: return (long)left >= (long)right;
  539. case LTE_OP: return (long)left <= (long)right;
  540. default:
  541. LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
  542. return E_BUG;
  543. }
  544. }
  545. /*
  546. * Compare given string "left" with right side of expression
  547. */
  548. inline static int comp_str(int op, str* left, int rtype, union exp_op* r, struct sip_msg* msg)
  549. {
  550. str* right;
  551. int_str val;
  552. str v;
  553. avp_t* avp;
  554. int ret;
  555. char backup;
  556. regex_t* re;
  557. unsigned int l;
  558. right=0; /* warning fix */
  559. if (rtype == AVP_ST) {
  560. avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
  561. if (avp && (avp->flags & AVP_VAL_STR)) right = &val.s;
  562. else return 0;
  563. } else if (rtype == SELECT_ST) {
  564. ret = run_select(&v, r->select, msg);
  565. if (ret > 0) return 0; /* Not found */
  566. else if (ret < 0) goto error; /* Error */
  567. right = &v;
  568. } else if ((op == MATCH_OP && rtype == RE_ST)) {
  569. } else if (op != MATCH_OP && rtype == STRING_ST) {
  570. right = &r->str;
  571. } else if (rtype == NUMBER_ST) {
  572. /* "123" > 100 is not allowed by cfg.y rules
  573. * but can happen as @select or $avp evaluation
  574. * $test > 10
  575. * the right operator MUST be number to do the conversion
  576. */
  577. str2int(left,&l);
  578. return comp_num(op, l, rtype, r);
  579. } else {
  580. LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
  581. "string or RE expected\n", rtype);
  582. goto error;
  583. }
  584. ret=-1;
  585. switch(op){
  586. case EQUAL_OP:
  587. if (left->len != right->len) return 0;
  588. ret=(strncasecmp(left->s, right->s, left->len)==0);
  589. break;
  590. case DIFF_OP:
  591. if (left->len != right->len) return 1;
  592. ret = (strncasecmp(left->s, right->s, left->len)!=0);
  593. break;
  594. case MATCH_OP:
  595. /* this is really ugly -- we put a temporary zero-terminating
  596. * character in the original string; that's because regexps
  597. * take 0-terminated strings and our messages are not
  598. * zero-terminated; it should not hurt as long as this function
  599. * is applied to content of pkg mem, which is always the case
  600. * with calls from route{}; the same goes for fline in reply_route{};
  601. *
  602. * also, the received function should always give us an extra
  603. * character, into which we can put the 0-terminator now;
  604. * an alternative would be allocating a new piece of memory,
  605. * which might be too slow
  606. * -jiri
  607. *
  608. * janakj: AVPs are zero terminated too so this is not problem either
  609. */
  610. backup=left->s[left->len];
  611. if (backup) left->s[left->len]='\0';
  612. if (rtype == AVP_ST || rtype == SELECT_ST) {
  613. /* For AVPs we need to compile the RE on the fly */
  614. re=(regex_t*)pkg_malloc(sizeof(regex_t));
  615. if (re==0){
  616. LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
  617. " failure\n");
  618. left->s[left->len] = backup;
  619. goto error;
  620. }
  621. if (regcomp(re, right->s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
  622. pkg_free(re);
  623. left->s[left->len] = backup;
  624. goto error;
  625. }
  626. ret=(regexec(re, left->s, 0, 0, 0)==0);
  627. regfree(re);
  628. pkg_free(re);
  629. } else {
  630. ret=(regexec(r->re, left->s, 0, 0, 0)==0);
  631. }
  632. if (backup) left->s[left->len] = backup;
  633. break;
  634. default:
  635. LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
  636. goto error;
  637. }
  638. return ret;
  639. error:
  640. return -1;
  641. }
  642. /* eval_elem helping function, returns str op param */
  643. inline static int comp_string(int op, char* left, int rtype, union exp_op* r)
  644. {
  645. int ret;
  646. ret=-1;
  647. switch(op){
  648. case EQUAL_OP:
  649. if (rtype!=STRING_ST){
  650. LOG(L_CRIT, "BUG: comp_string: bad type %d, "
  651. "string expected\n", rtype);
  652. goto error;
  653. }
  654. ret=(strcasecmp(left, r->str.s)==0);
  655. break;
  656. case DIFF_OP:
  657. if (rtype!=STRING_ST){
  658. LOG(L_CRIT, "BUG: comp_string: bad type %d, "
  659. "string expected\n", rtype);
  660. goto error;
  661. }
  662. ret=(strcasecmp(left, r->str.s)!=0);
  663. break;
  664. case MATCH_OP:
  665. if (rtype!=RE_ST){
  666. LOG(L_CRIT, "BUG: comp_string: bad type %d, "
  667. " RE expected\n", rtype);
  668. goto error;
  669. }
  670. ret=(regexec(r->re, left, 0, 0, 0)==0);
  671. break;
  672. default:
  673. LOG(L_CRIT, "BUG: comp_string: unknown op %d\n", op);
  674. goto error;
  675. }
  676. return ret;
  677. error:
  678. return -1;
  679. }
  680. inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r, struct sip_msg* msg)
  681. {
  682. avp_t* avp;
  683. int_str val;
  684. union exp_op num_val;
  685. str tmp;
  686. if (spec->type & AVP_INDEX_ALL) {
  687. avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name, NULL, NULL);
  688. return (avp!=0);
  689. }
  690. avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
  691. if (!avp) return 0;
  692. switch(op) {
  693. case NO_OP:
  694. if (avp->flags & AVP_VAL_STR) {
  695. return val.s.len!=0;
  696. } else {
  697. return val.n != 0;
  698. }
  699. break;
  700. case BINOR_OP:
  701. return (val.n | r->intval)!=0;
  702. break;
  703. case BINAND_OP:
  704. return (val.n & r->intval)!=0;
  705. break;
  706. }
  707. if (avp->flags & AVP_VAL_STR) {
  708. return comp_str(op, &val.s, rtype, r, msg);
  709. } else {
  710. switch(rtype){
  711. case NUMBER_ST:
  712. return comp_num(op, val.n, rtype, r);
  713. case STRING_ST:
  714. tmp.s=r->string;
  715. tmp.len=strlen(r->string);
  716. if (str2int(&tmp, (unsigned int*)&num_val.intval)<0){
  717. LOG(L_ERR, "ERROR: comp_avp: cannot convert string value"
  718. " to int (%s)\n", ZSW(r->string));
  719. return -1;
  720. }
  721. return comp_num(op, val.n, NUMBER_ST, &num_val);
  722. case STR_ST:
  723. if (str2int(&r->str, (unsigned int*)&num_val.intval)<0){
  724. LOG(L_ERR, "ERROR: comp_avp: cannot convert str value"
  725. " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
  726. return -1;
  727. }
  728. return comp_num(op, val.n, NUMBER_ST, &num_val);
  729. default:
  730. LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
  731. "comparison (%d)\n", rtype);
  732. return -1;
  733. }
  734. }
  735. }
  736. /*
  737. * Left side of expression was select
  738. */
  739. inline static int comp_select(int op, select_t* sel, int rtype, union exp_op* r, struct sip_msg* msg)
  740. {
  741. int ret;
  742. str val;
  743. ret = run_select(&val, sel, msg);
  744. if (ret < 0) return -1;
  745. if (ret > 0) return 0;
  746. if (val.len==0) return 0;
  747. switch(op) {
  748. case NO_OP: return 1;
  749. case BINOR_OP:
  750. case BINAND_OP:
  751. ERR("Binary operators cannot be used with string selects\n");
  752. return -1;
  753. }
  754. return comp_str(op, &val, rtype, r, msg);
  755. }
  756. /* check_self wrapper -- it checks also for the op */
  757. inline static int check_self_op(int op, str* s, unsigned short p)
  758. {
  759. int ret;
  760. ret=check_self(s, p, 0);
  761. switch(op){
  762. case EQUAL_OP:
  763. break;
  764. case DIFF_OP:
  765. if (ret>=0) ret=!ret;
  766. break;
  767. default:
  768. LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
  769. ret=-1;
  770. }
  771. return ret;
  772. }
  773. /* eval_elem helping function, returns an op param */
  774. inline static int comp_ip(int op, struct ip_addr* ip, int rtype, union exp_op* r)
  775. {
  776. struct hostent* he;
  777. char ** h;
  778. int ret;
  779. str tmp;
  780. ret=-1;
  781. switch(rtype){
  782. case NET_ST:
  783. switch(op){
  784. case EQUAL_OP:
  785. ret=(matchnet(ip, r->net)==1);
  786. break;
  787. case DIFF_OP:
  788. ret=(matchnet(ip, r->net)!=1);
  789. break;
  790. default:
  791. goto error_op;
  792. }
  793. break;
  794. case STRING_ST:
  795. case RE_ST:
  796. switch(op){
  797. case EQUAL_OP:
  798. case MATCH_OP:
  799. /* 1: compare with ip2str*/
  800. ret=comp_string(op, ip_addr2a(ip), rtype, r);
  801. if (ret==1) break;
  802. /* 2: resolve (name) & compare w/ all the ips */
  803. if (rtype==STRING_ST){
  804. he=resolvehost(r->str.s);
  805. if (he==0){
  806. DBG("comp_ip: could not resolve %s\n",
  807. r->str.s);
  808. }else if (he->h_addrtype==ip->af){
  809. for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
  810. ret=(memcmp(ip->u.addr, *h, ip->len)==0);
  811. }
  812. if (ret==1) break;
  813. }
  814. }
  815. /* 3: (slow) rev dns the address
  816. * and compare with all the aliases
  817. * !!??!! review: remove this? */
  818. he=rev_resolvehost(ip);
  819. if (he==0){
  820. print_ip( "comp_ip: could not rev_resolve ip address:"
  821. " ", ip, "\n");
  822. ret=0;
  823. }else{
  824. /* compare with primary host name */
  825. ret=comp_string(op, he->h_name, rtype, r);
  826. /* compare with all the aliases */
  827. for(h=he->h_aliases; (ret!=1) && (*h); h++){
  828. ret=comp_string(op, *h, rtype, r);
  829. }
  830. }
  831. break;
  832. case DIFF_OP:
  833. ret=comp_ip(EQUAL_OP, ip, rtype, r);
  834. if (ret>=0) ret=!ret;
  835. break;
  836. default:
  837. goto error_op;
  838. }
  839. break;
  840. case MYSELF_ST: /* check if it's one of our addresses*/
  841. tmp.s=ip_addr2a(ip);
  842. tmp.len=strlen(tmp.s);
  843. ret=check_self_op(op, &tmp, 0);
  844. break;
  845. default:
  846. LOG(L_CRIT, "BUG: comp_ip: invalid type for "
  847. " src_ip or dst_ip (%d)\n", rtype);
  848. ret=-1;
  849. }
  850. return ret;
  851. error_op:
  852. LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
  853. return -1;
  854. }
  855. /* returns: 0/1 (false/true) or -1 on error */
  856. inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
  857. struct sip_msg* msg)
  858. {
  859. struct sip_uri uri;
  860. int ret;
  861. struct onsend_info* snd_inf;
  862. struct ip_addr ip;
  863. ret=E_BUG;
  864. if (e->type!=ELEM_T){
  865. LOG(L_CRIT," BUG: eval_elem: invalid type\n");
  866. goto error;
  867. }
  868. switch(e->l_type){
  869. case METHOD_O:
  870. ret=comp_str(e->op, &msg->first_line.u.request.method,
  871. e->r_type, &e->r, msg);
  872. break;
  873. case URI_O:
  874. if(msg->new_uri.s) {
  875. if (e->r_type==MYSELF_ST){
  876. if (parse_sip_msg_uri(msg)<0) ret=-1;
  877. else ret=check_self_op(e->op, &msg->parsed_uri.host,
  878. msg->parsed_uri.port_no?
  879. msg->parsed_uri.port_no:SIP_PORT);
  880. }else{
  881. ret=comp_str(e->op, &msg->new_uri,
  882. e->r_type, &e->r, msg);
  883. }
  884. }else{
  885. if (e->r_type==MYSELF_ST){
  886. if (parse_sip_msg_uri(msg)<0) ret=-1;
  887. else ret=check_self_op(e->op, &msg->parsed_uri.host,
  888. msg->parsed_uri.port_no?
  889. msg->parsed_uri.port_no:SIP_PORT);
  890. }else{
  891. ret=comp_str(e->op, &msg->first_line.u.request.uri,
  892. e->r_type, &e->r, msg);
  893. }
  894. }
  895. break;
  896. case FROM_URI_O:
  897. if (parse_from_header(msg)!=0){
  898. LOG(L_ERR, "ERROR: eval_elem: bad or missing"
  899. " From: header\n");
  900. goto error;
  901. }
  902. if (e->r_type==MYSELF_ST){
  903. if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
  904. &uri) < 0){
  905. LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
  906. goto error;
  907. }
  908. ret=check_self_op(e->op, &uri.host,
  909. uri.port_no?uri.port_no:SIP_PORT);
  910. }else{
  911. ret=comp_str(e->op, &get_from(msg)->uri,
  912. e->r_type, &e->r, msg);
  913. }
  914. break;
  915. case TO_URI_O:
  916. if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
  917. (msg->to==0))){
  918. LOG(L_ERR, "ERROR: eval_elem: bad or missing"
  919. " To: header\n");
  920. goto error;
  921. }
  922. /* to content is parsed automatically */
  923. if (e->r_type==MYSELF_ST){
  924. if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
  925. &uri) < 0){
  926. LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
  927. goto error;
  928. }
  929. ret=check_self_op(e->op, &uri.host,
  930. uri.port_no?uri.port_no:SIP_PORT);
  931. }else{
  932. ret=comp_str(e->op, &get_to(msg)->uri,
  933. e->r_type, &e->r, msg);
  934. }
  935. break;
  936. case SRCIP_O:
  937. ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r);
  938. break;
  939. case DSTIP_O:
  940. ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r);
  941. break;
  942. case SNDIP_O:
  943. snd_inf=get_onsend_info();
  944. if (snd_inf && snd_inf->send_sock){
  945. ret=comp_ip(e->op, &snd_inf->send_sock->address, e->r_type, &e->r);
  946. }else{
  947. BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
  948. }
  949. break;
  950. case TOIP_O:
  951. snd_inf=get_onsend_info();
  952. if (snd_inf && snd_inf->to){
  953. su2ip_addr(&ip, snd_inf->to);
  954. ret=comp_ip(e->op, &ip, e->r_type, &e->r);
  955. }else{
  956. BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
  957. }
  958. break;
  959. case NUMBER_O:
  960. ret=!(!e->r.intval); /* !! to transform it in {0,1} */
  961. break;
  962. case ACTION_O:
  963. ret=run_actions(h, (struct action*)e->r.param, msg);
  964. if (ret<=0) ret=0;
  965. else ret=1;
  966. break;
  967. case SRCPORT_O:
  968. ret=comp_num(e->op, (int)msg->rcv.src_port,
  969. e->r_type, &e->r);
  970. break;
  971. case DSTPORT_O:
  972. ret=comp_num(e->op, (int)msg->rcv.dst_port,
  973. e->r_type, &e->r);
  974. break;
  975. case SNDPORT_O:
  976. snd_inf=get_onsend_info();
  977. if (snd_inf && snd_inf->send_sock){
  978. ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
  979. e->r_type, &e->r);
  980. }else{
  981. BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
  982. }
  983. break;
  984. case TOPORT_O:
  985. snd_inf=get_onsend_info();
  986. if (snd_inf && snd_inf->to){
  987. ret=comp_num(e->op, (int)su_getport(snd_inf->to),
  988. e->r_type, &e->r);
  989. }else{
  990. BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
  991. }
  992. break;
  993. case PROTO_O:
  994. ret=comp_num(e->op, msg->rcv.proto,
  995. e->r_type, &e->r);
  996. break;
  997. case SNDPROTO_O:
  998. snd_inf=get_onsend_info();
  999. if (snd_inf && snd_inf->send_sock){
  1000. ret=comp_num(e->op, snd_inf->send_sock->proto,
  1001. e->r_type, &e->r);
  1002. }else{
  1003. BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
  1004. }
  1005. break;
  1006. case AF_O:
  1007. ret=comp_num(e->op, (int)msg->rcv.src_ip.af,
  1008. e->r_type, &e->r);
  1009. break;
  1010. case SNDAF_O:
  1011. snd_inf=get_onsend_info();
  1012. if (snd_inf && snd_inf->send_sock){
  1013. ret=comp_num(e->op, snd_inf->send_sock->address.af,
  1014. e->r_type, &e->r);
  1015. }else{
  1016. BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
  1017. }
  1018. break;
  1019. case MSGLEN_O:
  1020. if ((snd_inf=get_onsend_info())!=0){
  1021. ret=comp_num(e->op, (int)snd_inf->len,
  1022. e->r_type, &e->r);
  1023. }else{
  1024. ret=comp_num(e->op, (int)msg->len,
  1025. e->r_type, &e->r);
  1026. }
  1027. break;
  1028. case RETCODE_O:
  1029. ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r);
  1030. break;
  1031. case AVP_O:
  1032. ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg);
  1033. break;
  1034. case SELECT_O:
  1035. ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg);
  1036. break;
  1037. default:
  1038. LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
  1039. e->l_type);
  1040. }
  1041. return ret;
  1042. error:
  1043. return -1;
  1044. }
  1045. /* ret= 0/1 (true/false) , -1 on error */
  1046. int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
  1047. {
  1048. int ret;
  1049. if (e->type==ELEM_T){
  1050. ret=eval_elem(h, e, msg);
  1051. }else if (e->type==EXP_T){
  1052. switch(e->op){
  1053. case LOGAND_OP:
  1054. ret=eval_expr(h, e->l.expr, msg);
  1055. /* if error or false stop evaluating the rest */
  1056. if (ret!=1) break;
  1057. ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
  1058. break;
  1059. case LOGOR_OP:
  1060. ret=eval_expr(h, e->l.expr, msg);
  1061. /* if true or error stop evaluating the rest */
  1062. if (ret!=0) break;
  1063. ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
  1064. break;
  1065. case NOT_OP:
  1066. ret=eval_expr(h, e->l.expr, msg);
  1067. if (ret<0) break;
  1068. ret= ! ret;
  1069. break;
  1070. default:
  1071. LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
  1072. ret=-1;
  1073. }
  1074. }else{
  1075. LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
  1076. ret=-1;
  1077. }
  1078. return ret;
  1079. }
  1080. /* adds an action list to head; a must be null terminated (last a->next=0))*/
  1081. void push(struct action* a, struct action** head)
  1082. {
  1083. struct action *t;
  1084. if (*head==0){
  1085. *head=a;
  1086. return;
  1087. }
  1088. for (t=*head; t->next;t=t->next);
  1089. t->next=a;
  1090. }
  1091. int add_actions(struct action* a, struct action** head)
  1092. {
  1093. int ret;
  1094. LOG(L_DBG, "add_actions: fixing actions...\n");
  1095. if ((ret=fix_actions(a))!=0) goto error;
  1096. push(a,head);
  1097. return 0;
  1098. error:
  1099. return ret;
  1100. }
  1101. static int fix_rl(struct route_list* rt)
  1102. {
  1103. int i;
  1104. int ret;
  1105. for(i=0;i<rt->idx; i++){
  1106. if(rt->rlist[i]){
  1107. if ((ret=fix_actions(rt->rlist[i]))!=0){
  1108. return ret;
  1109. }
  1110. }
  1111. }
  1112. return 0;
  1113. }
  1114. /* fixes all action tables */
  1115. /* returns 0 if ok , <0 on error */
  1116. int fix_rls()
  1117. {
  1118. int ret;
  1119. if ((ret=fix_rl(&main_rt))!=0)
  1120. return ret;
  1121. if ((ret=fix_rl(&onreply_rt))!=0)
  1122. return ret;
  1123. if ((ret=fix_rl(&failure_rt))!=0)
  1124. return ret;
  1125. if ((ret=fix_rl(&branch_rt))!=0)
  1126. return ret;
  1127. if ((ret=fix_rl(&onsend_rt))!=0)
  1128. return ret;
  1129. return 0;
  1130. }
  1131. static void print_rl(struct route_list* rt, char* name)
  1132. {
  1133. int j;
  1134. for(j=0; j<rt->entries; j++){
  1135. if (rt->rlist[j]==0){
  1136. if ((j==0) && (rt==&main_rt))
  1137. DBG("WARNING: the main routing table is empty\n");
  1138. continue;
  1139. }
  1140. DBG("%s routing table %d:\n", name, j);
  1141. print_actions(rt->rlist[j]);
  1142. DBG("\n");
  1143. }
  1144. }
  1145. /* debug function, prints routing tables */
  1146. void print_rls()
  1147. {
  1148. print_rl(&main_rt, "");
  1149. print_rl(&onreply_rt, "onreply");
  1150. print_rl(&failure_rt, "failure");
  1151. print_rl(&branch_rt, "branch");
  1152. print_rl(&onsend_rt, "onsend");
  1153. }