int pdt_add_to_tree(pdt_tree_t **dpt, str *sdomain, str *code, str *domain) { pdt_tree_t *ndl, *it, *prev; if( sdomain==NULL || sdomain->s==NULL || code==NULL || code->s==NULL || domain==NULL || domain->s==NULL) { LM_ERR("bad parameters\n"); return -1; } ndl = NULL; it = *dpt; prev = NULL; /* search the it position before which to insert new domain */ while(it!=NULL && str_strcmp(&it->sdomain, sdomain)<0) { prev = it; it = it->next; } // printf("sdomain:%.*s\n", sdomain->len, sdomain->s); /* add new sdomain*/ if(it==NULL || str_strcmp(&it->sdomain, sdomain)>0) { ndl = pdt_init_tree(sdomain); if(ndl==NULL) { LM_ERR("no more shm memory\n"); return -1; } if(add_to_tree(ndl, code, domain)<0) { LM_ERR("internal error!\n"); return -1; } ndl->next = it; /* new domain must be added as first element */ if(prev==NULL) *dpt = ndl; else prev->next=ndl; } else /* add (prefix, code) to already present sdomain */ if(add_to_tree(it, code, domain)<0) { LM_ERR("internal error!\n"); return -1; } return 0; }
/** * init module function */ static int mod_init(void) { DBG("PDT: initializing...\n"); if(hs_two_pow<0) { LOG(L_ERR, "PDT:mod_init: hash_size_two_pow must be" " positive and less than %d\n", MAX_HSIZE_TWO_POW); return -1; } prefix.len = strlen(prefix.s); /* binding to mysql module */ if(bind_dbmod(db_url, &pdt_dbf)) { LOG(L_ERR, "PDT:mod_init: Database module not found\n"); return -1; } if (!DB_CAPABILITY(pdt_dbf, DB_CAP_ALL)) { LOG(L_ERR, "PDT: mod_init: Database module does not " "implement all functions needed by the module\n"); return -1; } /* open a connection with the database */ db_con = pdt_dbf.init(db_url); if(db_con==NULL) { LOG(L_ERR, "PDT: mod_init: Error while connecting to database\n"); return -1; } if (pdt_dbf.use_table(db_con, db_table) < 0) { LOG(L_ERR, "PDT: mod_init: Error in use_table\n"); goto error1; } DBG("PDT: mod_init: Database connection opened successfully\n"); /* init the hash and tree in share memory */ if( (_dhash = pdt_init_hash(hs_two_pow)) == NULL) { LOG(L_ERR, "PDT:mod_init: domain hash could not be allocated\n"); goto error1; } if( (_ptree = pdt_init_tree()) == NULL) { LOG(L_ERR, "PDT:mod_init: prefix tree could not be allocated\n"); goto error2; } /* loading all information from database */ if(pdt_load_db()!=0) { LOG(L_ERR, "PDT:mod_init: cannot load info from database\n"); goto error3; } pdt_dbf.close(db_con); db_con = 0; pdt_print_tree(_ptree); DBG("PDT:mod_init: -------------------\n"); pdt_print_hash(_dhash); last_sync = time(NULL); register_timer(pdt_clean_cache, 0, clean_time); /* success code */ return 0; error3: if(_ptree!=NULL) { pdt_free_tree(_ptree); _ptree = 0; } error2: if(_dhash!=NULL) { pdt_free_hash(_dhash); _dhash = 0; } error1: if(db_con!=NULL) { pdt_dbf.close(db_con); db_con = 0; } return -1; }