route.c 33 KB

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