action.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History:
  28. * ---------
  29. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  30. * 2003-01-29 removed scratchpad (jiri)
  31. * 2003-03-19 fixed set* len calculation bug & simplified a little the code
  32. * (should be a little faster now) (andrei)
  33. * replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  34. * 2003-04-01 Added support for loose routing in forward (janakj)
  35. * 2003-04-12 FORCE_RPORT_T added (andrei)
  36. * 2003-04-22 strip_tail added (jiri)
  37. * 2003-10-02 added SET_ADV_ADDR_T & SET_ADV_PORT_T (andrei)
  38. * 2003-10-29 added FORCE_TCP_ALIAS_T (andrei)
  39. * 2004-11-30 added FORCE_SEND_SOCKET_T (andrei)
  40. * 2005-12-12 return & drop/exit differentiation (andrei)
  41. * 2005-12-19 select framework (mma)
  42. * 2006-04-12 updated *_send() calls to use a struct dest_info (andrei)
  43. * 2006-07-27 dns cache and dns based send address failover support (andrei)
  44. * 2006-12-06 on popular request last_retcode set also by module functions
  45. * (andrei)
  46. * 2007-06-14 run_actions & do_action need a ctx or handle now, no more
  47. * static vars (andrei)
  48. * 2008-11-18 support for variable parameter module functions (andrei)
  49. * 2008-12-03 use lvalues/rvalues for assignments (andrei)
  50. * 2008-12-17 added UDP_MTU_TRY_PROTO_T (andrei)
  51. * 2009-05-04 switched IF_T to rval_expr (andrei)
  52. * 2009-09-15 added SET_{FWD,RPL}_NO_CONNECT, SET_{FWD,RPL}_CLOSE (andrei)
  53. * 2010-06-01 special hack/support for fparam fixups so that they can handle
  54. * variable RVEs (andrei)
  55. */
  56. /*!
  57. * \file
  58. * \brief SIP-router core ::
  59. * \ingroup core
  60. * Module: \ref core
  61. */
  62. #include "comp_defs.h"
  63. #include "action.h"
  64. #include "config.h"
  65. #include "error.h"
  66. #include "dprint.h"
  67. #include "proxy.h"
  68. #include "forward.h"
  69. #include "udp_server.h"
  70. #include "route.h"
  71. #include "parser/msg_parser.h"
  72. #include "parser/parse_uri.h"
  73. #include "ut.h"
  74. #include "lvalue.h"
  75. #include "sr_module.h"
  76. #include "select_buf.h"
  77. #include "mem/mem.h"
  78. #include "globals.h"
  79. #include "dset.h"
  80. #include "onsend.h"
  81. #include "resolve.h"
  82. #ifdef USE_TCP
  83. #include "tcp_server.h"
  84. #endif
  85. #ifdef USE_SCTP
  86. #include "sctp_core.h"
  87. #endif
  88. #include "switch.h"
  89. #include "events.h"
  90. #include "cfg/cfg_struct.h"
  91. #include <sys/types.h>
  92. #include <sys/socket.h>
  93. #include <netdb.h>
  94. #include <stdlib.h>
  95. #include <netinet/in.h>
  96. #include <arpa/inet.h>
  97. #include <string.h>
  98. #ifdef DEBUG_DMALLOC
  99. #include <dmalloc.h>
  100. #endif
  101. int _last_returned_code = 0;
  102. struct onsend_info* p_onsend=0; /* onsend route send info */
  103. /* current action executed from config file */
  104. static cfg_action_t *_cfg_crt_action = 0;
  105. /* return current action executed from config file */
  106. cfg_action_t *get_cfg_crt_action(void)
  107. {
  108. return _cfg_crt_action;
  109. }
  110. /* return line in config for current executed action */
  111. int get_cfg_crt_line(void)
  112. {
  113. if(_cfg_crt_action==0)
  114. return 0;
  115. return _cfg_crt_action->cline;
  116. }
  117. /* return name of config for current executed action */
  118. char *get_cfg_crt_name(void)
  119. {
  120. if(_cfg_crt_action==0)
  121. return 0;
  122. return _cfg_crt_action->cfile;
  123. }
  124. /* handle the exit code of a module function call.
  125. * (used internally in do_action())
  126. * @param h - script handle (h->last_retcode and h->run_flags will be set).
  127. * @param ret - module function (v0 or v2) retcode
  128. * Side-effects: sets _last_returned_code
  129. */
  130. #define MODF_HANDLE_RETCODE(h, ret) \
  131. do { \
  132. /* if (unlikely((ret)==0)) (h)->run_flags|=EXIT_R_F; */ \
  133. (h)->run_flags |= EXIT_R_F & (((ret) != 0) -1); \
  134. (h)->last_retcode=(ret); \
  135. _last_returned_code = (h)->last_retcode; \
  136. } while(0)
  137. /* frees parameters converted using MODF_RVE_PARAM_CONVERT() from dst.
  138. * (used internally in do_action())
  139. * Assumes src is unchanged.
  140. * Side-effects: clobbers i (int).
  141. */
  142. #define MODF_RVE_PARAM_FREE(cmd, src, dst) \
  143. for (i=0; i < (dst)[1].u.number; i++) { \
  144. if ((src)[i+2].type == RVE_ST && (dst)[i+2].u.data) { \
  145. if ((dst)[i+2].type == RVE_FREE_FIXUP_ST) {\
  146. /* call free_fixup (which should restore the original
  147. string) */ \
  148. (void)call_fixup((cmd)->free_fixup, &(dst)[i+2].u.data, i+1); \
  149. } else if ((dst)[i+2].type == FPARAM_DYN_ST) {\
  150. /* completely frees fparam and restore original string */\
  151. fparam_free_restore(&(dst)[i+2].u.data); \
  152. } \
  153. /* free allocated string */ \
  154. pkg_free((dst)[i+2].u.data); \
  155. (dst)[i+2].u.data = 0; \
  156. } \
  157. }
  158. /* fills dst from src, converting RVE_ST params to STRING_ST.
  159. * (used internally in do_action())
  160. * @param src - source action_u_t array, as in the action structure
  161. * @param dst - destination action_u_t array, will be filled from src.
  162. * WARNING: dst must be cleaned when done, use MODULE_RVE_PARAM_FREE()
  163. * Side-effects: clobbers i (int), s (str), rv (rvalue*), might jump to error.
  164. */
  165. #define MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst) \
  166. do { \
  167. (dst)[1]=(src)[1]; \
  168. for (i=0; i < (src)[1].u.number; i++) { \
  169. if ((src)[2+i].type == RVE_ST) { \
  170. rv=rval_expr_eval((h), (msg), (src)[i+2].u.data); \
  171. if (unlikely(rv == 0 || \
  172. rval_get_str((h), (msg), &s, rv, 0) < 0)) { \
  173. rval_destroy(rv); \
  174. ERR("failed to convert RVE to string\n"); \
  175. (dst)[1].u.number = i; \
  176. MODF_RVE_PARAM_FREE(cmd, src, dst); \
  177. goto error; \
  178. } \
  179. (dst)[i+2].type = STRING_RVE_ST; \
  180. (dst)[i+2].u.string = s.s; \
  181. (dst)[i+2].u.str.len = s.len; \
  182. rval_destroy(rv); \
  183. if ((cmd)->fixup) {\
  184. if ((cmd)->free_fixup) {\
  185. if (likely( call_fixup((cmd)->fixup, \
  186. &(dst)[i+2].u.data, i+1) >= 0) ) { \
  187. /* success => mark it for calling free fixup */ \
  188. if (likely((dst)[i+2].u.data != s.s)) \
  189. (dst)[i+2].type = RVE_FREE_FIXUP_ST; \
  190. } else { \
  191. /* error calling fixup => mark conv. parameter \
  192. and return error */ \
  193. (dst)[1].u.number = i; \
  194. ERR("runtime fixup failed for %s param %d\n", \
  195. (cmd)->name, i+1); \
  196. MODF_RVE_PARAM_FREE(cmd, src, dst); \
  197. goto error; \
  198. } \
  199. } else if ((cmd)->fixup_flags & FIXUP_F_FPARAM_RVE) { \
  200. if (likely( call_fixup((cmd)->fixup, \
  201. &(dst)[i+2].u.data, i+1) >= 0)) { \
  202. if ((dst)[i+2].u.data != s.s) \
  203. (dst)[i+2].type = FPARAM_DYN_ST; \
  204. } else { \
  205. /* error calling fixup => mark conv. parameter \
  206. and return error */ \
  207. (dst)[1].u.number = i; \
  208. ERR("runtime fixup failed for %s param %d\n", \
  209. (cmd)->name, i+1); \
  210. MODF_RVE_PARAM_FREE(cmd, src, dst); \
  211. goto error; \
  212. }\
  213. } \
  214. } \
  215. } else \
  216. (dst)[i+2]=(src)[i+2]; \
  217. } \
  218. } while(0)
  219. /* call a module function with normal STRING_ST params.
  220. * (used internally in do_action())
  221. * @param f_type - cmd_function type
  222. * @param h
  223. * @param msg
  224. * @param src - source action_u_t array (e.g. action->val)
  225. * @param params... - variable list of parameters, passed to the module
  226. * function
  227. * Side-effects: sets ret, clobbers i (int), s (str), rv (rvalue*), cmd,
  228. * might jump to error.
  229. *
  230. */
  231. #ifdef __SUNPRO_C
  232. #define MODF_CALL(f_type, h, msg, src, ...) \
  233. do { \
  234. cmd=(src)[0].u.data; \
  235. ret=((f_type)cmd->function)((msg), __VAR_ARGS__); \
  236. MODF_HANDLE_RETCODE(h, ret); \
  237. } while (0)
  238. #else /* ! __SUNPRO_C (gcc, icc a.s.o) */
  239. #define MODF_CALL(f_type, h, msg, src, params...) \
  240. do { \
  241. cmd=(src)[0].u.data; \
  242. ret=((f_type)cmd->function)((msg), ## params ); \
  243. MODF_HANDLE_RETCODE(h, ret); \
  244. } while (0)
  245. #endif /* __SUNPRO_C */
  246. /* call a module function with possible RVE params.
  247. * (used internally in do_action())
  248. * @param f_type - cmd_function type
  249. * @param h
  250. * @param msg
  251. * @param src - source action_u_t array (e.g. action->val)
  252. * @param dst - temporary action_u_t array used for conversions. It can be
  253. * used for the function parameters. It's contents it's not
  254. * valid after the call.
  255. * @param params... - variable list of parameters, passed to the module
  256. * function
  257. * Side-effects: sets ret, clobbers i (int), s (str), rv (rvalue*), f, dst,
  258. * might jump to error.
  259. *
  260. */
  261. #ifdef __SUNPRO_C
  262. #define MODF_RVE_CALL(f_type, h, msg, src, dst, ...) \
  263. do { \
  264. cmd=(src)[0].u.data; \
  265. MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \
  266. ret=((f_type)cmd->function)((msg), __VAR_ARGS__); \
  267. MODF_HANDLE_RETCODE(h, ret); \
  268. /* free strings allocated by us or fixups */ \
  269. MODF_RVE_PARAM_FREE(cmd, src, dst); \
  270. } while (0)
  271. #else /* ! __SUNPRO_C (gcc, icc a.s.o) */
  272. #define MODF_RVE_CALL(f_type, h, msg, src, dst, params...) \
  273. do { \
  274. cmd=(src)[0].u.data; \
  275. MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \
  276. ret=((f_type)cmd->function)((msg), ## params ); \
  277. MODF_HANDLE_RETCODE(h, ret); \
  278. /* free strings allocated by us or fixups */ \
  279. MODF_RVE_PARAM_FREE(cmd, src, dst); \
  280. } while (0)
  281. #endif /* __SUNPRO_C */
  282. /* ret= 0! if action -> end of list(e.g DROP),
  283. > 0 to continue processing next actions
  284. and <0 on error */
  285. int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
  286. {
  287. int ret;
  288. int v;
  289. struct dest_info dst;
  290. char* tmp;
  291. char *new_uri, *end, *crt;
  292. sr31_cmd_export_t* cmd;
  293. int len;
  294. int user;
  295. struct sip_uri uri, next_hop;
  296. struct sip_uri *u;
  297. unsigned short port;
  298. str* dst_host;
  299. int i, flags;
  300. avp_t* avp;
  301. struct search_state st;
  302. struct switch_cond_table* sct;
  303. struct switch_jmp_table* sjt;
  304. struct rval_expr* rve;
  305. struct match_cond_table* mct;
  306. struct rvalue* rv;
  307. struct rvalue* rv1;
  308. struct rval_cache c1;
  309. str s;
  310. void *srevp[2];
  311. /* temporary storage space for a struct action.val[] working copy
  312. (needed to transform RVE intro STRING before calling module
  313. functions). [0] is not used (corresp. to the module export pointer),
  314. [1] contains the number of params, and [2..] the param values.
  315. We need [1], because some fixup function use it
  316. (see fixup_get_param_count()). */
  317. static action_u_t mod_f_params[MAX_ACTIONS];
  318. /* reset the value of error to E_UNSPEC so avoid unknowledgable
  319. functions to return with error (status<0) and not setting it
  320. leaving there previous error; cache the previous value though
  321. for functions which want to process it */
  322. prev_ser_error=ser_error;
  323. ser_error=E_UNSPEC;
  324. /* hook for every executed action (in use by cfg debugger) */
  325. if(unlikely(sr_event_enabled(SREV_CFG_RUN_ACTION)))
  326. {
  327. srevp[0] = (void*)a;
  328. srevp[1] = (void*)msg;
  329. sr_event_exec(SREV_CFG_RUN_ACTION, (void*)srevp);
  330. }
  331. ret=E_BUG;
  332. switch ((unsigned char)a->type){
  333. case DROP_T:
  334. switch(a->val[0].type){
  335. case NUMBER_ST:
  336. ret=(int) a->val[0].u.number;
  337. break;
  338. case RVE_ST:
  339. rve=(struct rval_expr*)a->val[0].u.data;
  340. rval_expr_eval_int(h, msg, &ret, rve);
  341. break;
  342. case RETCODE_ST:
  343. ret=h->last_retcode;
  344. break;
  345. default:
  346. BUG("unexpected subtype %d in DROP_T\n",
  347. a->val[0].type);
  348. ret=0;
  349. goto error;
  350. }
  351. h->run_flags|=(unsigned int)a->val[1].u.number;
  352. break;
  353. case FORWARD_T:
  354. #ifdef USE_TCP
  355. case FORWARD_TCP_T:
  356. #endif
  357. #ifdef USE_TLS
  358. case FORWARD_TLS_T:
  359. #endif
  360. #ifdef USE_SCTP
  361. case FORWARD_SCTP_T:
  362. #endif
  363. case FORWARD_UDP_T:
  364. /* init dst */
  365. init_dest_info(&dst);
  366. if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP;
  367. #ifdef USE_TCP
  368. else if (a->type==FORWARD_TCP_T) dst.proto= PROTO_TCP;
  369. #endif
  370. #ifdef USE_TLS
  371. else if (a->type==FORWARD_TLS_T) dst.proto= PROTO_TLS;
  372. #endif
  373. #ifdef USE_SCTP
  374. else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP;
  375. #endif
  376. else dst.proto=PROTO_NONE;
  377. if (a->val[0].type==URIHOST_ST){
  378. /*parse uri*/
  379. if (msg->dst_uri.len) {
  380. ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
  381. &next_hop);
  382. u = &next_hop;
  383. } else {
  384. ret = parse_sip_msg_uri(msg);
  385. u = &msg->parsed_uri;
  386. }
  387. if (ret<0) {
  388. LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
  389. " dropping packet\n");
  390. goto error;
  391. }
  392. switch (a->val[1].type){
  393. case URIPORT_ST:
  394. port=u->port_no;
  395. break;
  396. case NUMBER_ST:
  397. port=a->val[1].u.number;
  398. break;
  399. default:
  400. LOG(L_CRIT, "BUG: do_action bad forward 2nd"
  401. " param type (%d)\n", a->val[1].type);
  402. ret=E_UNSPEC;
  403. goto error_fwd_uri;
  404. }
  405. if (dst.proto == PROTO_NONE){ /* only if proto not set get it
  406. from the uri */
  407. switch(u->proto){
  408. case PROTO_NONE:
  409. /*dst.proto=PROTO_UDP; */
  410. /* no proto, try to get it from the dns */
  411. break;
  412. case PROTO_UDP:
  413. #ifdef USE_TCP
  414. case PROTO_TCP:
  415. case PROTO_WS:
  416. #endif
  417. #ifdef USE_TLS
  418. case PROTO_TLS:
  419. case PROTO_WSS:
  420. #endif
  421. #ifdef USE_SCTP
  422. case PROTO_SCTP:
  423. #endif
  424. dst.proto=u->proto;
  425. break;
  426. default:
  427. LOG(L_ERR,"ERROR: do action: forward: bad uri"
  428. " transport %d\n", u->proto);
  429. ret=E_BAD_PROTO;
  430. goto error_fwd_uri;
  431. }
  432. #ifdef USE_TLS
  433. if (u->type==SIPS_URI_T){
  434. if (u->proto==PROTO_UDP){
  435. LOG(L_ERR, "ERROR: do_action: forward: secure uri"
  436. " incompatible with transport %d\n",
  437. u->proto);
  438. ret=E_BAD_PROTO;
  439. goto error_fwd_uri;
  440. } else if (u->proto!=PROTO_WSS)
  441. dst.proto=PROTO_TLS;
  442. else
  443. dst.proto=PROTO_WSS;
  444. }
  445. #endif
  446. }
  447. #ifdef HONOR_MADDR
  448. if (u->maddr_val.s && u->maddr_val.len)
  449. dst_host=&u->maddr_val;
  450. else
  451. #endif
  452. dst_host=&u->host;
  453. #ifdef USE_COMP
  454. dst.comp=u->comp;
  455. #endif
  456. ret=forward_request(msg, dst_host, port, &dst);
  457. if (ret>=0){
  458. ret=1;
  459. }
  460. }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){
  461. if (dst.proto==PROTO_NONE)
  462. dst.proto=msg->rcv.proto;
  463. proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
  464. ret=forward_request(msg, 0, 0, &dst);
  465. if (ret>=0){
  466. ret=1;
  467. proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
  468. }else if (ser_error!=E_OK){
  469. proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
  470. }
  471. }else{
  472. LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
  473. a->val[0].type, a->val[1].type);
  474. ret=E_BUG;
  475. goto error;
  476. }
  477. break;
  478. case LOG_T:
  479. if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){
  480. LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
  481. a->val[0].type, a->val[1].type);
  482. ret=E_BUG;
  483. goto error;
  484. }
  485. LOG_(DEFAULT_FACILITY, a->val[0].u.number, "<script>: ", "%s",
  486. a->val[1].u.string);
  487. ret=1;
  488. break;
  489. /* jku -- introduce a new branch */
  490. case APPEND_BRANCH_T:
  491. if (unlikely(a->val[0].type!=STR_ST)) {
  492. LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
  493. a->val[0].type );
  494. ret=E_BUG;
  495. goto error;
  496. }
  497. getbflagsval(0, (flag_t*)&flags);
  498. ret=append_branch(msg, &a->val[0].u.str, &msg->dst_uri,
  499. &msg->path_vec, a->val[1].u.number,
  500. (flag_t)flags, msg->force_send_socket,
  501. 0, 0, 0, 0);
  502. /* if the uri is the ruri and q was also not changed, mark
  503. ruri as consumed, to avoid having an identical branch */
  504. if ((a->val[0].u.str.s == 0 || a->val[0].u.str.len == 0) &&
  505. a->val[1].u.number == Q_UNSPECIFIED)
  506. ruri_mark_consumed();
  507. break;
  508. /* remove last branch */
  509. case REMOVE_BRANCH_T:
  510. if (a->val[0].type!=NUMBER_ST) {
  511. ret=drop_sip_branch(0) ? -1 : 1;
  512. } else {
  513. ret=drop_sip_branch(a->val[0].u.number) ? -1 : 1;
  514. }
  515. break;
  516. /* remove all branches */
  517. case CLEAR_BRANCHES_T:
  518. clear_branches();
  519. ret=1;
  520. break;
  521. /* jku begin: is_length_greater_than */
  522. case LEN_GT_T:
  523. if (a->val[0].type!=NUMBER_ST) {
  524. LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
  525. a->val[0].type );
  526. ret=E_BUG;
  527. goto error;
  528. }
  529. /* DBG("XXX: message length %d, max %d\n",
  530. msg->len, a->val[0].u.number ); */
  531. ret = msg->len >= a->val[0].u.number ? 1 : -1;
  532. break;
  533. /* jku end: is_length_greater_than */
  534. /* jku - begin : flag processing */
  535. case SETFLAG_T:
  536. if (a->val[0].type!=NUMBER_ST) {
  537. LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
  538. a->val[0].type );
  539. ret=E_BUG;
  540. goto error;
  541. }
  542. if (!flag_in_range( a->val[0].u.number )) {
  543. ret=E_CFG;
  544. goto error;
  545. }
  546. setflag( msg, a->val[0].u.number );
  547. ret=1;
  548. break;
  549. case RESETFLAG_T:
  550. if (a->val[0].type!=NUMBER_ST) {
  551. LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
  552. a->val[0].type );
  553. ret=E_BUG;
  554. goto error;
  555. }
  556. if (!flag_in_range( a->val[0].u.number )) {
  557. ret=E_CFG;
  558. goto error;
  559. }
  560. resetflag( msg, a->val[0].u.number );
  561. ret=1;
  562. break;
  563. case ISFLAGSET_T:
  564. if (a->val[0].type!=NUMBER_ST) {
  565. LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
  566. a->val[0].type );
  567. ret=E_BUG;
  568. goto error;
  569. }
  570. if (!flag_in_range( a->val[0].u.number )) {
  571. ret=E_CFG;
  572. goto error;
  573. }
  574. ret=isflagset( msg, a->val[0].u.number );
  575. break;
  576. /* jku - end : flag processing */
  577. case AVPFLAG_OPER_T:
  578. ret = 0;
  579. if ((a->val[0].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL ||
  580. (a->val[0].u.attr->type & AVP_NAME_RE)!=0) {
  581. for (avp=search_first_avp(a->val[0].u.attr->type,
  582. a->val[0].u.attr->name, NULL, &st);
  583. avp;
  584. avp = search_next_avp(&st, NULL)) {
  585. switch (a->val[2].u.number) {
  586. /* oper: 0..reset, 1..set, -1..no change */
  587. case 0:
  588. avp->flags &= ~(avp_flags_t)a->val[1].u.number;
  589. break;
  590. case 1:
  591. avp->flags |= (avp_flags_t)a->val[1].u.number;
  592. break;
  593. default:;
  594. }
  595. ret = ret ||
  596. ((avp->flags & (avp_flags_t)a->val[1].u.number) != 0);
  597. }
  598. } else {
  599. avp = search_avp_by_index(a->val[0].u.attr->type,
  600. a->val[0].u.attr->name, NULL,
  601. a->val[0].u.attr->index);
  602. if (avp) {
  603. switch (a->val[2].u.number) {
  604. /* oper: 0..reset, 1..set, -1..no change */
  605. case 0:
  606. avp->flags &= ~(avp_flags_t)a->val[1].u.number;
  607. break;
  608. case 1:
  609. avp->flags |= (avp_flags_t)a->val[1].u.number;
  610. break;
  611. default:;
  612. }
  613. ret = (avp->flags & (avp_flags_t)a->val[1].u.number) != 0;
  614. }
  615. }
  616. if (ret==0)
  617. ret = -1;
  618. break;
  619. case ERROR_T:
  620. if ((a->val[0].type!=STRING_ST)|(a->val[1].type!=STRING_ST)){
  621. LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
  622. a->val[0].type, a->val[1].type);
  623. ret=E_BUG;
  624. goto error;
  625. }
  626. LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
  627. "not implemented yet\n", a->val[0].u.string, a->val[1].u.string);
  628. ret=1;
  629. break;
  630. case ROUTE_T:
  631. if (likely(a->val[0].type == NUMBER_ST))
  632. i = a->val[0].u.number;
  633. else if (a->val[0].type == RVE_ST) {
  634. rv = rval_expr_eval(h, msg, a->val[0].u.data);
  635. rval_cache_init(&c1);
  636. if (unlikely(rv == 0 ||
  637. rval_get_tmp_str(h, msg, &s, rv, 0, &c1) < 0)) {
  638. rval_destroy(rv);
  639. rval_cache_clean(&c1);
  640. ERR("failed to convert RVE to string\n");
  641. ret = E_UNSPEC;
  642. goto error;
  643. }
  644. i = route_lookup(&main_rt, s.s);
  645. if (unlikely(i < 0)) {
  646. ERR("route \"%s\" not found at %s:%d\n",
  647. s.s, (a->cfile)?a->cfile:"line", a->cline);
  648. rval_destroy(rv);
  649. rval_cache_clean(&c1);
  650. s.s = 0;
  651. ret = E_SCRIPT;
  652. goto error;
  653. }
  654. rval_destroy(rv);
  655. rval_cache_clean(&c1);
  656. s.s = 0;
  657. } else {
  658. LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
  659. a->val[0].type);
  660. ret=E_BUG;
  661. goto error;
  662. }
  663. if (unlikely((i>=main_rt.idx)||(i<0))){
  664. LOG(L_ERR, "ERROR: invalid routing table number in"
  665. "route(%lu) at %s:%d\n", a->val[0].u.number,
  666. (a->cfile)?a->cfile:"line", a->cline);
  667. ret=E_CFG;
  668. goto error;
  669. }
  670. /*ret=((ret=run_actions(rlist[a->val[0].u.number],msg))<0)?ret:1;*/
  671. ret=run_actions(h, main_rt.rlist[i], msg);
  672. h->last_retcode=ret;
  673. _last_returned_code = h->last_retcode;
  674. h->run_flags&=~(RETURN_R_F|BREAK_R_F); /* absorb return & break */
  675. break;
  676. case EXEC_T:
  677. if (a->val[0].type!=STRING_ST){
  678. LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
  679. a->val[0].type);
  680. ret=E_BUG;
  681. goto error;
  682. }
  683. LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
  684. " using dumb version...\n", a->val[0].u.string);
  685. ret=system(a->val[0].u.string);
  686. if (ret!=0){
  687. LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
  688. }
  689. ret=1;
  690. break;
  691. case REVERT_URI_T:
  692. if (msg->new_uri.s) {
  693. pkg_free(msg->new_uri.s);
  694. msg->new_uri.len=0;
  695. msg->new_uri.s=0;
  696. msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
  697. ruri_mark_new(); /* available for forking */
  698. };
  699. ret=1;
  700. break;
  701. case SET_HOST_T:
  702. case SET_HOSTPORT_T:
  703. case SET_HOSTPORTTRANS_T:
  704. case SET_HOSTALL_T:
  705. case SET_USER_T:
  706. case SET_USERPASS_T:
  707. case SET_PORT_T:
  708. case SET_URI_T:
  709. case PREFIX_T:
  710. case STRIP_T:
  711. case STRIP_TAIL_T:
  712. case SET_USERPHONE_T:
  713. user=0;
  714. if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
  715. if (a->val[0].type!=NUMBER_ST) {
  716. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  717. a->val[0].type);
  718. ret=E_BUG;
  719. goto error;
  720. }
  721. } else if (a->type!=SET_USERPHONE_T) {
  722. if (a->val[0].type!=STRING_ST) {
  723. LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
  724. a->val[0].type);
  725. ret=E_BUG;
  726. goto error;
  727. }
  728. }
  729. if (a->type==SET_URI_T){
  730. if (msg->new_uri.s) {
  731. pkg_free(msg->new_uri.s);
  732. msg->new_uri.len=0;
  733. }
  734. msg->parsed_uri_ok=0;
  735. len=strlen(a->val[0].u.string);
  736. msg->new_uri.s=pkg_malloc(len+1);
  737. if (msg->new_uri.s==0){
  738. LOG(L_ERR, "ERROR: do_action: memory allocation"
  739. " failure\n");
  740. ret=E_OUT_OF_MEM;
  741. goto error;
  742. }
  743. memcpy(msg->new_uri.s, a->val[0].u.string, len);
  744. msg->new_uri.s[len]=0;
  745. msg->new_uri.len=len;
  746. ruri_mark_new(); /* available for forking */
  747. ret=1;
  748. break;
  749. }
  750. if (msg->parsed_uri_ok==0) {
  751. if (msg->new_uri.s) {
  752. tmp=msg->new_uri.s;
  753. len=msg->new_uri.len;
  754. }else{
  755. tmp=msg->first_line.u.request.uri.s;
  756. len=msg->first_line.u.request.uri.len;
  757. }
  758. if (parse_uri(tmp, len, &uri)<0){
  759. LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
  760. " packet\n", tmp);
  761. ret=E_UNSPEC;
  762. goto error;
  763. }
  764. } else {
  765. uri=msg->parsed_uri;
  766. }
  767. /* skip SET_USERPHONE_T action if the URI is already
  768. * a tel: or tels: URI, or contains the user=phone param */
  769. if ((a->type==SET_USERPHONE_T)
  770. && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T)
  771. || ((uri.user_param_val.len==5) && (memcmp(uri.user_param_val.s, "phone", 5)==0)))
  772. ) {
  773. ret=1;
  774. break;
  775. }
  776. /* SET_PORT_T does not work with tel: URIs */
  777. if ((a->type==SET_PORT_T)
  778. && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
  779. && ((uri.flags & URI_SIP_USER_PHONE)==0)
  780. ) {
  781. LOG(L_ERR, "ERROR: do_action: port number of a tel: URI cannot be set\n");
  782. ret=E_UNSPEC;
  783. goto error;
  784. }
  785. new_uri=pkg_malloc(MAX_URI_SIZE);
  786. if (new_uri==0){
  787. LOG(L_ERR, "ERROR: do_action: memory allocation "
  788. " failure\n");
  789. ret=E_OUT_OF_MEM;
  790. goto error;
  791. }
  792. end=new_uri+MAX_URI_SIZE;
  793. crt=new_uri;
  794. /* begin copying */
  795. /* Preserve the URI scheme unless the host part needs
  796. * to be rewritten, and the shceme is tel: or tels: */
  797. switch (uri.type) {
  798. case SIP_URI_T:
  799. len=s_sip.len;
  800. tmp=s_sip.s;
  801. break;
  802. case SIPS_URI_T:
  803. len=s_sips.len;
  804. tmp=s_sips.s;
  805. break;
  806. case TEL_URI_T:
  807. if ((uri.flags & URI_SIP_USER_PHONE)
  808. || (a->type==SET_HOST_T)
  809. || (a->type==SET_HOSTPORT_T)
  810. || (a->type==SET_HOSTPORTTRANS_T)
  811. ) {
  812. len=s_sip.len;
  813. tmp=s_sip.s;
  814. break;
  815. }
  816. len=s_tel.len;
  817. tmp=s_tel.s;
  818. break;
  819. case TELS_URI_T:
  820. if ((uri.flags & URI_SIP_USER_PHONE)
  821. || (a->type==SET_HOST_T)
  822. || (a->type==SET_HOSTPORT_T)
  823. || (a->type==SET_HOSTPORTTRANS_T)
  824. ) {
  825. len=s_sips.len;
  826. tmp=s_sips.s;
  827. break;
  828. }
  829. len=s_tels.len;
  830. tmp=s_tels.s;
  831. break;
  832. default:
  833. LOG(L_ERR, "ERROR: Unsupported URI scheme (%d), "
  834. "reverted to sip:\n",
  835. uri.type);
  836. len=s_sip.len;
  837. tmp=s_sip.s;
  838. }
  839. if(crt+len+1 /* colon */ >end) goto error_uri;
  840. memcpy(crt,tmp,len);crt+=len;
  841. *crt=':'; crt++;
  842. /* user */
  843. /* prefix (-jiri) */
  844. if (a->type==PREFIX_T) {
  845. tmp=a->val[0].u.string;
  846. len=strlen(tmp); if(crt+len>end) goto error_uri;
  847. memcpy(crt,tmp,len);crt+=len;
  848. /* whatever we had before, with prefix we have username
  849. now */
  850. user=1;
  851. }
  852. if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
  853. tmp=a->val[0].u.string;
  854. len=strlen(tmp);
  855. } else if (a->type==STRIP_T) {
  856. if (a->val[0].u.number>uri.user.len) {
  857. LOG(L_WARN, "Error: too long strip asked; "
  858. " deleting username: %lu of <%.*s>\n",
  859. a->val[0].u.number, uri.user.len, uri.user.s );
  860. len=0;
  861. } else if (a->val[0].u.number==uri.user.len) {
  862. len=0;
  863. } else {
  864. tmp=uri.user.s + a->val[0].u.number;
  865. len=uri.user.len - a->val[0].u.number;
  866. }
  867. } else if (a->type==STRIP_TAIL_T) {
  868. if (a->val[0].u.number>uri.user.len) {
  869. LOG(L_WARN, "WARNING: too long strip_tail asked; "
  870. " deleting username: %lu of <%.*s>\n",
  871. a->val[0].u.number, uri.user.len, uri.user.s );
  872. len=0;
  873. } else if (a->val[0].u.number==uri.user.len) {
  874. len=0;
  875. } else {
  876. tmp=uri.user.s;
  877. len=uri.user.len - a->val[0].u.number;
  878. }
  879. } else {
  880. tmp=uri.user.s;
  881. len=uri.user.len;
  882. }
  883. if (len){
  884. if(crt+len>end) goto error_uri;
  885. memcpy(crt,tmp,len);crt+=len;
  886. user=1; /* we have an user field so mark it */
  887. }
  888. if (a->type==SET_USERPASS_T) tmp=0;
  889. else tmp=uri.passwd.s;
  890. /* passwd */
  891. if (tmp){
  892. len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
  893. *crt=':'; crt++;
  894. memcpy(crt,tmp,len);crt+=len;
  895. }
  896. /* tel: URI parameters */
  897. if ((uri.type==TEL_URI_T)
  898. || (uri.type==TELS_URI_T)
  899. ) {
  900. tmp=uri.params.s;
  901. if (tmp){
  902. len=uri.params.len; if(crt+len+1>end) goto error_uri;
  903. *crt=';'; crt++;
  904. memcpy(crt,tmp,len);crt+=len;
  905. }
  906. }
  907. /* host */
  908. if ((a->type==SET_HOST_T)
  909. || (a->type==SET_HOSTPORT_T)
  910. || (a->type==SET_HOSTALL_T)
  911. || (a->type==SET_HOSTPORTTRANS_T)
  912. ) {
  913. tmp=a->val[0].u.string;
  914. if (tmp) len = strlen(tmp);
  915. else len=0;
  916. } else if ((uri.type==SIP_URI_T)
  917. || (uri.type==SIPS_URI_T)
  918. || (uri.flags & URI_SIP_USER_PHONE)
  919. ) {
  920. tmp=uri.host.s;
  921. len=uri.host.len;
  922. } else {
  923. tmp=0;
  924. }
  925. if (tmp){
  926. if (user) { /* add @ */
  927. if(crt+1>end) goto error_uri;
  928. *crt='@'; crt++;
  929. }
  930. if(crt+len>end) goto error_uri;
  931. memcpy(crt,tmp,len);crt+=len;
  932. }
  933. if(a->type==SET_HOSTALL_T)
  934. goto done_seturi;
  935. /* port */
  936. if ((a->type==SET_HOSTPORT_T)
  937. || (a->type==SET_HOSTPORTTRANS_T))
  938. tmp=0;
  939. else if (a->type==SET_PORT_T) {
  940. tmp=a->val[0].u.string;
  941. if (tmp) {
  942. len = strlen(tmp);
  943. if(len==0) tmp = 0;
  944. } else len = 0;
  945. } else {
  946. tmp=uri.port.s;
  947. len = uri.port.len;
  948. }
  949. if (tmp){
  950. if(crt+len+1>end) goto error_uri;
  951. *crt=':'; crt++;
  952. memcpy(crt,tmp,len);crt+=len;
  953. }
  954. /* params */
  955. if ((a->type==SET_HOSTPORTTRANS_T)
  956. && uri.sip_params.s
  957. && uri.transport.s
  958. ) {
  959. /* bypass the transport parameter */
  960. if (uri.sip_params.s < uri.transport.s) {
  961. /* there are parameters before transport */
  962. len = uri.transport.s - uri.sip_params.s - 1;
  963. /* ignore the ';' at the end */
  964. if (crt+len+1>end) goto error_uri;
  965. *crt=';'; crt++;
  966. memcpy(crt,uri.sip_params.s,len);crt+=len;
  967. }
  968. len = (uri.sip_params.s + uri.sip_params.len) -
  969. (uri.transport.s + uri.transport.len);
  970. if (len > 0) {
  971. /* there are parameters after transport */
  972. if (crt+len>end) goto error_uri;
  973. tmp = uri.transport.s + uri.transport.len;
  974. memcpy(crt,tmp,len);crt+=len;
  975. }
  976. } else {
  977. tmp=uri.sip_params.s;
  978. if (tmp){
  979. len=uri.sip_params.len; if(crt+len+1>end) goto error_uri;
  980. *crt=';'; crt++;
  981. memcpy(crt,tmp,len);crt+=len;
  982. }
  983. }
  984. /* Add the user=phone param if a tel: or tels:
  985. * URI was converted to sip: or sips:.
  986. * (host part of a tel/tels URI was set.)
  987. * Or in case of sip: URI and SET_USERPHONE_T action */
  988. if (((((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
  989. && ((uri.flags & URI_SIP_USER_PHONE)==0))
  990. && ((a->type==SET_HOST_T)
  991. || (a->type==SET_HOSTPORT_T)
  992. || (a->type==SET_HOSTPORTTRANS_T)))
  993. || (a->type==SET_USERPHONE_T)
  994. ) {
  995. tmp=";user=phone";
  996. len=strlen(tmp);
  997. if(crt+len>end) goto error_uri;
  998. memcpy(crt,tmp,len);crt+=len;
  999. }
  1000. /* headers */
  1001. tmp=uri.headers.s;
  1002. if (tmp){
  1003. len=uri.headers.len; if(crt+len+1>end) goto error_uri;
  1004. *crt='?'; crt++;
  1005. memcpy(crt,tmp,len);crt+=len;
  1006. }
  1007. done_seturi:
  1008. *crt=0; /* null terminate the thing */
  1009. /* copy it to the msg */
  1010. if (msg->new_uri.s) pkg_free(msg->new_uri.s);
  1011. msg->new_uri.s=new_uri;
  1012. msg->new_uri.len=crt-new_uri;
  1013. msg->parsed_uri_ok=0;
  1014. ruri_mark_new(); /* available for forking */
  1015. ret=1;
  1016. break;
  1017. case IF_T:
  1018. rve=(struct rval_expr*)a->val[0].u.data;
  1019. if (unlikely(rval_expr_eval_int(h, msg, &v, rve) != 0)){
  1020. ERR("if expression evaluation failed (%d,%d-%d,%d)\n",
  1021. rve->fpos.s_line, rve->fpos.s_col,
  1022. rve->fpos.e_line, rve->fpos.e_col);
  1023. v=0; /* false */
  1024. }
  1025. if (unlikely(h->run_flags & EXIT_R_F)){
  1026. ret=0;
  1027. break;
  1028. }
  1029. h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
  1030. break in expr*/
  1031. ret=1; /*default is continue */
  1032. if (v>0) {
  1033. if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
  1034. ret=run_actions(h,
  1035. (struct action*)a->val[1].u.data, msg);
  1036. }
  1037. }else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
  1038. ret=run_actions(h,
  1039. (struct action*)a->val[2].u.data, msg);
  1040. }
  1041. break;
  1042. case MODULE0_T:
  1043. MODF_CALL(cmd_function, h, msg, a->val, 0, 0);
  1044. break;
  1045. /* instead of using the parameter number, we use different names
  1046. * for calls to functions with 3, 4, 5, 6 or variable number of
  1047. * parameters due to performance reasons */
  1048. case MODULE1_T:
  1049. MODF_CALL(cmd_function, h, msg, a->val,
  1050. (char*)a->val[2].u.data,
  1051. 0
  1052. );
  1053. break;
  1054. case MODULE2_T:
  1055. MODF_CALL(cmd_function, h, msg, a->val,
  1056. (char*)a->val[2].u.data,
  1057. (char*)a->val[3].u.data
  1058. );
  1059. break;
  1060. case MODULE3_T:
  1061. MODF_CALL(cmd_function3, h, msg, a->val,
  1062. (char*)a->val[2].u.data,
  1063. (char*)a->val[3].u.data,
  1064. (char*)a->val[4].u.data
  1065. );
  1066. break;
  1067. case MODULE4_T:
  1068. MODF_CALL(cmd_function4, h, msg, a->val,
  1069. (char*)a->val[2].u.data,
  1070. (char*)a->val[3].u.data,
  1071. (char*)a->val[4].u.data,
  1072. (char*)a->val[5].u.data
  1073. );
  1074. break;
  1075. case MODULE5_T:
  1076. MODF_CALL(cmd_function5, h, msg, a->val,
  1077. (char*)a->val[2].u.data,
  1078. (char*)a->val[3].u.data,
  1079. (char*)a->val[4].u.data,
  1080. (char*)a->val[5].u.data,
  1081. (char*)a->val[6].u.data
  1082. );
  1083. break;
  1084. case MODULE6_T:
  1085. MODF_CALL(cmd_function6, h, msg, a->val,
  1086. (char*)a->val[2].u.data,
  1087. (char*)a->val[3].u.data,
  1088. (char*)a->val[4].u.data,
  1089. (char*)a->val[5].u.data,
  1090. (char*)a->val[6].u.data,
  1091. (char*)a->val[7].u.data
  1092. );
  1093. break;
  1094. case MODULEX_T:
  1095. MODF_CALL(cmd_function_var, h, msg, a->val,
  1096. a->val[1].u.number, &a->val[2]);
  1097. break;
  1098. case MODULE1_RVE_T:
  1099. MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
  1100. (char*)mod_f_params[2].u.data,
  1101. 0
  1102. );
  1103. break;
  1104. case MODULE2_RVE_T:
  1105. MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
  1106. (char*)mod_f_params[2].u.data,
  1107. (char*)mod_f_params[3].u.data
  1108. );
  1109. break;
  1110. case MODULE3_RVE_T:
  1111. MODF_RVE_CALL(cmd_function3, h, msg, a->val, mod_f_params,
  1112. (char*)mod_f_params[2].u.data,
  1113. (char*)mod_f_params[3].u.data,
  1114. (char*)mod_f_params[4].u.data
  1115. );
  1116. break;
  1117. case MODULE4_RVE_T:
  1118. MODF_RVE_CALL(cmd_function4, h, msg, a->val, mod_f_params,
  1119. (char*)mod_f_params[2].u.data,
  1120. (char*)mod_f_params[3].u.data,
  1121. (char*)mod_f_params[4].u.data,
  1122. (char*)mod_f_params[5].u.data
  1123. );
  1124. break;
  1125. case MODULE5_RVE_T:
  1126. MODF_RVE_CALL(cmd_function5, h, msg, a->val, mod_f_params,
  1127. (char*)mod_f_params[2].u.data,
  1128. (char*)mod_f_params[3].u.data,
  1129. (char*)mod_f_params[4].u.data,
  1130. (char*)mod_f_params[5].u.data,
  1131. (char*)mod_f_params[6].u.data
  1132. );
  1133. break;
  1134. case MODULE6_RVE_T:
  1135. MODF_RVE_CALL(cmd_function6, h, msg, a->val, mod_f_params,
  1136. (char*)mod_f_params[2].u.data,
  1137. (char*)mod_f_params[3].u.data,
  1138. (char*)mod_f_params[4].u.data,
  1139. (char*)mod_f_params[5].u.data,
  1140. (char*)mod_f_params[6].u.data,
  1141. (char*)mod_f_params[7].u.data
  1142. );
  1143. break;
  1144. case MODULEX_RVE_T:
  1145. MODF_RVE_CALL(cmd_function_var, h, msg, a->val, mod_f_params,
  1146. a->val[1].u.number, &mod_f_params[2]);
  1147. break;
  1148. case EVAL_T:
  1149. /* only eval the expression to account for possible
  1150. side-effect */
  1151. rval_expr_eval_int(h, msg, &v,
  1152. (struct rval_expr*)a->val[0].u.data);
  1153. if (h->run_flags & EXIT_R_F){
  1154. ret=0;
  1155. break;
  1156. }
  1157. h->run_flags &= ~RETURN_R_F|BREAK_R_F; /* catch return & break in
  1158. expr */
  1159. ret=1; /* default is continue */
  1160. break;
  1161. case SWITCH_COND_T:
  1162. sct=(struct switch_cond_table*)a->val[1].u.data;
  1163. if (unlikely( rval_expr_eval_int(h, msg, &v,
  1164. (struct rval_expr*)a->val[0].u.data) <0)){
  1165. /* handle error in expression => use default */
  1166. ret=-1;
  1167. goto sw_cond_def;
  1168. }
  1169. if (h->run_flags & EXIT_R_F){
  1170. ret=0;
  1171. break;
  1172. }
  1173. h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
  1174. in expr */
  1175. ret=1; /* default is continue */
  1176. for(i=0; i<sct->n; i++)
  1177. if (sct->cond[i]==v){
  1178. if (likely(sct->jump[i])){
  1179. ret=run_actions(h, sct->jump[i], msg);
  1180. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1181. returns passthrough */
  1182. }
  1183. goto skip;
  1184. }
  1185. sw_cond_def:
  1186. if (sct->def){
  1187. ret=run_actions(h, sct->def, msg);
  1188. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1189. returns passthrough */
  1190. }
  1191. break;
  1192. case SWITCH_JT_T:
  1193. sjt=(struct switch_jmp_table*)a->val[1].u.data;
  1194. if (unlikely( rval_expr_eval_int(h, msg, &v,
  1195. (struct rval_expr*)a->val[0].u.data) <0)){
  1196. /* handle error in expression => use default */
  1197. ret=-1;
  1198. goto sw_jt_def;
  1199. }
  1200. if (h->run_flags & EXIT_R_F){
  1201. ret=0;
  1202. break;
  1203. }
  1204. h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
  1205. in expr */
  1206. ret=1; /* default is continue */
  1207. if (likely(v >= sjt->first && v <= sjt->last)){
  1208. if (likely(sjt->tbl[v - sjt->first])){
  1209. ret=run_actions(h, sjt->tbl[v - sjt->first], msg);
  1210. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1211. returns passthrough */
  1212. }
  1213. break;
  1214. }else{
  1215. for(i=0; i<sjt->rest.n; i++)
  1216. if (sjt->rest.cond[i]==v){
  1217. if (likely(sjt->rest.jump[i])){
  1218. ret=run_actions(h, sjt->rest.jump[i], msg);
  1219. h->run_flags &= ~BREAK_R_F; /* catch breaks, but
  1220. let returns pass */
  1221. }
  1222. goto skip;
  1223. }
  1224. }
  1225. /* not found => try default */
  1226. sw_jt_def:
  1227. if (sjt->rest.def){
  1228. ret=run_actions(h, sjt->rest.def, msg);
  1229. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1230. returns passthrough */
  1231. }
  1232. break;
  1233. case BLOCK_T:
  1234. if (likely(a->val[0].u.data)){
  1235. ret=run_actions(h, (struct action*)a->val[0].u.data, msg);
  1236. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1237. returns passthrough */
  1238. }
  1239. break;
  1240. case MATCH_COND_T:
  1241. mct=(struct match_cond_table*)a->val[1].u.data;
  1242. rval_cache_init(&c1);
  1243. rv=0;
  1244. rv1=0;
  1245. ret=rval_expr_eval_rvint(h, msg, &rv, &v,
  1246. (struct rval_expr*)a->val[0].u.data, &c1);
  1247. if (unlikely( ret<0)){
  1248. /* handle error in expression => use default */
  1249. ret=-1;
  1250. goto match_cond_def;
  1251. }
  1252. if (h->run_flags & EXIT_R_F){
  1253. ret=0;
  1254. break;
  1255. }
  1256. h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
  1257. in expr */
  1258. if (likely(rv)){
  1259. rv1=rval_convert(h, msg, RV_STR, rv, &c1);
  1260. if (unlikely(rv1==0)){
  1261. ret=-1;
  1262. goto match_cond_def;
  1263. }
  1264. s=rv1->v.s;
  1265. }else{
  1266. /* int result in v */
  1267. rval_cache_clean(&c1);
  1268. s.s=sint2str(v, &s.len);
  1269. }
  1270. ret=1; /* default is continue */
  1271. for(i=0; i<mct->n; i++)
  1272. if (( mct->match[i].type==MATCH_STR &&
  1273. mct->match[i].l.s.len==s.len &&
  1274. memcmp(mct->match[i].l.s.s, s.s, s.len) == 0 ) ||
  1275. ( mct->match[i].type==MATCH_RE &&
  1276. regexec(mct->match[i].l.regex, s.s, 0, 0, 0) == 0)
  1277. ){
  1278. if (likely(mct->jump[i])){
  1279. /* make sure we cleanup first, in case run_actions()
  1280. exits the script directly via longjmp() */
  1281. if (rv1){
  1282. rval_destroy(rv1);
  1283. rval_destroy(rv);
  1284. rval_cache_clean(&c1);
  1285. }else if (rv){
  1286. rval_destroy(rv);
  1287. rval_cache_clean(&c1);
  1288. }
  1289. ret=run_actions(h, mct->jump[i], msg);
  1290. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1291. returns passthrough */
  1292. goto skip;
  1293. }
  1294. goto match_cleanup;
  1295. }
  1296. match_cond_def:
  1297. if (mct->def){
  1298. /* make sure we cleanup first, in case run_actions()
  1299. exits the script directly via longjmp() */
  1300. if (rv1){
  1301. rval_destroy(rv1);
  1302. rval_destroy(rv);
  1303. rval_cache_clean(&c1);
  1304. }else if (rv){
  1305. rval_destroy(rv);
  1306. rval_cache_clean(&c1);
  1307. }
  1308. ret=run_actions(h, mct->def, msg);
  1309. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1310. returns passthrough */
  1311. break;
  1312. }
  1313. match_cleanup:
  1314. if (rv1){
  1315. rval_destroy(rv1);
  1316. rval_destroy(rv);
  1317. rval_cache_clean(&c1);
  1318. }else if (rv){
  1319. rval_destroy(rv);
  1320. rval_cache_clean(&c1);
  1321. }
  1322. break;
  1323. case WHILE_T:
  1324. i=0;
  1325. flags=0;
  1326. rve=(struct rval_expr*)a->val[0].u.data;
  1327. ret=1;
  1328. while(!(flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F)) &&
  1329. (rval_expr_eval_int(h, msg, &v, rve) == 0) && v){
  1330. if (cfg_get(core, core_cfg, max_while_loops) > 0)
  1331. i++;
  1332. if (unlikely(i > cfg_get(core, core_cfg, max_while_loops))){
  1333. LOG(L_ERR, "ERROR: runaway while (%d, %d): more then"
  1334. " %d loops\n",
  1335. rve->fpos.s_line, rve->fpos.s_col,
  1336. cfg_get(core, core_cfg, max_while_loops));
  1337. ret=-1;
  1338. goto error;
  1339. }
  1340. if (likely(a->val[1].u.data)){
  1341. ret=run_actions(h, (struct action*)a->val[1].u.data, msg);
  1342. flags|=h->run_flags;
  1343. h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
  1344. returns pass-through */
  1345. }
  1346. }
  1347. break;
  1348. case FORCE_RPORT_T:
  1349. msg->msg_flags|=FL_FORCE_RPORT;
  1350. ret=1; /* continue processing */
  1351. break;
  1352. case ADD_LOCAL_RPORT_T:
  1353. msg->msg_flags|=FL_ADD_LOCAL_RPORT;
  1354. ret=1; /* continue processing */
  1355. break;
  1356. case UDP_MTU_TRY_PROTO_T:
  1357. msg->msg_flags|= (unsigned int)a->val[0].u.number & FL_MTU_FB_MASK;
  1358. ret=1; /* continue processing */
  1359. break;
  1360. case SET_ADV_ADDR_T:
  1361. if (a->val[0].type!=STR_ST){
  1362. LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
  1363. "type %d\n", a->val[0].type);
  1364. ret=E_BUG;
  1365. goto error;
  1366. }
  1367. msg->set_global_address=*((str*)a->val[0].u.data);
  1368. ret=1; /* continue processing */
  1369. break;
  1370. case SET_ADV_PORT_T:
  1371. if (a->val[0].type!=STR_ST){
  1372. LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
  1373. "type %d\n", a->val[0].type);
  1374. ret=E_BUG;
  1375. goto error;
  1376. }
  1377. msg->set_global_port=*((str*)a->val[0].u.data);
  1378. ret=1; /* continue processing */
  1379. break;
  1380. #ifdef USE_TCP
  1381. case FORCE_TCP_ALIAS_T:
  1382. if ( msg->rcv.proto==PROTO_TCP
  1383. #ifdef USE_TLS
  1384. || msg->rcv.proto==PROTO_TLS
  1385. #endif
  1386. ){
  1387. if (a->val[0].type==NOSUBTYPE) port=msg->via1->port;
  1388. else if (a->val[0].type==NUMBER_ST) port=(int)a->val[0].u.number;
  1389. else{
  1390. LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
  1391. " port type %d\n", a->val[0].type);
  1392. ret=E_BUG;
  1393. goto error;
  1394. }
  1395. if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
  1396. msg->rcv.proto)!=0){
  1397. LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
  1398. ret=E_UNSPEC;
  1399. goto error;
  1400. }
  1401. }
  1402. #endif
  1403. ret=1; /* continue processing */
  1404. break;
  1405. case FORCE_SEND_SOCKET_T:
  1406. if (a->val[0].type!=SOCKETINFO_ST){
  1407. LOG(L_CRIT, "BUG: do_action: bad force_send_socket argument"
  1408. " type: %d\n", a->val[0].type);
  1409. ret=E_BUG;
  1410. goto error;
  1411. }
  1412. set_force_socket(msg, (struct socket_info*)a->val[0].u.data);
  1413. ret=1; /* continue processing */
  1414. break;
  1415. case ADD_T:
  1416. case ASSIGN_T:
  1417. v=lval_assign(h, msg, (struct lvalue*)a->val[0].u.data,
  1418. (struct rval_expr*)a->val[1].u.data);
  1419. if (likely(v>=0))
  1420. ret = 1;
  1421. else if (unlikely (v == EXPR_DROP)) /* hack to quit on DROP*/
  1422. ret=0;
  1423. else
  1424. ret=v;
  1425. break;
  1426. case SET_FWD_NO_CONNECT_T:
  1427. msg->fwd_send_flags.f|= SND_F_FORCE_CON_REUSE;
  1428. ret=1; /* continue processing */
  1429. break;
  1430. case SET_RPL_NO_CONNECT_T:
  1431. msg->rpl_send_flags.f|= SND_F_FORCE_CON_REUSE;
  1432. ret=1; /* continue processing */
  1433. break;
  1434. case SET_FWD_CLOSE_T:
  1435. msg->fwd_send_flags.f|= SND_F_CON_CLOSE;
  1436. ret=1; /* continue processing */
  1437. break;
  1438. case SET_RPL_CLOSE_T:
  1439. msg->rpl_send_flags.f|= SND_F_CON_CLOSE;
  1440. ret=1; /* continue processing */
  1441. break;
  1442. case CFG_SELECT_T:
  1443. if (a->val[0].type != CFG_GROUP_ST) {
  1444. BUG("unsupported parameter in CFG_SELECT_T: %d\n",
  1445. a->val[0].type);
  1446. ret=-1;
  1447. goto error;
  1448. }
  1449. switch(a->val[1].type) {
  1450. case NUMBER_ST:
  1451. v=(int)a->val[1].u.number;
  1452. break;
  1453. case RVE_ST:
  1454. if (rval_expr_eval_int(h, msg, &v, (struct rval_expr*)a->val[1].u.data) < 0) {
  1455. ret=-1;
  1456. goto error;
  1457. }
  1458. break;
  1459. default:
  1460. BUG("unsupported group id type in CFG_SELECT_T: %d\n",
  1461. a->val[1].type);
  1462. ret=-1;
  1463. goto error;
  1464. }
  1465. ret=(cfg_select((cfg_group_t*)a->val[0].u.data, v) == 0) ? 1 : -1;
  1466. break;
  1467. case CFG_RESET_T:
  1468. if (a->val[0].type != CFG_GROUP_ST) {
  1469. BUG("unsupported parameter in CFG_RESET_T: %d\n",
  1470. a->val[0].type);
  1471. ret=-1;
  1472. goto error;
  1473. }
  1474. ret=(cfg_reset((cfg_group_t*)a->val[0].u.data) == 0) ? 1 : -1;
  1475. break;
  1476. /*
  1477. default:
  1478. LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
  1479. */
  1480. }
  1481. skip:
  1482. return ret;
  1483. error_uri:
  1484. LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
  1485. if (new_uri) pkg_free(new_uri);
  1486. LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
  1487. return E_UNSPEC;
  1488. error_fwd_uri:
  1489. /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
  1490. error:
  1491. LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
  1492. return ret;
  1493. }
  1494. /* returns: 0, or 1 on success, <0 on error */
  1495. /* (0 if drop or break encountered, 1 if not ) */
  1496. int run_actions(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
  1497. {
  1498. struct action* t;
  1499. int ret;
  1500. struct sr_module *mod;
  1501. unsigned int ms = 0;
  1502. ret=E_UNSPEC;
  1503. h->rec_lev++;
  1504. if (unlikely(h->rec_lev>ROUTE_MAX_REC_LEV)){
  1505. LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
  1506. " giving up!\n", h->rec_lev);
  1507. ret=E_UNSPEC;
  1508. goto error;
  1509. }
  1510. if (unlikely(h->rec_lev==1)){
  1511. h->run_flags=0;
  1512. h->last_retcode=0;
  1513. _last_returned_code = h->last_retcode;
  1514. #ifdef USE_LONGJMP
  1515. if (unlikely(setjmp(h->jmp_env))){
  1516. h->rec_lev=0;
  1517. ret=h->last_retcode;
  1518. goto end;
  1519. }
  1520. #endif
  1521. }
  1522. if (unlikely(a==0)){
  1523. DBG("DEBUG: run_actions: null action list (rec_level=%d)\n",
  1524. h->rec_lev);
  1525. ret=1;
  1526. }
  1527. for (t=a; t!=0; t=t->next){
  1528. if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0))
  1529. ms = TICKS_TO_MS(get_ticks_raw());
  1530. _cfg_crt_action = t;
  1531. ret=do_action(h, t, msg);
  1532. _cfg_crt_action = 0;
  1533. if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)) {
  1534. ms = TICKS_TO_MS(get_ticks_raw()) - ms;
  1535. if(ms >= cfg_get(core, core_cfg, latency_limit_action)) {
  1536. LOG(cfg_get(core, core_cfg, latency_log),
  1537. "alert - action [%s (%d)]"
  1538. " cfg [%s:%d] took too long [%u ms]\n",
  1539. is_mod_func(t) ?
  1540. ((cmd_export_common_t*)(t->val[0].u.data))->name
  1541. : "corefunc",
  1542. t->type, (t->cfile)?t->cfile:"", t->cline, ms);
  1543. }
  1544. }
  1545. /* break, return or drop/exit stop execution of the current
  1546. block */
  1547. if (unlikely(h->run_flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F))){
  1548. if (unlikely(h->run_flags & EXIT_R_F)) {
  1549. h->last_retcode=ret;
  1550. _last_returned_code = h->last_retcode;
  1551. #ifdef USE_LONGJMP
  1552. longjmp(h->jmp_env, ret);
  1553. #endif
  1554. }
  1555. break;
  1556. }
  1557. /* ignore error returns */
  1558. }
  1559. h->rec_lev--;
  1560. end:
  1561. /* process module onbreak handlers if present */
  1562. if (unlikely(h->rec_lev==0 && ret==0 &&
  1563. !(h->run_flags & IGNORE_ON_BREAK_R_F)))
  1564. for (mod=modules;mod;mod=mod->next)
  1565. if (unlikely(mod->exports.onbreak_f)) {
  1566. mod->exports.onbreak_f( msg );
  1567. }
  1568. return ret;
  1569. error:
  1570. h->rec_lev--;
  1571. return ret;
  1572. }
  1573. #ifdef USE_LONGJMP
  1574. /** safe version of run_actions().
  1575. * It always return (it doesn't longjmp on forced script end).
  1576. * @returns 0, or 1 on success, <0 on error
  1577. * (0 if drop or break encountered, 1 if not ) */
  1578. int run_actions_safe(struct run_act_ctx* h, struct action* a,
  1579. struct sip_msg* msg)
  1580. {
  1581. struct run_act_ctx ctx;
  1582. int ret;
  1583. int ign_on_break;
  1584. /* start with a fresh action context */
  1585. init_run_actions_ctx(&ctx);
  1586. ctx.last_retcode = h->last_retcode;
  1587. ign_on_break = h->run_flags & IGNORE_ON_BREAK_R_F;
  1588. ctx.run_flags = h->run_flags | IGNORE_ON_BREAK_R_F;
  1589. ret = run_actions(&ctx, a, msg);
  1590. h->last_retcode = ctx.last_retcode;
  1591. h->run_flags = (ctx.run_flags & ~IGNORE_ON_BREAK_R_F) | ign_on_break;
  1592. return ret;
  1593. }
  1594. #endif /* USE_LONGJMP */
  1595. int run_top_route(struct action* a, sip_msg_t* msg, struct run_act_ctx *c)
  1596. {
  1597. struct run_act_ctx ctx;
  1598. struct run_act_ctx *p;
  1599. int ret;
  1600. flag_t sfbk;
  1601. p = (c)?c:&ctx;
  1602. sfbk = getsflags();
  1603. setsflagsval(0);
  1604. reset_static_buffer();
  1605. init_run_actions_ctx(p);
  1606. ret = run_actions(p, a, msg);
  1607. setsflagsval(sfbk);
  1608. return ret;
  1609. }