t_lookup.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /*
  2. * $Id$
  3. *
  4. * This C-file takes care of matching requests and replies with
  5. * existing transactions. Note that we do not do SIP-compliant
  6. * request matching as asked by SIP spec. We do bitwise matching of
  7. * all header fields in requests which form a transaction key.
  8. * It is much faster and it works pretty well -- we haven't
  9. * had any interop issue neither in lab nor in bake-offs. The reason
  10. * is that retransmissions do look same as original requests
  11. * (it would be really silly if they would be mangled). The only
  12. * exception is we parse To as To in ACK is compared to To in
  13. * reply and both of them are constructed by different software.
  14. *
  15. * As for reply matching, we match based on branch value -- that is
  16. * faster too. There are two versions .. with SYNONYMs #define
  17. * enabled, the branch includes ordinal number of a transaction
  18. * in a synonym list in hash table and is somewhat faster but
  19. * not reboot-resilient. SYNONYMs turned off are little slower
  20. * but work across reboots as well.
  21. *
  22. * The branch parameter is formed as follows:
  23. * SYNONYMS on: hash.synonym.branch
  24. * SYNONYMS off: hash.md5.branch
  25. *
  26. * -jiri
  27. *
  28. *
  29. * Copyright (C) 2001-2003 FhG Fokus
  30. *
  31. * This file is part of ser, a free SIP server.
  32. *
  33. * ser is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2 of the License, or
  36. * (at your option) any later version
  37. *
  38. * For a license to use the ser software under conditions
  39. * other than those described here, or to purchase support for this
  40. * software, please contact iptel.org by e-mail at the following addresses:
  41. * [email protected]
  42. *
  43. * ser is distributed in the hope that it will be useful,
  44. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. * GNU General Public License for more details.
  47. *
  48. * You should have received a copy of the GNU General Public License
  49. * along with this program; if not, write to the Free Software
  50. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  51. *
  52. * History:
  53. * ----------
  54. * 2003-01-23 options for disabling r-uri matching introduced (jiri)
  55. * nameser_compat.h (andrei)
  56. * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
  57. * 2003-01-28 scratchpad removed (jiri)
  58. * 2003-02-13 init_rb() is proto indep. & it uses struct dest_info (andrei)
  59. * 2003-02-24 s/T_NULL/T_NULL_CELL/ to avoid redefinition conflict w/
  60. * 2003-02-27 3261 ACK/200 consumption bug removed (jiri)
  61. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  62. * 2003-03-01 kr set through a function now (jiri)
  63. * 2003-03-06 dialog matching introduced for ACKs -- that's important for
  64. * INVITE UAS (like INVITE) and 200/ACK proxy matching (jiri)
  65. * 2003-03-29 optimization: e2e ACK matching only if callback installed
  66. * (jiri)
  67. * 2003-03-30 set_kr for requests only (jiri)
  68. * 2003-04-04 bug_fix: RESPONSE_IN callback not called for local
  69. * UAC transactions (jiri)
  70. * 2003-04-07 new transactions inherit on_negative and on_relpy from script
  71. * variables on instantiation (jiri)
  72. * 2003-04-30 t_newtran clean up (jiri)
  73. * 2003-08-21 request lookups fixed to skip UAC transactions,
  74. * thanks Ed (jiri)
  75. * 2003-12-04 global TM callbacks switched to per transaction callbacks
  76. * (bogdan)
  77. * 2004-02-11 FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
  78. * 2004-02-13: t->is_invite and t->local replaced with flags (bogdan)
  79. * 2004-10-10: use of mhomed disabled for replies (jiri)
  80. * 2005-02-01: use the incoming request interface for sending the replies
  81. * - changes in init_rb() (bogdan)
  82. * 2005-12-09 added t_set_fr() (andrei)
  83. * 2006-01-27 transaction lookup function will set up a cancel flag
  84. * if the searched transaction was pre-canceled (andrei)
  85. *
  86. */
  87. #include "defs.h"
  88. #include "../../comp_defs.h"
  89. #include "../../dprint.h"
  90. #include "../../config.h"
  91. #include "../../parser/parser_f.h"
  92. #include "../../parser/parse_from.h"
  93. #include "../../ut.h"
  94. #include "../../timer.h"
  95. #include "../../timer_ticks.h"
  96. #include "../../hash_func.h"
  97. #include "../../globals.h"
  98. #include "../../forward.h"
  99. #include "t_funcs.h"
  100. #include "config.h"
  101. #include "sip_msg.h"
  102. #include "t_hooks.h"
  103. #include "t_fwd.h"
  104. #include "t_lookup.h"
  105. #include "dlg.h" /* for t_lookup_callid */
  106. #include "t_msgbuilder.h" /* for t_lookup_callid */
  107. #define EQ_VIA_LEN(_via)\
  108. ( (p_msg->via1->bsize-(p_msg->_via->name.s-(p_msg->_via->hdr.s+p_msg->_via->hdr.len)))==\
  109. (t_msg->via1->bsize-(t_msg->_via->name.s-(t_msg->_via->hdr.s+t_msg->_via->hdr.len))) )
  110. #define EQ_LEN(_hf) (t_msg->_hf->body.len==p_msg->_hf->body.len)
  111. #define EQ_REQ_URI_LEN\
  112. (p_msg->first_line.u.request.uri.len==t_msg->first_line.u.request.uri.len)
  113. #define EQ_STR(_hf) (memcmp(t_msg->_hf->body.s,\
  114. p_msg->_hf->body.s, \
  115. p_msg->_hf->body.len)==0)
  116. #define EQ_REQ_URI_STR\
  117. ( memcmp( t_msg->first_line.u.request.uri.s,\
  118. p_msg->first_line.u.request.uri.s,\
  119. p_msg->first_line.u.request.uri.len)==0)
  120. #define EQ_VIA_STR(_via)\
  121. ( memcmp( t_msg->_via->name.s,\
  122. p_msg->_via->name.s,\
  123. (t_msg->via1->bsize-(t_msg->_via->name.s-(t_msg->_via->hdr.s+t_msg->_via->hdr.len)))\
  124. )==0 )
  125. #define HF_LEN(_hf) ((_hf)->len)
  126. /* should be request-uri matching used as a part of pre-3261
  127. * transaction matching, as the standard wants us to do so
  128. * (and is reasonable to do so, to be able to distinguish
  129. * spirals)? turn only off for better interaction with
  130. * devices that are broken and send different r-uri in
  131. * CANCEL/ACK than in original INVITE
  132. */
  133. int ruri_matching=1;
  134. int via1_matching=1;
  135. /* presumably matching transaction for an e2e ACK */
  136. static struct cell *t_ack;
  137. /* this is a global variable which keeps pointer to
  138. transaction currently processed by a process; it it
  139. set by t_lookup_request or t_reply_matching; don't
  140. dare to change it anywhere else as it would
  141. break ref_counting
  142. */
  143. static struct cell *T;
  144. /* number of currently processed message; good to know
  145. to be able to doublecheck whether we are still working
  146. on a current transaction or a new message arrived;
  147. don't even think of changing it
  148. */
  149. unsigned int global_msg_id;
  150. struct cell *get_t() { return T; }
  151. void set_t(struct cell *t) { T=t; }
  152. void init_t() {global_msg_id=0; set_t(T_UNDEFINED);}
  153. static inline int parse_dlg( struct sip_msg *msg )
  154. {
  155. if (parse_headers(msg, HDR_FROM_F | HDR_CSEQ_F | HDR_TO_F, 0)==-1) {
  156. LOG(L_ERR, "ERROR: parse_dlg: From or Cseq or To invalid\n");
  157. return 0;
  158. }
  159. if ((msg->from==0)||(msg->cseq==0)||(msg->to==0)) {
  160. LOG(L_ERR, "ERROR: parse_dlg: missing From or Cseq or To\n");
  161. return 0;
  162. }
  163. if (parse_from_header(msg)==-1) {
  164. LOG(L_ERR, "ERROR: parse_dlg: From broken\n");
  165. return 0;
  166. }
  167. /* To is automatically parsed through HDR_TO in parse bitmap,
  168. * we don't need to worry about it now
  169. if (parse_to_header(msg)==-1) {
  170. LOG(L_ERR, "ERROR: tid_matching: To broken\n");
  171. return 0;
  172. }
  173. */
  174. return 1;
  175. }
  176. /* is the ACK (p_msg) in p_msg dialog-wise equal to the INVITE (t_msg)
  177. * except to-tags? */
  178. static inline int partial_dlg_matching(struct sip_msg *t_msg, struct sip_msg *p_msg)
  179. {
  180. struct to_body *inv_from;
  181. if (!EQ_LEN(callid)) return 0;
  182. if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
  183. return 0;
  184. inv_from=get_from(t_msg);
  185. if (!inv_from) {
  186. LOG(L_ERR, "ERROR: partial_dlg_matching: INV/From not parsed\n");
  187. return 0;
  188. }
  189. if (inv_from->tag_value.len!=get_from(p_msg)->tag_value.len)
  190. return 0;
  191. if (!EQ_STR(callid))
  192. return 0;
  193. if (memcmp(get_cseq(t_msg)->number.s, get_cseq(p_msg)->number.s,
  194. get_cseq(p_msg)->number.len)!=0)
  195. return 0;
  196. if (memcmp(inv_from->tag_value.s, get_from(p_msg)->tag_value.s,
  197. get_from(p_msg)->tag_value.len)!=0)
  198. return 0;
  199. return 1;
  200. }
  201. /* are to-tags in ACK/200 same as those we sent out? */
  202. static inline int dlg_matching(struct cell *p_cell, struct sip_msg *ack )
  203. {
  204. if (get_to(ack)->tag_value.len!=p_cell->uas.local_totag.len)
  205. return 0;
  206. if (memcmp(get_to(ack)->tag_value.s,p_cell->uas.local_totag.s,
  207. p_cell->uas.local_totag.len)!=0)
  208. return 0;
  209. return 1;
  210. }
  211. static inline int ack_matching(struct cell *p_cell, struct sip_msg *p_msg)
  212. {
  213. /* partial dialog matching -- no to-tag, only from-tag,
  214. * callid, cseq number ; */
  215. if (!partial_dlg_matching(p_cell->uas.request, p_msg))
  216. return 0;
  217. /* if this transaction is proxied (as opposed to UAS) we're
  218. * done now -- we ignore to-tags; the ACK simply belongs to
  219. * this UAS part of dialog, whatever to-tag it gained
  220. */
  221. if (p_cell->relayed_reply_branch!=-2) {
  222. return 2; /* e2e proxied ACK */
  223. }
  224. /* it's a local dialog -- we wish to verify to-tags too */
  225. if (dlg_matching(p_cell, p_msg)) {
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. /* branch-based transaction matching */
  231. static inline int via_matching( struct via_body *inv_via,
  232. struct via_body *ack_via )
  233. {
  234. if (inv_via->tid.len!=ack_via->tid.len)
  235. return 0;
  236. if (memcmp(inv_via->tid.s, ack_via->tid.s,
  237. ack_via->tid.len)!=0)
  238. return 0;
  239. /* ok, tid matches -- now make sure that the
  240. * originator matches too to avoid confusion with
  241. * different senders generating the same tid
  242. */
  243. if (inv_via->host.len!=ack_via->host.len)
  244. return 0;;
  245. if (memcmp(inv_via->host.s, ack_via->host.s,
  246. ack_via->host.len)!=0)
  247. return 0;
  248. if (inv_via->port!=ack_via->port)
  249. return 0;
  250. if (inv_via->transport.len!=ack_via->transport.len)
  251. return 0;
  252. if (memcmp(inv_via->transport.s, ack_via->transport.s,
  253. ack_via->transport.len)!=0)
  254. return 0;
  255. /* everything matched -- we found it */
  256. return 1;
  257. }
  258. /* transaction matching a-la RFC-3261 using transaction ID in branch
  259. (the function assumes there is magic cookie in branch)
  260. It returns:
  261. 2 if e2e ACK for a proxied transaction found
  262. 1 if found (covers ACK for local UAS)
  263. 0 if not found (trans undefined)
  264. It also sets *cancel if a cancel was found for the searched transaction
  265. */
  266. static int matching_3261( struct sip_msg *p_msg, struct cell **trans,
  267. enum request_method skip_method, int* cancel)
  268. {
  269. struct cell *p_cell;
  270. struct sip_msg *t_msg;
  271. struct via_body *via1;
  272. int is_ack;
  273. int dlg_parsed;
  274. int ret = 0;
  275. struct cell *e2e_ack_trans;
  276. *cancel=0;
  277. e2e_ack_trans=0;
  278. via1=p_msg->via1;
  279. is_ack=p_msg->REQ_METHOD==METHOD_ACK;
  280. dlg_parsed=0;
  281. /* update parsed tid */
  282. via1->tid.s=via1->branch->value.s+MCOOKIE_LEN;
  283. via1->tid.len=via1->branch->value.len-MCOOKIE_LEN;
  284. for ( p_cell = get_tm_table()->entrys[p_msg->hash_index].first_cell;
  285. p_cell; p_cell = p_cell->next_cell )
  286. {
  287. t_msg=p_cell->uas.request;
  288. if (!t_msg) continue; /* don't try matching UAC transactions */
  289. /* we want to set *cancel for transaction for which there is
  290. * already a canceled transaction (e.g. re-ordered INV-CANCEL, or
  291. * INV blocked in dns lookup); we don't care about ACKs */
  292. if ((is_ack || (t_msg->REQ_METHOD!=METHOD_CANCEL)) &&
  293. (skip_method & t_msg->REQ_METHOD))
  294. continue;
  295. /* here we do an exercise which will be removed from future code
  296. versions: we try to match end-2-end ACKs if they appear at our
  297. server. This allows some applications bound to TM via callbacks
  298. to correlate the e2e ACKs with transaction context, e.g., for
  299. purpose of accounting. We think it is a bad place here, among
  300. other things because it is not reliable. If a transaction loops
  301. via SER the ACK can't be matched to proper INVITE transaction
  302. (it is a separate transactino with its own branch ID) and it
  303. matches all transaction instances in the loop dialog-wise.
  304. Eventually, regardless to which transaction in the loop the
  305. ACK belongs, only the first one will match.
  306. */
  307. /* dialog matching needs to be applied for ACK/200s */
  308. if (is_ack && p_cell->uas.status<300 && e2e_ack_trans==0) {
  309. /* make sure we have parsed all things we need for dialog
  310. * matching */
  311. if (!dlg_parsed) {
  312. dlg_parsed=1;
  313. if (!parse_dlg(p_msg)) {
  314. LOG(L_ERR, "ERROR: matching_3261: dlg parsing failed\n");
  315. return 0;
  316. }
  317. }
  318. ret=ack_matching(p_cell /* t w/invite */, p_msg /* ack */);
  319. if (ret>0) {
  320. e2e_ack_trans=p_cell;
  321. break;
  322. }
  323. /* this ACK is neither local "negative" one, nor a proxied
  324. * end-2-end one, nor an end-2-end one for a UAS transaction
  325. * -- we failed to match */
  326. continue;
  327. }
  328. /* now real tid matching occurs for negative ACKs and any
  329. * other requests */
  330. if (!via_matching(t_msg->via1 /* inv via */, via1 /* ack */ ))
  331. continue;
  332. if (t_msg->REQ_METHOD==METHOD_CANCEL){
  333. if ((p_msg->REQ_METHOD!=METHOD_CANCEL) && !is_ack){
  334. /* found an existing cancel for the searched transaction */
  335. *cancel=1;
  336. }
  337. if (skip_method & t_msg->REQ_METHOD) continue;
  338. }
  339. /* all matched -- we found the transaction ! */
  340. DBG("DEBUG: RFC3261 transaction matched, tid=%.*s\n",
  341. via1->tid.len, via1->tid.s);
  342. *trans=p_cell;
  343. return 1;
  344. }
  345. /* :-( ... we didn't find any */
  346. /* just check if it we found an e2e ACK previously */
  347. if (e2e_ack_trans) {
  348. *trans=e2e_ack_trans;
  349. return ret;
  350. }
  351. DBG("DEBUG: RFC3261 transaction matching failed\n");
  352. return 0;
  353. }
  354. /* function returns:
  355. * negative - transaction wasn't found
  356. * (-2 = possibly e2e ACK matched )
  357. * positive - transaction found
  358. * It also sets *cancel if a there is already a cancel transaction
  359. */
  360. int t_lookup_request( struct sip_msg* p_msg , int leave_new_locked,
  361. int* cancel)
  362. {
  363. struct cell *p_cell;
  364. unsigned int isACK;
  365. struct sip_msg *t_msg;
  366. int ret;
  367. struct via_param *branch;
  368. int match_status;
  369. struct cell *e2e_ack_trans;
  370. /* parse all*/
  371. if (check_transaction_quadruple(p_msg)==0)
  372. {
  373. LOG(L_ERR, "ERROR: TM module: t_lookup_request: too few headers\n");
  374. set_t(0);
  375. /* stop processing */
  376. return 0;
  377. }
  378. /* start searching into the table */
  379. if (!p_msg->hash_index)
  380. p_msg->hash_index=hash( p_msg->callid->body , get_cseq(p_msg)->number ) ;
  381. isACK = p_msg->REQ_METHOD==METHOD_ACK;
  382. DBG("t_lookup_request: start searching: hash=%d, isACK=%d\n",
  383. p_msg->hash_index,isACK);
  384. /* assume not found */
  385. ret=-1;
  386. e2e_ack_trans = 0;
  387. /* first of all, look if there is RFC3261 magic cookie in branch; if
  388. * so, we can do very quick matching and skip the old-RFC bizzar
  389. * comparison of many header fields
  390. */
  391. if (!p_msg->via1) {
  392. LOG(L_ERR, "ERROR: t_lookup_request: no via\n");
  393. set_t(0);
  394. return 0;
  395. }
  396. branch=p_msg->via1->branch;
  397. if (branch && branch->value.s && branch->value.len>MCOOKIE_LEN
  398. && memcmp(branch->value.s,MCOOKIE,MCOOKIE_LEN)==0) {
  399. /* huhuhu! the cookie is there -- let's proceed fast */
  400. LOCK_HASH(p_msg->hash_index);
  401. match_status=matching_3261(p_msg,&p_cell,
  402. /* skip transactions with different method; otherwise CANCEL
  403. * would match the previous INVITE trans. */
  404. isACK ? ~METHOD_INVITE: ~p_msg->REQ_METHOD,
  405. cancel);
  406. switch(match_status) {
  407. case 0: goto notfound; /* no match */
  408. case 1: goto found; /* match */
  409. case 2: goto e2e_ack; /* e2e proxy ACK */
  410. }
  411. }
  412. /* ok -- it's ugly old-fashioned transaction matching -- it is
  413. * a bit simplified to be fast -- we don't do all the comparisons
  414. * of parsed uri, which was simply too bloated */
  415. DBG("DEBUG: proceeding to pre-RFC3261 transaction matching\n");
  416. *cancel=0;
  417. /* lock the whole entry*/
  418. LOCK_HASH(p_msg->hash_index);
  419. /* all the transactions from the entry are compared */
  420. for ( p_cell = get_tm_table()->entrys[p_msg->hash_index].first_cell;
  421. p_cell; p_cell = p_cell->next_cell )
  422. {
  423. t_msg = p_cell->uas.request;
  424. if (!t_msg) continue; /* skip UAC transactions */
  425. if (!isACK) {
  426. /* for non-ACKs we want same method matching, we
  427. * make an exception for pre-exisiting CANCELs because we
  428. * want to set *cancel */
  429. if ((t_msg->REQ_METHOD!=p_msg->REQ_METHOD) &&
  430. (t_msg->REQ_METHOD!=METHOD_CANCEL))
  431. continue;
  432. /* compare lengths first */
  433. if (!EQ_LEN(callid)) continue;
  434. /* CSeq only the number without method ! */
  435. if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
  436. continue;
  437. if (!EQ_LEN(from)) continue;
  438. if (!EQ_LEN(to)) continue;
  439. if (ruri_matching && !EQ_REQ_URI_LEN) continue;
  440. if (via1_matching && !EQ_VIA_LEN(via1)) continue;
  441. /* length ok -- move on */
  442. if (!EQ_STR(callid)) continue;
  443. if (memcmp(get_cseq(t_msg)->number.s, get_cseq(p_msg)->number.s,
  444. get_cseq(p_msg)->number.len)!=0) continue;
  445. if (!EQ_STR(from)) continue;
  446. if (!EQ_STR(to)) continue;
  447. if (ruri_matching && !EQ_REQ_URI_STR) continue;
  448. if (via1_matching && !EQ_VIA_STR(via1)) continue;
  449. if ((t_msg->REQ_METHOD==METHOD_CANCEL) &&
  450. (p_msg->REQ_METHOD!=METHOD_CANCEL)){
  451. /* we've matched an existing CANCEL */
  452. *cancel=1;
  453. continue;
  454. }
  455. /* request matched ! */
  456. DBG("DEBUG: non-ACK matched\n");
  457. goto found;
  458. } else { /* it's an ACK request*/
  459. /* ACK's relate only to INVITEs */
  460. if (t_msg->REQ_METHOD!=METHOD_INVITE) continue;
  461. /* From|To URI , CallID, CSeq # must be always there */
  462. /* compare lengths now */
  463. if (!EQ_LEN(callid)) continue;
  464. /* CSeq only the number without method ! */
  465. if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
  466. continue;
  467. if (! EQ_LEN(from)) continue;
  468. /* To only the uri -- to many UACs screw up tags */
  469. if (get_to(t_msg)->uri.len!=get_to(p_msg)->uri.len)
  470. continue;
  471. if (!EQ_STR(callid)) continue;
  472. if (memcmp(get_cseq(t_msg)->number.s, get_cseq(p_msg)->number.s,
  473. get_cseq(p_msg)->number.len)!=0) continue;
  474. if (!EQ_STR(from)) continue;
  475. if (memcmp(get_to(t_msg)->uri.s, get_to(p_msg)->uri.s,
  476. get_to(t_msg)->uri.len)!=0) continue;
  477. /* it is e2e ACK/200 */
  478. if (p_cell->uas.status<300 && e2e_ack_trans==0) {
  479. /* all criteria for proxied ACK are ok */
  480. if (p_cell->relayed_reply_branch!=-2) {
  481. e2e_ack_trans=p_cell;
  482. continue;
  483. }
  484. /* it's a local UAS transaction */
  485. if (dlg_matching(p_cell, p_msg))
  486. goto found;
  487. continue;
  488. }
  489. /* it is not an e2e ACK/200 -- perhaps it is
  490. * local negative case; in which case we will want
  491. * more elements to match: r-uri and via; allow
  492. * mismatching r-uri as an config option for broken
  493. * UACs */
  494. if (ruri_matching && !EQ_REQ_URI_LEN ) continue;
  495. if (via1_matching && !EQ_VIA_LEN(via1)) continue;
  496. if (ruri_matching && !EQ_REQ_URI_STR) continue;
  497. if (via1_matching && !EQ_VIA_STR(via1)) continue;
  498. /* wow -- we survived all the check! we matched! */
  499. DBG("DEBUG: non-2xx ACK matched\n");
  500. goto found;
  501. } /* ACK */
  502. } /* synonym loop */
  503. notfound:
  504. if (e2e_ack_trans) {
  505. p_cell=e2e_ack_trans;
  506. goto e2e_ack;
  507. }
  508. /* no transaction found */
  509. set_t(0);
  510. if (!leave_new_locked) {
  511. UNLOCK_HASH(p_msg->hash_index);
  512. }
  513. DBG("DEBUG: t_lookup_request: no transaction found\n");
  514. return -1;
  515. e2e_ack:
  516. t_ack=p_cell; /* e2e proxied ACK */
  517. set_t(0);
  518. if (!leave_new_locked) {
  519. UNLOCK_HASH(p_msg->hash_index);
  520. }
  521. DBG("DEBUG: t_lookup_request: e2e proxy ACK found\n");
  522. return -2;
  523. found:
  524. set_t(p_cell);
  525. REF_UNSAFE( T );
  526. set_kr(REQ_EXIST);
  527. UNLOCK_HASH( p_msg->hash_index );
  528. DBG("DEBUG: t_lookup_request: transaction found (T=%p)\n",T);
  529. return 1;
  530. }
  531. /* function lookups transaction being canceled by CANCEL in p_msg;
  532. * it returns:
  533. * 0 - transaction wasn't found
  534. * T - transaction found
  535. */
  536. struct cell* t_lookupOriginalT( struct sip_msg* p_msg )
  537. {
  538. struct cell *p_cell;
  539. unsigned int hash_index;
  540. struct sip_msg *t_msg;
  541. struct via_param *branch;
  542. int foo;
  543. int ret;
  544. /* start searching in the table */
  545. hash_index = p_msg->hash_index;
  546. DBG("DEBUG: t_lookupOriginalT: searching on hash entry %d\n",hash_index );
  547. /* first of all, look if there is RFC3261 magic cookie in branch; if
  548. * so, we can do very quick matching and skip the old-RFC bizzar
  549. * comparison of many header fields
  550. */
  551. if (!p_msg->via1) {
  552. LOG(L_ERR, "ERROR: t_lookupOriginalT: no via\n");
  553. set_t(0);
  554. return 0;
  555. }
  556. branch=p_msg->via1->branch;
  557. if (branch && branch->value.s && branch->value.len>MCOOKIE_LEN
  558. && memcmp(branch->value.s,MCOOKIE,MCOOKIE_LEN)==0) {
  559. /* huhuhu! the cookie is there -- let's proceed fast */
  560. LOCK_HASH(hash_index);
  561. ret=matching_3261(p_msg, &p_cell,
  562. /* we are seeking the original transaction --
  563. * skip CANCEL transactions during search
  564. */
  565. METHOD_CANCEL, &foo);
  566. if (ret==1) goto found; else goto notfound;
  567. }
  568. /* no cookies --proceed to old-fashioned pre-3261 t-matching */
  569. LOCK_HASH(hash_index);
  570. /* all the transactions from the entry are compared */
  571. for (p_cell=get_tm_table()->entrys[hash_index].first_cell;
  572. p_cell; p_cell = p_cell->next_cell )
  573. {
  574. t_msg = p_cell->uas.request;
  575. if (!t_msg) continue; /* skip UAC transactions */
  576. /* we don't cancel CANCELs ;-) */
  577. if (t_msg->REQ_METHOD==METHOD_CANCEL)
  578. continue;
  579. /* check lengths now */
  580. if (!EQ_LEN(callid))
  581. continue;
  582. if (get_cseq(t_msg)->number.len!=get_cseq(p_msg)->number.len)
  583. continue;
  584. if (!EQ_LEN(from))
  585. continue;
  586. #ifdef CANCEL_TAG
  587. if (!EQ_LEN(to))
  588. continue;
  589. #else
  590. /* relaxed matching -- we don't care about to-tags anymore,
  591. * many broken UACs screw them up and ignoring them does not
  592. * actually hurt
  593. */
  594. if (get_to(t_msg)->uri.len!=get_to(p_msg)->uri.len)
  595. continue;
  596. #endif
  597. if (ruri_matching && !EQ_REQ_URI_LEN)
  598. continue;
  599. if (via1_matching && !EQ_VIA_LEN(via1))
  600. continue;
  601. /* check the content now */
  602. if (!EQ_STR(callid))
  603. continue;
  604. if (memcmp(get_cseq(t_msg)->number.s,
  605. get_cseq(p_msg)->number.s,get_cseq(p_msg)->number.len)!=0)
  606. continue;
  607. if (!EQ_STR(from))
  608. continue;
  609. #ifdef CANCEL_TAG
  610. if (!EQ_STR(to))
  611. continue;
  612. #else
  613. if (memcmp(get_to(t_msg)->uri.s, get_to(p_msg)->uri.s,
  614. get_to(t_msg)->uri.len)!=0)
  615. continue;
  616. #endif
  617. if (ruri_matching && !EQ_REQ_URI_STR)
  618. continue;
  619. if (via1_matching && !EQ_VIA_STR(via1))
  620. continue;
  621. /* found */
  622. goto found;
  623. }
  624. notfound:
  625. /* no transaction found */
  626. DBG("DEBUG: t_lookupOriginalT: no CANCEL matching found! \n" );
  627. UNLOCK_HASH(hash_index);
  628. DBG("DEBUG: t_lookupOriginalT completed\n");
  629. return 0;
  630. found:
  631. DBG("DEBUG: t_lookupOriginalT: canceled transaction"
  632. " found (%p)! \n",p_cell );
  633. REF_UNSAFE( p_cell );
  634. UNLOCK_HASH(hash_index);
  635. DBG("DEBUG: t_lookupOriginalT completed\n");
  636. return p_cell;
  637. }
  638. /* Returns 0 - nothing found
  639. * 1 - T found
  640. */
  641. int t_reply_matching( struct sip_msg *p_msg , int *p_branch )
  642. {
  643. struct cell* p_cell;
  644. unsigned int hash_index = 0;
  645. unsigned int entry_label = 0;
  646. unsigned int branch_id = 0;
  647. char *hashi, *branchi, *p, *n;
  648. int hashl, branchl;
  649. int scan_space;
  650. str cseq_method;
  651. str req_method;
  652. char *loopi;
  653. int loopl;
  654. char *syni;
  655. int synl;
  656. short is_cancel;
  657. /* make compiler warnings happy */
  658. loopi=0;
  659. loopl=0;
  660. syni=0;
  661. synl=0;
  662. /* split the branch into pieces: loop_detection_check(ignored),
  663. hash_table_id, synonym_id, branch_id */
  664. if (!(p_msg->via1 && p_msg->via1->branch && p_msg->via1->branch->value.s))
  665. goto nomatch2;
  666. /* we do RFC 3261 tid matching and want to see first if there is
  667. * magic cookie in branch */
  668. if (p_msg->via1->branch->value.len<=MCOOKIE_LEN)
  669. goto nomatch2;
  670. if (memcmp(p_msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)!=0)
  671. goto nomatch2;
  672. p=p_msg->via1->branch->value.s+MCOOKIE_LEN;
  673. scan_space=p_msg->via1->branch->value.len-MCOOKIE_LEN;
  674. /* hash_id */
  675. n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR);
  676. hashl=n-p;
  677. scan_space-=hashl;
  678. if (!hashl || scan_space<2 || *n!=BRANCH_SEPARATOR) goto nomatch2;
  679. hashi=p;
  680. p=n+1;scan_space--;
  681. if (!syn_branch) {
  682. /* md5 value */
  683. n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR );
  684. loopl = n-p;
  685. scan_space-= loopl;
  686. if (n==p || scan_space<2 || *n!=BRANCH_SEPARATOR)
  687. goto nomatch2;
  688. loopi=p;
  689. p=n+1; scan_space--;
  690. } else {
  691. /* synonym id */
  692. n=eat_token2_end( p, p+scan_space, BRANCH_SEPARATOR);
  693. synl=n-p;
  694. scan_space-=synl;
  695. if (!synl || scan_space<2 || *n!=BRANCH_SEPARATOR)
  696. goto nomatch2;
  697. syni=p;
  698. p=n+1;scan_space--;
  699. }
  700. /* branch id - should exceed the scan_space */
  701. n=eat_token_end( p, p+scan_space );
  702. branchl=n-p;
  703. if (!branchl ) goto nomatch2;
  704. branchi=p;
  705. /* sanity check */
  706. if (reverse_hex2int(hashi, hashl, &hash_index)<0
  707. ||hash_index>=TABLE_ENTRIES
  708. || reverse_hex2int(branchi, branchl, &branch_id)<0
  709. ||branch_id>=MAX_BRANCHES
  710. || (syn_branch ? (reverse_hex2int(syni, synl, &entry_label))<0
  711. : loopl!=MD5_LEN )
  712. ) {
  713. DBG("DEBUG: t_reply_matching: poor reply labels %d label %d "
  714. "branch %d\n", hash_index, entry_label, branch_id );
  715. goto nomatch2;
  716. }
  717. DBG("DEBUG: t_reply_matching: hash %d label %d branch %d\n",
  718. hash_index, entry_label, branch_id );
  719. /* search the hash table list at entry 'hash_index'; lock the
  720. entry first
  721. */
  722. cseq_method=get_cseq(p_msg)->method;
  723. is_cancel=cseq_method.len==CANCEL_LEN
  724. && memcmp(cseq_method.s, CANCEL, CANCEL_LEN)==0;
  725. LOCK_HASH(hash_index);
  726. for (p_cell = get_tm_table()->entrys[hash_index].first_cell; p_cell;
  727. p_cell=p_cell->next_cell) {
  728. /* first look if branch matches */
  729. if (syn_branch) {
  730. if (p_cell->label != entry_label)
  731. continue;
  732. } else {
  733. if ( memcmp(p_cell->md5, loopi,MD5_LEN)!=0)
  734. continue;
  735. }
  736. /* sanity check ... too high branch ? */
  737. if ( branch_id>=p_cell->nr_of_outgoings )
  738. continue;
  739. /* does method match ? (remember -- CANCELs have the same branch
  740. as canceled transactions) */
  741. req_method=p_cell->method;
  742. if ( /* method match */
  743. ! ((cseq_method.len==req_method.len
  744. && memcmp( cseq_method.s, req_method.s, cseq_method.len )==0)
  745. /* or it is a local cancel */
  746. || (is_cancel && is_invite(p_cell)
  747. /* commented out -- should_cancel_branch set it to
  748. BUSY_BUFFER to avoid collisions with replies;
  749. thus, we test here by buffer size
  750. */
  751. /* && p_cell->uac[branch_id].local_cancel.buffer ))) */
  752. && p_cell->uac[branch_id].local_cancel.buffer_len )))
  753. continue;
  754. /* we passed all disqualifying factors .... the transaction has been
  755. matched !
  756. */
  757. set_t(p_cell);
  758. *p_branch =(int) branch_id;
  759. REF_UNSAFE( T );
  760. UNLOCK_HASH(hash_index);
  761. DBG("DEBUG: t_reply_matching: reply matched (T=%p)!\n",T);
  762. /* if this is a 200 for INVITE, we will wish to store to-tags to be
  763. * able to distinguish retransmissions later and not to call
  764. * TMCB_RESPONSE_OUT uselessly; we do it only if callbacks are
  765. * enabled -- except callback customers, nobody cares about
  766. * retransmissions of multiple 200/INV or ACK/200s
  767. */
  768. if (is_invite(p_cell) && p_msg->REPLY_STATUS>=200
  769. && p_msg->REPLY_STATUS<300
  770. && ( (!is_local(p_cell) &&
  771. has_tran_tmcbs(p_cell,TMCB_RESPONSE_OUT|TMCB_E2EACK_IN) )
  772. || (is_local(p_cell)&&has_tran_tmcbs(p_cell,TMCB_LOCAL_COMPLETED))
  773. )) {
  774. if (parse_headers(p_msg, HDR_TO_F, 0)==-1) {
  775. LOG(L_ERR, "ERROR: t_reply_matching: to parsing failed\n");
  776. }
  777. }
  778. if (!is_local(p_cell)) {
  779. run_trans_callbacks( TMCB_RESPONSE_IN, T, T->uas.request, p_msg,
  780. p_msg->REPLY_STATUS);
  781. }
  782. return 1;
  783. } /* for cycle */
  784. /* nothing found */
  785. UNLOCK_HASH(hash_index);
  786. DBG("DEBUG: t_reply_matching: no matching transaction exists\n");
  787. nomatch2:
  788. DBG("DEBUG: t_reply_matching: failure to match a transaction\n");
  789. *p_branch = -1;
  790. set_t(0);
  791. return -1;
  792. }
  793. /* Determine current transaction
  794. *
  795. * Found Not Found Error (e.g. parsing)
  796. * Return Value 1 0 -1
  797. * T ptr 0 T_UNDEFINED
  798. */
  799. int t_check( struct sip_msg* p_msg , int *param_branch )
  800. {
  801. int local_branch;
  802. int canceled;
  803. /* is T still up-to-date ? */
  804. DBG("DEBUG: t_check: msg id=%d global id=%d T start=%p\n",
  805. p_msg->id,global_msg_id,T);
  806. if ( p_msg->id != global_msg_id || T==T_UNDEFINED )
  807. {
  808. global_msg_id = p_msg->id;
  809. T = T_UNDEFINED;
  810. /* transaction lookup */
  811. if ( p_msg->first_line.type==SIP_REQUEST ) {
  812. /* force parsing all the needed headers*/
  813. if (parse_headers(p_msg, HDR_EOH_F, 0 )==-1) {
  814. LOG(L_ERR, "ERROR: t_check: parsing error\n");
  815. return -1;
  816. }
  817. /* in case, we act as UAS for INVITE and reply with 200,
  818. * we will need to run dialog-matching for subsequent
  819. * ACK, for which we need From-tag; We also need from-tag
  820. * in case people want to have proxied e2e ACKs accounted
  821. */
  822. if (p_msg->REQ_METHOD==METHOD_INVITE
  823. && parse_from_header(p_msg)==-1) {
  824. LOG(L_ERR, "ERROR: t_check: from parsing failed\n");
  825. return -1;
  826. }
  827. t_lookup_request( p_msg , 0 /* unlock before returning */,
  828. &canceled);
  829. } else {
  830. /* we need Via for branch and Cseq method to distinguish
  831. replies with the same branch/cseqNr (CANCEL)
  832. */
  833. if ( parse_headers(p_msg, HDR_VIA1_F|HDR_CSEQ_F, 0 )==-1
  834. || !p_msg->via1 || !p_msg->cseq ) {
  835. LOG(L_ERR, "ERROR: reply cannot be parsed\n");
  836. return -1;
  837. }
  838. /* if that is an INVITE, we will also need to-tag
  839. for later ACK matching
  840. */
  841. if ( get_cseq(p_msg)->method.len==INVITE_LEN
  842. && memcmp( get_cseq(p_msg)->method.s, INVITE, INVITE_LEN )==0 ) {
  843. if (parse_headers(p_msg, HDR_TO_F, 0)==-1
  844. || !p_msg->to) {
  845. LOG(L_ERR, "ERROR: INVITE reply cannot be parsed\n");
  846. return -1;
  847. }
  848. }
  849. t_reply_matching( p_msg ,
  850. param_branch!=0?param_branch:&local_branch );
  851. }
  852. #ifdef EXTRA_DEBUG
  853. if ( T && T!=T_UNDEFINED && T->flags & (T_IN_AGONY)) {
  854. LOG( L_ERR, "ERROR: transaction %p scheduled for deletion "
  855. "and called from t_check (flags=%x)\n", T, T->flags);
  856. abort();
  857. }
  858. #endif
  859. DBG("DEBUG: t_check: msg id=%d global id=%d T end=%p\n",
  860. p_msg->id,global_msg_id,T);
  861. } else {
  862. if (T)
  863. DBG("DEBUG: t_check: T already found!\n");
  864. else
  865. DBG("DEBUG: t_check: T previously sought and not found\n");
  866. }
  867. return T ? (T==T_UNDEFINED ? -1 : 1 ) : 0;
  868. }
  869. int init_rb( struct retr_buf *rb, struct sip_msg *msg)
  870. {
  871. /*struct socket_info* send_sock;*/
  872. struct via_body* via;
  873. int proto;
  874. int backup_mhomed;
  875. /* rb. timers are init. init_t()/new_cell() */
  876. via=msg->via1;
  877. if (!reply_to_via) {
  878. update_sock_struct_from_ip( &rb->dst.to, msg );
  879. proto=msg->rcv.proto;
  880. } else {
  881. /*init retrans buffer*/
  882. if (update_sock_struct_from_via( &(rb->dst.to), msg, via )==-1) {
  883. LOG(L_ERR, "ERROR: init_rb: cannot lookup reply dst: %.*s\n",
  884. via->host.len, via->host.s );
  885. ser_error=E_BAD_VIA;
  886. return 0;
  887. }
  888. proto=via->proto;
  889. }
  890. rb->dst.proto=proto;
  891. rb->dst.id=msg->rcv.proto_reserved1;
  892. /* turn off mhomed for generating replies -- they are ideally sent to where
  893. request came from to make life with NATs and other beasts easier
  894. */
  895. backup_mhomed=mhomed;
  896. mhomed=0;
  897. mhomed=backup_mhomed;
  898. /* use for sending replies the incoming interface of the request -bogdan */
  899. /*send_sock=get_send_socket(msg, &rb->dst.to, proto);
  900. if (send_sock==0) {
  901. LOG(L_ERR, "ERROR: init_rb: cannot fwd to af %d, proto %d "
  902. "no socket\n", rb->dst.to.s.sa_family, proto);
  903. ser_error=E_BAD_VIA;
  904. return 0;
  905. }*/
  906. rb->dst.send_sock=msg->rcv.bind_address;
  907. return 1;
  908. }
  909. static inline void init_new_t(struct cell *new_cell, struct sip_msg *p_msg)
  910. {
  911. struct sip_msg *shm_msg;
  912. unsigned int timeout; /* avp timeout gets stored here (in s) */
  913. shm_msg=new_cell->uas.request;
  914. new_cell->from.s=shm_msg->from->name.s;
  915. new_cell->from.len=HF_LEN(shm_msg->from);
  916. new_cell->to.s=shm_msg->to->name.s;
  917. new_cell->to.len=HF_LEN(shm_msg->to);
  918. new_cell->callid.s=shm_msg->callid->name.s;
  919. new_cell->callid.len=HF_LEN(shm_msg->callid);
  920. new_cell->cseq_n.s=shm_msg->cseq->name.s;
  921. new_cell->cseq_n.len=get_cseq(shm_msg)->number.s
  922. +get_cseq(shm_msg)->number.len
  923. -shm_msg->cseq->name.s;
  924. new_cell->method=new_cell->uas.request->first_line.u.request.method;
  925. if (p_msg->REQ_METHOD==METHOD_INVITE) new_cell->flags |= T_IS_INVITE_FLAG;
  926. new_cell->on_negative=get_on_negative();
  927. new_cell->on_reply=get_on_reply();
  928. new_cell->fr_timeout=(ticks_t)get_msgid_val(user_fr_timeout,
  929. p_msg->id, int);
  930. new_cell->fr_inv_timeout=(ticks_t)get_msgid_val(user_fr_inv_timeout,
  931. p_msg->id, int);
  932. if (new_cell->fr_timeout==0){
  933. if (!fr_avp2timer(&timeout)) {
  934. DBG("init_new_t: FR__TIMER = %d s\n", timeout);
  935. new_cell->fr_timeout=S_TO_TICKS((ticks_t)timeout);
  936. }else{
  937. new_cell->fr_timeout=fr_timeout;
  938. }
  939. }
  940. if (new_cell->fr_inv_timeout==0){
  941. if (!fr_inv_avp2timer(&timeout)) {
  942. DBG("init_new_t: FR_INV_TIMER = %d s\n", timeout);
  943. new_cell->fr_inv_timeout=S_TO_TICKS((ticks_t)timeout);
  944. new_cell->flags |= T_NOISY_CTIMER_FLAG;
  945. }else{
  946. new_cell->fr_inv_timeout=fr_inv_timeout;
  947. }
  948. }
  949. new_cell->on_branch=get_on_branch();
  950. }
  951. static inline int new_t(struct sip_msg *p_msg)
  952. {
  953. struct cell *new_cell;
  954. /* for ACK-dlw-wise matching, we want From-tags */
  955. if (p_msg->REQ_METHOD==METHOD_INVITE && parse_from_header(p_msg)<0) {
  956. LOG(L_ERR, "ERROR: new_t: no valid From in INVITE\n");
  957. return E_BAD_REQ;
  958. }
  959. /* make sure uri will be parsed before cloning */
  960. if (parse_sip_msg_uri(p_msg)<0) {
  961. LOG(L_ERR, "ERROR: new_t: uri invalid\n");
  962. return E_BAD_REQ;
  963. }
  964. /* add new transaction */
  965. new_cell = build_cell( p_msg ) ;
  966. if ( !new_cell ){
  967. LOG(L_ERR, "ERROR: new_t: out of mem:\n");
  968. return E_OUT_OF_MEM;
  969. }
  970. insert_into_hash_table_unsafe( new_cell, p_msg->hash_index );
  971. set_t(new_cell);
  972. INIT_REF_UNSAFE(T);
  973. /* init pointers to headers needed to construct local
  974. requests such as CANCEL/ACK
  975. */
  976. init_new_t(new_cell, p_msg);
  977. return 1;
  978. }
  979. /* atomic "new_tran" construct; it returns:
  980. <0 on error
  981. +1 if a request did not match a transaction
  982. - it that was an ack, the calling function
  983. shall forward statelessly
  984. - otherwise it means, a new transaction was
  985. introduced and the calling function
  986. shall reply/relay/whatever_appropriate
  987. 0 on retransmission
  988. */
  989. int t_newtran( struct sip_msg* p_msg )
  990. {
  991. int lret, my_err;
  992. int canceled;
  993. /* is T still up-to-date ? */
  994. DBG("DEBUG: t_newtran: msg id=%d , global msg id=%d ,"
  995. " T on entrance=%p\n",p_msg->id,global_msg_id,T);
  996. if ( T && T!=T_UNDEFINED ) {
  997. LOG(L_ERR, "ERROR: t_newtran: "
  998. "transaction already in process %p\n", T );
  999. return E_SCRIPT;
  1000. }
  1001. global_msg_id = p_msg->id;
  1002. T = T_UNDEFINED;
  1003. /* first of all, parse everything -- we will store in shared memory
  1004. and need to have all headers ready for generating potential replies
  1005. later; parsing later on demand is not an option since the request
  1006. will be in shmem and applying parse_headers to it would intermix
  1007. shmem with pkg_mem
  1008. */
  1009. if (parse_headers(p_msg, HDR_EOH_F, 0 )) {
  1010. LOG(L_ERR, "ERROR: t_newtran: parse_headers failed\n");
  1011. return E_BAD_REQ;
  1012. }
  1013. if ((p_msg->parsed_flag & HDR_EOH_F)!=HDR_EOH_F) {
  1014. LOG(L_ERR, "ERROR: t_newtran: EoH not parsed\n");
  1015. return E_OUT_OF_MEM;
  1016. }
  1017. /* t_lookup_requests attempts to find the transaction;
  1018. it also calls check_transaction_quadruple -> it is
  1019. safe to assume we have from/callid/cseq/to
  1020. */
  1021. lret = t_lookup_request( p_msg, 1 /* leave locked if not found */,
  1022. &canceled );
  1023. /* on error, pass the error in the stack ... nothing is locked yet
  1024. if 0 is returned */
  1025. if (lret==0) return E_BAD_TUPEL;
  1026. /* transaction found, it's a retransmission */
  1027. if (lret>0) {
  1028. if (p_msg->REQ_METHOD==METHOD_ACK) {
  1029. t_release_transaction(T);
  1030. } else {
  1031. t_retransmit_reply(T);
  1032. }
  1033. /* things are done -- return from script */
  1034. return 0;
  1035. }
  1036. /* from now on, be careful -- hash table is locked */
  1037. if (lret==-2) { /* was it an e2e ACK ? if so, trigger a callback */
  1038. /* no callbacks? complete quickly */
  1039. if ( !has_tran_tmcbs(t_ack,TMCB_E2EACK_IN) ) {
  1040. UNLOCK_HASH(p_msg->hash_index);
  1041. return 1;
  1042. }
  1043. REF_UNSAFE(t_ack);
  1044. UNLOCK_HASH(p_msg->hash_index);
  1045. /* we don't call from within REPLY_LOCK -- that introduces
  1046. * a race condition; however, it is so unlikely and the
  1047. * impact is so small (callback called multiple times of
  1048. * multiple ACK/200s received in parallel), that we do not
  1049. * better waste time in locks */
  1050. if (unmatched_totag(t_ack, p_msg)) {
  1051. run_trans_callbacks( TMCB_E2EACK_IN , t_ack, p_msg, 0,
  1052. -p_msg->REQ_METHOD );
  1053. }
  1054. UNREF(t_ack);
  1055. return 1;
  1056. }
  1057. /* transaction not found, it's a new request (lret<0, lret!=-2);
  1058. establish a new transaction ... */
  1059. if (p_msg->REQ_METHOD==METHOD_ACK) { /* ... unless it is in ACK */
  1060. my_err=1;
  1061. goto new_err;
  1062. }
  1063. my_err=new_t(p_msg);
  1064. if (my_err<0) {
  1065. LOG(L_ERR, "ERROR: t_newtran: new_t failed\n");
  1066. goto new_err;
  1067. }
  1068. if (canceled) T->flags|=T_CANCELED; /* mark it for future ref. */
  1069. UNLOCK_HASH(p_msg->hash_index);
  1070. /* now, when the transaction state exists, check if
  1071. there is a meaningful Via and calculate it; better
  1072. do it now than later: state is established so that
  1073. subsequent retransmissions will be absorbed and will
  1074. not possibly block during Via DNS resolution; doing
  1075. it later would only burn more CPU as if there is an
  1076. error, we cannot relay later whatever comes out of the
  1077. the transaction
  1078. */
  1079. if (!init_rb( &T->uas.response, p_msg)) {
  1080. LOG(L_ERR, "ERROR: t_newtran: unresolvable via1\n");
  1081. put_on_wait( T );
  1082. t_unref(p_msg);
  1083. return E_BAD_VIA;
  1084. }
  1085. return 1;
  1086. new_err:
  1087. UNLOCK_HASH(p_msg->hash_index);
  1088. return my_err;
  1089. }
  1090. int t_unref( struct sip_msg* p_msg )
  1091. {
  1092. enum kill_reason kr;
  1093. if (T==T_UNDEFINED || T==T_NULL_CELL)
  1094. return -1;
  1095. if (p_msg->first_line.type==SIP_REQUEST){
  1096. kr=get_kr();
  1097. if (kr==0
  1098. ||(p_msg->REQ_METHOD==METHOD_ACK && !(kr & REQ_RLSD))) {
  1099. LOG(L_WARN, "WARNING: script writer didn't release transaction\n");
  1100. t_release_transaction(T);
  1101. }
  1102. }
  1103. UNREF( T );
  1104. set_t(T_UNDEFINED);
  1105. return 1;
  1106. }
  1107. int t_get_trans_ident(struct sip_msg* p_msg, unsigned int* hash_index, unsigned int* label)
  1108. {
  1109. struct cell* t;
  1110. if(t_check(p_msg,0) != 1){
  1111. LOG(L_ERR,"ERROR: t_get_trans_ident: no transaction found\n");
  1112. return -1;
  1113. }
  1114. t = get_t();
  1115. if(!t){
  1116. LOG(L_ERR,"ERROR: t_get_trans_ident: transaction found is NULL\n");
  1117. return -1;
  1118. }
  1119. *hash_index = t->hash_index;
  1120. *label = t->label;
  1121. return 1;
  1122. }
  1123. int t_lookup_ident(struct cell ** trans, unsigned int hash_index, unsigned int label)
  1124. {
  1125. struct cell* p_cell;
  1126. if(hash_index >= TABLE_ENTRIES){
  1127. LOG(L_ERR,"ERROR: t_lookup_ident: invalid hash_index=%u\n",hash_index);
  1128. return -1;
  1129. }
  1130. LOCK_HASH(hash_index);
  1131. /* all the transactions from the entry are compared */
  1132. for ( p_cell = get_tm_table()->entrys[hash_index].first_cell;
  1133. p_cell; p_cell = p_cell->next_cell )
  1134. {
  1135. if(p_cell->label == label){
  1136. REF_UNSAFE(p_cell);
  1137. UNLOCK_HASH(hash_index);
  1138. set_t(p_cell);
  1139. *trans=p_cell;
  1140. DBG("DEBUG: t_lookup_ident: transaction found\n");
  1141. return 1;
  1142. }
  1143. }
  1144. UNLOCK_HASH(hash_index);
  1145. set_t(0);
  1146. *trans=p_cell;
  1147. DBG("DEBUG: t_lookup_ident: transaction not found\n");
  1148. return -1;
  1149. }
  1150. int t_is_local(struct sip_msg* p_msg)
  1151. {
  1152. struct cell* t;
  1153. if(t_check(p_msg,0) != 1){
  1154. LOG(L_ERR,"ERROR: t_is_local: no transaction found\n");
  1155. return -1;
  1156. }
  1157. t = get_t();
  1158. if(!t){
  1159. LOG(L_ERR,"ERROR: t_is_local: transaction found is NULL\n");
  1160. return -1;
  1161. }
  1162. return is_local(t);
  1163. }
  1164. /* lookup a transaction by callid and cseq, parameters are pure
  1165. * header field content only, e.g. "[email protected]" and "11"
  1166. */
  1167. int t_lookup_callid(struct cell ** trans, str callid, str cseq) {
  1168. struct cell* p_cell;
  1169. unsigned hash_index;
  1170. /* I use MAX_HEADER, not sure if this is a good choice... */
  1171. char callid_header[MAX_HEADER];
  1172. char cseq_header[MAX_HEADER];
  1173. /* save return value of print_* functions here */
  1174. char* endpos;
  1175. /* need method, which is always INVITE in our case */
  1176. /* CANCEL is only useful after INVITE */
  1177. str invite_method;
  1178. char* invite_string = INVITE;
  1179. invite_method.s = invite_string;
  1180. invite_method.len = INVITE_LEN;
  1181. /* lookup the hash index where the transaction is stored */
  1182. hash_index=hash(callid, cseq);
  1183. if(hash_index >= TABLE_ENTRIES){
  1184. LOG(L_ERR,"ERROR: t_lookup_callid: invalid hash_index=%u\n",hash_index);
  1185. return -1;
  1186. }
  1187. /* create header fields the same way tm does itself, then compare headers */
  1188. endpos = print_callid_mini(callid_header, callid);
  1189. DBG("created comparable call_id header field: >%.*s<\n",
  1190. (int)(endpos - callid_header), callid_header);
  1191. endpos = print_cseq_mini(cseq_header, &cseq, &invite_method);
  1192. DBG("created comparable cseq header field: >%.*s<\n",
  1193. (int)(endpos - cseq_header), cseq_header);
  1194. LOCK_HASH(hash_index);
  1195. DBG("just locked hash index %u, looking for transactions there:\n", hash_index);
  1196. /* all the transactions from the entry are compared */
  1197. for ( p_cell = get_tm_table()->entrys[hash_index].first_cell;
  1198. p_cell; p_cell = p_cell->next_cell ) {
  1199. /* compare complete header fields, casecmp to make sure invite=INVITE */
  1200. if ( (strncmp(callid_header, p_cell->callid.s, p_cell->callid.len) == 0)
  1201. && (strncasecmp(cseq_header, p_cell->cseq_n.s, p_cell->cseq_n.len) == 0) ) {
  1202. DBG("we have a match: callid=>>%.*s<< cseq=>>%.*s<<\n", p_cell->callid.len,
  1203. p_cell->callid.s, p_cell->cseq_n.len, p_cell->cseq_n.s);
  1204. REF_UNSAFE(p_cell);
  1205. UNLOCK_HASH(hash_index);
  1206. set_t(p_cell);
  1207. *trans=p_cell;
  1208. DBG("DEBUG: t_lookup_callid: transaction found.\n");
  1209. return 1;
  1210. }
  1211. DBG("NO match: callid=%.*s cseq=%.*s\n", p_cell->callid.len,
  1212. p_cell->callid.s, p_cell->cseq_n.len, p_cell->cseq_n.s);
  1213. }
  1214. UNLOCK_HASH(hash_index);
  1215. DBG("DEBUG: t_lookup_callid: transaction not found.\n");
  1216. return -1;
  1217. }
  1218. /* params: fr_inv & fr value in ms, 0 means "do not touch"
  1219. * ret: 1 on success, -1 on error (script safe)*/
  1220. int t_set_fr(struct sip_msg* msg, unsigned int fr_inv_to, unsigned int fr_to)
  1221. {
  1222. struct cell *t;
  1223. ticks_t fr_inv, fr;
  1224. fr_inv=MS_TO_TICKS((ticks_t)fr_inv_to);
  1225. if ((fr_inv==0) && (fr_inv_to!=0)){
  1226. ERR("t_set_fr_inv: fr_inv_timeout too small (%d)\n", fr_inv_to);
  1227. return -1;
  1228. }
  1229. fr=MS_TO_TICKS((ticks_t)fr_to);
  1230. if ((fr==0) && (fr_to!=0)){
  1231. ERR("t_set_fr_inv: fr_timeout too small (%d)\n", fr_to);
  1232. return -1;
  1233. }
  1234. t=get_t();
  1235. /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
  1236. * in MODE_REQUEST T will be set only if the transaction was already
  1237. * created; if not -> use the static variable */
  1238. if (!t || t==T_UNDEFINED ){
  1239. set_msgid_val(user_fr_inv_timeout, msg->id, int, (int)fr_inv);
  1240. set_msgid_val(user_fr_timeout, msg->id, int, (int)fr);
  1241. }else{
  1242. change_fr(t, fr_inv, fr); /* change running uac timers */
  1243. }
  1244. return 1;
  1245. }