dirvote.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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(or_options_t *options, time_t now)
  955. {
  956. int interval, vote_delay, dist_delay;
  957. time_t start;
  958. time_t end;
  959. networkstatus_vote_t *consensus;
  960. if (!authdir_mode_v3(options))
  961. return;
  962. consensus = networkstatus_get_live_consensus(now);
  963. memset(&voting_schedule, 0, sizeof(voting_schedule));
  964. if (consensus) {
  965. interval = consensus->fresh_until - consensus->valid_after;
  966. vote_delay = consensus->vote_seconds;
  967. dist_delay = consensus->dist_seconds;
  968. } else {
  969. interval = 30*60;
  970. vote_delay = dist_delay = 300;
  971. }
  972. tor_assert(interval > 0);
  973. if (vote_delay + dist_delay > interval/2)
  974. vote_delay = dist_delay = interval / 4;
  975. start = voting_schedule.interval_starts =
  976. dirvote_get_start_of_next_interval(now,interval);
  977. end = dirvote_get_start_of_next_interval(start+1, interval);
  978. tor_assert(end > start);
  979. voting_schedule.fetch_missing_signatures = start - (dist_delay/2);
  980. voting_schedule.voting_ends = start - dist_delay;
  981. voting_schedule.fetch_missing_votes = start - dist_delay - (vote_delay/2);
  982. voting_schedule.voting_starts = start - dist_delay - vote_delay;
  983. {
  984. char tbuf[ISO_TIME_LEN+1];
  985. format_iso_time(tbuf, voting_schedule.interval_starts);
  986. log_notice(LD_DIR,"Choosing expected valid-after time as %s: "
  987. "consensus_set=%d, interval=%d",
  988. tbuf, consensus?1:0, interval);
  989. }
  990. }
  991. /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
  992. void
  993. dirvote_act(or_options_t *options, time_t now)
  994. {
  995. if (!authdir_mode_v3(options))
  996. return;
  997. if (!voting_schedule.voting_starts) {
  998. char *keys = list_v3_auth_ids();
  999. authority_cert_t *c = get_my_v3_authority_cert();
  1000. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
  1001. "Mine is %s.",
  1002. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  1003. tor_free(keys);
  1004. dirvote_recalculate_timing(options, now);
  1005. }
  1006. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  1007. log_notice(LD_DIR, "Time to vote.");
  1008. dirvote_perform_vote();
  1009. voting_schedule.have_voted = 1;
  1010. }
  1011. if (voting_schedule.fetch_missing_votes < now &&
  1012. !voting_schedule.have_fetched_missing_votes) {
  1013. log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
  1014. dirvote_fetch_missing_votes();
  1015. voting_schedule.have_fetched_missing_votes = 1;
  1016. }
  1017. if (voting_schedule.voting_ends < now &&
  1018. !voting_schedule.have_built_consensus) {
  1019. log_notice(LD_DIR, "Time to compute a consensus.");
  1020. dirvote_compute_consensus();
  1021. /* XXXX020 we will want to try again later if we haven't got enough
  1022. * votes yet. */
  1023. voting_schedule.have_built_consensus = 1;
  1024. }
  1025. if (voting_schedule.fetch_missing_signatures < now &&
  1026. !voting_schedule.have_fetched_missing_signatures) {
  1027. log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
  1028. dirvote_fetch_missing_signatures();
  1029. voting_schedule.have_fetched_missing_signatures = 1;
  1030. }
  1031. if (voting_schedule.interval_starts < now &&
  1032. !voting_schedule.have_published_consensus) {
  1033. log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
  1034. dirvote_publish_consensus();
  1035. dirvote_clear_votes(0);
  1036. voting_schedule.have_published_consensus = 1;
  1037. /* XXXX020 we will want to try again later if we haven't got enough
  1038. * signatures yet. */
  1039. dirvote_recalculate_timing(options, now);
  1040. }
  1041. }
  1042. /** A vote networkstatus_vote_t and its unparsed body: held around so we can
  1043. * use it to generate a consensus (at voting_ends) and so we can serve it to
  1044. * other authorities that might want it. */
  1045. typedef struct pending_vote_t {
  1046. cached_dir_t *vote_body;
  1047. networkstatus_vote_t *vote;
  1048. } pending_vote_t;
  1049. /** List of pending_vote_t for the current vote. Before we've used them to
  1050. * build a consensus, the votes go here. */
  1051. static smartlist_t *pending_vote_list = NULL;
  1052. /** List of pending_vote_t for the previous vote. After we've used them to
  1053. * build a consensus, the votes go here for the next period. */
  1054. static smartlist_t *previous_vote_list = NULL;
  1055. /** The body of the consensus that we're currently building. Once we
  1056. * have it built, it goes into dirserv.c */
  1057. static char *pending_consensus_body = NULL;
  1058. /** The detached signatures for the consensus that we're currently
  1059. * building. */
  1060. static char *pending_consensus_signatures = NULL;
  1061. /** The parsed in-progress consensus document. */
  1062. static networkstatus_vote_t *pending_consensus = NULL;
  1063. /** List of ns_detached_signatures_t: hold signatures that get posted to us
  1064. * before we have generated the consensus on our own. */
  1065. static smartlist_t *pending_consensus_signature_list = NULL;
  1066. /** Generate a networkstatus vote and post it to all the v3 authorities.
  1067. * (V3 Authority only) */
  1068. static void
  1069. dirvote_perform_vote(void)
  1070. {
  1071. cached_dir_t *new_vote = generate_v3_networkstatus();
  1072. pending_vote_t *pending_vote;
  1073. int status;
  1074. const char *msg = "";
  1075. if (!new_vote)
  1076. return;
  1077. if (!(pending_vote = dirvote_add_vote(new_vote->dir, &msg, &status))) {
  1078. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1079. msg);
  1080. return;
  1081. }
  1082. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1083. ROUTER_PURPOSE_GENERAL,
  1084. V3_AUTHORITY,
  1085. pending_vote->vote_body->dir,
  1086. pending_vote->vote_body->dir_len, 0);
  1087. log_notice(LD_DIR, "Vote posted.");
  1088. }
  1089. /** Send an HTTP request to every other v3 authority, for the votes of every
  1090. * authority for which we haven't received a vote yet in this period. (V3
  1091. * authority only) */
  1092. static void
  1093. dirvote_fetch_missing_votes(void)
  1094. {
  1095. smartlist_t *missing_fps = smartlist_create();
  1096. char *resource;
  1097. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1098. trusted_dir_server_t *, ds,
  1099. {
  1100. if (!(ds->type & V3_AUTHORITY))
  1101. continue;
  1102. if (!dirvote_get_vote(ds->v3_identity_digest,
  1103. DGV_BY_ID|DGV_INCLUDE_PENDING)) {
  1104. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  1105. base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
  1106. DIGEST_LEN);
  1107. smartlist_add(missing_fps, cp);
  1108. }
  1109. });
  1110. if (!smartlist_len(missing_fps)) {
  1111. smartlist_free(missing_fps);
  1112. return;
  1113. }
  1114. log_notice(LOG_NOTICE, "We're missing votes from %d authorities. Asking "
  1115. "every other authority for a copy.", smartlist_len(missing_fps));
  1116. resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
  1117. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
  1118. 0, resource);
  1119. tor_free(resource);
  1120. SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
  1121. smartlist_free(missing_fps);
  1122. }
  1123. /** Send a request to every other authority for its detached signatures,
  1124. * unless we have signatures from all other v3 authorities already. */
  1125. static void
  1126. dirvote_fetch_missing_signatures(void)
  1127. {
  1128. if (!pending_consensus)
  1129. return;
  1130. if (networkstatus_check_consensus_signature(pending_consensus, -1) == 1)
  1131. return; /* we have a signature from everybody. */
  1132. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  1133. 0, NULL);
  1134. }
  1135. /** Drop all currently pending votes, consensus, and detached signatures. */
  1136. static void
  1137. dirvote_clear_votes(int all_votes)
  1138. {
  1139. if (!previous_vote_list)
  1140. previous_vote_list = smartlist_create();
  1141. if (!pending_vote_list)
  1142. pending_vote_list = smartlist_create();
  1143. /* All "previous" votes are now junk. */
  1144. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
  1145. cached_dir_decref(v->vote_body);
  1146. v->vote_body = NULL;
  1147. networkstatus_vote_free(v->vote);
  1148. tor_free(v);
  1149. });
  1150. smartlist_clear(previous_vote_list);
  1151. if (all_votes) {
  1152. /* If we're dumping all the votes, we delete the pending ones. */
  1153. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1154. cached_dir_decref(v->vote_body);
  1155. v->vote_body = NULL;
  1156. networkstatus_vote_free(v->vote);
  1157. tor_free(v);
  1158. });
  1159. } else {
  1160. /* Otherwise, we move them into "previous". */
  1161. smartlist_add_all(previous_vote_list, pending_vote_list);
  1162. }
  1163. smartlist_clear(pending_vote_list);
  1164. if (pending_consensus_signature_list) {
  1165. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1166. tor_free(cp));
  1167. smartlist_clear(pending_consensus_signature_list);
  1168. }
  1169. tor_free(pending_consensus_body);
  1170. tor_free(pending_consensus_signatures);
  1171. if (pending_consensus) {
  1172. networkstatus_vote_free(pending_consensus);
  1173. pending_consensus = NULL;
  1174. }
  1175. }
  1176. /** Return a newly allocated string containing the hex-encoded v3 authority
  1177. identity digest of every recognized v3 authority. */
  1178. static char *
  1179. list_v3_auth_ids(void)
  1180. {
  1181. smartlist_t *known_v3_keys = smartlist_create();
  1182. char *keys;
  1183. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1184. trusted_dir_server_t *, ds,
  1185. if ((ds->type & V3_AUTHORITY) &&
  1186. !tor_digest_is_zero(ds->v3_identity_digest))
  1187. smartlist_add(known_v3_keys,
  1188. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  1189. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  1190. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  1191. smartlist_free(known_v3_keys);
  1192. return keys;
  1193. }
  1194. /** Called when we have received a networkstatus vote in <b>vote_body</b>.
  1195. * Parse and validate it, and on success store it as a pending vote (which we
  1196. * then return). Return NULL on failure. Sets *<b>msg_out</b> and
  1197. * *<b>status_out</b> to an HTTP response and status code. (V3 authority
  1198. * only) */
  1199. pending_vote_t *
  1200. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  1201. {
  1202. networkstatus_vote_t *vote;
  1203. networkstatus_voter_info_t *vi;
  1204. trusted_dir_server_t *ds;
  1205. pending_vote_t *pending_vote = NULL;
  1206. const char *end_of_vote = NULL;
  1207. int any_failed = 0;
  1208. tor_assert(vote_body);
  1209. tor_assert(msg_out);
  1210. tor_assert(status_out);
  1211. if (!pending_vote_list)
  1212. pending_vote_list = smartlist_create();
  1213. *status_out = 0;
  1214. *msg_out = NULL;
  1215. again:
  1216. vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote, 1);
  1217. if (!end_of_vote)
  1218. end_of_vote = vote_body + strlen(vote_body);
  1219. if (!vote) {
  1220. log_warn(LD_DIR, "Couldn't parse vote: length was %d",
  1221. (int)strlen(vote_body));
  1222. *msg_out = "Unable to parse vote";
  1223. goto err;
  1224. }
  1225. tor_assert(smartlist_len(vote->voters) == 1);
  1226. vi = get_voter(vote);
  1227. tor_assert(vi->good_signature == 1);
  1228. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1229. if (!ds || !(ds->type & V3_AUTHORITY)) {
  1230. char *keys = list_v3_auth_ids();
  1231. log_warn(LD_DIR, "Got a vote from an authority with authority key ID %s. "
  1232. "This authority %s. Known v3 key IDs are: %s",
  1233. hex_str(vi->identity_digest, DIGEST_LEN),
  1234. ds?"is not recognized":"is recognized, but is not listed as v3",
  1235. keys);
  1236. tor_free(keys);
  1237. *msg_out = "Vote not from a recognized v3 authority";
  1238. goto err;
  1239. }
  1240. tor_assert(vote->cert);
  1241. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1242. vote->cert->signing_key_digest)) {
  1243. /* Hey, it's a new cert! */
  1244. trusted_dirs_load_certs_from_string(
  1245. vote->cert->cache_info.signed_descriptor_body,
  1246. 0 /* from_store */);
  1247. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1248. vote->cert->signing_key_digest)) {
  1249. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  1250. }
  1251. }
  1252. /* Is it for the right period? */
  1253. if (vote->valid_after != voting_schedule.interval_starts) {
  1254. char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
  1255. format_iso_time(tbuf1, vote->valid_after);
  1256. format_iso_time(tbuf2, voting_schedule.interval_starts);
  1257. log_warn(LD_DIR, "Rejecting vote with valid-after time of %s; we were "
  1258. "expecting %s", tbuf1, tbuf2);
  1259. *msg_out = "Bad valid-after time";
  1260. goto err;
  1261. }
  1262. /* Now see whether we already h<ave a vote from this authority.*/
  1263. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1264. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1265. vote->cert->cache_info.identity_digest,
  1266. DIGEST_LEN)) {
  1267. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  1268. if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  1269. /* Ah, it's the same vote. Not a problem. */
  1270. log_info(LD_DIR, "Discarding a vote we already have.");
  1271. if (*status_out < 200)
  1272. *status_out = 200;
  1273. goto discard;
  1274. } else if (v->vote->published < vote->published) {
  1275. log_notice(LD_DIR, "Replacing an older pending vote from this "
  1276. "directory.");
  1277. cached_dir_decref(v->vote_body);
  1278. networkstatus_vote_free(v->vote);
  1279. v->vote_body = new_cached_dir(tor_strndup(vote_body,
  1280. end_of_vote-vote_body),
  1281. vote->published);
  1282. v->vote = vote;
  1283. if (end_of_vote &&
  1284. !strcmpstart(end_of_vote, "network-status-version"))
  1285. goto again;
  1286. if (*status_out < 200)
  1287. *status_out = 200;
  1288. if (!*msg_out)
  1289. *msg_out = "OK";
  1290. return v;
  1291. } else {
  1292. *msg_out = "Already have a newer pending vote";
  1293. goto err;
  1294. }
  1295. }
  1296. });
  1297. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1298. pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
  1299. end_of_vote-vote_body),
  1300. vote->published);
  1301. pending_vote->vote = vote;
  1302. smartlist_add(pending_vote_list, pending_vote);
  1303. if (!strcmpstart(end_of_vote, "network-status-version ")) {
  1304. vote_body = end_of_vote;
  1305. goto again;
  1306. }
  1307. goto done;
  1308. err:
  1309. any_failed = 1;
  1310. if (!*msg_out)
  1311. *msg_out = "Error adding vote";
  1312. if (*status_out < 400)
  1313. *status_out = 400;
  1314. discard:
  1315. if (vote)
  1316. networkstatus_vote_free(vote);
  1317. if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
  1318. vote_body = end_of_vote;
  1319. goto again;
  1320. }
  1321. done:
  1322. if (*status_out < 200)
  1323. *status_out = 200;
  1324. if (!*msg_out) {
  1325. if (!any_failed && !pending_vote) {
  1326. *msg_out = "Duplicate discarded";
  1327. } else {
  1328. *msg_out = "ok";
  1329. }
  1330. }
  1331. return any_failed ? NULL : pending_vote;
  1332. }
  1333. /** Try to compute a v3 networkstatus consensus from the currently pending
  1334. * votes. Return 0 on success, -1 on failure. Store the consensus in
  1335. * pending_consensus: it won't be ready to be published until we have
  1336. * everybody else's signatures collected too. (V3 Authoritity only) */
  1337. static int
  1338. dirvote_compute_consensus(void)
  1339. {
  1340. /* Have we got enough votes to try? */
  1341. int n_votes, n_voters;
  1342. smartlist_t *votes = NULL;
  1343. char *consensus_body = NULL, *signatures = NULL;
  1344. networkstatus_vote_t *consensus = NULL;
  1345. authority_cert_t *my_cert;
  1346. if (!pending_vote_list)
  1347. pending_vote_list = smartlist_create();
  1348. n_voters = get_n_authorities(V3_AUTHORITY);
  1349. n_votes = smartlist_len(pending_vote_list);
  1350. if (n_votes <= n_voters/2) {
  1351. log_warn(LD_DIR, "We don't have enough votes to generate a consensus.");
  1352. goto err;
  1353. }
  1354. if (!(my_cert = get_my_v3_authority_cert())) {
  1355. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1356. goto err;
  1357. }
  1358. votes = smartlist_create();
  1359. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1360. smartlist_add(votes, v->vote));
  1361. consensus_body = networkstatus_compute_consensus(
  1362. votes, n_voters,
  1363. my_cert->identity_key,
  1364. get_my_v3_authority_signing_key());
  1365. if (!consensus_body) {
  1366. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1367. goto err;
  1368. }
  1369. consensus = networkstatus_parse_vote_from_string(consensus_body, NULL, 0);
  1370. if (!consensus) {
  1371. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1372. goto err;
  1373. }
  1374. /* 'Check' our own signature, to mark it valid. */
  1375. networkstatus_check_consensus_signature(consensus, -1);
  1376. signatures = networkstatus_get_detached_signatures(consensus);
  1377. if (!signatures) {
  1378. log_warn(LD_DIR, "Couldn't extract signatures.");
  1379. goto err;
  1380. }
  1381. tor_free(pending_consensus_body);
  1382. pending_consensus_body = consensus_body;
  1383. tor_free(pending_consensus_signatures);
  1384. pending_consensus_signatures = signatures;
  1385. if (pending_consensus)
  1386. networkstatus_vote_free(pending_consensus);
  1387. pending_consensus = consensus;
  1388. if (pending_consensus_signature_list) {
  1389. int n_sigs = 0;
  1390. /* we may have gotten signatures for this consensus before we built
  1391. * it ourself. Add them now. */
  1392. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1393. {
  1394. const char *msg = NULL;
  1395. n_sigs += dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1396. tor_free(sig);
  1397. });
  1398. if (n_sigs)
  1399. log_notice(LD_DIR, "Added %d pending signatures while building "
  1400. "consensus.", n_sigs);
  1401. smartlist_clear(pending_consensus_signature_list);
  1402. }
  1403. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  1404. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  1405. ROUTER_PURPOSE_GENERAL,
  1406. V3_AUTHORITY,
  1407. pending_consensus_signatures,
  1408. strlen(pending_consensus_signatures), 0);
  1409. log_notice(LD_DIR, "Signature(s) posted.");
  1410. return 0;
  1411. err:
  1412. if (votes)
  1413. smartlist_free(votes);
  1414. tor_free(consensus_body);
  1415. tor_free(signatures);
  1416. networkstatus_vote_free(consensus);
  1417. return -1;
  1418. }
  1419. /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
  1420. * signatures on the currently pending consensus. Add them to the consensus
  1421. * as appropriate. Return the number of signatures added. (?) */
  1422. static int
  1423. dirvote_add_signatures_to_pending_consensus(
  1424. const char *detached_signatures_body,
  1425. const char **msg_out)
  1426. {
  1427. ns_detached_signatures_t *sigs = NULL;
  1428. int r = -1;
  1429. tor_assert(detached_signatures_body);
  1430. tor_assert(msg_out);
  1431. /* Only call if we have a pending consensus right now. */
  1432. tor_assert(pending_consensus);
  1433. tor_assert(pending_consensus_body);
  1434. tor_assert(pending_consensus_signatures);
  1435. *msg_out = NULL;
  1436. if (!(sigs = networkstatus_parse_detached_signatures(
  1437. detached_signatures_body, NULL))) {
  1438. *msg_out = "Couldn't parse detached signatures.";
  1439. goto err;
  1440. }
  1441. r = networkstatus_add_detached_signatures(pending_consensus,
  1442. sigs);
  1443. if (r >= 0) {
  1444. char *new_detached =
  1445. networkstatus_get_detached_signatures(pending_consensus);
  1446. const char *src;
  1447. char *dst;
  1448. size_t new_consensus_len =
  1449. strlen(pending_consensus_body) + strlen(new_detached) + 1;
  1450. pending_consensus_body = tor_realloc(pending_consensus_body,
  1451. new_consensus_len);
  1452. dst = strstr(pending_consensus_body, "directory-signature ");
  1453. tor_assert(dst);
  1454. src = strstr(new_detached, "directory-signature ");
  1455. tor_assert(src);
  1456. strlcpy(dst, src, new_consensus_len - (dst-pending_consensus_body));
  1457. /* XXXX020 remove this block once it has failed to crash for a while. */
  1458. {
  1459. ns_detached_signatures_t *sigs =
  1460. networkstatus_parse_detached_signatures(new_detached, NULL);
  1461. networkstatus_vote_t *v = networkstatus_parse_vote_from_string(
  1462. pending_consensus_body, NULL, 0);
  1463. tor_assert(sigs);
  1464. ns_detached_signatures_free(sigs);
  1465. tor_assert(v);
  1466. networkstatus_vote_free(v);
  1467. }
  1468. tor_free(pending_consensus_signatures);
  1469. pending_consensus_signatures = new_detached;
  1470. *msg_out = "Signatures added";
  1471. } else {
  1472. *msg_out = "Digest mismatch when adding detached signatures";
  1473. }
  1474. goto done;
  1475. err:
  1476. if (!msg_out)
  1477. *msg_out = "Unrecognized error while adding detached signatures.";
  1478. done:
  1479. if (sigs)
  1480. ns_detached_signatures_free(sigs);
  1481. return r;
  1482. }
  1483. /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
  1484. * signatures on the currently pending consensus. Add them to the pending
  1485. * consensus (if we have one); otherwise queue them until we have a
  1486. * consensus. Return negative on failure, nonnegative on success. */
  1487. int
  1488. dirvote_add_signatures(const char *detached_signatures_body,
  1489. const char **msg)
  1490. {
  1491. if (pending_consensus) {
  1492. log_notice(LD_DIR, "Got a signature. Adding it to the pending consensus.");
  1493. return dirvote_add_signatures_to_pending_consensus(
  1494. detached_signatures_body, msg);
  1495. } else {
  1496. log_notice(LD_DIR, "Got a signature. Queueing it for the next consensus.");
  1497. if (!pending_consensus_signature_list)
  1498. pending_consensus_signature_list = smartlist_create();
  1499. smartlist_add(pending_consensus_signature_list,
  1500. tor_strdup(detached_signatures_body));
  1501. *msg = "Signature queued";
  1502. return 0;
  1503. }
  1504. }
  1505. /** Replace the consensus that we're currently serving with the one that we've
  1506. * been building. (V3 Authority only) */
  1507. static int
  1508. dirvote_publish_consensus(void)
  1509. {
  1510. /* Can we actually publish it yet? */
  1511. if (!pending_consensus ||
  1512. networkstatus_check_consensus_signature(pending_consensus, 1)<0) {
  1513. log_warn(LD_DIR, "Not enough info to publish pending consensus");
  1514. return -1;
  1515. }
  1516. if (networkstatus_set_current_consensus(pending_consensus_body, 0, 0))
  1517. log_warn(LD_DIR, "Error publishing consensus");
  1518. else
  1519. log_notice(LD_DIR, "Consensus published.");
  1520. return 0;
  1521. }
  1522. /** Release all static storage held in dirvote.c */
  1523. void
  1524. dirvote_free_all(void)
  1525. {
  1526. dirvote_clear_votes(1);
  1527. /* now empty as a result of clear_pending_votes. */
  1528. smartlist_free(pending_vote_list);
  1529. pending_vote_list = NULL;
  1530. smartlist_free(previous_vote_list);
  1531. previous_vote_list = NULL;
  1532. tor_free(pending_consensus_body);
  1533. tor_free(pending_consensus_signatures);
  1534. if (pending_consensus) {
  1535. networkstatus_vote_free(pending_consensus);
  1536. pending_consensus = NULL;
  1537. }
  1538. if (pending_consensus_signature_list) {
  1539. /* now empty as a result of clear_pending_votes. */
  1540. smartlist_free(pending_consensus_signature_list);
  1541. pending_consensus_signature_list = NULL;
  1542. }
  1543. }
  1544. /* ====
  1545. * Access to pending items.
  1546. * ==== */
  1547. /** Return the body of the consensus that we're currently trying to build. */
  1548. const char *
  1549. dirvote_get_pending_consensus(void)
  1550. {
  1551. return pending_consensus_body;
  1552. }
  1553. /** Return the signatures that we know for the consensus that we're currently
  1554. * trying to build */
  1555. const char *
  1556. dirvote_get_pending_detached_signatures(void)
  1557. {
  1558. return pending_consensus_signatures;
  1559. }
  1560. /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
  1561. * vote for the authority with the v3 authority identity key digest <b>fp</b>;
  1562. * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
  1563. * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
  1564. * false, do not consider any votes for a consensus that's already been built.
  1565. * If <b>include_pending</b> is false, do not consider any votes for the
  1566. * consensus that's in progress. May return NULL if we have no vote for the
  1567. * authority in question. */
  1568. const cached_dir_t *
  1569. dirvote_get_vote(const char *fp, int flags)
  1570. {
  1571. int by_id = flags & DGV_BY_ID;
  1572. const int include_pending = flags & DGV_INCLUDE_PENDING;
  1573. const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
  1574. if (!pending_vote_list && !previous_vote_list)
  1575. return NULL;
  1576. if (fp == NULL) {
  1577. authority_cert_t *c = get_my_v3_authority_cert();
  1578. if (c) {
  1579. fp = c->cache_info.identity_digest;
  1580. by_id = 1;
  1581. } else
  1582. return NULL;
  1583. }
  1584. if (by_id) {
  1585. if (pending_vote_list && include_pending) {
  1586. SMARTLIST_FOREACH(pending_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. if (previous_vote_list && include_previous) {
  1591. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1592. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1593. return pv->vote_body);
  1594. }
  1595. } else {
  1596. if (pending_vote_list && include_pending) {
  1597. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1598. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  1599. return pv->vote_body);
  1600. }
  1601. if (previous_vote_list && include_previous) {
  1602. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1603. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  1604. return pv->vote_body);
  1605. }
  1606. }
  1607. return NULL;
  1608. }