mi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * $Id: mi.c 4565 2008-08-05 14:58:52Z klaus_darilion $
  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. *
  23. * History:
  24. * ---------
  25. * 2006-09-08 first version (bogdan)
  26. */
  27. /*!
  28. * \file
  29. * \brief MI :: Attributes
  30. * \ingroup mi
  31. */
  32. /*!
  33. * \defgroup mi Kamailio Management Interface
  34. *
  35. * The Kamailio management interface (MI) is a plugin architecture with a few different
  36. * handlers that gives access to the management interface over various transports.
  37. *
  38. * The Kamailio core and modules register commands to the interface at runtime.
  39. * Look into the various module documentation files for information of these
  40. * commands.
  41. *
  42. */
  43. #include <string.h>
  44. #include "../../dprint.h"
  45. #include "../../sr_module.h"
  46. #include "mi_mem.h"
  47. #include "mi.h"
  48. static struct mi_cmd* mi_cmds = 0;
  49. static int mi_cmds_no = 0;
  50. static inline int get_mi_id( char *name, int len)
  51. {
  52. int n;
  53. int i;
  54. for( n=0,i=0 ; i<len ; n+=name[i] ,i++ );
  55. return n;
  56. }
  57. static inline struct mi_cmd* lookup_mi_cmd_id(int id,char *name, int len)
  58. {
  59. int i;
  60. for( i=0 ; i<mi_cmds_no ; i++ ) {
  61. if ( id==mi_cmds[i].id && len==mi_cmds[i].name.len &&
  62. memcmp(mi_cmds[i].name.s,name,len)==0 )
  63. return &mi_cmds[i];
  64. }
  65. return 0;
  66. }
  67. int register_mi_mod( char *mod_name, mi_export_t *mis)
  68. {
  69. int ret;
  70. int i;
  71. if (mis==0)
  72. return 0;
  73. for ( i=0 ; mis[i].name ; i++ ) {
  74. ret = register_mi_cmd( mis[i].cmd, mis[i].name, mis[i].param,
  75. mis[i].init_f, mis[i].flags);
  76. if (ret!=0) {
  77. LM_ERR("failed to register cmd <%s> for module %s\n",
  78. mis[i].name,mod_name);
  79. }
  80. }
  81. return 0;
  82. }
  83. static int mi_commands_initialized = 0;
  84. /**
  85. * Init a process to work properly for MI commands
  86. * - rank: rank of the process (PROC_XYZ...)
  87. * - mode: 0 - don't try to init worker for SIP commands
  88. * 1 - try to init worker for SIP commands
  89. */
  90. int init_mi_child(int rank, int mode)
  91. {
  92. int i;
  93. if(mi_commands_initialized)
  94. return 0;
  95. mi_commands_initialized = 1;
  96. for ( i=0 ; i<mi_cmds_no ; i++ ) {
  97. if ( mi_cmds[i].init_f && mi_cmds[i].init_f()!=0 ) {
  98. LM_ERR("failed to init <%.*s>\n",
  99. mi_cmds[i].name.len,mi_cmds[i].name.s);
  100. return -1;
  101. }
  102. }
  103. if(mode==1) {
  104. if(is_sip_worker(rank)) {
  105. LM_DBG("initalizing proc rpc for sip handling\n");
  106. if(init_child(PROC_SIPRPC)<0) {
  107. LM_ERR("failed to init proc rpc for sip handling\n");
  108. return -1;
  109. }
  110. }
  111. }
  112. return 0;
  113. }
  114. int register_mi_cmd( mi_cmd_f f, char *name, void *param,
  115. mi_child_init_f in, unsigned int flags)
  116. {
  117. struct mi_cmd *cmds;
  118. int id;
  119. int len;
  120. if (f==0 || name==0) {
  121. LM_ERR("invalid params f=%p, name=%s\n", f, name);
  122. return -1;
  123. }
  124. if (flags&MI_NO_INPUT_FLAG && flags&MI_ASYNC_RPL_FLAG) {
  125. LM_ERR("invalids flags for <%s> - "
  126. "async functions must take input\n",name);
  127. }
  128. len = strlen(name);
  129. id = get_mi_id(name,len);
  130. if (lookup_mi_cmd_id( id, name, len)) {
  131. LM_ERR("command <%.*s> already registered\n", len, name);
  132. return -1;
  133. }
  134. cmds = (struct mi_cmd*)mi_realloc( mi_cmds,
  135. (mi_cmds_no+1)*sizeof(struct mi_cmd) );
  136. if (cmds==0) {
  137. LM_ERR("no more pkg memory\n");
  138. return -1;
  139. }
  140. mi_cmds = cmds;
  141. mi_cmds_no++;
  142. cmds = &cmds[mi_cmds_no-1];
  143. cmds->f = f;
  144. cmds->init_f = in;
  145. cmds->flags = flags;
  146. cmds->name.s = name;
  147. cmds->name.len = len;
  148. cmds->id = id;
  149. cmds->param = param;
  150. return 0;
  151. }
  152. struct mi_cmd* lookup_mi_cmd( char *name, int len)
  153. {
  154. int id;
  155. id = get_mi_id(name,len);
  156. return lookup_mi_cmd_id( id, name, len);
  157. }
  158. void get_mi_cmds( struct mi_cmd** cmds, int *size)
  159. {
  160. *cmds = mi_cmds;
  161. *size = mi_cmds_no;
  162. }