2
0

route.c 56 KB

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