/** * Store potential poison in the cache (only if hardening disabled). * The rrset is stored in the cache but removed from the message. * So that it will be used for infrastructure purposes, but not be * returned to the client. * @param pkt: packet * @param msg: message parsed * @param env: environment with cache * @param rrset: to store. */ static void store_rrset(ldns_buffer* pkt, struct msg_parse* msg, struct module_env* env, struct rrset_parse* rrset) { struct ub_packed_rrset_key* k; struct packed_rrset_data* d; struct rrset_ref ref; uint32_t now = *env->now; k = alloc_special_obtain(env->alloc); if(!k) return; k->entry.data = NULL; if(!parse_copy_decompress_rrset(pkt, msg, rrset, NULL, k)) { alloc_special_release(env->alloc, k); return; } d = (struct packed_rrset_data*)k->entry.data; packed_rrset_ttl_add(d, now); ref.key = k; ref.id = k->id; /*ignore ret: it was in the cache, ref updated */ (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, now); }
struct ub_packed_rrset_key* packed_rrset_copy_alloc(struct ub_packed_rrset_key* key, struct alloc_cache* alloc, time_t now) { struct packed_rrset_data* fd, *dd; struct ub_packed_rrset_key* dk = alloc_special_obtain(alloc); if(!dk) return NULL; fd = (struct packed_rrset_data*)key->entry.data; dk->entry.hash = key->entry.hash; dk->rk = key->rk; dk->rk.dname = (uint8_t*)memdup(key->rk.dname, key->rk.dname_len); if(!dk->rk.dname) { alloc_special_release(alloc, dk); return NULL; } dd = (struct packed_rrset_data*)memdup(fd, packed_rrset_sizeof(fd)); if(!dd) { free(dk->rk.dname); alloc_special_release(alloc, dk); return NULL; } packed_rrset_ptr_fixup(dd); dk->entry.data = (void*)dd; packed_rrset_ttl_add(dd, now); return dk; }
/** move entry into cache */ static int move_into_cache(struct ub_packed_rrset_key* k, struct packed_rrset_data* d, struct worker* worker) { struct ub_packed_rrset_key* ak; struct packed_rrset_data* ad; size_t s, i, num = d->count + d->rrsig_count; struct rrset_ref ref; uint8_t* p; ak = alloc_special_obtain(&worker->alloc); if(!ak) { log_warn("error out of memory"); return 0; } ak->entry.data = NULL; ak->rk = k->rk; ak->entry.hash = rrset_key_hash(&k->rk); ak->rk.dname = (uint8_t*)memdup(k->rk.dname, k->rk.dname_len); if(!ak->rk.dname) { log_warn("error out of memory"); ub_packed_rrset_parsedelete(ak, &worker->alloc); return 0; } s = sizeof(*ad) + (sizeof(size_t) + sizeof(uint8_t*) + sizeof(time_t))* num; for(i=0; i<num; i++) s += d->rr_len[i]; ad = (struct packed_rrset_data*)malloc(s); if(!ad) { log_warn("error out of memory"); ub_packed_rrset_parsedelete(ak, &worker->alloc); return 0; } p = (uint8_t*)ad; memmove(p, d, sizeof(*ad)); p += sizeof(*ad); memmove(p, &d->rr_len[0], sizeof(size_t)*num); p += sizeof(size_t)*num; memmove(p, &d->rr_data[0], sizeof(uint8_t*)*num); p += sizeof(uint8_t*)*num; memmove(p, &d->rr_ttl[0], sizeof(time_t)*num); p += sizeof(time_t)*num; for(i=0; i<num; i++) { memmove(p, d->rr_data[i], d->rr_len[i]); p += d->rr_len[i]; } packed_rrset_ptr_fixup(ad); ak->entry.data = ad; ref.key = ak; ref.id = ak->id; (void)rrset_cache_update(worker->env.rrset_cache, &ref, &worker->alloc, *worker->env.now); return 1; }
/** allocate (special) rrset keys, return 0 on error */ static int repinfo_alloc_rrset_keys(struct reply_info* rep, struct alloc_cache* alloc, struct regional* region) { size_t i; for(i=0; i<rep->rrset_count; i++) { if(region) { rep->rrsets[i] = (struct ub_packed_rrset_key*) regional_alloc(region, sizeof(struct ub_packed_rrset_key)); if(rep->rrsets[i]) { memset(rep->rrsets[i], 0, sizeof(struct ub_packed_rrset_key)); rep->rrsets[i]->entry.key = rep->rrsets[i]; } } else rep->rrsets[i] = alloc_special_obtain(alloc); if(!rep->rrsets[i]) return 0; rep->rrsets[i]->entry.data = NULL; } return 1; }