route.c 54 KB

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