/** create rate data item for name, number 1 in now */ static void infra_create_ratedata(struct infra_cache* infra, uint8_t* name, size_t namelen, time_t timenow) { hashvalue_t h = dname_query_hash(name, 0xab); struct rate_key* k = (struct rate_key*)calloc(1, sizeof(*k)); struct rate_data* d = (struct rate_data*)calloc(1, sizeof(*d)); if(!k || !d) { free(k); free(d); return; /* alloc failure */ } k->namelen = namelen; k->name = memdup(name, namelen); if(!k->name) { free(k); free(d); return; /* alloc failure */ } lock_rw_init(&k->entry.lock); k->entry.hash = h; k->entry.key = k; k->entry.data = d; d->qps[0] = 1; d->timestamp[0] = timenow; slabhash_insert(infra->domain_rates, h, &k->entry, d, NULL); }
/** setup new special type */ static void alloc_setup_special(alloc_special_type* t) { memset(t, 0, sizeof(*t)); lock_rw_init(&t->entry.lock); t->entry.key = t; }
/** * Create and init a new entry for a host * @param infra: infra structure with config parameters. * @param addr: host address. * @param addrlen: length of addr. * @param name: name of zone * @param namelen: length of name. * @param tm: time now. * @return: the new entry or NULL on malloc failure. */ static struct lruhash_entry* new_entry(struct infra_cache* infra, struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* name, size_t namelen, time_t tm) { struct infra_data* data; struct infra_key* key = (struct infra_key*)malloc(sizeof(*key)); if(!key) return NULL; data = (struct infra_data*)malloc(sizeof(struct infra_data)); if(!data) { free(key); return NULL; } key->zonename = memdup(name, namelen); if(!key->zonename) { free(key); free(data); return NULL; } key->namelen = namelen; lock_rw_init(&key->entry.lock); key->entry.hash = hash_infra(addr, addrlen, name); key->entry.key = (void*)key; key->entry.data = (void*)data; key->addrlen = addrlen; memcpy(&key->addr, addr, addrlen); data_entry_init(infra, &key->entry, tm); return &key->entry; }
/** create a new localzone */ static struct local_zone* local_zone_create(uint8_t* nm, size_t len, int labs, enum localzone_type t, uint16_t dclass) { struct local_zone* z = (struct local_zone*)calloc(1, sizeof(*z)); if(!z) { return NULL; } z->node.key = z; z->dclass = dclass; z->type = t; z->name = nm; z->namelen = len; z->namelabs = labs; lock_rw_init(&z->lock); z->region = regional_create(); if(!z->region) { free(z); return NULL; } rbtree_init(&z->data, &local_data_cmp); lock_protect(&z->lock, &z->parent, sizeof(*z)-sizeof(rbnode_t)); /* also the zones->lock protects node, parent, name*, class */ return z; }
/** allocate new key, fill in hash */ static testkey_t* newkey(int id) { testkey_t* k = (testkey_t*)calloc(1, sizeof(testkey_t)); if(!k) fatal_exit("out of memory"); k->id = id; k->entry.hash = myhash(id); k->entry.key = k; lock_rw_init(&k->entry.lock); return k; }
struct local_zones* local_zones_create(void) { struct local_zones* zones = (struct local_zones*)calloc(1, sizeof(*zones)); if(!zones) return NULL; rbtree_init(&zones->ztree, &local_zone_cmp); lock_rw_init(&zones->lock); lock_protect(&zones->lock, &zones->ztree, sizeof(zones->ztree)); /* also lock protects the rbnode's in struct local_zone */ return zones; }
struct msgreply_entry* query_info_entrysetup(struct query_info* q, struct reply_info* r, hashvalue_t h) { struct msgreply_entry* e = (struct msgreply_entry*)malloc( sizeof(struct msgreply_entry)); if(!e) return NULL; memcpy(&e->key, q, sizeof(*q)); e->entry.hash = h; e->entry.key = e; e->entry.data = r; lock_rw_init(&e->entry.lock); lock_protect(&e->entry.lock, &e->key, sizeof(e->key)); lock_protect(&e->entry.lock, &e->entry.hash, sizeof(e->entry.hash) + sizeof(e->entry.key) + sizeof(e->entry.data)); lock_protect(&e->entry.lock, e->key.qname, e->key.qname_len); q->qname = NULL; return e; }