ul_mi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * $Id$
  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 "../../lib/kmi/mi.h"
  36. #include "../../lib/srutils/sruid.h"
  37. #include "../../dprint.h"
  38. #include "../../ut.h"
  39. #include "../../qvalue.h"
  40. #include "../../ip_addr.h"
  41. #include "ul_mi.h"
  42. #include "dlist.h"
  43. #include "udomain.h"
  44. #include "utime.h"
  45. #include "ul_mod.h"
  46. #include "usrloc.h"
  47. /*! CSEQ nr used */
  48. static int MI_UL_CSEQ = 0;
  49. /*! call-id used for ul_add and ul_rm_contact */
  50. static str mi_ul_cid = {0,0}; /* str_init("[email protected]");*/
  51. /*! user agent used for ul_add */
  52. static str mi_ul_ua = str_init("SIP Router MI Server");
  53. /*! path used for ul_add and ul_rm_contact */
  54. static str mi_ul_path = str_init("dummypath");
  55. extern sruid_t _ul_sruid;
  56. static char mi_ul_cid_buf[32];
  57. /************************ helper functions ****************************/
  58. static void set_mi_ul_cid(void)
  59. {
  60. int i;
  61. char charset[] = "0123456789"
  62. "abcdefghijklmnopqrstuvwxyz"
  63. "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  64. if(mi_ul_cid.s!=NULL) return;
  65. for(i=0; i<19; i++) {
  66. mi_ul_cid_buf[i] = charset[rand()%(sizeof(charset) - 1)];
  67. }
  68. memcpy(mi_ul_cid_buf+i, "@kamailio.mi", sizeof("@kamailio.mi"));
  69. mi_ul_cid.s = mi_ul_cid_buf;
  70. mi_ul_cid.len = strlen(mi_ul_cid.s);
  71. }
  72. /*!
  73. * \brief Search a domain in the global domain list
  74. * \param table domain (table) name
  75. * \return pointer to domain if found, 0 if not found
  76. */
  77. static inline udomain_t* mi_find_domain(str* table)
  78. {
  79. dlist_t* dom;
  80. for( dom=root ; dom ; dom=dom->next ) {
  81. if ((dom->name.len == table->len) &&
  82. !memcmp(dom->name.s, table->s, table->len))
  83. return dom->d;
  84. }
  85. return 0;
  86. }
  87. /*!
  88. * \brief Convert address of record
  89. *
  90. * Convert an address of record string to lower case, and truncate
  91. * it when use_domain is not set.
  92. * \param aor address of record
  93. * \return 0 on success, -1 on error
  94. */
  95. static inline int mi_fix_aor(str *aor)
  96. {
  97. char *p;
  98. p = memchr( aor->s, '@', aor->len);
  99. if (use_domain) {
  100. if (p==NULL)
  101. return -1;
  102. } else {
  103. if (p)
  104. aor->len = p - aor->s;
  105. }
  106. strlower(aor);
  107. return 0;
  108. }
  109. /*!
  110. * \brief Add a node for a address of record
  111. * \param parent parent node
  112. * \param r printed record
  113. * \param t actual time
  114. * \param short_dump 0 means that all informations will be included, 1 that only the AOR is printed
  115. * \return 0 on success, -1 on failure
  116. */
  117. static inline int mi_add_aor_node(struct mi_node *parent, urecord_t* r, time_t t, int short_dump)
  118. {
  119. struct mi_node *anode, *cnode, *node;
  120. struct mi_attr *attr;
  121. ucontact_t* c;
  122. char *p;
  123. int len;
  124. anode = add_mi_node_child( parent, MI_DUP_VALUE, "AOR", 3,
  125. r->aor.s, r->aor.len);
  126. if (anode==0)
  127. return -1;
  128. if (short_dump)
  129. return 0;
  130. #if 0
  131. /* aor hash */
  132. p = int2str((unsigned long)r->aorhash, &len);
  133. node = add_mi_node_child( anode, MI_DUP_VALUE, "HashID", 6, p, len);
  134. if (node==0)
  135. return -1;
  136. #endif
  137. for( c=r->contacts ; c ; c=c->next) {
  138. /* contact */
  139. cnode = add_mi_node_child( anode, MI_DUP_VALUE, "Contact", 7,
  140. c->c.s, c->c.len);
  141. if (cnode==0)
  142. return -1;
  143. /* expires */
  144. if (c->expires == 0) {
  145. node = add_mi_node_child( cnode, 0, "Expires", 7, "permanent", 9);
  146. } else if (c->expires == UL_EXPIRED_TIME) {
  147. node = add_mi_node_child( cnode, 0, "Expires", 7, "deleted", 7);
  148. } else if (t > c->expires) {
  149. node = add_mi_node_child( cnode, 0, "Expires", 7, "expired", 7);
  150. } else {
  151. p = int2str((unsigned long)(c->expires - t), &len);
  152. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Expires", 7,p,len);
  153. }
  154. if (node==0)
  155. return -1;
  156. /* q */
  157. p = q2str(c->q, (unsigned int*)&len);
  158. attr = add_mi_attr( cnode, MI_DUP_VALUE, "Q", 1, p, len);
  159. if (attr==0)
  160. return -1;
  161. /* callid */
  162. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Callid", 6,
  163. c->callid.s, c->callid.len);
  164. if (node==0)
  165. return -1;
  166. /* cseq */
  167. p = int2str((unsigned long)c->cseq, &len);
  168. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Cseq", 4, p, len);
  169. if (node==0)
  170. return -1;
  171. /* User-Agent */
  172. if (c->user_agent.len) {
  173. node = add_mi_node_child( cnode, MI_DUP_VALUE, "User-agent", 10,
  174. c->user_agent.s, c->user_agent.len);
  175. if (node==0)
  176. return -1;
  177. }
  178. /* received */
  179. if (c->received.len) {
  180. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Received", 8,
  181. c->received.s, c->received.len);
  182. if (node==0)
  183. return -1;
  184. }
  185. /* path */
  186. if (c->path.len) {
  187. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Path", 4,
  188. c->path.s, c->path.len);
  189. if (node==0)
  190. return -1;
  191. }
  192. /* state */
  193. if (c->state == CS_NEW) {
  194. node = add_mi_node_child( cnode, 0, "State", 5, "CS_NEW", 6);
  195. } else if (c->state == CS_SYNC) {
  196. node = add_mi_node_child( cnode, 0, "State", 5, "CS_SYNC", 7);
  197. } else if (c->state== CS_DIRTY) {
  198. node = add_mi_node_child( cnode, 0, "State", 5, "CS_DIRTY", 8);
  199. } else {
  200. node = add_mi_node_child( cnode, 0, "State", 5, "CS_UNKNOWN", 10);
  201. }
  202. if (node==0)
  203. return -1;
  204. /* flags */
  205. p = int2str((unsigned long)c->flags, &len);
  206. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Flags", 5, p, len);
  207. if (node==0)
  208. return -1;
  209. /* cflags */
  210. p = int2str((unsigned long)c->cflags, &len);
  211. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Cflags", 5, p, len);
  212. if (node==0)
  213. return -1;
  214. /* socket */
  215. if (c->sock) {
  216. node = add_mi_node_child( cnode, 0, "Socket", 6,
  217. c->sock->sock_str.s, c->sock->sock_str.len);
  218. if (node==0)
  219. return -1;
  220. }
  221. /* methods */
  222. p = int2str((unsigned long)c->methods, &len);
  223. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Methods", 7, p, len);
  224. if (node==0)
  225. return -1;
  226. /* ruid */
  227. if (c->ruid.len) {
  228. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Ruid", 4,
  229. c->ruid.s, c->ruid.len);
  230. if (node==0)
  231. return -1;
  232. }
  233. /* instance */
  234. if (c->instance.len) {
  235. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Instance", 8,
  236. c->instance.s, c->instance.len);
  237. if (node==0)
  238. return -1;
  239. }
  240. /* reg-id */
  241. p = int2str((unsigned long)c->reg_id, &len);
  242. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Reg-Id", 6, p, len);
  243. if (node==0)
  244. return -1;
  245. /* last keepalive */
  246. p = int2str((unsigned long)c->last_keepalive, &len);
  247. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Last-Keepalive",
  248. 14, p, len);
  249. if (node==0)
  250. return -1;
  251. /* last modified */
  252. p = int2str((unsigned long)c->last_modified, &len);
  253. node = add_mi_node_child( cnode, MI_DUP_VALUE, "Last-Modified",
  254. 13, p, len);
  255. if (node==0)
  256. return -1;
  257. } /* for */
  258. return 0;
  259. }
  260. /*************************** MI functions *****************************/
  261. /*!
  262. * \brief Delete a address of record including its contacts
  263. * \param cmd mi_root containing the parameter
  264. * \param param not used
  265. * \note expects 2 nodes: the table name and the AOR
  266. * \return mi_root with the result
  267. */
  268. struct mi_root* mi_usrloc_rm_aor(struct mi_root *cmd, void *param)
  269. {
  270. struct mi_node *node;
  271. udomain_t *dom;
  272. str *aor;
  273. node = cmd->node.kids;
  274. if (node==NULL || node->next==NULL || node->next->next!=NULL)
  275. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  276. /* look for table */
  277. dom = mi_find_domain( &node->value );
  278. if (dom==NULL)
  279. return init_mi_tree( 404, "Table not found", 15);
  280. /* process the aor */
  281. aor = &node->next->value;
  282. if ( mi_fix_aor(aor)!=0 )
  283. return init_mi_tree( 400, "Domain missing in AOR", 21);
  284. lock_udomain( dom, aor);
  285. if (delete_urecord( dom, aor, 0) < 0) {
  286. unlock_udomain( dom, aor);
  287. return init_mi_tree( 500, "Failed to delete AOR", 20);
  288. }
  289. unlock_udomain( dom, aor);
  290. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  291. }
  292. /*!
  293. * \brief Delete a contact from an AOR record
  294. * \param cmd mi_root containing the parameter
  295. * \param param not used
  296. * \note expects 3 nodes: the table name, the AOR and contact
  297. * \return mi_root with the result or 0 on failure
  298. */
  299. struct mi_root* mi_usrloc_rm_contact(struct mi_root *cmd, void *param)
  300. {
  301. struct mi_node *node;
  302. udomain_t *dom;
  303. urecord_t *rec;
  304. ucontact_t* con;
  305. str *aor, *contact;
  306. int ret;
  307. node = cmd->node.kids;
  308. if (node==NULL || node->next==NULL || node->next->next==NULL ||
  309. node->next->next->next!=NULL)
  310. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  311. /* look for table */
  312. dom = mi_find_domain( &node->value );
  313. if (dom==NULL)
  314. return init_mi_tree( 404, "Table not found", 15);
  315. /* process the aor */
  316. aor = &node->next->value;
  317. if ( mi_fix_aor(aor)!=0 )
  318. return init_mi_tree( 400, "Domain missing in AOR", 21);
  319. lock_udomain( dom, aor);
  320. ret = get_urecord( dom, aor, &rec);
  321. if (ret == 1) {
  322. unlock_udomain( dom, aor);
  323. return init_mi_tree( 404, "AOR not found", 13);
  324. }
  325. contact = &node->next->next->value;
  326. set_mi_ul_cid();
  327. ret = get_ucontact( rec, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ+1, &con);
  328. if (ret < 0) {
  329. unlock_udomain( dom, aor);
  330. return 0;
  331. }
  332. if (ret > 0) {
  333. unlock_udomain( dom, aor);
  334. return init_mi_tree( 404, "Contact not found", 17);
  335. }
  336. if (delete_ucontact(rec, con) < 0) {
  337. unlock_udomain( dom, aor);
  338. return 0;
  339. }
  340. release_urecord(rec);
  341. unlock_udomain( dom, aor);
  342. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  343. }
  344. /*!
  345. * \brief Dump the content of the usrloc
  346. * \param cmd mi_root containing the parameter
  347. * \param param not used
  348. * \return mi_root with the result or 0 on failure
  349. */
  350. struct mi_root* mi_usrloc_dump(struct mi_root *cmd, void *param)
  351. {
  352. struct mi_root *rpl_tree;
  353. struct mi_node *rpl, *node;
  354. struct mi_attr *attr;
  355. struct urecord* r;
  356. dlist_t* dl;
  357. udomain_t* dom;
  358. time_t t;
  359. char *p;
  360. int max, len, n, i, short_dump;
  361. node = cmd->node.kids;
  362. if (node && node->next)
  363. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  364. if (node && node->value.len==5 && !strncasecmp(node->value.s, "brief", 5)){
  365. /* short version */
  366. short_dump = 1;
  367. } else {
  368. short_dump = 0;
  369. }
  370. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  371. if (rpl_tree==NULL)
  372. return 0;
  373. rpl = &rpl_tree->node;
  374. t = time(0);
  375. for( dl=root ; dl ; dl=dl->next ) {
  376. /* add a domain node */
  377. node = add_mi_node_child( rpl, 0, "Domain", 6,
  378. dl->name.s, dl->name.len);
  379. if (node==0)
  380. goto error;
  381. dom = dl->d;
  382. /* add some attributes to the domain node */
  383. p= int2str((unsigned long)dom->size, &len);
  384. attr = add_mi_attr( node, MI_DUP_VALUE, "table", 5, p, len);
  385. if (attr==0)
  386. goto error;
  387. /* add the entries per hash */
  388. for(i=0,n=0,max=0; i<dom->size; i++) {
  389. lock_ulslot( dom, i);
  390. n += dom->table[i].n;
  391. if(max<dom->table[i].n)
  392. max= dom->table[i].n;
  393. for( r = dom->table[i].first ; r ; r=r->next ) {
  394. /* add entry */
  395. if (mi_add_aor_node( node, r, t, short_dump)!=0) {
  396. unlock_ulslot( dom, i);
  397. goto error;
  398. }
  399. }
  400. unlock_ulslot( dom, i);
  401. }
  402. /* add more attributes to the domain node */
  403. p= int2str((unsigned long)n, &len);
  404. attr = add_mi_attr( node, MI_DUP_VALUE, "records", 7, p, len);
  405. if (attr==0)
  406. goto error;
  407. p= int2str((unsigned long)max, &len);
  408. attr = add_mi_attr( node, MI_DUP_VALUE, "max_slot", 8, p, len);
  409. if (attr==0)
  410. goto error;
  411. }
  412. return rpl_tree;
  413. error:
  414. free_mi_tree(rpl_tree);
  415. return 0;
  416. }
  417. /*!
  418. * \brief Flush the usrloc memory cache to DB
  419. * \param cmd mi_root containing the parameter
  420. * \param param not used
  421. * \return mi_root with the result or 0 on failure
  422. */
  423. struct mi_root* mi_usrloc_flush(struct mi_root *cmd, void *param)
  424. {
  425. struct mi_root *rpl_tree;
  426. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  427. if (rpl_tree==NULL)
  428. return 0;
  429. synchronize_all_udomains(0, 1);
  430. return rpl_tree;
  431. }
  432. /*!
  433. * \brief Add a new contact for an address of record
  434. * \param cmd mi_root containing the parameter
  435. * \param param not used
  436. * \note Expects 9 nodes: table name, AOR, contact, expires, Q,
  437. * path, flags, cflags, methods
  438. * \return mi_root with the result
  439. */
  440. struct mi_root* mi_usrloc_add(struct mi_root *cmd, void *param)
  441. {
  442. ucontact_info_t ci;
  443. urecord_t* r;
  444. ucontact_t* c;
  445. struct mi_node *node;
  446. udomain_t *dom;
  447. str *aor, *contact;
  448. unsigned int ui_val;
  449. int n;
  450. for( n=0,node = cmd->node.kids; n<9 && node ; n++,node=node->next );
  451. if (n!=9 || node!=0)
  452. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  453. node = cmd->node.kids;
  454. /* look for table (param 1) */
  455. dom = mi_find_domain( &node->value );
  456. if (dom==NULL)
  457. return init_mi_tree( 404, "Table not found", 15);
  458. /* process the aor (param 2) */
  459. node = node->next;
  460. aor = &node->value;
  461. if ( mi_fix_aor(aor)!=0 )
  462. return init_mi_tree( 400, "Domain missing in AOR", 21);
  463. /* contact (param 3) */
  464. node = node->next;
  465. contact = &node->value;
  466. memset( &ci, 0, sizeof(ucontact_info_t));
  467. /* expire (param 4) */
  468. node = node->next;
  469. if (str2int( &node->value, &ui_val) < 0)
  470. goto bad_syntax;
  471. ci.expires = ui_val;
  472. /* q value (param 5) */
  473. node = node->next;
  474. if (str2q( &ci.q, node->value.s, node->value.len) < 0)
  475. goto bad_syntax;
  476. /* path value (param 6) */
  477. node = node->next;
  478. if(strncmp(node->value.s, "0", 1) != 0 && node->value.len > 1)
  479. ci.path = &node->value;
  480. /* flags value (param 7) */
  481. node = node->next;
  482. if (str2int( &node->value, (unsigned int*)&ci.flags) < 0)
  483. goto bad_syntax;
  484. /* branch flags value (param 8) */
  485. node = node->next;
  486. if (str2int( &node->value, (unsigned int*)&ci.cflags) < 0)
  487. goto bad_syntax;
  488. /* methods value (param 9) */
  489. node = node->next;
  490. if (str2int( &node->value, (unsigned int*)&ci.methods) < 0)
  491. goto bad_syntax;
  492. if(sruid_next(&_ul_sruid)<0)
  493. goto error;
  494. ci.ruid = _ul_sruid.uid;
  495. lock_udomain( dom, aor);
  496. set_mi_ul_cid();
  497. n = get_urecord( dom, aor, &r);
  498. if ( n==1) {
  499. if (insert_urecord( dom, aor, &r) < 0)
  500. goto lock_error;
  501. c = 0;
  502. } else {
  503. if (get_ucontact( r, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ+1, &c) < 0)
  504. goto lock_error;
  505. }
  506. get_act_time();
  507. ci.callid = &mi_ul_cid;
  508. ci.user_agent = &mi_ul_ua;
  509. ci.cseq = ++MI_UL_CSEQ;
  510. /* 0 expires means permanent contact */
  511. if (ci.expires!=0)
  512. ci.expires += act_time;
  513. if (c) {
  514. if (update_ucontact( r, c, &ci) < 0)
  515. goto release_error;
  516. } else {
  517. if ( insert_ucontact( r, contact, &ci, &c) < 0 )
  518. goto release_error;
  519. }
  520. release_urecord(r);
  521. unlock_udomain( dom, aor);
  522. return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  523. bad_syntax:
  524. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  525. release_error:
  526. release_urecord(r);
  527. lock_error:
  528. unlock_udomain( dom, aor);
  529. error:
  530. return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
  531. }
  532. /*!
  533. * \brief Dumps the contacts of an AOR
  534. * \param cmd mi_root containing the parameter
  535. * \param param not used
  536. * \note expects 2 nodes: the table name and the AOR
  537. * \return mi_root with the result or 0 on failure
  538. */
  539. struct mi_root* mi_usrloc_show_contact(struct mi_root *cmd, void *param)
  540. {
  541. struct mi_root *rpl_tree;
  542. struct mi_node *rpl, *node;
  543. udomain_t *dom;
  544. urecord_t *rec;
  545. ucontact_t* con;
  546. str *aor;
  547. int ret;
  548. node = cmd->node.kids;
  549. if (node==NULL || node->next==NULL || node->next->next!=NULL)
  550. return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  551. /* look for table */
  552. dom = mi_find_domain( &node->value );
  553. if (dom==NULL)
  554. return init_mi_tree( 404, "Table not found", 15);
  555. /* process the aor */
  556. aor = &node->next->value;
  557. if ( mi_fix_aor(aor)!=0 )
  558. return init_mi_tree( 400, "Domain missing in AOR", 21);
  559. lock_udomain( dom, aor);
  560. ret = get_urecord( dom, aor, &rec);
  561. if (ret == 1) {
  562. unlock_udomain( dom, aor);
  563. return init_mi_tree( 404, "AOR not found", 13);
  564. }
  565. get_act_time();
  566. rpl_tree = 0;
  567. rpl = 0;
  568. for( con=rec->contacts ; con ; con=con->next) {
  569. if (VALID_CONTACT( con, act_time)) {
  570. if (rpl_tree==0) {
  571. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  572. if (rpl_tree==0)
  573. goto error;
  574. rpl = &rpl_tree->node;
  575. }
  576. node = addf_mi_node_child( rpl, 0, "Contact", 7,
  577. "<%.*s>;q=%s;expires=%d;flags=0x%X;cflags=0x%X;state=%d;socket=<%.*s>;"
  578. "methods=0x%X"
  579. "%s%.*s%s" /*received*/
  580. "%s%.*s%s" /*user-agent*/
  581. "%s%.*s%s" /*path*/
  582. "%s%.*s" /*instance*/
  583. ";reg-id=%u",
  584. con->c.len, ZSW(con->c.s),
  585. q2str(con->q, 0), (int)(con->expires - act_time),
  586. con->flags, con->cflags, con->state,
  587. con->sock?con->sock->sock_str.len:3,
  588. con->sock?con->sock->sock_str.s:"NULL",
  589. con->methods,
  590. con->received.len?";received=<":"",con->received.len,
  591. ZSW(con->received.s), con->received.len?">":"",
  592. con->user_agent.len?";user_agent=<":"",con->user_agent.len,
  593. ZSW(con->user_agent.s), con->user_agent.len?">":"",
  594. con->path.len?";path=<":"", con->path.len,
  595. ZSW(con->path.s), con->path.len?">":"",
  596. con->instance.len?";+sip.instance=":"", con->instance.len,
  597. ZSW(con->instance.s),
  598. con->reg_id
  599. );
  600. if (node==0)
  601. goto error;
  602. }
  603. }
  604. unlock_udomain( dom, aor);
  605. if (rpl_tree==0)
  606. return init_mi_tree( 404 , "AOR has no contacts", 18);
  607. return rpl_tree;
  608. error:
  609. if (rpl_tree)
  610. free_mi_tree( rpl_tree );
  611. unlock_udomain( dom, aor);
  612. return 0;
  613. }