route.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. * 2008-12-03 fixups for rvalues in assignments (andrei)
  52. * 2009-05-04 switched IF_T to rval_expr (andrei)
  53. * 2010-06-01 special hack/support for fparam fixups so that they can handle
  54. * variable RVEs (andrei)
  55. * 2010-06-18 ip comparison (comp_ip()) normalizes strings to
  56. * ip/netmask (andrei)
  57. */
  58. /** expression evaluation, route fixups and routing lists.
  59. * @file route.c
  60. * @ingroup core
  61. * Module: @ref core
  62. */
  63. #include <stdlib.h>
  64. #include <sys/types.h>
  65. #include <regex.h>
  66. #include <netdb.h>
  67. #include <string.h>
  68. #include <sys/socket.h>
  69. #include <netinet/in.h>
  70. #include <arpa/inet.h>
  71. #include <netdb.h>
  72. #include "route.h"
  73. #include "forward.h"
  74. #include "dprint.h"
  75. #include "proxy.h"
  76. #include "action.h"
  77. #include "lvalue.h"
  78. #include "rvalue.h"
  79. #include "sr_module.h"
  80. #include "ip_addr.h"
  81. #include "resolve.h"
  82. #include "socket_info.h"
  83. #include "parser/parse_uri.h"
  84. #include "parser/parse_from.h"
  85. #include "parser/parse_to.h"
  86. #include "mem/mem.h"
  87. #include "select.h"
  88. #include "onsend.h"
  89. #include "str_hash.h"
  90. #include "ut.h"
  91. #include "rvalue.h"
  92. #include "switch.h"
  93. #include "cfg/cfg_struct.h"
  94. #define RT_HASH_SIZE 8 /* route names hash */
  95. /* main routing script table */
  96. struct route_list main_rt;
  97. struct route_list onreply_rt;
  98. struct route_list failure_rt;
  99. struct route_list branch_rt;
  100. struct route_list onsend_rt;
  101. struct route_list event_rt;
  102. int route_type = REQUEST_ROUTE;
  103. /** script optimization level, useful for debugging.
  104. * 0 - no optimization
  105. * 1 - optimize rval expressions
  106. * 2 - optimize expr elems
  107. */
  108. int scr_opt_lev=9;
  109. inline static void destroy_rlist(struct route_list* rt)
  110. {
  111. struct str_hash_entry* e;
  112. struct str_hash_entry* tmp;
  113. if (rt->rlist){
  114. pkg_free(rt->rlist);
  115. rt->rlist=0;
  116. rt->entries=0;
  117. }
  118. if (rt->names.table){
  119. clist_foreach_safe(rt->names.table, e, tmp, next){
  120. pkg_free(e);
  121. }
  122. pkg_free(rt->names.table);
  123. rt->names.table=0;
  124. rt->names.size=0;
  125. }
  126. }
  127. void destroy_routes()
  128. {
  129. destroy_rlist(&main_rt);
  130. destroy_rlist(&onreply_rt);
  131. destroy_rlist(&failure_rt);
  132. destroy_rlist(&branch_rt);
  133. destroy_rlist(&event_rt);
  134. }
  135. /* adds route name -> i mapping
  136. * WARNING: it doesn't check for pre-existing routes
  137. * return -1 on error, route index on success
  138. */
  139. static int route_add(struct route_list* rt, char* name, int i)
  140. {
  141. struct str_hash_entry* e;
  142. e=pkg_malloc(sizeof(struct str_hash_entry));
  143. if (e==0){
  144. LOG(L_CRIT, "ERROR: route_add: out of memory\n");
  145. goto error;
  146. }
  147. LM_DBG("mapping routing block (%p)[%s] to %d\n", rt, name, i);
  148. e->key.s=name;
  149. e->key.len=strlen(name);
  150. e->flags=0;
  151. e->u.n=i;
  152. str_hash_add(&rt->names, e);
  153. return 0;
  154. error:
  155. return -1;
  156. }
  157. /* returns -1 on error, 0 on success */
  158. inline static int init_rlist(char* r_name, struct route_list* rt,
  159. int n_entries, int hash_size)
  160. {
  161. rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
  162. if (rt->rlist==0){
  163. LOG(L_CRIT, "ERROR: failed to allocate \"%s\" route tables: "
  164. "out of memory\n", r_name);
  165. goto error;
  166. }
  167. memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
  168. rt->idx=1; /* idx=0 == default == reserved */
  169. rt->entries=n_entries;
  170. if (str_hash_alloc(&rt->names, hash_size)<0){
  171. LOG(L_CRIT, "ERROR: \"%s\" route table: failed to alloc hash\n",
  172. r_name);
  173. goto error;
  174. }
  175. str_hash_init(&rt->names);
  176. route_add(rt, "0", 0); /* default route */
  177. return 0;
  178. error:
  179. return -1;
  180. }
  181. /* init route tables */
  182. int init_routes()
  183. {
  184. if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
  185. goto error;
  186. if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
  187. goto error;
  188. if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
  189. goto error;
  190. if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
  191. goto error;
  192. if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
  193. goto error;
  194. if (init_rlist("event", &event_rt, EVENT_RT_NO, RT_HASH_SIZE)<0)
  195. goto error;
  196. return 0;
  197. error:
  198. destroy_routes();
  199. return -1;
  200. }
  201. static inline int route_new_list(struct route_list* rt)
  202. {
  203. int ret;
  204. struct action** tmp;
  205. ret=-1;
  206. if (rt->idx >= rt->entries){
  207. tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
  208. if (tmp==0){
  209. LOG(L_CRIT, "ERROR: route_new_list: out of memory\n");
  210. goto end;
  211. }
  212. /* init the newly allocated memory chunk */
  213. memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
  214. rt->rlist=tmp;
  215. rt->entries*=2;
  216. }
  217. if (rt->idx<rt->entries){
  218. ret=rt->idx;
  219. rt->idx++;
  220. }
  221. end:
  222. return ret;
  223. }
  224. /*
  225. * if the "name" route already exists, return its index, else
  226. * create a new empty route
  227. * return route index in rt->rlist or -1 on error
  228. */
  229. int route_get(struct route_list* rt, char* name)
  230. {
  231. int len;
  232. struct str_hash_entry* e;
  233. int i;
  234. len=strlen(name);
  235. /* check if exists an non empty*/
  236. e=str_hash_get(&rt->names, name, len);
  237. if (e){
  238. i=e->u.n;
  239. }else{
  240. i=route_new_list(rt);
  241. if (i==-1) goto error;
  242. if (route_add(rt, name, i)<0){
  243. goto error;
  244. }
  245. }
  246. return i;
  247. error:
  248. return -1;
  249. }
  250. /*
  251. * if the "name" route already exists, return its index, else
  252. * return error
  253. * return route index in rt->rlist or -1 on error
  254. */
  255. int route_lookup(struct route_list* rt, char* name)
  256. {
  257. int len;
  258. struct str_hash_entry* e;
  259. len=strlen(name);
  260. /* check if exists an non empty*/
  261. e=str_hash_get(&rt->names, name, len);
  262. if (e){
  263. return e->u.n;
  264. }else{
  265. return -1;
  266. }
  267. }
  268. int fix_actions(struct action* a); /*fwd declaration*/
  269. /** optimize the left side of a struct expr.
  270. * @return 1 if optimized, 0 if not and -1 on error
  271. */
  272. static int exp_optimize_left(struct expr* exp)
  273. {
  274. struct rval_expr* rve;
  275. struct rvalue* rval;
  276. int old_ltype, old_rtype, old_op;
  277. int ret;
  278. ret=0;
  279. if (exp->type!=ELEM_T)
  280. return 0;
  281. old_ltype=exp->l_type;
  282. old_rtype=exp->r_type;
  283. old_op=exp->op;
  284. if (exp->l_type==RVEXP_O){
  285. rve=exp->l.param;
  286. /* rve should be previously fixed/optimized */
  287. /* optimize exp (rval(val)) -> exp(val) */
  288. if (rve->op==RVE_RVAL_OP){
  289. rval=&rve->left.rval;
  290. switch(rval->type){
  291. case RV_INT:
  292. if (exp->op==NO_OP){
  293. exp->l_type=NUMBER_O;
  294. exp->l.param=0;
  295. exp->r_type=NUMBER_ST;
  296. exp->r.numval=rval->v.l;
  297. rval_destroy(rval);
  298. pkg_free(rve);
  299. ret=1;
  300. }
  301. break;
  302. case RV_STR:
  303. /* string evaluated in expression context - not
  304. supported */
  305. break;
  306. case RV_BEXPR:
  307. if (exp->op==NO_OP){
  308. /* replace the current expr. */
  309. *exp=*(rval->v.bexpr);
  310. rval_destroy(rval);
  311. pkg_free(rve);
  312. ret=1;
  313. };
  314. break;
  315. case RV_ACTION_ST:
  316. if (exp->op==NO_OP){
  317. exp->l_type=ACTION_O;
  318. exp->l.param=0;
  319. exp->r_type=ACTION_ST;
  320. exp->r.param=rval->v.action;
  321. rval_destroy(rval);
  322. pkg_free(rve);
  323. ret=1;
  324. }
  325. break;
  326. case RV_SEL:
  327. exp->l.select=pkg_malloc(sizeof(*exp->l.select));
  328. if (exp->l.select){
  329. exp->l_type=SELECT_O;
  330. *exp->l.select=rval->v.sel;
  331. rval_destroy(rval);
  332. pkg_free(rve);
  333. ret=1;
  334. }else
  335. ret=-1;
  336. break;
  337. case RV_AVP:
  338. exp->l.attr=pkg_malloc(sizeof(*exp->l.attr));
  339. if (exp->l.attr){
  340. exp->l_type=AVP_O;
  341. *exp->l.attr=rval->v.avps;
  342. rval_destroy(rval);
  343. pkg_free(rve);
  344. ret=1;
  345. }else
  346. ret=-1;
  347. break;
  348. case RV_PVAR:
  349. exp->l.param=pkg_malloc(sizeof(pv_spec_t));
  350. if (exp->l.param){
  351. exp->l_type=PVAR_O;
  352. *((pv_spec_t*)exp->l.param)=rval->v.pvs;
  353. rval_destroy(rval);
  354. pkg_free(rve);
  355. ret=1;
  356. }else
  357. ret=-1;
  358. break;
  359. case RV_NONE:
  360. break;
  361. }
  362. }
  363. }
  364. if (ret>0)
  365. DBG("left EXP optimized: op%d(_O%d_, ST%d) => op%d(_O%d_, ST%d)\n",
  366. old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
  367. return ret;
  368. }
  369. /** optimize the left side of a struct expr.
  370. * @return 1 if optimized, 0 if not and -1 on error
  371. */
  372. static int exp_optimize_right(struct expr* exp)
  373. {
  374. struct rval_expr* rve;
  375. struct rvalue* rval;
  376. int old_ltype, old_rtype, old_op;
  377. int ret;
  378. ret=0;
  379. if ((exp->type!=ELEM_T) ||(exp->op==NO_OP))
  380. return 0;
  381. old_ltype=exp->l_type;
  382. old_rtype=exp->r_type;
  383. old_op=exp->op;
  384. if (exp->r_type==RVE_ST){
  385. rve=exp->r.param;
  386. /* rve should be previously fixed/optimized */
  387. /* optimize exp (rval(val)) -> exp(val) */
  388. if (rve->op==RVE_RVAL_OP){
  389. rval=&rve->left.rval;
  390. switch(rval->type){
  391. case RV_INT:
  392. exp->r_type=NUMBER_ST;
  393. exp->r.numval=rval->v.l;
  394. rval_destroy(rval);
  395. pkg_free(rve);
  396. ret=1;
  397. break;
  398. case RV_STR:
  399. exp->r.str.s=pkg_malloc(rval->v.s.len+1);
  400. if (exp->r.str.s){
  401. exp->r.str.len=rval->v.s.len;
  402. memcpy(exp->r.str.s, rval->v.s.s, rval->v.s.len);
  403. exp->r.str.s[exp->r.str.len]=0;
  404. exp->r_type=STRING_ST;
  405. rval_destroy(rval);
  406. pkg_free(rve);
  407. ret=1;
  408. }else
  409. ret=-1;
  410. break;
  411. case RV_BEXPR:
  412. /* cannot be optimized further, is an exp_elem
  413. which is not constant */
  414. break;
  415. case RV_ACTION_ST:
  416. /* cannot be optimized further, is not constant and
  417. eval_elem() does not support ACTION_ST for op!=NO_OP*/
  418. break;
  419. case RV_SEL:
  420. exp->r.select=pkg_malloc(sizeof(*exp->l.select));
  421. if (exp->r.select){
  422. exp->r_type=SELECT_ST;
  423. *exp->r.select=rval->v.sel;
  424. rval_destroy(rval);
  425. pkg_free(rve);
  426. ret=1;
  427. }else
  428. ret=-1;
  429. break;
  430. case RV_AVP:
  431. exp->r.attr=pkg_malloc(sizeof(*exp->l.attr));
  432. if (exp->r.attr){
  433. exp->r_type=AVP_ST;
  434. *exp->r.attr=rval->v.avps;
  435. rval_destroy(rval);
  436. pkg_free(rve);
  437. ret=1;
  438. }else
  439. ret=-1;
  440. break;
  441. case RV_PVAR:
  442. exp->r.param=pkg_malloc(sizeof(pv_spec_t));
  443. if (exp->r.param){
  444. exp->r_type=PVAR_ST;
  445. *((pv_spec_t*)exp->r.param)=rval->v.pvs;
  446. rval_destroy(rval);
  447. pkg_free(rve);
  448. ret=1;
  449. }else
  450. ret=-1;
  451. break;
  452. case RV_NONE:
  453. ret=-1;
  454. break;
  455. }
  456. }
  457. }
  458. if (ret>0)
  459. DBG("right EXP optimized: op%d(O%d, _ST%d_) => op%d(O%d, _ST%d_)\n",
  460. old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
  461. return ret;
  462. }
  463. /* traverses an expr tree and compiles the REs where necessary)
  464. * returns: 0 for ok, <0 if errors */
  465. int fix_expr(struct expr* exp)
  466. {
  467. regex_t* re;
  468. int ret;
  469. int len;
  470. ret=E_BUG;
  471. if (exp==0){
  472. LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
  473. return E_BUG;
  474. }
  475. if (exp->type==EXP_T){
  476. switch(exp->op){
  477. case LOGAND_OP:
  478. case LOGOR_OP:
  479. if ((ret=fix_expr(exp->l.expr))!=0)
  480. return ret;
  481. ret=fix_expr(exp->r.expr);
  482. break;
  483. case NOT_OP:
  484. ret=fix_expr(exp->l.expr);
  485. break;
  486. default:
  487. LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
  488. exp->op);
  489. }
  490. }else if (exp->type==ELEM_T){
  491. /* first calculate lengths of strings (only right side, since
  492. left side can never be a string) */
  493. if (exp->r_type==STRING_ST) {
  494. if (exp->r.string) len = strlen(exp->r.string);
  495. else len = 0;
  496. exp->r.str.s = exp->r.string;
  497. exp->r.str.len = len;
  498. }
  499. /* then fix & optimize rve/rvals (they might be optimized
  500. to non-rvals, e.g. string, avp a.s.o and needs to be done
  501. before MATCH_OP and other fixups) */
  502. if (exp->l_type==RVEXP_O){
  503. if ((ret=fix_rval_expr(exp->l.param))<0){
  504. ERR("Unable to fix left rval expression\n");
  505. return ret;
  506. }
  507. if (scr_opt_lev>=2)
  508. exp_optimize_left(exp);
  509. }
  510. if (exp->r_type==RVE_ST){
  511. if ((ret=fix_rval_expr(exp->r.param))<0){
  512. ERR("Unable to fix right rval expression\n");
  513. return ret;
  514. }
  515. if (scr_opt_lev>=2)
  516. exp_optimize_right(exp);
  517. }
  518. if (exp->op==MATCH_OP){
  519. /* right side either has to be string, in which case
  520. * we turn it into regular expression, or it is regular
  521. * expression already. In that case we do nothing
  522. */
  523. if (exp->r_type==STRING_ST){
  524. re=(regex_t*)pkg_malloc(sizeof(regex_t));
  525. if (re==0){
  526. LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
  527. " failure\n");
  528. return E_OUT_OF_MEM;
  529. }
  530. if (regcomp(re, (char*) exp->r.param,
  531. REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
  532. LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
  533. (char*) exp->r.param);
  534. pkg_free(re);
  535. return E_BAD_RE;
  536. }
  537. /* replace the string with the re */
  538. pkg_free(exp->r.param);
  539. exp->r.re=re;
  540. exp->r_type=RE_ST;
  541. }else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST
  542. && exp->r_type != SELECT_ST &&
  543. exp->r_type != SELECT_UNFIXED_ST &&
  544. exp->r_type!= RVE_ST
  545. && exp->r_type != PVAR_ST){
  546. LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
  547. return E_BUG;
  548. }
  549. }
  550. if (exp->l_type==ACTION_O){
  551. ret=fix_actions((struct action*)exp->r.param);
  552. if (ret!=0){
  553. LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
  554. return ret;
  555. }
  556. }
  557. if (exp->l_type==SELECT_UNFIXED_O) {
  558. if ((ret=resolve_select(exp->l.select)) < 0) {
  559. ERR("Unable to resolve select\n");
  560. print_select(exp->l.select);
  561. return ret;
  562. }
  563. exp->l_type=SELECT_O;
  564. }
  565. if (exp->r_type==SELECT_UNFIXED_ST) {
  566. if ((ret=resolve_select(exp->r.select)) < 0) {
  567. ERR("Unable to resolve select\n");
  568. print_select(exp->r.select);
  569. return ret;
  570. }
  571. exp->r_type=SELECT_ST;
  572. }
  573. /* PVAR don't need fixing */
  574. ret=0;
  575. }
  576. return ret;
  577. }
  578. /* adds the proxies in the proxy list & resolves the hostnames */
  579. /* returns 0 if ok, <0 on error */
  580. int fix_actions(struct action* a)
  581. {
  582. struct action *t;
  583. struct proxy_l* p;
  584. char *tmp;
  585. void *tmp_p;
  586. int ret;
  587. int i;
  588. sr31_cmd_export_t* cmd;
  589. str s;
  590. struct hostent* he;
  591. struct ip_addr ip;
  592. struct socket_info* si;
  593. struct lvalue* lval;
  594. struct rval_expr* rve;
  595. struct rval_expr* err_rve;
  596. enum rval_type rve_type, err_type, expected_type;
  597. struct rvalue* rv;
  598. int rve_param_no;
  599. if (a==0){
  600. LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
  601. return E_BUG;
  602. }
  603. for(t=a; t!=0; t=t->next){
  604. switch(t->type){
  605. case FORWARD_T:
  606. case FORWARD_TLS_T:
  607. case FORWARD_TCP_T:
  608. case FORWARD_SCTP_T:
  609. case FORWARD_UDP_T:
  610. switch(t->val[0].type){
  611. case IP_ST:
  612. tmp=strdup(ip_addr2a(
  613. (struct ip_addr*)t->val[0].u.data));
  614. if (tmp==0){
  615. LOG(L_CRIT, "ERROR: fix_actions:"
  616. "memory allocation failure\n");
  617. ret = E_OUT_OF_MEM;
  618. goto error;
  619. }
  620. t->val[0].type=STRING_ST;
  621. t->val[0].u.string=tmp;
  622. /* no break */
  623. case STRING_ST:
  624. s.s = t->val[0].u.string;
  625. s.len = strlen(s.s);
  626. p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
  627. if (p==0) { ret =E_BAD_ADDRESS; goto error; }
  628. t->val[0].u.data=p;
  629. t->val[0].type=PROXY_ST;
  630. break;
  631. case URIHOST_ST:
  632. break;
  633. default:
  634. LOG(L_CRIT, "BUG: fix_actions: invalid type"
  635. "%d (should be string or number)\n",
  636. t->type);
  637. ret = E_BUG;
  638. goto error;
  639. }
  640. break;
  641. case IF_T:
  642. if (t->val[0].type!=RVE_ST){
  643. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  644. "%d for if (should be rval expr)\n",
  645. t->val[0].type);
  646. ret = E_BUG;
  647. goto error;
  648. }else if( (t->val[1].type!=ACTIONS_ST) &&
  649. (t->val[1].type!=NOSUBTYPE) ){
  650. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  651. "%d for if() {...} (should be action)\n",
  652. t->val[1].type);
  653. ret = E_BUG;
  654. goto error;
  655. }else if( (t->val[2].type!=ACTIONS_ST) &&
  656. (t->val[2].type!=NOSUBTYPE) ){
  657. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  658. "%d for if() {} else{...}(should be action)\n",
  659. t->val[2].type);
  660. ret = E_BUG;
  661. goto error;
  662. }
  663. rve=(struct rval_expr*)t->val[0].u.data;
  664. if (rve){
  665. err_rve=0;
  666. if (!rve_check_type(&rve_type, rve, &err_rve,
  667. &err_type, &expected_type)){
  668. if (err_rve)
  669. LOG(L_ERR, "fix_actions: invalid expression "
  670. "(%d,%d): subexpression (%d,%d) has type"
  671. " %s, but %s is expected\n",
  672. rve->fpos.s_line, rve->fpos.s_col,
  673. err_rve->fpos.s_line, err_rve->fpos.s_col,
  674. rval_type_name(err_type),
  675. rval_type_name(expected_type) );
  676. else
  677. LOG(L_ERR, "fix_actions: invalid expression "
  678. "(%d,%d): type mismatch?",
  679. rve->fpos.s_line, rve->fpos.s_col);
  680. ret = E_SCRIPT;
  681. goto error;
  682. }
  683. /* it's not an error anymore to have non-int in an if,
  684. only a script warning (to allow backward compat. stuff
  685. like if (@ruri)
  686. if (rve_type!=RV_INT && rve_type!=RV_NONE){
  687. LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
  688. " bad type, integer expected\n",
  689. rve->fpos.s_line, rve->fpos.s_col);
  690. return E_UNSPEC;
  691. }
  692. */
  693. if ((ret=fix_rval_expr(t->val[0].u.data))<0)
  694. goto error;
  695. }
  696. if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
  697. if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
  698. goto error;
  699. }
  700. if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
  701. if ((ret=fix_actions((struct action*)t->val[2].u.data))
  702. <0)
  703. goto error;
  704. }
  705. break;
  706. case SWITCH_T:
  707. if (t->val[0].type!=RVE_ST){
  708. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  709. "%d for switch() (should be expr)\n",
  710. t->val[0].type);
  711. ret = E_BUG;
  712. goto error;
  713. }else if (t->val[1].type!=CASE_ST){
  714. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  715. "%d for switch(...){...}(should be case)\n",
  716. t->val[1].type);
  717. ret = E_BUG;
  718. goto error;
  719. }
  720. if (t->val[0].u.data){
  721. if ((ret=fix_rval_expr(t->val[0].u.data))<0)
  722. goto error;
  723. }else{
  724. LOG(L_CRIT, "BUG: fix_actions: null switch()"
  725. " expression\n");
  726. ret = E_BUG;
  727. goto error;
  728. }
  729. if ((ret=fix_switch(t))<0)
  730. goto error;
  731. break;
  732. case WHILE_T:
  733. if (t->val[0].type!=RVE_ST){
  734. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  735. "%d for while() (should be expr)\n",
  736. t->val[0].type);
  737. ret = E_BUG;
  738. goto error;
  739. }else if (t->val[1].type!=ACTIONS_ST){
  740. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  741. "%d for while(...){...}(should be action)\n",
  742. t->val[1].type);
  743. ret = E_BUG;
  744. goto error;
  745. }
  746. rve=(struct rval_expr*)t->val[0].u.data;
  747. if (rve){
  748. err_rve=0;
  749. if (!rve_check_type(&rve_type, rve, &err_rve,
  750. &err_type, &expected_type)){
  751. if (err_rve)
  752. LOG(L_ERR, "fix_actions: invalid expression "
  753. "(%d,%d): subexpression (%d,%d) has type"
  754. " %s, but %s is expected\n",
  755. rve->fpos.s_line, rve->fpos.s_col,
  756. err_rve->fpos.s_line, err_rve->fpos.s_col,
  757. rval_type_name(err_type),
  758. rval_type_name(expected_type) );
  759. else
  760. LOG(L_ERR, "fix_actions: invalid expression "
  761. "(%d,%d): type mismatch?",
  762. rve->fpos.s_line, rve->fpos.s_col);
  763. ret = E_SCRIPT;
  764. goto error;
  765. }
  766. if (rve_type!=RV_INT && rve_type!=RV_NONE){
  767. LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
  768. " bad type, integer expected\n",
  769. rve->fpos.s_line, rve->fpos.s_col);
  770. ret = E_SCRIPT;
  771. goto error;
  772. }
  773. if ((ret=fix_rval_expr(t->val[0].u.data))<0)
  774. goto error;
  775. }else{
  776. LOG(L_CRIT, "BUG: fix_actions: null while()"
  777. " expression\n");
  778. ret = E_BUG;
  779. goto error;
  780. }
  781. if ( t->val[1].u.data &&
  782. ((ret= fix_actions((struct action*)t->val[1].u.data))<0)){
  783. goto error;
  784. }
  785. break;
  786. case DROP_T:
  787. /* only RVEs need fixing for drop/return/break */
  788. if (t->val[0].type!=RVE_ST)
  789. break;
  790. rve=(struct rval_expr*)t->val[0].u.data;
  791. if (rve){
  792. err_rve=0;
  793. if (!rve_check_type(&rve_type, rve, &err_rve,
  794. &err_type, &expected_type)){
  795. if (err_rve)
  796. LOG(L_ERR, "fix_actions: invalid expression "
  797. "(%d,%d): subexpression (%d,%d) has type"
  798. " %s, but %s is expected\n",
  799. rve->fpos.s_line, rve->fpos.s_col,
  800. err_rve->fpos.s_line, err_rve->fpos.s_col,
  801. rval_type_name(err_type),
  802. rval_type_name(expected_type) );
  803. else
  804. LOG(L_ERR, "fix_actions: invalid expression "
  805. "(%d,%d): type mismatch?",
  806. rve->fpos.s_line, rve->fpos.s_col);
  807. ret = E_SCRIPT;
  808. goto error;
  809. }
  810. if (rve_type!=RV_INT && rve_type!=RV_NONE){
  811. LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
  812. " bad type, integer expected\n",
  813. rve->fpos.s_line, rve->fpos.s_col);
  814. ret = E_SCRIPT;
  815. goto error;
  816. }
  817. if ((ret=fix_rval_expr(t->val[0].u.data))<0)
  818. goto error;
  819. }else{
  820. LOG(L_CRIT, "BUG: fix_actions: null drop/return"
  821. " expression\n");
  822. ret = E_BUG;
  823. goto error;
  824. }
  825. break;
  826. case ASSIGN_T:
  827. case ADD_T:
  828. if (t->val[0].type !=LVAL_ST) {
  829. LOG(L_CRIT, "BUG: fix_actions: Invalid left side of"
  830. " assignment\n");
  831. ret = E_BUG;
  832. goto error;
  833. }
  834. if (t->val[1].type !=RVE_ST) {
  835. LOG(L_CRIT, "BUG: fix_actions: Invalid right side of"
  836. " assignment (%d)\n", t->val[1].type);
  837. ret = E_BUG;
  838. goto error;
  839. }
  840. lval=t->val[0].u.data;
  841. if (lval->type==LV_AVP){
  842. if (lval->lv.avps.type & AVP_CLASS_DOMAIN) {
  843. LOG(L_ERR, "ERROR: You cannot change domain"
  844. " attributes from the script, they are"
  845. " read-only\n");
  846. ret = E_BUG;
  847. goto error;
  848. } else if (lval->lv.avps.type & AVP_CLASS_GLOBAL) {
  849. LOG(L_ERR, "ERROR: You cannot change global"
  850. " attributes from the script, they are"
  851. "read-only\n");
  852. ret = E_BUG;
  853. goto error;
  854. }
  855. }
  856. if ((ret=fix_rval_expr(t->val[1].u.data))<0)
  857. goto error;
  858. break;
  859. case MODULE0_T:
  860. case MODULE1_T:
  861. case MODULE2_T:
  862. case MODULE3_T:
  863. case MODULE4_T:
  864. case MODULE5_T:
  865. case MODULE6_T:
  866. case MODULEX_T:
  867. cmd = t->val[0].u.data;
  868. rve_param_no = 0;
  869. if (cmd) {
  870. DBG("fixing %s()\n", cmd->name);
  871. if (t->val[1].u.number==0) {
  872. ret = call_fixup(cmd->fixup, 0, 0);
  873. if (ret < 0)
  874. goto error;
  875. }
  876. for (i=0; i < t->val[1].u.number; i++) {
  877. if (t->val[i+2].type == RVE_ST) {
  878. rve = t->val[i+2].u.data;
  879. if (rve_is_constant(rve)) {
  880. /* if expression is constant => evaluate it
  881. as string and replace it with the corresp.
  882. string */
  883. rv = rval_expr_eval(0, 0, rve);
  884. if (rv == 0 ||
  885. rval_get_str( 0, 0, &s, rv, 0) < 0 ) {
  886. ERR("failed to fix constant rve");
  887. if (rv) rval_destroy(rv);
  888. ret = E_BUG;
  889. goto error;
  890. }
  891. rval_destroy(rv);
  892. rve_destroy(rve);
  893. t->val[i+2].type = STRING_ST;/*asciiz string*/
  894. t->val[i+2].u.string = s.s;
  895. /* len is not used for now */
  896. t->val[i+2].u.str.len = s.len;
  897. tmp_p = t->val[i+2].u.data;
  898. ret = call_fixup(cmd->fixup,
  899. &t->val[i+2].u.data, i+1);
  900. if (t->val[i+2].u.data != tmp_p)
  901. t->val[i+2].type = MODFIXUP_ST;
  902. if (ret < 0)
  903. goto error;
  904. } else {
  905. /* expression is not constant => fixup &
  906. optimize it */
  907. rve_param_no++;
  908. if ((ret=fix_rval_expr(t->val[i+2].u.data))
  909. < 0) {
  910. ERR("rve fixup failed\n");
  911. ret = E_BUG;
  912. goto error;
  913. }
  914. }
  915. } else if (t->val[i+2].type == STRING_ST) {
  916. tmp_p = t->val[i+2].u.data;
  917. ret = call_fixup(cmd->fixup,
  918. &t->val[i+2].u.data, i+1);
  919. if (t->val[i+2].u.data != tmp_p)
  920. t->val[i+2].type = MODFIXUP_ST;
  921. if (ret < 0)
  922. goto error;
  923. } else {
  924. BUG("invalid module function param type %d\n",
  925. t->val[i+2].type);
  926. ret = E_BUG;
  927. goto error;
  928. }
  929. } /* for */
  930. /* here all the params are either STRING_ST
  931. (constant RVEs), MODFIXUP_ST (fixed up)
  932. or RVE_ST (non-ct RVEs) */
  933. if (rve_param_no) { /* we have to fix the type */
  934. if (cmd->fixup &&
  935. !(cmd->fixup_flags & FIXUP_F_FPARAM_RVE) &&
  936. cmd->free_fixup == 0) {
  937. BUG("non-ct RVEs (%d) in module function call"
  938. "that does not support them (%s)\n",
  939. rve_param_no, cmd->name);
  940. ret = E_BUG;
  941. goto error;
  942. }
  943. switch(t->type) {
  944. case MODULE1_T:
  945. t->type = MODULE1_RVE_T;
  946. break;
  947. case MODULE2_T:
  948. t->type = MODULE2_RVE_T;
  949. break;
  950. case MODULE3_T:
  951. t->type = MODULE3_RVE_T;
  952. break;
  953. case MODULE4_T:
  954. t->type = MODULE4_RVE_T;
  955. break;
  956. case MODULE5_T:
  957. t->type = MODULE5_RVE_T;
  958. break;
  959. case MODULE6_T:
  960. t->type = MODULE6_RVE_T;
  961. break;
  962. case MODULEX_T:
  963. t->type = MODULEX_RVE_T;
  964. break;
  965. default:
  966. BUG("unsupported module function type %d\n",
  967. t->type);
  968. ret = E_BUG;
  969. goto error;
  970. }
  971. } /* if rve_param_no */
  972. }
  973. break;
  974. case FORCE_SEND_SOCKET_T:
  975. if (t->val[0].type!=SOCKID_ST){
  976. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  977. "%d for force_send_socket\n",
  978. t->val[0].type);
  979. ret = E_BUG;
  980. goto error;
  981. }
  982. he=resolvehost(
  983. ((struct socket_id*)t->val[0].u.data)->addr_lst->name
  984. );
  985. if (he==0){
  986. LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
  987. " could not resolve %s\n",
  988. ((struct socket_id*)t->val[0].u.data)->addr_lst->name);
  989. ret = E_BAD_ADDRESS;
  990. goto error;
  991. }
  992. hostent2ip_addr(&ip, he, 0);
  993. si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
  994. ((struct socket_id*)t->val[0].u.data)->proto);
  995. if (si==0){
  996. LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
  997. " argument: %s:%d (ser doesn't listen on it)\n",
  998. ((struct socket_id*)t->val[0].u.data)->addr_lst->name,
  999. ((struct socket_id*)t->val[0].u.data)->port);
  1000. ret = E_BAD_ADDRESS;
  1001. goto error;
  1002. }
  1003. t->val[0].u.data=si;
  1004. t->val[0].type=SOCKETINFO_ST;
  1005. break;
  1006. case UDP_MTU_TRY_PROTO_T:
  1007. if (t->val[0].type!=NUMBER_ST){
  1008. LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
  1009. "%d for udp_mtu_try_proto\n",
  1010. t->val[0].type);
  1011. ret = E_BUG;
  1012. goto error;
  1013. }
  1014. switch(t->val[0].u.number){
  1015. case PROTO_UDP:
  1016. t->val[0].u.number=0;
  1017. break;
  1018. case PROTO_TCP:
  1019. t->val[0].u.number=FL_MTU_TCP_FB;
  1020. break;
  1021. case PROTO_TLS:
  1022. t->val[0].u.number=FL_MTU_TLS_FB;
  1023. break;
  1024. case PROTO_SCTP:
  1025. t->val[0].u.number=FL_MTU_SCTP_FB;
  1026. break;
  1027. default:
  1028. LOG(L_CRIT, "BUG: fix actions: invalid argument for"
  1029. " udp_mtu_try_proto (%d)\n",
  1030. (unsigned int)t->val[0].u.number);
  1031. }
  1032. break;
  1033. case APPEND_BRANCH_T:
  1034. if (t->val[0].type!=STRING_ST){
  1035. BUG("invalid subtype%d for append_branch_t\n",
  1036. t->val[0].type);
  1037. ret = E_BUG;
  1038. goto error;
  1039. }
  1040. s.s=t->val[0].u.string;
  1041. s.len=(s.s)?strlen(s.s):0;
  1042. t->val[0].u.str=s;
  1043. t->val[0].type=STR_ST;
  1044. break;
  1045. case ROUTE_T:
  1046. if (t->val[0].type == RVE_ST) {
  1047. rve=(struct rval_expr*)t->val[0].u.data;
  1048. if (!rve_is_constant(rve)) {
  1049. if ((ret=fix_rval_expr(t->val[0].u.data)) < 0){
  1050. ERR("route() failed to fix rve at %s:%d\n",
  1051. (t->cfile)?t->cfile:"line", t->cline);
  1052. ret = E_BUG;
  1053. goto error;
  1054. }
  1055. } else {
  1056. /* rve is constant => replace it with a string */
  1057. if ((rv = rval_expr_eval(0, 0, rve)) == 0 ||
  1058. rval_get_str(0, 0, &s, rv, 0) < 0) {
  1059. /* out of mem. or bug ? */
  1060. rval_destroy(rv);
  1061. ERR("route() failed to fix ct. rve at %s:%d\n",
  1062. (t->cfile)?t->cfile:"line", t->cline);
  1063. ret = E_BUG;
  1064. goto error;
  1065. }
  1066. rval_destroy(rv);
  1067. rve_destroy(rve);
  1068. t->val[0].type = STRING_ST;
  1069. t->val[0].u.string = s.s;
  1070. t->val[0].u.str.len = s.len; /* not used */
  1071. /* fall-through the STRING_ST if */
  1072. }
  1073. }
  1074. if (t->val[0].type == STRING_ST) {
  1075. i=route_lookup(&main_rt, t->val[0].u.string);
  1076. if (i < 0) {
  1077. ERR("route \"%s\" not found at %s:%d\n",
  1078. t->val[0].u.string,
  1079. (t->cfile)?t->cfile:"line", t->cline);
  1080. ret = E_SCRIPT;
  1081. goto error;
  1082. }
  1083. t->val[0].type = NUMBER_ST;
  1084. pkg_free(t->val[0].u.string);
  1085. t->val[0].u.number = i;
  1086. } else if (t->val[0].type != NUMBER_ST &&
  1087. t->val[0].type != RVE_ST) {
  1088. BUG("invalid subtype %d for route()\n",
  1089. t->val[0].type);
  1090. ret = E_BUG;
  1091. goto error;
  1092. }
  1093. break;
  1094. case CFG_SELECT_T:
  1095. if (t->val[1].type == RVE_ST) {
  1096. rve = t->val[1].u.data;
  1097. if (rve_is_constant(rve)) {
  1098. /* if expression is constant => evaluate it
  1099. as integer and replace it with the corresp.
  1100. int */
  1101. rv = rval_expr_eval(0, 0, rve);
  1102. if (rv == 0 ||
  1103. rval_get_int( 0, 0, &i, rv, 0) < 0 ) {
  1104. ERR("failed to fix constant rve");
  1105. if (rv) rval_destroy(rv);
  1106. ret = E_BUG;
  1107. goto error;
  1108. }
  1109. rval_destroy(rv);
  1110. rve_destroy(rve);
  1111. t->val[1].type = NUMBER_ST;
  1112. t->val[1].u.number = i;
  1113. } else {
  1114. /* expression is not constant => fixup &
  1115. optimize it */
  1116. if ((ret=fix_rval_expr(rve))
  1117. < 0) {
  1118. ERR("rve fixup failed\n");
  1119. ret = E_BUG;
  1120. goto error;
  1121. }
  1122. }
  1123. } else if (t->val[1].type != NUMBER_ST) {
  1124. BUG("invalid subtype %d for cfg_select()\n",
  1125. t->val[1].type);
  1126. ret = E_BUG;
  1127. goto error;
  1128. }
  1129. case CFG_RESET_T:
  1130. if (t->val[0].type != STRING_ST) {
  1131. BUG("invalid subtype %d for cfg_select() or cfg_reset()\n",
  1132. t->val[0].type);
  1133. ret = E_BUG;
  1134. goto error;
  1135. }
  1136. tmp_p = (void *)cfg_lookup_group(t->val[0].u.string, strlen(t->val[0].u.string));
  1137. if (!tmp_p) {
  1138. ERR("configuration group \"%s\" not found\n",
  1139. t->val[0].u.string);
  1140. ret = E_SCRIPT;
  1141. goto error;
  1142. }
  1143. pkg_free(t->val[0].u.string);
  1144. t->val[0].u.data = tmp_p;
  1145. t->val[0].type = CFG_GROUP_ST;
  1146. break;
  1147. default:
  1148. /* no fixup required for the rest */
  1149. break;
  1150. }
  1151. }
  1152. return 0;
  1153. error:
  1154. LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret,
  1155. (t->cfile)?t->cfile:"", t->cline);
  1156. return ret;
  1157. }
  1158. /* Compare parameters as ordinary numbers
  1159. *
  1160. * Left and right operands can be either numbers or
  1161. * attributes. If either of the attributes if of string type then the length of
  1162. * its value will be used.
  1163. */
  1164. inline static int comp_num(int op, long left, int rtype, union exp_op* r,
  1165. struct sip_msg* msg, struct run_act_ctx* h)
  1166. {
  1167. int_str val;
  1168. pv_value_t pval;
  1169. avp_t* avp;
  1170. int right;
  1171. if (unlikely(op==NO_OP)) return !(!left);
  1172. switch(rtype){
  1173. case AVP_ST:
  1174. avp = search_avp_by_index(r->attr->type, r->attr->name,
  1175. &val, r->attr->index);
  1176. if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
  1177. else return (op == DIFF_OP);
  1178. break;
  1179. case NUMBER_ST:
  1180. right = r->numval;
  1181. break;
  1182. case RVE_ST:
  1183. if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
  1184. return (op == DIFF_OP); /* not found/invalid */
  1185. break;
  1186. case PVAR_ST:
  1187. memset(&pval, 0, sizeof(pv_value_t));
  1188. if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
  1189. return (op == DIFF_OP); /* error, not found => false */
  1190. }
  1191. if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
  1192. right=pval.ri;
  1193. pv_value_destroy(&pval);
  1194. }else{
  1195. pv_value_destroy(&pval);
  1196. return (op == DIFF_OP); /* not found or invalid type */
  1197. }
  1198. break;
  1199. default:
  1200. LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
  1201. return E_BUG;
  1202. }
  1203. switch (op){
  1204. case EQUAL_OP: return (long)left == (long)right;
  1205. case DIFF_OP: return (long)left != (long)right;
  1206. case GT_OP: return (long)left > (long)right;
  1207. case LT_OP: return (long)left < (long)right;
  1208. case GTE_OP: return (long)left >= (long)right;
  1209. case LTE_OP: return (long)left <= (long)right;
  1210. default:
  1211. LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
  1212. return E_BUG;
  1213. }
  1214. return E_BUG;
  1215. }
  1216. /*
  1217. * Compare given string "left" with right side of expression
  1218. */
  1219. inline static int comp_str(int op, str* left, int rtype,
  1220. union exp_op* r, struct sip_msg* msg,
  1221. struct run_act_ctx* h)
  1222. {
  1223. str* right;
  1224. int_str val;
  1225. str v;
  1226. avp_t* avp;
  1227. int ret;
  1228. char backup;
  1229. regex_t* re;
  1230. unsigned int l;
  1231. struct rvalue* rv;
  1232. struct rval_cache rv_cache;
  1233. pv_value_t pval;
  1234. int destroy_pval;
  1235. right=0; /* warning fix */
  1236. rv=0;
  1237. destroy_pval=0;
  1238. if (unlikely(op==NO_OP)) return (left->s!=0);
  1239. switch(rtype){
  1240. case AVP_ST:
  1241. avp = search_avp_by_index(r->attr->type, r->attr->name,
  1242. &val, r->attr->index);
  1243. if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
  1244. else return (op == DIFF_OP);
  1245. break;
  1246. case SELECT_ST:
  1247. ret = run_select(&v, r->select, msg);
  1248. if (unlikely(ret != 0))
  1249. return (op == DIFF_OP); /* Not found or error */
  1250. right = &v;
  1251. break;
  1252. case RVE_ST:
  1253. rval_cache_init(&rv_cache);
  1254. rv=rval_expr_eval(h, msg, r->param);
  1255. if (unlikely (rv==0))
  1256. return (op==DIFF_OP); /* not found or error*/
  1257. if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
  1258. goto error;
  1259. }
  1260. right = &v;
  1261. break;
  1262. case PVAR_ST:
  1263. memset(&pval, 0, sizeof(pv_value_t));
  1264. if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
  1265. return (op == DIFF_OP); /* error, not found => false */
  1266. }
  1267. destroy_pval=1;
  1268. if (likely(pval.flags & PV_VAL_STR)){
  1269. right=&pval.rs;
  1270. }else{
  1271. pv_value_destroy(&pval);
  1272. return (op == DIFF_OP); /* not found or invalid type */
  1273. }
  1274. break;
  1275. case RE_ST:
  1276. if (unlikely(op != MATCH_OP)){
  1277. LOG(L_CRIT, "BUG: comp_str: Bad operator %d,"
  1278. " ~= expected\n", op);
  1279. goto error;
  1280. }
  1281. break;
  1282. case STRING_ST: /* strings are stored as {asciiz, len } */
  1283. case STR_ST:
  1284. right=&r->str;
  1285. break;
  1286. case NUMBER_ST:
  1287. /* "123" > 100 is not allowed by cfg.y rules
  1288. * but can happen as @select or $avp evaluation
  1289. * $test > 10
  1290. * the right operator MUST be number to do the conversion
  1291. */
  1292. if (str2int(left,&l) < 0)
  1293. goto error;
  1294. return comp_num(op, l, rtype, r, msg, h);
  1295. default:
  1296. LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
  1297. "string or RE expected\n", rtype);
  1298. goto error;
  1299. }
  1300. ret=-1;
  1301. switch(op){
  1302. case EQUAL_OP:
  1303. if (left->len != right->len) return 0;
  1304. ret=(strncasecmp(left->s, right->s, left->len)==0);
  1305. break;
  1306. case DIFF_OP:
  1307. if (left->len != right->len) return 1;
  1308. ret = (strncasecmp(left->s, right->s, left->len)!=0);
  1309. break;
  1310. case MATCH_OP:
  1311. /* this is really ugly -- we put a temporary zero-terminating
  1312. * character in the original string; that's because regexps
  1313. * take 0-terminated strings and our messages are not
  1314. * zero-terminated; it should not hurt as long as this function
  1315. * is applied to content of pkg mem, which is always the case
  1316. * with calls from route{}; the same goes for fline in
  1317. * reply_route{};
  1318. *
  1319. * also, the received function should always give us an extra
  1320. * character, into which we can put the 0-terminator now;
  1321. * an alternative would be allocating a new piece of memory,
  1322. * which might be too slow
  1323. * -jiri
  1324. *
  1325. * janakj: AVPs are zero terminated too so this is not problem
  1326. * either
  1327. */
  1328. backup=left->s[left->len];
  1329. left->s[left->len]='\0';
  1330. switch(rtype){
  1331. case AVP_ST:
  1332. case SELECT_ST:
  1333. case RVE_ST:
  1334. case PVAR_ST:
  1335. case STRING_ST:
  1336. case STR_ST:
  1337. /* we need to compile the RE on the fly */
  1338. re=(regex_t*)pkg_malloc(sizeof(regex_t));
  1339. if (re==0){
  1340. LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
  1341. " failure\n");
  1342. left->s[left->len] = backup;
  1343. goto error;
  1344. }
  1345. if (regcomp(re, right->s,
  1346. REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
  1347. pkg_free(re);
  1348. left->s[left->len] = backup;
  1349. goto error;
  1350. }
  1351. ret=(regexec(re, left->s, 0, 0, 0)==0);
  1352. regfree(re);
  1353. pkg_free(re);
  1354. break;
  1355. case RE_ST:
  1356. ret=(regexec(r->re, left->s, 0, 0, 0)==0);
  1357. break;
  1358. default:
  1359. LOG(L_CRIT, "BUG: comp_str: Bad operator type %d, "
  1360. "for ~= \n", rtype);
  1361. goto error;
  1362. }
  1363. left->s[left->len] = backup;
  1364. break;
  1365. default:
  1366. LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
  1367. goto error;
  1368. }
  1369. if (rv){
  1370. rval_cache_clean(&rv_cache);
  1371. rval_destroy(rv);
  1372. }
  1373. if (destroy_pval)
  1374. pv_value_destroy(&pval);
  1375. return ret;
  1376. error:
  1377. if (rv){
  1378. rval_cache_clean(&rv_cache);
  1379. rval_destroy(rv);
  1380. }
  1381. if (destroy_pval)
  1382. pv_value_destroy(&pval);
  1383. return (op == DIFF_OP) ? 1 : -1;
  1384. }
  1385. /* eval_elem helping function, returns str op param */
  1386. inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
  1387. struct sip_msg* msg, struct run_act_ctx* h)
  1388. {
  1389. str s;
  1390. s.s=left;
  1391. s.len=strlen(left);
  1392. return comp_str(op, &s, rtype, r, msg, h);
  1393. }
  1394. inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
  1395. union exp_op* r, struct sip_msg* msg,
  1396. struct run_act_ctx* h)
  1397. {
  1398. avp_t* avp;
  1399. int_str val;
  1400. union exp_op num_val;
  1401. str tmp;
  1402. unsigned int uval;
  1403. if (spec->type & AVP_INDEX_ALL) {
  1404. avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
  1405. NULL, NULL);
  1406. return (avp!=0);
  1407. }
  1408. avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
  1409. if (!avp) return (op == DIFF_OP);
  1410. if (op==NO_OP){
  1411. if (avp->flags & AVP_VAL_STR) {
  1412. return val.s.len!=0;
  1413. } else {
  1414. return val.n != 0;
  1415. }
  1416. }
  1417. if (avp->flags & AVP_VAL_STR) {
  1418. return comp_str(op, &val.s, rtype, r, msg, h);
  1419. } else {
  1420. switch(rtype){
  1421. case NUMBER_ST:
  1422. case AVP_ST:
  1423. case RVE_ST:
  1424. case PVAR_ST:
  1425. return comp_num(op, val.n, rtype, r, msg, h);
  1426. break;
  1427. case STRING_ST:
  1428. tmp.s=r->string;
  1429. tmp.len=strlen(r->string);
  1430. if (str2int(&tmp, &uval)<0){
  1431. LOG(L_WARN, "WARNING: comp_avp: cannot convert"
  1432. " string value to int (%s)\n",
  1433. ZSW(r->string));
  1434. goto error;
  1435. }
  1436. num_val.numval=uval;
  1437. return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
  1438. case STR_ST:
  1439. if (str2int(&r->str, &uval)<0){
  1440. LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
  1441. " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
  1442. goto error;
  1443. }
  1444. num_val.numval=uval;
  1445. return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
  1446. default:
  1447. LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
  1448. "comparison (%d)\n", rtype);
  1449. goto error;
  1450. }
  1451. }
  1452. error:
  1453. return (op == DIFF_OP) ? 1 : -1;
  1454. }
  1455. /*
  1456. * Left side of expression was select
  1457. */
  1458. inline static int comp_select(int op, select_t* sel, int rtype,
  1459. union exp_op* r, struct sip_msg* msg,
  1460. struct run_act_ctx* h)
  1461. {
  1462. int ret;
  1463. str val;
  1464. char empty_str=0;
  1465. ret = run_select(&val, sel, msg);
  1466. if (ret != 0) return (op == DIFF_OP);
  1467. if (op==NO_OP) return (val.len>0);
  1468. if (unlikely(val.len==0)) {
  1469. /* make sure the string pointer uses accessible memory range
  1470. * the comp_str function might dereference it
  1471. */
  1472. val.s=&empty_str;
  1473. }
  1474. return comp_str(op, &val, rtype, r, msg, h);
  1475. }
  1476. inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
  1477. union exp_op* r, struct sip_msg* msg,
  1478. struct run_act_ctx* h)
  1479. {
  1480. int i;
  1481. struct rvalue* rv;
  1482. struct rvalue* rv1;
  1483. struct rval_cache c1;
  1484. rval_cache_init(&c1);
  1485. if (unlikely(rval_expr_eval_rvint(h, msg, &rv, &i, rve, &c1)<0)){
  1486. ERR("failure evaluating expression: bad type\n");
  1487. i=0; /* false */
  1488. goto int_expr;
  1489. }
  1490. if (unlikely(rv)){
  1491. /* no int => str */
  1492. rv1=rval_convert(h, msg, RV_STR, rv, &c1);
  1493. i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
  1494. rval_destroy(rv1);
  1495. rval_destroy(rv);
  1496. rval_cache_clean(&c1);
  1497. return i;
  1498. }
  1499. /* expr evaluated to int */
  1500. int_expr:
  1501. rval_cache_clean(&c1);
  1502. if (op==NO_OP)
  1503. return !(!i); /* transform it into { 0, 1 } */
  1504. return comp_num(op, i, rtype, r, msg, h);
  1505. }
  1506. inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
  1507. union exp_op* r, struct sip_msg* msg,
  1508. struct run_act_ctx* h)
  1509. {
  1510. pv_value_t pval;
  1511. int ret;
  1512. ret=0;
  1513. memset(&pval, 0, sizeof(pv_value_t));
  1514. if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
  1515. return 0; /* error, not found => false */
  1516. }
  1517. if (likely(pval.flags & PV_TYPE_INT)){
  1518. if (op==NO_OP)
  1519. ret=!(!pval.ri);
  1520. else
  1521. ret=comp_num(op, pval.ri, rtype, r, msg, h);
  1522. }else if ((pval.flags==PV_VAL_NONE) ||
  1523. (pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
  1524. if (op==NO_OP)
  1525. ret=0;
  1526. else
  1527. ret=comp_num(op, 0, rtype, r, msg, h);
  1528. }else{
  1529. ret=pval.rs.len!=0;
  1530. if (op!=NO_OP)
  1531. ret=comp_num(op, ret, rtype, r, msg, h);
  1532. }
  1533. pv_value_destroy(&pval);
  1534. return ret;
  1535. }
  1536. /* check_self wrapper -- it checks also for the op */
  1537. inline static int check_self_op(int op, str* s, unsigned short p)
  1538. {
  1539. int ret;
  1540. ret=check_self(s, p, 0);
  1541. switch(op){
  1542. case EQUAL_OP:
  1543. case MATCH_OP:
  1544. break;
  1545. case DIFF_OP:
  1546. ret=(ret > 0) ? 0 : 1;
  1547. break;
  1548. default:
  1549. LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
  1550. ret=-1;
  1551. }
  1552. return ret;
  1553. }
  1554. /* eval_elem helping function, returns an op param */
  1555. inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
  1556. union exp_op* r, struct sip_msg* msg,
  1557. struct run_act_ctx *ctx )
  1558. {
  1559. struct hostent* he;
  1560. char ** h;
  1561. int ret;
  1562. str tmp;
  1563. str* right;
  1564. struct net net;
  1565. union exp_op r_expop;
  1566. struct rvalue* rv;
  1567. struct rval_cache rv_cache;
  1568. avp_t* avp;
  1569. int_str val;
  1570. pv_value_t pval;
  1571. int destroy_pval;
  1572. right=NULL; /* warning fix */
  1573. rv=NULL;
  1574. destroy_pval=0;
  1575. ret=-1;
  1576. he=NULL; /* warning fix */
  1577. switch(rtype){
  1578. case NET_ST:
  1579. switch(op){
  1580. case EQUAL_OP:
  1581. ret=(matchnet(ip, r->net)==1);
  1582. break;
  1583. case DIFF_OP:
  1584. ret=(matchnet(ip, r->net)!=1);
  1585. break;
  1586. default:
  1587. goto error_op;
  1588. }
  1589. return ret; /* exit directly */
  1590. case MYSELF_ST: /* check if it's one of our addresses*/
  1591. tmp.s=ip_addr2a(ip);
  1592. tmp.len=strlen(tmp.s);
  1593. ret=check_self_op(op, &tmp, 0);
  1594. return ret;
  1595. case STRING_ST:
  1596. case STR_ST:
  1597. right=&r->str;
  1598. break;
  1599. case RVE_ST:
  1600. rval_cache_init(&rv_cache);
  1601. rv=rval_expr_eval(ctx, msg, r->param);
  1602. if (unlikely (rv==0))
  1603. return (op==DIFF_OP); /* not found or error*/
  1604. if (unlikely(rval_get_tmp_str(ctx, msg, &tmp, rv, 0, &rv_cache)
  1605. < 0)){
  1606. goto error;
  1607. }
  1608. right = &tmp;
  1609. break;
  1610. case AVP_ST:
  1611. /* we can still have AVP_ST due to the RVE optimisations
  1612. (if a RVE == $avp => rve wrapper removed => pure avp) */
  1613. avp = search_avp_by_index(r->attr->type, r->attr->name,
  1614. &val, r->attr->index);
  1615. if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
  1616. else return (op == DIFF_OP);
  1617. break;
  1618. case SELECT_ST:
  1619. /* see AVP_ST comment and s/AVP_ST/SELECT_ST/ */
  1620. ret = run_select(&tmp, r->select, msg);
  1621. if (unlikely(ret != 0))
  1622. return (op == DIFF_OP); /* Not found or error */
  1623. right = &tmp;
  1624. break;
  1625. case PVAR_ST:
  1626. /* see AVP_ST comment and s/AVP_ST/PVAR_ST/ */
  1627. memset(&pval, 0, sizeof(pv_value_t));
  1628. if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
  1629. return (op == DIFF_OP); /* error, not found => false */
  1630. }
  1631. destroy_pval=1;
  1632. if (likely(pval.flags & PV_VAL_STR)){
  1633. right=&pval.rs;
  1634. }else{
  1635. pv_value_destroy(&pval);
  1636. return (op == DIFF_OP); /* not found or invalid type */
  1637. }
  1638. break;
  1639. case RE_ST:
  1640. if (unlikely(op != MATCH_OP))
  1641. goto error_op;
  1642. /* 1: compare with ip2str*/
  1643. ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
  1644. if (likely(ret==1))
  1645. return ret;
  1646. /* 3: (slow) rev dns the address
  1647. * and compare with all the aliases
  1648. * !!??!! review: remove this? */
  1649. if (unlikely((received_dns & DO_REV_DNS) &&
  1650. ((he=rev_resolvehost(ip))!=0) )){
  1651. /* compare with primary host name */
  1652. ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
  1653. /* compare with all the aliases */
  1654. for(h=he->h_aliases; (ret!=1) && (*h); h++){
  1655. ret=comp_string(op, *h, rtype, r, msg, ctx);
  1656. }
  1657. }else{
  1658. ret=0;
  1659. }
  1660. return ret;
  1661. default:
  1662. LOG(L_CRIT, "BUG: comp_ip: invalid type for "
  1663. " src_ip or dst_ip (%d)\n", rtype);
  1664. return -1;
  1665. }
  1666. /* here "right" is set to the str we compare with */
  1667. r_expop.str=*right;
  1668. switch(op){
  1669. case EQUAL_OP:
  1670. /* 0: try if ip or network (ip/mask) */
  1671. if (mk_net_str(&net, right) == 0) {
  1672. ret=(matchnet(ip, &net)==1);
  1673. break;
  1674. }
  1675. /* 2: resolve (name) & compare w/ all the ips */
  1676. he=resolvehost(right->s);
  1677. if (he==0){
  1678. DBG("comp_ip: could not resolve %s\n", r->str.s);
  1679. }else if (he->h_addrtype==ip->af){
  1680. for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
  1681. ret=(memcmp(ip->u.addr, *h, ip->len)==0);
  1682. }
  1683. if (ret==1) break;
  1684. }
  1685. /* 3: (slow) rev dns the address
  1686. * and compare with all the aliases
  1687. * !!??!! review: remove this? */
  1688. if (unlikely((received_dns & DO_REV_DNS) &&
  1689. ((he=rev_resolvehost(ip))!=0) )){
  1690. /* compare with primary host name */
  1691. ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
  1692. /* compare with all the aliases */
  1693. for(h=he->h_aliases; (ret!=1) && (*h); h++){
  1694. ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
  1695. }
  1696. }else{
  1697. ret=0;
  1698. }
  1699. break;
  1700. case MATCH_OP:
  1701. /* 0: try if ip or network (ip/mask)
  1702. (one should not use MATCH for that, but try to be nice)*/
  1703. if (mk_net_str(&net, right) == 0) {
  1704. ret=(matchnet(ip, &net)==1);
  1705. break;
  1706. }
  1707. /* 1: compare with ip2str (but only for =~)*/
  1708. ret=comp_string(op, ip_addr2a(ip), STR_ST, &r_expop, msg, ctx);
  1709. if (likely(ret==1)) break;
  1710. /* 2: resolve (name) & compare w/ all the ips */
  1711. he=resolvehost(right->s);
  1712. if (he==0){
  1713. DBG("comp_ip: could not resolve %s\n", r->str.s);
  1714. }else if (he->h_addrtype==ip->af){
  1715. for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
  1716. ret=(memcmp(ip->u.addr, *h, ip->len)==0);
  1717. }
  1718. if (ret==1) break;
  1719. }
  1720. /* 3: (slow) rev dns the address
  1721. * and compare with all the aliases
  1722. * !!??!! review: remove this? */
  1723. if (unlikely((received_dns & DO_REV_DNS) &&
  1724. ((he=rev_resolvehost(ip))!=0) )){
  1725. /* compare with primary host name */
  1726. ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
  1727. /* compare with all the aliases */
  1728. for(h=he->h_aliases; (ret!=1) && (*h); h++){
  1729. ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
  1730. }
  1731. }else{
  1732. ret=0;
  1733. }
  1734. break;
  1735. case DIFF_OP:
  1736. ret=(comp_ip(EQUAL_OP, ip, STR_ST, &r_expop, msg, ctx) > 0)?0:1;
  1737. break;
  1738. default:
  1739. goto error_op;
  1740. }
  1741. if (rv){
  1742. rval_cache_clean(&rv_cache);
  1743. rval_destroy(rv);
  1744. }
  1745. if (destroy_pval)
  1746. pv_value_destroy(&pval);
  1747. return ret;
  1748. error_op:
  1749. LOG(L_CRIT, "BUG: comp_ip: invalid operator %d for type %d\n", op, rtype);
  1750. error:
  1751. if (unlikely(rv)){
  1752. rval_cache_clean(&rv_cache);
  1753. rval_destroy(rv);
  1754. }
  1755. if (destroy_pval)
  1756. pv_value_destroy(&pval);
  1757. return -1;
  1758. }
  1759. /* returns: 0/1 (false/true) or -1 on error */
  1760. inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
  1761. struct sip_msg* msg)
  1762. {
  1763. struct sip_uri uri;
  1764. int ret;
  1765. struct onsend_info* snd_inf;
  1766. struct ip_addr ip;
  1767. ret=E_BUG;
  1768. if (e->type!=ELEM_T){
  1769. LOG(L_CRIT," BUG: eval_elem: invalid type\n");
  1770. goto error;
  1771. }
  1772. switch(e->l_type){
  1773. case METHOD_O:
  1774. if(msg->first_line.type==SIP_REQUEST)
  1775. {
  1776. ret=comp_str(e->op, &msg->first_line.u.request.method,
  1777. e->r_type, &e->r, msg, h);
  1778. } else {
  1779. if(parse_headers(msg, HDR_CSEQ_F, 0)!=0 || msg->cseq==NULL)
  1780. {
  1781. LM_ERR("cannot parse cseq header\n");
  1782. goto error;
  1783. }
  1784. ret=comp_str(e->op, &get_cseq(msg)->method,
  1785. e->r_type, &e->r, msg, h);
  1786. }
  1787. break;
  1788. case URI_O:
  1789. if(msg->new_uri.s) {
  1790. if (e->r_type==MYSELF_ST){
  1791. if (parse_sip_msg_uri(msg)<0) ret=-1;
  1792. else ret=check_self_op(e->op, &msg->parsed_uri.host,
  1793. GET_URI_PORT(&msg->parsed_uri));
  1794. }else{
  1795. ret=comp_str(e->op, &msg->new_uri,
  1796. e->r_type, &e->r, msg, h);
  1797. }
  1798. }else{
  1799. if (e->r_type==MYSELF_ST){
  1800. if (parse_sip_msg_uri(msg)<0) ret=-1;
  1801. else ret=check_self_op(e->op, &msg->parsed_uri.host,
  1802. GET_URI_PORT(&msg->parsed_uri));
  1803. }else{
  1804. ret=comp_str(e->op, &msg->first_line.u.request.uri,
  1805. e->r_type, &e->r, msg, h);
  1806. }
  1807. }
  1808. break;
  1809. case FROM_URI_O:
  1810. if (parse_from_header(msg)!=0){
  1811. LOG(L_ERR, "ERROR: eval_elem: bad or missing"
  1812. " From: header\n");
  1813. goto error;
  1814. }
  1815. if (e->r_type==MYSELF_ST){
  1816. if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
  1817. &uri) < 0){
  1818. LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
  1819. goto error;
  1820. }
  1821. ret=check_self_op(e->op, &uri.host, GET_URI_PORT(&uri));
  1822. }else{
  1823. ret=comp_str(e->op, &get_from(msg)->uri,
  1824. e->r_type, &e->r, msg, h);
  1825. }
  1826. break;
  1827. case TO_URI_O:
  1828. if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
  1829. (msg->to==0))){
  1830. LOG(L_ERR, "ERROR: eval_elem: bad or missing"
  1831. " To: header\n");
  1832. goto error;
  1833. }
  1834. /* to content is parsed automatically */
  1835. if (e->r_type==MYSELF_ST){
  1836. if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
  1837. &uri) < 0){
  1838. LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
  1839. goto error;
  1840. }
  1841. ret=check_self_op(e->op, &uri.host, GET_URI_PORT(&uri));
  1842. }else{
  1843. ret=comp_str(e->op, &get_to(msg)->uri,
  1844. e->r_type, &e->r, msg, h);
  1845. }
  1846. break;
  1847. case SRCIP_O:
  1848. ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
  1849. break;
  1850. case DSTIP_O:
  1851. ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
  1852. break;
  1853. case SNDIP_O:
  1854. snd_inf=get_onsend_info();
  1855. if (likely(snd_inf && snd_inf->send_sock)){
  1856. ret=comp_ip(e->op, &snd_inf->send_sock->address,
  1857. e->r_type, &e->r, msg, h);
  1858. }else{
  1859. BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
  1860. }
  1861. break;
  1862. case TOIP_O:
  1863. snd_inf=get_onsend_info();
  1864. if (likely(snd_inf && snd_inf->to)){
  1865. su2ip_addr(&ip, snd_inf->to);
  1866. ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
  1867. }else{
  1868. BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
  1869. }
  1870. break;
  1871. case NUMBER_O:
  1872. ret=!(!e->r.numval); /* !! to transform it in {0,1} */
  1873. break;
  1874. case ACTION_O:
  1875. ret=run_actions(h, (struct action*)e->r.param, msg);
  1876. if (ret<=0) ret=0;
  1877. else ret=1;
  1878. break;
  1879. case SRCPORT_O:
  1880. ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
  1881. break;
  1882. case DSTPORT_O:
  1883. ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
  1884. break;
  1885. case SNDPORT_O:
  1886. snd_inf=get_onsend_info();
  1887. if (likely(snd_inf && snd_inf->send_sock)){
  1888. ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
  1889. e->r_type, &e->r, msg, h);
  1890. }else{
  1891. BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
  1892. }
  1893. break;
  1894. case TOPORT_O:
  1895. snd_inf=get_onsend_info();
  1896. if (likely(snd_inf && snd_inf->to)){
  1897. ret=comp_num(e->op, (int)su_getport(snd_inf->to),
  1898. e->r_type, &e->r, msg, h);
  1899. }else{
  1900. BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
  1901. }
  1902. break;
  1903. case PROTO_O:
  1904. ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
  1905. break;
  1906. case SNDPROTO_O:
  1907. snd_inf=get_onsend_info();
  1908. if (likely(snd_inf && snd_inf->send_sock)){
  1909. ret=comp_num(e->op, snd_inf->send_sock->proto,
  1910. e->r_type, &e->r, msg, h);
  1911. }else{
  1912. BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
  1913. }
  1914. break;
  1915. case AF_O:
  1916. ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
  1917. break;
  1918. case SNDAF_O:
  1919. snd_inf=get_onsend_info();
  1920. if (likely(snd_inf && snd_inf->send_sock)){
  1921. ret=comp_num(e->op, snd_inf->send_sock->address.af,
  1922. e->r_type, &e->r, msg, h);
  1923. }else{
  1924. BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
  1925. }
  1926. break;
  1927. case MSGLEN_O:
  1928. if ((snd_inf=get_onsend_info())!=0){
  1929. ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
  1930. }else{
  1931. ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
  1932. }
  1933. break;
  1934. case RETCODE_O:
  1935. ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
  1936. break;
  1937. case AVP_O:
  1938. ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
  1939. break;
  1940. case SELECT_O:
  1941. ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
  1942. break;
  1943. case RVEXP_O:
  1944. ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
  1945. break;
  1946. case PVAR_O:
  1947. ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
  1948. break;
  1949. case SELECT_UNFIXED_O:
  1950. BUG("unexpected unfixed select operand %d\n", e->l_type);
  1951. break;
  1952. /*
  1953. default:
  1954. LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
  1955. e->l_type);
  1956. */
  1957. }
  1958. return ret;
  1959. error:
  1960. return (e->op == DIFF_OP) ? 1 : -1;
  1961. }
  1962. /* ret= 1/0 (true/false) , -1 on error (evaluates as false)*/
  1963. int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
  1964. {
  1965. int ret;
  1966. if (e->type==ELEM_T){
  1967. ret=eval_elem(h, e, msg);
  1968. }else if (e->type==EXP_T){
  1969. switch(e->op){
  1970. case LOGAND_OP:
  1971. ret=eval_expr(h, e->l.expr, msg);
  1972. /* if error or false stop evaluating the rest */
  1973. if (ret <= 0) break;
  1974. ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
  1975. break;
  1976. case LOGOR_OP:
  1977. ret=eval_expr(h, e->l.expr, msg);
  1978. /* if true stop evaluating the rest */
  1979. if (ret > 0) break;
  1980. ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
  1981. break;
  1982. case NOT_OP:
  1983. ret=eval_expr(h, e->l.expr, msg);
  1984. ret=(ret > 0) ? 0 : 1;
  1985. break;
  1986. default:
  1987. LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
  1988. ret=-1;
  1989. }
  1990. }else{
  1991. LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
  1992. ret=-1;
  1993. }
  1994. return ret;
  1995. }
  1996. /* adds an action list to head; a must be null terminated (last a->next=0))*/
  1997. void push(struct action* a, struct action** head)
  1998. {
  1999. struct action *t;
  2000. if (*head==0){
  2001. *head=a;
  2002. return;
  2003. }
  2004. for (t=*head; t->next;t=t->next);
  2005. t->next=a;
  2006. }
  2007. int add_actions(struct action* a, struct action** head)
  2008. {
  2009. int ret;
  2010. LOG(L_DBG, "add_actions: fixing actions...\n");
  2011. if ((ret=fix_actions(a))!=0) goto error;
  2012. push(a,head);
  2013. return 0;
  2014. error:
  2015. return ret;
  2016. }
  2017. static int fix_rl(struct route_list* rt)
  2018. {
  2019. int i;
  2020. int ret;
  2021. for(i=0;i<rt->idx; i++){
  2022. if(rt->rlist[i]){
  2023. if ((ret=fix_actions(rt->rlist[i]))!=0){
  2024. return ret;
  2025. }
  2026. }
  2027. }
  2028. return 0;
  2029. }
  2030. /* fixes all action tables */
  2031. /* returns 0 if ok , <0 on error */
  2032. int fix_rls()
  2033. {
  2034. int ret;
  2035. if ((ret=fix_rl(&main_rt))!=0)
  2036. return ret;
  2037. if ((ret=fix_rl(&onreply_rt))!=0)
  2038. return ret;
  2039. if ((ret=fix_rl(&failure_rt))!=0)
  2040. return ret;
  2041. if ((ret=fix_rl(&branch_rt))!=0)
  2042. return ret;
  2043. if ((ret=fix_rl(&onsend_rt))!=0)
  2044. return ret;
  2045. if ((ret=fix_rl(&event_rt))!=0)
  2046. return ret;
  2047. return 0;
  2048. }
  2049. static void print_rl(struct route_list* rt, char* name)
  2050. {
  2051. int j;
  2052. for(j=0; j<rt->entries; j++){
  2053. if (rt->rlist[j]==0){
  2054. if ((j==0) && (rt==&main_rt))
  2055. DBG("WARNING: the main routing table is empty\n");
  2056. continue;
  2057. }
  2058. DBG("%s routing table %d:\n", name, j);
  2059. print_actions(rt->rlist[j]);
  2060. DBG("\n");
  2061. }
  2062. }
  2063. /* debug function, prints routing tables */
  2064. void print_rls()
  2065. {
  2066. print_rl(&main_rt, "");
  2067. print_rl(&onreply_rt, "onreply");
  2068. print_rl(&failure_rt, "failure");
  2069. print_rl(&branch_rt, "branch");
  2070. print_rl(&onsend_rt, "onsend");
  2071. print_rl(&event_rt, "event");
  2072. }