tm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * $Id$
  3. *
  4. * TM module
  5. *
  6. *
  7. * Copyright (C) 2001-2003 Fhg Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <netdb.h>
  33. #include "../../sr_module.h"
  34. #include "../../dprint.h"
  35. #include "../../error.h"
  36. #include "../../ut.h"
  37. #include "../../script_cb.h"
  38. #include "../../fifo_server.h"
  39. #include "sip_msg.h"
  40. #include "h_table.h"
  41. #include "t_funcs.h"
  42. #include "t_hooks.h"
  43. #include "tm_load.h"
  44. #include "ut.h"
  45. #include "t_reply.h"
  46. #include "uac.h"
  47. #include "t_fwd.h"
  48. #include "t_lookup.h"
  49. #include "t_stats.h"
  50. inline static int w_t_check(struct sip_msg* msg, char* str, char* str2);
  51. inline static int w_t_reply(struct sip_msg* msg, char* str, char* str2);
  52. inline static int w_t_reply_unsafe(struct sip_msg* msg, char* str, char* str2);
  53. inline static int w_t_release(struct sip_msg* msg, char* str, char* str2);
  54. inline static int fixup_t_send_reply(void** param, int param_no);
  55. inline static int fixup_str2int( void** param, int param_no);
  56. inline static int w_t_retransmit_reply(struct sip_msg* p_msg, char* foo, char* bar );
  57. inline static int w_t_newtran(struct sip_msg* p_msg, char* foo, char* bar );
  58. inline static int w_t_newdlg( struct sip_msg* p_msg, char* foo, char* bar );
  59. inline static int w_t_relay( struct sip_msg *p_msg , char *_foo, char *_bar);
  60. inline static int w_t_relay_to( struct sip_msg *p_msg , char *proxy, char *);
  61. inline static int w_t_replicate( struct sip_msg *p_msg ,
  62. char *proxy, /* struct proxy_l *proxy expected */
  63. char *_foo /* nothing expected */ );
  64. inline static int w_t_forward_nonack(struct sip_msg* msg, char* str, char* );
  65. inline static int fixup_hostport2proxy(void** param, int param_no);
  66. inline static int w_t_on_negative( struct sip_msg* msg, char *go_to, char *foo );
  67. static int mod_init(void);
  68. static int child_init(int rank);
  69. #ifdef STATIC_TM
  70. struct module_exports tm_exports = {
  71. #else
  72. struct module_exports exports= {
  73. #endif
  74. "tm",
  75. /* -------- exported functions ----------- */
  76. (char*[]){
  77. "t_newtran",
  78. "t_lookup_request",
  79. T_REPLY,
  80. T_REPLY_UNSAFE,
  81. "t_retransmit_reply",
  82. "t_release",
  83. T_RELAY_TO,
  84. "t_replicate",
  85. T_RELAY,
  86. T_FORWARD_NONACK,
  87. "t_on_negative",
  88. /* not applicable from script ... */
  89. "register_tmcb",
  90. T_UAC,
  91. "load_tm",
  92. "t_newdlg"
  93. },
  94. (cmd_function[]){
  95. w_t_newtran,
  96. w_t_check,
  97. w_t_reply,
  98. w_t_reply_unsafe,
  99. w_t_retransmit_reply,
  100. w_t_release,
  101. w_t_relay_to,
  102. w_t_replicate,
  103. w_t_relay,
  104. w_t_forward_nonack,
  105. w_t_on_negative,
  106. (cmd_function) register_tmcb,
  107. (cmd_function) t_uac,
  108. (cmd_function) load_tm,
  109. w_t_newdlg,
  110. },
  111. (int[]){
  112. 0, /* t_newtran */
  113. 0, /* t_lookup_request */
  114. 2, /* t_reply */
  115. 2, /* t_reply_unsafe */
  116. 0, /* t_retransmit_reply */
  117. 0, /* t_release */
  118. 2, /* t_relay_to */
  119. 2, /* t_replicate */
  120. 0, /* t_relay */
  121. 2, /* t_forward_nonack */
  122. 1, /* t_on_negative */
  123. NO_SCRIPT /* register_tmcb */,
  124. NO_SCRIPT /* t_uac */,
  125. NO_SCRIPT /* load_tm */,
  126. 0 /* t_newdlg */
  127. },
  128. (fixup_function[]){
  129. 0, /* t_newtran */
  130. 0, /* t_lookup_request */
  131. fixup_t_send_reply, /* t_reply */
  132. fixup_t_send_reply, /* t_reply_unsafe */
  133. 0, /* t_retransmit_reply */
  134. 0, /* t_release */
  135. fixup_hostport2proxy, /* t_relay_to */
  136. fixup_hostport2proxy, /* t_replicate */
  137. 0, /* t_relay */
  138. fixup_hostport2proxy, /* t_forward_nonack */
  139. fixup_str2int, /* t_on_negative */
  140. 0, /* register_tmcb */
  141. 0, /* t_uac */
  142. 0, /* load_tm */
  143. 0 /* t_newdlg */
  144. },
  145. 15,
  146. /* ------------ exported variables ---------- */
  147. (char *[]) { /* Module parameter names */
  148. "fr_timer",
  149. "fr_inv_timer",
  150. "wt_timer",
  151. "delete_timer",
  152. "retr_timer1p1",
  153. "retr_timer1p2",
  154. "retr_timer1p3",
  155. "retr_timer2",
  156. "noisy_ctimer",
  157. "uac_from"
  158. },
  159. (modparam_t[]) { /* variable types */
  160. INT_PARAM, /* fr_timer */
  161. INT_PARAM, /* fr_inv_timer */
  162. INT_PARAM, /* wt_timer */
  163. INT_PARAM, /* delete_timer */
  164. INT_PARAM,/* retr_timer1p1 */
  165. INT_PARAM, /* retr_timer1p2 */
  166. INT_PARAM, /* retr_timer1p3 */
  167. INT_PARAM, /* retr_timer2 */
  168. INT_PARAM, /* noisy_ctimer */
  169. STR_PARAM, /* uac_from */
  170. },
  171. (void *[]) { /* variable pointers */
  172. &(timer_id2timeout[FR_TIMER_LIST]),
  173. &(timer_id2timeout[FR_INV_TIMER_LIST]),
  174. &(timer_id2timeout[WT_TIMER_LIST]),
  175. &(timer_id2timeout[DELETE_LIST]),
  176. &(timer_id2timeout[RT_T1_TO_1]),
  177. &(timer_id2timeout[RT_T1_TO_2]),
  178. &(timer_id2timeout[RT_T1_TO_3]),
  179. &(timer_id2timeout[RT_T2]),
  180. &noisy_ctimer,
  181. &uac_from
  182. },
  183. 11, /* Number of module paramers */
  184. mod_init, /* module initialization function */
  185. (response_function) t_on_reply,
  186. (destroy_function) tm_shutdown,
  187. 0, /* w_onbreak, */
  188. child_init /* per-child init function */
  189. };
  190. inline static int fixup_str2int( void** param, int param_no)
  191. {
  192. unsigned int go_to;
  193. int err;
  194. if (param_no==1) {
  195. go_to=str2s(*param, strlen(*param), &err );
  196. if (err==0) {
  197. free(*param);
  198. *param=(void *)go_to;
  199. return 0;
  200. } else {
  201. LOG(L_ERR, "ERROR: fixup_str2int: bad number <%s>\n",
  202. (char *)(*param));
  203. return E_CFG;
  204. }
  205. }
  206. return 0;
  207. }
  208. static int w_t_unref( struct sip_msg *foo, void *bar)
  209. {
  210. return t_unref(foo);
  211. }
  212. static int script_init( struct sip_msg *foo, void *bar)
  213. {
  214. /* we primarily reset all private memory here to make sure
  215. private values left over from previous message will
  216. not be used again
  217. */
  218. /* make sure the new message will not inherit previous
  219. message's t_on_negative value
  220. */
  221. t_on_negative( 0 );
  222. return 1;
  223. }
  224. static int mod_init(void)
  225. {
  226. DBG( "TM - initializing...\n");
  227. /* checking if we have sufficient bitmap capacity for given
  228. maximum number of branches */
  229. if (1<<(MAX_BRANCHES+1)>UINT_MAX) {
  230. LOG(L_CRIT, "Too many max UACs for UAC branch_bm_t bitmap: %d\n",
  231. MAX_BRANCHES );
  232. return -1;
  233. }
  234. if (register_fifo_cmd(fifo_uac, "t_uac", 0)<0) {
  235. LOG(L_CRIT, "cannot register fifo uac\n");
  236. return -1;
  237. }
  238. if (register_fifo_cmd(fifo_uac_from, "t_uac_from", 0)<0) {
  239. LOG(L_CRIT, "cannot register fifo uac\n");
  240. return -1;
  241. }
  242. if (init_tm_stats()<0) {
  243. LOG(L_CRIT, "ERROR: mod_init: failed to init stats\n");
  244. return -1;
  245. }
  246. /* building the hash table*/
  247. if (!init_hash_table()) {
  248. LOG(L_ERR, "ERROR: mod_init: initializing hash_table failed\n");
  249. return -1;
  250. }
  251. if (!tm_init_timers()) {
  252. LOG(L_ERR, "ERROR: mod_init: timer init failed\n");
  253. return -1;
  254. }
  255. /* init static hidden values */
  256. init_t();
  257. uac_init();
  258. register_tmcb( TMCB_ON_NEGATIVE, on_negative_reply, 0 /* empty param */);
  259. /* register the timer function */
  260. register_timer( timer_routine , 0 /* empty attr */, 1 );
  261. /* register post-script clean-up function */
  262. register_script_cb( w_t_unref, POST_SCRIPT_CB, 0 /* empty param */ );
  263. register_script_cb( script_init, PRE_SCRIPT_CB , 0 /* empty param */ );
  264. return 0;
  265. }
  266. static int child_init(int rank) {
  267. uac_child_init(rank);
  268. return 1;
  269. }
  270. /* (char *hostname, char *port_nr) ==> (struct proxy_l *, -) */
  271. inline static int fixup_hostport2proxy(void** param, int param_no)
  272. {
  273. unsigned int port;
  274. char *host;
  275. int err;
  276. struct proxy_l *proxy;
  277. DBG("TM module: fixup_t_forward(%s, %d)\n", (char*)*param, param_no);
  278. if (param_no==1){
  279. DBG("TM module: fixup_t_forward: param 1.. do nothing, wait for #2\n");
  280. return 0;
  281. } else if (param_no==2) {
  282. host=(char *) (*(param-1));
  283. port=str2s(*param, strlen(*param), &err);
  284. if (err!=0) {
  285. LOG(L_ERR, "TM module:fixup_t_forward: bad port number <%s>\n",
  286. (char*)(*param));
  287. return E_UNSPEC;
  288. }
  289. proxy=mk_proxy(host, port);
  290. if (proxy==0) {
  291. LOG(L_ERR, "ERROR: fixup_t_forwardv6: bad host name in URI <%s>\n",
  292. host );
  293. return E_BAD_ADDRESS;
  294. }
  295. /* success -- fix the first parameter to proxy now ! */
  296. free( *(param-1));
  297. *(param-1)=proxy;
  298. return 0;
  299. } else {
  300. LOG(L_ERR, "ERROR: fixup_t_forwardv6 called with parameter #<>{1,2}\n");
  301. return E_BUG;
  302. }
  303. }
  304. /* (char *code, char *reason_phrase)==>(int code, r_p as is) */
  305. inline static int fixup_t_send_reply(void** param, int param_no)
  306. {
  307. unsigned int code;
  308. int err;
  309. if (param_no==1){
  310. code=str2s(*param, strlen(*param), &err);
  311. if (err==0){
  312. free(*param);
  313. *param=(void*)code;
  314. return 0;
  315. }else{
  316. LOG(L_ERR, "TM module:fixup_t_send_reply: bad number <%s>\n",
  317. (char*)(*param));
  318. return E_UNSPEC;
  319. }
  320. }
  321. /* second param => no conversion*/
  322. return 0;
  323. }
  324. inline static int w_t_check(struct sip_msg* msg, char* str, char* str2)
  325. {
  326. return t_check( msg , 0 ) ? 1 : -1;
  327. }
  328. inline static int w_t_forward_nonack(struct sip_msg* msg, char* proxy, char* _foo)
  329. {
  330. struct cell *t;
  331. if (t_check( msg , 0 )==-1) return -1;
  332. t=get_t();
  333. if ( t && t!=T_UNDEFINED ) {
  334. if (msg->REQ_METHOD==METHOD_ACK) {
  335. LOG(L_WARN,"WARNING: you don't really want to fwd hbh ACK\n");
  336. return -1;
  337. }
  338. return t_forward_nonack(t, msg, ( struct proxy_l *) proxy );
  339. } else {
  340. DBG("DEBUG: t_forward_nonack: no transaction found\n");
  341. return -1;
  342. }
  343. }
  344. inline static int w_t_reply(struct sip_msg* msg, char* str, char* str2)
  345. {
  346. struct cell *t;
  347. if (msg->REQ_METHOD==METHOD_ACK) {
  348. LOG(L_WARN, "WARNING: t_reply: ACKs are not replied\n");
  349. return -1;
  350. }
  351. if (t_check( msg , 0 )==-1) return -1;
  352. t=get_t();
  353. if (!t) {
  354. LOG(L_ERR, "ERROR: t_reply: cannot send a t_reply to a message "
  355. "for which no T-state has been established\n");
  356. return -1;
  357. }
  358. return t_reply( t, msg, (unsigned int) str, str2);
  359. }
  360. inline static int w_t_reply_unsafe(struct sip_msg* msg, char* str, char* str2)
  361. {
  362. struct cell *t;
  363. if (msg->REQ_METHOD==METHOD_ACK) {
  364. LOG(L_WARN, "WARNING: t_reply: ACKs are not replied\n");
  365. return -1;
  366. }
  367. if (t_check( msg , 0 )==-1) return -1;
  368. t=get_t();
  369. if (!t) {
  370. LOG(L_ERR, "ERROR: t_reply: cannot send a t_reply to a message "
  371. "for which no T-state has been established\n");
  372. return -1;
  373. }
  374. return t_reply_unsafe(t, msg, (unsigned int) str, str2);
  375. }
  376. inline static int w_t_release(struct sip_msg* msg, char* str, char* str2)
  377. {
  378. struct cell *t;
  379. if (t_check( msg , 0 )==-1) return -1;
  380. t=get_t();
  381. if ( t && t!=T_UNDEFINED )
  382. return t_release_transaction( t );
  383. return 1;
  384. }
  385. inline static int w_t_retransmit_reply( struct sip_msg* p_msg, char* foo, char* bar)
  386. {
  387. struct cell *t;
  388. if (t_check( p_msg , 0 )==-1)
  389. return 1;
  390. t=get_t();
  391. if (t) {
  392. if (p_msg->REQ_METHOD==METHOD_ACK) {
  393. LOG(L_WARN, "WARNING: : ACKs ansmit_replies not replied\n");
  394. return -1;
  395. }
  396. return t_retransmit_reply( t );
  397. } else
  398. return -1;
  399. return 1;
  400. }
  401. inline static int w_t_newdlg( struct sip_msg* p_msg, char* foo, char* bar )
  402. {
  403. return t_newdlg( p_msg );
  404. }
  405. inline static int w_t_newtran( struct sip_msg* p_msg, char* foo, char* bar )
  406. {
  407. /* t_newtran returns 0 on error (negative value means
  408. 'transaction exists'
  409. */
  410. return t_newtran( p_msg );
  411. }
  412. inline static int w_t_on_negative( struct sip_msg* msg, char *go_to, char *foo )
  413. {
  414. return t_on_negative( (unsigned int ) go_to );
  415. }
  416. inline static int w_t_relay_to( struct sip_msg *p_msg ,
  417. char *proxy, /* struct proxy_l *proxy expected */
  418. char *_foo /* nothing expected */ )
  419. {
  420. return t_relay_to( p_msg, ( struct proxy_l *) proxy,
  421. 0 /* no replication */ );
  422. }
  423. inline static int w_t_replicate( struct sip_msg *p_msg ,
  424. char *proxy, /* struct proxy_l *proxy expected */
  425. char *_foo /* nothing expected */ )
  426. {
  427. return t_replicate(p_msg, ( struct proxy_l *) proxy );
  428. }
  429. inline static int w_t_relay( struct sip_msg *p_msg ,
  430. char *_foo, char *_bar)
  431. {
  432. return t_relay_to( p_msg,
  433. (struct proxy_l *) 0 /* no proxy */,
  434. 0 /* no replication */ );
  435. }