dirvote.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char dirvote_c_id[] =
  7. "$Id$";
  8. #define DIRVOTE_PRIVATE
  9. #include "or.h"
  10. /**
  11. * \file dirvote.c
  12. * \brief Functions to compute directory consensus, and schedule voting.
  13. **/
  14. static int dirvote_add_signatures_to_pending_consensus(
  15. const char *detached_signatures_body,
  16. const char **msg_out);
  17. static char *list_v3_auth_ids(void);
  18. static void dirvote_fetch_missing_votes(void);
  19. static void dirvote_fetch_missing_signatures(void);
  20. static int dirvote_perform_vote(void);
  21. static void dirvote_clear_votes(int all_votes);
  22. static int dirvote_compute_consensus(void);
  23. static int dirvote_publish_consensus(void);
  24. /* =====
  25. * Voting
  26. * =====*/
  27. /** Return a new string containing the string representation of the vote in
  28. * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
  29. * For v3 authorities. */
  30. char *
  31. format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
  32. networkstatus_t *v3_ns)
  33. {
  34. size_t len;
  35. char *status = NULL;
  36. const char *client_versions = NULL, *server_versions = NULL;
  37. char *outp, *endp;
  38. char fingerprint[FINGERPRINT_LEN+1];
  39. char ipaddr[INET_NTOA_BUF_LEN];
  40. char digest[DIGEST_LEN];
  41. struct in_addr in;
  42. uint32_t addr;
  43. routerlist_t *rl = router_get_routerlist();
  44. char *version_lines = NULL;
  45. networkstatus_voter_info_t *voter;
  46. tor_assert(private_signing_key);
  47. tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
  48. voter = smartlist_get(v3_ns->voters, 0);
  49. addr = voter->addr;
  50. in.s_addr = htonl(addr);
  51. tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
  52. base16_encode(fingerprint, sizeof(fingerprint),
  53. v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
  54. client_versions = v3_ns->client_versions;
  55. server_versions = v3_ns->server_versions;
  56. if (client_versions || server_versions) {
  57. size_t v_len = 64;
  58. char *cp;
  59. if (client_versions)
  60. v_len += strlen(client_versions);
  61. if (server_versions)
  62. v_len += strlen(server_versions);
  63. version_lines = tor_malloc(v_len);
  64. cp = version_lines;
  65. if (client_versions) {
  66. tor_snprintf(cp, v_len-(cp-version_lines),
  67. "client-versions %s\n", client_versions);
  68. cp += strlen(cp);
  69. }
  70. if (server_versions)
  71. tor_snprintf(cp, v_len-(cp-version_lines),
  72. "server-versions %s\n", server_versions);
  73. } else {
  74. version_lines = tor_strdup("");
  75. }
  76. len = 8192;
  77. len += strlen(version_lines);
  78. len += (RS_ENTRY_LEN)*smartlist_len(rl->routers);
  79. len += v3_ns->cert->cache_info.signed_descriptor_len;
  80. status = tor_malloc(len);
  81. {
  82. char published[ISO_TIME_LEN+1];
  83. char va[ISO_TIME_LEN+1];
  84. char fu[ISO_TIME_LEN+1];
  85. char vu[ISO_TIME_LEN+1];
  86. char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
  87. authority_cert_t *cert = v3_ns->cert;
  88. format_iso_time(published, v3_ns->published);
  89. format_iso_time(va, v3_ns->valid_after);
  90. format_iso_time(fu, v3_ns->fresh_until);
  91. format_iso_time(vu, v3_ns->valid_until);
  92. tor_assert(cert);
  93. tor_snprintf(status, len,
  94. "network-status-version 3\n"
  95. "vote-status %s\n"
  96. "consensus-methods 1 2 3 4 5\n"
  97. "published %s\n"
  98. "valid-after %s\n"
  99. "fresh-until %s\n"
  100. "valid-until %s\n"
  101. "voting-delay %d %d\n"
  102. "%s" /* versions */
  103. "known-flags %s\n"
  104. "dir-source %s %s %s %s %d %d\n"
  105. "contact %s\n",
  106. v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
  107. published, va, fu, vu,
  108. v3_ns->vote_seconds, v3_ns->dist_seconds,
  109. version_lines,
  110. flags,
  111. voter->nickname, fingerprint, voter->address,
  112. ipaddr, voter->dir_port, voter->or_port, voter->contact);
  113. tor_free(flags);
  114. outp = status + strlen(status);
  115. endp = status + len;
  116. if (!tor_digest_is_zero(voter->legacy_id_digest)) {
  117. char fpbuf[HEX_DIGEST_LEN+1];
  118. base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
  119. tor_snprintf(outp, endp-outp, "legacy-dir-key %s\n", fpbuf);
  120. outp += strlen(outp);
  121. }
  122. tor_assert(outp + cert->cache_info.signed_descriptor_len < endp);
  123. memcpy(outp, cert->cache_info.signed_descriptor_body,
  124. cert->cache_info.signed_descriptor_len);
  125. outp += cert->cache_info.signed_descriptor_len;
  126. }
  127. SMARTLIST_FOREACH(v3_ns->routerstatus_list, vote_routerstatus_t *, vrs,
  128. {
  129. if (routerstatus_format_entry(outp, endp-outp, &vrs->status,
  130. vrs->version, 0, 0) < 0) {
  131. log_warn(LD_BUG, "Unable to print router status.");
  132. goto err;
  133. }
  134. outp += strlen(outp);
  135. });
  136. {
  137. char signing_key_fingerprint[FINGERPRINT_LEN+1];
  138. if (tor_snprintf(outp, endp-outp, "directory-signature ")<0) {
  139. log_warn(LD_BUG, "Unable to start signature line.");
  140. goto err;
  141. }
  142. outp += strlen(outp);
  143. if (crypto_pk_get_fingerprint(private_signing_key,
  144. signing_key_fingerprint, 0)<0) {
  145. log_warn(LD_BUG, "Unable to get fingerprint for signing key");
  146. goto err;
  147. }
  148. if (tor_snprintf(outp, endp-outp, "%s %s\n", fingerprint,
  149. signing_key_fingerprint)<0) {
  150. log_warn(LD_BUG, "Unable to end signature line.");
  151. goto err;
  152. }
  153. outp += strlen(outp);
  154. }
  155. if (router_get_networkstatus_v3_hash(status, digest)<0)
  156. goto err;
  157. note_crypto_pk_op(SIGN_DIR);
  158. if (router_append_dirobj_signature(outp,endp-outp,digest,
  159. private_signing_key)<0) {
  160. log_warn(LD_BUG, "Unable to sign networkstatus vote.");
  161. goto err;
  162. }
  163. {
  164. networkstatus_t *v;
  165. if (!(v = networkstatus_parse_vote_from_string(status, NULL,
  166. v3_ns->type))) {
  167. log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
  168. "<<%s>>",
  169. v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
  170. goto err;
  171. }
  172. networkstatus_vote_free(v);
  173. }
  174. goto done;
  175. err:
  176. tor_free(status);
  177. done:
  178. tor_free(version_lines);
  179. return status;
  180. }
  181. /* =====
  182. * Consensus generation
  183. * ===== */
  184. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  185. * networkstatus_voter_info_t. */
  186. static networkstatus_voter_info_t *
  187. get_voter(const networkstatus_t *vote)
  188. {
  189. tor_assert(vote);
  190. tor_assert(vote->type == NS_TYPE_VOTE);
  191. tor_assert(vote->voters);
  192. tor_assert(smartlist_len(vote->voters) == 1);
  193. return smartlist_get(vote->voters, 0);
  194. }
  195. typedef struct {
  196. networkstatus_t *v;
  197. const char *digest;
  198. int is_legacy;
  199. } dir_src_ent_t;
  200. /** Helper for sorting networkstatus_t votes (not consensuses) by the
  201. * hash of their voters' identity digests. */
  202. static int
  203. _compare_votes_by_authority_id(const void **_a, const void **_b)
  204. {
  205. const networkstatus_t *a = *_a, *b = *_b;
  206. return memcmp(get_voter(a)->identity_digest,
  207. get_voter(b)->identity_digest, DIGEST_LEN);
  208. }
  209. static int
  210. _compare_dir_src_ents_by_authority_id(const void **_a, const void **_b)
  211. {
  212. const dir_src_ent_t *a = *_a, *b = *_b;
  213. const networkstatus_voter_info_t *a_v = get_voter(a->v),
  214. *b_v = get_voter(b->v);
  215. const char *a_id, *b_id;
  216. a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
  217. b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
  218. return memcmp(a_id, b_id, DIGEST_LEN);
  219. }
  220. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  221. * that occurs more than <b>min</b> times. */
  222. static void
  223. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  224. {
  225. char *cur = NULL;
  226. int count = 0;
  227. SMARTLIST_FOREACH(in, char *, cp,
  228. {
  229. if (cur && !strcmp(cp, cur)) {
  230. ++count;
  231. } else {
  232. if (count > min)
  233. smartlist_add(out, cur);
  234. cur = cp;
  235. count = 1;
  236. }
  237. });
  238. if (count > min)
  239. smartlist_add(out, cur);
  240. }
  241. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  242. * most. Break ties in favor of later-occurring members. */
  243. static const char *
  244. get_most_frequent_member(smartlist_t *lst)
  245. {
  246. const char *most_frequent = NULL;
  247. int most_frequent_count = 0;
  248. const char *cur = NULL;
  249. int count = 0;
  250. SMARTLIST_FOREACH(lst, const char *, s,
  251. {
  252. if (cur && !strcmp(s, cur)) {
  253. ++count;
  254. } else {
  255. if (count >= most_frequent_count) {
  256. most_frequent = cur;
  257. most_frequent_count = count;
  258. }
  259. cur = s;
  260. count = 1;
  261. }
  262. });
  263. if (count >= most_frequent_count) {
  264. most_frequent = cur;
  265. most_frequent_count = count;
  266. }
  267. return most_frequent;
  268. }
  269. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  270. * that come from the same routerinfo, with the same derived elements.
  271. */
  272. static int
  273. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  274. {
  275. int r;
  276. if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
  277. DIGEST_LEN)))
  278. return r;
  279. if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
  280. DIGEST_LEN)))
  281. return r;
  282. if ((r = (int)(b->status.published_on - a->status.published_on)))
  283. return r;
  284. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  285. return r;
  286. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  287. return r;
  288. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  289. return r;
  290. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  291. return r;
  292. return 0;
  293. }
  294. /** Helper for sorting routerlists based on compare_vote_rs. */
  295. static int
  296. _compare_vote_rs(const void **_a, const void **_b)
  297. {
  298. const vote_routerstatus_t *a = *_a, *b = *_b;
  299. return compare_vote_rs(a,b);
  300. }
  301. /** Given a list of vote_routerstatus_t, all for the same router identity,
  302. * return whichever is most frequent, breaking ties in favor of more
  303. * recently published vote_routerstatus_t and in case of ties there,
  304. * in favour of smaller descriptor digest.
  305. */
  306. static vote_routerstatus_t *
  307. compute_routerstatus_consensus(smartlist_t *votes)
  308. {
  309. vote_routerstatus_t *most = NULL, *cur = NULL;
  310. int most_n = 0, cur_n = 0;
  311. time_t most_published = 0;
  312. /* _compare_vote_rs() sorts the items by identity digest (all the same),
  313. * then by SD digest. That way, if we have a tie that the published_on
  314. * date cannot tie, we use the descriptor with the smaller digest.
  315. */
  316. smartlist_sort(votes, _compare_vote_rs);
  317. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  318. {
  319. if (cur && !compare_vote_rs(cur, rs)) {
  320. ++cur_n;
  321. } else {
  322. if (cur_n > most_n ||
  323. (cur && cur_n == most_n &&
  324. cur->status.published_on > most_published)) {
  325. most = cur;
  326. most_n = cur_n;
  327. most_published = cur->status.published_on;
  328. }
  329. cur_n = 1;
  330. cur = rs;
  331. }
  332. });
  333. if (cur_n > most_n ||
  334. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  335. most = cur;
  336. most_n = cur_n;
  337. most_published = cur->status.published_on;
  338. }
  339. tor_assert(most);
  340. return most;
  341. }
  342. /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
  343. * <b>digest_out</b> to the hash of the concatenation of those strings. */
  344. static void
  345. hash_list_members(char *digest_out, smartlist_t *lst)
  346. {
  347. crypto_digest_env_t *d = crypto_new_digest_env();
  348. SMARTLIST_FOREACH(lst, const char *, cp,
  349. crypto_digest_add_bytes(d, cp, strlen(cp)));
  350. crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
  351. crypto_free_digest_env(d);
  352. }
  353. /** Sorting helper: compare two strings based on their values as base-ten
  354. * positive integers. (Non-integers are treated as prior to all integers, and
  355. * compared lexically.) */
  356. static int
  357. _cmp_int_strings(const void **_a, const void **_b)
  358. {
  359. const char *a = *_a, *b = *_b;
  360. int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
  361. int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
  362. if (ai<bi) {
  363. return -1;
  364. } else if (ai==bi) {
  365. if (ai == 0) /* Parsing failed. */
  366. return strcmp(a, b);
  367. return 0;
  368. } else {
  369. return 1;
  370. }
  371. }
  372. /** Given a list of networkstatus_t votes, determine and return the number of
  373. * the highest consensus method that is supported by 2/3 of the voters. */
  374. static int
  375. compute_consensus_method(smartlist_t *votes)
  376. {
  377. smartlist_t *all_methods = smartlist_create();
  378. smartlist_t *acceptable_methods = smartlist_create();
  379. smartlist_t *tmp = smartlist_create();
  380. int min = (smartlist_len(votes) * 2) / 3;
  381. int n_ok;
  382. int result;
  383. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  384. {
  385. tor_assert(vote->supported_methods);
  386. smartlist_add_all(tmp, vote->supported_methods);
  387. smartlist_sort(tmp, _cmp_int_strings);
  388. smartlist_uniq(tmp, _cmp_int_strings, NULL);
  389. smartlist_add_all(all_methods, tmp);
  390. smartlist_clear(tmp);
  391. });
  392. smartlist_sort(all_methods, _cmp_int_strings);
  393. get_frequent_members(acceptable_methods, all_methods, min);
  394. n_ok = smartlist_len(acceptable_methods);
  395. if (n_ok) {
  396. const char *best = smartlist_get(acceptable_methods, n_ok-1);
  397. result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
  398. } else {
  399. result = 1;
  400. }
  401. smartlist_free(tmp);
  402. smartlist_free(all_methods);
  403. smartlist_free(acceptable_methods);
  404. return result;
  405. }
  406. /** Return true iff <b>method</b> is a consensus method that we support. */
  407. static int
  408. consensus_method_is_supported(int method)
  409. {
  410. return (method >= 1) && (method <= 5);
  411. }
  412. /** Helper: given <b>lst</b>, a list of version strings such that every
  413. * version appears once for every versioning voter who recommends it, returna
  414. * newly allocated string holding the resulting client-versions or
  415. * server-versions list. May change contents of <b>lst</b> */
  416. static char *
  417. compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
  418. {
  419. int min = n_versioning / 2;
  420. smartlist_t *good = smartlist_create();
  421. char *result;
  422. sort_version_list(lst, 0);
  423. get_frequent_members(good, lst, min);
  424. result = smartlist_join_strings(good, ",", 0, NULL);
  425. smartlist_free(good);
  426. return result;
  427. }
  428. /** Given a list of vote networkstatus_t in <b>votes</b>, our public
  429. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  430. * and the number of <b>total_authorities</b> that we believe exist in our
  431. * voting quorum, generate the text of a new v3 consensus vote, and return the
  432. * value in a newly allocated string.
  433. *
  434. * Note: this function DOES NOT check whether the votes are from
  435. * recognized authorities. (dirvote_add_vote does that.) */
  436. char *
  437. networkstatus_compute_consensus(smartlist_t *votes,
  438. int total_authorities,
  439. crypto_pk_env_t *identity_key,
  440. crypto_pk_env_t *signing_key,
  441. const char *legacy_id_key_digest,
  442. crypto_pk_env_t *legacy_signing_key)
  443. {
  444. smartlist_t *chunks;
  445. char *result = NULL;
  446. int consensus_method;
  447. time_t valid_after, fresh_until, valid_until;
  448. int vote_seconds, dist_seconds;
  449. char *client_versions = NULL, *server_versions = NULL;
  450. smartlist_t *flags;
  451. tor_assert(total_authorities >= smartlist_len(votes));
  452. if (!smartlist_len(votes)) {
  453. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  454. return NULL;
  455. }
  456. flags = smartlist_create();
  457. consensus_method = compute_consensus_method(votes);
  458. if (consensus_method_is_supported(consensus_method)) {
  459. log_info(LD_DIR, "Generating consensus using method %d.",
  460. consensus_method);
  461. } else {
  462. log_warn(LD_DIR, "The other authorities will use consensus method %d, "
  463. "which I don't support. Maybe I should upgrade!",
  464. consensus_method);
  465. consensus_method = 1;
  466. }
  467. /* Compute medians of time-related things, and figure out how many
  468. * routers we might need to talk about. */
  469. {
  470. int n_votes = smartlist_len(votes);
  471. time_t *va_times = tor_malloc(n_votes * sizeof(time_t));
  472. time_t *fu_times = tor_malloc(n_votes * sizeof(time_t));
  473. time_t *vu_times = tor_malloc(n_votes * sizeof(time_t));
  474. int *votesec_list = tor_malloc(n_votes * sizeof(int));
  475. int *distsec_list = tor_malloc(n_votes * sizeof(int));
  476. int n_versioning_clients = 0, n_versioning_servers = 0;
  477. smartlist_t *combined_client_versions = smartlist_create();
  478. smartlist_t *combined_server_versions = smartlist_create();
  479. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  480. tor_assert(v->type == NS_TYPE_VOTE);
  481. va_times[v_sl_idx] = v->valid_after;
  482. fu_times[v_sl_idx] = v->fresh_until;
  483. vu_times[v_sl_idx] = v->valid_until;
  484. votesec_list[v_sl_idx] = v->vote_seconds;
  485. distsec_list[v_sl_idx] = v->dist_seconds;
  486. if (v->client_versions) {
  487. smartlist_t *cv = smartlist_create();
  488. ++n_versioning_clients;
  489. smartlist_split_string(cv, v->client_versions, ",",
  490. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  491. sort_version_list(cv, 1);
  492. smartlist_add_all(combined_client_versions, cv);
  493. smartlist_free(cv); /* elements get freed later. */
  494. }
  495. if (v->server_versions) {
  496. smartlist_t *sv = smartlist_create();
  497. ++n_versioning_servers;
  498. smartlist_split_string(sv, v->server_versions, ",",
  499. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  500. sort_version_list(sv, 1);
  501. smartlist_add_all(combined_server_versions, sv);
  502. smartlist_free(sv); /* elements get freed later. */
  503. }
  504. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  505. smartlist_add(flags, tor_strdup(cp)));
  506. } SMARTLIST_FOREACH_END(v);
  507. valid_after = median_time(va_times, n_votes);
  508. fresh_until = median_time(fu_times, n_votes);
  509. valid_until = median_time(vu_times, n_votes);
  510. vote_seconds = median_int(votesec_list, n_votes);
  511. dist_seconds = median_int(distsec_list, n_votes);
  512. tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
  513. tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
  514. tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
  515. tor_assert(dist_seconds >= MIN_DIST_SECONDS);
  516. server_versions = compute_consensus_versions_list(combined_server_versions,
  517. n_versioning_servers);
  518. client_versions = compute_consensus_versions_list(combined_client_versions,
  519. n_versioning_clients);
  520. SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
  521. SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
  522. smartlist_free(combined_server_versions);
  523. smartlist_free(combined_client_versions);
  524. smartlist_sort_strings(flags);
  525. smartlist_uniq_strings(flags);
  526. tor_free(va_times);
  527. tor_free(fu_times);
  528. tor_free(vu_times);
  529. tor_free(votesec_list);
  530. tor_free(distsec_list);
  531. }
  532. chunks = smartlist_create();
  533. {
  534. char buf[1024];
  535. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  536. vu_buf[ISO_TIME_LEN+1];
  537. char *flaglist;
  538. format_iso_time(va_buf, valid_after);
  539. format_iso_time(fu_buf, fresh_until);
  540. format_iso_time(vu_buf, valid_until);
  541. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  542. smartlist_add(chunks, tor_strdup("network-status-version 3\n"
  543. "vote-status consensus\n"));
  544. if (consensus_method >= 2) {
  545. tor_snprintf(buf, sizeof(buf), "consensus-method %d\n",
  546. consensus_method);
  547. smartlist_add(chunks, tor_strdup(buf));
  548. }
  549. tor_snprintf(buf, sizeof(buf),
  550. "valid-after %s\n"
  551. "fresh-until %s\n"
  552. "valid-until %s\n"
  553. "voting-delay %d %d\n"
  554. "client-versions %s\n"
  555. "server-versions %s\n"
  556. "known-flags %s\n",
  557. va_buf, fu_buf, vu_buf,
  558. vote_seconds, dist_seconds,
  559. client_versions, server_versions, flaglist);
  560. smartlist_add(chunks, tor_strdup(buf));
  561. tor_free(flaglist);
  562. }
  563. /* Sort the votes. */
  564. smartlist_sort(votes, _compare_votes_by_authority_id);
  565. /* Add the authority sections. */
  566. {
  567. smartlist_t *dir_sources = smartlist_create();
  568. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  569. {
  570. dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
  571. e->v = v;
  572. e->digest = get_voter(v)->identity_digest;
  573. e->is_legacy = 0;
  574. smartlist_add(dir_sources, e);
  575. if (consensus_method >= 3 &&
  576. !tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
  577. dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
  578. e_legacy->v = v;
  579. e_legacy->digest = get_voter(v)->legacy_id_digest;
  580. e_legacy->is_legacy = 1;
  581. smartlist_add(dir_sources, e_legacy);
  582. }
  583. });
  584. smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
  585. SMARTLIST_FOREACH(dir_sources, const dir_src_ent_t *, e,
  586. {
  587. char buf[1024];
  588. struct in_addr in;
  589. char ip[INET_NTOA_BUF_LEN];
  590. char fingerprint[HEX_DIGEST_LEN+1];
  591. char votedigest[HEX_DIGEST_LEN+1];
  592. networkstatus_t *v = e->v;
  593. networkstatus_voter_info_t *voter = get_voter(v);
  594. if (e->is_legacy)
  595. tor_assert(consensus_method >= 2);
  596. in.s_addr = htonl(voter->addr);
  597. tor_inet_ntoa(&in, ip, sizeof(ip));
  598. base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
  599. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  600. DIGEST_LEN);
  601. tor_snprintf(buf, sizeof(buf),
  602. "dir-source %s%s %s %s %s %d %d\n",
  603. voter->nickname, e->is_legacy ? "-legacy" : "",
  604. fingerprint, voter->address, ip,
  605. voter->dir_port,
  606. voter->or_port);
  607. smartlist_add(chunks, tor_strdup(buf));
  608. if (! e->is_legacy) {
  609. tor_snprintf(buf, sizeof(buf),
  610. "contact %s\n"
  611. "vote-digest %s\n",
  612. voter->contact,
  613. votedigest);
  614. smartlist_add(chunks, tor_strdup(buf));
  615. }
  616. });
  617. SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
  618. smartlist_free(dir_sources);
  619. }
  620. /* Add the actual router entries. */
  621. {
  622. int *index; /* index[j] is the current index into votes[j]. */
  623. int *size; /* size[j] is the number of routerstatuses in votes[j]. */
  624. int *flag_counts; /* The number of voters that list flag[j] for the
  625. * currently considered router. */
  626. int i;
  627. smartlist_t *matching_descs = smartlist_create();
  628. smartlist_t *chosen_flags = smartlist_create();
  629. smartlist_t *versions = smartlist_create();
  630. smartlist_t *exitsummaries = smartlist_create();
  631. uint32_t *bandwidths = tor_malloc(sizeof(uint32_t) * smartlist_len(votes));
  632. int num_bandwidths;
  633. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  634. * votes[j] knows about. */
  635. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  636. * about flags[f]. */
  637. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  638. * is the same flag as votes[j]->known_flags[b]. */
  639. int *named_flag; /* Index of the flag "Named" for votes[j] */
  640. int *unnamed_flag; /* Index of the flag "Unnamed" for votes[j] */
  641. int chosen_named_idx, chosen_unnamed_idx;
  642. strmap_t *name_to_id_map = strmap_new();
  643. char conflict[DIGEST_LEN];
  644. char unknown[DIGEST_LEN];
  645. memset(conflict, 0, sizeof(conflict));
  646. memset(unknown, 0xff, sizeof(conflict));
  647. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  648. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  649. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  650. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  651. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  652. named_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  653. unnamed_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  654. for (i = 0; i < smartlist_len(votes); ++i)
  655. unnamed_flag[i] = named_flag[i] = -1;
  656. chosen_named_idx = smartlist_string_pos(flags, "Named");
  657. chosen_unnamed_idx = smartlist_string_pos(flags, "Unnamed");
  658. /* Build the flag index. */
  659. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  660. {
  661. flag_map[v_sl_idx] = tor_malloc_zero(
  662. sizeof(int)*smartlist_len(v->known_flags));
  663. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  664. {
  665. int p = smartlist_string_pos(flags, fl);
  666. tor_assert(p >= 0);
  667. flag_map[v_sl_idx][fl_sl_idx] = p;
  668. ++n_flag_voters[p];
  669. if (!strcmp(fl, "Named"))
  670. named_flag[v_sl_idx] = fl_sl_idx;
  671. if (!strcmp(fl, "Unnamed"))
  672. unnamed_flag[v_sl_idx] = fl_sl_idx;
  673. });
  674. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  675. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  676. });
  677. /* Named and Unnamed get treated specially */
  678. if (consensus_method >= 2) {
  679. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  680. {
  681. uint64_t nf;
  682. if (named_flag[v_sl_idx]<0)
  683. continue;
  684. nf = U64_LITERAL(1) << named_flag[v_sl_idx];
  685. SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
  686. {
  687. if ((rs->flags & nf) != 0) {
  688. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  689. if (!d) {
  690. /* We have no name officially mapped to this digest. */
  691. strmap_set_lc(name_to_id_map, rs->status.nickname,
  692. rs->status.identity_digest);
  693. } else if (d != conflict &&
  694. memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
  695. /* Authorities disagree about this nickname. */
  696. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  697. } else {
  698. /* It's already a conflict, or it's already this ID. */
  699. }
  700. }
  701. });
  702. });
  703. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  704. {
  705. uint64_t uf;
  706. if (unnamed_flag[v_sl_idx]<0)
  707. continue;
  708. uf = U64_LITERAL(1) << unnamed_flag[v_sl_idx];
  709. SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
  710. {
  711. if ((rs->flags & uf) != 0) {
  712. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  713. if (d == conflict || d == unknown) {
  714. /* Leave it alone; we know what it is. */
  715. } else if (!d) {
  716. /* We have no name officially mapped to this digest. */
  717. strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
  718. } else if (!memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
  719. /* Authorities disagree about this nickname. */
  720. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  721. } else {
  722. /* It's mapped to a different name. */
  723. }
  724. }
  725. });
  726. });
  727. }
  728. /* Now go through all the votes */
  729. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  730. while (1) {
  731. vote_routerstatus_t *rs;
  732. routerstatus_t rs_out;
  733. const char *lowest_id = NULL;
  734. const char *chosen_version;
  735. const char *chosen_name = NULL;
  736. int exitsummary_disagreement = 0;
  737. int is_named = 0, is_unnamed = 0, is_running = 0;
  738. int naming_conflict = 0;
  739. int n_listing = 0;
  740. int i;
  741. char buf[256];
  742. /* Of the next-to-be-considered digest in each voter, which is first? */
  743. SMARTLIST_FOREACH(votes, networkstatus_t *, v, {
  744. if (index[v_sl_idx] < size[v_sl_idx]) {
  745. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  746. if (!lowest_id ||
  747. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  748. lowest_id = rs->status.identity_digest;
  749. }
  750. });
  751. if (!lowest_id) /* we're out of routers. */
  752. break;
  753. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  754. smartlist_clear(matching_descs);
  755. smartlist_clear(chosen_flags);
  756. smartlist_clear(versions);
  757. num_bandwidths = 0;
  758. /* Okay, go through all the entries for this digest. */
  759. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  760. if (index[v_sl_idx] >= size[v_sl_idx])
  761. continue; /* out of entries. */
  762. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  763. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  764. continue; /* doesn't include this router. */
  765. /* At this point, we know that we're looking at a routersatus with
  766. * identity "lowest".
  767. */
  768. ++index[v_sl_idx];
  769. ++n_listing;
  770. smartlist_add(matching_descs, rs);
  771. if (rs->version && rs->version[0])
  772. smartlist_add(versions, rs->version);
  773. /* Tally up all the flags. */
  774. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  775. if (rs->flags & (U64_LITERAL(1) << i))
  776. ++flag_counts[flag_map[v_sl_idx][i]];
  777. }
  778. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  779. if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
  780. log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
  781. chosen_name, rs->status.nickname);
  782. naming_conflict = 1;
  783. }
  784. chosen_name = rs->status.nickname;
  785. }
  786. /* count bandwidths */
  787. if (rs->status.has_bandwidth)
  788. bandwidths[num_bandwidths++] = rs->status.bandwidth;
  789. } SMARTLIST_FOREACH_END(v);
  790. /* We don't include this router at all unless more than half of
  791. * the authorities we believe in list it. */
  792. if (n_listing <= total_authorities/2)
  793. continue;
  794. /* Figure out the most popular opinion of what the most recent
  795. * routerinfo and its contents are. */
  796. rs = compute_routerstatus_consensus(matching_descs);
  797. /* Copy bits of that into rs_out. */
  798. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  799. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  800. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  801. DIGEST_LEN);
  802. rs_out.addr = rs->status.addr;
  803. rs_out.published_on = rs->status.published_on;
  804. rs_out.dir_port = rs->status.dir_port;
  805. rs_out.or_port = rs->status.or_port;
  806. rs_out.has_bandwidth = 0;
  807. rs_out.has_exitsummary = 0;
  808. if (chosen_name && !naming_conflict) {
  809. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  810. } else {
  811. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  812. }
  813. if (consensus_method == 1) {
  814. is_named = chosen_named_idx >= 0 &&
  815. (!naming_conflict && flag_counts[chosen_named_idx]);
  816. } else {
  817. const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
  818. if (!d) {
  819. is_named = is_unnamed = 0;
  820. } else if (!memcmp(d, lowest_id, DIGEST_LEN)) {
  821. is_named = 1; is_unnamed = 0;
  822. } else {
  823. is_named = 0; is_unnamed = 1;
  824. }
  825. }
  826. /* Set the flags. */
  827. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  828. SMARTLIST_FOREACH(flags, const char *, fl,
  829. {
  830. if (!strcmp(fl, "Named")) {
  831. if (is_named)
  832. smartlist_add(chosen_flags, (char*)fl);
  833. } else if (!strcmp(fl, "Unnamed") && consensus_method >= 2) {
  834. if (is_unnamed)
  835. smartlist_add(chosen_flags, (char*)fl);
  836. } else {
  837. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
  838. smartlist_add(chosen_flags, (char*)fl);
  839. if (!strcmp(fl, "Running"))
  840. is_running = 1;
  841. }
  842. }
  843. });
  844. /* Starting with consensus method 4 we do not list servers
  845. * that are not running in a consensus. See Proposal 138 */
  846. if (consensus_method >= 4 && !is_running)
  847. continue;
  848. /* Pick the version. */
  849. if (smartlist_len(versions)) {
  850. sort_version_list(versions, 0);
  851. chosen_version = get_most_frequent_member(versions);
  852. } else {
  853. chosen_version = NULL;
  854. }
  855. /* Pick a bandwidth */
  856. if (consensus_method >= 5 && num_bandwidths > 0) {
  857. rs_out.has_bandwidth = 1;
  858. rs_out.bandwidth = median_uint32(bandwidths, num_bandwidths);
  859. }
  860. /* Ok, we already picked a descriptor digest we want to list
  861. * previously. Now we want to use the exit policy summary from
  862. * that descriptor. If everybody plays nice all the voters who
  863. * listed that descriptor will have the same summary. If not then
  864. * something is fishy and we'll use the most common one (breaking
  865. * ties in favor of lexigraphically larger one (only because it
  866. * lets me reuse more existing code.
  867. *
  868. * The other case that can happen is that no authority that voted
  869. * for that descriptor has an exit policy summary. That's
  870. * probably quite unlikely but can happen. In that case we use
  871. * the policy that was most often listed in votes, again breaking
  872. * ties like in the previous case.
  873. */
  874. if (consensus_method >= 5) {
  875. /* Okay, go through all the votes for this router. We prepared
  876. * that list previously */
  877. const char *chosen_exitsummary = NULL;
  878. smartlist_clear(exitsummaries);
  879. SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
  880. /* Check if the vote where this status comes from had the
  881. * proper descriptor */
  882. tor_assert(!memcmp(rs_out.identity_digest,
  883. vsr->status.identity_digest,
  884. DIGEST_LEN));
  885. if (vsr->status.has_exitsummary &&
  886. !memcmp(rs_out.descriptor_digest,
  887. vsr->status.descriptor_digest,
  888. DIGEST_LEN)) {
  889. tor_assert(vsr->status.exitsummary);
  890. smartlist_add(exitsummaries, vsr->status.exitsummary);
  891. if (!chosen_exitsummary) {
  892. chosen_exitsummary = vsr->status.exitsummary;
  893. } else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
  894. /* Great. There's disagreement among the voters. That
  895. * really shouldn't be */
  896. exitsummary_disagreement = 1;
  897. }
  898. }
  899. });
  900. if (exitsummary_disagreement) {
  901. char id[HEX_DIGEST_LEN+1];
  902. char dd[HEX_DIGEST_LEN+1];
  903. base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
  904. base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
  905. log_warn(LD_DIR, "The voters disgreed on the exit policy summary for"
  906. " router %s with descriptor %s. This really shouldn't"
  907. " have happened.", id, dd);
  908. smartlist_sort_strings(exitsummaries);
  909. chosen_exitsummary = get_most_frequent_member(exitsummaries);
  910. } else if (!chosen_exitsummary) {
  911. char id[HEX_DIGEST_LEN+1];
  912. char dd[HEX_DIGEST_LEN+1];
  913. base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
  914. base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
  915. log_warn(LD_DIR, "Not one of the voters that made us select"
  916. "descriptor %s for router %s had an exit policy"
  917. "summary", dd, id);
  918. /* Ok, none of those voting for the digest we chose had an
  919. * exit policy for us. Well, that kinda sucks.
  920. */
  921. smartlist_clear(exitsummaries);
  922. SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
  923. if (vsr->status.has_exitsummary)
  924. smartlist_add(exitsummaries, vsr->status.exitsummary);
  925. });
  926. smartlist_sort_strings(exitsummaries);
  927. chosen_exitsummary = get_most_frequent_member(exitsummaries);
  928. if (!chosen_exitsummary)
  929. log_warn(LD_DIR, "Wow, not one of the voters had an exit "
  930. "policy summary for %s. Wow.", id);
  931. }
  932. if (chosen_exitsummary) {
  933. rs_out.has_exitsummary = 1;
  934. /* yea, discards the const */
  935. rs_out.exitsummary = (char *)chosen_exitsummary;
  936. }
  937. }
  938. /* Okay!! Now we can write the descriptor... */
  939. /* First line goes into "buf". */
  940. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1, 0);
  941. smartlist_add(chunks, tor_strdup(buf));
  942. /* Second line is all flags. The "\n" is missing. */
  943. smartlist_add(chunks,
  944. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  945. /* Now the version line. */
  946. if (chosen_version) {
  947. smartlist_add(chunks, tor_strdup("\nv "));
  948. smartlist_add(chunks, tor_strdup(chosen_version));
  949. }
  950. smartlist_add(chunks, tor_strdup("\n"));
  951. /* Now the weight line. */
  952. if (rs_out.has_bandwidth) {
  953. int r = tor_snprintf(buf, sizeof(buf),
  954. "w Bandwidth=%d\n", rs_out.bandwidth);
  955. if (r<0) {
  956. log_warn(LD_BUG, "Not enough space in buffer for weight line.");
  957. *buf = '\0';
  958. }
  959. smartlist_add(chunks, tor_strdup(buf));
  960. };
  961. /* Now the exitpolicy summary line. */
  962. if (rs_out.has_exitsummary) {
  963. char buf[MAX_POLICY_LINE_LEN+1];
  964. int r = tor_snprintf(buf, sizeof(buf), "p %s\n", rs_out.exitsummary);
  965. if (r<0) {
  966. log_warn(LD_BUG, "Not enough space in buffer for exitpolicy line.");
  967. *buf = '\0';
  968. }
  969. smartlist_add(chunks, tor_strdup(buf));
  970. };
  971. /* And the loop is over and we move on to the next router */
  972. }
  973. tor_free(index);
  974. tor_free(size);
  975. tor_free(n_voter_flags);
  976. tor_free(n_flag_voters);
  977. for (i = 0; i < smartlist_len(votes); ++i)
  978. tor_free(flag_map[i]);
  979. tor_free(flag_map);
  980. tor_free(flag_counts);
  981. tor_free(named_flag);
  982. tor_free(unnamed_flag);
  983. strmap_free(name_to_id_map, NULL);
  984. smartlist_free(matching_descs);
  985. smartlist_free(chosen_flags);
  986. smartlist_free(versions);
  987. smartlist_free(exitsummaries);
  988. tor_free(bandwidths);
  989. }
  990. /* Add a signature. */
  991. {
  992. char digest[DIGEST_LEN];
  993. char fingerprint[HEX_DIGEST_LEN+1];
  994. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  995. char buf[4096];
  996. smartlist_add(chunks, tor_strdup("directory-signature "));
  997. /* Compute the hash of the chunks. */
  998. hash_list_members(digest, chunks);
  999. /* Get the fingerprints */
  1000. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  1001. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  1002. /* add the junk that will go at the end of the line. */
  1003. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  1004. signing_key_fingerprint);
  1005. /* And the signature. */
  1006. if (router_append_dirobj_signature(buf, sizeof(buf), digest,
  1007. signing_key)) {
  1008. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  1009. return NULL; /* This leaks, but it should never happen. */
  1010. }
  1011. smartlist_add(chunks, tor_strdup(buf));
  1012. if (legacy_id_key_digest && legacy_signing_key && consensus_method >= 3) {
  1013. smartlist_add(chunks, tor_strdup("directory-signature "));
  1014. base16_encode(fingerprint, sizeof(fingerprint),
  1015. legacy_id_key_digest, DIGEST_LEN);
  1016. crypto_pk_get_fingerprint(legacy_signing_key,
  1017. signing_key_fingerprint, 0);
  1018. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  1019. signing_key_fingerprint);
  1020. if (router_append_dirobj_signature(buf, sizeof(buf), digest,
  1021. legacy_signing_key)) {
  1022. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  1023. return NULL; /* This leaks, but it should never happen. */
  1024. }
  1025. smartlist_add(chunks, tor_strdup(buf));
  1026. }
  1027. }
  1028. result = smartlist_join_strings(chunks, "", 0, NULL);
  1029. tor_free(client_versions);
  1030. tor_free(server_versions);
  1031. SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
  1032. smartlist_free(flags);
  1033. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  1034. smartlist_free(chunks);
  1035. {
  1036. networkstatus_t *c;
  1037. if (!(c = networkstatus_parse_vote_from_string(result, NULL,
  1038. NS_TYPE_CONSENSUS))) {
  1039. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  1040. "parse.");
  1041. tor_free(result);
  1042. return NULL;
  1043. }
  1044. networkstatus_vote_free(c);
  1045. }
  1046. return result;
  1047. }
  1048. /** Given a consensus vote <b>target</b> and a set of detached signatures in
  1049. * <b>sigs</b> that correspond to the same consensus, check whether there are
  1050. * any new signatures in <b>src_voter_list</b> that should be added to
  1051. * <b>target. (A signature should be added if we have no signature for that
  1052. * voter in <b>target</b> yet, or if we have no verifiable signature and the
  1053. * new signature is verifiable.) Return the number of signatures added or
  1054. * changed, or -1 if the document signed by <b>sigs</b> isn't the same
  1055. * document as <b>target</b>. */
  1056. int
  1057. networkstatus_add_detached_signatures(networkstatus_t *target,
  1058. ns_detached_signatures_t *sigs,
  1059. const char **msg_out)
  1060. {
  1061. int r = 0;
  1062. tor_assert(sigs);
  1063. tor_assert(target);
  1064. tor_assert(target->type == NS_TYPE_CONSENSUS);
  1065. /* Do the times seem right? */
  1066. if (target->valid_after != sigs->valid_after) {
  1067. *msg_out = "Valid-After times do not match "
  1068. "when adding detached signatures to consensus";
  1069. return -1;
  1070. }
  1071. if (target->fresh_until != sigs->fresh_until) {
  1072. *msg_out = "Fresh-until times do not match "
  1073. "when adding detached signatures to consensus";
  1074. return -1;
  1075. }
  1076. if (target->valid_until != sigs->valid_until) {
  1077. *msg_out = "Valid-until times do not match "
  1078. "when adding detached signatures to consensus";
  1079. return -1;
  1080. }
  1081. /* Are they the same consensus? */
  1082. if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
  1083. DIGEST_LEN)) {
  1084. *msg_out = "Digest mismatch when adding detached signatures to consensus";
  1085. return -1;
  1086. }
  1087. /* For each voter in src... */
  1088. SMARTLIST_FOREACH_BEGIN(sigs->signatures, networkstatus_voter_info_t *,
  1089. src_voter) {
  1090. char voter_identity[HEX_DIGEST_LEN+1];
  1091. networkstatus_voter_info_t *target_voter =
  1092. networkstatus_get_voter_by_id(target, src_voter->identity_digest);
  1093. authority_cert_t *cert = NULL;
  1094. base16_encode(voter_identity, sizeof(voter_identity),
  1095. src_voter->identity_digest, DIGEST_LEN);
  1096. log_info(LD_DIR, "Looking at signature from %s", voter_identity);
  1097. /* If the target doesn't know about this voter, then forget it. */
  1098. if (!target_voter) {
  1099. log_info(LD_DIR, "We do not know about %s", voter_identity);
  1100. continue;
  1101. }
  1102. /* If the target already has a good signature from this voter, then skip
  1103. * this one. */
  1104. if (target_voter->good_signature) {
  1105. log_info(LD_DIR, "We already have a good signature from %s",
  1106. voter_identity);
  1107. continue;
  1108. }
  1109. /* Try checking the signature if we haven't already. */
  1110. if (!src_voter->good_signature && !src_voter->bad_signature) {
  1111. cert = authority_cert_get_by_digests(src_voter->identity_digest,
  1112. src_voter->signing_key_digest);
  1113. if (cert) {
  1114. networkstatus_check_voter_signature(target, src_voter, cert);
  1115. }
  1116. }
  1117. /* If this signature is good, or we don't have any signature yet,
  1118. * then add it. */
  1119. if (src_voter->good_signature || !target_voter->signature) {
  1120. log_info(LD_DIR, "Adding signature from %s", voter_identity);
  1121. ++r;
  1122. tor_free(target_voter->signature);
  1123. target_voter->signature =
  1124. tor_memdup(src_voter->signature, src_voter->signature_len);
  1125. memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
  1126. DIGEST_LEN);
  1127. target_voter->signature_len = src_voter->signature_len;
  1128. target_voter->good_signature = src_voter->good_signature;
  1129. target_voter->bad_signature = src_voter->bad_signature;
  1130. } else {
  1131. log_info(LD_DIR, "Not adding signature from %s", voter_identity);
  1132. }
  1133. } SMARTLIST_FOREACH_END(src_voter);
  1134. return r;
  1135. }
  1136. /** Return a newly allocated string holding the detached-signatures document
  1137. * corresponding to the signatures on <b>consensus</b>. */
  1138. char *
  1139. networkstatus_get_detached_signatures(networkstatus_t *consensus)
  1140. {
  1141. smartlist_t *elements;
  1142. char buf[4096];
  1143. char *result = NULL;
  1144. int n_sigs = 0;
  1145. tor_assert(consensus);
  1146. tor_assert(consensus->type == NS_TYPE_CONSENSUS);
  1147. elements = smartlist_create();
  1148. {
  1149. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  1150. vu_buf[ISO_TIME_LEN+1];
  1151. char d[HEX_DIGEST_LEN+1];
  1152. base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
  1153. format_iso_time(va_buf, consensus->valid_after);
  1154. format_iso_time(fu_buf, consensus->fresh_until);
  1155. format_iso_time(vu_buf, consensus->valid_until);
  1156. tor_snprintf(buf, sizeof(buf),
  1157. "consensus-digest %s\n"
  1158. "valid-after %s\n"
  1159. "fresh-until %s\n"
  1160. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  1161. smartlist_add(elements, tor_strdup(buf));
  1162. }
  1163. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
  1164. {
  1165. char sk[HEX_DIGEST_LEN+1];
  1166. char id[HEX_DIGEST_LEN+1];
  1167. if (!v->signature || v->bad_signature)
  1168. continue;
  1169. ++n_sigs;
  1170. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  1171. base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
  1172. tor_snprintf(buf, sizeof(buf),
  1173. "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
  1174. id, sk);
  1175. smartlist_add(elements, tor_strdup(buf));
  1176. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  1177. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  1178. smartlist_add(elements, tor_strdup(buf));
  1179. });
  1180. result = smartlist_join_strings(elements, "", 0, NULL);
  1181. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1182. smartlist_free(elements);
  1183. if (!n_sigs)
  1184. tor_free(result);
  1185. return result;
  1186. }
  1187. /** Release all storage held in <b>s</b>. */
  1188. void
  1189. ns_detached_signatures_free(ns_detached_signatures_t *s)
  1190. {
  1191. if (s->signatures) {
  1192. SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
  1193. {
  1194. tor_free(v->signature);
  1195. tor_free(v);
  1196. });
  1197. smartlist_free(s->signatures);
  1198. }
  1199. tor_free(s);
  1200. }
  1201. /* =====
  1202. * Certificate functions
  1203. * ===== */
  1204. /** Allocate and return a new authority_cert_t with the same contents as
  1205. * <b>cert</b>. */
  1206. authority_cert_t *
  1207. authority_cert_dup(authority_cert_t *cert)
  1208. {
  1209. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  1210. tor_assert(cert);
  1211. memcpy(out, cert, sizeof(authority_cert_t));
  1212. /* Now copy pointed-to things. */
  1213. out->cache_info.signed_descriptor_body =
  1214. tor_strndup(cert->cache_info.signed_descriptor_body,
  1215. cert->cache_info.signed_descriptor_len);
  1216. out->cache_info.saved_location = SAVED_NOWHERE;
  1217. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  1218. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  1219. return out;
  1220. }
  1221. /* =====
  1222. * Vote scheduling
  1223. * ===== */
  1224. /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
  1225. * Note that these aren't the intervals we'll use to vote; they're the ones
  1226. * that we'll vote to use. */
  1227. void
  1228. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  1229. {
  1230. or_options_t *options = get_options();
  1231. tor_assert(timing_out);
  1232. timing_out->vote_interval = options->V3AuthVotingInterval;
  1233. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  1234. timing_out->vote_delay = options->V3AuthVoteDelay;
  1235. timing_out->dist_delay = options->V3AuthDistDelay;
  1236. }
  1237. /** Return the start of the next interval of size <b>interval</b> (in seconds)
  1238. * after <b>now</b>. Midnight always starts a fresh interval, and if the last
  1239. * interval of a day would be truncated to less than half its size, it is
  1240. * rolled into the previous interval. */
  1241. time_t
  1242. dirvote_get_start_of_next_interval(time_t now, int interval)
  1243. {
  1244. struct tm tm;
  1245. time_t midnight_today;
  1246. time_t midnight_tomorrow;
  1247. time_t next;
  1248. tor_gmtime_r(&now, &tm);
  1249. tm.tm_hour = 0;
  1250. tm.tm_min = 0;
  1251. tm.tm_sec = 0;
  1252. midnight_today = tor_timegm(&tm);
  1253. midnight_tomorrow = midnight_today + (24*60*60);
  1254. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  1255. /* Intervals never cross midnight. */
  1256. if (next > midnight_tomorrow)
  1257. next = midnight_tomorrow;
  1258. /* If the interval would only last half as long as it's supposed to, then
  1259. * skip over to the next day. */
  1260. if (next + interval/2 > midnight_tomorrow)
  1261. next = midnight_tomorrow;
  1262. return next;
  1263. }
  1264. /** Scheduling information for a voting interval. */
  1265. static struct {
  1266. /** When do we generate and distribute our vote for this interval? */
  1267. time_t voting_starts;
  1268. /** When do we send an HTTP request for any votes that we haven't
  1269. * been posted yet?*/
  1270. time_t fetch_missing_votes;
  1271. /** When do we give up on getting more votes and generate a consensus? */
  1272. time_t voting_ends;
  1273. /** When do we send an HTTP request for any signatures we're expecting to
  1274. * see on the consensus? */
  1275. time_t fetch_missing_signatures;
  1276. /** When do we publish the consensus? */
  1277. time_t interval_starts;
  1278. /* True iff we have generated and distributed our vote. */
  1279. int have_voted;
  1280. /* True iff we've requested missing votes. */
  1281. int have_fetched_missing_votes;
  1282. /* True iff we have built a consensus and sent the signatures around. */
  1283. int have_built_consensus;
  1284. /* True iff we've fetched missing signatures. */
  1285. int have_fetched_missing_signatures;
  1286. /* True iff we have published our consensus. */
  1287. int have_published_consensus;
  1288. } voting_schedule = {0,0,0,0,0,0,0,0,0,0};
  1289. /** Set voting_schedule to hold the timing for the next vote we should be
  1290. * doing. */
  1291. void
  1292. dirvote_recalculate_timing(or_options_t *options, time_t now)
  1293. {
  1294. int interval, vote_delay, dist_delay;
  1295. time_t start;
  1296. time_t end;
  1297. networkstatus_t *consensus;
  1298. if (!authdir_mode_v3(options))
  1299. return;
  1300. consensus = networkstatus_get_live_consensus(now);
  1301. memset(&voting_schedule, 0, sizeof(voting_schedule));
  1302. if (consensus) {
  1303. interval = (int)( consensus->fresh_until - consensus->valid_after );
  1304. vote_delay = consensus->vote_seconds;
  1305. dist_delay = consensus->dist_seconds;
  1306. } else {
  1307. interval = options->TestingV3AuthInitialVotingInterval;
  1308. vote_delay = options->TestingV3AuthInitialVoteDelay;
  1309. dist_delay = options->TestingV3AuthInitialDistDelay;
  1310. }
  1311. tor_assert(interval > 0);
  1312. if (vote_delay + dist_delay > interval/2)
  1313. vote_delay = dist_delay = interval / 4;
  1314. start = voting_schedule.interval_starts =
  1315. dirvote_get_start_of_next_interval(now,interval);
  1316. end = dirvote_get_start_of_next_interval(start+1, interval);
  1317. tor_assert(end > start);
  1318. voting_schedule.fetch_missing_signatures = start - (dist_delay/2);
  1319. voting_schedule.voting_ends = start - dist_delay;
  1320. voting_schedule.fetch_missing_votes = start - dist_delay - (vote_delay/2);
  1321. voting_schedule.voting_starts = start - dist_delay - vote_delay;
  1322. {
  1323. char tbuf[ISO_TIME_LEN+1];
  1324. format_iso_time(tbuf, voting_schedule.interval_starts);
  1325. log_notice(LD_DIR,"Choosing expected valid-after time as %s: "
  1326. "consensus_set=%d, interval=%d",
  1327. tbuf, consensus?1:0, interval);
  1328. }
  1329. }
  1330. /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
  1331. void
  1332. dirvote_act(or_options_t *options, time_t now)
  1333. {
  1334. if (!authdir_mode_v3(options))
  1335. return;
  1336. if (!voting_schedule.voting_starts) {
  1337. char *keys = list_v3_auth_ids();
  1338. authority_cert_t *c = get_my_v3_authority_cert();
  1339. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
  1340. "Mine is %s.",
  1341. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  1342. tor_free(keys);
  1343. dirvote_recalculate_timing(options, now);
  1344. }
  1345. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  1346. log_notice(LD_DIR, "Time to vote.");
  1347. dirvote_perform_vote();
  1348. voting_schedule.have_voted = 1;
  1349. }
  1350. if (voting_schedule.fetch_missing_votes < now &&
  1351. !voting_schedule.have_fetched_missing_votes) {
  1352. log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
  1353. dirvote_fetch_missing_votes();
  1354. voting_schedule.have_fetched_missing_votes = 1;
  1355. }
  1356. if (voting_schedule.voting_ends < now &&
  1357. !voting_schedule.have_built_consensus) {
  1358. log_notice(LD_DIR, "Time to compute a consensus.");
  1359. dirvote_compute_consensus();
  1360. /* XXXX We will want to try again later if we haven't got enough
  1361. * votes yet. Implement this if it turns out to ever happen. */
  1362. voting_schedule.have_built_consensus = 1;
  1363. }
  1364. if (voting_schedule.fetch_missing_signatures < now &&
  1365. !voting_schedule.have_fetched_missing_signatures) {
  1366. log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
  1367. dirvote_fetch_missing_signatures();
  1368. voting_schedule.have_fetched_missing_signatures = 1;
  1369. }
  1370. if (voting_schedule.interval_starts < now &&
  1371. !voting_schedule.have_published_consensus) {
  1372. log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
  1373. dirvote_publish_consensus();
  1374. dirvote_clear_votes(0);
  1375. voting_schedule.have_published_consensus = 1;
  1376. /* XXXX We will want to try again later if we haven't got enough
  1377. * signatures yet. Implement this if it turns out to ever happen. */
  1378. dirvote_recalculate_timing(options, now);
  1379. }
  1380. }
  1381. /** A vote networkstatus_t and its unparsed body: held around so we can
  1382. * use it to generate a consensus (at voting_ends) and so we can serve it to
  1383. * other authorities that might want it. */
  1384. typedef struct pending_vote_t {
  1385. cached_dir_t *vote_body;
  1386. networkstatus_t *vote;
  1387. } pending_vote_t;
  1388. /** List of pending_vote_t for the current vote. Before we've used them to
  1389. * build a consensus, the votes go here. */
  1390. static smartlist_t *pending_vote_list = NULL;
  1391. /** List of pending_vote_t for the previous vote. After we've used them to
  1392. * build a consensus, the votes go here for the next period. */
  1393. static smartlist_t *previous_vote_list = NULL;
  1394. /** The body of the consensus that we're currently building. Once we
  1395. * have it built, it goes into dirserv.c */
  1396. static char *pending_consensus_body = NULL;
  1397. /** The detached signatures for the consensus that we're currently
  1398. * building. */
  1399. static char *pending_consensus_signatures = NULL;
  1400. /** The parsed in-progress consensus document. */
  1401. static networkstatus_t *pending_consensus = NULL;
  1402. /** List of ns_detached_signatures_t: hold signatures that get posted to us
  1403. * before we have generated the consensus on our own. */
  1404. static smartlist_t *pending_consensus_signature_list = NULL;
  1405. /** Generate a networkstatus vote and post it to all the v3 authorities.
  1406. * (V3 Authority only) */
  1407. static int
  1408. dirvote_perform_vote(void)
  1409. {
  1410. crypto_pk_env_t *key = get_my_v3_authority_signing_key();
  1411. authority_cert_t *cert = get_my_v3_authority_cert();
  1412. networkstatus_t *ns;
  1413. char *contents;
  1414. pending_vote_t *pending_vote;
  1415. time_t now = time(NULL);
  1416. int status;
  1417. const char *msg = "";
  1418. if (!cert || !key) {
  1419. log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
  1420. return -1;
  1421. } else if (cert->expires < now) {
  1422. log_warn(LD_NET, "Can't generate v3 vote with expired certificate");
  1423. return -1;
  1424. }
  1425. if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
  1426. return -1;
  1427. contents = format_networkstatus_vote(key, ns);
  1428. networkstatus_vote_free(ns);
  1429. if (!contents)
  1430. return -1;
  1431. pending_vote = dirvote_add_vote(contents, &msg, &status);
  1432. tor_free(contents);
  1433. if (!pending_vote) {
  1434. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1435. msg);
  1436. return -1;
  1437. }
  1438. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1439. ROUTER_PURPOSE_GENERAL,
  1440. V3_AUTHORITY,
  1441. pending_vote->vote_body->dir,
  1442. pending_vote->vote_body->dir_len, 0);
  1443. log_notice(LD_DIR, "Vote posted.");
  1444. return 0;
  1445. }
  1446. /** Send an HTTP request to every other v3 authority, for the votes of every
  1447. * authority for which we haven't received a vote yet in this period. (V3
  1448. * authority only) */
  1449. static void
  1450. dirvote_fetch_missing_votes(void)
  1451. {
  1452. smartlist_t *missing_fps = smartlist_create();
  1453. char *resource;
  1454. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1455. trusted_dir_server_t *, ds,
  1456. {
  1457. if (!(ds->type & V3_AUTHORITY))
  1458. continue;
  1459. if (!dirvote_get_vote(ds->v3_identity_digest,
  1460. DGV_BY_ID|DGV_INCLUDE_PENDING)) {
  1461. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  1462. base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
  1463. DIGEST_LEN);
  1464. smartlist_add(missing_fps, cp);
  1465. }
  1466. });
  1467. if (!smartlist_len(missing_fps)) {
  1468. smartlist_free(missing_fps);
  1469. return;
  1470. }
  1471. log_notice(LOG_NOTICE, "We're missing votes from %d authorities. Asking "
  1472. "every other authority for a copy.", smartlist_len(missing_fps));
  1473. resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
  1474. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
  1475. 0, resource);
  1476. tor_free(resource);
  1477. SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
  1478. smartlist_free(missing_fps);
  1479. }
  1480. /** Send a request to every other authority for its detached signatures,
  1481. * unless we have signatures from all other v3 authorities already. */
  1482. static void
  1483. dirvote_fetch_missing_signatures(void)
  1484. {
  1485. if (!pending_consensus)
  1486. return;
  1487. if (networkstatus_check_consensus_signature(pending_consensus, -1) == 1)
  1488. return; /* we have a signature from everybody. */
  1489. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  1490. 0, NULL);
  1491. }
  1492. /** Drop all currently pending votes, consensus, and detached signatures. */
  1493. static void
  1494. dirvote_clear_votes(int all_votes)
  1495. {
  1496. if (!previous_vote_list)
  1497. previous_vote_list = smartlist_create();
  1498. if (!pending_vote_list)
  1499. pending_vote_list = smartlist_create();
  1500. /* All "previous" votes are now junk. */
  1501. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
  1502. cached_dir_decref(v->vote_body);
  1503. v->vote_body = NULL;
  1504. networkstatus_vote_free(v->vote);
  1505. tor_free(v);
  1506. });
  1507. smartlist_clear(previous_vote_list);
  1508. if (all_votes) {
  1509. /* If we're dumping all the votes, we delete the pending ones. */
  1510. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1511. cached_dir_decref(v->vote_body);
  1512. v->vote_body = NULL;
  1513. networkstatus_vote_free(v->vote);
  1514. tor_free(v);
  1515. });
  1516. } else {
  1517. /* Otherwise, we move them into "previous". */
  1518. smartlist_add_all(previous_vote_list, pending_vote_list);
  1519. }
  1520. smartlist_clear(pending_vote_list);
  1521. if (pending_consensus_signature_list) {
  1522. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1523. tor_free(cp));
  1524. smartlist_clear(pending_consensus_signature_list);
  1525. }
  1526. tor_free(pending_consensus_body);
  1527. tor_free(pending_consensus_signatures);
  1528. if (pending_consensus) {
  1529. networkstatus_vote_free(pending_consensus);
  1530. pending_consensus = NULL;
  1531. }
  1532. }
  1533. /** Return a newly allocated string containing the hex-encoded v3 authority
  1534. identity digest of every recognized v3 authority. */
  1535. static char *
  1536. list_v3_auth_ids(void)
  1537. {
  1538. smartlist_t *known_v3_keys = smartlist_create();
  1539. char *keys;
  1540. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1541. trusted_dir_server_t *, ds,
  1542. if ((ds->type & V3_AUTHORITY) &&
  1543. !tor_digest_is_zero(ds->v3_identity_digest))
  1544. smartlist_add(known_v3_keys,
  1545. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  1546. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  1547. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  1548. smartlist_free(known_v3_keys);
  1549. return keys;
  1550. }
  1551. /** Called when we have received a networkstatus vote in <b>vote_body</b>.
  1552. * Parse and validate it, and on success store it as a pending vote (which we
  1553. * then return). Return NULL on failure. Sets *<b>msg_out</b> and
  1554. * *<b>status_out</b> to an HTTP response and status code. (V3 authority
  1555. * only) */
  1556. pending_vote_t *
  1557. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  1558. {
  1559. networkstatus_t *vote;
  1560. networkstatus_voter_info_t *vi;
  1561. trusted_dir_server_t *ds;
  1562. pending_vote_t *pending_vote = NULL;
  1563. const char *end_of_vote = NULL;
  1564. int any_failed = 0;
  1565. tor_assert(vote_body);
  1566. tor_assert(msg_out);
  1567. tor_assert(status_out);
  1568. if (!pending_vote_list)
  1569. pending_vote_list = smartlist_create();
  1570. *status_out = 0;
  1571. *msg_out = NULL;
  1572. again:
  1573. vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote,
  1574. NS_TYPE_VOTE);
  1575. if (!end_of_vote)
  1576. end_of_vote = vote_body + strlen(vote_body);
  1577. if (!vote) {
  1578. log_warn(LD_DIR, "Couldn't parse vote: length was %d",
  1579. (int)strlen(vote_body));
  1580. *msg_out = "Unable to parse vote";
  1581. goto err;
  1582. }
  1583. tor_assert(smartlist_len(vote->voters) == 1);
  1584. vi = get_voter(vote);
  1585. tor_assert(vi->good_signature == 1);
  1586. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1587. if (!ds) {
  1588. char *keys = list_v3_auth_ids();
  1589. log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
  1590. "with authority key ID %s. "
  1591. "This key ID is not recognized. Known v3 key IDs are: %s",
  1592. vi->nickname, vi->address,
  1593. hex_str(vi->identity_digest, DIGEST_LEN), keys);
  1594. tor_free(keys);
  1595. *msg_out = "Vote not from a recognized v3 authority";
  1596. goto err;
  1597. }
  1598. tor_assert(vote->cert);
  1599. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1600. vote->cert->signing_key_digest)) {
  1601. /* Hey, it's a new cert! */
  1602. trusted_dirs_load_certs_from_string(
  1603. vote->cert->cache_info.signed_descriptor_body,
  1604. 0 /* from_store */, 1 /*flush*/);
  1605. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1606. vote->cert->signing_key_digest)) {
  1607. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  1608. }
  1609. }
  1610. /* Is it for the right period? */
  1611. if (vote->valid_after != voting_schedule.interval_starts) {
  1612. char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
  1613. format_iso_time(tbuf1, vote->valid_after);
  1614. format_iso_time(tbuf2, voting_schedule.interval_starts);
  1615. log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
  1616. "we were expecting %s", vi->address, tbuf1, tbuf2);
  1617. *msg_out = "Bad valid-after time";
  1618. goto err;
  1619. }
  1620. /* Now see whether we already have a vote from this authority. */
  1621. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1622. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1623. vote->cert->cache_info.identity_digest,
  1624. DIGEST_LEN)) {
  1625. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  1626. if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  1627. /* Ah, it's the same vote. Not a problem. */
  1628. log_info(LD_DIR, "Discarding a vote we already have.");
  1629. if (*status_out < 200)
  1630. *status_out = 200;
  1631. goto discard;
  1632. } else if (v->vote->published < vote->published) {
  1633. log_notice(LD_DIR, "Replacing an older pending vote from this "
  1634. "directory.");
  1635. cached_dir_decref(v->vote_body);
  1636. networkstatus_vote_free(v->vote);
  1637. v->vote_body = new_cached_dir(tor_strndup(vote_body,
  1638. end_of_vote-vote_body),
  1639. vote->published);
  1640. v->vote = vote;
  1641. if (end_of_vote &&
  1642. !strcmpstart(end_of_vote, "network-status-version"))
  1643. goto again;
  1644. if (*status_out < 200)
  1645. *status_out = 200;
  1646. if (!*msg_out)
  1647. *msg_out = "OK";
  1648. return v;
  1649. } else {
  1650. *msg_out = "Already have a newer pending vote";
  1651. goto err;
  1652. }
  1653. }
  1654. });
  1655. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1656. pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
  1657. end_of_vote-vote_body),
  1658. vote->published);
  1659. pending_vote->vote = vote;
  1660. smartlist_add(pending_vote_list, pending_vote);
  1661. if (!strcmpstart(end_of_vote, "network-status-version ")) {
  1662. vote_body = end_of_vote;
  1663. goto again;
  1664. }
  1665. goto done;
  1666. err:
  1667. any_failed = 1;
  1668. if (!*msg_out)
  1669. *msg_out = "Error adding vote";
  1670. if (*status_out < 400)
  1671. *status_out = 400;
  1672. discard:
  1673. if (vote)
  1674. networkstatus_vote_free(vote);
  1675. if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
  1676. vote_body = end_of_vote;
  1677. goto again;
  1678. }
  1679. done:
  1680. if (*status_out < 200)
  1681. *status_out = 200;
  1682. if (!*msg_out) {
  1683. if (!any_failed && !pending_vote) {
  1684. *msg_out = "Duplicate discarded";
  1685. } else {
  1686. *msg_out = "ok";
  1687. }
  1688. }
  1689. return any_failed ? NULL : pending_vote;
  1690. }
  1691. /** Try to compute a v3 networkstatus consensus from the currently pending
  1692. * votes. Return 0 on success, -1 on failure. Store the consensus in
  1693. * pending_consensus: it won't be ready to be published until we have
  1694. * everybody else's signatures collected too. (V3 Authority only) */
  1695. static int
  1696. dirvote_compute_consensus(void)
  1697. {
  1698. /* Have we got enough votes to try? */
  1699. int n_votes, n_voters;
  1700. smartlist_t *votes = NULL, *votestrings = NULL;
  1701. char *consensus_body = NULL, *signatures = NULL, *votefile;
  1702. networkstatus_t *consensus = NULL;
  1703. authority_cert_t *my_cert;
  1704. if (!pending_vote_list)
  1705. pending_vote_list = smartlist_create();
  1706. n_voters = get_n_authorities(V3_AUTHORITY);
  1707. n_votes = smartlist_len(pending_vote_list);
  1708. if (n_votes <= n_voters/2) {
  1709. log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
  1710. "%d of %d", n_votes, n_voters/2);
  1711. goto err;
  1712. }
  1713. if (!(my_cert = get_my_v3_authority_cert())) {
  1714. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1715. goto err;
  1716. }
  1717. votes = smartlist_create();
  1718. votestrings = smartlist_create();
  1719. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1720. {
  1721. sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
  1722. c->bytes = v->vote_body->dir;
  1723. c->len = v->vote_body->dir_len;
  1724. smartlist_add(votestrings, c); /* collect strings to write to disk */
  1725. smartlist_add(votes, v->vote); /* collect votes to compute consensus */
  1726. });
  1727. votefile = get_datadir_fname("v3-status-votes");
  1728. write_chunks_to_file(votefile, votestrings, 0);
  1729. tor_free(votefile);
  1730. SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
  1731. smartlist_free(votestrings);
  1732. {
  1733. char legacy_dbuf[DIGEST_LEN];
  1734. crypto_pk_env_t *legacy_sign=NULL;
  1735. char *legacy_id_digest = NULL;
  1736. if (get_options()->V3AuthUseLegacyKey) {
  1737. authority_cert_t *cert = get_my_v3_legacy_cert();
  1738. legacy_sign = get_my_v3_legacy_signing_key();
  1739. if (cert) {
  1740. crypto_pk_get_digest(cert->identity_key, legacy_dbuf);
  1741. legacy_id_digest = legacy_dbuf;
  1742. }
  1743. }
  1744. consensus_body = networkstatus_compute_consensus(
  1745. votes, n_voters,
  1746. my_cert->identity_key,
  1747. get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign);
  1748. }
  1749. if (!consensus_body) {
  1750. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1751. goto err;
  1752. }
  1753. consensus = networkstatus_parse_vote_from_string(consensus_body, NULL,
  1754. NS_TYPE_CONSENSUS);
  1755. if (!consensus) {
  1756. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1757. goto err;
  1758. }
  1759. /* 'Check' our own signature, to mark it valid. */
  1760. networkstatus_check_consensus_signature(consensus, -1);
  1761. signatures = networkstatus_get_detached_signatures(consensus);
  1762. if (!signatures) {
  1763. log_warn(LD_DIR, "Couldn't extract signatures.");
  1764. goto err;
  1765. }
  1766. tor_free(pending_consensus_body);
  1767. pending_consensus_body = consensus_body;
  1768. tor_free(pending_consensus_signatures);
  1769. pending_consensus_signatures = signatures;
  1770. if (pending_consensus)
  1771. networkstatus_vote_free(pending_consensus);
  1772. pending_consensus = consensus;
  1773. if (pending_consensus_signature_list) {
  1774. int n_sigs = 0;
  1775. /* we may have gotten signatures for this consensus before we built
  1776. * it ourself. Add them now. */
  1777. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1778. {
  1779. const char *msg = NULL;
  1780. int r = dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1781. if (r >= 0)
  1782. n_sigs += r;
  1783. else
  1784. log_warn(LD_DIR,
  1785. "Could not add queued signature to new consensus: %s",
  1786. msg);
  1787. tor_free(sig);
  1788. });
  1789. if (n_sigs)
  1790. log_notice(LD_DIR, "Added %d pending signatures while building "
  1791. "consensus.", n_sigs);
  1792. smartlist_clear(pending_consensus_signature_list);
  1793. }
  1794. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  1795. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  1796. ROUTER_PURPOSE_GENERAL,
  1797. V3_AUTHORITY,
  1798. pending_consensus_signatures,
  1799. strlen(pending_consensus_signatures), 0);
  1800. log_notice(LD_DIR, "Signature(s) posted.");
  1801. return 0;
  1802. err:
  1803. if (votes)
  1804. smartlist_free(votes);
  1805. tor_free(consensus_body);
  1806. tor_free(signatures);
  1807. networkstatus_vote_free(consensus);
  1808. return -1;
  1809. }
  1810. /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
  1811. * signatures on the currently pending consensus. Add them to the consensus
  1812. * as appropriate. Return the number of signatures added. (?) */
  1813. static int
  1814. dirvote_add_signatures_to_pending_consensus(
  1815. const char *detached_signatures_body,
  1816. const char **msg_out)
  1817. {
  1818. ns_detached_signatures_t *sigs = NULL;
  1819. int r = -1;
  1820. tor_assert(detached_signatures_body);
  1821. tor_assert(msg_out);
  1822. /* Only call if we have a pending consensus right now. */
  1823. tor_assert(pending_consensus);
  1824. tor_assert(pending_consensus_body);
  1825. tor_assert(pending_consensus_signatures);
  1826. *msg_out = NULL;
  1827. if (!(sigs = networkstatus_parse_detached_signatures(
  1828. detached_signatures_body, NULL))) {
  1829. *msg_out = "Couldn't parse detached signatures.";
  1830. goto err;
  1831. }
  1832. log_info(LD_DIR, "Have %d signatures for adding to consensus.",
  1833. smartlist_len(sigs->signatures));
  1834. r = networkstatus_add_detached_signatures(pending_consensus,
  1835. sigs, msg_out);
  1836. log_info(LD_DIR,"Added %d signatures to consensus.", r);
  1837. if (r >= 1) {
  1838. char *new_detached =
  1839. networkstatus_get_detached_signatures(pending_consensus);
  1840. const char *src;
  1841. char *dst, *dst_end;
  1842. size_t new_consensus_len;
  1843. if (!new_detached) {
  1844. *msg_out = "No signatures to add";
  1845. goto err;
  1846. }
  1847. new_consensus_len =
  1848. strlen(pending_consensus_body) + strlen(new_detached) + 1;
  1849. pending_consensus_body = tor_realloc(pending_consensus_body,
  1850. new_consensus_len);
  1851. dst_end = pending_consensus_body + new_consensus_len;
  1852. dst = strstr(pending_consensus_body, "directory-signature ");
  1853. tor_assert(dst);
  1854. src = strstr(new_detached, "directory-signature ");
  1855. tor_assert(src);
  1856. strlcpy(dst, src, dst_end-dst);
  1857. /* We remove this block once it has failed to crash for a while. But
  1858. * unless it shows up in profiles, we're probably better leaving it in,
  1859. * just in case we break detached signature processing at some point. */
  1860. {
  1861. ns_detached_signatures_t *sigs =
  1862. networkstatus_parse_detached_signatures(new_detached, NULL);
  1863. networkstatus_t *v = networkstatus_parse_vote_from_string(
  1864. pending_consensus_body, NULL,
  1865. NS_TYPE_CONSENSUS);
  1866. tor_assert(sigs);
  1867. ns_detached_signatures_free(sigs);
  1868. tor_assert(v);
  1869. networkstatus_vote_free(v);
  1870. }
  1871. tor_free(pending_consensus_signatures);
  1872. pending_consensus_signatures = new_detached;
  1873. *msg_out = "Signatures added";
  1874. } else {
  1875. goto err;
  1876. }
  1877. goto done;
  1878. err:
  1879. if (!msg_out)
  1880. *msg_out = "Unrecognized error while adding detached signatures.";
  1881. done:
  1882. if (sigs)
  1883. ns_detached_signatures_free(sigs);
  1884. return r;
  1885. }
  1886. /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
  1887. * signatures on the currently pending consensus. Add them to the pending
  1888. * consensus (if we have one); otherwise queue them until we have a
  1889. * consensus. Return negative on failure, nonnegative on success. */
  1890. int
  1891. dirvote_add_signatures(const char *detached_signatures_body,
  1892. const char *source,
  1893. const char **msg)
  1894. {
  1895. if (pending_consensus) {
  1896. log_notice(LD_DIR, "Got a signature from %s. "
  1897. "Adding it to the pending consensus.", source);
  1898. return dirvote_add_signatures_to_pending_consensus(
  1899. detached_signatures_body, msg);
  1900. } else {
  1901. log_notice(LD_DIR, "Got a signature from %s. "
  1902. "Queueing it for the next consensus.", source);
  1903. if (!pending_consensus_signature_list)
  1904. pending_consensus_signature_list = smartlist_create();
  1905. smartlist_add(pending_consensus_signature_list,
  1906. tor_strdup(detached_signatures_body));
  1907. *msg = "Signature queued";
  1908. return 0;
  1909. }
  1910. }
  1911. /** Replace the consensus that we're currently serving with the one that we've
  1912. * been building. (V3 Authority only) */
  1913. static int
  1914. dirvote_publish_consensus(void)
  1915. {
  1916. /* Can we actually publish it yet? */
  1917. if (!pending_consensus ||
  1918. networkstatus_check_consensus_signature(pending_consensus, 1)<0) {
  1919. log_warn(LD_DIR, "Not enough info to publish pending consensus");
  1920. return -1;
  1921. }
  1922. if (networkstatus_set_current_consensus(pending_consensus_body, 0))
  1923. log_warn(LD_DIR, "Error publishing consensus");
  1924. else
  1925. log_notice(LD_DIR, "Consensus published.");
  1926. return 0;
  1927. }
  1928. /** Release all static storage held in dirvote.c */
  1929. void
  1930. dirvote_free_all(void)
  1931. {
  1932. dirvote_clear_votes(1);
  1933. /* now empty as a result of clear_pending_votes. */
  1934. smartlist_free(pending_vote_list);
  1935. pending_vote_list = NULL;
  1936. smartlist_free(previous_vote_list);
  1937. previous_vote_list = NULL;
  1938. tor_free(pending_consensus_body);
  1939. tor_free(pending_consensus_signatures);
  1940. if (pending_consensus) {
  1941. networkstatus_vote_free(pending_consensus);
  1942. pending_consensus = NULL;
  1943. }
  1944. if (pending_consensus_signature_list) {
  1945. /* now empty as a result of clear_pending_votes. */
  1946. smartlist_free(pending_consensus_signature_list);
  1947. pending_consensus_signature_list = NULL;
  1948. }
  1949. }
  1950. /* ====
  1951. * Access to pending items.
  1952. * ==== */
  1953. /** Return the body of the consensus that we're currently trying to build. */
  1954. const char *
  1955. dirvote_get_pending_consensus(void)
  1956. {
  1957. return pending_consensus_body;
  1958. }
  1959. /** Return the signatures that we know for the consensus that we're currently
  1960. * trying to build */
  1961. const char *
  1962. dirvote_get_pending_detached_signatures(void)
  1963. {
  1964. return pending_consensus_signatures;
  1965. }
  1966. /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
  1967. * vote for the authority with the v3 authority identity key digest <b>fp</b>;
  1968. * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
  1969. * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
  1970. * false, do not consider any votes for a consensus that's already been built.
  1971. * If <b>include_pending</b> is false, do not consider any votes for the
  1972. * consensus that's in progress. May return NULL if we have no vote for the
  1973. * authority in question. */
  1974. const cached_dir_t *
  1975. dirvote_get_vote(const char *fp, int flags)
  1976. {
  1977. int by_id = flags & DGV_BY_ID;
  1978. const int include_pending = flags & DGV_INCLUDE_PENDING;
  1979. const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
  1980. if (!pending_vote_list && !previous_vote_list)
  1981. return NULL;
  1982. if (fp == NULL) {
  1983. authority_cert_t *c = get_my_v3_authority_cert();
  1984. if (c) {
  1985. fp = c->cache_info.identity_digest;
  1986. by_id = 1;
  1987. } else
  1988. return NULL;
  1989. }
  1990. if (by_id) {
  1991. if (pending_vote_list && include_pending) {
  1992. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1993. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1994. return pv->vote_body);
  1995. }
  1996. if (previous_vote_list && include_previous) {
  1997. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1998. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1999. return pv->vote_body);
  2000. }
  2001. } else {
  2002. if (pending_vote_list && include_pending) {
  2003. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  2004. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  2005. return pv->vote_body);
  2006. }
  2007. if (previous_vote_list && include_previous) {
  2008. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  2009. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  2010. return pv->vote_body);
  2011. }
  2012. }
  2013. return NULL;
  2014. }