PERFORCE change 127142 for review
Fredrik Lindberg
fli at FreeBSD.org
Wed Oct 3 14:06:10 PDT 2007
http://perforce.freebsd.org/chv.cgi?CH=127142
Change 127142 by fli at fli_nexus on 2007/10/03 21:05:36
- Follow changes to record subsystem.
- Add cache related structures and prototypes into
its own header file.
- Cope with failures in a more clean way.
- Some minor whitespace fixes and new debug asserts
Affected files ...
.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/cache.c#7 edit
.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/cache.h#1 add
Differences ...
==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/cache.c#7 (text+ko) ====
@@ -28,6 +28,7 @@
#include <stdint.h>
#include <string.h>
+#include "cache.h"
#include "mdnsd.h"
#include "record.h"
#include "log.h"
@@ -80,12 +81,17 @@
TAILQ_REMOVE(&c->c_list, cr, cr_next);
}
-void
+int
cache_init(struct cache *c, struct queries *q)
{
- records_init(&c->c_recs, mdns_c_in);
+ int error;
+ error = records_init(&c->c_recs);
+ if (error != 0)
+ return (-1);
TAILQ_INIT(&c->c_list);
c->c_queries = q;
+ MDNS_INIT_SET(c, c_magic);
+ return (0);
}
void
@@ -94,7 +100,7 @@
struct cache_res *cr, *cr2;
TAILQ_FOREACH_SAFE(cr, &c->c_list, cr_next, cr2) {
- cache_del(c, &cr->cr_res);
+ cache_del(c, cr);
}
records_destroy(&c->c_recs);
}
@@ -106,7 +112,6 @@
cache_clean(struct cache *c)
{
struct cache_res *cr;
- struct record_res *rr;
cr = TAILQ_FIRST(&c->c_list);
if (cr == NULL)
@@ -117,8 +122,8 @@
/* Remove entries with the same ttl */
do {
- rr = &cr->cr_res;
- cache_del(c, rr);
+ MDNS_INIT_ASSERT(cr, cr_magic);
+ cache_del(c, cr);
cr = TAILQ_FIRST(&c->c_list);
if (cr == NULL)
break;
@@ -134,55 +139,64 @@
* ttl value specified in the given `rrset'.
*/
int
-cache_add(struct cache *c, struct mdns_rrset *rrset, struct record_res **new)
+cache_add(struct cache *c, struct mdns_rrset *rrset, struct cache_res **new)
{
- struct record *r;
struct record_res *rr;
- struct record_type *rt;
struct cache_res *cr;
- int rval;
+ int error, rval;
+ struct recpar rp;
+
+ MDNS_INIT_ASSERT(c, c_magic);
- rr = NULL;
- rt = record_type_find(&c->c_recs, rrset->r_name, rrset->r_type);
- if (rt != NULL) {
- record_type_foreach(rr, rt) {
- if (rr->rr_len == rrset->r_datalen &&
- memcmp(rr->rr_data, rrset->r_data, rr->rr_len) == 0)
- break;
- }
- }
+ rr = record_res_find(&c->c_recs, 0, rrset->r_name, rrset->r_class,
+ rrset->r_type, rrset->r_data, rrset->r_datalen);
if (rr != NULL) {
- cache_set_ttl(c, rr, rrset->r_ttl);
+ cr = record_res_getparent(rr);
+ cache_set_ttl(c, cr, rrset->r_ttl);
dprintf(DEBUG_CACHE,
- "TTL set to %d on name=%s, type=%d, rdatalen=%d",
- rrset->r_ttl, rrset->r_name, rrset->r_type,
+ "TTL set to %d on name=%s, class=%d, type=%d, rdatalen=%d",
+ rrset->r_ttl, rrset->r_name, rrset->r_class, rrset->r_type,
rrset->r_datalen);
rval = 1;
if (new != NULL)
- *new = rr;
+ *new = cr;
}
else if (rrset->r_ttl > 0) {
- record_get(&c->c_recs, &r, 0, rrset->r_name);
cr = malloc(sizeof(struct cache_res));
+ if (cr == NULL)
+ goto cache_add_fail;
rr = &cr->cr_res;
- record_res_add(r, &rr, RECORD_NOALLOC, rrset->r_type,
- rrset->r_data, rrset->r_datalen);
- record_res_setparent(&cr->cr_res, cr);
+ rp.rp_handle = cr;
+ rp.rp_del_cb = NULL;
+ error = record_res_add(&c->c_recs, &rr,
+ REC_NOALLOC | REC_RESOWN, &rp, rrset->r_name,
+ rrset->r_class, rrset->r_type, rrset->r_data,
+ rrset->r_datalen);
+ if (error != 0) {
+ free(cr);
+ goto cache_add_fail;
+ }
+
cr->cr_ttl_abs = rrset->r_ttl;
cr->cr_ttl_rel = rrset->r_ttl;
cr->cr_ctime = time(NULL);
MDNS_INIT_SET(cr, cr_magic);
enqueue_ttl(c, cr);
- dprintf(DEBUG_CACHE,
- "Record added to cache name=%s, type=%d, ttl=%d, dlen=%d",
- rrset->r_name, rrset->r_type, rrset->r_ttl,
+ dprintf(DEBUG_CACHE, "Record added to cache name=%s, "
+ "class=%d, type=%d, ttl=%d, dlen=%d", rrset->r_name,
+ rrset->r_class, rrset->r_type, rrset->r_ttl,
rrset->r_datalen);
rval = 0;
if (new != NULL)
- *new = &cr->cr_res;
+ *new = cr;
}
return (rval);
+cache_add_fail:
+ dprintf(DEBUG_CACHE, "Failed to add record to cache rrset=%p, name=%s, "
+ "class=%d, type=%d, ttl=%d, dlen=%d", rrset, rrset->r_name,
+ rrset->r_class, rrset->r_type, rrset->r_ttl, rrset->r_datalen);
+ return (-1);
}
/*
@@ -191,37 +205,41 @@
* rr - Resource record
*/
int
-cache_del(struct cache *c, struct record_res *rr)
+cache_del(struct cache *c, struct cache_res *cr)
{
- struct cache_res *cr;
- struct record *r = rr->rr_type->rt_record;
- struct record_type *rt = rr->rr_type;
+ struct record *r;
+ struct record_class *rc;
+ struct record_type *rt;
+ struct record_res *rr;
#ifdef DEBUG
time_t rtime = time(NULL);
#endif
struct mdns_rrset rs;
- MDNS_INIT_ASSERT(rr, rr_magic);
- cr = record_res_getparent(rr);
+ MDNS_INIT_ASSERT(c, c_magic);
+ MDNS_INIT_ASSERT(cr, cr_magic);
+
+ rr = &cr->cr_res;
+ rt = record_get_type(rr);
+ rc = record_get_class(rt);
+ r = record_get_record(rc);
- dprintf(DEBUG_CACHE, "Removed %s from cache, type=%d, dlen=%d,"
- "attl=%d, rttl=%d, cached=%d", r->r_name, rt->rt_type, rr->rr_len,
+ dprintf(DEBUG_CACHE, "Removed %s from cache, class=%d, type=%d, dlen=%d"
+ ", attl=%d, rttl=%d, cached=%d", r->r_name, rt->rt_type, rr->rr_len,
cr->cr_ttl_abs, cr->cr_ttl_rel, rtime - cr->cr_ctime);
if (c->c_queries != NULL) {
mdns_rrset_name(&rs, r->r_name);
rs.r_ttl = 0;
- rs.r_class = mdns_c_in;
+ rs.r_class = rc->rc_class;
rs.r_type = rt->rt_type;
rs.r_data = rr->rr_data;
rs.r_datalen = rr->rr_len;
query_notify(c->c_queries, &rs, 0, 0, 0);
}
- MDNS_INIT_ASSERT(cr, cr_magic);
dequeue_ttl(c, cr);
record_res_del(rr);
- record_release(r);
MDNS_INIT_UNSET(cr, cr_magic);
free(cr);
return (0);
@@ -229,21 +247,24 @@
/*
* Check if a record is in cache
- * c - Cache handle
- * name - Resource name
- * type - Resource type
+ * c - Cache handle
+ * name - Resource name
+ * class - Resource class
+ * type - Resource type
*/
struct cache_res *
-cache_find(struct cache *c, char *name, uint16_t type)
+cache_find(struct cache *c, char *name, uint16_t class, uint16_t type)
{
struct record_res *rr;
struct record_type *rt;
- rt = record_type_find(&c->c_recs, name, type);
+ rt = record_type_find(&c->c_recs, REC_CTANY, name, class, type);
if (rt == NULL)
return (NULL);
- rr = record_type_first(rt);
+ rr = record_res_first(rt);
+ if (rr == NULL)
+ return (NULL);
return (record_res_getparent(rr));
}
@@ -252,16 +273,39 @@
* are available per (name,type) pair.
*/
struct cache_res *
-cache_find_next(struct cache_res *cr)
+cache_find_next(struct cache *c, struct cache_res *cr,
+ uint16_t class, uint16_t type)
{
struct record_res *rr;
+ struct record_type *rt;
+ struct record_class *rc;
rr = &cr->cr_res;
- rr = record_type_next(rr);
+ rt = record_get_type(rr);
+ rc = record_get_class(rt);
+ rr = record_res_next(rr);
if (rr != NULL)
return (record_res_getparent(rr));
- else
+
+ rt = record_type_find_next(rt, type);
+ if (rt != NULL) {
+ rr = record_res_first(rt);
+ if (rr == NULL)
+ return (NULL);
+ return (record_res_getparent(rr));
+ }
+
+ rc = record_class_find_next(rc, class);
+ if (rc == NULL)
+ return (NULL);
+ rt = record_type_find(&c->c_recs, REC_CTANY | REC_OBJCLASS, NULL,
+ (uintptr_t)rc, type);
+ if (rt == NULL)
+ return (NULL);
+ rr = record_res_first(rt);
+ if (rr == NULL)
return (NULL);
+ return (record_res_getparent(rr));
}
/*
@@ -272,14 +316,15 @@
* type - Resource type
*/
void
-cache_purge(struct cache *c, time_t offset, char *name, uint16_t type)
+cache_purge(struct cache *c, time_t offset, char *name, uint16_t class,
+ uint16_t type)
{
struct cache_res *cr;
struct record_res *rr, *rr2;
struct record_type *rt;
time_t cur;
- rt = record_type_find(&c->c_recs, name, type);
+ rt = record_type_find(&c->c_recs, 0, name, class, type);
if (rt == NULL)
return;
@@ -288,7 +333,7 @@
TAILQ_FOREACH_SAFE(rr, &rt->rt_list, rr_next, rr2) {
cr = record_res_getparent(rr);
if ((cur - cr->cr_ctime) > offset)
- cache_set_ttl(c, rr, 1);
+ cache_set_ttl(c, cr, 1);
}
}
@@ -299,13 +344,14 @@
* ttl - New TTL
*/
void
-cache_set_ttl(struct cache *c, struct record_res *rr, uint32_t ttl)
+cache_set_ttl(struct cache *c, struct cache_res *cr, uint32_t ttl)
{
- struct cache_res *cr;
+
+ MDNS_INIT_ASSERT(c, c_magic);
+ MDNS_INIT_ASSERT(cr, cr_magic);
if (ttl == 0)
ttl++;
- cr = record_res_getparent(rr);
dequeue_ttl(c, cr);
cr->cr_ttl_rel = ttl;
cr->cr_ctime = time(NULL);
@@ -322,7 +368,7 @@
struct cache_res *cr, *cr2;
TAILQ_FOREACH_SAFE(cr, &c->c_list, cr_next, cr2) {
- cache_del(c, &cr->cr_res);
+ cache_del(c, cr);
}
dprintf(DEBUG_CACHE, "Flushed all cache entries c=%x", c);
}
More information about the p4-projects
mailing list