dirvote.c 60 KB

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