t_reply.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * History:
  29. * --------
  30. * 2003-01-19 faked lump list created in on_reply handlers
  31. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  32. * 2003-02-13 updated to use rb->dst (andrei)
  33. * 2003-02-18 replaced TOTAG_LEN w/ TOTAG_VALUE_LEN (TOTAG_LEN was defined
  34. * twice with different values!) (andrei)
  35. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  36. * 2003-03-01 kr set through a function now (jiri)
  37. * 2003-03-06 saving of to-tags for ACK/200 matching introduced,
  38. * voicemail changes accepted, updated to new callback
  39. * names (jiri)
  40. * 2003-03-10 fixed new to tag bug/typo (if w/o {}) (andrei)
  41. * 2003-03-16 removed _TOTAG (jiri)
  42. * 2003-03-31 200 for INVITE/UAS resent even for UDP (jiri)
  43. * 2003-03-31 removed msg->repl_add_rm (andrei)
  44. * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
  45. * 2003-04-14 local acks generated before reply processing to avoid
  46. * delays in length reply processing (like opening TCP
  47. * connection to an unavailable destination) (jiri)
  48. * 2003-09-11 updates to new build_res_buf_from_sip_req() interface (bogdan)
  49. * 2003-09-11 t_reply_with_body() reshaped to use reply_lumps +
  50. * build_res_buf_from_sip_req() instead of
  51. * build_res_buf_with_body_from_sip_req() (bogdan)
  52. * 2003-11-05 flag context updated from failure/reply handlers back
  53. * to transaction context (jiri)
  54. * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
  55. * 2003-12-04 global TM callbacks switched to per transaction callbacks
  56. * (bogdan)
  57. * 2004-02-06: support for user pref. added - destroy_avps (bogdan)
  58. * 2003-11-05 flag context updated from failure/reply handlers back
  59. * to transaction context (jiri)
  60. * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
  61. * 2004-02-13: t->is_invite and t->local replaced with flags (bogdan)
  62. * 2004-02-18 fifo_t_reply imported from vm module (bogdan)
  63. * 2004-08-23 avp list is available from failure/on_reply routes (bogdan)
  64. * 2004-10-01 added a new param.: restart_fr_on_each_reply (andrei)
  65. * 2005-03-01 force for statefull replies the incoming interface of
  66. * the request (bogdan)
  67. * 2005-09-01 reverted to the old way of checking response.dst.send_sock
  68. * in t_retransmit_reply & reply_light (andrei)
  69. * 2005-11-09 updated to the new timers interface (andrei)
  70. * 2006-02-07 named routes support (andrei)
  71. * 2006-09-13 t_pick_branch will skip also over branches with empty reply
  72. * t_should_relay_response will re-pick the branch if failure
  73. * route /handlers added new branches (andrei)
  74. */
  75. #include "../../comp_defs.h"
  76. #include "../../hash_func.h"
  77. #include "../../dprint.h"
  78. #include "../../config.h"
  79. #include "../../parser/parser_f.h"
  80. #include "../../ut.h"
  81. #include "../../timer.h"
  82. #include "../../error.h"
  83. #include "../../action.h"
  84. #include "../../dset.h"
  85. #include "../../tags.h"
  86. #include "../../data_lump.h"
  87. #include "../../data_lump_rpl.h"
  88. #include "../../usr_avp.h"
  89. #include "defs.h"
  90. #include "h_table.h"
  91. #include "t_hooks.h"
  92. #include "t_funcs.h"
  93. #include "t_reply.h"
  94. #include "t_cancel.h"
  95. #include "t_msgbuilder.h"
  96. #include "t_lookup.h"
  97. #include "t_fwd.h"
  98. #include "fix_lumps.h"
  99. #include "t_stats.h"
  100. #include "uac.h"
  101. /* restart fr timer on each provisional reply, default yes */
  102. int restart_fr_on_each_reply=1;
  103. /* are we processing original or shmemed request ? */
  104. enum route_mode rmode=MODE_REQUEST;
  105. /* private place where we create to-tags for replies */
  106. /* janakj: made public, I need to access this value to store it in dialogs */
  107. char tm_tags[TOTAG_VALUE_LEN];
  108. /* bogdan: pack tm_tag buffer and len into a str to pass them to
  109. * build_res_buf_from_sip_req() */
  110. static str tm_tag = {tm_tags,TOTAG_VALUE_LEN};
  111. char *tm_tag_suffix;
  112. /* where to go if there is no positive reply */
  113. static int goto_on_negative=0;
  114. /* where to go on receipt of reply */
  115. static int goto_on_reply=0;
  116. /* we store the reply_route # in private memory which is
  117. then processed during t_relay; we cannot set this value
  118. before t_relay creates transaction context or after
  119. t_relay when a reply may arrive after we set this
  120. value; that's why we do it how we do it, i.e.,
  121. *inside* t_relay using hints stored in private memory
  122. before t_relay is called
  123. */
  124. void t_on_negative( unsigned int go_to )
  125. {
  126. struct cell *t = get_t();
  127. /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
  128. * in MODE_REQUEST T will be set only if the transaction was already
  129. * created; if not -> use the static variable */
  130. if (!t || t==T_UNDEFINED )
  131. goto_on_negative=go_to;
  132. else
  133. get_t()->on_negative = go_to;
  134. }
  135. void t_on_reply( unsigned int go_to )
  136. {
  137. struct cell *t = get_t();
  138. /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
  139. * in MODE_REQUEST T will be set only if the transaction was already
  140. * created; if not -> use the static variable */
  141. if (!t || t==T_UNDEFINED )
  142. goto_on_reply=go_to;
  143. else
  144. get_t()->on_reply = go_to;
  145. }
  146. unsigned int get_on_negative()
  147. {
  148. return goto_on_negative;
  149. }
  150. unsigned int get_on_reply()
  151. {
  152. return goto_on_reply;
  153. }
  154. void tm_init_tags()
  155. {
  156. init_tags(tm_tags, &tm_tag_suffix,
  157. "SER-TM/tags", TM_TAG_SEPARATOR );
  158. }
  159. /* returns 0 if the message was previously acknowledged
  160. * (i.e., no E2EACK callback is needed) and one if the
  161. * callback shall be executed */
  162. int unmatched_totag(struct cell *t, struct sip_msg *ack)
  163. {
  164. struct totag_elem *i;
  165. str *tag;
  166. if (parse_headers(ack, HDR_TO_F,0)==-1 ||
  167. !ack->to ) {
  168. LOG(L_ERR, "ERROR: unmatched_totag: To invalid\n");
  169. return 1;
  170. }
  171. tag=&get_to(ack)->tag_value;
  172. for (i=t->fwded_totags; i; i=i->next) {
  173. if (i->tag.len==tag->len
  174. && memcmp(i->tag.s, tag->s, tag->len)==0) {
  175. DBG("DEBUG: totag for e2e ACK found: %d\n", i->acked);
  176. /* to-tag recorded, and an ACK has been received for it */
  177. if (i->acked) return 0;
  178. /* to-tag recorded, but this ACK came for the first time */
  179. i->acked=1;
  180. return 1;
  181. }
  182. }
  183. /* surprising: to-tag never sighted before */
  184. return 1;
  185. }
  186. static inline void update_local_tags(struct cell *trans,
  187. struct bookmark *bm, char *dst_buffer,
  188. char *src_buffer /* to which bm refers */)
  189. {
  190. if (bm->to_tag_val.s) {
  191. trans->uas.local_totag.s=bm->to_tag_val.s-src_buffer+dst_buffer;
  192. trans->uas.local_totag.len=bm->to_tag_val.len;
  193. }
  194. }
  195. /* append a newly received tag from a 200/INVITE to
  196. * transaction's set; (only safe if called from within
  197. * a REPLY_LOCK); it returns 1 if such a to tag already
  198. * exists
  199. */
  200. inline static int update_totag_set(struct cell *t, struct sip_msg *ok)
  201. {
  202. struct totag_elem *i, *n;
  203. str *tag;
  204. char *s;
  205. if (!ok->to || !ok->to->parsed) {
  206. LOG(L_ERR, "ERROR: update_totag_set: to not parsed\n");
  207. return 0;
  208. }
  209. tag=&get_to(ok)->tag_value;
  210. if (!tag->s) {
  211. DBG("ERROR: update_totag_set: no tag in to\n");
  212. return 0;
  213. }
  214. for (i=t->fwded_totags; i; i=i->next) {
  215. if (i->tag.len==tag->len
  216. && memcmp(i->tag.s, tag->s, tag->len) ==0 ){
  217. /* to tag already recorded */
  218. #ifdef XL_DEBUG
  219. LOG(L_CRIT, "DEBUG: update_totag_set: totag retransmission\n");
  220. #else
  221. DBG("DEBUG: update_totag_set: totag retransmission\n");
  222. #endif
  223. return 1;
  224. }
  225. }
  226. /* that's a new to-tag -- record it */
  227. shm_lock();
  228. n=(struct totag_elem*) shm_malloc_unsafe(sizeof(struct totag_elem));
  229. s=(char *)shm_malloc_unsafe(tag->len);
  230. shm_unlock();
  231. if (!s || !n) {
  232. LOG(L_ERR, "ERROR: update_totag_set: no memory \n");
  233. if (n) shm_free(n);
  234. if (s) shm_free(s);
  235. return 0;
  236. }
  237. memset(n, 0, sizeof(struct totag_elem));
  238. memcpy(s, tag->s, tag->len );
  239. n->tag.s=s;n->tag.len=tag->len;
  240. n->next=t->fwded_totags;
  241. t->fwded_totags=n;
  242. DBG("DEBUG: update_totag_set: new totag \n");
  243. return 0;
  244. }
  245. /*
  246. * Build an ACK to a negative reply
  247. */
  248. static char *build_ack(struct sip_msg* rpl,struct cell *trans,int branch,
  249. unsigned int *ret_len)
  250. {
  251. str to;
  252. if (parse_headers(rpl,HDR_TO_F, 0)==-1 || !rpl->to ) {
  253. LOG(L_ERR, "ERROR: build_ack: "
  254. "cannot generate a HBH ACK if key HFs in reply missing\n");
  255. return NULL;
  256. }
  257. to.s=rpl->to->name.s;
  258. to.len=rpl->to->len;
  259. return build_local( trans, branch, ret_len,
  260. ACK, ACK_LEN, &to );
  261. }
  262. /*
  263. * The function builds an ACK to 200 OK of local transactions, honor the
  264. * route set, the URI to which the message should be sent will be returned
  265. * in next_hop parameter
  266. */
  267. static char *build_local_ack(struct sip_msg* rpl, struct cell *trans, int branch,
  268. unsigned int *ret_len, str* next_hop)
  269. {
  270. str to;
  271. if (parse_headers(rpl, HDR_EOH_F, 0) == -1 || !rpl->to) {
  272. LOG(L_ERR, "ERROR: build_local_ack: Error while parsing headers\n");
  273. return 0;
  274. }
  275. to.s = rpl->to->name.s;
  276. to.len = rpl->to->len;
  277. return build_dlg_ack(rpl, trans, branch, &to, ret_len, next_hop);
  278. }
  279. /*
  280. * The function is used to send a localy generated ACK to INVITE
  281. * (tm generates the ACK on behalf of application using UAC
  282. */
  283. static int send_local_ack(struct sip_msg* msg, str* next_hop,
  284. char* ack, int ack_len)
  285. {
  286. struct dest_info dst;
  287. #ifdef USE_DNS_FAILOVER
  288. struct dns_srv_handle dns_h;
  289. #endif
  290. if (!next_hop) {
  291. LOG(L_ERR, "send_local_ack: Invalid parameter value\n");
  292. return -1;
  293. }
  294. #ifdef USE_DNS_FAILOVER
  295. if (use_dns_failover){
  296. dns_srv_handle_init(&dns_h);
  297. if ((uri2dst(&dns_h, &dst, msg, next_hop, PROTO_NONE)==0) ||
  298. (dst.send_sock==0)){
  299. dns_srv_handle_put(&dns_h);
  300. LOG(L_ERR, "send_local_ack: no socket found\n");
  301. return -1;
  302. }
  303. dns_srv_handle_put(&dns_h); /* not needed anymore */
  304. }else{
  305. if ((uri2dst(0, &dst, msg, next_hop, PROTO_NONE)==0) ||
  306. (dst.send_sock==0)){
  307. LOG(L_ERR, "send_local_ack: no socket found\n");
  308. return -1;
  309. }
  310. }
  311. #else
  312. if ((uri2dst(&dst, msg, next_hop, PROTO_NONE)==0) || (dst.send_sock==0)){
  313. LOG(L_ERR, "send_local_ack: no socket found\n");
  314. return -1;
  315. }
  316. #endif
  317. return msg_send(&dst, ack, ack_len);
  318. }
  319. static int _reply_light( struct cell *trans, char* buf, unsigned int len,
  320. unsigned int code, char * text,
  321. char *to_tag, unsigned int to_tag_len, int lock,
  322. struct bookmark *bm )
  323. {
  324. struct retr_buf *rb;
  325. unsigned int buf_len;
  326. branch_bm_t cancel_bitmap;
  327. if (!buf)
  328. {
  329. DBG("DEBUG: _reply_light: response building failed\n");
  330. /* determine if there are some branches to be canceled */
  331. if ( is_invite(trans) ) {
  332. if (lock) LOCK_REPLIES( trans );
  333. which_cancel(trans, &cancel_bitmap );
  334. if (lock) UNLOCK_REPLIES( trans );
  335. }
  336. /* and clean-up, including cancellations, if needed */
  337. goto error;
  338. }
  339. cancel_bitmap=0;
  340. if (lock) LOCK_REPLIES( trans );
  341. if ( is_invite(trans) ) which_cancel(trans, &cancel_bitmap );
  342. if (trans->uas.status>=200) {
  343. LOG( L_ERR, "ERROR: _reply_light: can't generate %d reply"
  344. " when a final %d was sent out\n", code, trans->uas.status);
  345. goto error2;
  346. }
  347. rb = & trans->uas.response;
  348. rb->activ_type=code;
  349. trans->uas.status = code;
  350. buf_len = rb->buffer ? len : len + REPLY_OVERBUFFER_LEN;
  351. rb->buffer = (char*)shm_resize( rb->buffer, buf_len );
  352. /* puts the reply's buffer to uas.response */
  353. if (! rb->buffer ) {
  354. LOG(L_ERR, "ERROR: _reply_light: cannot allocate shmem buffer\n");
  355. goto error3;
  356. }
  357. update_local_tags(trans, bm, rb->buffer, buf);
  358. rb->buffer_len = len ;
  359. memcpy( rb->buffer , buf , len );
  360. /* needs to be protected too because what timers are set depends
  361. on current transactions status */
  362. /* t_update_timers_after_sending_reply( rb ); */
  363. update_reply_stats( code );
  364. trans->relayed_reply_branch=-2;
  365. tm_stats->replied_localy++;
  366. if (lock) UNLOCK_REPLIES( trans );
  367. /* do UAC cleanup procedures in case we generated
  368. a final answer whereas there are pending UACs */
  369. if (code>=200) {
  370. if ( is_local(trans) ) {
  371. DBG("DEBUG: local transaction completed from _reply\n");
  372. if ( has_tran_tmcbs(trans, TMCB_LOCAL_COMPLETED) )
  373. run_trans_callbacks( TMCB_LOCAL_COMPLETED, trans,
  374. 0, FAKED_REPLY, code);
  375. } else {
  376. if ( has_tran_tmcbs(trans, TMCB_RESPONSE_OUT) )
  377. run_trans_callbacks( TMCB_RESPONSE_OUT, trans,
  378. trans->uas.request, FAKED_REPLY, code);
  379. }
  380. cleanup_uac_timers( trans );
  381. if (is_invite(trans)) cancel_uacs( trans, cancel_bitmap );
  382. set_final_timer( trans );
  383. }
  384. /* send it out */
  385. /* first check if we managed to resolve topmost Via -- if
  386. not yet, don't try to retransmit
  387. */
  388. /*
  389. response.dst.send_sock might be unset if the process that created
  390. the original transaction has not finished initialising the
  391. retransmission buffer (see t_newtran/ init_rb).
  392. If reply_to_via is set and via contains a host name (and not an ip)
  393. the chances for this increase a lot.
  394. */
  395. if (!trans->uas.response.dst.send_sock) {
  396. LOG(L_ERR, "ERROR: _reply_light: no resolved dst to send reply to\n");
  397. } else {
  398. SEND_PR_BUFFER( rb, buf, len );
  399. DBG("DEBUG: reply sent out. buf=%p: %.9s..., shmem=%p: %.9s\n",
  400. buf, buf, rb->buffer, rb->buffer );
  401. }
  402. pkg_free( buf ) ;
  403. DBG("DEBUG: _reply_light: finished\n");
  404. return 1;
  405. error3:
  406. error2:
  407. if (lock) UNLOCK_REPLIES( trans );
  408. pkg_free ( buf );
  409. error:
  410. /* do UAC cleanup */
  411. cleanup_uac_timers( trans );
  412. if ( is_invite(trans) ) cancel_uacs( trans, cancel_bitmap );
  413. /* we did not succeed -- put the transaction on wait */
  414. put_on_wait(trans);
  415. return -1;
  416. }
  417. /* send a UAS reply
  418. * returns 1 if everything was OK or -1 for error
  419. */
  420. static int _reply( struct cell *trans, struct sip_msg* p_msg,
  421. unsigned int code, char * text, int lock )
  422. {
  423. unsigned int len;
  424. char * buf, *dset;
  425. struct bookmark bm;
  426. int dset_len;
  427. if (code>=200) set_kr(REQ_RPLD);
  428. /* compute the buffer in private memory prior to entering lock;
  429. * create to-tag if needed */
  430. /* if that is a redirection message, dump current message set to it */
  431. if (code>=300 && code<400) {
  432. dset=print_dset(p_msg, &dset_len);
  433. if (dset) {
  434. add_lump_rpl(p_msg, dset, dset_len, LUMP_RPL_HDR);
  435. }
  436. }
  437. if (code>=180 && p_msg->to
  438. && (get_to(p_msg)->tag_value.s==0
  439. || get_to(p_msg)->tag_value.len==0)) {
  440. calc_crc_suffix( p_msg, tm_tag_suffix );
  441. buf = build_res_buf_from_sip_req(code,text, &tm_tag, p_msg, &len, &bm);
  442. return _reply_light( trans, buf, len, code, text,
  443. tm_tag.s, TOTAG_VALUE_LEN, lock, &bm);
  444. } else {
  445. buf = build_res_buf_from_sip_req(code,text, 0 /*no to-tag*/,
  446. p_msg, &len, &bm);
  447. return _reply_light(trans,buf,len,code,text,
  448. 0, 0, /* no to-tag */lock, &bm);
  449. }
  450. }
  451. /*if msg is set -> it will fake the env. vars conforming with the msg; if NULL
  452. * the env. will be restore to original */
  453. static inline void faked_env( struct cell *t,struct sip_msg *msg)
  454. {
  455. static enum route_mode backup_mode;
  456. static struct cell *backup_t;
  457. static unsigned int backup_msgid;
  458. static avp_list_t* backup_user_from, *backup_user_to;
  459. static avp_list_t* backup_domain_from, *backup_domain_to;
  460. static avp_list_t* backup_uri_from, *backup_uri_to;
  461. static struct socket_info* backup_si;
  462. if (msg) {
  463. /* remember we are back in request processing, but process
  464. * a shmem-ed replica of the request; advertise it in rmode;
  465. * for example t_reply needs to know that
  466. */
  467. backup_mode=rmode;
  468. rmode=MODE_ONFAILURE;
  469. /* also, tm actions look in beginning whether transaction is
  470. * set -- whether we are called from a reply-processing
  471. * or a timer process, we need to set current transaction;
  472. * otherwise the actions would attempt to look the transaction
  473. * up (unnecessary overhead, refcounting)
  474. */
  475. /* backup */
  476. backup_t=get_t();
  477. backup_msgid=global_msg_id;
  478. /* fake transaction and message id */
  479. global_msg_id=msg->id;
  480. set_t(t);
  481. /* make available the avp list from transaction */
  482. backup_uri_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI, &t->uri_avps_from );
  483. backup_uri_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI, &t->uri_avps_to );
  484. backup_user_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER, &t->user_avps_from );
  485. backup_user_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER, &t->user_avps_to );
  486. backup_domain_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN, &t->domain_avps_from );
  487. backup_domain_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN, &t->domain_avps_to );
  488. /* set default send address to the saved value */
  489. backup_si=bind_address;
  490. bind_address=t->uac[0].request.dst.send_sock;
  491. } else {
  492. /* restore original environment */
  493. set_t(backup_t);
  494. global_msg_id=backup_msgid;
  495. rmode=backup_mode;
  496. /* restore original avp list */
  497. set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER, backup_user_from );
  498. set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER, backup_user_to );
  499. set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN, backup_domain_from );
  500. set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN, backup_domain_to );
  501. set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI, backup_uri_from );
  502. set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI, backup_uri_to );
  503. bind_address=backup_si;
  504. }
  505. }
  506. static inline int fake_req(struct sip_msg *faked_req,
  507. struct sip_msg *shmem_msg)
  508. {
  509. /* on_negative_reply faked msg now copied from shmem msg (as opposed
  510. * to zero-ing) -- more "read-only" actions (exec in particular) will
  511. * work from reply_route as they will see msg->from, etc.; caution,
  512. * rw actions may append some pkg stuff to msg, which will possibly be
  513. * never released (shmem is released in a single block) */
  514. memcpy( faked_req, shmem_msg, sizeof(struct sip_msg));
  515. /* if we set msg_id to something different from current's message
  516. * id, the first t_fork will properly clean new branch URIs */
  517. faked_req->id=shmem_msg->id-1;
  518. /* msg->parsed_uri_ok must be reset since msg_parsed_uri is
  519. * not cloned (and cannot be cloned) */
  520. faked_req->parsed_uri_ok = 0;
  521. /* new_uri can change -- make a private copy */
  522. if (shmem_msg->new_uri.s!=0 && shmem_msg->new_uri.len!=0) {
  523. faked_req->new_uri.s=pkg_malloc(shmem_msg->new_uri.len+1);
  524. if (!faked_req->new_uri.s) {
  525. LOG(L_ERR, "ERROR: fake_req: no uri/pkg mem\n");
  526. goto error00;
  527. }
  528. faked_req->new_uri.len=shmem_msg->new_uri.len;
  529. memcpy( faked_req->new_uri.s, shmem_msg->new_uri.s,
  530. faked_req->new_uri.len);
  531. faked_req->new_uri.s[faked_req->new_uri.len]=0;
  532. }
  533. /* dst_uri can change ALSO!!! -- make a private copy */
  534. if (shmem_msg->dst_uri.s!=0 && shmem_msg->dst_uri.len!=0) {
  535. faked_req->dst_uri.s=pkg_malloc(shmem_msg->dst_uri.len+1);
  536. if (!faked_req->dst_uri.s) {
  537. LOG(L_ERR, "ERROR: fake_req: no uri/pkg mem\n");
  538. goto error00;
  539. }
  540. faked_req->dst_uri.len=shmem_msg->dst_uri.len;
  541. memcpy( faked_req->dst_uri.s, shmem_msg->dst_uri.s,
  542. faked_req->dst_uri.len);
  543. faked_req->dst_uri.s[faked_req->dst_uri.len]=0;
  544. }
  545. return 1;
  546. error00:
  547. return 0;
  548. }
  549. void inline static free_faked_req(struct sip_msg *faked_req, struct cell *t)
  550. {
  551. struct hdr_field *hdr;
  552. if (faked_req->new_uri.s) {
  553. pkg_free(faked_req->new_uri.s);
  554. faked_req->new_uri.s = 0;
  555. }
  556. if (faked_req->dst_uri.s) {
  557. pkg_free(faked_req->dst_uri.s);
  558. faked_req->dst_uri.s = 0;
  559. }
  560. /* free all types of lump that were added in failure handlers */
  561. del_nonshm_lump( &(faked_req->add_rm) );
  562. del_nonshm_lump( &(faked_req->body_lumps) );
  563. del_nonshm_lump_rpl( &(faked_req->reply_lump) );
  564. /* free header's parsed structures that were added by failure handlers */
  565. for( hdr=faked_req->headers ; hdr ; hdr=hdr->next ) {
  566. if ( hdr->parsed && hdr_allocs_parse(hdr) &&
  567. (hdr->parsed<(void*)t->uas.request ||
  568. hdr->parsed>=(void*)t->uas.end_request)) {
  569. /* header parsed filed doesn't point inside uas.request memory
  570. * chunck -> it was added by failure funcs.-> free it as pkg */
  571. DBG("DBG:free_faked_req: removing hdr->parsed %d\n",
  572. hdr->type);
  573. clean_hdr_field(hdr);
  574. hdr->parsed = 0;
  575. }
  576. }
  577. }
  578. /* return 1 if a failure_route processes */
  579. static inline int run_failure_handlers(struct cell *t, struct sip_msg *rpl,
  580. int code)
  581. {
  582. static struct sip_msg faked_req;
  583. struct sip_msg *shmem_msg = t->uas.request;
  584. int on_failure;
  585. /* failure_route for a local UAC? */
  586. if (!shmem_msg) {
  587. LOG(L_WARN,"Warning: run_failure_handlers: no UAC support (%d, %d) \n",
  588. t->on_negative, t->tmcb_hl.reg_types);
  589. return 0;
  590. }
  591. /* don't start faking anything if we don't have to */
  592. if ( !has_tran_tmcbs( t, TMCB_ON_FAILURE) && !t->on_negative ) {
  593. LOG(L_WARN,
  594. "Warning: run_failure_handlers: no negative handler (%d, %d)\n",
  595. t->on_negative,
  596. t->tmcb_hl.reg_types);
  597. return 1;
  598. }
  599. if (!fake_req(&faked_req, shmem_msg)) {
  600. LOG(L_ERR, "ERROR: run_failure_handlers: fake_req failed\n");
  601. return 0;
  602. }
  603. /* fake also the env. conforming to the fake msg */
  604. faked_env( t, &faked_req);
  605. /* DONE with faking ;-) -> run the failure handlers */
  606. if ( has_tran_tmcbs( t, TMCB_ON_FAILURE) ) {
  607. run_trans_callbacks( TMCB_ON_FAILURE, t, &faked_req, rpl, code);
  608. }
  609. if (t->on_negative) {
  610. /* avoid recursion -- if failure_route forwards, and does not
  611. * set next failure route, failure_route will not be reentered
  612. * on failure */
  613. on_failure = t->on_negative;
  614. t->on_negative=0;
  615. /* run a reply_route action if some was marked */
  616. if (run_actions(failure_rt.rlist[on_failure], &faked_req)<0)
  617. LOG(L_ERR, "ERROR: run_failure_handlers: Error in do_action\n");
  618. }
  619. /* restore original environment and free the fake msg */
  620. faked_env( t, 0);
  621. free_faked_req(&faked_req,t);
  622. /* if failure handler changed flag, update transaction context */
  623. shmem_msg->flags = faked_req.flags;
  624. return 1;
  625. }
  626. /* select a branch for forwarding; returns:
  627. * 0..X ... branch number
  628. * -1 ... error
  629. * -2 ... can't decide yet -- incomplete branches present
  630. */
  631. int t_pick_branch(int inc_branch, int inc_code, struct cell *t, int *res_code)
  632. {
  633. int lowest_b, lowest_s, b;
  634. lowest_b=-1; lowest_s=999;
  635. for ( b=0; b<t->nr_of_outgoings ; b++ ) {
  636. /* "fake" for the currently processed branch */
  637. if (b==inc_branch) {
  638. if (inc_code<lowest_s) {
  639. lowest_b=b;
  640. lowest_s=inc_code;
  641. }
  642. continue;
  643. }
  644. /* skip 'empty branches' */
  645. if (!t->uac[b].request.buffer) continue;
  646. /* there is still an unfinished UAC transaction; wait now! */
  647. if ( t->uac[b].last_received<200 )
  648. return -2;
  649. /* if reply is null => t_send_branch "faked" reply, skip over it */
  650. if ( t->uac[b].last_received<lowest_s && t->uac[b].reply ) {
  651. lowest_b =b;
  652. lowest_s = t->uac[b].last_received;
  653. }
  654. } /* find lowest branch */
  655. *res_code=lowest_s;
  656. return lowest_b;
  657. }
  658. /* This is the neurological point of reply processing -- called
  659. * from within a REPLY_LOCK, t_should_relay_response decides
  660. * how a reply shall be processed and how transaction state is
  661. * affected.
  662. *
  663. * Checks if the new reply (with new_code status) should be sent or not
  664. * based on the current
  665. * transaction status.
  666. * Returns - branch number (0,1,...) which should be relayed
  667. * -1 if nothing to be relayed
  668. */
  669. static enum rps t_should_relay_response( struct cell *Trans , int new_code,
  670. int branch , int *should_store, int *should_relay,
  671. branch_bm_t *cancel_bitmap, struct sip_msg *reply )
  672. {
  673. int branch_cnt;
  674. int picked_branch;
  675. int picked_code;
  676. int new_branch;
  677. int inv_through;
  678. /* note: this code never lets replies to CANCEL go through;
  679. we generate always a local 200 for CANCEL; 200s are
  680. not relayed because it's not an INVITE transaction;
  681. >= 300 are not relayed because 200 was already sent
  682. out
  683. */
  684. DBG("->>>>>>>>> T_code=%d, new_code=%d\n",Trans->uas.status,new_code);
  685. inv_through=new_code>=200 && new_code<300 && is_invite(Trans);
  686. /* if final response sent out, allow only INVITE 2xx */
  687. if ( Trans->uas.status >= 200 ) {
  688. if (inv_through) {
  689. DBG("DBG: t_should_relay_response: 200 INV after final sent\n");
  690. *should_store=0;
  691. Trans->uac[branch].last_received=new_code;
  692. *should_relay=branch;
  693. return RPS_PUSHED_AFTER_COMPLETION;
  694. }
  695. /* except the exception above, too late messages will
  696. be discarded */
  697. goto discard;
  698. }
  699. /* if final response received at this branch, allow only INVITE 2xx */
  700. if (Trans->uac[branch].last_received>=200
  701. && !(inv_through && Trans->uac[branch].last_received<300)) {
  702. /* don't report on retransmissions */
  703. if (Trans->uac[branch].last_received==new_code) {
  704. DBG("DEBUG: final reply retransmission\n");
  705. goto discard;
  706. }
  707. /* if you FR-timed-out, faked a local 408 or 480 and 487 came, don't
  708. * report on it either */
  709. if ((Trans->uac[branch].last_received==408 ||
  710. Trans->uac[branch].last_received==480) && new_code==487) {
  711. DBG("DEBUG: 487 came for a timed-out branch\n");
  712. goto discard;
  713. }
  714. /* this looks however how a very strange status rewrite attempt;
  715. * report on it */
  716. LOG(L_ERR, "ERROR: t_should_relay_response: status rewrite by UAS: "
  717. "stored: %d, received: %d\n",
  718. Trans->uac[branch].last_received, new_code );
  719. goto discard;
  720. }
  721. /* no final response sent yet */
  722. /* negative replies subject to fork picking */
  723. if (new_code >=300 ) {
  724. Trans->uac[branch].last_received=new_code;
  725. /* if all_final return lowest */
  726. picked_branch=t_pick_branch(branch,new_code, Trans, &picked_code);
  727. if (picked_branch==-2) { /* branches open yet */
  728. *should_store=1;
  729. *should_relay=-1;
  730. return RPS_STORE;
  731. }
  732. if (picked_branch==-1) {
  733. LOG(L_CRIT, "ERROR: t_should_relay_response: lowest==-1\n");
  734. goto error;
  735. }
  736. /* no more pending branches -- try if that changes after
  737. a callback; save branch count to be able to determine
  738. later if new branches were initiated */
  739. branch_cnt=Trans->nr_of_outgoings;
  740. /* also append the current reply to the transaction to
  741. * make it available in failure routes - a kind of "fake"
  742. * save of the final reply per branch */
  743. Trans->uac[branch].reply = reply;
  744. /* run ON_FAILURE handlers ( route and callbacks) */
  745. if ( has_tran_tmcbs( Trans, TMCB_ON_FAILURE_RO|TMCB_ON_FAILURE)
  746. || Trans->on_negative ) {
  747. run_failure_handlers( Trans, Trans->uac[picked_branch].reply,
  748. picked_code);
  749. }
  750. /* now reset it; after the failure logic, the reply may
  751. * not be stored any more and we don't want to keep into
  752. * transaction some broken reference */
  753. Trans->uac[branch].reply = 0;
  754. /* look if the callback perhaps replied transaction; it also
  755. covers the case in which a transaction is replied localy
  756. on CANCEL -- then it would make no sense to proceed to
  757. new branches bellow
  758. */
  759. if (Trans->uas.status >= 200) {
  760. *should_store=0;
  761. *should_relay=-1;
  762. /* this might deserve an improvement -- if something
  763. was already replied, it was put on wait and then,
  764. returning RPS_COMPLETED will make t_on_reply
  765. put it on wait again; perhaps splitting put_on_wait
  766. from send_reply or a new RPS_ code would be healthy
  767. */
  768. return RPS_COMPLETED;
  769. }
  770. /* look if the callback/failure_route introduced new branches ... */
  771. if (branch_cnt<Trans->nr_of_outgoings){
  772. /* the new branches might be already "finished" => we
  773. * must use t_pick_branch again */
  774. new_branch=t_pick_branch(branch, new_code, Trans, &picked_code);
  775. if (new_branch<0){
  776. if (new_branch==-2) { /* branches open yet */
  777. *should_store=1;
  778. *should_relay=-1;
  779. return RPS_STORE;
  780. }
  781. /* error, use the old picked_branch */
  782. }else{
  783. /* found a new_branch */
  784. picked_branch=new_branch;
  785. }
  786. }
  787. /* really no more pending branches -- return lowest code */
  788. *should_store=0;
  789. *should_relay=picked_branch;
  790. /* we dont need 'which_cancel' here -- all branches
  791. known to have completed */
  792. /* which_cancel( Trans, cancel_bitmap ); */
  793. return RPS_COMPLETED;
  794. }
  795. /* not >=300 ... it must be 2xx or provisional 1xx */
  796. if (new_code>=100) {
  797. /* 1xx and 2xx except 100 will be relayed */
  798. Trans->uac[branch].last_received=new_code;
  799. *should_store=0;
  800. *should_relay= new_code==100? -1 : branch;
  801. if (new_code>=200 ) {
  802. which_cancel( Trans, cancel_bitmap );
  803. return RPS_COMPLETED;
  804. } else return RPS_PROVISIONAL;
  805. }
  806. error:
  807. /* reply_status didn't match -- it must be something weird */
  808. LOG(L_CRIT, "ERROR: Oh my gooosh! We don't know whether to relay %d\n",
  809. new_code);
  810. discard:
  811. *should_store=0;
  812. *should_relay=-1;
  813. return RPS_DISCARDED;
  814. }
  815. /* Retransmits the last sent inbound reply.
  816. * input: p_msg==request for which I want to retransmit an associated reply
  817. * Returns -1 - error
  818. * 1 - OK
  819. */
  820. int t_retransmit_reply( struct cell *t )
  821. {
  822. static char b[BUF_SIZE];
  823. int len;
  824. /* first check if we managed to resolve topmost Via -- if
  825. not yet, don't try to retransmit
  826. */
  827. /*
  828. response.dst.send_sock might be unset if the process that created
  829. the original transaction has not finished initialising the
  830. retransmission buffer (see t_newtran/ init_rb).
  831. If reply_to_via is set and via contains a host name (and not an ip)
  832. the chances for this increase a lot.
  833. */
  834. if (!t->uas.response.dst.send_sock) {
  835. LOG(L_WARN, "WARNING: t_retransmit_reply: "
  836. "no resolved dst to retransmit\n");
  837. return -1;
  838. }
  839. /* we need to lock the transaction as messages from
  840. upstream may change it continuously
  841. */
  842. LOCK_REPLIES( t );
  843. if (!t->uas.response.buffer) {
  844. DBG("DBG: t_retransmit_reply: nothing to retransmit\n");
  845. goto error;
  846. }
  847. len=t->uas.response.buffer_len;
  848. if ( len==0 || len>BUF_SIZE ) {
  849. DBG("DBG: t_retransmit_reply: "
  850. "zero length or too big to retransmit: %d\n", len);
  851. goto error;
  852. }
  853. memcpy( b, t->uas.response.buffer, len );
  854. UNLOCK_REPLIES( t );
  855. SEND_PR_BUFFER( & t->uas.response, b, len );
  856. DBG("DEBUG: reply retransmitted. buf=%p: %.9s..., shmem=%p: %.9s\n",
  857. b, b, t->uas.response.buffer, t->uas.response.buffer );
  858. return 1;
  859. error:
  860. UNLOCK_REPLIES(t);
  861. return -1;
  862. }
  863. int t_reply( struct cell *t, struct sip_msg* p_msg, unsigned int code,
  864. char * text )
  865. {
  866. return _reply( t, p_msg, code, text, 1 /* lock replies */ );
  867. }
  868. int t_reply_unsafe( struct cell *t, struct sip_msg* p_msg, unsigned int code,
  869. char * text )
  870. {
  871. return _reply( t, p_msg, code, text, 0 /* don't lock replies */ );
  872. }
  873. void set_final_timer( /* struct s_table *h_table, */ struct cell *t )
  874. {
  875. if ( !is_local(t) && t->uas.request->REQ_METHOD==METHOD_INVITE ) {
  876. /* crank timers for negative replies */
  877. if (t->uas.status>=300) {
  878. if (start_retr(&t->uas.response)!=0)
  879. LOG(L_CRIT, "BUG: set_final_timer: start retr failed for %p\n",
  880. &t->uas.response);
  881. return;
  882. }
  883. /* local UAS retransmits too */
  884. if (t->relayed_reply_branch==-2 && t->uas.status>=200) {
  885. /* we retransmit 200/INVs regardless of transport --
  886. even if TCP used, UDP could be used upstream and
  887. loose the 200, which is not retransmitted by proxies
  888. */
  889. if (force_retr( &t->uas.response )!=0)
  890. LOG(L_CRIT, "BUG: set_final_timer: force retr failed for %p\n",
  891. &t->uas.response);
  892. return;
  893. }
  894. }
  895. put_on_wait(t);
  896. }
  897. void cleanup_uac_timers( struct cell *t )
  898. {
  899. int i;
  900. /* reset FR/retransmission timers */
  901. for (i=0; i<t->nr_of_outgoings; i++ ){
  902. stop_rb_timers(&t->uac[i].request);
  903. }
  904. DBG("DEBUG: cleanup_uac_timers: RETR/FR timers reset\n");
  905. }
  906. static int store_reply( struct cell *trans, int branch, struct sip_msg *rpl)
  907. {
  908. # ifdef EXTRA_DEBUG
  909. if (trans->uac[branch].reply) {
  910. LOG(L_ERR, "ERROR: replacing stored reply; aborting\n");
  911. abort();
  912. }
  913. # endif
  914. /* when we later do things such as challenge aggregation,
  915. we should parse the message here before we conserve
  916. it in shared memory; -jiri
  917. */
  918. if (rpl==FAKED_REPLY)
  919. trans->uac[branch].reply=FAKED_REPLY;
  920. else
  921. trans->uac[branch].reply = sip_msg_cloner( rpl, 0 );
  922. if (! trans->uac[branch].reply ) {
  923. LOG(L_ERR, "ERROR: store_reply: can't alloc' clone memory\n");
  924. return 0;
  925. }
  926. return 1;
  927. }
  928. /* this is the code which decides what and when shall be relayed
  929. upstream; note well -- it assumes it is entered locked with
  930. REPLY_LOCK and it returns unlocked!
  931. */
  932. enum rps relay_reply( struct cell *t, struct sip_msg *p_msg, int branch,
  933. unsigned int msg_status, branch_bm_t *cancel_bitmap )
  934. {
  935. int relay;
  936. int save_clone;
  937. char *buf;
  938. /* length of outbound reply */
  939. unsigned int res_len;
  940. int relayed_code;
  941. struct sip_msg *relayed_msg;
  942. struct bookmark bm;
  943. int totag_retr;
  944. enum rps reply_status;
  945. /* retransmission structure of outbound reply and request */
  946. struct retr_buf *uas_rb;
  947. /* keep compiler warnings about use of uninit vars silent */
  948. res_len=0;
  949. buf=0;
  950. relayed_msg=0;
  951. relayed_code=0;
  952. totag_retr=0;
  953. /* remember, what was sent upstream to know whether we are
  954. * forwarding a first final reply or not */
  955. /* *** store and relay message as needed *** */
  956. reply_status = t_should_relay_response(t, msg_status, branch,
  957. &save_clone, &relay, cancel_bitmap, p_msg );
  958. DBG("DEBUG: relay_reply: branch=%d, save=%d, relay=%d\n",
  959. branch, save_clone, relay );
  960. /* store the message if needed */
  961. if (save_clone) /* save for later use, typically branch picking */
  962. {
  963. if (!store_reply( t, branch, p_msg ))
  964. goto error01;
  965. }
  966. uas_rb = & t->uas.response;
  967. if (relay >= 0 ) {
  968. /* initialize sockets for outbound reply */
  969. uas_rb->activ_type=msg_status;
  970. /* only messages known to be relayed immediately will be
  971. * be called on; we do not evoke this callback on messages
  972. * stored in shmem -- they are fixed and one cannot change them
  973. * anyway */
  974. if (msg_status<300 && branch==relay
  975. && has_tran_tmcbs(t,TMCB_RESPONSE_FWDED) ) {
  976. run_trans_callbacks( TMCB_RESPONSE_FWDED, t, t->uas.request,
  977. p_msg, msg_status );
  978. }
  979. /* try building the outbound reply from either the current
  980. * or a stored message */
  981. relayed_msg = branch==relay ? p_msg : t->uac[relay].reply;
  982. if (relayed_msg==FAKED_REPLY) {
  983. tm_stats->replied_localy++;
  984. relayed_code = branch==relay
  985. ? msg_status : t->uac[relay].last_received;
  986. if (relayed_code>=180 && t->uas.request->to
  987. && (get_to(t->uas.request)->tag_value.s==0
  988. || get_to(t->uas.request)->tag_value.len==0)) {
  989. calc_crc_suffix( t->uas.request, tm_tag_suffix );
  990. buf = build_res_buf_from_sip_req(
  991. relayed_code,
  992. error_text(relayed_code),
  993. &tm_tag,
  994. t->uas.request, &res_len, &bm );
  995. } else {
  996. buf = build_res_buf_from_sip_req( relayed_code,
  997. error_text(relayed_code), 0/* no to-tag */,
  998. t->uas.request, &res_len, &bm );
  999. }
  1000. } else {
  1001. relayed_code=relayed_msg->REPLY_STATUS;
  1002. buf = build_res_buf_from_sip_res( relayed_msg, &res_len );
  1003. /* if we build a message from shmem, we need to remove
  1004. via delete lumps which are now stirred in the shmem-ed
  1005. structure
  1006. */
  1007. if (branch!=relay) {
  1008. free_via_clen_lump(&relayed_msg->add_rm);
  1009. }
  1010. }
  1011. update_reply_stats( relayed_code );
  1012. if (!buf) {
  1013. LOG(L_ERR, "ERROR: relay_reply: "
  1014. "no mem for outbound reply buffer\n");
  1015. goto error02;
  1016. }
  1017. /* attempt to copy the message to UAS's shmem:
  1018. - copy to-tag for ACK matching as well
  1019. - allocate little a bit more for provisional as
  1020. larger messages are likely to follow and we will be
  1021. able to reuse the memory frag
  1022. */
  1023. uas_rb->buffer = (char*)shm_resize( uas_rb->buffer, res_len +
  1024. (msg_status<200 ? REPLY_OVERBUFFER_LEN : 0));
  1025. if (!uas_rb->buffer) {
  1026. LOG(L_ERR, "ERROR: relay_reply: cannot alloc reply shmem\n");
  1027. goto error03;
  1028. }
  1029. uas_rb->buffer_len = res_len;
  1030. memcpy( uas_rb->buffer, buf, res_len );
  1031. if (relayed_msg==FAKED_REPLY) { /* to-tags for local replies */
  1032. update_local_tags(t, &bm, uas_rb->buffer, buf);
  1033. }
  1034. tm_stats->replied_localy++;
  1035. /* update the status ... */
  1036. t->uas.status = relayed_code;
  1037. t->relayed_reply_branch = relay;
  1038. if (is_invite(t) && relayed_msg!=FAKED_REPLY
  1039. && relayed_code>=200 && relayed_code < 300
  1040. && has_tran_tmcbs( t, TMCB_RESPONSE_OUT|TMCB_E2EACK_IN) ) {
  1041. totag_retr=update_totag_set(t, relayed_msg);
  1042. }
  1043. }; /* if relay ... */
  1044. UNLOCK_REPLIES( t );
  1045. /* Set retransmission timer before the reply is sent out to avoid
  1046. * race conditions
  1047. */
  1048. if (reply_status == RPS_COMPLETED) {
  1049. set_final_timer(t);
  1050. }
  1051. /* send it now (from the private buffer) */
  1052. if (relay >= 0) {
  1053. SEND_PR_BUFFER( uas_rb, buf, res_len );
  1054. DBG("DEBUG: reply relayed. buf=%p: %.15s..., shmem=%p: %.9s totag_retr=%d\n",
  1055. buf, buf, uas_rb->buffer, uas_rb->buffer, totag_retr );
  1056. if (!totag_retr && has_tran_tmcbs(t, TMCB_RESPONSE_OUT) ) {
  1057. run_trans_callbacks( TMCB_RESPONSE_OUT, t, t->uas.request,
  1058. relayed_msg, relayed_code);
  1059. }
  1060. pkg_free( buf );
  1061. }
  1062. /* success */
  1063. return reply_status;
  1064. error03:
  1065. pkg_free( buf );
  1066. error02:
  1067. if (save_clone) {
  1068. if (t->uac[branch].reply!=FAKED_REPLY)
  1069. sip_msg_free( t->uac[branch].reply );
  1070. t->uac[branch].reply = NULL;
  1071. }
  1072. error01:
  1073. t_reply_unsafe( t, t->uas.request, 500, "Reply processing error" );
  1074. UNLOCK_REPLIES(t);
  1075. if (is_invite(t)) cancel_uacs( t, *cancel_bitmap );
  1076. /* a serious error occurred -- attempt to send an error reply;
  1077. it will take care of clean-ups */
  1078. /* failure */
  1079. return RPS_ERROR;
  1080. }
  1081. /* this is the "UAC" above transaction layer; if a final reply
  1082. is received, it triggers a callback; note well -- it assumes
  1083. it is entered locked with REPLY_LOCK and it returns unlocked!
  1084. */
  1085. enum rps local_reply( struct cell *t, struct sip_msg *p_msg, int branch,
  1086. unsigned int msg_status, branch_bm_t *cancel_bitmap)
  1087. {
  1088. /* how to deal with replies for local transaction */
  1089. int local_store, local_winner;
  1090. enum rps reply_status;
  1091. struct sip_msg *winning_msg;
  1092. int winning_code;
  1093. int totag_retr;
  1094. /* branch_bm_t cancel_bitmap; */
  1095. /* keep warning 'var might be used un-inited' silent */
  1096. winning_msg=0;
  1097. winning_code=0;
  1098. totag_retr=0;
  1099. *cancel_bitmap=0;
  1100. reply_status=t_should_relay_response( t, msg_status, branch,
  1101. &local_store, &local_winner, cancel_bitmap, p_msg );
  1102. DBG("DEBUG: local_reply: branch=%d, save=%d, winner=%d\n",
  1103. branch, local_store, local_winner );
  1104. if (local_store) {
  1105. if (!store_reply(t, branch, p_msg))
  1106. goto error;
  1107. }
  1108. if (local_winner>=0) {
  1109. winning_msg= branch==local_winner
  1110. ? p_msg : t->uac[local_winner].reply;
  1111. if (winning_msg==FAKED_REPLY) {
  1112. tm_stats->replied_localy++;
  1113. winning_code = branch==local_winner
  1114. ? msg_status : t->uac[local_winner].last_received;
  1115. } else {
  1116. winning_code=winning_msg->REPLY_STATUS;
  1117. }
  1118. t->uas.status = winning_code;
  1119. update_reply_stats( winning_code );
  1120. if (is_invite(t) && winning_msg!=FAKED_REPLY
  1121. && winning_code>=200 && winning_code <300
  1122. && has_tran_tmcbs(t,TMCB_RESPONSE_OUT|TMCB_E2EACK_IN) ) {
  1123. totag_retr=update_totag_set(t, winning_msg);
  1124. }
  1125. }
  1126. UNLOCK_REPLIES(t);
  1127. if (local_winner >= 0 && pass_provisional_replies && winning_code < 200) {
  1128. if (!totag_retr && has_tran_tmcbs(t, TMCB_LOCAL_RESPONSE_OUT) ) {
  1129. DBG("DEBUG: Passing provisional reply %d to FIFO application\n", winning_code);
  1130. run_trans_callbacks( TMCB_LOCAL_RESPONSE_OUT, t, 0,
  1131. winning_msg, winning_code);
  1132. }
  1133. }
  1134. if (local_winner>=0 && winning_code>=200 ) {
  1135. DBG("DEBUG: local transaction completed\n");
  1136. if (!totag_retr) {
  1137. if ( has_tran_tmcbs(t,TMCB_LOCAL_COMPLETED) )
  1138. run_trans_callbacks( TMCB_LOCAL_COMPLETED, t, 0,
  1139. winning_msg, winning_code );
  1140. }
  1141. }
  1142. return reply_status;
  1143. error:
  1144. which_cancel(t, cancel_bitmap);
  1145. UNLOCK_REPLIES(t);
  1146. cleanup_uac_timers(t);
  1147. if ( get_cseq(p_msg)->method.len==INVITE_LEN
  1148. && memcmp( get_cseq(p_msg)->method.s, INVITE, INVITE_LEN)==0)
  1149. cancel_uacs( t, *cancel_bitmap );
  1150. put_on_wait(t);
  1151. return RPS_ERROR;
  1152. }
  1153. /* This function is called whenever a reply for our module is received;
  1154. * we need to register this function on module initialization;
  1155. * Returns : 0 - core router stops
  1156. * 1 - core router relay statelessly
  1157. */
  1158. int reply_received( struct sip_msg *p_msg )
  1159. {
  1160. int msg_status;
  1161. int last_uac_status;
  1162. char *ack;
  1163. unsigned int ack_len;
  1164. int branch;
  1165. /* has the transaction completed now and we need to clean-up? */
  1166. int reply_status;
  1167. branch_bm_t cancel_bitmap;
  1168. struct ua_client *uac;
  1169. struct cell *t;
  1170. str next_hop;
  1171. avp_list_t* backup_user_from, *backup_user_to;
  1172. avp_list_t* backup_domain_from, *backup_domain_to;
  1173. avp_list_t* backup_uri_from, *backup_uri_to;
  1174. /* make sure we know the associated transaction ... */
  1175. if (t_check( p_msg , &branch )==-1)
  1176. return 1;
  1177. /*... if there is none, tell the core router to fwd statelessly */
  1178. t=get_t();
  1179. if ( (t==0)||(t==T_UNDEFINED)) return 1;
  1180. cancel_bitmap=0;
  1181. msg_status=p_msg->REPLY_STATUS;
  1182. uac=&t->uac[branch];
  1183. DBG("DEBUG: reply_received: org. status uas=%d, "
  1184. "uac[%d]=%d local=%d is_invite=%d)\n",
  1185. t->uas.status, branch, uac->last_received,
  1186. is_local(t), is_invite(t));
  1187. last_uac_status=uac->last_received;
  1188. /* it's a cancel ... ? */
  1189. if (get_cseq(p_msg)->method.len==CANCEL_LEN
  1190. && memcmp( get_cseq(p_msg)->method.s, CANCEL, CANCEL_LEN)==0
  1191. /* .. which is not e2e ? ... */
  1192. && is_invite(t) ) {
  1193. /* ... then just stop timers */
  1194. if ( msg_status >= 200 )
  1195. stop_rb_timers(&uac->local_cancel); /* stop retr & fr */
  1196. else
  1197. stop_rb_retr(&uac->local_cancel); /* stop only retr */
  1198. DBG("DEBUG: reply to local CANCEL processed\n");
  1199. goto done;
  1200. }
  1201. if ( msg_status >= 200 ){
  1202. /* stop final response timer & retr. only if I got a final response */
  1203. stop_rb_timers(&uac->request);
  1204. /* acknowledge negative INVITE replies (do it before detailed
  1205. * on_reply processing, which may take very long, like if it
  1206. * is attempted to establish a TCP connection to a fail-over dst */
  1207. if (is_invite(t)) {
  1208. if (msg_status >= 300) {
  1209. ack = build_ack(p_msg, t, branch, &ack_len);
  1210. if (ack) {
  1211. SEND_PR_BUFFER(&uac->request, ack, ack_len);
  1212. shm_free(ack);
  1213. }
  1214. } else if (is_local(t) /*&& msg_status >= 200*/) {
  1215. ack = build_local_ack(p_msg, t, branch, &ack_len, &next_hop);
  1216. if (ack) {
  1217. if (send_local_ack(p_msg, &next_hop, ack, ack_len) < 0) {
  1218. LOG(L_ERR, "Error while sending local ACK\n");
  1219. }
  1220. shm_free(ack);
  1221. }
  1222. }
  1223. }
  1224. }else{
  1225. if (is_invite(t)){
  1226. /* stop only retr. (and not fr) */
  1227. stop_rb_retr(&uac->request);
  1228. }else{
  1229. /* non-invite: increase retransmissions interval (slow now) */
  1230. switch_rb_retr_to_t2(&uac->request);
  1231. }
  1232. }
  1233. /* processing of on_reply block */
  1234. if (t->on_reply) {
  1235. rmode=MODE_ONREPLY;
  1236. /* transfer transaction flag to message context */
  1237. if (t->uas.request) p_msg->flags=t->uas.request->flags;
  1238. /* set the as avp_list the one from transaction */
  1239. backup_uri_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI, &t->uri_avps_from );
  1240. backup_uri_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI, &t->uri_avps_to );
  1241. backup_user_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER, &t->user_avps_from );
  1242. backup_user_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER, &t->user_avps_to );
  1243. backup_domain_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN, &t->domain_avps_from );
  1244. backup_domain_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN, &t->domain_avps_to );
  1245. if (run_actions(onreply_rt.rlist[t->on_reply], p_msg)<0)
  1246. LOG(L_ERR, "ERROR: on_reply processing failed\n");
  1247. /* transfer current message context back to t */
  1248. if (t->uas.request) t->uas.request->flags=p_msg->flags;
  1249. /* restore original avp list */
  1250. set_avp_list( AVP_TRACK_FROM | AVP_CLASS_URI, backup_uri_from );
  1251. set_avp_list( AVP_TRACK_TO | AVP_CLASS_URI, backup_uri_to );
  1252. set_avp_list( AVP_TRACK_FROM | AVP_CLASS_USER, backup_user_from );
  1253. set_avp_list( AVP_TRACK_TO | AVP_CLASS_USER, backup_user_to );
  1254. set_avp_list( AVP_TRACK_FROM | AVP_CLASS_DOMAIN, backup_domain_from );
  1255. set_avp_list( AVP_TRACK_TO | AVP_CLASS_DOMAIN, backup_domain_to );
  1256. }
  1257. LOCK_REPLIES( t );
  1258. if ( is_local(t) ) {
  1259. reply_status=local_reply( t, p_msg, branch, msg_status, &cancel_bitmap );
  1260. if (reply_status == RPS_COMPLETED) {
  1261. /* no more UAC FR/RETR (if I received a 2xx, there may
  1262. * be still pending branches ...
  1263. */
  1264. cleanup_uac_timers( t );
  1265. if (is_invite(t)) cancel_uacs( t, cancel_bitmap );
  1266. /* There is no need to call set_final_timer because we know
  1267. >--->--->--- * that the transaction is local */
  1268. put_on_wait(t);
  1269. }
  1270. } else {
  1271. reply_status=relay_reply( t, p_msg, branch, msg_status,
  1272. &cancel_bitmap );
  1273. if (reply_status == RPS_COMPLETED) {
  1274. /* no more UAC FR/RETR (if I received a 2xx, there may
  1275. be still pending branches ...
  1276. */
  1277. cleanup_uac_timers( t );
  1278. if (is_invite(t)) cancel_uacs( t, cancel_bitmap );
  1279. /* FR for negative INVITES, WAIT anything else */
  1280. /* Call to set_final_timer is embedded in relay_reply to avoid
  1281. * race conditions when reply is sent out and an ACK to stop
  1282. * retransmissions comes before retransmission timer is set.*/
  1283. /* set_final_timer(t) */
  1284. }
  1285. }
  1286. if (reply_status==RPS_ERROR)
  1287. goto done;
  1288. /* update FR/RETR timers on provisional replies */
  1289. if (msg_status<200 && ( restart_fr_on_each_reply ||
  1290. ( (last_uac_status<msg_status) &&
  1291. ((msg_status>=180) || (last_uac_status==0)) )
  1292. ) ) { /* provisional now */
  1293. if (is_invite(t)) {
  1294. restart_rb_fr(& uac->request, t->fr_inv_timeout);
  1295. uac->request.flags|=F_RB_FR_INV; /* mark fr_inv */
  1296. }
  1297. } /* provisional replies */
  1298. done:
  1299. /* we are done with the transaction, so unref it - the reference
  1300. * was incremented by t_check() function -bogdan*/
  1301. t_unref(p_msg);
  1302. /* don't try to relay statelessly neither on success
  1303. (we forwarded statefully) nor on error; on troubles,
  1304. simply do nothing; that will make the other party to
  1305. retransmit; hopefuly, we'll then be better off */
  1306. return 0;
  1307. }
  1308. int t_reply_with_body( struct cell *trans, unsigned int code,
  1309. char * text, char * body, char * new_header, char * to_tag )
  1310. {
  1311. struct lump_rpl *hdr_lump;
  1312. struct lump_rpl *body_lump;
  1313. str s_to_tag;
  1314. str rpl;
  1315. int ret;
  1316. struct bookmark bm;
  1317. s_to_tag.s = to_tag;
  1318. if(to_tag)
  1319. s_to_tag.len = strlen(to_tag);
  1320. /* mark the transaction as replied */
  1321. if (code>=200) set_kr(REQ_RPLD);
  1322. /* add the lumps for new_header and for body (by bogdan) */
  1323. if (new_header && strlen(new_header)) {
  1324. hdr_lump = add_lump_rpl( trans->uas.request, new_header,
  1325. strlen(new_header), LUMP_RPL_HDR );
  1326. if ( !hdr_lump ) {
  1327. LOG(L_ERR,"ERROR:tm:t_reply_with_body: cannot add hdr lump\n");
  1328. goto error;
  1329. }
  1330. } else {
  1331. hdr_lump = 0;
  1332. }
  1333. /* body lump */
  1334. if(body && strlen(body)) {
  1335. body_lump = add_lump_rpl( trans->uas.request, body, strlen(body),
  1336. LUMP_RPL_BODY );
  1337. if (body_lump==0) {
  1338. LOG(L_ERR,"ERROR:tm:t_reply_with_body: cannot add body lump\n");
  1339. goto error_1;
  1340. }
  1341. } else {
  1342. body_lump = 0;
  1343. }
  1344. rpl.s = build_res_buf_from_sip_req(
  1345. code, text, &s_to_tag,
  1346. trans->uas.request, (unsigned int*)&rpl.len, &bm);
  1347. /* since the msg (trans->uas.request) is a clone into shm memory, to avoid
  1348. * memory leak or crashing (lumps are create in private memory) I will
  1349. * remove the lumps by myself here (bogdan) */
  1350. if ( hdr_lump ) {
  1351. unlink_lump_rpl( trans->uas.request, hdr_lump);
  1352. free_lump_rpl( hdr_lump );
  1353. }
  1354. if( body_lump ) {
  1355. unlink_lump_rpl( trans->uas.request, body_lump);
  1356. free_lump_rpl( body_lump );
  1357. }
  1358. if (rpl.s==0) {
  1359. LOG(L_ERR,"ERROR:tm:t_reply_with_body: failed in doing "
  1360. "build_res_buf_from_sip_req()\n");
  1361. goto error;
  1362. }
  1363. DBG("t_reply_with_body: buffer computed\n");
  1364. // frees 'res.s' ... no panic !
  1365. ret=_reply_light( trans, rpl.s, rpl.len, code, text,
  1366. s_to_tag.s, s_to_tag.len, 1 /* lock replies */, &bm );
  1367. /* this is ugly hack -- the function caller may wish to continue with
  1368. * transaction and I unref; however, there is now only one use from
  1369. * vm/fifo_vm_reply and I'm currently to lazy to export UNREF; -jiri
  1370. */
  1371. UNREF(trans);
  1372. return ret;
  1373. error_1:
  1374. if ( hdr_lump ) {
  1375. unlink_lump_rpl( trans->uas.request, hdr_lump);
  1376. free_lump_rpl( hdr_lump );
  1377. }
  1378. error:
  1379. return -1;
  1380. }
  1381. #if 0
  1382. static int send_reply(struct cell *trans, unsigned int code, str* text, str* body, str* headers, str* to_tag)
  1383. {
  1384. struct lump_rpl *hdr_lump, *body_lump;
  1385. str rpl;
  1386. int ret;
  1387. struct bookmark bm;
  1388. /* mark the transaction as replied */
  1389. if (code >= 200) set_kr(REQ_RPLD);
  1390. /* add the lumps for new_header and for body (by bogdan) */
  1391. if (headers && headers->len) {
  1392. hdr_lump = add_lump_rpl(trans->uas.request, headers->s, headers->len, LUMP_RPL_HDR);
  1393. if (!hdr_lump) {
  1394. LOG(L_ERR, "send_reply: cannot add hdr lump\n");
  1395. goto sr_error;
  1396. }
  1397. } else {
  1398. hdr_lump = 0;
  1399. }
  1400. /* body lump */
  1401. if (body && body->len) {
  1402. body_lump = add_lump_rpl(trans->uas.request, body->s, body->len, LUMP_RPL_BODY);
  1403. if (body_lump == 0) {
  1404. LOG(L_ERR,"send_reply: cannot add body lump\n");
  1405. goto sr_error_1;
  1406. }
  1407. } else {
  1408. body_lump = 0;
  1409. }
  1410. /* We can safely zero-terminate the text here, because it is followed
  1411. * by next line in the received message
  1412. */
  1413. text->s[text->len] = '\0';
  1414. rpl.s = build_res_buf_from_sip_req(code, text->s, to_tag, trans->uas.request, (unsigned int*)&rpl.len, &bm);
  1415. /* since the msg (trans->uas.request) is a clone into shm memory, to avoid
  1416. * memory leak or crashing (lumps are create in private memory) I will
  1417. * remove the lumps by myself here (bogdan) */
  1418. if (hdr_lump) {
  1419. unlink_lump_rpl(trans->uas.request, hdr_lump);
  1420. free_lump_rpl(hdr_lump);
  1421. }
  1422. if (body_lump) {
  1423. unlink_lump_rpl(trans->uas.request, body_lump);
  1424. free_lump_rpl(body_lump);
  1425. }
  1426. if (rpl.s == 0) {
  1427. LOG(L_ERR,"send_reply: failed in build_res_buf_from_sip_req\n");
  1428. goto sr_error;
  1429. }
  1430. ret = _reply_light(trans, rpl.s, rpl.len, code, text->s, to_tag->s, to_tag->len, 1 /* lock replies */, &bm);
  1431. /* this is ugly hack -- the function caller may wish to continue with
  1432. * transaction and I unref; however, there is now only one use from
  1433. * vm/fifo_vm_reply and I'm currently to lazy to export UNREF; -jiri
  1434. */
  1435. UNREF(trans);
  1436. return ret;
  1437. sr_error_1:
  1438. if (hdr_lump) {
  1439. unlink_lump_rpl(trans->uas.request, hdr_lump);
  1440. free_lump_rpl(hdr_lump);
  1441. }
  1442. sr_error:
  1443. return -1;
  1444. }
  1445. #endif
  1446. const char* rpc_reply_doc[2] = {
  1447. "Reply transaction",
  1448. 0
  1449. };
  1450. /*
  1451. Syntax:
  1452. ":tm.reply:[response file]\n
  1453. code\n
  1454. reason\n
  1455. trans_id\n
  1456. to_tag\n
  1457. [new headers]\n
  1458. \n
  1459. [Body]\n
  1460. .\n
  1461. \n"
  1462. */
  1463. void rpc_reply(rpc_t* rpc, void* c)
  1464. {
  1465. int ret;
  1466. struct cell *trans;
  1467. unsigned int hash_index, label, code;
  1468. str ti;
  1469. char* reason, *body, *headers, *tag;
  1470. if (rpc->scan(c, "d", &code) < 1) {
  1471. rpc->fault(c, 400, "Reply code expected");
  1472. return;
  1473. }
  1474. if (rpc->scan(c, "s", &reason) < 1) {
  1475. rpc->fault(c, 400, "Reason phrase expected");
  1476. return;
  1477. }
  1478. if (rpc->scan(c, "s", &ti.s) < 1) {
  1479. rpc->fault(c, 400, "Transaction ID expected");
  1480. return;
  1481. }
  1482. ti.len = strlen(ti.s);
  1483. if (rpc->scan(c, "s", &tag) < 1) {
  1484. rpc->fault(c, 400, "To tag expected");
  1485. return;
  1486. }
  1487. if (rpc->scan(c, "s", &headers) < 0) return;
  1488. if (rpc->scan(c, "s", &body) < 0) return;
  1489. if(sscanf(ti.s,"%u:%u", &hash_index, &label) != 2) {
  1490. ERR("Invalid trans_id (%s)\n", ti.s);
  1491. rpc->fault(c, 400, "Invalid transaction ID");
  1492. return;
  1493. }
  1494. DBG("hash_index=%u label=%u\n", hash_index, label);
  1495. if( t_lookup_ident(&trans, hash_index, label) < 0 ) {
  1496. ERR("Lookup failed\n");
  1497. rpc->fault(c, 481, "No such transaction");
  1498. return;
  1499. }
  1500. /* it's refcounted now, t_reply_with body unrefs for me -- I can
  1501. * continue but may not use T anymore */
  1502. ret = t_reply_with_body(trans, code, reason, body, headers, tag);
  1503. if (ret < 0) {
  1504. ERR("Reply failed\n");
  1505. rpc->fault(c, 500, "Reply failed");
  1506. return;
  1507. }
  1508. }