dirvote.c 57 KB

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