mi/mi.c

Go to the documentation of this file.
00001 /*
00002  * $Id: mi.c 5472 2009-01-14 22:18:08Z miconda $
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 
00028 /*!
00029  * \file 
00030  * \brief MI :: Attributes
00031  * \ingroup mi
00032  */
00033 
00034 /*!
00035  * \defgroup mi Kamailio Management Interface
00036  * 
00037  * The Kamailio management interface (MI) is a plugin architecture with a few different 
00038  * handlers that gives access to the management interface over various transports.
00039  *
00040  * The Kamailio core and modules register commands to the interface at runtime.
00041  * Look into the various module documentation files for information of these
00042  * commands.
00043  *
00044  */
00045 
00046 #include <string.h>
00047 
00048 #include "../dprint.h"
00049 #include "../mem/mem.h"
00050 #include "../local_route.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 int init_mi_child(void)
00102 {
00103    int i;
00104 
00105    for ( i=0 ; i<mi_cmds_no ; i++ ) {
00106       if ( mi_cmds[i].init_f && mi_cmds[i].init_f()!=0 ) {
00107          LM_ERR("failed to init <%.*s>\n",
00108                mi_cmds[i].name.len,mi_cmds[i].name.s);
00109          return -1;
00110       }
00111    }
00112    if(lrt_do_init_child()<0)
00113    {
00114       LM_ERR("failed to init local route for MI processes\n");
00115       return -1;
00116    }
00117    return 0;
00118 }
00119 
00120 
00121 int register_mi_cmd( mi_cmd_f f, char *name, void *param,
00122                            mi_child_init_f in, unsigned int flags)
00123 {
00124    struct mi_cmd *cmds;
00125    int id;
00126    int len;
00127 
00128    if (f==0 || name==0) {
00129       LM_ERR("invalid params f=%p, name=%s\n", f, name);
00130       return -1;
00131    }
00132 
00133    if (flags&MI_NO_INPUT_FLAG && flags&MI_ASYNC_RPL_FLAG) {
00134       LM_ERR("invalids flags for <%s> - "
00135          "async functions must take input\n",name);
00136    }
00137 
00138    len = strlen(name);
00139    id = get_mi_id(name,len);
00140 
00141    if (lookup_mi_cmd_id( id, name, len)) {
00142       LM_ERR("command <%.*s> already registered\n", len, name);
00143       return -1;
00144    }
00145 
00146    cmds = (struct mi_cmd*)pkg_realloc( mi_cmds,
00147          (mi_cmds_no+1)*sizeof(struct mi_cmd) );
00148    if (cmds==0) {
00149       LM_ERR("no more pkg memory\n");
00150       return -1;
00151    }
00152 
00153    mi_cmds = cmds;
00154    mi_cmds_no++;
00155 
00156    cmds = &cmds[mi_cmds_no-1];
00157 
00158    cmds->f = f;
00159    cmds->init_f = in;
00160    cmds->flags = flags;
00161    cmds->name.s = name;
00162    cmds->name.len = len;
00163    cmds->id = id;
00164    cmds->param = param;
00165 
00166    return 0;
00167 }
00168 
00169 
00170 struct mi_cmd* lookup_mi_cmd( char *name, int len)
00171 {
00172    int id;
00173 
00174    id = get_mi_id(name,len);
00175    return lookup_mi_cmd_id( id, name, len);
00176 }
00177 
00178 
00179 void get_mi_cmds( struct mi_cmd** cmds, int *size)
00180 {
00181    *cmds = mi_cmds;
00182    *size = mi_cmds_no;
00183 }
00184 
00185 

Generated on Wed May 23 20:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6