domain_rpc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Domain module
  3. *
  4. * Copyright (C) 2002-2003 Juha Heinanen
  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 it under
  9. * the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * 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 along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "../../dprint.h"
  23. #include "../../lib/srdb2/db.h"
  24. #include "../../ut.h"
  25. #include "../../mem/mem.h"
  26. #include "hash.h"
  27. #include "uid_domain_mod.h"
  28. #include "domain_rpc.h"
  29. static void dump_domain(rpc_t* rpc, void* ctx, domain_t* d)
  30. {
  31. avp_t* a;
  32. void* st;
  33. int i;
  34. str* name;
  35. int_str val;
  36. if (rpc->add(ctx, "{", &st) < 0) return;
  37. if (rpc->struct_add(st, "S", "did", &d->did) < 0) return;
  38. for(i = 0; i < d->n; i++) {
  39. if (rpc->struct_add(st, "S", "domain", &d->domain[i]) < 0) return;
  40. if (rpc->struct_add(st, "d", "flags", d->flags[i]) < 0) return;
  41. }
  42. a = d->attrs;
  43. while(a) {
  44. name = get_avp_name(a);
  45. get_avp_val(a, &val);
  46. if (a->flags & AVP_VAL_STR) {
  47. if (rpc->struct_printf(st, "attr", "%.*s=%.*s",
  48. STR_FMT(name), STR_FMT(&val.s)) < 0) return;
  49. } else {
  50. if (rpc->struct_printf(st, "attr", "%.*s=%d",
  51. STR_FMT(name), val.n) < 0) return;
  52. }
  53. a = a->next;
  54. }
  55. }
  56. void dump_domain_list(rpc_t* rpc, void* ctx, domain_t* list)
  57. {
  58. while(list) {
  59. dump_domain(rpc, ctx, list);
  60. list = list->next;
  61. }
  62. }
  63. static const char* domain_reload_doc[2] = {
  64. "Reload domain table from database",
  65. 0
  66. };
  67. /*
  68. * Fifo function to reload domain table
  69. */
  70. static void domain_reload(rpc_t* rpc, void* ctx)
  71. {
  72. if (!db_mode) {
  73. rpc->fault(ctx, 200, "Server Domain Cache Disabled");
  74. return;
  75. }
  76. if (reload_domain_list() < 0) {
  77. rpc->fault(ctx, 400, "Domain Table Reload Failed");
  78. }
  79. }
  80. static const char* domain_dump_doc[2] = {
  81. "Return the contents of domain table",
  82. 0
  83. };
  84. /*
  85. * Fifo function to print domains from current hash table
  86. */
  87. static void domain_dump(rpc_t* rpc, void* ctx)
  88. {
  89. domain_t* list;
  90. if (!db_mode) {
  91. rpc->fault(ctx, 400, "Server Domain Cache Disabled");
  92. return;
  93. }
  94. if (*active_hash == hash_1) list = *domains_1;
  95. else list = *domains_2;
  96. dump_domain_list(rpc, ctx, list);
  97. }
  98. rpc_export_t domain_rpc[] = {
  99. {"domain.reload", domain_reload, domain_reload_doc, 0},
  100. {"domain.dump", domain_dump, domain_dump_doc, 0},
  101. {0, 0, 0, 0}
  102. };