ring.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2008-2009 1&1 Internet AG
  5. *
  6. * This file is part of SIP-router, a free SIP server.
  7. *
  8. * SIP-router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * SIP-router is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * \file
  24. * \brief SIP-utils :: Only allow one 183 message per call-id
  25. * \ingroup siputils
  26. * - Module; \ref siputils
  27. *
  28. * \section ring_utils UTILS :: Ringing functionality
  29. *
  30. * In a parallel forking scenario you may get several 183s with SDP. You don't want
  31. * that your customers hear more than one ringtone or answer machine in parallel
  32. * on the phone. So its necessary to drop the 183 in these cases and send a 180 instead.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <assert.h>
  38. #include "../../parser/msg_parser.h"
  39. #include "../../dprint.h"
  40. #include "../../error.h"
  41. #include "../../ut.h"
  42. #include "../../mem/mem.h"
  43. #include "../../mem/shm_mem.h"
  44. #include "../../timer.h"
  45. #include "../../locking.h"
  46. #include "../../md5.h"
  47. #include "config.h"
  48. #include "ring.h"
  49. /*! list of calls for ringing functionality */
  50. struct ring_record_t {
  51. struct ring_record_t *next;
  52. unsigned int time; /*!< timeout value */
  53. char callid[MAXCALLIDLEN+1]; /*!< callid of this call */
  54. };
  55. /*! hashtable for ringing records */
  56. struct hashtable_entry_t {
  57. struct ring_record_t *head;
  58. struct ring_record_t *tail;
  59. };
  60. typedef struct hashtable_entry_t hashtable_t[HASHTABLESIZE];
  61. /*! global hashtable */
  62. static hashtable_t *hashtable = NULL;
  63. static void insert(str callid);
  64. static int contains(str callid);
  65. /*!
  66. * \brief Inserts callid of message into hashtable
  67. *
  68. * Inserts callid of message into hashtable. Any 183 messages with
  69. * this callid that occur in the next ring_timeout seconds, will be
  70. * converted to 180.
  71. * \param msg SIP message
  72. * \param unused1 unused
  73. * \param unused2 unused
  74. * \return 1 on success, -1 otherwise
  75. */
  76. int ring_insert_callid(struct sip_msg *msg, char *unused1, char *unused2)
  77. {
  78. /* could fail, eg if already parsed don't care about result */
  79. parse_headers(msg, HDR_CALLID_F, 0);
  80. if (msg->callid) {
  81. lock_get(ring_lock);
  82. if (!contains(msg->callid->body)) insert(msg->callid->body);
  83. lock_release(ring_lock);
  84. } else {
  85. LM_ERR("no callid\n");
  86. return -1;
  87. }
  88. return 1;
  89. }
  90. /*!
  91. * \brief Initialize the ring hashtable in shared memory
  92. */
  93. void ring_init_hashtable(void)
  94. {
  95. int i;
  96. hashtable = shm_malloc(sizeof(hashtable_t));
  97. assert(hashtable);
  98. for (i=0; i<HASHTABLESIZE; i++) {
  99. (*hashtable)[i].head = NULL;
  100. (*hashtable)[i].tail = NULL;
  101. }
  102. }
  103. /*!
  104. * \brief Destroy the ring hashtable
  105. */
  106. void ring_destroy_hashtable(void)
  107. {
  108. int i;
  109. if (hashtable) {
  110. for (i=0; i<HASHTABLESIZE; i++) {
  111. while ((*hashtable)[i].head) {
  112. struct ring_record_t* rr = (*hashtable)[i].head;
  113. (*hashtable)[i].head = rr->next;
  114. shm_free(rr);
  115. }
  116. (*hashtable)[i].tail = NULL;
  117. }
  118. shm_free(hashtable);
  119. }
  120. }
  121. /*!
  122. * \brief Hash helper function
  123. * \param buf hashed buffer
  124. * \param len length of buffer
  125. * \return hash value, can be 0
  126. */
  127. static unsigned int hash(char *buf, int len)
  128. {
  129. int i;
  130. unsigned int retval = 0;
  131. MD5_CTX md5context;
  132. char digest[16];
  133. MD5Init(&md5context);
  134. MD5Update(&md5context, buf, len);
  135. MD5Final(digest, &md5context);
  136. for (i=0; i<16; i++) {
  137. retval ^= ((unsigned int)((unsigned char)buf[i])) << i;
  138. }
  139. return retval;
  140. }
  141. /*!
  142. * \brief Expire entries on the hashtable
  143. * \param index array index that should expired
  144. */
  145. static void remove_timeout(unsigned int index)
  146. {
  147. int ring_timeout = cfg_get(siputils, siputils_cfg, ring_timeout);
  148. if(ring_timeout == 0){
  149. LM_ERR("Could not get timeout from cfg. This will expire all entries");
  150. }
  151. while ((*hashtable)[index].head && ((*hashtable)[index].head)->time + ring_timeout < get_ticks()) {
  152. struct ring_record_t* rr = (*hashtable)[index].head;
  153. (*hashtable)[index].head = rr->next;
  154. if ((*hashtable)[index].head == NULL) (*hashtable)[index].tail = NULL;
  155. LM_DBG("deleting ticks=%d %s\n", get_ticks(), rr->callid);
  156. shm_free(rr);
  157. }
  158. }
  159. /*!
  160. * \brief Insert a new entry on the hashtable
  161. * \param callid Call-ID string
  162. */
  163. static void insert(str callid)
  164. {
  165. unsigned int index = hash(callid.s, callid.len) & HASHTABLEMASK;
  166. struct ring_record_t* rr;
  167. remove_timeout(index);
  168. rr = shm_malloc(sizeof(struct ring_record_t));
  169. assert(rr);
  170. rr->next = NULL;
  171. rr->time = get_ticks();
  172. strncpy(rr->callid, callid.s, MIN(callid.len, MAXCALLIDLEN));
  173. rr->callid[MIN(callid.len, MAXCALLIDLEN)] = 0;
  174. if ((*hashtable)[index].tail) {
  175. (*hashtable)[index].tail->next = rr;
  176. (*hashtable)[index].tail = rr;
  177. }
  178. else {
  179. (*hashtable)[index].head = rr;
  180. (*hashtable)[index].tail = rr;
  181. }
  182. LM_DBG("inserting at %d %.*s ticks=%d\n", index, callid.len, callid.s, rr->time);
  183. }
  184. /*!
  185. * \brief Helper functions that checks if the hash table contains the callid
  186. * \param callid Call-ID that is searched
  187. * \return 1 when callid could be found, 0 when not found
  188. */
  189. static int contains(str callid)
  190. {
  191. unsigned int index = hash(callid.s, callid.len) & HASHTABLEMASK;
  192. struct ring_record_t* rr;
  193. remove_timeout(index);
  194. rr = (*hashtable)[index].head;
  195. while (rr) {
  196. if (strncmp(rr->callid, callid.s, callid.len) == 0) return 1;
  197. rr = rr->next;
  198. }
  199. return 0;
  200. }
  201. /*!
  202. * \brief Convert a 183 to a 180 message.
  203. * \param msg SIP message
  204. */
  205. static int conv183(struct sip_msg *msg)
  206. {
  207. /* content-length and content-type headers are removed */
  208. char *del1_start = strstr(msg->buf, "Content-Length:");
  209. char *del2_start = strstr(msg->buf, "Content-Type:");
  210. char *del1_end;
  211. char *del2_end;
  212. char *eoh;
  213. char *chunk1_start;
  214. int chunk1_len;
  215. char *chunk1_dst;
  216. char *chunk2_start;
  217. int chunk2_len;
  218. char *chunk2_dst;
  219. char *chunk3_start;
  220. int chunk3_len;
  221. char *chunk3_dst;
  222. if (del1_start>del2_start) {
  223. char *tmp = del1_start;
  224. del1_start = del2_start;
  225. del2_start = tmp;
  226. }
  227. del1_end = NULL;
  228. if (del1_start) {
  229. del1_end = strstr(del1_start, "\r\n");
  230. if (del1_end) del1_end+=2;
  231. }
  232. del2_end = NULL;
  233. if (del2_start) {
  234. del2_end = strstr(del2_start, "\r\n");
  235. if (del2_end) del2_end+=2;
  236. }
  237. /* 180 message does not need session description */
  238. eoh = strstr(msg->buf, "\r\n\r\n");
  239. if (eoh) eoh+=2;
  240. if ((!del1_start) || (!del2_start) || (!del1_end) || (!del2_end) || (!eoh)) {
  241. LM_ERR("got invalid 183 message\n");
  242. return -1;
  243. }
  244. /*
  245. * if message is parsed further than first deletion, offsets of parsed strings would
  246. * not be correct any more. In that case do not convert! If this error is reported,
  247. * check if other pre script callbacks are installed before the one of this module.
  248. */
  249. if (msg->unparsed>del1_start) {
  250. LM_ERR("183 message got parsed too far!\n");
  251. return -1;
  252. }
  253. /* setting new status */
  254. msg->first_line.u.reply.statuscode=180;
  255. msg->first_line.u.reply.status.s[2]='0';
  256. // don't change length of reason string
  257. strncpy(msg->first_line.u.reply.reason.s, "Ringing ", msg->first_line.u.reply.reason.len);
  258. /* calculate addresses of chunks to be moved */
  259. chunk1_start = del1_end;
  260. chunk1_len = (int)(long)(del2_start-del1_end);
  261. chunk1_dst = del1_start;
  262. chunk2_start = del2_end;
  263. chunk2_len = (int)(long)(eoh-del2_end);
  264. chunk2_dst = chunk1_dst+chunk1_len;
  265. chunk3_start = "Content-Length: 0\r\n\r\n";
  266. chunk3_len = strlen(chunk3_start);
  267. chunk3_dst = chunk2_dst+chunk2_len;
  268. // move chunks
  269. memmove(chunk1_dst, chunk1_start, chunk1_len);
  270. memmove(chunk2_dst, chunk2_start, chunk2_len);
  271. memmove(chunk3_dst, chunk3_start, chunk3_len);
  272. /* terminate string with zero */
  273. *(chunk3_dst+chunk3_len)='\0';
  274. /* update message length */
  275. msg->len = strlen(msg->buf);
  276. return 0;
  277. }
  278. /*!
  279. * \brief Callback function that does the work inside the server.
  280. * \param msg SIP message
  281. * \param flags unused
  282. * \param bar unused
  283. * \return 1 on success, -1 on failure
  284. */
  285. int ring_filter(struct sip_msg *msg, unsigned int flags, void *bar)
  286. {
  287. int contains_callid;
  288. if (msg->first_line.type == SIP_REPLY && msg->first_line.u.reply.statuscode == 183) {
  289. /* could fail, eg if already parsed, don't care about result */
  290. parse_headers(msg, HDR_CALLID_F, 0);
  291. if (msg->callid) {
  292. lock_get(ring_lock);
  293. contains_callid=contains(msg->callid->body);
  294. lock_release(ring_lock);
  295. if (contains_callid) {
  296. LM_DBG("converting 183 to 180 for %.*s\n", msg->callid->body.len, msg->callid->body.s);
  297. if (conv183(msg)!=0) return -1;
  298. }
  299. } else {
  300. LM_ERR("no callid\n");
  301. return -1;
  302. }
  303. }
  304. return 1;
  305. }
  306. /*!
  307. * \brief Fixup function for the ring_insert_callid function
  308. * \param param unused
  309. * \param param_no unused
  310. * \return 0
  311. */
  312. int ring_fixup(void ** param, int param_no) {
  313. int ring_timeout = cfg_get(siputils, siputils_cfg, ring_timeout);
  314. if (ring_timeout == 0) {
  315. LM_ERR("ring_insert_callid functionality deactivated, you need to set a positive ring_timeout\n");
  316. return -1;
  317. }
  318. return 0;
  319. }