[or-cvs] Use digestmap_t instead of strmap_t where appropriate. Do ...
Nick Mathewson
nickm at seul.org
Tue Oct 18 20:12:24 UTC 2005
Update of /home/or/cvsroot/tor/src/or
In directory moria:/tmp/cvs-serv3067/src/or
Modified Files:
dirserv.c or.h rephist.c
Log Message:
Use digestmap_t instead of strmap_t where appropriate. Do less hex en/decoding
Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/dirserv.c,v
retrieving revision 1.256
retrieving revision 1.257
diff -u -d -r1.256 -r1.257
--- dirserv.c 18 Oct 2005 17:43:54 -0000 1.256
+++ dirserv.c 18 Oct 2005 20:12:22 -0000 1.257
@@ -827,7 +827,7 @@
/* Used for other dirservers' v2 network statuses. Map from hexdigest to
* cached_dir_t. */
-static strmap_t *cached_v2_networkstatus = NULL;
+static digestmap_t *cached_v2_networkstatus = NULL;
/** Possibly replace the contents of <b>d</b> with the value of
* <b>directory</b> published on <b>when</b>, unless <b>when</b> is older than
@@ -891,26 +891,25 @@
}
/** We've just received a v2 network-status for an authoritative directory
- * with fingerprint <b>fp</b> (hex digest, no spaces), published at
+ * with identity digest <b>identity</b> published at
* <b>published</b>. Store it so we can serve it to others. If
* <b>directory</b> is NULL, remove the entry with the given fingerprint from
* the cache.
*/
void
-dirserv_set_cached_networkstatus_v2(const char *directory, const char *fp,
+dirserv_set_cached_networkstatus_v2(const char *directory,
+ const char *identity,
time_t published)
{
cached_dir_t *d;
if (!cached_v2_networkstatus)
- cached_v2_networkstatus = strmap_new();
-
- tor_assert(strlen(fp) == HEX_DIGEST_LEN);
+ cached_v2_networkstatus = digestmap_new();
- if (!(d = strmap_get(cached_v2_networkstatus, fp))) {
+ if (!(d = digestmap_get(cached_v2_networkstatus, identity))) {
if (!directory)
return;
d = tor_malloc_zero(sizeof(cached_dir_t));
- strmap_set(cached_v2_networkstatus, fp, d);
+ digestmap_set(cached_v2_networkstatus, identity, d);
}
tor_assert(d);
@@ -918,7 +917,7 @@
set_cached_dir(d, tor_strdup(directory), published);
} else {
free_cached_dir(d);
- strmap_remove(cached_v2_networkstatus, fp);
+ digestmap_remove(cached_v2_networkstatus, identity);
}
}
@@ -1308,7 +1307,7 @@
tor_assert(result);
if (!cached_v2_networkstatus)
- cached_v2_networkstatus = strmap_new();
+ cached_v2_networkstatus = digestmap_new();
if (!(strcmp(key,"authority"))) {
if (get_options()->AuthoritativeDir) {
@@ -1324,20 +1323,20 @@
log_fn(LOG_WARN,"Unable to generate an authoritative network status.");
}
} else if (!strcmp(key, "all")) {
- strmap_iter_t *iter = strmap_iter_init(cached_v2_networkstatus);
- while (!strmap_iter_done(iter)) {
- const char *fp;
+ digestmap_iter_t *iter = digestmap_iter_init(cached_v2_networkstatus);
+ while (!digestmap_iter_done(iter)) {
+ const char *ident;
void *val;
- strmap_iter_get(iter, &fp, &val);
+ digestmap_iter_get(iter, &ident, &val);
smartlist_add(result, val);
- iter = strmap_iter_next(cached_v2_networkstatus, iter);
+ iter = digestmap_iter_next(cached_v2_networkstatus, iter);
}
if (smartlist_len(result) == 0)
log_fn(LOG_WARN, "Client requested 'all' network status objects; we have none.");
} else if (!strcmpstart(key, "fp/")) {
- smartlist_t *hexdigests = smartlist_create();
- dir_split_resource_into_fingerprints(key+3, hexdigests, NULL, 0);
- SMARTLIST_FOREACH(hexdigests, char *, cp,
+ smartlist_t *digests = smartlist_create();
+ dir_split_resource_into_fingerprints(key+3, digests, NULL, 1);
+ SMARTLIST_FOREACH(digests, char *, cp,
{
cached_dir_t *cached;
tor_strupper(cp);
@@ -1346,7 +1345,7 @@
the_v2_networkstatus_is_dirty &&
the_v2_networkstatus_is_dirty + DIR_REGEN_SLACK_TIME < time(NULL))
generate_v2_networkstatus();
- cached = strmap_get(cached_v2_networkstatus, cp);
+ cached = digestmap_get(cached_v2_networkstatus, cp);
if (cached) {
smartlist_add(result, cached);
} else {
@@ -1354,7 +1353,7 @@
}
tor_free(cp);
});
- smartlist_free(hexdigests);
+ smartlist_free(digests);
}
return 0;
}
@@ -1501,7 +1500,7 @@
clear_cached_dir(&cached_directory);
clear_cached_dir(&cached_runningrouters);
if (cached_v2_networkstatus) {
- strmap_free(cached_v2_networkstatus, free_cached_dir);
+ digestmap_free(cached_v2_networkstatus, free_cached_dir);
cached_v2_networkstatus = NULL;
}
}
Index: or.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/or.h,v
retrieving revision 1.720
retrieving revision 1.721
diff -u -d -r1.720 -r1.721
--- or.h 18 Oct 2005 17:43:54 -0000 1.720
+++ or.h 18 Oct 2005 20:12:22 -0000 1.721
@@ -871,6 +871,7 @@
typedef struct {
/** List of routerinfo_t. */
smartlist_t *routers;
+ digestmap_t *identity_map;
} routerlist_t;
/** Information on router used when extending a circuit. (We don't need a
@@ -1778,7 +1779,8 @@
size_t dirserv_get_runningrouters(const char **rr, int compress);
void dirserv_set_cached_directory(const char *directory, time_t when,
int is_running_routers);
-void dirserv_set_cached_networkstatus_v2(const char *directory, const char *fp,
+void dirserv_set_cached_networkstatus_v2(const char *directory,
+ const char *identity,
time_t published);
int dirserv_get_networkstatus_v2(smartlist_t *result, const char *key);
int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
Index: rephist.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/rephist.c,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -d -r1.67 -r1.68
--- rephist.c 30 Sep 2005 01:39:24 -0000 1.67
+++ rephist.c 18 Oct 2005 20:12:22 -0000 1.68
@@ -52,11 +52,11 @@
time_t down_since;
/** Map from hex OR2 identity digest to a link_history_t for the link
* from this OR to OR2. */
- strmap_t *link_history_map;
+ digestmap_t *link_history_map;
} or_history_t;
/** Map from hex OR identity digest to or_history_t. */
-static strmap_t *history_map = NULL;
+static digestmap_t *history_map = NULL;
/** Return the or_history_t for the named OR, creating it if necessary.
*/
@@ -64,20 +64,18 @@
get_or_history(const char* id)
{
or_history_t *hist;
- char hexid[HEX_DIGEST_LEN+1];
- base16_encode(hexid, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
- if (!strcmp(hexid, "0000000000000000000000000000000000000000"))
+ if (!memcmp(id, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", DIGEST_LEN))
return NULL;
- hist = (or_history_t*) strmap_get(history_map, hexid);
+ hist = digestmap_get(history_map, id);
if (!hist) {
hist = tor_malloc_zero(sizeof(or_history_t));
rephist_total_alloc += sizeof(or_history_t);
rephist_total_num++;
- hist->link_history_map = strmap_new();
+ hist->link_history_map = digestmap_new();
hist->since = hist->changed = time(NULL);
- strmap_set(history_map, hexid, hist);
+ digestmap_set(history_map, id, hist);
}
return hist;
}
@@ -91,19 +89,17 @@
{
or_history_t *orhist;
link_history_t *lhist;
- char to_hexid[HEX_DIGEST_LEN+1];
orhist = get_or_history(from_id);
if (!orhist)
return NULL;
- base16_encode(to_hexid, HEX_DIGEST_LEN+1, to_id, DIGEST_LEN);
- if (!strcmp(to_hexid, "0000000000000000000000000000000000000000"))
+ if (!memcmp(to_id, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", DIGEST_LEN))
return NULL;
- lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_hexid);
+ lhist = (link_history_t*) digestmap_get(orhist->link_history_map, to_id);
if (!lhist) {
lhist = tor_malloc_zero(sizeof(link_history_t));
rephist_total_alloc += sizeof(link_history_t);
lhist->since = lhist->changed = time(NULL);
- strmap_set(orhist->link_history_map, to_hexid, lhist);
+ digestmap_set(orhist->link_history_map, to_id, lhist);
}
return lhist;
}
@@ -121,7 +117,7 @@
free_or_history(void *_hist)
{
or_history_t *hist = _hist;
- strmap_free(hist->link_history_map, _free_link_history);
+ digestmap_free(hist->link_history_map, _free_link_history);
rephist_total_alloc -= sizeof(or_history_t);
rephist_total_num--;
tor_free(hist);
@@ -149,7 +145,7 @@
void
rep_hist_init(void)
{
- history_map = strmap_new();
+ history_map = digestmap_new();
bw_arrays_init();
predicted_ports_init();
}
@@ -277,9 +273,10 @@
void
rep_hist_dump_stats(time_t now, int severity)
{
- strmap_iter_t *lhist_it;
- strmap_iter_t *orhist_it;
- const char *name1, *name2, *hexdigest1, *hexdigest2;
+ digestmap_iter_t *lhist_it;
+ digestmap_iter_t *orhist_it;
+ const char *name1, *name2, *digest1, *digest2;
+ char hexdigest1[HEX_DIGEST_LEN+1];
or_history_t *or_history;
link_history_t *link_history;
void *or_history_p, *link_history_p;
@@ -294,16 +291,16 @@
log(severity, "--------------- Dumping history information:");
- for (orhist_it = strmap_iter_init(history_map); !strmap_iter_done(orhist_it);
- orhist_it = strmap_iter_next(history_map,orhist_it)) {
- strmap_iter_get(orhist_it, &hexdigest1, &or_history_p);
+ for (orhist_it = digestmap_iter_init(history_map); !digestmap_iter_done(orhist_it);
+ orhist_it = digestmap_iter_next(history_map,orhist_it)) {
+ digestmap_iter_get(orhist_it, &digest1, &or_history_p);
or_history = (or_history_t*) or_history_p;
- if ((r = router_get_by_hexdigest(hexdigest1)))
+ if ((r = router_get_by_digest(digest1)))
name1 = r->nickname;
else
name1 = "(unknown)";
-
+ base16_encode(hexdigest1, sizeof(hexdigest1), digest1, DIGEST_LEN);
update_or_history(or_history, now);
upt = or_history->uptime;
downt = or_history->downtime;
@@ -318,14 +315,14 @@
or_history->n_conn_ok, or_history->n_conn_fail+or_history->n_conn_ok,
upt, upt+downt, uptime*100.0);
- if (!strmap_isempty(or_history->link_history_map)) {
+ if (!digestmap_isempty(or_history->link_history_map)) {
strlcpy(buffer, " Extend attempts: ", sizeof(buffer));
len = strlen(buffer);
- for (lhist_it = strmap_iter_init(or_history->link_history_map);
- !strmap_iter_done(lhist_it);
- lhist_it = strmap_iter_next(or_history->link_history_map, lhist_it)) {
- strmap_iter_get(lhist_it, &hexdigest2, &link_history_p);
- if ((r = router_get_by_hexdigest(hexdigest2)))
+ for (lhist_it = digestmap_iter_init(or_history->link_history_map);
+ !digestmap_iter_done(lhist_it);
+ lhist_it = digestmap_iter_next(or_history->link_history_map, lhist_it)) {
+ digestmap_iter_get(lhist_it, &digest2, &link_history_p);
+ if ((r = router_get_by_digest(digest2)))
name2 = r->nickname;
else
name2 = "(unknown)";
@@ -353,31 +350,31 @@
or_history_t *or_history;
link_history_t *link_history;
void *or_history_p, *link_history_p;
- strmap_iter_t *orhist_it, *lhist_it;
- const char *hd1, *hd2;
+ digestmap_iter_t *orhist_it, *lhist_it;
+ const char *d1, *d2;
- orhist_it = strmap_iter_init(history_map);
- while (!strmap_iter_done(orhist_it)) {
- strmap_iter_get(orhist_it, &hd1, &or_history_p);
+ orhist_it = digestmap_iter_init(history_map);
+ while (!digestmap_iter_done(orhist_it)) {
+ digestmap_iter_get(orhist_it, &d1, &or_history_p);
or_history = or_history_p;
if (or_history->changed < before) {
free_or_history(or_history);
- orhist_it = strmap_iter_next_rmv(history_map, orhist_it);
+ orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
continue;
}
- for (lhist_it = strmap_iter_init(or_history->link_history_map);
- !strmap_iter_done(lhist_it); ) {
- strmap_iter_get(lhist_it, &hd2, &link_history_p);
+ for (lhist_it = digestmap_iter_init(or_history->link_history_map);
+ !digestmap_iter_done(lhist_it); ) {
+ digestmap_iter_get(lhist_it, &d2, &link_history_p);
link_history = link_history_p;
if (link_history->changed < before) {
rephist_total_alloc -= sizeof(link_history_t);
tor_free(link_history);
- lhist_it = strmap_iter_next_rmv(or_history->link_history_map,lhist_it);
+ lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,lhist_it);
continue;
}
- lhist_it = strmap_iter_next(or_history->link_history_map,lhist_it);
+ lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
}
- orhist_it = strmap_iter_next(history_map, orhist_it);
+ orhist_it = digestmap_iter_next(history_map, orhist_it);
}
}
@@ -782,7 +779,7 @@
void
rep_hist_free_all(void)
{
- strmap_free(history_map, free_or_history);
+ digestmap_free(history_map, free_or_history);
tor_free(read_array);
tor_free(write_array);
predicted_ports_free();
More information about the tor-commits
mailing list