dirvote.c 58 KB

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