ul_rpc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * $Id$
  3. *
  4. * usrloc module
  5. *
  6. * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com).
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. #include "../../ip_addr.h"
  21. #include "../../dprint.h"
  22. #include "../../lib/srutils/sruid.h"
  23. #include "ul_rpc.h"
  24. #include "dlist.h"
  25. #include "ucontact.h"
  26. #include "udomain.h"
  27. #include "ul_mod.h"
  28. #include "utime.h"
  29. /*! CSEQ nr used */
  30. #define RPC_UL_CSEQ 1
  31. /*! call-id used for ul_add and ul_rm_contact */
  32. static str rpc_ul_cid = str_init("[email protected]");
  33. /*! path used for ul_add and ul_rm_contact */
  34. static str rpc_ul_path = str_init("dummypath");
  35. /*! user agent used for ul_add */
  36. static str rpc_ul_ua = str_init("SIP Router MI Server");
  37. extern sruid_t _ul_sruid;
  38. static const char* ul_rpc_dump_doc[2] = {
  39. "Dump user location tables",
  40. 0
  41. };
  42. int rpc_dump_contact(rpc_t* rpc, void* ctx, void *ih, ucontact_t* c)
  43. {
  44. void* vh;
  45. str empty_str = {"[not set]", 9};
  46. str state_str = {"[not set]", 9};
  47. str socket_str = {"[not set]", 9};
  48. time_t t;
  49. t = time(0);
  50. if(rpc->struct_add(ih, "{", "Contact", &vh)<0)
  51. {
  52. rpc->fault(ctx, 500, "Internal error creating contact struct");
  53. return -1;
  54. }
  55. if(rpc->struct_add(vh, "S", "Address", &c->c)<0)
  56. {
  57. rpc->fault(ctx, 500, "Internal error adding addr");
  58. return -1;
  59. }
  60. if (c->expires == 0) { if(rpc->struct_add(vh, "s", "Expires", "permanent")<0)
  61. {
  62. rpc->fault(ctx, 500, "Internal error adding expire");
  63. return -1;
  64. }
  65. } else if (c->expires == UL_EXPIRED_TIME) {
  66. if(rpc->struct_add(vh, "s", "Expires", "deleted")<0)
  67. {
  68. rpc->fault(ctx, 500, "Internal error adding expire");
  69. return -1;
  70. }
  71. } else if (t > c->expires) {
  72. if(rpc->struct_add(vh, "s", "Expires", "expired")<0)
  73. {
  74. rpc->fault(ctx, 500, "Internal error adding expire");
  75. return -1;
  76. }
  77. } else {
  78. if(rpc->struct_add(vh, "d", "Expires", (int)(c->expires - t))<0)
  79. {
  80. rpc->fault(ctx, 500, "Internal error adding expire");
  81. return -1;
  82. }
  83. }
  84. if (c->state == CS_NEW) {
  85. state_str.s = "CS_NEW";
  86. state_str.len = 6;
  87. } else if (c->state == CS_SYNC) {
  88. state_str.s = "CS_SYNC";
  89. state_str.len = 7;
  90. } else if (c->state== CS_DIRTY) {
  91. state_str.s = "CS_DIRTY";
  92. state_str.len = 8;
  93. } else {
  94. state_str.s = "CS_UNKNOWN";
  95. state_str.len = 10;
  96. }
  97. if(c->sock)
  98. {
  99. socket_str.s = c->sock->sock_str.s;
  100. socket_str.len = c->sock->sock_str.len;
  101. }
  102. if(rpc->struct_add(vh, "f", "Q", q2double(c->q))<0)
  103. {
  104. rpc->fault(ctx, 500, "Internal error adding q");
  105. return -1;
  106. }
  107. if(rpc->struct_add(vh, "S", "Call-ID", &c->callid)<0)
  108. {
  109. rpc->fault(ctx, 500, "Internal error adding callid");
  110. return -1;
  111. }
  112. if(rpc->struct_add(vh, "d", "CSeq", c->cseq)<0)
  113. {
  114. rpc->fault(ctx, 500, "Internal error adding cseq");
  115. return -1;
  116. }
  117. if(rpc->struct_add(vh, "S", "User-Agent",
  118. (c->user_agent.len)?&c->user_agent: &empty_str)<0)
  119. {
  120. rpc->fault(ctx, 500, "Internal error adding user-agent");
  121. return -1;
  122. }
  123. if(rpc->struct_add(vh, "S", "Received",
  124. (c->received.len)?&c->received: &empty_str)<0)
  125. {
  126. rpc->fault(ctx, 500, "Internal error adding received");
  127. return -1;
  128. }
  129. if(rpc->struct_add(vh, "S", "Path",
  130. (c->path.len)?&c->path: &empty_str)<0)
  131. {
  132. rpc->fault(ctx, 500, "Internal error adding path");
  133. return -1;
  134. }
  135. if(rpc->struct_add(vh, "S", "State", &state_str)<0)
  136. {
  137. rpc->fault(ctx, 500, "Internal error adding state");
  138. return -1;
  139. }
  140. if(rpc->struct_add(vh, "d", "Flags", c->flags)<0)
  141. {
  142. rpc->fault(ctx, 500, "Internal error adding flags");
  143. return -1;
  144. }
  145. if(rpc->struct_add(vh, "d", "CFlags", c->cflags)<0)
  146. {
  147. rpc->fault(ctx, 500, "Internal error adding cflags");
  148. return -1;
  149. }
  150. if(rpc->struct_add(vh, "S", "Socket", &socket_str)<0)
  151. {
  152. rpc->fault(ctx, 500, "Internal error adding socket");
  153. return -1;
  154. }
  155. if(rpc->struct_add(vh, "d", "Methods", c->methods)<0)
  156. {
  157. rpc->fault(ctx, 500, "Internal error adding methods");
  158. return -1;
  159. }
  160. if(rpc->struct_add(vh, "S", "Ruid", (c->ruid.len)?&c->ruid: &empty_str)<0)
  161. {
  162. rpc->fault(ctx, 500, "Internal error adding ruid");
  163. return -1;
  164. }
  165. if(rpc->struct_add(vh, "S", "Instance",
  166. (c->instance.len)?&c->instance: &empty_str)<0)
  167. {
  168. rpc->fault(ctx, 500, "Internal error adding instance");
  169. return -1;
  170. }
  171. if(rpc->struct_add(vh, "d", "Reg-Id", c->reg_id)<0)
  172. {
  173. rpc->fault(ctx, 500, "Internal error adding reg_id");
  174. return -1;
  175. }
  176. if(rpc->struct_add(vh, "d", "Last-Keepalive", (int)c->last_keepalive)<0)
  177. {
  178. rpc->fault(ctx, 500, "Internal error adding reg_id");
  179. return -1;
  180. }
  181. if(rpc->struct_add(vh, "d", "Last-Modified", (int)c->last_modified)<0)
  182. {
  183. rpc->fault(ctx, 500, "Internal error adding reg_id");
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. static void ul_rpc_dump(rpc_t* rpc, void* ctx)
  189. {
  190. struct urecord* r;
  191. dlist_t* dl;
  192. udomain_t* dom;
  193. str brief = {0, 0};
  194. int summary = 0;
  195. ucontact_t* c;
  196. void* th;
  197. void* ah;
  198. void* ih;
  199. void* sh;
  200. int max, n, i;
  201. rpc->scan(ctx, "*S", &brief);
  202. if(brief.len==5 && (strncmp(brief.s, "brief", 5)==0))
  203. summary = 1;
  204. for( dl=root ; dl ; dl=dl->next ) {
  205. dom = dl->d;
  206. if (rpc->add(ctx, "{", &th) < 0)
  207. {
  208. rpc->fault(ctx, 500, "Internal error creating top rpc");
  209. return;
  210. }
  211. if(rpc->struct_add(th, "Sd{",
  212. "Domain", &dl->name,
  213. "Size", (int)dom->size,
  214. "AoRs", &ah)<0)
  215. {
  216. rpc->fault(ctx, 500, "Internal error creating inner struct");
  217. return;
  218. }
  219. for(i=0,n=0,max=0; i<dom->size; i++) {
  220. lock_ulslot( dom, i);
  221. n += dom->table[i].n;
  222. if(max<dom->table[i].n)
  223. max= dom->table[i].n;
  224. for( r = dom->table[i].first ; r ; r=r->next ) {
  225. if(summary==1)
  226. {
  227. if(rpc->struct_add(ah, "S",
  228. "AoR", &r->aor)<0)
  229. {
  230. rpc->fault(ctx, 500, "Internal error creating aor struct");
  231. return;
  232. }
  233. } else {
  234. if(rpc->struct_add(ah, "Sd{",
  235. "AoR", &r->aor,
  236. "HashID", r->aorhash,
  237. "Contacts", &ih)<0)
  238. {
  239. unlock_ulslot( dom, i);
  240. rpc->fault(ctx, 500, "Internal error creating aor struct");
  241. return;
  242. }
  243. for( c=r->contacts ; c ; c=c->next)
  244. {
  245. if (rpc_dump_contact(rpc, ctx, ih, c) == -1) {
  246. unlock_ulslot(dom, i);
  247. return;
  248. }
  249. }
  250. }
  251. }
  252. unlock_ulslot( dom, i);
  253. }
  254. /* extra attributes node */
  255. if(rpc->struct_add(th, "{", "Stats", &sh)<0)
  256. {
  257. rpc->fault(ctx, 500, "Internal error creating stats struct");
  258. return;
  259. }
  260. if(rpc->struct_add(sh, "dd",
  261. "Records", n,
  262. "Max-Slots", max)<0)
  263. {
  264. rpc->fault(ctx, 500, "Internal error adding stats");
  265. return;
  266. }
  267. }
  268. }
  269. static const char* ul_rpc_lookup_doc[2] = {
  270. "Lookup one AOR in the usrloc location table",
  271. 0
  272. };
  273. /*!
  274. * \brief Search a domain in the global domain list
  275. * \param table domain (table) name
  276. * \return pointer to domain if found, 0 if not found
  277. */
  278. static inline udomain_t* rpc_find_domain(str* table)
  279. {
  280. dlist_t* dom;
  281. for( dom=root ; dom ; dom=dom->next ) {
  282. if ((dom->name.len == table->len) &&
  283. !memcmp(dom->name.s, table->s, table->len))
  284. return dom->d;
  285. }
  286. return 0;
  287. }
  288. /*!
  289. * \brief Convert address of record
  290. *
  291. * Convert an address of record string to lower case, and truncate
  292. * it when use_domain is not set.
  293. * \param aor address of record
  294. * \return 0 on success, -1 on error
  295. */
  296. static inline int rpc_fix_aor(str *aor)
  297. {
  298. char *p;
  299. p = memchr( aor->s, '@', aor->len);
  300. if (use_domain) {
  301. if (p==NULL)
  302. return -1;
  303. } else {
  304. if (p)
  305. aor->len = p - aor->s;
  306. }
  307. strlower(aor);
  308. return 0;
  309. }
  310. /*!
  311. * \brief Dumps the contacts of an AOR
  312. * \param rpc Handle to RPC structure
  313. * \param ctx not used
  314. * \note expects 2 arguments: the table name and the AOR
  315. */
  316. static void ul_rpc_lookup(rpc_t* rpc, void* ctx)
  317. {
  318. udomain_t* dom;
  319. str table = {0, 0};
  320. str aor = {0, 0};
  321. void* th;
  322. urecord_t *rec;
  323. ucontact_t* con;
  324. int ret;
  325. int rpl_tree;
  326. if (rpc->scan(ctx, "S", &table) != 1) {
  327. rpc->fault(ctx, 500, "Not enough parameters (table and AOR to lookup)");
  328. return;
  329. }
  330. if (rpc->scan(ctx, "S", &aor) != 1) {
  331. rpc->fault(ctx, 500, "Not enough parameters (table and AOR to lookup)");
  332. return;
  333. }
  334. /* look for table */
  335. dom = rpc_find_domain( &table );
  336. if (dom == NULL) {
  337. rpc->fault(ctx, 500, "Domain not found");
  338. return;
  339. }
  340. /* process the aor */
  341. if ( rpc_fix_aor(&aor) != 0 ) {
  342. rpc->fault(ctx, 500, "Domain missing in AOR");
  343. return;
  344. }
  345. lock_udomain( dom, &aor);
  346. ret = get_urecord( dom, &aor, &rec);
  347. if (ret == 1) {
  348. unlock_udomain( dom, &aor);
  349. rpc->fault(ctx, 500, "AOR not found in location table");
  350. return;
  351. }
  352. get_act_time();
  353. rpl_tree = 0;
  354. if (rpc->add(ctx, "{", &th) < 0)
  355. {
  356. rpc->fault(ctx, 500, "Internal error creating outer rpc");
  357. return;
  358. }
  359. /* We have contacts, list them */
  360. for( con=rec->contacts ; con ; con=con->next) {
  361. if (VALID_CONTACT( con, act_time)) {
  362. rpl_tree++;
  363. if (rpc_dump_contact(rpc, ctx, th, con) == -1) {
  364. unlock_udomain(dom, &aor);
  365. return;
  366. }
  367. }
  368. }
  369. unlock_udomain( dom, &aor);
  370. if (rpl_tree==0) {
  371. rpc->fault(ctx, 500, "AOR has no contacts");
  372. return;
  373. }
  374. return;
  375. }
  376. static void ul_rpc_rm_aor(rpc_t* rpc, void* ctx)
  377. {
  378. udomain_t* dom;
  379. str table = {0, 0};
  380. str aor = {0, 0};
  381. if (rpc->scan(ctx, "SS", &table, &aor) != 2) {
  382. rpc->fault(ctx, 500, "Not enough parameters (table and AOR to lookup)");
  383. return;
  384. }
  385. /* look for table */
  386. dom = rpc_find_domain( &table );
  387. if (dom == NULL) {
  388. rpc->fault(ctx, 500, "Domain not found");
  389. return;
  390. }
  391. /* process the aor */
  392. if ( rpc_fix_aor(&aor) != 0 ) {
  393. rpc->fault(ctx, 500, "Domain missing in AOR");
  394. return;
  395. }
  396. lock_udomain( dom, &aor);
  397. if (delete_urecord( dom, &aor, 0) < 0) {
  398. unlock_udomain( dom, &aor);
  399. rpc->fault(ctx, 500, "Failed to delete AOR");
  400. return;
  401. }
  402. unlock_udomain( dom, &aor);
  403. return;
  404. }
  405. static const char* ul_rpc_rm_aor_doc[2] = {
  406. "Delete a address of record including its contacts",
  407. 0
  408. };
  409. static void ul_rpc_rm_contact(rpc_t* rpc, void* ctx)
  410. {
  411. udomain_t* dom;
  412. str table = {0, 0};
  413. str aor = {0, 0};
  414. str contact = {0, 0};
  415. urecord_t *rec;
  416. ucontact_t* con;
  417. int ret;
  418. if (rpc->scan(ctx, "SSS", &table, &aor, &contact) != 3) {
  419. rpc->fault(ctx, 500, "Not enough parameters (table, AOR and contact)");
  420. return;
  421. }
  422. /* look for table */
  423. dom = rpc_find_domain( &table );
  424. if (dom == NULL) {
  425. rpc->fault(ctx, 500, "Domain not found");
  426. return;
  427. }
  428. /* process the aor */
  429. if ( rpc_fix_aor(&aor) != 0 ) {
  430. rpc->fault(ctx, 500, "Domain missing in AOR");
  431. return;
  432. }
  433. lock_udomain( dom, &aor);
  434. ret = get_urecord( dom, &aor, &rec);
  435. if (ret == 1) {
  436. unlock_udomain( dom, &aor);
  437. rpc->fault(ctx, 404, "AOR not found");
  438. return;
  439. }
  440. ret = get_ucontact( rec, &contact, &rpc_ul_cid, &rpc_ul_path, RPC_UL_CSEQ+1, &con);
  441. if (ret < 0) {
  442. unlock_udomain( dom, &aor);
  443. rpc->fault(ctx, 500, "Internal error (can't get contact)");
  444. return;
  445. }
  446. if (ret > 0) {
  447. unlock_udomain( dom, &aor);
  448. rpc->fault(ctx, 404, "Contact not found");
  449. return;
  450. }
  451. if (delete_ucontact(rec, con) < 0) {
  452. unlock_udomain( dom, &aor);
  453. rpc->fault(ctx, 500, "Internal error (can't delete contact)");
  454. return;
  455. }
  456. release_urecord(rec);
  457. unlock_udomain( dom, &aor);
  458. return;
  459. }
  460. static const char* ul_rpc_rm_contact_doc[2] = {
  461. "Delete a contact from an AOR record",
  462. 0
  463. };
  464. static void ul_rpc_flush(rpc_t* rpc, void* ctx)
  465. {
  466. synchronize_all_udomains(0, 1);
  467. return;
  468. }
  469. static const char* ul_rpc_flush_doc[2] = {
  470. "Flush the usrloc memory cache to DB",
  471. 0
  472. };
  473. /*!
  474. * \brief Add a new contact for an address of record
  475. * \note Expects 9 parameters: table name, AOR, contact, expires, Q,
  476. * path, flags, cflags, methods
  477. */
  478. static void ul_rpc_add(rpc_t* rpc, void* ctx)
  479. {
  480. str table = {0, 0};
  481. str aor = {0, 0};
  482. str contact = {0, 0};
  483. str path = {0, 0};
  484. str temp = {0, 0};
  485. double dtemp;
  486. ucontact_info_t ci;
  487. urecord_t* r;
  488. ucontact_t* c;
  489. udomain_t *dom;
  490. int ret;
  491. memset( &ci, 0, sizeof(ucontact_info_t));
  492. ret = rpc->scan(ctx, "SSSdfSddd", &table, &aor, &contact, &ci.expires,
  493. &dtemp, &path, &ci.flags, &ci.cflags, &ci.methods);
  494. if(path.len==1 && (strncmp(path.s, "0", 1)==0)) {
  495. LM_DBG("path == 0 -> unset\n");
  496. }
  497. else {
  498. ci.path = &path;
  499. }
  500. LM_DBG("ret: %d table:%.*s aor:%.*s contact:%.*s expires:%d dtemp:%f path:%.*s flags:%d bflags:%d methods:%d\n",
  501. ret, table.len, table.s, aor.len, aor.s, contact.len, contact.s,
  502. (int) ci.expires, dtemp, (ci.path)?ci.path->len:0, (ci.path && ci.path->s)?ci.path->s:"", ci.flags, ci.cflags, (int) ci.methods);
  503. if ( ret != 9) {
  504. rpc->fault(ctx, 500, "Not enough parameters or wrong format");
  505. return;
  506. }
  507. ci.q = double2q(dtemp);
  508. temp.s = q2str(ci.q, (unsigned int*)&temp.len);
  509. LM_DBG("q:%.*s\n", temp.len, temp.s);
  510. /* look for table */
  511. dom = rpc_find_domain( &table );
  512. if (dom == NULL) {
  513. rpc->fault(ctx, 500, "Domain not found");
  514. return;
  515. }
  516. /* process the aor */
  517. if ( rpc_fix_aor(&aor) != 0 ) {
  518. rpc->fault(ctx, 500, "Domain missing in AOR");
  519. return;
  520. }
  521. if(sruid_next(&_ul_sruid)<0)
  522. {
  523. rpc->fault(ctx, 500, "Can't obtain next uid");
  524. return;
  525. }
  526. ci.ruid = _ul_sruid.uid;
  527. lock_udomain( dom, &aor);
  528. ret = get_urecord( dom, &aor, &r);
  529. if(ret==1) {
  530. if (insert_urecord( dom, &aor, &r) < 0)
  531. {
  532. unlock_udomain( dom, &aor);
  533. rpc->fault(ctx, 500, "Can't insert record");
  534. return;
  535. }
  536. c = 0;
  537. } else {
  538. if (get_ucontact( r, &contact, &rpc_ul_cid, &rpc_ul_path, RPC_UL_CSEQ+1, &c) < 0)
  539. {
  540. unlock_udomain( dom, &aor);
  541. rpc->fault(ctx, 500, "Can't get record");
  542. return;
  543. }
  544. }
  545. get_act_time();
  546. ci.callid = &rpc_ul_cid;
  547. ci.user_agent = &rpc_ul_ua;
  548. ci.cseq = RPC_UL_CSEQ;
  549. /* 0 expires means permanent contact */
  550. if (ci.expires!=0)
  551. ci.expires += act_time;
  552. if (c) {
  553. if (update_ucontact( r, c, &ci) < 0)
  554. {
  555. release_urecord(r);
  556. unlock_udomain( dom, &aor);
  557. rpc->fault(ctx, 500, "Can't update contact");
  558. return;
  559. }
  560. } else {
  561. if ( insert_ucontact( r, &contact, &ci, &c) < 0 )
  562. {
  563. release_urecord(r);
  564. unlock_udomain( dom, &aor);
  565. rpc->fault(ctx, 500, "Can't insert contact");
  566. return;
  567. }
  568. }
  569. release_urecord(r);
  570. unlock_udomain( dom, &aor);
  571. return;
  572. }
  573. static const char* ul_rpc_add_doc[2] = {
  574. "Add a new contact for an address of record",
  575. 0
  576. };
  577. #define QUERY_LEN 256
  578. static void ul_rpc_db_users(rpc_t* rpc, void* ctx)
  579. {
  580. str table = {0, 0};
  581. char query[QUERY_LEN];
  582. str query_str;
  583. db1_res_t* res;
  584. int count;
  585. if (db_mode == NO_DB) {
  586. rpc->fault(ctx, 500, "Command is not supported in db_mode=0");
  587. return;
  588. }
  589. if (rpc->scan(ctx, "S", &table) != 1) {
  590. rpc->fault(ctx, 500, "Not enough parameters (table to lookup)");
  591. return;
  592. }
  593. if (user_col.len + domain_col.len + table.len + 32 > QUERY_LEN) {
  594. rpc->fault(ctx, 500, "Too long database query");
  595. return;
  596. }
  597. if (!DB_CAPABILITY(ul_dbf, DB_CAP_RAW_QUERY)) {
  598. rpc->fault(ctx, 500, "Database does not support raw queries");
  599. return;
  600. }
  601. if (ul_dbf.use_table(ul_dbh, &table) < 0) {
  602. rpc->fault(ctx, 500, "Failed to use table");
  603. return;
  604. }
  605. memset(query, 0, QUERY_LEN);
  606. query_str.len = snprintf(query, QUERY_LEN,
  607. "SELECT COUNT(DISTINCT %.*s, %.*s) FROM %.*s WHERE (UNIX_TIMESTAMP(expires) = 0) OR (expires > NOW())",
  608. user_col.len, user_col.s,
  609. domain_col.len, domain_col.s,
  610. table.len, table.s);
  611. query_str.s = query;
  612. if (ul_dbf.raw_query(ul_dbh, &query_str, &res) < 0) {
  613. rpc->fault(ctx, 500, "Failed to query AoR count");
  614. return;
  615. }
  616. count = (int)VAL_INT(ROW_VALUES(RES_ROWS(res)));
  617. ul_dbf.free_result(ul_dbh, res);
  618. rpc->add(ctx, "d", count);
  619. }
  620. static const char* ul_rpc_db_users_doc[2] = {
  621. "Tell number of different unexpired users (AoRs) in database table (db_mode!=0 only)",
  622. 0
  623. };
  624. static void ul_rpc_db_contacts(rpc_t* rpc, void* ctx)
  625. {
  626. str table = {0, 0};
  627. char query[QUERY_LEN];
  628. str query_str;
  629. db1_res_t* res;
  630. int count;
  631. if (db_mode == NO_DB) {
  632. rpc->fault(ctx, 500, "Command is not supported in db_mode=0");
  633. return;
  634. }
  635. if (rpc->scan(ctx, "S", &table) != 1) {
  636. rpc->fault(ctx, 500, "Not enough parameters (table to lookup)");
  637. return;
  638. }
  639. if (table.len + 22 > QUERY_LEN) {
  640. rpc->fault(ctx, 500, "Too long database query");
  641. return;
  642. }
  643. if (!DB_CAPABILITY(ul_dbf, DB_CAP_RAW_QUERY)) {
  644. rpc->fault(ctx, 500, "Database does not support raw queries");
  645. return;
  646. }
  647. if (ul_dbf.use_table(ul_dbh, &table) < 0) {
  648. rpc->fault(ctx, 500, "Failed to use table");
  649. return;
  650. }
  651. memset(query, 0, QUERY_LEN);
  652. query_str.len = snprintf(query, QUERY_LEN, "SELECT COUNT(*) FROM %.*s WHERE (UNIX_TIMESTAMP(expires) = 0) OR (expires > NOW())",
  653. table.len, table.s);
  654. query_str.s = query;
  655. if (ul_dbf.raw_query(ul_dbh, &query_str, &res) < 0) {
  656. rpc->fault(ctx, 500, "Failed to query contact count");
  657. return;
  658. }
  659. count = (int)VAL_INT(ROW_VALUES(RES_ROWS(res)));
  660. ul_dbf.free_result(ul_dbh, res);
  661. rpc->add(ctx, "d", count);
  662. }
  663. static const char* ul_rpc_db_contacts_doc[2] = {
  664. "Tell number of unexpired contacts in database table (db_mode=3 only)",
  665. 0
  666. };
  667. static void ul_rpc_db_expired_contacts(rpc_t* rpc, void* ctx)
  668. {
  669. str table = {0, 0};
  670. char query[QUERY_LEN];
  671. str query_str;
  672. db1_res_t* res;
  673. int count;
  674. if (db_mode == NO_DB) {
  675. rpc->fault(ctx, 500, "Command is not supported in db_mode=0");
  676. return;
  677. }
  678. if (rpc->scan(ctx, "S", &table) != 1) {
  679. rpc->fault(ctx, 500, "Not enough parameters (table to lookup)");
  680. return;
  681. }
  682. if (table.len + 22 > QUERY_LEN) {
  683. rpc->fault(ctx, 500, "Too long database query");
  684. return;
  685. }
  686. if (!DB_CAPABILITY(ul_dbf, DB_CAP_RAW_QUERY)) {
  687. rpc->fault(ctx, 500, "Database does not support raw queries");
  688. return;
  689. }
  690. if (ul_dbf.use_table(ul_dbh, &table) < 0) {
  691. rpc->fault(ctx, 500, "Failed to use table");
  692. return;
  693. }
  694. memset(query, 0, QUERY_LEN);
  695. query_str.len = snprintf(query, QUERY_LEN, "SELECT COUNT(*) FROM %.*s WHERE (UNIX_TIMESTAMP(expires) > 0) AND (expires <= NOW())",
  696. table.len, table.s);
  697. query_str.s = query;
  698. if (ul_dbf.raw_query(ul_dbh, &query_str, &res) < 0) {
  699. rpc->fault(ctx, 500, "Failed to query contact count");
  700. return;
  701. }
  702. count = (int)VAL_INT(ROW_VALUES(RES_ROWS(res)));
  703. ul_dbf.free_result(ul_dbh, res);
  704. rpc->add(ctx, "d", count);
  705. }
  706. static const char* ul_rpc_db_expired_contacts_doc[2] = {
  707. "Tell number of expired contacts in database table (db_mode=3 only)",
  708. 0
  709. };
  710. rpc_export_t ul_rpc[] = {
  711. {"ul.dump", ul_rpc_dump, ul_rpc_dump_doc, 0},
  712. {"ul.lookup", ul_rpc_lookup, ul_rpc_lookup_doc, 0},
  713. {"ul.rm", ul_rpc_rm_aor, ul_rpc_rm_aor_doc, 0},
  714. {"ul.rm_contact", ul_rpc_rm_contact, ul_rpc_rm_contact_doc, 0},
  715. {"ul.flush", ul_rpc_flush, ul_rpc_flush_doc, 0},
  716. {"ul.add", ul_rpc_add, ul_rpc_add_doc, 0},
  717. {"ul.db_users", ul_rpc_db_users, ul_rpc_db_users_doc, 0},
  718. {"ul.db_contacts", ul_rpc_db_contacts, ul_rpc_db_contacts_doc, 0},
  719. {"ul.db_expired_contacts", ul_rpc_db_expired_contacts, ul_rpc_db_expired_contacts_doc, 0},
  720. {0, 0, 0, 0}
  721. };