corex_rpc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  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. */
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "../../dprint.h"
  26. #include "../../ut.h"
  27. #include "../../socket_info.h"
  28. #include "../../name_alias.h"
  29. #include "../../mem/shm_mem.h"
  30. #include "../../rpc.h"
  31. #include "../../rpc_lookup.h"
  32. static const char* corex_rpc_list_sockets_doc[2] = {
  33. "List listening sockets",
  34. 0
  35. };
  36. /*
  37. * RPC command to list the listening sockets
  38. */
  39. static void corex_rpc_list_sockets(rpc_t* rpc, void* ctx)
  40. {
  41. void* th;
  42. void* ih;
  43. struct socket_info *si;
  44. struct socket_info** list;
  45. struct addr_info* ai;
  46. unsigned short proto;
  47. proto=PROTO_UDP;
  48. do {
  49. list=get_sock_info_list(proto);
  50. for(si=list?*list:0; si; si=si->next)
  51. {
  52. /* add structure node */
  53. if (rpc->add(ctx, "{", &th) < 0)
  54. {
  55. rpc->fault(ctx, 500, "Internal error socket structure");
  56. return;
  57. }
  58. if(rpc->struct_add(th, "ss{",
  59. "PROTO", get_valid_proto_name(proto),
  60. "NAME", si->name.s,
  61. "ADDRLIST", &ih)<0)
  62. {
  63. rpc->fault(ctx, 500, "Internal error address list structure");
  64. return;
  65. }
  66. if(rpc->struct_add(ih, "s", "ADDR", si->address_str.s)<0)
  67. {
  68. rpc->fault(ctx, 500, "Internal error address structure");
  69. return;
  70. }
  71. if (si->addr_info_lst)
  72. {
  73. for (ai=si->addr_info_lst; ai; ai=ai->next)
  74. {
  75. if(rpc->struct_add(ih, "s", "ADDR", ai->address_str.s)<0)
  76. {
  77. rpc->fault(ctx, 500,
  78. "Internal error extra address structure");
  79. return;
  80. }
  81. }
  82. }
  83. if(rpc->struct_add(th, "ssss",
  84. "PORT", si->port_no_str.s,
  85. "MCAST", si->flags & SI_IS_MCAST ? "yes" : "no",
  86. "MHOMED", si->flags & SI_IS_MHOMED? "yes" : "no",
  87. "ADVERTISE", si->useinfo.name.s?si->useinfo.name.s:"-")<0)
  88. {
  89. rpc->fault(ctx, 500, "Internal error attrs structure");
  90. return;
  91. }
  92. }
  93. } while((proto=next_proto(proto)));
  94. return;
  95. }
  96. static const char* corex_rpc_list_aliases_doc[2] = {
  97. "List socket aliases",
  98. 0
  99. };
  100. /*
  101. * RPC command to list the socket aliases
  102. */
  103. static void corex_rpc_list_aliases(rpc_t* rpc, void* ctx)
  104. {
  105. void* th;
  106. struct host_alias* a;
  107. for(a=aliases; a; a=a->next)
  108. {
  109. /* add structure node */
  110. if (rpc->add(ctx, "{", &th) < 0)
  111. {
  112. rpc->fault(ctx, 500, "Internal error alias structure");
  113. return;
  114. }
  115. if(rpc->struct_add(th, "sSd",
  116. "PROTO", get_valid_proto_name(a->proto),
  117. "ADDR", &a->alias,
  118. "PORT", a->port)<0)
  119. {
  120. rpc->fault(ctx, 500, "Internal error alias attributes");
  121. return;
  122. }
  123. }
  124. return;
  125. }
  126. static const char* corex_rpc_shm_status_doc[2] = {
  127. "Trigger shm status dump to syslog",
  128. 0
  129. };
  130. /*
  131. * RPC command to dump shm status to syslog
  132. */
  133. static void corex_rpc_shm_status(rpc_t* rpc, void* ctx)
  134. {
  135. LM_DBG("printing shared memory status report\n");
  136. shm_status();
  137. }
  138. static const char* corex_rpc_shm_summary_doc[2] = {
  139. "Trigger shm summary dump to syslog",
  140. 0
  141. };
  142. /*
  143. * RPC command to dump shm summary to syslog
  144. */
  145. static void corex_rpc_shm_summary(rpc_t* rpc, void* ctx)
  146. {
  147. LM_DBG("printing shared memory summary report\n");
  148. shm_sums();
  149. }
  150. rpc_export_t corex_rpc_cmds[] = {
  151. {"corex.list_sockets", corex_rpc_list_sockets,
  152. corex_rpc_list_sockets_doc, RET_ARRAY},
  153. {"corex.list_aliases", corex_rpc_list_aliases,
  154. corex_rpc_list_aliases_doc, RET_ARRAY},
  155. {"corex.shm_status", corex_rpc_shm_status,
  156. corex_rpc_shm_status_doc, 0},
  157. {"corex.shm_summary", corex_rpc_shm_summary,
  158. corex_rpc_shm_summary_doc, 0},
  159. {0, 0, 0, 0}
  160. };
  161. /**
  162. * register RPC commands
  163. */
  164. int corex_init_rpc(void)
  165. {
  166. if (rpc_register_array(corex_rpc_cmds)!=0)
  167. {
  168. LM_ERR("failed to register RPC commands\n");
  169. return -1;
  170. }
  171. return 0;
  172. }