urecord.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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. * Kamailio 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. * History:
  23. * ---------
  24. * 2003-03-12 added replication mark and zombie state support (nils)
  25. * 2004-03-17 generic callbacks added (bogdan)
  26. * 2004-06-07 updated to the new DB api (andrei)
  27. */
  28. /*! \file
  29. * \brief USRLOC - Usrloc record structure
  30. * \ingroup usrloc
  31. *
  32. * - Module \ref usrloc
  33. */
  34. #include "urecord.h"
  35. #include <string.h>
  36. #include "../../mem/shm_mem.h"
  37. #include "../../dprint.h"
  38. #include "../../ut.h"
  39. #include "../../hashes.h"
  40. #include "../../tcp_conn.h"
  41. #include "ul_mod.h"
  42. #include "usrloc.h"
  43. #include "utime.h"
  44. #include "ul_callback.h"
  45. #include "usrloc.h"
  46. /*! contact matching mode */
  47. int matching_mode = CONTACT_ONLY;
  48. /*! retransmission detection interval in seconds */
  49. int cseq_delay = 20;
  50. /*!
  51. * \brief Create and initialize new record structure
  52. * \param _dom domain name
  53. * \param _aor address of record
  54. * \param _r pointer to the new record
  55. * \return 0 on success, negative on failure
  56. */
  57. int new_urecord(str* _dom, str* _aor, urecord_t** _r)
  58. {
  59. *_r = (urecord_t*)shm_malloc(sizeof(urecord_t));
  60. if (*_r == 0) {
  61. LM_ERR("no more share memory\n");
  62. return -1;
  63. }
  64. memset(*_r, 0, sizeof(urecord_t));
  65. (*_r)->aor.s = (char*)shm_malloc(_aor->len);
  66. if ((*_r)->aor.s == 0) {
  67. LM_ERR("no more share memory\n");
  68. shm_free(*_r);
  69. *_r = 0;
  70. return -2;
  71. }
  72. memcpy((*_r)->aor.s, _aor->s, _aor->len);
  73. (*_r)->aor.len = _aor->len;
  74. (*_r)->domain = _dom;
  75. (*_r)->aorhash = ul_get_aorhash(_aor);
  76. return 0;
  77. }
  78. /*!
  79. * \brief Free all memory used by the given structure
  80. *
  81. * Free all memory used by the given structure.
  82. * The structure must be removed from all linked
  83. * lists first
  84. * \param _r freed record list
  85. */
  86. void free_urecord(urecord_t* _r)
  87. {
  88. ucontact_t* ptr;
  89. while(_r->contacts) {
  90. ptr = _r->contacts;
  91. _r->contacts = _r->contacts->next;
  92. free_ucontact(ptr);
  93. }
  94. /* if mem cache is not used, the urecord struct is static*/
  95. if (db_mode!=DB_ONLY) {
  96. if (_r->aor.s) shm_free(_r->aor.s);
  97. shm_free(_r);
  98. }
  99. }
  100. /*!
  101. * \brief Print a record, useful for debugging
  102. * \param _f print output
  103. * \param _r printed record
  104. */
  105. void print_urecord(FILE* _f, urecord_t* _r)
  106. {
  107. ucontact_t* ptr;
  108. fprintf(_f, "...Record(%p)...\n", _r);
  109. fprintf(_f, "domain : '%.*s'\n", _r->domain->len, ZSW(_r->domain->s));
  110. fprintf(_f, "aor : '%.*s'\n", _r->aor.len, ZSW(_r->aor.s));
  111. fprintf(_f, "aorhash: '%u'\n", (unsigned)_r->aorhash);
  112. fprintf(_f, "slot: '%d'\n", _r->aorhash&(_r->slot->d->size-1));
  113. if (_r->contacts) {
  114. ptr = _r->contacts;
  115. while(ptr) {
  116. print_ucontact(_f, ptr);
  117. ptr = ptr->next;
  118. }
  119. }
  120. fprintf(_f, ".../Record...\n");
  121. }
  122. /*!
  123. * \brief Add a new contact in memory
  124. *
  125. * Add a new contact in memory, contacts are ordered by:
  126. * 1) q value, 2) descending modification time
  127. * \param _r record this contact belongs to
  128. * \param _c contact
  129. * \param _ci contact information
  130. * \return pointer to new created contact on success, 0 on failure
  131. */
  132. ucontact_t* mem_insert_ucontact(urecord_t* _r, str* _c, ucontact_info_t* _ci)
  133. {
  134. ucontact_t* ptr, *prev = 0;
  135. ucontact_t* c;
  136. if ( (c=new_ucontact(_r->domain, &_r->aor, _c, _ci)) == 0) {
  137. LM_ERR("failed to create new contact\n");
  138. return 0;
  139. }
  140. if_update_stat( _r->slot, _r->slot->d->contacts, 1);
  141. ptr = _r->contacts;
  142. if (!desc_time_order) {
  143. while(ptr) {
  144. if (ptr->q < c->q) break;
  145. prev = ptr;
  146. ptr = ptr->next;
  147. }
  148. }
  149. if (ptr) {
  150. if (!ptr->prev) {
  151. ptr->prev = c;
  152. c->next = ptr;
  153. _r->contacts = c;
  154. } else {
  155. c->next = ptr;
  156. c->prev = ptr->prev;
  157. ptr->prev->next = c;
  158. ptr->prev = c;
  159. }
  160. } else if (prev) {
  161. prev->next = c;
  162. c->prev = prev;
  163. } else {
  164. _r->contacts = c;
  165. }
  166. return c;
  167. }
  168. /*!
  169. * \brief Remove the contact from lists in memory
  170. * \param _r record this contact belongs to
  171. * \param _c removed contact
  172. */
  173. void mem_remove_ucontact(urecord_t* _r, ucontact_t* _c)
  174. {
  175. if (_c->prev) {
  176. _c->prev->next = _c->next;
  177. if (_c->next) {
  178. _c->next->prev = _c->prev;
  179. }
  180. } else {
  181. _r->contacts = _c->next;
  182. if (_c->next) {
  183. _c->next->prev = 0;
  184. }
  185. }
  186. }
  187. /*!
  188. * \brief Remove contact in memory from the list and delete it
  189. * \param _r record this contact belongs to
  190. * \param _c deleted contact
  191. */
  192. void mem_delete_ucontact(urecord_t* _r, ucontact_t* _c)
  193. {
  194. mem_remove_ucontact(_r, _c);
  195. if_update_stat( _r->slot, _r->slot->d->contacts, -1);
  196. free_ucontact(_c);
  197. }
  198. static inline int is_valid_tcpconn(ucontact_t *c)
  199. {
  200. if (c->tcpconn_id == -1)
  201. return 0; /* tcpconn_id is not present */
  202. else
  203. return 1; /* valid tcpconn_id */
  204. }
  205. static inline int is_tcp_alive(ucontact_t *c)
  206. {
  207. struct tcp_connection *con = NULL;
  208. int rc = 0;
  209. if ((con = tcpconn_get(c->tcpconn_id, 0, 0, 0, 0))) {
  210. tcpconn_put(con); /* refcnt-- */
  211. rc = 1;
  212. }
  213. return rc;
  214. }
  215. /*!
  216. * \brief Expires timer for NO_DB db_mode
  217. *
  218. * Expires timer for NO_DB db_mode, process all contacts from
  219. * the record, delete the expired ones from memory.
  220. * \param _r processed record
  221. */
  222. static inline void nodb_timer(urecord_t* _r)
  223. {
  224. ucontact_t* ptr, *t;
  225. ptr = _r->contacts;
  226. while(ptr) {
  227. if (handle_lost_tcp && is_valid_tcpconn(ptr) && !is_tcp_alive(ptr)) {
  228. LM_DBG("tcp connection has been lost, expiring contact %.*s\n", ptr->c.len, ptr->c.s);
  229. ptr->expires = UL_EXPIRED_TIME;
  230. }
  231. if (!VALID_CONTACT(ptr, act_time)) {
  232. /* run callbacks for EXPIRE event */
  233. if (exists_ulcb_type(UL_CONTACT_EXPIRE))
  234. run_ul_callbacks( UL_CONTACT_EXPIRE, ptr);
  235. LM_DBG("Binding '%.*s','%.*s' has expired\n",
  236. ptr->aor->len, ZSW(ptr->aor->s),
  237. ptr->c.len, ZSW(ptr->c.s));
  238. t = ptr;
  239. ptr = ptr->next;
  240. mem_delete_ucontact(_r, t);
  241. update_stat( _r->slot->d->expires, 1);
  242. } else {
  243. ptr = ptr->next;
  244. }
  245. }
  246. }
  247. /*!
  248. * \brief Write through timer, used for WRITE_THROUGH db_mode
  249. *
  250. * Write through timer, used for WRITE_THROUGH db_mode. Process all
  251. * contacts from the record, delete all expired ones from the DB.
  252. * \param _r processed record
  253. * \note currently unused, this mode is also handled by the wb_timer
  254. */
  255. static inline void wt_timer(urecord_t* _r)
  256. {
  257. ucontact_t* ptr, *t;
  258. ptr = _r->contacts;
  259. while(ptr) {
  260. if (!VALID_CONTACT(ptr, act_time)) {
  261. /* run callbacks for EXPIRE event */
  262. if (exists_ulcb_type(UL_CONTACT_EXPIRE)) {
  263. run_ul_callbacks( UL_CONTACT_EXPIRE, ptr);
  264. }
  265. LM_DBG("Binding '%.*s','%.*s' has expired\n",
  266. ptr->aor->len, ZSW(ptr->aor->s),
  267. ptr->c.len, ZSW(ptr->c.s));
  268. t = ptr;
  269. ptr = ptr->next;
  270. if (db_delete_ucontact(t) < 0) {
  271. LM_ERR("deleting contact from database failed\n");
  272. }
  273. mem_delete_ucontact(_r, t);
  274. update_stat( _r->slot->d->expires, 1);
  275. } else {
  276. ptr = ptr->next;
  277. }
  278. }
  279. }
  280. /*!
  281. * \brief Write-back timer, used for WRITE_BACK db_mode
  282. *
  283. * Write-back timer, used for WRITE_BACK db_mode. Process
  284. * all contacts from the record, delete expired ones from the DB.
  285. * Furthermore it updates changed contacts, and also insert new
  286. * ones in the DB.
  287. * \param _r processed record
  288. */
  289. static inline void wb_timer(urecord_t* _r)
  290. {
  291. ucontact_t* ptr, *t;
  292. cstate_t old_state;
  293. int op;
  294. int res;
  295. ptr = _r->contacts;
  296. while(ptr) {
  297. if (handle_lost_tcp && is_valid_tcpconn(ptr) && !is_tcp_alive(ptr)) {
  298. LM_DBG("tcp connection has been lost, expiring contact %.*s\n", ptr->c.len, ptr->c.s);
  299. ptr->expires = UL_EXPIRED_TIME;
  300. }
  301. if (!VALID_CONTACT(ptr, act_time)) {
  302. /* run callbacks for EXPIRE event */
  303. if (exists_ulcb_type(UL_CONTACT_EXPIRE)) {
  304. run_ul_callbacks( UL_CONTACT_EXPIRE, ptr);
  305. }
  306. LM_DBG("Binding '%.*s','%.*s' has expired\n",
  307. ptr->aor->len, ZSW(ptr->aor->s),
  308. ptr->c.len, ZSW(ptr->c.s));
  309. update_stat( _r->slot->d->expires, 1);
  310. t = ptr;
  311. ptr = ptr->next;
  312. /* Should we remove the contact from the database ? */
  313. if (st_expired_ucontact(t) == 1) {
  314. if (db_delete_ucontact(t) < 0) {
  315. LM_ERR("failed to delete contact from the database"
  316. " (aor: %.*s)\n",
  317. t->aor->len, ZSW(t->aor->s));
  318. }
  319. }
  320. mem_delete_ucontact(_r, t);
  321. } else {
  322. /* Determine the operation we have to do */
  323. old_state = ptr->state;
  324. op = st_flush_ucontact(ptr);
  325. switch(op) {
  326. case 0: /* do nothing, contact is synchronized */
  327. break;
  328. case 1: /* insert */
  329. if (db_insert_ucontact(ptr) < 0) {
  330. LM_ERR("inserting contact into database failed"
  331. " (aor: %.*s)\n",
  332. ptr->aor->len, ZSW(ptr->aor->s));
  333. ptr->state = old_state;
  334. }
  335. break;
  336. case 2: /* update */
  337. if (ul_db_update_as_insert)
  338. res = db_insert_ucontact(ptr);
  339. else
  340. res = db_update_ucontact(ptr);
  341. if (res < 0) {
  342. LM_ERR("updating contact in db failed (aor: %.*s)\n",
  343. ptr->aor->len, ZSW(ptr->aor->s));
  344. ptr->state = old_state;
  345. }
  346. break;
  347. }
  348. ptr = ptr->next;
  349. }
  350. }
  351. }
  352. /*!
  353. * \brief Run timer functions depending on the db_mode setting.
  354. *
  355. * Helper function that run the appropriate timer function, depending
  356. * on the db_mode setting.
  357. * \param _r processed record
  358. */
  359. void timer_urecord(urecord_t* _r)
  360. {
  361. switch(db_mode) {
  362. case DB_READONLY:
  363. case NO_DB: nodb_timer(_r);
  364. break;
  365. /* use also the write_back timer routine to handle the failed
  366. * realtime inserts/updates */
  367. case WRITE_THROUGH: wb_timer(_r); /*wt_timer(_r);*/
  368. break;
  369. case WRITE_BACK: wb_timer(_r);
  370. break;
  371. }
  372. }
  373. /*!
  374. * \brief Delete a record from the database
  375. * \param _r deleted record
  376. * \return 0 on success, -1 on failure
  377. */
  378. int db_delete_urecord(urecord_t* _r)
  379. {
  380. db_key_t keys[2];
  381. db_val_t vals[2];
  382. char* dom;
  383. keys[0] = &user_col;
  384. keys[1] = &domain_col;
  385. vals[0].type = DB1_STR;
  386. vals[0].nul = 0;
  387. vals[0].val.str_val.s = _r->aor.s;
  388. vals[0].val.str_val.len = _r->aor.len;
  389. if (use_domain) {
  390. dom = memchr(_r->aor.s, '@', _r->aor.len);
  391. vals[0].val.str_val.len = dom - _r->aor.s;
  392. vals[1].type = DB1_STR;
  393. vals[1].nul = 0;
  394. vals[1].val.str_val.s = dom + 1;
  395. vals[1].val.str_val.len = _r->aor.s + _r->aor.len - dom - 1;
  396. }
  397. if (ul_dbf.use_table(ul_dbh, _r->domain) < 0) {
  398. LM_ERR("use_table failed\n");
  399. return -1;
  400. }
  401. if (ul_dbf.delete(ul_dbh, keys, 0, vals, (use_domain) ? (2) : (1)) < 0) {
  402. LM_ERR("failed to delete from database\n");
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. /*!
  408. * \brief Delete a record from the database based on ruid
  409. * \return 0 on success, -1 on failure
  410. */
  411. int db_delete_urecord_by_ruid(str *_table, str *_ruid)
  412. {
  413. db_key_t keys[1];
  414. db_val_t vals[1];
  415. keys[0] = &ruid_col;
  416. vals[0].type = DB1_STR;
  417. vals[0].nul = 0;
  418. vals[0].val.str_val.s = _ruid->s;
  419. vals[0].val.str_val.len = _ruid->len;
  420. if (ul_dbf.use_table(ul_dbh, _table) < 0) {
  421. LM_ERR("use_table failed\n");
  422. return -1;
  423. }
  424. if (ul_dbf.delete(ul_dbh, keys, 0, vals, 1) < 0) {
  425. LM_ERR("failed to delete from database\n");
  426. return -1;
  427. }
  428. if (ul_dbf.affected_rows(ul_dbh) == 0) {
  429. return -2;
  430. }
  431. return 0;
  432. }
  433. /*!
  434. * \brief Release urecord previously obtained through get_urecord
  435. * \warning Failing to calls this function after get_urecord will
  436. * result in a memory leak when the DB_ONLY mode is used. When
  437. * the records is later deleted, e.g. with delete_urecord, then
  438. * its not necessary, as this function already releases the record.
  439. * \param _r released record
  440. */
  441. void release_urecord(urecord_t* _r)
  442. {
  443. if (db_mode==DB_ONLY) {
  444. free_urecord(_r);
  445. } else if (_r->contacts == 0) {
  446. mem_delete_urecord(_r->slot->d, _r);
  447. }
  448. }
  449. /*!
  450. * \brief Create and insert new contact into urecord
  451. * \param _r record into the new contact should be inserted
  452. * \param _contact contact string
  453. * \param _ci contact information
  454. * \param _c new created contact
  455. * \return 0 on success, -1 on failure
  456. */
  457. int insert_ucontact(urecord_t* _r, str* _contact, ucontact_info_t* _ci,
  458. ucontact_t** _c)
  459. {
  460. if ( ((*_c)=mem_insert_ucontact(_r, _contact, _ci)) == 0) {
  461. LM_ERR("failed to insert contact\n");
  462. return -1;
  463. }
  464. if (exists_ulcb_type(UL_CONTACT_INSERT)) {
  465. run_ul_callbacks( UL_CONTACT_INSERT, *_c);
  466. }
  467. if (db_mode == WRITE_THROUGH || db_mode==DB_ONLY) {
  468. if (db_insert_ucontact(*_c) < 0) {
  469. LM_ERR("failed to insert in database\n");
  470. return -1;
  471. } else {
  472. (*_c)->state = CS_SYNC;
  473. }
  474. }
  475. return 0;
  476. }
  477. /*!
  478. * \brief Delete ucontact from urecord
  479. * \param _r record where the contact belongs to
  480. * \param _c deleted contact
  481. * \return 0 on success, -1 on failure
  482. */
  483. int delete_ucontact(urecord_t* _r, struct ucontact* _c)
  484. {
  485. int ret = 0;
  486. if (exists_ulcb_type(UL_CONTACT_DELETE)) {
  487. run_ul_callbacks( UL_CONTACT_DELETE, _c);
  488. }
  489. if (st_delete_ucontact(_c) > 0) {
  490. if (db_mode == WRITE_THROUGH || db_mode==DB_ONLY) {
  491. if (db_delete_ucontact(_c) < 0) {
  492. LM_ERR("failed to remove contact from database\n");
  493. ret = -1;
  494. }
  495. }
  496. mem_delete_ucontact(_r, _c);
  497. }
  498. return ret;
  499. }
  500. int delete_urecord_by_ruid(udomain_t* _d, str *_ruid)
  501. {
  502. if (db_mode != DB_ONLY) {
  503. LM_ERR("delete_urecord_by_ruid currently available only in db_mode=3\n");
  504. return -1;
  505. }
  506. return db_delete_urecord_by_ruid(_d->name, _ruid);
  507. }
  508. /*!
  509. * \brief Match a contact record to a contact string
  510. * \param ptr contact record
  511. * \param _c contact string
  512. * \return ptr on successfull match, 0 when they not match
  513. */
  514. static inline struct ucontact* contact_match( ucontact_t* ptr, str* _c)
  515. {
  516. while(ptr) {
  517. if ((_c->len == ptr->c.len) && !memcmp(_c->s, ptr->c.s, _c->len)) {
  518. return ptr;
  519. }
  520. ptr = ptr->next;
  521. }
  522. return 0;
  523. }
  524. /*!
  525. * \brief Match a contact record to a contact string and callid
  526. * \param ptr contact record
  527. * \param _c contact string
  528. * \param _callid callid
  529. * \return ptr on successfull match, 0 when they not match
  530. */
  531. static inline struct ucontact* contact_callid_match( ucontact_t* ptr,
  532. str* _c, str *_callid)
  533. {
  534. while(ptr) {
  535. if ( (_c->len==ptr->c.len) && (_callid->len==ptr->callid.len)
  536. && !memcmp(_c->s, ptr->c.s, _c->len)
  537. && !memcmp(_callid->s, ptr->callid.s, _callid->len)
  538. ) {
  539. return ptr;
  540. }
  541. ptr = ptr->next;
  542. }
  543. return 0;
  544. }
  545. /*!
  546. + * \brief Match a contact record to a contact string and path
  547. + * \param ptr contact record
  548. + * \param _c contact string
  549. + * \param _path path
  550. + * \return ptr on successfull match, 0 when they not match
  551. + */
  552. static inline struct ucontact* contact_path_match( ucontact_t* ptr, str* _c, str *_path)
  553. {
  554. /* if no path is preset (in REGISTER request) or use_path is not configured
  555. in registrar module, default to contact_match() */
  556. if( _path == NULL) return contact_match(ptr, _c);
  557. while(ptr) {
  558. if ( (_c->len==ptr->c.len) && (_path->len==ptr->path.len)
  559. && !memcmp(_c->s, ptr->c.s, _c->len)
  560. && !memcmp(_path->s, ptr->path.s, _path->len)
  561. ) {
  562. return ptr;
  563. }
  564. ptr = ptr->next;
  565. }
  566. return 0;
  567. }
  568. /*!
  569. * \brief Get pointer to ucontact with given contact
  570. * \param _r record where to search the contacts
  571. * \param _c contact string
  572. * \param _callid callid
  573. * \param _path path
  574. * \param _cseq CSEQ number
  575. * \param _co found contact
  576. * \return 0 - found, 1 - not found, -1 - invalid found,
  577. * -2 - found, but to be skipped (same cseq)
  578. */
  579. int get_ucontact(urecord_t* _r, str* _c, str* _callid, str* _path, int _cseq,
  580. struct ucontact** _co)
  581. {
  582. ucontact_t* ptr;
  583. int no_callid;
  584. ptr = 0;
  585. no_callid = 0;
  586. *_co = 0;
  587. switch (matching_mode) {
  588. case CONTACT_ONLY:
  589. ptr = contact_match( _r->contacts, _c);
  590. break;
  591. case CONTACT_CALLID:
  592. ptr = contact_callid_match( _r->contacts, _c, _callid);
  593. no_callid = 1;
  594. break;
  595. case CONTACT_PATH:
  596. ptr = contact_path_match( _r->contacts, _c, _path);
  597. break;
  598. default:
  599. LM_CRIT("unknown matching_mode %d\n", matching_mode);
  600. return -1;
  601. }
  602. if (ptr) {
  603. /* found -> check callid and cseq */
  604. if ( no_callid || (ptr->callid.len==_callid->len
  605. && memcmp(_callid->s, ptr->callid.s, _callid->len)==0 ) ) {
  606. if (_cseq<ptr->cseq)
  607. return -1;
  608. if (_cseq==ptr->cseq) {
  609. get_act_time();
  610. return (ptr->last_modified+cseq_delay>act_time)?-2:-1;
  611. }
  612. }
  613. *_co = ptr;
  614. return 0;
  615. }
  616. return 1;
  617. }
  618. /*
  619. * Get pointer to ucontact with given info (by address or sip.instance)
  620. */
  621. int get_ucontact_by_instance(urecord_t* _r, str* _c, ucontact_info_t* _ci,
  622. ucontact_t** _co)
  623. {
  624. ucontact_t* ptr;
  625. str i1;
  626. str i2;
  627. if (_ci->instance.s == NULL || _ci->instance.len <= 0) {
  628. return get_ucontact(_r, _c, _ci->callid, _ci->path, _ci->cseq, _co);
  629. }
  630. /* find by instance */
  631. ptr = _r->contacts;
  632. while(ptr) {
  633. if (ptr->instance.len>0 && _ci->reg_id==ptr->reg_id)
  634. {
  635. i1 = _ci->instance;
  636. i2 = ptr->instance;
  637. if(i1.s[0]=='<' && i1.s[i1.len-1]=='>') {
  638. i1.s++;
  639. i1.len-=2;
  640. }
  641. if(i2.s[0]=='<' && i2.s[i2.len-1]=='>') {
  642. i2.s++;
  643. i2.len-=2;
  644. }
  645. if(i1.len==i2.len && memcmp(i1.s, i2.s, i2.len)==0) {
  646. *_co = ptr;
  647. return 0;
  648. }
  649. }
  650. ptr = ptr->next;
  651. }
  652. return 1;
  653. }
  654. unsigned int ul_get_aorhash(str *_aor)
  655. {
  656. return core_hash(_aor, 0, 0);
  657. }