Commit a07cc5e1 authored by Pekka Pessi's avatar Pekka Pessi

sres.c, sres_cache.c: NetModule hack for re-prioritizing SRV records

Original hack by Stefan Leuenberger <Stefan.Leuenberger@netmodule.com>.

The hack reprioritizes the SRV records used with transaction in case a
server refuses connection or it does not answer.

New functions sres_cache_set_srv_priority() and
sres_set_cached_srv_priority().

darcs-hash:20080111171223-65a35-be722d766d41da438d2bd6fe517fdc9169349755.gz
parent 7020e299
......@@ -218,6 +218,24 @@ sres_record_t **sres_cached_answers_sockaddr(sres_resolver_t *res,
uint16_t type,
struct sockaddr const *addr);
/**Modify the priority of the specified SRV records.
*
* @param res pointer to resolver object
* @param service domain name of the SRV records to search in cache
* @param target target to lower the prio
* @param port port number to lower the prio
* @param newprio new priority value
*
* @return Number of modified records on success, -1 otherwise
*/
SRESPUBFUN
int sres_set_cached_srv_priority(sres_resolver_t *res,
char const *domain,
char const *target,
uint16_t port,
uint16_t newprio);
/** Send a query, wait for answer, return results. */
SRESPUBFUN
int sres_blocking_query(sres_resolver_t *res,
......
......@@ -102,6 +102,13 @@ SRESPUBFUN void sres_cache_free_record(sres_cache_t *cache, void *rr);
/** Store a record to cache */
SRESPUBFUN void sres_cache_store(sres_cache_t *, sres_record_t *, time_t now);
/** Modify the priority in the specified SRV record */
SRESPUBFUN int sres_cache_set_srv_priority(sres_cache_t *,
char const *domain,
char const *target,
uint16_t port,
uint16_t prio);
#ifdef __cplusplus
}
#endif
......
......@@ -1339,6 +1339,28 @@ sres_cached_answers_sockaddr(sres_resolver_t *res,
return result;
}
/* Originally by Netmodule */
int sres_set_cached_srv_priority(sres_resolver_t *res,
char const *domain,
char const *target,
uint16_t port,
uint16_t newvalue)
{
char rooted_domain[SRES_MAXDNAME];
if (res == NULL || res->res_cache == NULL)
return su_seterrno(EFAULT);
domain = sres_toplevel(rooted_domain, sizeof rooted_domain, domain);
if (!domain)
return -1;
return sres_cache_set_srv_priority(res->res_cache,
domain, target, port, newvalue);
}
/** Sort answers. */
int
sres_sort_answers(sres_resolver_t *res, sres_record_t **answers)
......
......@@ -471,6 +471,48 @@ void sres_cache_clean(sres_cache_t *cache, time_t now)
}
}
/* NetModule hack: Modify priority of a SRV record */
int sres_cache_set_srv_priority(sres_cache_t *cache,
char const *domain,
char const *target,
uint16_t port,
uint16_t prio)
{
int ret = 0;
unsigned hash;
sres_rr_hash_entry_t **iter;
if (cache == NULL || domain == NULL || target == NULL)
return -1;
hash = sres_hash_key(domain);
if (!LOCK(cache))
return -1;
for (iter = sres_htable_hash(cache->cache_hash, hash);
iter && *iter;
iter = sres_htable_next(cache->cache_hash, iter)) {
sres_record_t *rr = (*iter)->rr;
if (rr && rr->sr_name &&
sres_type_srv == rr->sr_type &&
(port == 0 || rr->sr_srv->srv_port == port) &&
rr->sr_srv->srv_target &&
strcasecmp(rr->sr_srv->srv_target, target) == 0 &&
strcasecmp(rr->sr_name, domain) == 0) {
/* record found --> change priority of server */
rr->sr_srv->srv_priority = prio;
ret++;
}
}
UNLOCK(cache);
/** @return number of modified entries or -1 upon an error. */
return ret;
}
HTABLE_BODIES_WITH(sres_htable, ht, sres_rr_hash_entry_t, SRES_HENTRY_HASH,
unsigned, size_t);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment