lib/kmi/mi.c

Go to the documentation of this file.
00001 /*
00002  * $Id: mi.c 4565 2008-08-05 14:58:52Z klaus_darilion $
00003  *
00004  * Copyright (C) 2006 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  *
00023  * History:
00024  * ---------
00025  *  2006-09-08  first version (bogdan)
00026  */
00027 
00046 #include <string.h>
00047 
00048 #include "../../dprint.h"
00049 #include "../../sr_module.h"
00050 #include "mi_mem.h"
00051 #include "mi.h"
00052 
00053 static struct mi_cmd*  mi_cmds = 0;
00054 static int mi_cmds_no = 0;
00055 
00056 
00057 static inline int get_mi_id( char *name, int len)
00058 {
00059         int n;
00060         int i;
00061 
00062         for( n=0,i=0 ; i<len ; n+=name[i] ,i++ );
00063         return n;
00064 }
00065 
00066 
00067 static inline struct mi_cmd* lookup_mi_cmd_id(int id,char *name, int len)
00068 {
00069         int i;
00070 
00071         for( i=0 ; i<mi_cmds_no ; i++ ) {
00072                 if ( id==mi_cmds[i].id && len==mi_cmds[i].name.len &&
00073                 memcmp(mi_cmds[i].name.s,name,len)==0 )
00074                         return &mi_cmds[i];
00075         }
00076 
00077         return 0;
00078 }
00079 
00080 
00081 int register_mi_mod( char *mod_name, mi_export_t *mis)
00082 {
00083         int ret;
00084         int i;
00085 
00086         if (mis==0)
00087                 return 0;
00088 
00089         for ( i=0 ; mis[i].name ; i++ ) {
00090                 ret = register_mi_cmd( mis[i].cmd, mis[i].name, mis[i].param,
00091                         mis[i].init_f, mis[i].flags);
00092                 if (ret!=0) {
00093                         LM_ERR("failed to register cmd <%s> for module %s\n",
00094                                         mis[i].name,mod_name);
00095                 }
00096         }
00097         return 0;
00098 }
00099 
00100 
00101 static int mi_commands_initialized = 0;
00102 
00103 
00110 int init_mi_child(int rank, int mode)
00111 {
00112         int i;
00113 
00114         if(mi_commands_initialized)
00115                 return 0;
00116         mi_commands_initialized = 1;
00117         for ( i=0 ; i<mi_cmds_no ; i++ ) {
00118                 if ( mi_cmds[i].init_f && mi_cmds[i].init_f()!=0 ) {
00119                         LM_ERR("failed to init <%.*s>\n",
00120                                         mi_cmds[i].name.len,mi_cmds[i].name.s);
00121                         return -1;
00122                 }
00123         }
00124         if(mode==1) {
00125                 if(is_sip_worker(rank)) {
00126                         LM_DBG("initalizing proc rpc for sip handling\n");
00127                         if(init_child(PROC_SIPRPC)<0) {
00128                                 LM_ERR("failed to init proc rpc for sip handling\n");
00129                                 return -1;
00130                         }
00131                 }
00132         }
00133         return 0;
00134 }
00135 
00136 
00137 
00138 int register_mi_cmd( mi_cmd_f f, char *name, void *param,
00139                                                                         mi_child_init_f in, unsigned int flags)
00140 {
00141         struct mi_cmd *cmds;
00142         int id;
00143         int len;
00144 
00145         if (f==0 || name==0) {
00146                 LM_ERR("invalid params f=%p, name=%s\n", f, name);
00147                 return -1;
00148         }
00149 
00150         if (flags&MI_NO_INPUT_FLAG && flags&MI_ASYNC_RPL_FLAG) {
00151                 LM_ERR("invalids flags for <%s> - "
00152                         "async functions must take input\n",name);
00153         }
00154 
00155         len = strlen(name);
00156         id = get_mi_id(name,len);
00157 
00158         if (lookup_mi_cmd_id( id, name, len)) {
00159                 LM_ERR("command <%.*s> already registered\n", len, name);
00160                 return -1;
00161         }
00162 
00163         cmds = (struct mi_cmd*)mi_realloc( mi_cmds,
00164                         (mi_cmds_no+1)*sizeof(struct mi_cmd) );
00165         if (cmds==0) {
00166                 LM_ERR("no more pkg memory\n");
00167                 return -1;
00168         }
00169 
00170         mi_cmds = cmds;
00171         mi_cmds_no++;
00172 
00173         cmds = &cmds[mi_cmds_no-1];
00174 
00175         cmds->f = f;
00176         cmds->init_f = in;
00177         cmds->flags = flags;
00178         cmds->name.s = name;
00179         cmds->name.len = len;
00180         cmds->id = id;
00181         cmds->param = param;
00182 
00183         return 0;
00184 }
00185 
00186 
00187 struct mi_cmd* lookup_mi_cmd( char *name, int len)
00188 {
00189         int id;
00190 
00191         id = get_mi_id(name,len);
00192         return lookup_mi_cmd_id( id, name, len);
00193 }
00194 
00195 
00196 void get_mi_cmds( struct mi_cmd** cmds, int *size)
00197 {
00198         *cmds = mi_cmds;
00199         *size = mi_cmds_no;
00200 }
00201 
00202