dirvote.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char dirvote_c_id[] =
  6. "$Id$";
  7. #define DIRVOTE_PRIVATE
  8. #include "or.h"
  9. /**
  10. * \file dirvote.c
  11. * \brief Functions to compute directory consensus, and schedule voting.
  12. **/
  13. static int dirvote_add_signatures_to_pending_consensus(
  14. const char *detached_signatures_body,
  15. const char **msg_out);
  16. static char *list_v3_auth_ids(void);
  17. static void dirvote_fetch_missing_votes(void);
  18. static void dirvote_fetch_missing_signatures(void);
  19. static void dirvote_perform_vote(void);
  20. static void dirvote_clear_votes(int all_votes);
  21. static int dirvote_compute_consensus(void);
  22. static int dirvote_publish_consensus(void);
  23. /* =====
  24. * Voting and consensus generation
  25. * ===== */
  26. /** Clear all storage held in <b>ns</b>. */
  27. void
  28. networkstatus_vote_free(networkstatus_vote_t *ns)
  29. {
  30. if (!ns)
  31. return;
  32. tor_free(ns->client_versions);
  33. tor_free(ns->server_versions);
  34. if (ns->known_flags) {
  35. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  36. smartlist_free(ns->known_flags);
  37. }
  38. if (ns->voters) {
  39. SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
  40. {
  41. tor_free(voter->nickname);
  42. tor_free(voter->address);
  43. tor_free(voter->contact);
  44. });
  45. smartlist_free(ns->voters);
  46. }
  47. if (ns->cert)
  48. authority_cert_free(ns->cert);
  49. if (ns->routerstatus_list) {
  50. if (ns->is_vote) {
  51. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  52. {
  53. tor_free(rs->version);
  54. tor_free(rs);
  55. });
  56. } else {
  57. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  58. tor_free(rs));
  59. }
  60. smartlist_free(ns->routerstatus_list);
  61. }
  62. memset(ns, 11, sizeof(*ns));
  63. tor_free(ns);
  64. }
  65. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  66. * is <b>identity</b>, or NULL if no such voter is associated with
  67. * <b>vote</b>. */
  68. networkstatus_voter_info_t *
  69. networkstatus_get_voter_by_id(networkstatus_vote_t *vote,
  70. const char *identity)
  71. {
  72. if (!vote || !vote->voters)
  73. return NULL;
  74. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  75. if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
  76. return voter);
  77. return NULL;
  78. }
  79. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  80. * networkstatus_voter_info_t.*/
  81. static networkstatus_voter_info_t *
  82. get_voter(const networkstatus_vote_t *vote)
  83. {
  84. tor_assert(vote);
  85. tor_assert(vote->is_vote);
  86. tor_assert(vote->voters);
  87. tor_assert(smartlist_len(vote->voters) == 1);
  88. return smartlist_get(vote->voters, 0);
  89. }
  90. /** Helper for sorting networkstatus_vote_t votes (not consensuses) by the
  91. * hash of their voters' identity digests. */
  92. static int
  93. _compare_votes_by_authority_id(const void **_a, const void **_b)
  94. {
  95. const networkstatus_vote_t *a = *_a, *b = *_b;
  96. return memcmp(get_voter(a)->identity_digest,
  97. get_voter(b)->identity_digest, DIGEST_LEN);
  98. }
  99. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  100. * that occurs more than <b>min</b> times. */
  101. static void
  102. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  103. {
  104. char *cur = NULL;
  105. int count = 0;
  106. SMARTLIST_FOREACH(in, char *, cp,
  107. {
  108. if (cur && !strcmp(cp, cur)) {
  109. ++count;
  110. } else {
  111. if (count > min)
  112. smartlist_add(out, cur);
  113. cur = cp;
  114. count = 1;
  115. }
  116. });
  117. if (count > min)
  118. smartlist_add(out, cur);
  119. }
  120. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  121. * most. Break ties in favor of later-occurring members. */
  122. static const char *
  123. get_most_frequent_member(smartlist_t *lst)
  124. {
  125. const char *most_frequent = NULL;
  126. int most_frequent_count = 0;
  127. const char *cur = NULL;
  128. int count = 0;
  129. SMARTLIST_FOREACH(lst, const char *, s,
  130. {
  131. if (cur && !strcmp(s, cur)) {
  132. ++count;
  133. } else {
  134. if (count >= most_frequent_count) {
  135. most_frequent = cur;
  136. most_frequent_count = count;
  137. }
  138. cur = s;
  139. count = 1;
  140. }
  141. });
  142. if (count >= most_frequent_count) {
  143. most_frequent = cur;
  144. most_frequent_count = count;
  145. }
  146. return most_frequent;
  147. }
  148. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  149. * that come from the same routerinfo, with the same derived elements.
  150. */
  151. static int
  152. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  153. {
  154. int r;
  155. if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
  156. DIGEST_LEN)))
  157. return r;
  158. if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
  159. DIGEST_LEN)))
  160. return r;
  161. if ((r = (b->status.published_on - a->status.published_on)))
  162. return r;
  163. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  164. return r;
  165. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  166. return r;
  167. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  168. return r;
  169. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  170. return r;
  171. return 0;
  172. }
  173. /** Helper for sorting routerlists based on compare_vote_rs. */
  174. static int
  175. _compare_vote_rs(const void **_a, const void **_b)
  176. {
  177. const vote_routerstatus_t *a = *_a, *b = *_b;
  178. return compare_vote_rs(a,b);
  179. }
  180. /** Given a list of vote_routerstatus_t, all for the same router identity,
  181. * return whichever is most frequent, breaking ties in favor of more
  182. * recently published vote_routerstatus_t.
  183. */
  184. static vote_routerstatus_t *
  185. compute_routerstatus_consensus(smartlist_t *votes)
  186. {
  187. vote_routerstatus_t *most = NULL, *cur = NULL;
  188. int most_n = 0, cur_n = 0;
  189. time_t most_published = 0;
  190. smartlist_sort(votes, _compare_vote_rs);
  191. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  192. {
  193. if (cur && !compare_vote_rs(cur, rs)) {
  194. ++cur_n;
  195. } else {
  196. if (cur_n > most_n ||
  197. (cur && cur_n == most_n &&
  198. cur->status.published_on > most_published)) {
  199. most = cur;
  200. most_n = cur_n;
  201. most_published = cur->status.published_on;
  202. }
  203. cur_n = 1;
  204. cur = rs;
  205. }
  206. });
  207. if (cur_n > most_n ||
  208. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  209. most = cur;
  210. most_n = cur_n;
  211. most_published = cur->status.published_on;
  212. }
  213. tor_assert(most);
  214. return most;
  215. }
  216. /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
  217. * <b>digest_out</b> to the hash of the concatenation of those strings. */
  218. static void
  219. hash_list_members(char *digest_out, smartlist_t *lst)
  220. {
  221. crypto_digest_env_t *d = crypto_new_digest_env();
  222. SMARTLIST_FOREACH(lst, const char *, cp,
  223. crypto_digest_add_bytes(d, cp, strlen(cp)));
  224. crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
  225. crypto_free_digest_env(d);
  226. }
  227. /** Given a list of vote networkstatus_vote_t in <b>votes</b>, our public
  228. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  229. * and the number of <b>total_authorities</b> that we believe exist in our
  230. * voting quorum, generate the text of a new v3 consensus vote, and return the
  231. * value in a newly allocated string.
  232. *
  233. * Note: this function DOES NOT check whether the votes are from
  234. * recognized authorities. (dirvote_add_vote does that.) */
  235. char *
  236. networkstatus_compute_consensus(smartlist_t *votes,
  237. int total_authorities,
  238. crypto_pk_env_t *identity_key,
  239. crypto_pk_env_t *signing_key)
  240. {
  241. smartlist_t *chunks;
  242. char *result = NULL;
  243. time_t valid_after, fresh_until, valid_until;
  244. int vote_seconds, dist_seconds;
  245. char *client_versions = NULL, *server_versions = NULL;
  246. smartlist_t *flags;
  247. tor_assert(total_authorities >= smartlist_len(votes));
  248. if (!smartlist_len(votes)) {
  249. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  250. return NULL;
  251. }
  252. flags = smartlist_create();
  253. /* Compute medians of time-related things, and figure out how many
  254. * routers we might need to talk about. */
  255. {
  256. int n_votes = smartlist_len(votes);
  257. time_t *va_times = tor_malloc(n_votes * sizeof(time_t));
  258. time_t *fu_times = tor_malloc(n_votes * sizeof(time_t));
  259. time_t *vu_times = tor_malloc(n_votes * sizeof(time_t));
  260. int *votesec_list = tor_malloc(n_votes * sizeof(int));
  261. int *distsec_list = tor_malloc(n_votes * sizeof(int));
  262. int n_versioning_clients = 0, n_versioning_servers = 0;
  263. smartlist_t *combined_client_versions = smartlist_create();
  264. smartlist_t *combined_server_versions = smartlist_create();
  265. int j;
  266. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  267. {
  268. tor_assert(v->is_vote);
  269. va_times[v_sl_idx] = v->valid_after;
  270. fu_times[v_sl_idx] = v->fresh_until;
  271. vu_times[v_sl_idx] = v->valid_until;
  272. votesec_list[v_sl_idx] = v->vote_seconds;
  273. distsec_list[v_sl_idx] = v->dist_seconds;
  274. if (v->client_versions) {
  275. smartlist_t *cv = smartlist_create();
  276. ++n_versioning_clients;
  277. smartlist_split_string(cv, v->client_versions, ",",
  278. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  279. sort_version_list(cv, 1);
  280. smartlist_add_all(combined_client_versions, cv);
  281. smartlist_free(cv); /* elements get freed later. */
  282. }
  283. if (v->server_versions) {
  284. smartlist_t *sv = smartlist_create();
  285. ++n_versioning_servers;
  286. smartlist_split_string(sv, v->server_versions, ",",
  287. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  288. sort_version_list(sv, 1);
  289. smartlist_add_all(combined_server_versions, sv);
  290. smartlist_free(sv); /* elements get freed later. */
  291. }
  292. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  293. smartlist_add(flags, tor_strdup(cp)));
  294. });
  295. valid_after = median_time(va_times, n_votes);
  296. fresh_until = median_time(fu_times, n_votes);
  297. valid_until = median_time(vu_times, n_votes);
  298. vote_seconds = median_int(votesec_list, n_votes);
  299. dist_seconds = median_int(distsec_list, n_votes);
  300. /*
  301. SMARTLIST_FOREACH(va_times, int*, i,
  302. printf("VA: %d\n", *i));
  303. SMARTLIST_FOREACH(fu_times, int*, i,
  304. printf("FU: %d\n", *i));
  305. printf("%d..%d\n", (int)valid_after, (int)valid_until);
  306. */
  307. tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
  308. tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
  309. tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
  310. tor_assert(dist_seconds >= MIN_DIST_SECONDS);
  311. for (j = 0; j < 2; ++j) {
  312. smartlist_t *lst =
  313. j ? combined_server_versions : combined_client_versions;
  314. int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
  315. smartlist_t *good = smartlist_create();
  316. char *res;
  317. sort_version_list(lst, 0);
  318. get_frequent_members(good, lst, min);
  319. res = smartlist_join_strings(good, ",", 0, NULL);
  320. if (j)
  321. server_versions = res;
  322. else
  323. client_versions = res;
  324. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  325. smartlist_free(good);
  326. smartlist_free(lst);
  327. }
  328. smartlist_sort_strings(flags);
  329. smartlist_uniq_strings(flags);
  330. tor_free(va_times);
  331. tor_free(fu_times);
  332. tor_free(vu_times);
  333. tor_free(votesec_list);
  334. tor_free(distsec_list);
  335. }
  336. chunks = smartlist_create();
  337. {
  338. char buf[1024];
  339. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  340. vu_buf[ISO_TIME_LEN+1];
  341. char *flaglist;
  342. format_iso_time(va_buf, valid_after);
  343. format_iso_time(fu_buf, fresh_until);
  344. format_iso_time(vu_buf, valid_until);
  345. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  346. tor_snprintf(buf, sizeof(buf),
  347. "network-status-version 3\n"
  348. "vote-status consensus\n"
  349. "valid-after %s\n"
  350. "fresh-until %s\n"
  351. "valid-until %s\n"
  352. "voting-delay %d %d\n"
  353. "client-versions %s\n"
  354. "server-versions %s\n"
  355. "known-flags %s\n",
  356. va_buf, fu_buf, vu_buf,
  357. vote_seconds, dist_seconds,
  358. client_versions, server_versions, flaglist);
  359. smartlist_add(chunks, tor_strdup(buf));
  360. tor_free(flaglist);
  361. }
  362. /* Sort the votes. */
  363. smartlist_sort(votes, _compare_votes_by_authority_id);
  364. /* Add the authority sections. */
  365. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  366. {
  367. char buf[1024];
  368. struct in_addr in;
  369. char ip[INET_NTOA_BUF_LEN];
  370. char fingerprint[HEX_DIGEST_LEN+1];
  371. char votedigest[HEX_DIGEST_LEN+1];
  372. networkstatus_voter_info_t *voter = get_voter(v);
  373. in.s_addr = htonl(voter->addr);
  374. tor_inet_ntoa(&in, ip, sizeof(ip));
  375. base16_encode(fingerprint, sizeof(fingerprint), voter->identity_digest,
  376. DIGEST_LEN);
  377. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  378. DIGEST_LEN);
  379. tor_snprintf(buf, sizeof(buf),
  380. "dir-source %s %s %s %s %d %d\n"
  381. "contact %s\n"
  382. "vote-digest %s\n",
  383. voter->nickname, fingerprint, voter->address, ip,
  384. voter->dir_port,
  385. voter->or_port,
  386. voter->contact,
  387. votedigest);
  388. smartlist_add(chunks, tor_strdup(buf));
  389. });
  390. /* Add the actual router entries. */
  391. {
  392. int *index; /* index[j] is the current index into votes[j]. */
  393. int *size; /* size[j] is the number of routerstatuses in votes[j]. */
  394. int *flag_counts; /* The number of voters that list flag[j] for the
  395. * currently considered router. */
  396. int i;
  397. smartlist_t *matching_descs = smartlist_create();
  398. smartlist_t *chosen_flags = smartlist_create();
  399. smartlist_t *versions = smartlist_create();
  400. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  401. * votes[j] knows about. */
  402. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  403. * about flags[f]. */
  404. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  405. * is the same flag as votes[j]->known_flags[b]. */
  406. int *named_flag; /* Index of the flag "Named" for votes[j] */
  407. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  408. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  409. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  410. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  411. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  412. named_flag = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  413. for (i = 0; i < smartlist_len(votes); ++i)
  414. named_flag[i] = -1;
  415. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  416. {
  417. flag_map[v_sl_idx] = tor_malloc_zero(
  418. sizeof(int)*smartlist_len(v->known_flags));
  419. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  420. {
  421. int p = smartlist_string_pos(flags, fl);
  422. tor_assert(p >= 0);
  423. flag_map[v_sl_idx][fl_sl_idx] = p;
  424. ++n_flag_voters[p];
  425. if (!strcmp(fl, "Named"))
  426. named_flag[v_sl_idx] = fl_sl_idx;
  427. });
  428. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  429. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  430. });
  431. /* Now go through all the votes */
  432. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  433. while (1) {
  434. vote_routerstatus_t *rs;
  435. routerstatus_t rs_out;
  436. const char *lowest_id = NULL;
  437. const char *chosen_version;
  438. const char *chosen_name = NULL;
  439. int naming_conflict = 0;
  440. int n_listing = 0;
  441. int i;
  442. char buf[256];
  443. /* Of the next-to-be-considered digest in each voter, which is first? */
  444. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  445. if (index[v_sl_idx] < size[v_sl_idx]) {
  446. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  447. if (!lowest_id ||
  448. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  449. lowest_id = rs->status.identity_digest;
  450. }
  451. });
  452. if (!lowest_id) /* we're out of routers. */
  453. break;
  454. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  455. smartlist_clear(matching_descs);
  456. smartlist_clear(chosen_flags);
  457. smartlist_clear(versions);
  458. /* Okay, go through all the entries for this digest. */
  459. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  460. if (index[v_sl_idx] >= size[v_sl_idx])
  461. continue; /* out of entries. */
  462. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  463. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  464. continue; /* doesn't include this router. */
  465. /* At this point, we know that we're looking at a routersatus with
  466. * identity "lowest".
  467. */
  468. ++index[v_sl_idx];
  469. ++n_listing;
  470. smartlist_add(matching_descs, rs);
  471. if (rs->version && rs->version[0])
  472. smartlist_add(versions, rs->version);
  473. /* Tally up all the flags. */
  474. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  475. if (rs->flags & (U64_LITERAL(1) << i))
  476. ++flag_counts[flag_map[v_sl_idx][i]];
  477. }
  478. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  479. if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
  480. log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
  481. chosen_name, rs->status.nickname);
  482. naming_conflict = 1;
  483. }
  484. chosen_name = rs->status.nickname;
  485. }
  486. });
  487. /* We don't include this router at all unless more than half of
  488. * the authorities we believe in list it. */
  489. if (n_listing <= total_authorities/2)
  490. continue;
  491. /* Figure out the most popular opinion of what the most recent
  492. * routerinfo and its contents are. */
  493. rs = compute_routerstatus_consensus(matching_descs);
  494. /* Copy bits of that into rs_out. */
  495. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  496. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  497. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  498. DIGEST_LEN);
  499. rs_out.addr = rs->status.addr;
  500. rs_out.published_on = rs->status.published_on;
  501. rs_out.dir_port = rs->status.dir_port;
  502. rs_out.or_port = rs->status.or_port;
  503. if (chosen_name && !naming_conflict) {
  504. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  505. } else {
  506. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  507. }
  508. /* Set the flags. */
  509. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  510. SMARTLIST_FOREACH(flags, const char *, fl,
  511. {
  512. if (strcmp(fl, "Named")) {
  513. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
  514. smartlist_add(chosen_flags, (char*)fl);
  515. } else {
  516. if (!naming_conflict && flag_counts[fl_sl_idx])
  517. smartlist_add(chosen_flags, (char*)"Named");
  518. }
  519. });
  520. /* Pick the version. */
  521. if (smartlist_len(versions)) {
  522. sort_version_list(versions, 0);
  523. chosen_version = get_most_frequent_member(versions);
  524. } else {
  525. chosen_version = NULL;
  526. }
  527. /* Okay!! Now we can write the descriptor... */
  528. /* First line goes into "buf". */
  529. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
  530. smartlist_add(chunks, tor_strdup(buf));
  531. /* Second line is all flags. The "\n" is missing. */
  532. smartlist_add(chunks,
  533. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  534. /* Now the version line. */
  535. if (chosen_version) {
  536. smartlist_add(chunks, tor_strdup("\nv "));
  537. smartlist_add(chunks, tor_strdup(chosen_version));
  538. }
  539. smartlist_add(chunks, tor_strdup("\n"));
  540. /* And the loop is over and we move on to the next router */
  541. }
  542. tor_free(index);
  543. tor_free(size);
  544. tor_free(n_voter_flags);
  545. tor_free(n_flag_voters);
  546. for (i = 0; i < smartlist_len(votes); ++i)
  547. tor_free(flag_map[i]);
  548. tor_free(flag_map);
  549. tor_free(flag_counts);
  550. smartlist_free(matching_descs);
  551. smartlist_free(chosen_flags);
  552. smartlist_free(versions);
  553. }
  554. /* Add a signature. */
  555. {
  556. char digest[DIGEST_LEN];
  557. char fingerprint[HEX_DIGEST_LEN+1];
  558. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  559. char buf[4096];
  560. smartlist_add(chunks, tor_strdup("directory-signature "));
  561. /* Compute the hash of the chunks. */
  562. hash_list_members(digest, chunks);
  563. /* Get the fingerprints */
  564. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  565. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  566. /* add the junk that will go at the end of the line. */
  567. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  568. signing_key_fingerprint);
  569. /* And the signature. */
  570. if (router_append_dirobj_signature(buf, sizeof(buf), digest,
  571. signing_key)) {
  572. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  573. return NULL; /* This leaks, but it should never happen. */
  574. }
  575. smartlist_add(chunks, tor_strdup(buf));
  576. }
  577. result = smartlist_join_strings(chunks, "", 0, NULL);
  578. tor_free(client_versions);
  579. tor_free(server_versions);
  580. smartlist_free(flags);
  581. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  582. smartlist_free(chunks);
  583. {
  584. networkstatus_vote_t *c;
  585. if (!(c = networkstatus_parse_vote_from_string(result, NULL, 0))) {
  586. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  587. "parse.");
  588. tor_free(result);
  589. return NULL;
  590. }
  591. networkstatus_vote_free(c);
  592. }
  593. return result;
  594. }
  595. /** Check whether the signature on <b>voter</b> is correctly signed by
  596. * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  597. * signing key; otherwise set the good_signature or bad_signature flag on
  598. * <b>voter</b>, and return 0. */
  599. /* (private; exposed for testing.) */
  600. int
  601. networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  602. networkstatus_voter_info_t *voter,
  603. authority_cert_t *cert)
  604. {
  605. char d[DIGEST_LEN];
  606. char *signed_digest;
  607. size_t signed_digest_len;
  608. if (crypto_pk_get_digest(cert->signing_key, d)<0)
  609. return -1;
  610. if (memcmp(voter->signing_key_digest, d, DIGEST_LEN))
  611. return -1;
  612. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  613. signed_digest = tor_malloc(signed_digest_len);
  614. if (crypto_pk_public_checksig(cert->signing_key,
  615. signed_digest,
  616. voter->signature,
  617. voter->signature_len) != DIGEST_LEN ||
  618. memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
  619. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  620. voter->bad_signature = 1;
  621. } else {
  622. voter->good_signature = 1;
  623. }
  624. return 0;
  625. }
  626. /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
  627. * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
  628. * signature from every recognized authority on it, 0 if there are
  629. * enough good signatures from recognized authorities on it, -1 if we might
  630. * get enough good signatures by fetching missing certificates, and -2
  631. * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
  632. * about every problem; if warn is at least 1, warn only if we can't get
  633. * enough signatures; if warn is negative, log nothing at all. */
  634. int
  635. networkstatus_check_consensus_signature(networkstatus_vote_t *consensus,
  636. int warn)
  637. {
  638. int n_good = 0;
  639. int n_missing_key = 0;
  640. int n_bad = 0;
  641. int n_unknown = 0;
  642. int n_no_signature = 0;
  643. int n_v3_authorities = get_n_authorities(V3_AUTHORITY);
  644. int n_required = n_v3_authorities/2 + 1;
  645. smartlist_t *need_certs_from = smartlist_create();
  646. smartlist_t *unrecognized = smartlist_create();
  647. smartlist_t *missing_authorities = smartlist_create();
  648. int severity;
  649. tor_assert(! consensus->is_vote);
  650. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
  651. {
  652. if (!voter->good_signature && !voter->bad_signature && voter->signature) {
  653. /* we can try to check the signature. */
  654. authority_cert_t *cert =
  655. authority_cert_get_by_digests(voter->identity_digest,
  656. voter->signing_key_digest);
  657. if (! cert) {
  658. if (!trusteddirserver_get_by_v3_auth_digest(voter->identity_digest)) {
  659. smartlist_add(unrecognized, voter);
  660. ++n_unknown;
  661. } else {
  662. smartlist_add(need_certs_from, voter);
  663. ++n_missing_key;
  664. }
  665. continue;
  666. }
  667. if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
  668. smartlist_add(need_certs_from, voter);
  669. ++n_missing_key;
  670. continue;
  671. }
  672. }
  673. if (voter->good_signature)
  674. ++n_good;
  675. else if (voter->bad_signature)
  676. ++n_bad;
  677. else
  678. ++n_no_signature;
  679. });
  680. /* Now see whether we're missing any voters entirely. */
  681. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  682. trusted_dir_server_t *, ds,
  683. {
  684. if ((ds->type & V3_AUTHORITY) &&
  685. !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
  686. smartlist_add(missing_authorities, ds);
  687. });
  688. if (warn > 1 || (warn >= 0 && n_good < n_required))
  689. severity = LOG_WARN;
  690. else
  691. severity = LOG_INFO;
  692. if (warn >= 0) {
  693. SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
  694. {
  695. log(severity, LD_DIR, "Consensus includes unrecognized authority '%s' "
  696. "at %s:%d (contact %s; identity %s)",
  697. voter->nickname, voter->address, (int)voter->dir_port,
  698. voter->contact?voter->contact:"n/a",
  699. hex_str(voter->identity_digest, DIGEST_LEN));
  700. });
  701. SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
  702. {
  703. log_info(LD_DIR, "Looks like we need to download a new certificate "
  704. "from authority '%s' at %s:%d (contact %s; identity %s)",
  705. voter->nickname, voter->address, (int)voter->dir_port,
  706. voter->contact?voter->contact:"n/a",
  707. hex_str(voter->identity_digest, DIGEST_LEN));
  708. });
  709. SMARTLIST_FOREACH(missing_authorities, trusted_dir_server_t *, ds,
  710. {
  711. log(severity, LD_DIR, "Consensus does not include configured "
  712. "authority '%s' at %s:%d (identity %s)",
  713. ds->nickname, ds->address, (int)ds->dir_port,
  714. hex_str(ds->v3_identity_digest, DIGEST_LEN));
  715. });
  716. log(severity, LD_DIR,
  717. "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
  718. "%d required", n_unknown, n_missing_key, n_good, n_bad,
  719. n_no_signature, n_required);
  720. }
  721. smartlist_free(unrecognized);
  722. smartlist_free(need_certs_from);
  723. smartlist_free(missing_authorities);
  724. if (n_good == n_v3_authorities)
  725. return 1;
  726. else if (n_good >= n_required)
  727. return 0;
  728. else if (n_good + n_missing_key >= n_required)
  729. return -1;
  730. else
  731. return -2;
  732. }
  733. /** Given a consensus vote <b>target</b> and a set of detached signatures in
  734. * <b>sigs</b> that correspond to the same consensus, check whether there are
  735. * any new signatures in <b>src_voter_list</b> that should be added to
  736. * <b>target. (A signature should be added if we have no signature for that
  737. * voter in <b>target</b> yet, or if we have no verifiable signature and the
  738. * new signature is verifiable.) Return the number of signatures added or
  739. * changed, or -1 if the document signed by <b>sigs</b> isn't the same
  740. * document as <b>target</b>. */
  741. int
  742. networkstatus_add_detached_signatures(networkstatus_vote_t *target,
  743. ns_detached_signatures_t *sigs)
  744. {
  745. int r = 0;
  746. tor_assert(sigs);
  747. tor_assert(target);
  748. tor_assert(!target->is_vote);
  749. /* Are they the same consensus? */
  750. if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
  751. DIGEST_LEN))
  752. return -1;
  753. /* For each voter in src... */
  754. SMARTLIST_FOREACH(sigs->signatures, networkstatus_voter_info_t *, src_voter,
  755. {
  756. networkstatus_voter_info_t *target_voter =
  757. networkstatus_get_voter_by_id(target, src_voter->identity_digest);
  758. authority_cert_t *cert;
  759. /* If the target doesn't know about this voter, then forget it. */
  760. if (!target_voter)
  761. continue;
  762. /* If the target already has a good signature from this voter, then skip
  763. * this one. */
  764. if (target_voter->good_signature)
  765. continue;
  766. /* Try checking the signature if we haven't already. */
  767. if (!src_voter->good_signature && !src_voter->bad_signature) {
  768. cert = authority_cert_get_by_digests(src_voter->identity_digest,
  769. src_voter->signing_key_digest);
  770. if (cert) {
  771. networkstatus_check_voter_signature(target, src_voter, cert);
  772. }
  773. }
  774. /* If this signature is good, or we don't have any signature yet,
  775. * then add it. */
  776. if (src_voter->good_signature || !target_voter->signature) {
  777. ++r;
  778. tor_free(target_voter->signature);
  779. target_voter->signature =
  780. tor_memdup(src_voter->signature, src_voter->signature_len);
  781. memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
  782. DIGEST_LEN);
  783. target_voter->signature_len = src_voter->signature_len;
  784. target_voter->good_signature = 1;
  785. target_voter->bad_signature = 0;
  786. }
  787. });
  788. return r;
  789. }
  790. /** Return a newly allocated string holding the detached-signatures document
  791. * corresponding to the signatures on <b>consensus</b>. */
  792. char *
  793. networkstatus_get_detached_signatures(networkstatus_vote_t *consensus)
  794. {
  795. smartlist_t *elements;
  796. char buf[4096];
  797. char *result = NULL;
  798. int n_sigs = 0;
  799. tor_assert(consensus);
  800. tor_assert(! consensus->is_vote);
  801. elements = smartlist_create();
  802. {
  803. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  804. vu_buf[ISO_TIME_LEN+1];
  805. char d[HEX_DIGEST_LEN+1];
  806. base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
  807. format_iso_time(va_buf, consensus->valid_after);
  808. format_iso_time(fu_buf, consensus->fresh_until);
  809. format_iso_time(vu_buf, consensus->valid_until);
  810. tor_snprintf(buf, sizeof(buf),
  811. "consensus-digest %s\n"
  812. "valid-after %s\n"
  813. "fresh-until %s\n"
  814. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  815. smartlist_add(elements, tor_strdup(buf));
  816. }
  817. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
  818. {
  819. char sk[HEX_DIGEST_LEN+1];
  820. char id[HEX_DIGEST_LEN+1];
  821. if (!v->signature || v->bad_signature)
  822. continue;
  823. ++n_sigs;
  824. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  825. base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
  826. tor_snprintf(buf, sizeof(buf),
  827. "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
  828. id, sk);
  829. smartlist_add(elements, tor_strdup(buf));
  830. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  831. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  832. smartlist_add(elements, tor_strdup(buf));
  833. });
  834. result = smartlist_join_strings(elements, "", 0, NULL);
  835. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  836. smartlist_free(elements);
  837. if (!n_sigs)
  838. tor_free(result);
  839. return result;
  840. }
  841. /** Release all storage held in <b>s</b>. */
  842. void
  843. ns_detached_signatures_free(ns_detached_signatures_t *s)
  844. {
  845. if (s->signatures) {
  846. SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
  847. {
  848. tor_free(v->signature);
  849. tor_free(v);
  850. });
  851. smartlist_free(s->signatures);
  852. }
  853. tor_free(s);
  854. }
  855. /* =====
  856. * Certificate functions
  857. * ===== */
  858. /** Free storage held in <b>cert</b>. */
  859. void
  860. authority_cert_free(authority_cert_t *cert)
  861. {
  862. if (!cert)
  863. return;
  864. tor_free(cert->cache_info.signed_descriptor_body);
  865. if (cert->signing_key)
  866. crypto_free_pk_env(cert->signing_key);
  867. if (cert->identity_key)
  868. crypto_free_pk_env(cert->identity_key);
  869. tor_free(cert);
  870. }
  871. /** Allocate and return a new authority_cert_t with the same contents as
  872. * <b>cert</b>. */
  873. authority_cert_t *
  874. authority_cert_dup(authority_cert_t *cert)
  875. {
  876. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  877. tor_assert(cert);
  878. memcpy(out, cert, sizeof(authority_cert_t));
  879. /* Now copy pointed-to things. */
  880. out->cache_info.signed_descriptor_body =
  881. tor_strndup(cert->cache_info.signed_descriptor_body,
  882. cert->cache_info.signed_descriptor_len);
  883. out->cache_info.saved_location = SAVED_NOWHERE;
  884. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  885. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  886. return out;
  887. }
  888. /* =====
  889. * Vote scheduling
  890. * ===== */
  891. /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
  892. * Note that these aren't the intervals we'll use to vote; they're the ones
  893. * that we'll vote to use. */
  894. void
  895. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  896. {
  897. or_options_t *options = get_options();
  898. tor_assert(timing_out);
  899. timing_out->vote_interval = options->V3AuthVotingInterval;
  900. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  901. timing_out->vote_delay = options->V3AuthVoteDelay;
  902. timing_out->dist_delay = options->V3AuthDistDelay;
  903. }
  904. /** Return the start of the next interval of size <b>interval</b> (in seconds)
  905. * after <b>now</b>. Midnight always starts a fresh interval, and if the last
  906. * interval of a day would be truncated to less than half its size, it is
  907. * rolled into the previous interval. */
  908. time_t
  909. dirvote_get_start_of_next_interval(time_t now, int interval)
  910. {
  911. struct tm tm;
  912. time_t midnight_today;
  913. time_t midnight_tomorrow;
  914. time_t next;
  915. tor_gmtime_r(&now, &tm);
  916. tm.tm_hour = 0;
  917. tm.tm_min = 0;
  918. tm.tm_sec = 0;
  919. midnight_today = tor_timegm(&tm);
  920. midnight_tomorrow = midnight_today + (24*60*60);
  921. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  922. /* Intervals never cross midnight. */
  923. if (next > midnight_tomorrow)
  924. next = midnight_tomorrow;
  925. /* If the interval would only last half as long as it's supposed to, then
  926. * skip over to the next day. */
  927. if (next + interval/2 > midnight_tomorrow)
  928. next = midnight_tomorrow;
  929. return next;
  930. }
  931. /** Scheduling information for a voting interval. */
  932. static struct {
  933. /** When do we generate and distribute our vote for this interval? */
  934. time_t voting_starts;
  935. /** When do we send an HTTP request for any votes that we haven't
  936. * been posted yet?*/
  937. time_t fetch_missing_votes;
  938. /** When do we give up on getting more votes and generate a consensus? */
  939. time_t voting_ends;
  940. /** When do we send an HTTP request for any signatures we're expecting to
  941. * see on the consensus? */
  942. time_t fetch_missing_signatures;
  943. /** When do we publish the consensus? */
  944. time_t interval_starts;
  945. /** When do we discard old votes and pending detached signatures? */
  946. time_t discard_old_votes;
  947. /* True iff we have generated and distributed our vote. */
  948. int have_voted;
  949. /* True iff we've requested missing votes. */
  950. int have_fetched_missing_votes;
  951. /* True iff we have built a consensus and sent the signatures around. */
  952. int have_built_consensus;
  953. /* True iff we've fetched missing signatures. */
  954. int have_fetched_missing_signatures;
  955. /* True iff we have published our consensus. */
  956. int have_published_consensus;
  957. } voting_schedule = {0,0,0,0,0,0,0,0,0,0,0};
  958. /** Set voting_schedule to hold the timing for the next vote we should be
  959. * doing. */
  960. void
  961. dirvote_recalculate_timing(time_t now)
  962. {
  963. int interval, vote_delay, dist_delay;
  964. time_t start;
  965. time_t end;
  966. networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
  967. memset(&voting_schedule, 0, sizeof(voting_schedule));
  968. if (consensus) {
  969. interval = consensus->fresh_until - consensus->valid_after;
  970. vote_delay = consensus->vote_seconds;
  971. dist_delay = consensus->dist_seconds;
  972. } else {
  973. interval = 30*60;
  974. vote_delay = dist_delay = 300;
  975. }
  976. tor_assert(interval > 0);
  977. if (vote_delay + dist_delay > interval/2)
  978. vote_delay = dist_delay = interval / 4;
  979. start = voting_schedule.interval_starts =
  980. dirvote_get_start_of_next_interval(now,interval);
  981. end = dirvote_get_start_of_next_interval(start+1, interval);
  982. tor_assert(end > start);
  983. voting_schedule.fetch_missing_signatures = start - (dist_delay/2);
  984. voting_schedule.voting_ends = start - dist_delay;
  985. voting_schedule.fetch_missing_votes = start - dist_delay - (vote_delay/2);
  986. voting_schedule.voting_starts = start - dist_delay - vote_delay;
  987. voting_schedule.discard_old_votes = start;
  988. }
  989. /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
  990. void
  991. dirvote_act(time_t now)
  992. {
  993. if (!voting_schedule.voting_starts) {
  994. char *keys = list_v3_auth_ids();
  995. authority_cert_t *c = get_my_v3_authority_cert();
  996. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
  997. "Mine is %s.",
  998. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  999. tor_free(keys);
  1000. dirvote_recalculate_timing(now);
  1001. }
  1002. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  1003. log_notice(LD_DIR, "Time to vote.");
  1004. dirvote_perform_vote();
  1005. voting_schedule.have_voted = 1;
  1006. }
  1007. if (voting_schedule.fetch_missing_votes < now &&
  1008. !voting_schedule.have_fetched_missing_votes) {
  1009. log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
  1010. dirvote_fetch_missing_votes();
  1011. voting_schedule.have_fetched_missing_votes = 1;
  1012. }
  1013. if (voting_schedule.voting_ends < now &&
  1014. !voting_schedule.have_built_consensus) {
  1015. log_notice(LD_DIR, "Time to compute a consensus.");
  1016. dirvote_compute_consensus();
  1017. /* XXXX020 we will want to try again later if we haven't got enough
  1018. * votes yet. */
  1019. voting_schedule.have_built_consensus = 1;
  1020. }
  1021. if (voting_schedule.fetch_missing_signatures < now &&
  1022. !voting_schedule.have_fetched_missing_signatures) {
  1023. log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
  1024. dirvote_fetch_missing_signatures();
  1025. voting_schedule.have_fetched_missing_signatures = 1;
  1026. }
  1027. if (voting_schedule.interval_starts < now &&
  1028. !voting_schedule.have_published_consensus) {
  1029. log_notice(LD_DIR, "Time to publish the consensus.");
  1030. dirvote_publish_consensus();
  1031. /* XXXX020 we will want to try again later if we haven't got enough
  1032. * signatures yet. */
  1033. voting_schedule.have_published_consensus = 1;
  1034. }
  1035. if (voting_schedule.discard_old_votes < now) {
  1036. log_notice(LD_DIR, "Time to discard old votes.");
  1037. dirvote_clear_votes(0);
  1038. dirvote_recalculate_timing(now);
  1039. }
  1040. }
  1041. /** A vote networkstatus_vote_t and its unparsed body: held around so we can
  1042. * use it to generate a consensus (at voting_ends) and so we can serve it to
  1043. * other authorities that might want it. */
  1044. typedef struct pending_vote_t {
  1045. cached_dir_t *vote_body;
  1046. networkstatus_vote_t *vote;
  1047. } pending_vote_t;
  1048. /** List of pending_vote_t for the current vote. Before we've used them to
  1049. * build a consensus, the votes go here. */
  1050. static smartlist_t *pending_vote_list = NULL;
  1051. /** List of pending_vote_t for the previous vote. After we've used them to
  1052. * build a consensus, the votes go here for the next period. */
  1053. static smartlist_t *previous_vote_list = NULL;
  1054. /** The body of the consensus that we're currently building. Once we
  1055. * have it built, it goes into dirserv.c */
  1056. static char *pending_consensus_body = NULL;
  1057. /** The detached signatures for the consensus that we're currently
  1058. * building. */
  1059. static char *pending_consensus_signatures = NULL;
  1060. /** The parsed in-progress consensus document. */
  1061. static networkstatus_vote_t *pending_consensus = NULL;
  1062. /** List of ns_detached_signatures_t: hold signatures that get posted to us
  1063. * before we have generated the consensus on our own. */
  1064. static smartlist_t *pending_consensus_signature_list = NULL;
  1065. /** Generate a networkstatus vote and post it to all the v3 authorities.
  1066. * (V3 Authority only) */
  1067. static void
  1068. dirvote_perform_vote(void)
  1069. {
  1070. cached_dir_t *new_vote = generate_v3_networkstatus();
  1071. pending_vote_t *pending_vote;
  1072. int status;
  1073. const char *msg = "";
  1074. if (!new_vote)
  1075. return;
  1076. if (!(pending_vote = dirvote_add_vote(new_vote->dir, &msg, &status))) {
  1077. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1078. msg);
  1079. return;
  1080. }
  1081. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1082. ROUTER_PURPOSE_GENERAL,
  1083. V3_AUTHORITY,
  1084. pending_vote->vote_body->dir,
  1085. pending_vote->vote_body->dir_len, 0);
  1086. log_notice(LD_DIR, "Vote posted.");
  1087. }
  1088. /** Send an HTTP request to every other v3 authority, for the votes of every
  1089. * authority for which we haven't received a vote yet in this period. (V3
  1090. * authority only) */
  1091. static void
  1092. dirvote_fetch_missing_votes(void)
  1093. {
  1094. smartlist_t *missing_fps = smartlist_create();
  1095. char *resource;
  1096. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1097. trusted_dir_server_t *, ds,
  1098. {
  1099. if (!(ds->type & V3_AUTHORITY))
  1100. continue;
  1101. if (!dirvote_get_vote(ds->v3_identity_digest,
  1102. DGV_BY_ID|DGV_INCLUDE_PENDING)) {
  1103. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  1104. base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
  1105. DIGEST_LEN);
  1106. smartlist_add(missing_fps, cp);
  1107. }
  1108. });
  1109. if (!smartlist_len(missing_fps)) {
  1110. smartlist_free(missing_fps);
  1111. return;
  1112. }
  1113. log_notice(LOG_NOTICE, "We're missing votes from %d authorities. Asking "
  1114. "every other authority for a copy.", smartlist_len(missing_fps));
  1115. resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
  1116. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
  1117. 0, resource);
  1118. tor_free(resource);
  1119. SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
  1120. smartlist_free(missing_fps);
  1121. }
  1122. /** Send a request to every other authority for its detached signatures,
  1123. * unless we have signatures from all other v3 authorities already. */
  1124. static void
  1125. dirvote_fetch_missing_signatures(void)
  1126. {
  1127. if (!pending_consensus)
  1128. return;
  1129. if (networkstatus_check_consensus_signature(pending_consensus, -1) == 1)
  1130. return; /* we have a signature from everybody. */
  1131. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  1132. 0, NULL);
  1133. }
  1134. /** Drop all currently pending votes, consensus, and detached signatures. */
  1135. static void
  1136. dirvote_clear_votes(int all_votes)
  1137. {
  1138. if (!previous_vote_list)
  1139. previous_vote_list = smartlist_create();
  1140. if (!pending_vote_list)
  1141. pending_vote_list = smartlist_create();
  1142. /* All "previous" votes are now junk. */
  1143. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
  1144. cached_dir_decref(v->vote_body);
  1145. v->vote_body = NULL;
  1146. networkstatus_vote_free(v->vote);
  1147. tor_free(v);
  1148. });
  1149. smartlist_clear(previous_vote_list);
  1150. if (all_votes) {
  1151. /* If we're dumping all the votes, we delete the pending ones. */
  1152. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1153. cached_dir_decref(v->vote_body);
  1154. v->vote_body = NULL;
  1155. networkstatus_vote_free(v->vote);
  1156. tor_free(v);
  1157. });
  1158. } else {
  1159. /* Otherwise, we move them into "previous". */
  1160. smartlist_add_all(previous_vote_list, pending_vote_list);
  1161. }
  1162. smartlist_clear(pending_vote_list);
  1163. if (pending_consensus_signature_list) {
  1164. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1165. tor_free(cp));
  1166. smartlist_clear(pending_consensus_signature_list);
  1167. }
  1168. tor_free(pending_consensus_body);
  1169. tor_free(pending_consensus_signatures);
  1170. if (pending_consensus) {
  1171. networkstatus_vote_free(pending_consensus);
  1172. pending_consensus = NULL;
  1173. }
  1174. }
  1175. /* XXXX020 delete me. */
  1176. static char *
  1177. list_v3_auth_ids(void)
  1178. {
  1179. smartlist_t *known_v3_keys = smartlist_create();
  1180. char *keys;
  1181. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1182. trusted_dir_server_t *, ds,
  1183. if (!tor_digest_is_zero(ds->v3_identity_digest))
  1184. smartlist_add(known_v3_keys,
  1185. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  1186. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  1187. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  1188. smartlist_free(known_v3_keys);
  1189. return keys;
  1190. }
  1191. /** Called when we have received a networkstatus vote in <b>vote_body</b>.
  1192. * Parse and validate it, and on success store it as a pending vote (which we
  1193. * then return). Return NULL on failure. Sets *<b>msg_out</b> and
  1194. * *<b>status_out</b> to an HTTP response and status code. (V3 authority
  1195. * only) */
  1196. pending_vote_t *
  1197. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  1198. {
  1199. networkstatus_vote_t *vote;
  1200. networkstatus_voter_info_t *vi;
  1201. trusted_dir_server_t *ds;
  1202. pending_vote_t *pending_vote = NULL;
  1203. const char *end_of_vote = NULL;
  1204. int any_failed = 0;
  1205. tor_assert(vote_body);
  1206. tor_assert(msg_out);
  1207. tor_assert(status_out);
  1208. if (!pending_vote_list)
  1209. pending_vote_list = smartlist_create();
  1210. *status_out = 0;
  1211. *msg_out = NULL;
  1212. again:
  1213. vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote, 1);
  1214. if (!end_of_vote)
  1215. end_of_vote = vote_body + strlen(vote_body);
  1216. if (!vote) {
  1217. log_warn(LD_DIR, "Couldn't parse vote: length was %d",
  1218. (int)strlen(vote_body));
  1219. *msg_out = "Unable to parse vote";
  1220. goto err;
  1221. }
  1222. tor_assert(smartlist_len(vote->voters) == 1);
  1223. vi = get_voter(vote);
  1224. tor_assert(vi->good_signature == 1);
  1225. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1226. if (!ds || !(ds->type & V3_AUTHORITY)) {
  1227. char *keys = list_v3_auth_ids();
  1228. log_warn(LD_DIR, "Got a vote from an authority with authority key ID %s. "
  1229. "This authority %s. Known v3 key IDs are: %s",
  1230. hex_str(vi->identity_digest, DIGEST_LEN),
  1231. ds?"is not recognized":"is recognized, but is not listed as v3",
  1232. keys);
  1233. tor_free(keys);
  1234. *msg_out = "Vote not from a recognized v3 authority";
  1235. goto err;
  1236. }
  1237. tor_assert(vote->cert);
  1238. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1239. vote->cert->signing_key_digest)) {
  1240. /* Hey, it's a new cert! */
  1241. trusted_dirs_load_certs_from_string(
  1242. vote->cert->cache_info.signed_descriptor_body,
  1243. 0 /* from_store */);
  1244. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1245. vote->cert->signing_key_digest)) {
  1246. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  1247. }
  1248. }
  1249. /* Is it for the right period? */
  1250. if (vote->valid_after != voting_schedule.interval_starts) {
  1251. char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
  1252. format_iso_time(tbuf1, vote->valid_after);
  1253. format_iso_time(tbuf2, voting_schedule.interval_starts);
  1254. log_warn(LD_DIR, "Rejecting vote with valid-after time of %s; we were "
  1255. "expecting %s", tbuf1, tbuf2);
  1256. *msg_out = "Bad valid-after time";
  1257. goto err;
  1258. }
  1259. /* Now see whether we already h<ave a vote from this authority.*/
  1260. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1261. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1262. vote->cert->cache_info.identity_digest,
  1263. DIGEST_LEN)) {
  1264. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  1265. if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  1266. /* Ah, it's the same vote. Not a problem. */
  1267. log_info(LD_DIR, "Discarding a vote we already have.");
  1268. if (*status_out < 200)
  1269. *status_out = 200;
  1270. if (!*msg_out)
  1271. *msg_out = "OK";
  1272. goto discard;
  1273. } else if (v->vote->published < vote->published) {
  1274. log_notice(LD_DIR, "Replacing an older pending vote from this "
  1275. "directory.");
  1276. cached_dir_decref(v->vote_body);
  1277. networkstatus_vote_free(v->vote);
  1278. v->vote_body = new_cached_dir(tor_strndup(vote_body,
  1279. end_of_vote-vote_body),
  1280. vote->published);
  1281. v->vote = vote;
  1282. if (end_of_vote &&
  1283. !strcmpstart(end_of_vote, "network-status-version"))
  1284. goto again;
  1285. if (*status_out < 200)
  1286. *status_out = 200;
  1287. if (!*msg_out)
  1288. *msg_out = "OK";
  1289. return v;
  1290. } else {
  1291. *msg_out = "Already have a newer pending vote";
  1292. goto err;
  1293. }
  1294. }
  1295. });
  1296. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1297. pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
  1298. end_of_vote-vote_body),
  1299. vote->published);
  1300. pending_vote->vote = vote;
  1301. smartlist_add(pending_vote_list, pending_vote);
  1302. if (!strcmpstart(end_of_vote, "network-status-version ")) {
  1303. vote_body = end_of_vote;
  1304. goto again;
  1305. }
  1306. goto done;
  1307. err:
  1308. any_failed = 1;
  1309. if (!*msg_out)
  1310. *msg_out = "Error adding vote";
  1311. if (*status_out < 400)
  1312. *status_out = 400;
  1313. discard:
  1314. if (vote)
  1315. networkstatus_vote_free(vote);
  1316. if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
  1317. vote_body = end_of_vote;
  1318. goto again;
  1319. }
  1320. done:
  1321. if (*status_out < 200)
  1322. *status_out = 200;
  1323. if (!*msg_out)
  1324. *msg_out = "ok";
  1325. return any_failed ? NULL : pending_vote;
  1326. }
  1327. /** Try to compute a v3 networkstatus consensus from the currently pending
  1328. * votes. Return 0 on success, -1 on failure. Store the consensus in
  1329. * pending_consensus: it won't be ready to be published until we have
  1330. * everybody else's signatures collected too. (V3 Authoritity only) */
  1331. static int
  1332. dirvote_compute_consensus(void)
  1333. {
  1334. /* Have we got enough votes to try? */
  1335. int n_votes, n_voters;
  1336. smartlist_t *votes = NULL;
  1337. char *consensus_body = NULL, *signatures = NULL;
  1338. networkstatus_vote_t *consensus = NULL;
  1339. authority_cert_t *my_cert;
  1340. if (!pending_vote_list)
  1341. pending_vote_list = smartlist_create();
  1342. n_voters = get_n_authorities(V3_AUTHORITY);
  1343. n_votes = smartlist_len(pending_vote_list);
  1344. if (n_votes <= n_voters/2) {
  1345. log_warn(LD_DIR, "We don't have enough votes to generate a consensus.");
  1346. goto err;
  1347. }
  1348. if (!(my_cert = get_my_v3_authority_cert())) {
  1349. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1350. goto err;
  1351. }
  1352. votes = smartlist_create();
  1353. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1354. smartlist_add(votes, v->vote));
  1355. consensus_body = networkstatus_compute_consensus(
  1356. votes, n_voters,
  1357. my_cert->identity_key,
  1358. get_my_v3_authority_signing_key());
  1359. if (!consensus_body) {
  1360. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1361. goto err;
  1362. }
  1363. consensus = networkstatus_parse_vote_from_string(consensus_body, NULL, 0);
  1364. if (!consensus) {
  1365. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1366. goto err;
  1367. }
  1368. /* 'Check' our own signature, to mark it valid. */
  1369. networkstatus_check_consensus_signature(consensus, -1);
  1370. signatures = networkstatus_get_detached_signatures(consensus);
  1371. if (!signatures) {
  1372. log_warn(LD_DIR, "Couldn't extract signatures.");
  1373. goto err;
  1374. }
  1375. tor_free(pending_consensus_body);
  1376. pending_consensus_body = consensus_body;
  1377. tor_free(pending_consensus_signatures);
  1378. pending_consensus_signatures = signatures;
  1379. if (pending_consensus)
  1380. networkstatus_vote_free(pending_consensus);
  1381. pending_consensus = consensus;
  1382. if (pending_consensus_signature_list) {
  1383. int n_sigs = 0;
  1384. /* we may have gotten signatures for this consensus before we built
  1385. * it ourself. Add them now. */
  1386. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1387. {
  1388. const char *msg = NULL;
  1389. n_sigs += dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1390. tor_free(sig);
  1391. });
  1392. if (n_sigs)
  1393. log_notice(LD_DIR, "Added %d pending signatures while building "
  1394. "consensus.", n_sigs);
  1395. smartlist_clear(pending_consensus_signature_list);
  1396. }
  1397. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  1398. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  1399. ROUTER_PURPOSE_GENERAL,
  1400. V3_AUTHORITY,
  1401. pending_consensus_signatures,
  1402. strlen(pending_consensus_signatures), 0);
  1403. log_notice(LD_DIR, "Signature(s) posted.");
  1404. return 0;
  1405. err:
  1406. if (votes)
  1407. smartlist_free(votes);
  1408. tor_free(consensus_body);
  1409. tor_free(signatures);
  1410. networkstatus_vote_free(consensus);
  1411. return -1;
  1412. }
  1413. /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
  1414. * signatures on the currently pending consensus. Add them to the consensus
  1415. * as appropriate. Return the number of signatures added. (?) */
  1416. static int
  1417. dirvote_add_signatures_to_pending_consensus(
  1418. const char *detached_signatures_body,
  1419. const char **msg_out)
  1420. {
  1421. ns_detached_signatures_t *sigs = NULL;
  1422. int r = -1;
  1423. tor_assert(detached_signatures_body);
  1424. tor_assert(msg_out);
  1425. /* Only call if we have a pending consensus right now. */
  1426. tor_assert(pending_consensus);
  1427. tor_assert(pending_consensus_body);
  1428. tor_assert(pending_consensus_signatures);
  1429. *msg_out = NULL;
  1430. if (!(sigs = networkstatus_parse_detached_signatures(
  1431. detached_signatures_body, NULL))) {
  1432. *msg_out = "Couldn't parse detached signatures.";
  1433. goto err;
  1434. }
  1435. r = networkstatus_add_detached_signatures(pending_consensus,
  1436. sigs);
  1437. if (r >= 0) {
  1438. /* XXXX This should really be its own function. */
  1439. char *new_detached =
  1440. networkstatus_get_detached_signatures(pending_consensus);
  1441. const char *src;
  1442. char *dst;
  1443. size_t new_consensus_len =
  1444. strlen(pending_consensus_body) + strlen(new_detached) + 1;
  1445. pending_consensus_body = tor_realloc(pending_consensus_body,
  1446. new_consensus_len);
  1447. dst = strstr(pending_consensus_body, "directory-signature ");
  1448. tor_assert(dst);
  1449. src = strstr(new_detached, "directory-signature ");
  1450. tor_assert(src);
  1451. strlcpy(dst, src, new_consensus_len - (dst-pending_consensus_body));
  1452. /* XXXX020 remove this block once it has failed to crash for a while. */
  1453. {
  1454. ns_detached_signatures_t *sigs =
  1455. networkstatus_parse_detached_signatures(new_detached, NULL);
  1456. networkstatus_vote_t *v = networkstatus_parse_vote_from_string(
  1457. pending_consensus_body, NULL, 0);
  1458. tor_assert(sigs);
  1459. ns_detached_signatures_free(sigs);
  1460. tor_assert(v);
  1461. networkstatus_vote_free(v);
  1462. }
  1463. tor_free(pending_consensus_signatures);
  1464. pending_consensus_signatures = new_detached;
  1465. *msg_out = "Signatures added";
  1466. } else {
  1467. *msg_out = "Digest mismatch when adding detached signatures";
  1468. }
  1469. goto done;
  1470. err:
  1471. if (!msg_out)
  1472. *msg_out = "Unrecognized error while adding detached signatures.";
  1473. done:
  1474. if (sigs)
  1475. ns_detached_signatures_free(sigs);
  1476. return r;
  1477. }
  1478. /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
  1479. * signatures on the currently pending consensus. Add them to the pending
  1480. * consensus (if we have one); otherwise queue them until we have a
  1481. * consensus. Return negative on failure, nonnegative on success. */
  1482. int
  1483. dirvote_add_signatures(const char *detached_signatures_body,
  1484. const char **msg)
  1485. {
  1486. if (pending_consensus) {
  1487. log_notice(LD_DIR, "Got a signature. Adding it to the pending consensus.");
  1488. return dirvote_add_signatures_to_pending_consensus(
  1489. detached_signatures_body, msg);
  1490. } else {
  1491. log_notice(LD_DIR, "Got a signature. Queueing it for the next consensus.");
  1492. if (!pending_consensus_signature_list)
  1493. pending_consensus_signature_list = smartlist_create();
  1494. smartlist_add(pending_consensus_signature_list,
  1495. tor_strdup(detached_signatures_body));
  1496. *msg = "Signature queued";
  1497. return 0;
  1498. }
  1499. }
  1500. /** Replace the consensus that we're currently serving with the one that we've
  1501. * been building. (V3 Authority only) */
  1502. static int
  1503. dirvote_publish_consensus(void)
  1504. {
  1505. /* Can we actually publish it yet? */
  1506. if (!pending_consensus ||
  1507. networkstatus_check_consensus_signature(pending_consensus, 1)<0) {
  1508. log_warn(LD_DIR, "Not enough info to publish pending consensus");
  1509. return -1;
  1510. }
  1511. if (networkstatus_set_current_consensus(pending_consensus_body, 0, 0))
  1512. log_warn(LD_DIR, "Error publishing consensus");
  1513. else
  1514. log_notice(LD_DIR, "Consensus published.");
  1515. return 0;
  1516. }
  1517. /** Release all static storage held in dirvote.c */
  1518. void
  1519. dirvote_free_all(void)
  1520. {
  1521. dirvote_clear_votes(1);
  1522. /* now empty as a result of clear_pending_votes. */
  1523. smartlist_free(pending_vote_list);
  1524. pending_vote_list = NULL;
  1525. smartlist_free(previous_vote_list);
  1526. previous_vote_list = NULL;
  1527. tor_free(pending_consensus_body);
  1528. tor_free(pending_consensus_signatures);
  1529. if (pending_consensus) {
  1530. networkstatus_vote_free(pending_consensus);
  1531. pending_consensus = NULL;
  1532. }
  1533. if (pending_consensus_signature_list) {
  1534. /* now empty as a result of clear_pending_votes. */
  1535. smartlist_free(pending_consensus_signature_list);
  1536. pending_consensus_signature_list = NULL;
  1537. }
  1538. }
  1539. /* ====
  1540. * Access to pending items.
  1541. * ==== */
  1542. /** Return the body of the consensus that we're currently trying to build. */
  1543. const char *
  1544. dirvote_get_pending_consensus(void)
  1545. {
  1546. return pending_consensus_body;
  1547. }
  1548. /** Return the signatures that we know for the consensus that we're currently
  1549. * trying to build */
  1550. const char *
  1551. dirvote_get_pending_detached_signatures(void)
  1552. {
  1553. return pending_consensus_signatures;
  1554. }
  1555. /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
  1556. * vote for the authority with the v3 authority identity key digest <b>fp</b>;
  1557. * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
  1558. * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
  1559. * false, do not consider any votes for a consensus that's already been built.
  1560. * If <b>include_pending</b> is false, do not consider any votes for the
  1561. * consensus that's in progress. May return NULL if we have no vote for the
  1562. * authority in question. */
  1563. const cached_dir_t *
  1564. dirvote_get_vote(const char *fp, int flags)
  1565. {
  1566. int by_id = flags & DGV_BY_ID;
  1567. const int include_pending = flags & DGV_INCLUDE_PENDING;
  1568. const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
  1569. if (!pending_vote_list && !previous_vote_list)
  1570. return NULL;
  1571. if (fp == NULL) {
  1572. authority_cert_t *c = get_my_v3_authority_cert();
  1573. if (c) {
  1574. fp = c->cache_info.identity_digest;
  1575. by_id = 1;
  1576. } else
  1577. return NULL;
  1578. }
  1579. if (by_id) {
  1580. if (pending_vote_list && include_pending) {
  1581. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1582. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1583. return pv->vote_body);
  1584. }
  1585. if (previous_vote_list && include_previous) {
  1586. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1587. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1588. return pv->vote_body);
  1589. }
  1590. } else {
  1591. if (pending_vote_list && include_pending) {
  1592. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1593. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  1594. return pv->vote_body);
  1595. }
  1596. if (previous_vote_list && include_previous) {
  1597. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1598. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  1599. return pv->vote_body);
  1600. }
  1601. }
  1602. return NULL;
  1603. }