cachedb_handle_query(struct module_qstate* qstate,
	struct cachedb_qstate* ATTR_UNUSED(iq),
	struct cachedb_env* ie, int id)
{
	/* check if we are enabled, and skip if so */
	if(!ie->enabled) {
		/* pass request to next module */
		qstate->ext_state[id] = module_wait_module;
		return;
	}

	if(qstate->blacklist || qstate->no_cache_lookup) {
		/* cache is blacklisted or we are instructed from edns to not look */
		/* pass request to next module */
		qstate->ext_state[id] = module_wait_module;
		return;
	}

	/* lookup inside unbound's internal cache.
	 * This does not look for expired entries. */
	if(cachedb_intcache_lookup(qstate)) {
		if(verbosity >= VERB_ALGO) {
			if(qstate->return_msg->rep)
				log_dns_msg("cachedb internal cache lookup",
					&qstate->return_msg->qinfo,
					qstate->return_msg->rep);
			else log_info("cachedb internal cache lookup: rcode %s",
				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
				:"??");
		}
		/* we are done with the query */
		qstate->ext_state[id] = module_finished;
		return;
	}

	/* ask backend cache to see if we have data */
	if(cachedb_extcache_lookup(qstate, ie)) {
		if(verbosity >= VERB_ALGO)
			log_dns_msg(ie->backend->name,
				&qstate->return_msg->qinfo,
				qstate->return_msg->rep);
		/* store this result in internal cache */
		cachedb_intcache_store(qstate);
		/* In case we have expired data but there is a client timer for expired
		 * answers, pass execution to next module in order to try updating the
		 * data first.
		 * TODO: this needs revisit. The expired data stored from cachedb has
		 * 0 TTL which is picked up by iterator later when looking in the cache.
		 * Document that ext cachedb does not work properly with
		 * serve_stale_reply_ttl yet. */
		if(qstate->need_refetch && qstate->serve_expired_data &&
			qstate->serve_expired_data->timer) {
				qstate->return_msg = NULL;
				qstate->ext_state[id] = module_wait_module;
				return;
		}
		/* we are done with the query */
		qstate->ext_state[id] = module_finished;
		return;
	}

	/* no cache fetches */
	/* pass request to next module */
	qstate->ext_state[id] = module_wait_module;
}
cachedb_handle_query(struct module_qstate* qstate,
	struct cachedb_qstate* ATTR_UNUSED(iq),
	struct cachedb_env* ie, int id)
{
	 
	if(!ie->enabled) {
		 
		qstate->ext_state[id] = module_wait_module;
		return;
	}

	if(qstate->blacklist || qstate->no_cache_lookup) {
		 
		 
		qstate->ext_state[id] = module_wait_module;
		return;
	}

	 
	if(cachedb_intcache_lookup(qstate)) {
		if(verbosity >= VERB_ALGO) {
			if(qstate->return_msg->rep)
				log_dns_msg("cachedb internal cache lookup",
					&qstate->return_msg->qinfo,
					qstate->return_msg->rep);
			else log_info("cachedb internal cache lookup: rcode %s",
				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
				:"??");
		}
		 
		qstate->ext_state[id] = module_finished;
		return;
	}

	 
	if(cachedb_extcache_lookup(qstate, ie)) {
		if(verbosity >= VERB_ALGO)
			log_dns_msg(ie->backend->name,
				&qstate->return_msg->qinfo,
				qstate->return_msg->rep);
		 
		cachedb_intcache_store(qstate);
		 
		if(qstate->need_refetch && qstate->serve_expired_data &&
			qstate->serve_expired_data->timer) {
				qstate->return_msg = NULL;
				qstate->ext_state[id] = module_wait_module;
				return;
		}
		 
		qstate->ext_state[id] = module_finished;
		return;
	}

	 
	 
	qstate->ext_state[id] = module_wait_module;
}