📄 domains.c
字号:
{ prev=it; it=it->next; } /* add new sdomain, i.e. new entry in the hash list */ if(it==NULL || scmp(&it->sdomain, sdomain)>0) { ph = init_hash(hl->hash_size, sdomain); /* !!!! check this hash size setting mode */ if(ph==NULL) { LOG(L_ERR, "PDT: pdt_add_to_hash: null pointer returned\n"); goto error1; } if(add_to_hash(ph, sp, sd)<0) { LOG(L_ERR, "PDT: pdt_add_to_hash: could not add to hash\n"); goto error; } if(prev==NULL) /* list initially empty */ hl->hash = ph; else prev->next = ph; ph->next = it; } else /* it is the entry of sdomain, just add a new prefix/domain pair to its hash */ { if(add_to_hash(it, sp, sd)<0) { LOG(L_ERR, "PDT: pdt_add_to_hash: could not add to hash\n"); goto error1; } } lock_release(&hl->hl_lock); return 0;error: free_hash(ph);error1: lock_release(&hl->hl_lock); return -1;}hash_t* pdt_search_hash(hash_list_t* hl, str *sd){ hash_t* it; if(sd==NULL || sd->s==NULL || hl==NULL) { LOG(L_ERR, "PDT:pdt_search_hash: bad parameters\n"); return NULL; } lock_get(&hl->hl_lock); /* search the it position where to insert new domain */ it = hl->hash; while(it!=NULL && scmp(&it->sdomain, sd)<0) it = it->next; if(it==NULL || scmp(&it->sdomain, sd)>0) { lock_release(&hl->hl_lock); return NULL; } lock_release(&hl->hl_lock); return it; }/* returns -1 if any error * returns 1 if does not exist in hash * returns 0 if deleted succesfully * */int remove_from_hash(hash_t *hash, str *sd){ int hash_entry=0; unsigned int dhash; pd_t *it, *prev; pd_op_t *ito, *tmp; if(hash==NULL || sd==NULL || sd->s==NULL) { LOG(L_ERR, "PDT:pdt_remove_from_hash: bad parameters\n"); return -1; /* error */ } /* find the list where the cell must be */ dhash = pdt_compute_hash(sd); hash_entry = get_hash_entry(dhash, hash->hash_size); /* first element of the list */ it = hash->dhash[hash_entry]; /* find the cell in the list */ /* a double linked list in the hash is kept alphabetically * or numerical ordered */ prev = NULL; while(it!=NULL) { if( it->dhash==dhash && it->domain.len==sd->len && strncasecmp(it->domain.s, sd->s, sd->len)==0) break; prev = it; it = it->n; } if(it==NULL) return 1; /* does not exist in hash, nothing to delete */ /* the prefix/domain pair exists and must be deleted */ if(prev!=NULL) prev->n = it->n; else hash->dhash[hash_entry] = it->n; if(it->n) it->n->p = it->p;// free_cell(it); no free, it will be free up by clean_cache /* mark the changes for sync with pdtree */ tmp = new_pd_op(it, 0, PDT_DELETE); if(tmp==NULL) { LOG(L_ERR, "PDT:remove_from_hash: no more shm!Cache not synchon!\n"); return -1; /* error */ } hash->max_id++; tmp->id = hash->max_id; if(hash->diff==NULL) { hash->diff = tmp; return 0; } ito = hash->diff; while(ito->n!=NULL) ito = ito->n; ito->n = tmp; tmp->p = ito; return 0; }/* returns -1 if any error * returns 1 if does not exist in hash * returns 0 if deleted succesfully * */int pdt_remove_from_hash_list(hash_list_t *hl, str* sdomain, str *sd){ hash_t *it; int ret; if(hl==NULL || sd==NULL || sd->s==NULL || sdomain==NULL || sdomain->s==NULL) { LOG(L_ERR, "PDT: pdt_remove_from_hash: bad parameters\n"); return -1; /* wrong parameters, error */ } lock_get(&hl->hl_lock); /* search the it position where to remove from */ it = hl->hash; while(it!=NULL && scmp(&it->sdomain, sdomain)<0) it = it->next; /* sdomain not found, nothing to delete */ if(it==NULL || scmp(&it->sdomain, sdomain)>0) { lock_release(&hl->hl_lock); return 1; /* nothing to delete */ } ret = remove_from_hash(it, sd); lock_release(&hl->hl_lock); return ret; }/*int pdt_remove_hash_from_hash_list(hash_list_t *hl, str* sdomain){ hash_t *it, *prev, *ph; if(hl==NULL || sdomain==NULL || sdomain->s==NULL) { LOG(L_ERR, "PDT: pdt_remove_from_hash: bad parameters\n"); return -1; } lock_get(&hl->hl_lock); // search the it position where to remove from it = hl->hash; prev=NULL; while(it!=NULL && scmp(&it->sdomain, sdomain)<0) { prev = it; it = it->next; } // sdomain not found, nothing to delete if(it==NULL || scmp(&it->sdomain, sdomain)>0) { lock_release(&hl->hl_lock); return 0; } if(prev!=NULL) { prev->next = it->next; it->next = NULL; lock_release(&hl->hl_lock); free_hash(it); it=NULL; return 0; } // remove first element prev = it->next; it->next = NULL; lock_release(&hl->hl_lock); free_hash(it); it=NULL; return 0;}*/str* get_prefix(hash_t *ph, str* sd){ int hash_entry; unsigned int dhash; pd_t* it; if(ph==NULL || ph->dhash==NULL || ph->hash_size>MAX_HASH_SIZE) { LOG(L_ERR, "PDT:pdt_get_prefix: bad parameters\n"); return NULL; } dhash = pdt_compute_hash(sd); hash_entry = get_hash_entry(dhash, ph->hash_size); it = ph->dhash[hash_entry]; while(it!=NULL && it->dhash<=dhash) { if(it->dhash==dhash && it->domain.len==sd->len && strncasecmp(it->domain.s, sd->s, sd->len)==0) return &it->prefix; it = it->n; } return NULL;}str* pdt_get_prefix(hash_list_t *hl, str*sdomain, str* sd){ hash_t *it; str *d; if(hl==NULL || sd==NULL || sd->s==NULL || sdomain==NULL || sdomain->s==NULL) { LOG(L_ERR, "PDT: pdt_get_prefix: bad parameters\n"); return NULL; } lock_get(&hl->hl_lock); it = pdt_search_hash(hl, sdomain); if(it==NULL) { lock_release(&hl->hl_lock); return NULL; } d = get_prefix(it, sd); lock_release(&hl->hl_lock); return d; }int check_pd(hash_t *ph, str *sp, str *sd){ int i; unsigned int dhash; pd_t* it; if(ph==NULL || sp==NULL || sd==NULL) { LOG(L_ERR, "PDT:check_pd: bad parameters\n"); return -1; } dhash = pdt_compute_hash(sd); for(i=0; i<ph->hash_size; i++) { it = ph->dhash[i]; while(it != NULL) { if((it->domain.len==sd->len && strncasecmp(it->domain.s, sd->s, sd->len)==0) || (it->prefix.len==sp->len && strncasecmp(it->prefix.s, sp->s, sp->len)==0)) return 1; it = it->n; } } return 0;}/* returns * 1 if domain already exists * 0 if domain does not exist * -1 if any error * */int pdt_check_pd(hash_list_t *hl, str* sdomain, str *sp, str *sd){ hash_t *it; int d; if(hl==NULL || sd==NULL || sd->s==NULL || sdomain==NULL || sdomain->s==NULL) { LOG(L_ERR, "PDT: pdt_check_pd: bad parameters\n"); return -1; } lock_get(&hl->hl_lock); /* search the it position */ it = hl->hash; while(it!=NULL && scmp(&it->sdomain, sdomain)<0) it = it->next; if(it==NULL || scmp(&it->sdomain, sdomain)>0) { lock_release(&hl->hl_lock); return 0; } d = check_pd(it, sp, sd); lock_release(&hl->hl_lock); return d; }void pdt_print_hash_list(hash_list_t* hl){ int i, count; pd_t *it; hash_t *hash; hash = hl->hash; lock_get(&hl->hl_lock); while(hash!=NULL) { DBG("PDT: print_hash: SDOMAIN=%.*s\n", hash->sdomain.len, hash->sdomain.s); for(i=0; i<hash->hash_size; i++) { it = hash->dhash[i]; DBG(" PDT:print_hash: entry<%d>:\n", i); count = 0; while(it!=NULL) { DBG(" PDT:print_hash: |Domain: %.*s |Code: %.*s | DHash:%u \n", it->domain.len, it->domain.s, it->prefix.len, it->prefix.s, it->dhash); it = it->n; count++; } DBG(" PDT:print_hash: ---- hash entry has %d records\n\n", count); } hash = hash->next; } lock_release(&hl->hl_lock);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -