ul_mi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * $Id: ul_mi.c 5194 2008-11-13 10:38:11Z henningw $
  3. *
  4. * Copyright (C) 2006 Voice Sistem SRL
  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. *
  25. * 2006-12-01 created (bogdan)
  26. */
  27. /*! \file
  28. * \brief USRLOC - Usrloc MI functions
  29. * \ingroup usrloc
  30. *
  31. * - Module: \ref usrloc
  32. */
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include "../../dprint.h"
  36. #include "../../ut.h"
  37. #include "../../qvalue.h"
  38. #include "../../ip_addr.h"
  39. #include "ul_mi.h"
  40. #include "dlist.h"
  41. #include "udomain.h"
  42. #include "utime.h"
  43. #include "p_usrloc_mod.h"
  44. /*! CSEQ nr used */
  45. #define MI_UL_CSEQ 1
  46. /*! call-id used for ul_add and ul_rm_contact */
  47. static str mi_ul_cid = str_init("[email protected]");
  48. /*! user agent used for ul_add */
  49. static str mi_ul_ua = str_init("Kamailio MI Server");
  50. static str mi_ul_path = str_init("dummypath");
  51. /************************ helper functions ****************************/
  52. /*!
  53. * \brief Search a domain in the global domain list
  54. * \param table domain (table) name
  55. * \return pointer to domain if found, 0 if not found
  56. */
  57. static inline udomain_t* mi_find_domain(str* table)
  58. {
  59. LM_ERR("not available in sp-ul_db mode");
  60. return 0;
  61. }
  62. /*!
  63. * \brief Convert address of record
  64. *
  65. * Convert an address of record string to lower case, and truncate
  66. * it when use_domain is not set.
  67. * \param aor address of record
  68. * \return 0 on success, -1 on error
  69. */
  70. static inline int mi_fix_aor(str *aor)
  71. {
  72. char *p;
  73. p = memchr( aor->s, '@', aor->len);
  74. if (use_domain) {
  75. if (p==NULL)
  76. return -1;
  77. } else {
  78. if (p)
  79. aor->len = p - aor->s;
  80. }
  81. strlower(aor);
  82. return 0;
  83. }
  84. /*!
  85. * \brief Add a node for a address of record
  86. * \param parent parent node
  87. * \param r printed record
  88. * \param t actual time
  89. * \param short_dump 0 means that all informations will be included, 1 that only the AOR is printed
  90. * \return 0 on success, -1 on failure
  91. */
  92. static inline int mi_add_aor_node(struct mi_node *parent, urecord_t* r, time_t t, int short_dump)
  93. {
  94. struct mi_node *anode, *cnode, *node;
  95. struct mi_attr *attr;
  96. ucontact_t* c;
  97. char *p;
  98. int len;
  99. anode = add_mi_node_child( parent, MI_DUP_VALUE, "AOR", 3,
  100. r->aor.s, r->aor.len);
  101. if (anode==0)
  102. return -1;
  103. if (short_dump)
  104. return 0;
  105. #if 0
  106. /* aor hash */
  107. p = int2str((unsigned long)r->aorhash, &len);
  108. node = add_mi_node_child( anode, MI_DUP_VALUE, "HashID", 6, p, len);
  109. if (node==0)
  110. return -1;
  111. #endif
  112. for( c=r->contacts ; c ; c=c->next) {
  113. /* contact */
  114. cnode = add_mi_node_child( anode, MI_DUP_VALUE, "Contact", 7,
  115. c->c.s, c->c.len);
  116. if (cnode==0)
  117. return -1;
  118. /* expires */
  119. if (c->expires == 0) {
  120. node = add_mi_node_child( cnode, 0, "Expires", 7, "permanent", 9);
  121. } else if (c->expires == UL_EXPIRED_TIME) {
  122. node = add_mi_node_child( cnode, 0, "Expires", 7, "deleted", 7);
  123. } else if (t > c->expires) {
  124. node = add_mi_node_child( cnode, 0, "Expires", 7, "expired", 7);
  125. } else {
  126. p = int2str((unsigned long)(c->expires - t), &len);
  127. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Expires", 7,p,len);
  128. }
  129. if (node==0)
  130. return -1;
  131. /* q */
  132. p = q2str(c->q, (unsigned int*)&len);
  133. attr = add_mi_attr( cnode, MI_DUP_VALUE, "Q", 1, p, len);
  134. if (attr==0)
  135. return -1;
  136. /* callid */
  137. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Callid", 6,
  138. c->callid.s, c->callid.len);
  139. if (node==0)
  140. return -1;
  141. /* cseq */
  142. p = int2str((unsigned long)c->cseq, &len);
  143. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Cseq", 4, p, len);
  144. if (node==0)
  145. return -1;
  146. /* User-Agent */
  147. if (c->user_agent.len) {
  148. node = add_mi_node_child( cnode, MI_DUP_VALUE, "User-agent", 10,
  149. c->user_agent.s, c->user_agent.len);
  150. if (node==0)
  151. return -1;
  152. }
  153. /* received */
  154. if (c->received.len) {
  155. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Received", 8,
  156. c->received.s, c->received.len);
  157. if (node==0)
  158. return -1;
  159. }
  160. /* path */
  161. if (c->path.len) {
  162. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Path", 4,
  163. c->path.s, c->path.len);
  164. if (node==0)
  165. return -1;
  166. }
  167. /* state */
  168. if (c->state == CS_NEW) {
  169. node = add_mi_node_child( cnode, 0, "State", 5, "CS_NEW", 6);
  170. } else if (c->state == CS_SYNC) {
  171. node = add_mi_node_child( cnode, 0, "State", 5, "CS_SYNC", 7);
  172. } else if (c->state== CS_DIRTY) {
  173. node = add_mi_node_child( cnode, 0, "State", 5, "CS_DIRTY", 8);
  174. } else {
  175. node = add_mi_node_child( cnode, 0, "State", 5, "CS_UNKNOWN", 10);
  176. }
  177. if (node==0)
  178. return -1;
  179. /* flags */
  180. p = int2str((unsigned long)c->flags, &len);
  181. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Flags", 5, p, len);
  182. if (node==0)
  183. return -1;
  184. /* cflags */
  185. p = int2str((unsigned long)c->cflags, &len);
  186. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Cflags", 5, p, len);
  187. if (node==0)
  188. return -1;
  189. /* socket */
  190. if (c->sock) {
  191. node = add_mi_node_child( cnode, 0, "Socket", 6,
  192. c->sock->sock_str.s, c->sock->sock_str.len);
  193. if (node==0)
  194. return -1;
  195. }
  196. /* methods */
  197. p = int2str((unsigned long)c->methods, &len);
  198. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Methods", 7, p, len);
  199. if (node==0)
  200. return -1;
  201. /* ruid */
  202. if (c->ruid.len) {
  203. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Ruid", 4,
  204. c->ruid.s, c->ruid.len);
  205. if (node==0)
  206. return -1;
  207. }
  208. /* instance */
  209. if (c->instance.len) {
  210. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Instance", 8,
  211. c->instance.s, c->instance.len);
  212. if (node==0)
  213. return -1;
  214. }
  215. /* reg-id */
  216. p = int2str((unsigned long)c->reg_id, &len);
  217. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Reg-Id", 6, p, len);
  218. if (node==0)
  219. return -1;
  220. } /* for */
  221. return 0;
  222. }
  223. /*************************** MI functions *****************************/
  224. /*!
  225. * \brief Delete a address of record including its contacts
  226. * \param cmd mi_root containing the parameter
  227. * \param param not used
  228. * \note expects 2 nodes: the table name and the AOR
  229. * \return mi_root with the result
  230. */
  231. struct mi_root* mi_usrloc_rm_aor(struct mi_root *cmd, void *param)
  232. {
  233. struct mi_node *node;
  234. udomain_t *dom;
  235. str *aor;
  236. node = cmd->node.kids;
  237. if (node==NULL || node->next==NULL || node->next->next!=NULL)
  238. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  239. /* look for table */
  240. dom = mi_find_domain( &node->value );
  241. if (dom==NULL)
  242. return init_mi_tree( 404, "Table not found", 15);
  243. /* process the aor */
  244. aor = &node->next->value;
  245. if ( mi_fix_aor(aor)!=0 )
  246. return init_mi_tree( 400, "Domain missing in AOR", 21);
  247. lock_udomain( dom, aor);
  248. if (delete_urecord( dom, aor, 0) < 0) {
  249. unlock_udomain( dom, aor);
  250. return init_mi_tree( 500, "Failed to delete AOR", 20);
  251. }
  252. unlock_udomain( dom, aor);
  253. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  254. }
  255. /*!
  256. * \brief Delete a contact from an AOR record
  257. * \param cmd mi_root containing the parameter
  258. * \param param not used
  259. * \note expects 3 nodes: the table name, the AOR and contact
  260. * \return mi_root with the result or 0 on failure
  261. */
  262. struct mi_root* mi_usrloc_rm_contact(struct mi_root *cmd, void *param)
  263. {
  264. struct mi_node *node;
  265. udomain_t *dom;
  266. urecord_t *rec;
  267. ucontact_t* con;
  268. str *aor, *contact;
  269. int ret;
  270. node = cmd->node.kids;
  271. if (node==NULL || node->next==NULL || node->next->next==NULL ||
  272. node->next->next->next!=NULL)
  273. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  274. /* look for table */
  275. dom = mi_find_domain( &node->value );
  276. if (dom==NULL)
  277. return init_mi_tree( 404, "Table not found", 15);
  278. /* process the aor */
  279. aor = &node->next->value;
  280. if ( mi_fix_aor(aor)!=0 )
  281. return init_mi_tree( 400, "Domain missing in AOR", 21);
  282. lock_udomain( dom, aor);
  283. ret = get_urecord( dom, aor, &rec);
  284. if (ret == 1) {
  285. unlock_udomain( dom, aor);
  286. return init_mi_tree( 404, "AOR not found", 13);
  287. }
  288. contact = &node->next->next->value;
  289. ret = get_ucontact( rec, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ+1, &con);
  290. if (ret < 0) {
  291. unlock_udomain( dom, aor);
  292. return 0;
  293. }
  294. if (ret > 0) {
  295. unlock_udomain( dom, aor);
  296. return init_mi_tree( 404, "Contact not found", 17);
  297. }
  298. if (delete_ucontact(rec, con) < 0) {
  299. unlock_udomain( dom, aor);
  300. return 0;
  301. }
  302. release_urecord(rec);
  303. unlock_udomain( dom, aor);
  304. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  305. }
  306. /*!
  307. * \brief Dump the content of the usrloc
  308. * \param cmd mi_root containing the parameter
  309. * \param param not used
  310. * \return mi_root with the result or 0 on failure
  311. */
  312. struct mi_root* mi_usrloc_dump(struct mi_root *cmd, void *param)
  313. {
  314. LM_ERR("not available in sp-ul_db mode");
  315. return 0;
  316. }
  317. /*!
  318. * \brief Flush the usrloc memory cache to DB
  319. * \param cmd mi_root containing the parameter
  320. * \param param not used
  321. * \return mi_root with the result or 0 on failure
  322. */
  323. struct mi_root* mi_usrloc_flush(struct mi_root *cmd, void *param)
  324. {
  325. struct mi_root *rpl_tree;
  326. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  327. if (rpl_tree==NULL)
  328. return 0;
  329. synchronize_all_udomains();
  330. return rpl_tree;
  331. }
  332. /*!
  333. * \brief Add a new contact for an address of record
  334. * \param cmd mi_root containing the parameter
  335. * \param param not used
  336. * \note Expects 7 nodes: table name, AOR, contact, expires, Q,
  337. * useless - backward compatible, flags, cflags, methods
  338. * \return mi_root with the result
  339. */
  340. struct mi_root* mi_usrloc_add(struct mi_root *cmd, void *param)
  341. {
  342. ucontact_info_t ci;
  343. urecord_t* r;
  344. ucontact_t* c;
  345. struct mi_node *node;
  346. udomain_t *dom;
  347. str *aor, *contact;
  348. unsigned int ui_val;
  349. int n;
  350. for( n=0,node = cmd->node.kids; n<9 && node ; n++,node=node->next );
  351. if (n!=9 || node!=0)
  352. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  353. node = cmd->node.kids;
  354. /* look for table (param 1) */
  355. dom = mi_find_domain( &node->value );
  356. if (dom==NULL)
  357. return init_mi_tree( 404, "Table not found", 15);
  358. /* process the aor (param 2) */
  359. node = node->next;
  360. aor = &node->value;
  361. if ( mi_fix_aor(aor)!=0 )
  362. return init_mi_tree( 400, "Domain missing in AOR", 21);
  363. /* contact (param 3) */
  364. node = node->next;
  365. contact = &node->value;
  366. memset( &ci, 0, sizeof(ucontact_info_t));
  367. /* expire (param 4) */
  368. node = node->next;
  369. if (str2int( &node->value, &ui_val) < 0)
  370. goto bad_syntax;
  371. ci.expires = ui_val;
  372. /* q value (param 5) */
  373. node = node->next;
  374. if (str2q( &ci.q, node->value.s, node->value.len) < 0)
  375. goto bad_syntax;
  376. /* unused value (param 6) FIXME */
  377. node = node->next;
  378. /* flags value (param 7) */
  379. node = node->next;
  380. if (str2int( &node->value, (unsigned int*)&ci.flags) < 0)
  381. goto bad_syntax;
  382. /* branch flags value (param 8) */
  383. node = node->next;
  384. if (str2int( &node->value, (unsigned int*)&ci.cflags) < 0)
  385. goto bad_syntax;
  386. /* methods value (param 9) */
  387. node = node->next;
  388. if (str2int( &node->value, (unsigned int*)&ci.methods) < 0)
  389. goto bad_syntax;
  390. lock_udomain( dom, aor);
  391. n = get_urecord( dom, aor, &r);
  392. if ( n==1) {
  393. if (insert_urecord( dom, aor, &r) < 0)
  394. goto lock_error;
  395. c = 0;
  396. } else {
  397. if (get_ucontact( r, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ+1, &c) < 0)
  398. goto lock_error;
  399. }
  400. get_act_time();
  401. ci.callid = &mi_ul_cid;
  402. ci.user_agent = &mi_ul_ua;
  403. ci.cseq = MI_UL_CSEQ;
  404. /* 0 expires means permanent contact */
  405. if (ci.expires!=0)
  406. ci.expires += act_time;
  407. if (c) {
  408. if (update_ucontact( r, c, &ci) < 0)
  409. goto release_error;
  410. } else {
  411. if ( insert_ucontact( r, contact, &ci, &c) < 0 )
  412. goto release_error;
  413. }
  414. release_urecord(r);
  415. unlock_udomain( dom, aor);
  416. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  417. bad_syntax:
  418. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  419. release_error:
  420. release_urecord(r);
  421. lock_error:
  422. unlock_udomain( dom, aor);
  423. return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
  424. }
  425. /*!
  426. * \brief Dumps the contacts of an AOR
  427. * \param cmd mi_root containing the parameter
  428. * \param param not used
  429. * \note expects 2 nodes: the table name and the AOR
  430. * \return mi_root with the result or 0 on failure
  431. */
  432. struct mi_root* mi_usrloc_show_contact(struct mi_root *cmd, void *param)
  433. {
  434. struct mi_root *rpl_tree;
  435. struct mi_node *rpl, *node;
  436. udomain_t *dom;
  437. urecord_t *rec;
  438. ucontact_t* con;
  439. str *aor;
  440. int ret;
  441. node = cmd->node.kids;
  442. if (node==NULL || node->next==NULL || node->next->next!=NULL)
  443. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  444. /* look for table */
  445. dom = mi_find_domain( &node->value );
  446. if (dom==NULL)
  447. return init_mi_tree( 404, "Table not found", 15);
  448. /* process the aor */
  449. aor = &node->next->value;
  450. if ( mi_fix_aor(aor)!=0 )
  451. return init_mi_tree( 400, "Domain missing in AOR", 21);
  452. lock_udomain( dom, aor);
  453. ret = get_urecord( dom, aor, &rec);
  454. if (ret == 1) {
  455. unlock_udomain( dom, aor);
  456. return init_mi_tree( 404, "AOR not found", 13);
  457. }
  458. get_act_time();
  459. rpl_tree = 0;
  460. rpl = 0;
  461. for( con=rec->contacts ; con ; con=con->next) {
  462. if (VALID_CONTACT( con, act_time)) {
  463. if (rpl_tree==0) {
  464. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  465. if (rpl_tree==0)
  466. goto error;
  467. rpl = &rpl_tree->node;
  468. }
  469. node = addf_mi_node_child( rpl, 0, "Contact", 7,
  470. "<%.*s>;q=%s;expires=%d;flags=0x%X;cflags=0x%X;socket=<%.*s>;"
  471. "methods=0x%X"
  472. "%s%.*s%s" /*received*/
  473. "%s%.*s%s" /*user-agent*/
  474. "%s%.*s%s", /*path*/
  475. con->c.len, ZSW(con->c.s),
  476. q2str(con->q, 0), (int)(con->expires - act_time),
  477. con->flags, con->cflags,
  478. con->sock?con->sock->sock_str.len:3,
  479. con->sock?con->sock->sock_str.s:"NULL",
  480. con->methods,
  481. con->received.len?";received=<":"",con->received.len,
  482. ZSW(con->received.s), con->received.len?">":"",
  483. con->user_agent.len?";user_agent=<":"",con->user_agent.len,
  484. ZSW(con->user_agent.s), con->user_agent.len?">":"",
  485. con->path.len?";path=<":"", con->path.len,
  486. ZSW(con->path.s), con->path.len?">":""
  487. );
  488. if (node==0)
  489. goto error;
  490. }
  491. }
  492. unlock_udomain( dom, aor);
  493. if (rpl_tree==0)
  494. return init_mi_tree( 404 , "AOR has no contacts", 18);
  495. return rpl_tree;
  496. error:
  497. if (rpl_tree)
  498. free_mi_tree( rpl_tree );
  499. unlock_udomain( dom, aor);
  500. return 0;
  501. }