00001
00026 #ifndef PDT_NO_MI
00027
00028 #include <stdio.h>
00029 #include <unistd.h>
00030 #include <stdlib.h>
00031
00032 #include "../../lib/srdb1/db_op.h"
00033 #include "../../lib/kmi/mi.h"
00034 #include "../../sr_module.h"
00035 #include "../../lib/srdb1/db.h"
00036 #include "../../mem/shm_mem.h"
00037 #include "../../mem/mem.h"
00038 #include "../../dprint.h"
00039 #include "../../parser/parse_uri.h"
00040 #include "../../timer.h"
00041 #include "../../ut.h"
00042 #include "../../locking.h"
00043 #include "../../action.h"
00044 #include "../../mod_fix.h"
00045 #include "../../parser/parse_from.h"
00046
00047 #include "pdtree.h"
00048
00049 static int mi_child_init(void);
00050
00051 static struct mi_root* pdt_mi_reload(struct mi_root*, void* param);
00052 static struct mi_root* pdt_mi_list(struct mi_root*, void* param);
00053
00054 int pdt_mi_init(void);
00055
00056 static mi_export_t mi_cmds[] = {
00057 { "pdt_reload", pdt_mi_reload, 0, 0, mi_child_init },
00058 { "pdt_list", pdt_mi_list, 0, 0, 0 },
00059 { 0, 0, 0, 0, 0}
00060 };
00061
00062
00066 int pdt_init_mi(char *mod)
00067 {
00068 if(register_mi_mod(mod, mi_cmds)!=0)
00069 {
00070 LM_ERR("failed to register MI commands\n");
00071 return -1;
00072 }
00073 return 0;
00074 }
00075
00079 static int mi_child_init(void)
00080 {
00081
00082 if(pdt_init_db()<0)
00083 {
00084 LM_ERR("failed to connect to database\n");
00085 return -1;
00086 }
00087
00088 return 0;
00089 }
00090
00091
00096 static struct mi_root* pdt_mi_reload(struct mi_root *cmd_tree, void *param)
00097 {
00098
00099 if(pdt_load_db()!=0)
00100 {
00101 LM_ERR("cannot re-load info from database\n");
00102 goto error;
00103 }
00104
00105 return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
00106
00107 error:
00108 return init_mi_tree( 500, "Failed to reload",16);
00109 }
00110
00111
00112 int pdt_print_mi_node(pdt_node_t *pt, struct mi_node* rpl, char *code,
00113 int len, str *sdomain, str *sd, str *sp)
00114 {
00115 int i;
00116 struct mi_node* node = NULL;
00117 struct mi_attr* attr= NULL;
00118 str *cl;
00119
00120 if(pt==NULL || len>=PDT_MAX_DEPTH)
00121 return 0;
00122
00123 cl = pdt_get_char_list();
00124
00125 for(i=0; i<cl->len; i++)
00126 {
00127 code[len]=cl->s[i];
00128 if(pt[i].domain.s!=NULL)
00129 {
00130 if((sp->s==NULL && sd->s==NULL)
00131 || (sp->s==NULL && (sd->s!=NULL && pt[i].domain.len==sd->len
00132 && strncasecmp(pt[i].domain.s, sd->s, sd->len)==0))
00133 || (sd->s==NULL && (len+1>=sp->len
00134 && strncmp(code, sp->s, sp->len)==0))
00135 || ((sp->s!=NULL && len+1>=sp->len
00136 && strncmp(code, sp->s, sp->len)==0)
00137 && (sd->s!=NULL && pt[i].domain.len>=sd->len
00138 && strncasecmp(pt[i].domain.s, sd->s, sd->len)==0)))
00139 {
00140 node = add_mi_node_child(rpl, 0, "PDT", 3, 0, 0);
00141 if(node == NULL)
00142 goto error;
00143
00144 attr = add_mi_attr(node, MI_DUP_VALUE, "SDOMAIN", 7,
00145 sdomain->s, sdomain->len);
00146 if(attr == NULL)
00147 goto error;
00148 attr = add_mi_attr(node, MI_DUP_VALUE, "PREFIX", 6,
00149 code, len+1);
00150 if(attr == NULL)
00151 goto error;
00152
00153 attr = add_mi_attr(node, MI_DUP_VALUE,"DOMAIN", 6,
00154 pt[i].domain.s, pt[i].domain.len);
00155 if(attr == NULL)
00156 goto error;
00157 }
00158 }
00159 if(pdt_print_mi_node(pt[i].child, rpl, code, len+1, sdomain, sd, sp)<0)
00160 goto error;
00161 }
00162 return 0;
00163 error:
00164 return -1;
00165 }
00166
00185 struct mi_root* pdt_mi_list(struct mi_root* cmd_tree, void* param)
00186 {
00187 str sd, sp, sdomain;
00188 pdt_tree_t *pt;
00189 struct mi_node* node = NULL;
00190 unsigned int i= 0;
00191 struct mi_root* rpl_tree = NULL;
00192 struct mi_node* rpl = NULL;
00193 static char code_buf[PDT_MAX_DEPTH+1];
00194 int len;
00195 str *cl;
00196 pdt_tree_t **ptree;
00197
00198 ptree = pdt_get_ptree();
00199
00200 if(ptree==NULL)
00201 {
00202 LM_ERR("empty domain list\n");
00203 return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
00204 }
00205
00206 cl = pdt_get_char_list();
00207
00208
00209 sdomain.s = 0;
00210 sdomain.len = 0;
00211 sp.s = 0;
00212 sp.len = 0;
00213 sd.s = 0;
00214 sd.len = 0;
00215 node = cmd_tree->node.kids;
00216 if(node != NULL)
00217 {
00218 sdomain = node->value;
00219 if(sdomain.s == NULL || sdomain.len== 0)
00220 return init_mi_tree( 404, "domain not found", 16);
00221
00222 if(*sdomain.s=='.')
00223 sdomain.s = 0;
00224
00225
00226 node = node->next;
00227 if(node != NULL)
00228 {
00229 sp= node->value;
00230 if(sp.s== NULL || sp.len==0 || *sp.s=='.')
00231 sp.s = NULL;
00232 else {
00233 while(sp.s!=NULL && i!=sp.len)
00234 {
00235 if(strpos(cl->s, sp.s[i]) < 0)
00236 {
00237 LM_ERR("bad prefix [%.*s]\n", sp.len, sp.s);
00238 return init_mi_tree( 400, "bad prefix", 10);
00239 }
00240 i++;
00241 }
00242 }
00243
00244
00245 node= node->next;
00246 if(node != NULL)
00247 {
00248 sd= node->value;
00249 if(sd.s== NULL || sd.len==0 || *sd.s=='.')
00250 sd.s = NULL;
00251 }
00252 }
00253 }
00254
00255 rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
00256 if(rpl_tree == NULL)
00257 return 0;
00258 rpl = &rpl_tree->node;
00259
00260 if(*ptree==0)
00261 return rpl_tree;
00262
00263 pt = *ptree;
00264
00265 while(pt!=NULL)
00266 {
00267 if(sdomain.s==NULL ||
00268 (sdomain.s!=NULL && pt->sdomain.len>=sdomain.len &&
00269 strncmp(pt->sdomain.s, sdomain.s, sdomain.len)==0))
00270 {
00271 len = 0;
00272 if(pdt_print_mi_node(pt->head, rpl, code_buf, len, &pt->sdomain,
00273 &sd, &sp)<0)
00274 goto error;
00275 }
00276 pt = pt->next;
00277 }
00278
00279 return rpl_tree;
00280
00281 error:
00282 free_mi_tree(rpl_tree);
00283 return 0;
00284 }
00285
00286 #endif