123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639 |
- #define DIRVOTE_PRIVATE
- #include "or.h"
- #include "config.h"
- #include "directory.h"
- #include "dirserv.h"
- #include "dirvote.h"
- #include "microdesc.h"
- #include "networkstatus.h"
- #include "policies.h"
- #include "rephist.h"
- #include "router.h"
- #include "routerlist.h"
- #include "routerparse.h"
- typedef struct pending_consensus_t {
-
- char *body;
-
- networkstatus_t *consensus;
- } pending_consensus_t;
- static int dirvote_add_signatures_to_all_pending_consensuses(
- const char *detached_signatures_body,
- const char **msg_out);
- static int dirvote_add_signatures_to_pending_consensus(
- pending_consensus_t *pc,
- ns_detached_signatures_t *sigs,
- const char **msg_out);
- static char *list_v3_auth_ids(void);
- static void dirvote_fetch_missing_votes(void);
- static void dirvote_fetch_missing_signatures(void);
- static int dirvote_perform_vote(void);
- static void dirvote_clear_votes(int all_votes);
- static int dirvote_compute_consensuses(void);
- static int dirvote_publish_consensus(void);
- static char *make_consensus_method_list(int low, int high, const char *sep);
- #define MAX_SUPPORTED_CONSENSUS_METHOD 11
- #define MIN_METHOD_FOR_FOOTER 9
- #define MIN_METHOD_FOR_BW_WEIGHTS 9
- #define MIN_METHOD_FOR_PARAMS 7
- #define MIN_METHOD_FOR_MICRODESC 8
- #define MICRODESC_LINE_LEN 80
- char *
- format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
- networkstatus_t *v3_ns)
- {
- size_t len;
- char *status = NULL;
- const char *client_versions = NULL, *server_versions = NULL;
- char *outp, *endp;
- char fingerprint[FINGERPRINT_LEN+1];
- char ipaddr[INET_NTOA_BUF_LEN];
- char digest[DIGEST_LEN];
- struct in_addr in;
- uint32_t addr;
- routerlist_t *rl = router_get_routerlist();
- char *version_lines = NULL;
- int r;
- networkstatus_voter_info_t *voter;
- tor_assert(private_signing_key);
- tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
- voter = smartlist_get(v3_ns->voters, 0);
- addr = voter->addr;
- in.s_addr = htonl(addr);
- tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
- base16_encode(fingerprint, sizeof(fingerprint),
- v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
- client_versions = v3_ns->client_versions;
- server_versions = v3_ns->server_versions;
- if (client_versions || server_versions) {
- size_t v_len = 64;
- char *cp;
- if (client_versions)
- v_len += strlen(client_versions);
- if (server_versions)
- v_len += strlen(server_versions);
- version_lines = tor_malloc(v_len);
- cp = version_lines;
- if (client_versions) {
- r = tor_snprintf(cp, v_len-(cp-version_lines),
- "client-versions %s\n", client_versions);
- if (r < 0) {
- log_err(LD_BUG, "Insufficient memory for client-versions line");
- tor_assert(0);
- }
- cp += strlen(cp);
- }
- if (server_versions) {
- r = tor_snprintf(cp, v_len-(cp-version_lines),
- "server-versions %s\n", server_versions);
- if (r < 0) {
- log_err(LD_BUG, "Insufficient memory for server-versions line");
- tor_assert(0);
- }
- }
- } else {
- version_lines = tor_strdup("");
- }
- len = 8192;
- len += strlen(version_lines);
- len += (RS_ENTRY_LEN+MICRODESC_LINE_LEN)*smartlist_len(rl->routers);
- len += strlen("\ndirectory-footer\n");
- len += v3_ns->cert->cache_info.signed_descriptor_len;
- status = tor_malloc(len);
- {
- char published[ISO_TIME_LEN+1];
- char va[ISO_TIME_LEN+1];
- char fu[ISO_TIME_LEN+1];
- char vu[ISO_TIME_LEN+1];
- char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
- char *params;
- authority_cert_t *cert = v3_ns->cert;
- char *methods =
- make_consensus_method_list(1, MAX_SUPPORTED_CONSENSUS_METHOD, " ");
- format_iso_time(published, v3_ns->published);
- format_iso_time(va, v3_ns->valid_after);
- format_iso_time(fu, v3_ns->fresh_until);
- format_iso_time(vu, v3_ns->valid_until);
- if (v3_ns->net_params)
- params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
- else
- params = tor_strdup("");
- tor_assert(cert);
- r = tor_snprintf(status, len,
- "network-status-version 3\n"
- "vote-status %s\n"
- "consensus-methods %s\n"
- "published %s\n"
- "valid-after %s\n"
- "fresh-until %s\n"
- "valid-until %s\n"
- "voting-delay %d %d\n"
- "%s"
- "known-flags %s\n"
- "params %s\n"
- "dir-source %s %s %s %s %d %d\n"
- "contact %s\n",
- v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
- methods,
- published, va, fu, vu,
- v3_ns->vote_seconds, v3_ns->dist_seconds,
- version_lines,
- flags,
- params,
- voter->nickname, fingerprint, voter->address,
- ipaddr, voter->dir_port, voter->or_port, voter->contact);
- if (r < 0) {
- log_err(LD_BUG, "Insufficient memory for network status line");
- tor_assert(0);
- }
- tor_free(params);
- tor_free(flags);
- tor_free(methods);
- outp = status + strlen(status);
- endp = status + len;
- if (!tor_digest_is_zero(voter->legacy_id_digest)) {
- char fpbuf[HEX_DIGEST_LEN+1];
- base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
- r = tor_snprintf(outp, endp-outp, "legacy-dir-key %s\n", fpbuf);
- if (r < 0) {
- log_err(LD_BUG, "Insufficient memory for legacy-dir-key line");
- tor_assert(0);
- }
- outp += strlen(outp);
- }
- tor_assert(outp + cert->cache_info.signed_descriptor_len < endp);
- memcpy(outp, cert->cache_info.signed_descriptor_body,
- cert->cache_info.signed_descriptor_len);
- outp += cert->cache_info.signed_descriptor_len;
- }
- SMARTLIST_FOREACH_BEGIN(v3_ns->routerstatus_list, vote_routerstatus_t *,
- vrs) {
- vote_microdesc_hash_t *h;
- if (routerstatus_format_entry(outp, endp-outp, &vrs->status,
- vrs->version, NS_V3_VOTE) < 0) {
- log_warn(LD_BUG, "Unable to print router status.");
- goto err;
- }
- outp += strlen(outp);
- for (h = vrs->microdesc; h; h = h->next) {
- size_t mlen = strlen(h->microdesc_hash_line);
- if (outp+mlen >= endp) {
- log_warn(LD_BUG, "Can't fit microdesc line in vote.");
- }
- memcpy(outp, h->microdesc_hash_line, mlen+1);
- outp += strlen(outp);
- }
- } SMARTLIST_FOREACH_END(vrs);
- r = tor_snprintf(outp, endp-outp, "directory-footer\n");
- if (r < 0) {
- log_err(LD_BUG, "Insufficient memory for directory-footer line");
- tor_assert(0);
- }
- outp += strlen(outp);
- {
- char signing_key_fingerprint[FINGERPRINT_LEN+1];
- if (tor_snprintf(outp, endp-outp, "directory-signature ")<0) {
- log_warn(LD_BUG, "Unable to start signature line.");
- goto err;
- }
- outp += strlen(outp);
- if (crypto_pk_get_fingerprint(private_signing_key,
- signing_key_fingerprint, 0)<0) {
- log_warn(LD_BUG, "Unable to get fingerprint for signing key");
- goto err;
- }
- if (tor_snprintf(outp, endp-outp, "%s %s\n", fingerprint,
- signing_key_fingerprint)<0) {
- log_warn(LD_BUG, "Unable to end signature line.");
- goto err;
- }
- outp += strlen(outp);
- }
- if (router_get_networkstatus_v3_hash(status, digest, DIGEST_SHA1)<0)
- goto err;
- note_crypto_pk_op(SIGN_DIR);
- if (router_append_dirobj_signature(outp,endp-outp,digest, DIGEST_LEN,
- private_signing_key)<0) {
- log_warn(LD_BUG, "Unable to sign networkstatus vote.");
- goto err;
- }
- {
- networkstatus_t *v;
- if (!(v = networkstatus_parse_vote_from_string(status, NULL,
- v3_ns->type))) {
- log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
- "<<%s>>",
- v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
- goto err;
- }
- networkstatus_vote_free(v);
- }
- goto done;
- err:
- tor_free(status);
- done:
- tor_free(version_lines);
- return status;
- }
- static networkstatus_voter_info_t *
- get_voter(const networkstatus_t *vote)
- {
- tor_assert(vote);
- tor_assert(vote->type == NS_TYPE_VOTE);
- tor_assert(vote->voters);
- tor_assert(smartlist_len(vote->voters) == 1);
- return smartlist_get(vote->voters, 0);
- }
- document_signature_t *
- voter_get_sig_by_algorithm(const networkstatus_voter_info_t *voter,
- digest_algorithm_t alg)
- {
- if (!voter->sigs)
- return NULL;
- SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
- if (sig->alg == alg)
- return sig);
- return NULL;
- }
- typedef struct dir_src_ent_t {
- networkstatus_t *v;
- const char *digest;
- int is_legacy;
- } dir_src_ent_t;
- static int
- _compare_votes_by_authority_id(const void **_a, const void **_b)
- {
- const networkstatus_t *a = *_a, *b = *_b;
- return fast_memcmp(get_voter(a)->identity_digest,
- get_voter(b)->identity_digest, DIGEST_LEN);
- }
- static int
- _compare_dir_src_ents_by_authority_id(const void **_a, const void **_b)
- {
- const dir_src_ent_t *a = *_a, *b = *_b;
- const networkstatus_voter_info_t *a_v = get_voter(a->v),
- *b_v = get_voter(b->v);
- const char *a_id, *b_id;
- a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
- b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
- return fast_memcmp(a_id, b_id, DIGEST_LEN);
- }
- static void
- get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
- {
- char *cur = NULL;
- int count = 0;
- SMARTLIST_FOREACH(in, char *, cp,
- {
- if (cur && !strcmp(cp, cur)) {
- ++count;
- } else {
- if (count > min)
- smartlist_add(out, cur);
- cur = cp;
- count = 1;
- }
- });
- if (count > min)
- smartlist_add(out, cur);
- }
- #define get_most_frequent_member(lst) \
- smartlist_get_most_frequent_string(lst)
- static int
- compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
- {
- int r;
- if ((r = fast_memcmp(a->status.identity_digest, b->status.identity_digest,
- DIGEST_LEN)))
- return r;
- if ((r = fast_memcmp(a->status.descriptor_digest,
- b->status.descriptor_digest,
- DIGEST_LEN)))
- return r;
- if ((r = (int)(b->status.published_on - a->status.published_on)))
- return r;
- if ((r = strcmp(b->status.nickname, a->status.nickname)))
- return r;
- if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
- return r;
- if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
- return r;
- if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
- return r;
- return 0;
- }
- static int
- _compare_vote_rs(const void **_a, const void **_b)
- {
- const vote_routerstatus_t *a = *_a, *b = *_b;
- return compare_vote_rs(a,b);
- }
- static vote_routerstatus_t *
- compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
- char *microdesc_digest256_out)
- {
- vote_routerstatus_t *most = NULL, *cur = NULL;
- int most_n = 0, cur_n = 0;
- time_t most_published = 0;
-
- smartlist_sort(votes, _compare_vote_rs);
- SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
- {
- if (cur && !compare_vote_rs(cur, rs)) {
- ++cur_n;
- } else {
- if (cur && (cur_n > most_n ||
- (cur_n == most_n &&
- cur->status.published_on > most_published))) {
- most = cur;
- most_n = cur_n;
- most_published = cur->status.published_on;
- }
- cur_n = 1;
- cur = rs;
- }
- });
- if (cur_n > most_n ||
- (cur && cur_n == most_n && cur->status.published_on > most_published)) {
- most = cur;
- most_n = cur_n;
- most_published = cur->status.published_on;
- }
- tor_assert(most);
- if (consensus_method >= MIN_METHOD_FOR_MICRODESC &&
- microdesc_digest256_out) {
- smartlist_t *digests = smartlist_create();
- const char *best_microdesc_digest;
- SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
- char d[DIGEST256_LEN];
- if (compare_vote_rs(rs, most))
- continue;
- if (!vote_routerstatus_find_microdesc_hash(d, rs, consensus_method,
- DIGEST_SHA256))
- smartlist_add(digests, tor_memdup(d, sizeof(d)));
- } SMARTLIST_FOREACH_END(rs);
- smartlist_sort_digests256(digests);
- best_microdesc_digest = smartlist_get_most_frequent_digest256(digests);
- if (best_microdesc_digest)
- memcpy(microdesc_digest256_out, best_microdesc_digest, DIGEST256_LEN);
- SMARTLIST_FOREACH(digests, char *, cp, tor_free(cp));
- smartlist_free(digests);
- }
- return most;
- }
- static void
- hash_list_members(char *digest_out, size_t len_out,
- smartlist_t *lst, digest_algorithm_t alg)
- {
- crypto_digest_env_t *d;
- if (alg == DIGEST_SHA1)
- d = crypto_new_digest_env();
- else
- d = crypto_new_digest256_env(alg);
- SMARTLIST_FOREACH(lst, const char *, cp,
- crypto_digest_add_bytes(d, cp, strlen(cp)));
- crypto_digest_get_digest(d, digest_out, len_out);
- crypto_free_digest_env(d);
- }
- static int
- _cmp_int_strings(const void **_a, const void **_b)
- {
- const char *a = *_a, *b = *_b;
- int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
- int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
- if (ai<bi) {
- return -1;
- } else if (ai==bi) {
- if (ai == 0)
- return strcmp(a, b);
- return 0;
- } else {
- return 1;
- }
- }
- static int
- compute_consensus_method(smartlist_t *votes)
- {
- smartlist_t *all_methods = smartlist_create();
- smartlist_t *acceptable_methods = smartlist_create();
- smartlist_t *tmp = smartlist_create();
- int min = (smartlist_len(votes) * 2) / 3;
- int n_ok;
- int result;
- SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
- {
- tor_assert(vote->supported_methods);
- smartlist_add_all(tmp, vote->supported_methods);
- smartlist_sort(tmp, _cmp_int_strings);
- smartlist_uniq(tmp, _cmp_int_strings, NULL);
- smartlist_add_all(all_methods, tmp);
- smartlist_clear(tmp);
- });
- smartlist_sort(all_methods, _cmp_int_strings);
- get_frequent_members(acceptable_methods, all_methods, min);
- n_ok = smartlist_len(acceptable_methods);
- if (n_ok) {
- const char *best = smartlist_get(acceptable_methods, n_ok-1);
- result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
- } else {
- result = 1;
- }
- smartlist_free(tmp);
- smartlist_free(all_methods);
- smartlist_free(acceptable_methods);
- return result;
- }
- static int
- consensus_method_is_supported(int method)
- {
- return (method >= 1) && (method <= MAX_SUPPORTED_CONSENSUS_METHOD);
- }
- static char *
- make_consensus_method_list(int low, int high, const char *separator)
- {
- char *list;
- char b[32];
- int i;
- smartlist_t *lst;
- lst = smartlist_create();
- for (i = low; i <= high; ++i) {
- if (!consensus_method_is_supported(i))
- continue;
- tor_snprintf(b, sizeof(b), "%d", i);
- smartlist_add(lst, tor_strdup(b));
- }
- list = smartlist_join_strings(lst, separator, 0, NULL);
- tor_assert(list);
- SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
- smartlist_free(lst);
- return list;
- }
- static char *
- compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
- {
- int min = n_versioning / 2;
- smartlist_t *good = smartlist_create();
- char *result;
- sort_version_list(lst, 0);
- get_frequent_members(good, lst, min);
- result = smartlist_join_strings(good, ",", 0, NULL);
- smartlist_free(good);
- return result;
- }
- char *
- dirvote_compute_params(smartlist_t *votes)
- {
- int i;
- int32_t *vals;
- int cur_param_len;
- const char *cur_param;
- const char *eq;
- char *result;
- const int n_votes = smartlist_len(votes);
- smartlist_t *output;
- smartlist_t *param_list = smartlist_create();
-
- vals = tor_malloc(sizeof(int)*n_votes);
- SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
- if (!v->net_params)
- continue;
- smartlist_add_all(param_list, v->net_params);
- } SMARTLIST_FOREACH_END(v);
- if (smartlist_len(param_list) == 0) {
- tor_free(vals);
- smartlist_free(param_list);
- return NULL;
- }
- smartlist_sort_strings(param_list);
- i = 0;
- cur_param = smartlist_get(param_list, 0);
- eq = strchr(cur_param, '=');
- tor_assert(eq);
- cur_param_len = (int)(eq+1 - cur_param);
- output = smartlist_create();
- SMARTLIST_FOREACH_BEGIN(param_list, const char *, param) {
- const char *next_param;
- int ok=0;
- eq = strchr(param, '=');
- tor_assert(i<n_votes);
- vals[i++] = (int32_t)
- tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
- tor_assert(ok);
- if (param_sl_idx+1 == smartlist_len(param_list))
- next_param = NULL;
- else
- next_param = smartlist_get(param_list, param_sl_idx+1);
- if (!next_param || strncmp(next_param, param, cur_param_len)) {
-
- int32_t median = median_int32(vals, i);
- char *out_string = tor_malloc(64+cur_param_len);
- memcpy(out_string, param, cur_param_len);
- tor_snprintf(out_string+cur_param_len,64, "%ld", (long)median);
- smartlist_add(output, out_string);
- i = 0;
- if (next_param) {
- eq = strchr(next_param, '=');
- cur_param_len = (int)(eq+1 - next_param);
- }
- }
- } SMARTLIST_FOREACH_END(param);
- result = smartlist_join_strings(output, " ", 0, NULL);
- SMARTLIST_FOREACH(output, char *, cp, tor_free(cp));
- smartlist_free(output);
- smartlist_free(param_list);
- tor_free(vals);
- return result;
- }
- #define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
- ((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
- (c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
- (e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
- (g) >= 0 && (g) <= (mx))
- #define CHECK_EQ(a, b, margin) \
- ((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
- typedef enum {
- BW_WEIGHTS_NO_ERROR = 0,
- BW_WEIGHTS_RANGE_ERROR = 1,
- BW_WEIGHTS_SUMG_ERROR = 2,
- BW_WEIGHTS_SUME_ERROR = 3,
- BW_WEIGHTS_SUMD_ERROR = 4,
- BW_WEIGHTS_BALANCE_MID_ERROR = 5,
- BW_WEIGHTS_BALANCE_EG_ERROR = 6
- } bw_weights_error_t;
- static bw_weights_error_t
- networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg,
- int64_t Wme, int64_t Wmd, int64_t Wee,
- int64_t Wed, int64_t scale, int64_t G,
- int64_t M, int64_t E, int64_t D, int64_t T,
- int64_t margin, int do_balance) {
- bw_weights_error_t berr = BW_WEIGHTS_NO_ERROR;
-
- if (!CHECK_EQ(Wed + Wmd + Wgd, scale, margin)) {
- berr = BW_WEIGHTS_SUMD_ERROR;
- goto out;
- }
-
- if (!CHECK_EQ(Wmg + Wgg, scale, margin)) {
- berr = BW_WEIGHTS_SUMG_ERROR;
- goto out;
- }
-
- if (!CHECK_EQ(Wme + Wee, scale, margin)) {
- berr = BW_WEIGHTS_SUME_ERROR;
- goto out;
- }
-
- if (!RANGE_CHECK(Wgg, Wgd, Wmg, Wme, Wmd, Wed, Wee, scale)) {
- berr = BW_WEIGHTS_RANGE_ERROR;
- goto out;
- }
- if (do_balance) {
-
- if (!CHECK_EQ(Wgg*G + Wgd*D, Wee*E + Wed*D, (margin*T)/3)) {
- berr = BW_WEIGHTS_BALANCE_EG_ERROR;
- goto out;
- }
-
- if (!CHECK_EQ(Wgg*G + Wgd*D, M*scale + Wmd*D + Wme*E + Wmg*G,
- (margin*T)/3)) {
- berr = BW_WEIGHTS_BALANCE_MID_ERROR;
- goto out;
- }
- }
- out:
- if (berr) {
- log_info(LD_DIR,
- "Bw weight mismatch %d. G="I64_FORMAT" M="I64_FORMAT
- " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
- " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
- " Wgd=%d Wgg=%d Wme=%d Wmg=%d",
- berr,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
- (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
- (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg);
- }
- return berr;
- }
- static int
- networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
- int64_t M, int64_t E, int64_t D,
- int64_t T, int64_t weight_scale)
- {
- bw_weights_error_t berr = 0;
- int64_t Wgg = -1, Wgd = -1;
- int64_t Wmg = -1, Wme = -1, Wmd = -1;
- int64_t Wed = -1, Wee = -1;
- const char *casename;
- char buf[512];
- int r;
- if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
- log_warn(LD_DIR, "Consensus with empty bandwidth: "
- "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
- " D="I64_FORMAT" T="I64_FORMAT,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- return 0;
- }
-
- if (3*E >= T && 3*G >= T) {
-
- casename = "Case 1 (Wgd=Wmd=Wed)";
- Wgd = weight_scale/3;
- Wed = weight_scale/3;
- Wmd = weight_scale/3;
- Wee = (weight_scale*(E+G+M))/(3*E);
- Wme = weight_scale - Wee;
- Wmg = (weight_scale*(2*G-E-M))/(3*G);
- Wgg = weight_scale - Wmg;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
- weight_scale, G, M, E, D, T, 10, 1);
- if (berr) {
- log_warn(LD_DIR,
- "Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
- " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
- " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
- " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
- berr, casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
- (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
- (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
- return 0;
- }
- } else if (3*E < T && 3*G < T) {
- int64_t R = MIN(E, G);
- int64_t S = MAX(E, G);
-
- if (R+D < S) {
- Wgg = weight_scale;
- Wee = weight_scale;
- Wmg = 0;
- Wme = 0;
- Wmd = 0;
- if (E < G) {
- casename = "Case 2a (E scarce)";
- Wed = weight_scale;
- Wgd = 0;
- } else {
- casename = "Case 2a (G scarce)";
- Wed = 0;
- Wgd = weight_scale;
- }
- } else {
- casename = "Case 2b1 (Wgg=1, Wmd=Wgd)";
- Wee = (weight_scale*(E - G + M))/E;
- Wed = (weight_scale*(D - 2*E + 4*G - 2*M))/(3*D);
- Wme = (weight_scale*(G-M))/E;
- Wmg = 0;
- Wgg = weight_scale;
- Wmd = (weight_scale - Wed)/2;
- Wgd = (weight_scale - Wed)/2;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
- weight_scale, G, M, E, D, T, 10, 1);
- if (berr) {
- casename = "Case 2b2 (Wgg=1, Wee=1)";
- Wgg = weight_scale;
- Wee = weight_scale;
- Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
- Wmd = (weight_scale*(D - 2*M + G + E))/(3*D);
- Wme = 0;
- Wmg = 0;
- if (Wmd < 0) {
- casename = "Case 2b3 (Wmd=0)";
- Wmd = 0;
- log_warn(LD_DIR,
- "Too much Middle bandwidth on the network to calculate "
- "balanced bandwidth-weights. Consider increasing the "
- "number of Guard nodes by lowering the requirements.");
- }
- Wgd = weight_scale - Wed - Wmd;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
- Wed, weight_scale, G, M, E, D, T, 10, 1);
- }
- if (berr != BW_WEIGHTS_NO_ERROR &&
- berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
- log_warn(LD_DIR,
- "Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
- " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
- " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
- " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
- berr, casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
- (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
- (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
- return 0;
- }
- }
- } else {
- int64_t S = MIN(E, G);
-
- if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
- log_warn(LD_BUG,
- "Bw-Weights Case 3 v10 but with G="I64_FORMAT" M="
- I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- }
- if (3*(S+D) < T) {
- if (G < E) {
- casename = "Case 3a (G scarce)";
- Wgg = Wgd = weight_scale;
- Wmd = Wed = Wmg = 0;
-
-
- if (E < M) Wme = 0;
- else Wme = (weight_scale*(E-M))/(2*E);
- Wee = weight_scale-Wme;
- } else {
- casename = "Case 3a (E scarce)";
- Wee = Wed = weight_scale;
- Wmd = Wgd = Wme = 0;
-
-
- if (G < M) Wmg = 0;
- else Wmg = (weight_scale*(G-M))/(2*G);
- Wgg = weight_scale-Wmg;
- }
- } else {
-
- if (G < E) {
- casename = "Case 3bg (G scarce, Wgg=1, Wmd == Wed)";
- Wgg = weight_scale;
- Wgd = (weight_scale*(D - 2*G + E + M))/(3*D);
- Wmg = 0;
- Wee = (weight_scale*(E+M))/(2*E);
- Wme = weight_scale - Wee;
- Wmd = (weight_scale - Wgd)/2;
- Wed = (weight_scale - Wgd)/2;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
- Wed, weight_scale, G, M, E, D, T, 10, 1);
- } else {
- casename = "Case 3be (E scarce, Wee=1, Wmd == Wgd)";
- Wee = weight_scale;
- Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
- Wme = 0;
- Wgg = (weight_scale*(G+M))/(2*G);
- Wmg = weight_scale - Wgg;
- Wmd = (weight_scale - Wed)/2;
- Wgd = (weight_scale - Wed)/2;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
- Wed, weight_scale, G, M, E, D, T, 10, 1);
- }
- if (berr) {
- log_warn(LD_DIR,
- "Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
- " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
- " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
- " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
- berr, casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
- (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
- (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
- return 0;
- }
- }
- }
-
- tor_assert(0 < weight_scale && weight_scale < INT32_MAX);
-
- r = tor_snprintf(buf, sizeof(buf),
- "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
- "Wdb=%d "
- "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
- "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
- "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
- (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
- (int)weight_scale,
- (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
- (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
- (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
- if (r<0) {
- log_warn(LD_BUG,
- "Not enough space in buffer for bandwidth-weights line.");
- *buf = '\0';
- return 0;
- }
- smartlist_add(chunks, tor_strdup(buf));
- log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: "
- "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- return 1;
- }
- static void
- networkstatus_compute_bw_weights_v9(smartlist_t *chunks, int64_t G, int64_t M,
- int64_t E, int64_t D, int64_t T,
- int64_t weight_scale)
- {
- int64_t Wgg = -1, Wgd = -1;
- int64_t Wmg = -1, Wme = -1, Wmd = -1;
- int64_t Wed = -1, Wee = -1;
- const char *casename;
- char buf[512];
- int r;
- if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
- log_warn(LD_DIR, "Consensus with empty bandwidth: "
- "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
- " D="I64_FORMAT" T="I64_FORMAT,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- return;
- }
-
- if (3*E >= T && 3*G >= T) {
- bw_weights_error_t berr = 0;
-
- casename = "Case 1 (Wme*E = Wmd*D)";
- Wgg = (weight_scale*(D+E+G+M))/(3*G);
- if (D==0) Wmd = 0;
- else Wmd = (weight_scale*(2*D + 2*E - G - M))/(6*D);
- Wme = (weight_scale*(2*D + 2*E - G - M))/(6*E);
- Wee = (weight_scale*(-2*D + 4*E + G + M))/(6*E);
- Wgd = 0;
- Wmg = weight_scale - Wgg;
- Wed = weight_scale - Wmd;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
- weight_scale, G, M, E, D, T, 10, 1);
- if (berr) {
- log_warn(LD_DIR, "Bw Weights error %d for case %s. "
- "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
- " D="I64_FORMAT" T="I64_FORMAT,
- berr, casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- }
- } else if (3*E < T && 3*G < T) {
- int64_t R = MIN(E, G);
- int64_t S = MAX(E, G);
-
- if (R+D < S) {
- Wgg = weight_scale;
- Wee = weight_scale;
- Wmg = 0;
- Wme = 0;
- Wmd = 0;
- if (E < G) {
- casename = "Case 2a (E scarce)";
- Wed = weight_scale;
- Wgd = 0;
- } else {
- casename = "Case 2a (G scarce)";
- Wed = 0;
- Wgd = weight_scale;
- }
- } else {
- bw_weights_error_t berr = 0;
- casename = "Case 2b (Wme*E == Wmd*D)";
- if (D != 0) {
- Wgg = weight_scale;
- Wgd = (weight_scale*(D + E - 2*G + M))/(3*D);
- Wmd = (weight_scale*(D + E + G - 2*M))/(6*D);
- Wme = (weight_scale*(D + E + G - 2*M))/(6*E);
- Wee = (weight_scale*(-D + 5*E - G + 2*M))/(6*E);
- Wmg = 0;
- Wed = weight_scale - Wgd - Wmd;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
- weight_scale, G, M, E, D, T, 10, 1);
- }
- if (D == 0 || berr) {
- casename = "Case 2b (E=G)";
- Wgg = weight_scale;
- Wee = weight_scale;
- Wmg = 0;
- Wme = 0;
- Wmd = 0;
- if (D == 0) Wgd = 0;
- else Wgd = (weight_scale*(D+E-G))/(2*D);
- Wed = weight_scale - Wgd;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
- Wed, weight_scale, G, M, E, D, T, 10, 1);
- }
- if (berr != BW_WEIGHTS_NO_ERROR &&
- berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
- log_warn(LD_DIR, "Bw Weights error %d for case %s. "
- "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
- " D="I64_FORMAT" T="I64_FORMAT,
- berr, casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- }
- }
- } else {
- int64_t S = MIN(E, G);
-
- if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
- log_warn(LD_BUG,
- "Bw-Weights Case 3 but with G="I64_FORMAT" M="
- I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- }
- if (3*(S+D) < T) {
- if (G < E) {
- casename = "Case 3a (G scarce)";
- Wgg = Wgd = weight_scale;
- Wmd = Wed = Wmg = 0;
-
-
- if (E < M) Wme = 0;
- else Wme = (weight_scale*(E-M))/(2*E);
- Wee = weight_scale-Wme;
- } else {
- casename = "Case 3a (E scarce)";
- Wee = Wed = weight_scale;
- Wmd = Wgd = Wme = 0;
-
-
- if (G < M) Wmg = 0;
- else Wmg = (weight_scale*(G-M))/(2*G);
- Wgg = weight_scale-Wmg;
- }
- } else {
- bw_weights_error_t berr = 0;
-
- if (G < E) {
- casename = "Case 3b (G scarce, Wme*E == Wmd*D)";
- Wgd = (weight_scale*(D + E - 2*G + M))/(3*D);
- Wmd = (weight_scale*(D + E + G - 2*M))/(6*D);
- Wme = (weight_scale*(D + E + G - 2*M))/(6*E);
- Wee = (weight_scale*(-D + 5*E - G + 2*M))/(6*E);
- Wgg = weight_scale;
- Wmg = 0;
- Wed = weight_scale - Wgd - Wmd;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
- Wed, weight_scale, G, M, E, D, T, 10, 1);
- } else {
- casename = "Case 3b (E scarce, Wme*E == Wmd*D)";
- Wgg = (weight_scale*(D + E + G + M))/(3*G);
- Wmd = (weight_scale*(2*D + 2*E - G - M))/(6*D);
- Wme = (weight_scale*(2*D + 2*E - G - M))/(6*E);
- Wee = (weight_scale*(-2*D + 4*E + G + M))/(6*E);
- Wgd = 0;
- Wmg = weight_scale - Wgg;
- Wed = weight_scale - Wmd;
- berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
- Wed, weight_scale, G, M, E, D, T, 10, 1);
- }
- if (berr) {
- log_warn(LD_DIR, "Bw Weights error %d for case %s. "
- "G="I64_FORMAT" M="I64_FORMAT
- " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
- berr, casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- }
- }
- }
-
- tor_assert(0 < weight_scale && weight_scale < INT32_MAX);
- if (Wgg < 0 || Wgg > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wgg="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wgg),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wgg = MAX(MIN(Wgg, weight_scale), 0);
- }
- if (Wgd < 0 || Wgd > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wgd="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wgd),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wgd = MAX(MIN(Wgd, weight_scale), 0);
- }
- if (Wmg < 0 || Wmg > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wmg="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wmg),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wmg = MAX(MIN(Wmg, weight_scale), 0);
- }
- if (Wme < 0 || Wme > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wme="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wme),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wme = MAX(MIN(Wme, weight_scale), 0);
- }
- if (Wmd < 0 || Wmd > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wmd="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wmd),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wmd = MAX(MIN(Wmd, weight_scale), 0);
- }
- if (Wee < 0 || Wee > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wee="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wee),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wee = MAX(MIN(Wee, weight_scale), 0);
- }
- if (Wed < 0 || Wed > weight_scale) {
- log_warn(LD_DIR, "Bw %s: Wed="I64_FORMAT"! G="I64_FORMAT
- " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename, I64_PRINTF_ARG(Wed),
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- Wed = MAX(MIN(Wed, weight_scale), 0);
- }
-
- smartlist_add(chunks, tor_strdup("bandwidth-weights "));
-
- r = tor_snprintf(buf, sizeof(buf),
- "Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
- "Wdb=%d "
- "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
- "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
- "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
- (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
- (int)weight_scale,
- (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
- (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
- (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
- if (r<0) {
- log_warn(LD_BUG,
- "Not enough space in buffer for bandwidth-weights line.");
- *buf = '\0';
- }
- smartlist_add(chunks, tor_strdup(buf));
- log_notice(LD_CIRC, "Computed bandwidth weights for %s with v9: "
- "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
- " T="I64_FORMAT,
- casename,
- I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
- I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
- }
- char *
- networkstatus_compute_consensus(smartlist_t *votes,
- int total_authorities,
- crypto_pk_env_t *identity_key,
- crypto_pk_env_t *signing_key,
- const char *legacy_id_key_digest,
- crypto_pk_env_t *legacy_signing_key,
- consensus_flavor_t flavor)
- {
- smartlist_t *chunks;
- char *result = NULL;
- int consensus_method;
- time_t valid_after, fresh_until, valid_until;
- int vote_seconds, dist_seconds;
- char *client_versions = NULL, *server_versions = NULL;
- smartlist_t *flags;
- const char *flavor_name;
- int64_t G=0, M=0, E=0, D=0, T=0;
- const routerstatus_format_type_t rs_format =
- flavor == FLAV_NS ? NS_V3_CONSENSUS : NS_V3_CONSENSUS_MICRODESC;
- char *params = NULL;
- int added_weights = 0;
- tor_assert(flavor == FLAV_NS || flavor == FLAV_MICRODESC);
- tor_assert(total_authorities >= smartlist_len(votes));
- flavor_name = networkstatus_get_flavor_name(flavor);
- if (!smartlist_len(votes)) {
- log_warn(LD_DIR, "Can't compute a consensus from no votes.");
- return NULL;
- }
- flags = smartlist_create();
- consensus_method = compute_consensus_method(votes);
- if (consensus_method_is_supported(consensus_method)) {
- log_info(LD_DIR, "Generating consensus using method %d.",
- consensus_method);
- } else {
- log_warn(LD_DIR, "The other authorities will use consensus method %d, "
- "which I don't support. Maybe I should upgrade!",
- consensus_method);
- consensus_method = 1;
- }
-
- {
- int n_votes = smartlist_len(votes);
- time_t *va_times = tor_malloc(n_votes * sizeof(time_t));
- time_t *fu_times = tor_malloc(n_votes * sizeof(time_t));
- time_t *vu_times = tor_malloc(n_votes * sizeof(time_t));
- int *votesec_list = tor_malloc(n_votes * sizeof(int));
- int *distsec_list = tor_malloc(n_votes * sizeof(int));
- int n_versioning_clients = 0, n_versioning_servers = 0;
- smartlist_t *combined_client_versions = smartlist_create();
- smartlist_t *combined_server_versions = smartlist_create();
- SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
- tor_assert(v->type == NS_TYPE_VOTE);
- va_times[v_sl_idx] = v->valid_after;
- fu_times[v_sl_idx] = v->fresh_until;
- vu_times[v_sl_idx] = v->valid_until;
- votesec_list[v_sl_idx] = v->vote_seconds;
- distsec_list[v_sl_idx] = v->dist_seconds;
- if (v->client_versions) {
- smartlist_t *cv = smartlist_create();
- ++n_versioning_clients;
- smartlist_split_string(cv, v->client_versions, ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- sort_version_list(cv, 1);
- smartlist_add_all(combined_client_versions, cv);
- smartlist_free(cv);
- }
- if (v->server_versions) {
- smartlist_t *sv = smartlist_create();
- ++n_versioning_servers;
- smartlist_split_string(sv, v->server_versions, ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- sort_version_list(sv, 1);
- smartlist_add_all(combined_server_versions, sv);
- smartlist_free(sv);
- }
- SMARTLIST_FOREACH(v->known_flags, const char *, cp,
- smartlist_add(flags, tor_strdup(cp)));
- } SMARTLIST_FOREACH_END(v);
- valid_after = median_time(va_times, n_votes);
- fresh_until = median_time(fu_times, n_votes);
- valid_until = median_time(vu_times, n_votes);
- vote_seconds = median_int(votesec_list, n_votes);
- dist_seconds = median_int(distsec_list, n_votes);
- tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
- tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
- tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
- tor_assert(dist_seconds >= MIN_DIST_SECONDS);
- server_versions = compute_consensus_versions_list(combined_server_versions,
- n_versioning_servers);
- client_versions = compute_consensus_versions_list(combined_client_versions,
- n_versioning_clients);
- SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
- SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
- smartlist_free(combined_server_versions);
- smartlist_free(combined_client_versions);
- smartlist_sort_strings(flags);
- smartlist_uniq_strings(flags);
- tor_free(va_times);
- tor_free(fu_times);
- tor_free(vu_times);
- tor_free(votesec_list);
- tor_free(distsec_list);
- }
- chunks = smartlist_create();
- {
- char *buf=NULL;
- char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
- vu_buf[ISO_TIME_LEN+1];
- char *flaglist;
- format_iso_time(va_buf, valid_after);
- format_iso_time(fu_buf, fresh_until);
- format_iso_time(vu_buf, valid_until);
- flaglist = smartlist_join_strings(flags, " ", 0, NULL);
- tor_asprintf(&buf, "network-status-version 3%s%s\n"
- "vote-status consensus\n",
- flavor == FLAV_NS ? "" : " ",
- flavor == FLAV_NS ? "" : flavor_name);
- smartlist_add(chunks, buf);
- if (consensus_method >= 2) {
- tor_asprintf(&buf, "consensus-method %d\n",
- consensus_method);
- smartlist_add(chunks, buf);
- }
- tor_asprintf(&buf,
- "valid-after %s\n"
- "fresh-until %s\n"
- "valid-until %s\n"
- "voting-delay %d %d\n"
- "client-versions %s\n"
- "server-versions %s\n"
- "known-flags %s\n",
- va_buf, fu_buf, vu_buf,
- vote_seconds, dist_seconds,
- client_versions, server_versions, flaglist);
- smartlist_add(chunks, buf);
- tor_free(flaglist);
- }
- if (consensus_method >= MIN_METHOD_FOR_PARAMS) {
- params = dirvote_compute_params(votes);
- if (params) {
- smartlist_add(chunks, tor_strdup("params "));
- smartlist_add(chunks, params);
- smartlist_add(chunks, tor_strdup("\n"));
- }
- }
-
- smartlist_sort(votes, _compare_votes_by_authority_id);
-
- {
- smartlist_t *dir_sources = smartlist_create();
- SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
- dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
- e->v = v;
- e->digest = get_voter(v)->identity_digest;
- e->is_legacy = 0;
- smartlist_add(dir_sources, e);
- if (consensus_method >= 3 &&
- !tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
- dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
- e_legacy->v = v;
- e_legacy->digest = get_voter(v)->legacy_id_digest;
- e_legacy->is_legacy = 1;
- smartlist_add(dir_sources, e_legacy);
- }
- } SMARTLIST_FOREACH_END(v);
- smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
- SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
- struct in_addr in;
- char ip[INET_NTOA_BUF_LEN];
- char fingerprint[HEX_DIGEST_LEN+1];
- char votedigest[HEX_DIGEST_LEN+1];
- networkstatus_t *v = e->v;
- networkstatus_voter_info_t *voter = get_voter(v);
- char *buf = NULL;
- if (e->is_legacy)
- tor_assert(consensus_method >= 2);
- in.s_addr = htonl(voter->addr);
- tor_inet_ntoa(&in, ip, sizeof(ip));
- base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
- base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
- DIGEST_LEN);
- tor_asprintf(&buf,
- "dir-source %s%s %s %s %s %d %d\n",
- voter->nickname, e->is_legacy ? "-legacy" : "",
- fingerprint, voter->address, ip,
- voter->dir_port,
- voter->or_port);
- smartlist_add(chunks, buf);
- if (! e->is_legacy) {
- tor_asprintf(&buf,
- "contact %s\n"
- "vote-digest %s\n",
- voter->contact,
- votedigest);
- smartlist_add(chunks, buf);
- }
- } SMARTLIST_FOREACH_END(e);
- SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
- smartlist_free(dir_sources);
- }
-
- {
- int *index;
- int *size;
- int *flag_counts;
- int i;
- smartlist_t *matching_descs = smartlist_create();
- smartlist_t *chosen_flags = smartlist_create();
- smartlist_t *versions = smartlist_create();
- smartlist_t *exitsummaries = smartlist_create();
- uint32_t *bandwidths = tor_malloc(sizeof(uint32_t) * smartlist_len(votes));
- uint32_t *measured_bws = tor_malloc(sizeof(uint32_t) *
- smartlist_len(votes));
- int num_bandwidths;
- int num_mbws;
- int *n_voter_flags;
- int *n_flag_voters;
- int **flag_map;
- int *named_flag;
- int *unnamed_flag;
- int chosen_named_idx;
- strmap_t *name_to_id_map = strmap_new();
- char conflict[DIGEST_LEN];
- char unknown[DIGEST_LEN];
- memset(conflict, 0, sizeof(conflict));
- memset(unknown, 0xff, sizeof(conflict));
- index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
- size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
- n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
- n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
- flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
- named_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
- unnamed_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
- for (i = 0; i < smartlist_len(votes); ++i)
- unnamed_flag[i] = named_flag[i] = -1;
- chosen_named_idx = smartlist_string_pos(flags, "Named");
-
- SMARTLIST_FOREACH(votes, networkstatus_t *, v,
- {
- flag_map[v_sl_idx] = tor_malloc_zero(
- sizeof(int)*smartlist_len(v->known_flags));
- SMARTLIST_FOREACH(v->known_flags, const char *, fl,
- {
- int p = smartlist_string_pos(flags, fl);
- tor_assert(p >= 0);
- flag_map[v_sl_idx][fl_sl_idx] = p;
- ++n_flag_voters[p];
- if (!strcmp(fl, "Named"))
- named_flag[v_sl_idx] = fl_sl_idx;
- if (!strcmp(fl, "Unnamed"))
- unnamed_flag[v_sl_idx] = fl_sl_idx;
- });
- n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
- size[v_sl_idx] = smartlist_len(v->routerstatus_list);
- });
-
- if (consensus_method >= 2) {
- SMARTLIST_FOREACH(votes, networkstatus_t *, v,
- {
- uint64_t nf;
- if (named_flag[v_sl_idx]<0)
- continue;
- nf = U64_LITERAL(1) << named_flag[v_sl_idx];
- SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
- {
- if ((rs->flags & nf) != 0) {
- const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
- if (!d) {
-
- strmap_set_lc(name_to_id_map, rs->status.nickname,
- rs->status.identity_digest);
- } else if (d != conflict &&
- fast_memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
-
- strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
- } else {
-
- }
- }
- });
- });
- SMARTLIST_FOREACH(votes, networkstatus_t *, v,
- {
- uint64_t uf;
- if (unnamed_flag[v_sl_idx]<0)
- continue;
- uf = U64_LITERAL(1) << unnamed_flag[v_sl_idx];
- SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
- {
- if ((rs->flags & uf) != 0) {
- const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
- if (d == conflict || d == unknown) {
-
- } else if (!d) {
-
- strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
- } else if (fast_memeq(d, rs->status.identity_digest, DIGEST_LEN)) {
-
- strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
- } else {
-
- }
- }
- });
- });
- }
-
- flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
- while (1) {
- vote_routerstatus_t *rs;
- routerstatus_t rs_out;
- const char *lowest_id = NULL;
- const char *chosen_version;
- const char *chosen_name = NULL;
- int exitsummary_disagreement = 0;
- int is_named = 0, is_unnamed = 0, is_running = 0;
- int is_guard = 0, is_exit = 0, is_bad_exit = 0;
- int naming_conflict = 0;
- int n_listing = 0;
- int i;
- char *buf=NULL;
- char microdesc_digest[DIGEST256_LEN];
-
- SMARTLIST_FOREACH(votes, networkstatus_t *, v, {
- if (index[v_sl_idx] < size[v_sl_idx]) {
- rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
- if (!lowest_id ||
- fast_memcmp(rs->status.identity_digest,
- lowest_id, DIGEST_LEN) < 0)
- lowest_id = rs->status.identity_digest;
- }
- });
- if (!lowest_id)
- break;
- memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
- smartlist_clear(matching_descs);
- smartlist_clear(chosen_flags);
- smartlist_clear(versions);
- num_bandwidths = 0;
- num_mbws = 0;
-
- SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
- if (index[v_sl_idx] >= size[v_sl_idx])
- continue;
- rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
- if (fast_memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
- continue;
-
- ++index[v_sl_idx];
- ++n_listing;
- smartlist_add(matching_descs, rs);
- if (rs->version && rs->version[0])
- smartlist_add(versions, rs->version);
-
- for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
- if (rs->flags & (U64_LITERAL(1) << i))
- ++flag_counts[flag_map[v_sl_idx][i]];
- }
- if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
- if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
- log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
- chosen_name, rs->status.nickname);
- naming_conflict = 1;
- }
- chosen_name = rs->status.nickname;
- }
-
- if (rs->status.has_measured_bw)
- measured_bws[num_mbws++] = rs->status.measured_bw;
- if (rs->status.has_bandwidth)
- bandwidths[num_bandwidths++] = rs->status.bandwidth;
- } SMARTLIST_FOREACH_END(v);
-
- if (n_listing <= total_authorities/2)
- continue;
-
- memset(microdesc_digest, 0, sizeof(microdesc_digest));
- rs = compute_routerstatus_consensus(matching_descs, consensus_method,
- microdesc_digest);
-
- tor_assert(fast_memeq(lowest_id, rs->status.identity_digest,DIGEST_LEN));
- memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
- memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
- DIGEST_LEN);
- rs_out.addr = rs->status.addr;
- rs_out.published_on = rs->status.published_on;
- rs_out.dir_port = rs->status.dir_port;
- rs_out.or_port = rs->status.or_port;
- rs_out.has_bandwidth = 0;
- rs_out.has_exitsummary = 0;
- if (chosen_name && !naming_conflict) {
- strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
- } else {
- strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
- }
- if (consensus_method == 1) {
- is_named = chosen_named_idx >= 0 &&
- (!naming_conflict && flag_counts[chosen_named_idx]);
- } else {
- const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
- if (!d) {
- is_named = is_unnamed = 0;
- } else if (fast_memeq(d, lowest_id, DIGEST_LEN)) {
- is_named = 1; is_unnamed = 0;
- } else {
- is_named = 0; is_unnamed = 1;
- }
- }
-
- smartlist_add(chosen_flags, (char*)"s");
- SMARTLIST_FOREACH(flags, const char *, fl,
- {
- if (!strcmp(fl, "Named")) {
- if (is_named)
- smartlist_add(chosen_flags, (char*)fl);
- } else if (!strcmp(fl, "Unnamed") && consensus_method >= 2) {
- if (is_unnamed)
- smartlist_add(chosen_flags, (char*)fl);
- } else {
- if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
- smartlist_add(chosen_flags, (char*)fl);
- if (!strcmp(fl, "Exit"))
- is_exit = 1;
- else if (!strcmp(fl, "Guard"))
- is_guard = 1;
- else if (!strcmp(fl, "Running"))
- is_running = 1;
- else if (!strcmp(fl, "BadExit"))
- is_bad_exit = 1;
- }
- }
- });
-
- if (consensus_method >= 4 && !is_running)
- continue;
-
- if (smartlist_len(versions)) {
- sort_version_list(versions, 0);
- chosen_version = get_most_frequent_member(versions);
- } else {
- chosen_version = NULL;
- }
-
- if (consensus_method >= 6 && num_mbws > 2) {
- rs_out.has_bandwidth = 1;
- rs_out.bandwidth = median_uint32(measured_bws, num_mbws);
- } else if (consensus_method >= 5 && num_bandwidths > 0) {
- rs_out.has_bandwidth = 1;
- rs_out.bandwidth = median_uint32(bandwidths, num_bandwidths);
- }
-
- if (consensus_method >= 11) {
- is_exit = is_exit && !is_bad_exit;
- }
- if (consensus_method >= MIN_METHOD_FOR_BW_WEIGHTS) {
- if (rs_out.has_bandwidth) {
- T += rs_out.bandwidth;
- if (is_exit && is_guard)
- D += rs_out.bandwidth;
- else if (is_exit)
- E += rs_out.bandwidth;
- else if (is_guard)
- G += rs_out.bandwidth;
- else
- M += rs_out.bandwidth;
- } else {
- log_warn(LD_BUG, "Missing consensus bandwidth for router %s",
- rs_out.nickname);
- }
- }
-
- if (consensus_method >= 5) {
-
- const char *chosen_exitsummary = NULL;
- smartlist_clear(exitsummaries);
- SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
-
- tor_assert(fast_memeq(rs_out.identity_digest,
- vsr->status.identity_digest,
- DIGEST_LEN));
- if (vsr->status.has_exitsummary &&
- fast_memeq(rs_out.descriptor_digest,
- vsr->status.descriptor_digest,
- DIGEST_LEN)) {
- tor_assert(vsr->status.exitsummary);
- smartlist_add(exitsummaries, vsr->status.exitsummary);
- if (!chosen_exitsummary) {
- chosen_exitsummary = vsr->status.exitsummary;
- } else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
-
- exitsummary_disagreement = 1;
- }
- }
- });
- if (exitsummary_disagreement) {
- char id[HEX_DIGEST_LEN+1];
- char dd[HEX_DIGEST_LEN+1];
- base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
- base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
- log_warn(LD_DIR, "The voters disagreed on the exit policy summary "
- " for router %s with descriptor %s. This really shouldn't"
- " have happened.", id, dd);
- smartlist_sort_strings(exitsummaries);
- chosen_exitsummary = get_most_frequent_member(exitsummaries);
- } else if (!chosen_exitsummary) {
- char id[HEX_DIGEST_LEN+1];
- char dd[HEX_DIGEST_LEN+1];
- base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
- base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
- log_warn(LD_DIR, "Not one of the voters that made us select"
- "descriptor %s for router %s had an exit policy"
- "summary", dd, id);
-
- smartlist_clear(exitsummaries);
- SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
- if (vsr->status.has_exitsummary)
- smartlist_add(exitsummaries, vsr->status.exitsummary);
- });
- smartlist_sort_strings(exitsummaries);
- chosen_exitsummary = get_most_frequent_member(exitsummaries);
- if (!chosen_exitsummary)
- log_warn(LD_DIR, "Wow, not one of the voters had an exit "
- "policy summary for %s. Wow.", id);
- }
- if (chosen_exitsummary) {
- rs_out.has_exitsummary = 1;
-
- rs_out.exitsummary = (char *)chosen_exitsummary;
- }
- }
- {
- char buf[4096];
-
-
- routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL,
- rs_format);
- smartlist_add(chunks, tor_strdup(buf));
- }
-
- if (flavor == FLAV_MICRODESC &&
- !tor_digest256_is_zero(microdesc_digest)) {
- char m[BASE64_DIGEST256_LEN+1], *cp;
- digest256_to_base64(m, microdesc_digest);
- tor_asprintf(&cp, "m %s\n", m);
- smartlist_add(chunks, cp);
- }
-
- smartlist_add(chunks,
- smartlist_join_strings(chosen_flags, " ", 0, NULL));
-
- if (chosen_version) {
- smartlist_add(chunks, tor_strdup("\nv "));
- smartlist_add(chunks, tor_strdup(chosen_version));
- }
- smartlist_add(chunks, tor_strdup("\n"));
-
- if (rs_out.has_bandwidth) {
- char *cp=NULL;
- tor_asprintf(&cp, "w Bandwidth=%d\n", rs_out.bandwidth);
- smartlist_add(chunks, cp);
- }
-
- if (rs_out.has_exitsummary && flavor == FLAV_NS) {
- tor_asprintf(&buf, "p %s\n", rs_out.exitsummary);
- smartlist_add(chunks, buf);
- }
-
- }
- tor_free(index);
- tor_free(size);
- tor_free(n_voter_flags);
- tor_free(n_flag_voters);
- for (i = 0; i < smartlist_len(votes); ++i)
- tor_free(flag_map[i]);
- tor_free(flag_map);
- tor_free(flag_counts);
- tor_free(named_flag);
- tor_free(unnamed_flag);
- strmap_free(name_to_id_map, NULL);
- smartlist_free(matching_descs);
- smartlist_free(chosen_flags);
- smartlist_free(versions);
- smartlist_free(exitsummaries);
- tor_free(bandwidths);
- tor_free(measured_bws);
- }
- if (consensus_method >= MIN_METHOD_FOR_FOOTER) {
-
- smartlist_add(chunks, tor_strdup("directory-footer\n"));
- }
- if (consensus_method >= MIN_METHOD_FOR_BW_WEIGHTS) {
- int64_t weight_scale = BW_WEIGHT_SCALE;
- char *bw_weight_param = NULL;
-
-
-
- if (params) {
- if (strcmpstart(params, "bwweightscale=") == 0)
- bw_weight_param = params;
- else
- bw_weight_param = strstr(params, " bwweightscale=");
- }
- if (bw_weight_param) {
- int ok=0;
- char *eq = strchr(bw_weight_param, '=');
- if (eq) {
- weight_scale = tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok,
- NULL);
- if (!ok) {
- log_warn(LD_DIR, "Bad element '%s' in bw weight param",
- escaped(bw_weight_param));
- weight_scale = BW_WEIGHT_SCALE;
- }
- } else {
- log_warn(LD_DIR, "Bad element '%s' in bw weight param",
- escaped(bw_weight_param));
- weight_scale = BW_WEIGHT_SCALE;
- }
- }
- if (consensus_method < 10) {
- networkstatus_compute_bw_weights_v9(chunks, G, M, E, D, T, weight_scale);
- added_weights = 1;
- } else {
- added_weights = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D,
- T, weight_scale);
- }
- }
-
- {
- char digest[DIGEST256_LEN];
- char fingerprint[HEX_DIGEST_LEN+1];
- char signing_key_fingerprint[HEX_DIGEST_LEN+1];
- digest_algorithm_t digest_alg =
- flavor == FLAV_NS ? DIGEST_SHA1 : DIGEST_SHA256;
- size_t digest_len =
- flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
- const char *algname = crypto_digest_algorithm_get_name(digest_alg);
- char *buf = NULL;
- char sigbuf[4096];
- smartlist_add(chunks, tor_strdup("directory-signature "));
-
- hash_list_members(digest, digest_len, chunks, digest_alg);
-
- crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
- crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
-
- if (flavor == FLAV_NS) {
- tor_asprintf(&buf, "%s %s\n", fingerprint,
- signing_key_fingerprint);
- } else {
- tor_asprintf(&buf, "%s %s %s\n",
- algname, fingerprint,
- signing_key_fingerprint);
- }
- smartlist_add(chunks, buf);
-
- sigbuf[0] = '\0';
- if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
- digest, digest_len,
- signing_key)) {
- log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
- return NULL;
- }
- smartlist_add(chunks, tor_strdup(sigbuf));
- if (legacy_id_key_digest && legacy_signing_key && consensus_method >= 3) {
- smartlist_add(chunks, tor_strdup("directory-signature "));
- base16_encode(fingerprint, sizeof(fingerprint),
- legacy_id_key_digest, DIGEST_LEN);
- crypto_pk_get_fingerprint(legacy_signing_key,
- signing_key_fingerprint, 0);
- if (flavor == FLAV_NS) {
- tor_asprintf(&buf, "%s %s\n", fingerprint,
- signing_key_fingerprint);
- } else {
- tor_asprintf(&buf, "%s %s %s\n",
- algname, fingerprint,
- signing_key_fingerprint);
- }
- smartlist_add(chunks, buf);
- sigbuf[0] = '\0';
- if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
- digest, digest_len,
- legacy_signing_key)) {
- log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
- return NULL;
- }
- smartlist_add(chunks, tor_strdup(sigbuf));
- }
- }
- result = smartlist_join_strings(chunks, "", 0, NULL);
- tor_free(client_versions);
- tor_free(server_versions);
- SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
- smartlist_free(flags);
- SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
- smartlist_free(chunks);
- {
- networkstatus_t *c;
- if (!(c = networkstatus_parse_vote_from_string(result, NULL,
- NS_TYPE_CONSENSUS))) {
- log_err(LD_BUG, "Generated a networkstatus consensus we couldn't "
- "parse.");
- tor_free(result);
- return NULL;
- }
-
- if (consensus_method >= MIN_METHOD_FOR_BW_WEIGHTS && added_weights) {
- networkstatus_verify_bw_weights(c);
- }
- networkstatus_vote_free(c);
- }
- return result;
- }
- int
- networkstatus_add_detached_signatures(networkstatus_t *target,
- ns_detached_signatures_t *sigs,
- const char **msg_out)
- {
- int r = 0;
- const char *flavor;
- smartlist_t *siglist;
- tor_assert(sigs);
- tor_assert(target);
- tor_assert(target->type == NS_TYPE_CONSENSUS);
- flavor = networkstatus_get_flavor_name(target->flavor);
-
- if (target->valid_after != sigs->valid_after) {
- *msg_out = "Valid-After times do not match "
- "when adding detached signatures to consensus";
- return -1;
- }
- if (target->fresh_until != sigs->fresh_until) {
- *msg_out = "Fresh-until times do not match "
- "when adding detached signatures to consensus";
- return -1;
- }
- if (target->valid_until != sigs->valid_until) {
- *msg_out = "Valid-until times do not match "
- "when adding detached signatures to consensus";
- return -1;
- }
- siglist = strmap_get(sigs->signatures, flavor);
- if (!siglist) {
- *msg_out = "No signatures for given consensus flavor";
- return -1;
- }
-
- {
- digests_t *digests = strmap_get(sigs->digests, flavor);
- int n_matches = 0;
- digest_algorithm_t alg;
- if (!digests) {
- *msg_out = "No digests for given consensus flavor";
- return -1;
- }
- for (alg = DIGEST_SHA1; alg < N_DIGEST_ALGORITHMS; ++alg) {
- if (!tor_mem_is_zero(digests->d[alg], DIGEST256_LEN)) {
- if (fast_memeq(target->digests.d[alg], digests->d[alg],
- DIGEST256_LEN)) {
- ++n_matches;
- } else {
- *msg_out = "Mismatched digest.";
- return -1;
- }
- }
- }
- if (!n_matches) {
- *msg_out = "No regognized digests for given consensus flavor";
- }
- }
-
- SMARTLIST_FOREACH_BEGIN(siglist, document_signature_t *, sig) {
- char voter_identity[HEX_DIGEST_LEN+1];
- networkstatus_voter_info_t *target_voter =
- networkstatus_get_voter_by_id(target, sig->identity_digest);
- authority_cert_t *cert = NULL;
- const char *algorithm;
- document_signature_t *old_sig = NULL;
- algorithm = crypto_digest_algorithm_get_name(sig->alg);
- base16_encode(voter_identity, sizeof(voter_identity),
- sig->identity_digest, DIGEST_LEN);
- log_info(LD_DIR, "Looking at signature from %s using %s", voter_identity,
- algorithm);
-
- if (!target_voter) {
- log_info(LD_DIR, "We do not know any voter with ID %s", voter_identity);
- continue;
- }
- old_sig = voter_get_sig_by_algorithm(target_voter, sig->alg);
-
- if (old_sig && old_sig->good_signature) {
- log_info(LD_DIR, "We already have a good signature from %s using %s",
- voter_identity, algorithm);
- continue;
- }
-
- if (!sig->good_signature && !sig->bad_signature) {
- cert = authority_cert_get_by_digests(sig->identity_digest,
- sig->signing_key_digest);
- if (cert)
- networkstatus_check_document_signature(target, sig, cert);
- }
-
- if (sig->good_signature || !old_sig || old_sig->bad_signature) {
- log_info(LD_DIR, "Adding signature from %s with %s", voter_identity,
- algorithm);
- ++r;
- if (old_sig) {
- smartlist_remove(target_voter->sigs, old_sig);
- document_signature_free(old_sig);
- }
- smartlist_add(target_voter->sigs, document_signature_dup(sig));
- } else {
- log_info(LD_DIR, "Not adding signature from %s", voter_identity);
- }
- } SMARTLIST_FOREACH_END(sig);
- return r;
- }
- static char *
- networkstatus_format_signatures(networkstatus_t *consensus,
- int for_detached_signatures)
- {
- smartlist_t *elements;
- char buf[4096];
- char *result = NULL;
- int n_sigs = 0;
- const consensus_flavor_t flavor = consensus->flavor;
- const char *flavor_name = networkstatus_get_flavor_name(flavor);
- const char *keyword;
- if (for_detached_signatures && flavor != FLAV_NS)
- keyword = "additional-signature";
- else
- keyword = "directory-signature";
- elements = smartlist_create();
- SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *, v) {
- SMARTLIST_FOREACH_BEGIN(v->sigs, document_signature_t *, sig) {
- char sk[HEX_DIGEST_LEN+1];
- char id[HEX_DIGEST_LEN+1];
- if (!sig->signature || sig->bad_signature)
- continue;
- ++n_sigs;
- base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
- base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
- if (flavor == FLAV_NS) {
- tor_snprintf(buf, sizeof(buf),
- "%s %s %s\n-----BEGIN SIGNATURE-----\n",
- keyword, id, sk);
- } else {
- const char *digest_name =
- crypto_digest_algorithm_get_name(sig->alg);
- tor_snprintf(buf, sizeof(buf),
- "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
- keyword,
- for_detached_signatures ? " " : "",
- for_detached_signatures ? flavor_name : "",
- digest_name, id, sk);
- }
- smartlist_add(elements, tor_strdup(buf));
- base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len);
- strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
- smartlist_add(elements, tor_strdup(buf));
- } SMARTLIST_FOREACH_END(sig);
- } SMARTLIST_FOREACH_END(v);
- result = smartlist_join_strings(elements, "", 0, NULL);
- SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
- smartlist_free(elements);
- if (!n_sigs)
- tor_free(result);
- return result;
- }
- char *
- networkstatus_get_detached_signatures(smartlist_t *consensuses)
- {
- smartlist_t *elements;
- char buf[4096];
- char *result = NULL, *sigs = NULL;
- networkstatus_t *consensus_ns = NULL;
- tor_assert(consensuses);
- SMARTLIST_FOREACH(consensuses, networkstatus_t *, ns, {
- tor_assert(ns);
- tor_assert(ns->type == NS_TYPE_CONSENSUS);
- if (ns && ns->flavor == FLAV_NS)
- consensus_ns = ns;
- });
- if (!consensus_ns) {
- log_warn(LD_BUG, "No NS consensus given.");
- return NULL;
- }
- elements = smartlist_create();
- {
- char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
- vu_buf[ISO_TIME_LEN+1];
- char d[HEX_DIGEST_LEN+1];
- base16_encode(d, sizeof(d),
- consensus_ns->digests.d[DIGEST_SHA1], DIGEST_LEN);
- format_iso_time(va_buf, consensus_ns->valid_after);
- format_iso_time(fu_buf, consensus_ns->fresh_until);
- format_iso_time(vu_buf, consensus_ns->valid_until);
- tor_snprintf(buf, sizeof(buf),
- "consensus-digest %s\n"
- "valid-after %s\n"
- "fresh-until %s\n"
- "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
- smartlist_add(elements, tor_strdup(buf));
- }
-
- SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
- const char *flavor_name = networkstatus_get_flavor_name(ns->flavor);
- int alg;
- if (ns->flavor == FLAV_NS)
- continue;
-
- for (alg = DIGEST_SHA256; alg < N_DIGEST_ALGORITHMS; ++alg) {
- char d[HEX_DIGEST256_LEN+1];
- const char *alg_name =
- crypto_digest_algorithm_get_name(alg);
- if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
- continue;
- base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN);
- tor_snprintf(buf, sizeof(buf), "additional-digest %s %s %s\n",
- flavor_name, alg_name, d);
- smartlist_add(elements, tor_strdup(buf));
- }
- } SMARTLIST_FOREACH_END(ns);
-
- SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
- char *sigs;
- if (ns->flavor == FLAV_NS)
- continue;
- sigs = networkstatus_format_signatures(ns, 1);
- if (!sigs) {
- log_warn(LD_DIR, "Couldn't format signatures");
- goto err;
- }
- smartlist_add(elements, sigs);
- } SMARTLIST_FOREACH_END(ns);
-
- sigs = networkstatus_format_signatures(consensus_ns, 1);
- if (!sigs)
- goto err;
- smartlist_add(elements, sigs);
- result = smartlist_join_strings(elements, "", 0, NULL);
- err:
- SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
- smartlist_free(elements);
- return result;
- }
- static char *
- get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
- int n_flavors)
- {
- int flav;
- char *signatures;
- smartlist_t *c = smartlist_create();
- for (flav = 0; flav < n_flavors; ++flav) {
- if (pending[flav].consensus)
- smartlist_add(c, pending[flav].consensus);
- }
- signatures = networkstatus_get_detached_signatures(c);
- smartlist_free(c);
- return signatures;
- }
- void
- ns_detached_signatures_free(ns_detached_signatures_t *s)
- {
- if (!s)
- return;
- if (s->signatures) {
- STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
- SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
- document_signature_free(sig));
- smartlist_free(sigs);
- } STRMAP_FOREACH_END;
- strmap_free(s->signatures, NULL);
- strmap_free(s->digests, _tor_free);
- }
- tor_free(s);
- }
- authority_cert_t *
- authority_cert_dup(authority_cert_t *cert)
- {
- authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
- tor_assert(cert);
- memcpy(out, cert, sizeof(authority_cert_t));
-
- out->cache_info.signed_descriptor_body =
- tor_strndup(cert->cache_info.signed_descriptor_body,
- cert->cache_info.signed_descriptor_len);
- out->cache_info.saved_location = SAVED_NOWHERE;
- out->identity_key = crypto_pk_dup_key(cert->identity_key);
- out->signing_key = crypto_pk_dup_key(cert->signing_key);
- return out;
- }
- void
- dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
- {
- or_options_t *options = get_options();
- tor_assert(timing_out);
- timing_out->vote_interval = options->V3AuthVotingInterval;
- timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
- timing_out->vote_delay = options->V3AuthVoteDelay;
- timing_out->dist_delay = options->V3AuthDistDelay;
- }
- time_t
- dirvote_get_start_of_next_interval(time_t now, int interval)
- {
- struct tm tm;
- time_t midnight_today;
- time_t midnight_tomorrow;
- time_t next;
- tor_gmtime_r(&now, &tm);
- tm.tm_hour = 0;
- tm.tm_min = 0;
- tm.tm_sec = 0;
- midnight_today = tor_timegm(&tm);
- midnight_tomorrow = midnight_today + (24*60*60);
- next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
-
- if (next > midnight_tomorrow)
- next = midnight_tomorrow;
-
- if (next + interval/2 > midnight_tomorrow)
- next = midnight_tomorrow;
- return next;
- }
- static struct {
-
- time_t voting_starts;
-
- time_t fetch_missing_votes;
-
- time_t voting_ends;
-
- time_t fetch_missing_signatures;
-
- time_t interval_starts;
-
- int have_voted;
-
- int have_fetched_missing_votes;
-
- int have_built_consensus;
-
- int have_fetched_missing_signatures;
-
- int have_published_consensus;
- } voting_schedule = {0,0,0,0,0,0,0,0,0,0};
- void
- dirvote_recalculate_timing(or_options_t *options, time_t now)
- {
- int interval, vote_delay, dist_delay;
- time_t start;
- time_t end;
- networkstatus_t *consensus;
- if (!authdir_mode_v3(options))
- return;
- consensus = networkstatus_get_live_consensus(now);
- memset(&voting_schedule, 0, sizeof(voting_schedule));
- if (consensus) {
- interval = (int)( consensus->fresh_until - consensus->valid_after );
- vote_delay = consensus->vote_seconds;
- dist_delay = consensus->dist_seconds;
- } else {
- interval = options->TestingV3AuthInitialVotingInterval;
- vote_delay = options->TestingV3AuthInitialVoteDelay;
- dist_delay = options->TestingV3AuthInitialDistDelay;
- }
- tor_assert(interval > 0);
- if (vote_delay + dist_delay > interval/2)
- vote_delay = dist_delay = interval / 4;
- start = voting_schedule.interval_starts =
- dirvote_get_start_of_next_interval(now,interval);
- end = dirvote_get_start_of_next_interval(start+1, interval);
- tor_assert(end > start);
- voting_schedule.fetch_missing_signatures = start - (dist_delay/2);
- voting_schedule.voting_ends = start - dist_delay;
- voting_schedule.fetch_missing_votes = start - dist_delay - (vote_delay/2);
- voting_schedule.voting_starts = start - dist_delay - vote_delay;
- {
- char tbuf[ISO_TIME_LEN+1];
- format_iso_time(tbuf, voting_schedule.interval_starts);
- log_notice(LD_DIR,"Choosing expected valid-after time as %s: "
- "consensus_set=%d, interval=%d",
- tbuf, consensus?1:0, interval);
- }
- }
- void
- dirvote_act(or_options_t *options, time_t now)
- {
- if (!authdir_mode_v3(options))
- return;
- if (!voting_schedule.voting_starts) {
- char *keys = list_v3_auth_ids();
- authority_cert_t *c = get_my_v3_authority_cert();
- log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
- "Mine is %s.",
- keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
- tor_free(keys);
- dirvote_recalculate_timing(options, now);
- }
- if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
- log_notice(LD_DIR, "Time to vote.");
- dirvote_perform_vote();
- voting_schedule.have_voted = 1;
- }
- if (voting_schedule.fetch_missing_votes < now &&
- !voting_schedule.have_fetched_missing_votes) {
- log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
- dirvote_fetch_missing_votes();
- voting_schedule.have_fetched_missing_votes = 1;
- }
- if (voting_schedule.voting_ends < now &&
- !voting_schedule.have_built_consensus) {
- log_notice(LD_DIR, "Time to compute a consensus.");
- dirvote_compute_consensuses();
-
- voting_schedule.have_built_consensus = 1;
- }
- if (voting_schedule.fetch_missing_signatures < now &&
- !voting_schedule.have_fetched_missing_signatures) {
- log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
- dirvote_fetch_missing_signatures();
- voting_schedule.have_fetched_missing_signatures = 1;
- }
- if (voting_schedule.interval_starts < now &&
- !voting_schedule.have_published_consensus) {
- log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
- dirvote_publish_consensus();
- dirvote_clear_votes(0);
- voting_schedule.have_published_consensus = 1;
-
- dirvote_recalculate_timing(options, now);
- }
- }
- typedef struct pending_vote_t {
- cached_dir_t *vote_body;
- networkstatus_t *vote;
- } pending_vote_t;
- static smartlist_t *pending_vote_list = NULL;
- static smartlist_t *previous_vote_list = NULL;
- static pending_consensus_t pending_consensuses[N_CONSENSUS_FLAVORS];
- static char *pending_consensus_signatures = NULL;
- static smartlist_t *pending_consensus_signature_list = NULL;
- static int
- dirvote_perform_vote(void)
- {
- crypto_pk_env_t *key = get_my_v3_authority_signing_key();
- authority_cert_t *cert = get_my_v3_authority_cert();
- networkstatus_t *ns;
- char *contents;
- pending_vote_t *pending_vote;
- time_t now = time(NULL);
- int status;
- const char *msg = "";
- if (!cert || !key) {
- log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
- return -1;
- } else if (cert->expires < now) {
- log_warn(LD_NET, "Can't generate v3 vote with expired certificate");
- return -1;
- }
- if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
- return -1;
- contents = format_networkstatus_vote(key, ns);
- networkstatus_vote_free(ns);
- if (!contents)
- return -1;
- pending_vote = dirvote_add_vote(contents, &msg, &status);
- tor_free(contents);
- if (!pending_vote) {
- log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
- msg);
- return -1;
- }
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
- ROUTER_PURPOSE_GENERAL,
- V3_AUTHORITY,
- pending_vote->vote_body->dir,
- pending_vote->vote_body->dir_len, 0);
- log_notice(LD_DIR, "Vote posted.");
- return 0;
- }
- static void
- dirvote_fetch_missing_votes(void)
- {
- smartlist_t *missing_fps = smartlist_create();
- char *resource;
- SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
- trusted_dir_server_t *, ds,
- {
- if (!(ds->type & V3_AUTHORITY))
- continue;
- if (!dirvote_get_vote(ds->v3_identity_digest,
- DGV_BY_ID|DGV_INCLUDE_PENDING)) {
- char *cp = tor_malloc(HEX_DIGEST_LEN+1);
- base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
- DIGEST_LEN);
- smartlist_add(missing_fps, cp);
- }
- });
- if (!smartlist_len(missing_fps)) {
- smartlist_free(missing_fps);
- return;
- }
- log_notice(LOG_NOTICE, "We're missing votes from %d authorities. Asking "
- "every other authority for a copy.", smartlist_len(missing_fps));
- resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
- directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
- 0, resource);
- tor_free(resource);
- SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
- smartlist_free(missing_fps);
- }
- static void
- dirvote_fetch_missing_signatures(void)
- {
- int need_any = 0;
- int i;
- for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
- networkstatus_t *consensus = pending_consensuses[i].consensus;
- if (!consensus ||
- networkstatus_check_consensus_signature(consensus, -1) == 1) {
-
- continue;
- }
- need_any = 1;
- }
- if (!need_any)
- return;
- directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
- 0, NULL);
- }
- static void
- dirvote_clear_pending_consensuses(void)
- {
- int i;
- for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
- pending_consensus_t *pc = &pending_consensuses[i];
- tor_free(pc->body);
- networkstatus_vote_free(pc->consensus);
- pc->consensus = NULL;
- }
- }
- static void
- dirvote_clear_votes(int all_votes)
- {
- if (!previous_vote_list)
- previous_vote_list = smartlist_create();
- if (!pending_vote_list)
- pending_vote_list = smartlist_create();
-
- SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
- cached_dir_decref(v->vote_body);
- v->vote_body = NULL;
- networkstatus_vote_free(v->vote);
- tor_free(v);
- });
- smartlist_clear(previous_vote_list);
- if (all_votes) {
-
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
- cached_dir_decref(v->vote_body);
- v->vote_body = NULL;
- networkstatus_vote_free(v->vote);
- tor_free(v);
- });
- } else {
-
- smartlist_add_all(previous_vote_list, pending_vote_list);
- }
- smartlist_clear(pending_vote_list);
- if (pending_consensus_signature_list) {
- SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
- tor_free(cp));
- smartlist_clear(pending_consensus_signature_list);
- }
- tor_free(pending_consensus_signatures);
- dirvote_clear_pending_consensuses();
- }
- static char *
- list_v3_auth_ids(void)
- {
- smartlist_t *known_v3_keys = smartlist_create();
- char *keys;
- SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
- trusted_dir_server_t *, ds,
- if ((ds->type & V3_AUTHORITY) &&
- !tor_digest_is_zero(ds->v3_identity_digest))
- smartlist_add(known_v3_keys,
- tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
- keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
- SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
- smartlist_free(known_v3_keys);
- return keys;
- }
- pending_vote_t *
- dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
- {
- networkstatus_t *vote;
- networkstatus_voter_info_t *vi;
- trusted_dir_server_t *ds;
- pending_vote_t *pending_vote = NULL;
- const char *end_of_vote = NULL;
- int any_failed = 0;
- tor_assert(vote_body);
- tor_assert(msg_out);
- tor_assert(status_out);
- if (!pending_vote_list)
- pending_vote_list = smartlist_create();
- *status_out = 0;
- *msg_out = NULL;
- again:
- vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote,
- NS_TYPE_VOTE);
- if (!end_of_vote)
- end_of_vote = vote_body + strlen(vote_body);
- if (!vote) {
- log_warn(LD_DIR, "Couldn't parse vote: length was %d",
- (int)strlen(vote_body));
- *msg_out = "Unable to parse vote";
- goto err;
- }
- tor_assert(smartlist_len(vote->voters) == 1);
- vi = get_voter(vote);
- {
- int any_sig_good = 0;
- SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
- if (sig->good_signature)
- any_sig_good = 1);
- tor_assert(any_sig_good);
- }
- ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
- if (!ds) {
- char *keys = list_v3_auth_ids();
- log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
- "with authority key ID %s. "
- "This key ID is not recognized. Known v3 key IDs are: %s",
- vi->nickname, vi->address,
- hex_str(vi->identity_digest, DIGEST_LEN), keys);
- tor_free(keys);
- *msg_out = "Vote not from a recognized v3 authority";
- goto err;
- }
- tor_assert(vote->cert);
- if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
- vote->cert->signing_key_digest)) {
-
- trusted_dirs_load_certs_from_string(
- vote->cert->cache_info.signed_descriptor_body,
- 0 , 1 );
- if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
- vote->cert->signing_key_digest)) {
- log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
- }
- }
-
- if (vote->valid_after != voting_schedule.interval_starts) {
- char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
- format_iso_time(tbuf1, vote->valid_after);
- format_iso_time(tbuf2, voting_schedule.interval_starts);
- log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
- "we were expecting %s", vi->address, tbuf1, tbuf2);
- *msg_out = "Bad valid-after time";
- goto err;
- }
-
- update_consensus_router_descriptor_downloads(time(NULL), 1, vote);
-
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
- if (fast_memeq(v->vote->cert->cache_info.identity_digest,
- vote->cert->cache_info.identity_digest,
- DIGEST_LEN)) {
- networkstatus_voter_info_t *vi_old = get_voter(v->vote);
- if (fast_memeq(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
-
- log_info(LD_DIR, "Discarding a vote we already have (from %s).",
- vi->address);
- if (*status_out < 200)
- *status_out = 200;
- goto discard;
- } else if (v->vote->published < vote->published) {
- log_notice(LD_DIR, "Replacing an older pending vote from this "
- "directory.");
- cached_dir_decref(v->vote_body);
- networkstatus_vote_free(v->vote);
- v->vote_body = new_cached_dir(tor_strndup(vote_body,
- end_of_vote-vote_body),
- vote->published);
- v->vote = vote;
- if (end_of_vote &&
- !strcmpstart(end_of_vote, "network-status-version"))
- goto again;
- if (*status_out < 200)
- *status_out = 200;
- if (!*msg_out)
- *msg_out = "OK";
- return v;
- } else {
- *msg_out = "Already have a newer pending vote";
- goto err;
- }
- }
- });
- pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
- pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
- end_of_vote-vote_body),
- vote->published);
- pending_vote->vote = vote;
- smartlist_add(pending_vote_list, pending_vote);
- if (!strcmpstart(end_of_vote, "network-status-version ")) {
- vote_body = end_of_vote;
- goto again;
- }
- goto done;
- err:
- any_failed = 1;
- if (!*msg_out)
- *msg_out = "Error adding vote";
- if (*status_out < 400)
- *status_out = 400;
- discard:
- networkstatus_vote_free(vote);
- if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
- vote_body = end_of_vote;
- goto again;
- }
- done:
- if (*status_out < 200)
- *status_out = 200;
- if (!*msg_out) {
- if (!any_failed && !pending_vote) {
- *msg_out = "Duplicate discarded";
- } else {
- *msg_out = "ok";
- }
- }
- return any_failed ? NULL : pending_vote;
- }
- static int
- dirvote_compute_consensuses(void)
- {
-
- int n_votes, n_voters, n_vote_running = 0;
- smartlist_t *votes = NULL, *votestrings = NULL;
- char *consensus_body = NULL, *signatures = NULL, *votefile;
- networkstatus_t *consensus = NULL;
- authority_cert_t *my_cert;
- pending_consensus_t pending[N_CONSENSUS_FLAVORS];
- int flav;
- memset(pending, 0, sizeof(pending));
- if (!pending_vote_list)
- pending_vote_list = smartlist_create();
- n_voters = get_n_authorities(V3_AUTHORITY);
- n_votes = smartlist_len(pending_vote_list);
- if (n_votes <= n_voters/2) {
- log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
- "%d of %d", n_votes, n_voters/2+1);
- goto err;
- }
- tor_assert(pending_vote_list);
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
- if (smartlist_string_isin(v->vote->known_flags, "Running"))
- n_vote_running++;
- });
- if (!n_vote_running) {
-
- log_warn(LD_DIR, "Nobody has voted on the Running flag. Generating "
- "and publishing a consensus without Running nodes "
- "would make many clients stop working. Not "
- "generating a consensus!");
- goto err;
- }
- if (!(my_cert = get_my_v3_authority_cert())) {
- log_warn(LD_DIR, "Can't generate consensus without a certificate.");
- goto err;
- }
- votes = smartlist_create();
- votestrings = smartlist_create();
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
- {
- sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
- c->bytes = v->vote_body->dir;
- c->len = v->vote_body->dir_len;
- smartlist_add(votestrings, c);
- smartlist_add(votes, v->vote);
- });
- votefile = get_datadir_fname("v3-status-votes");
- write_chunks_to_file(votefile, votestrings, 0);
- tor_free(votefile);
- SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
- smartlist_free(votestrings);
- {
- char legacy_dbuf[DIGEST_LEN];
- crypto_pk_env_t *legacy_sign=NULL;
- char *legacy_id_digest = NULL;
- int n_generated = 0;
- if (get_options()->V3AuthUseLegacyKey) {
- authority_cert_t *cert = get_my_v3_legacy_cert();
- legacy_sign = get_my_v3_legacy_signing_key();
- if (cert) {
- if (crypto_pk_get_digest(cert->identity_key, legacy_dbuf)) {
- log_warn(LD_BUG,
- "Unable to compute digest of legacy v3 identity key");
- } else {
- legacy_id_digest = legacy_dbuf;
- }
- }
- }
- for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
- const char *flavor_name = networkstatus_get_flavor_name(flav);
- consensus_body = networkstatus_compute_consensus(
- votes, n_voters,
- my_cert->identity_key,
- get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign,
- flav);
- if (!consensus_body) {
- log_warn(LD_DIR, "Couldn't generate a %s consensus at all!",
- flavor_name);
- continue;
- }
- consensus = networkstatus_parse_vote_from_string(consensus_body, NULL,
- NS_TYPE_CONSENSUS);
- if (!consensus) {
- log_warn(LD_DIR, "Couldn't parse %s consensus we generated!",
- flavor_name);
- tor_free(consensus_body);
- continue;
- }
-
- networkstatus_check_consensus_signature(consensus, -1);
- pending[flav].body = consensus_body;
- pending[flav].consensus = consensus;
- n_generated++;
- consensus_body = NULL;
- consensus = NULL;
- }
- if (!n_generated) {
- log_warn(LD_DIR, "Couldn't generate any consensus flavors at all.");
- goto err;
- }
- }
- signatures = get_detached_signatures_from_pending_consensuses(
- pending, N_CONSENSUS_FLAVORS);
- if (!signatures) {
- log_warn(LD_DIR, "Couldn't extract signatures.");
- goto err;
- }
- dirvote_clear_pending_consensuses();
- memcpy(pending_consensuses, pending, sizeof(pending));
- tor_free(pending_consensus_signatures);
- pending_consensus_signatures = signatures;
- if (pending_consensus_signature_list) {
- int n_sigs = 0;
-
- SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
- {
- const char *msg = NULL;
- int r = dirvote_add_signatures_to_all_pending_consensuses(sig, &msg);
- if (r >= 0)
- n_sigs += r;
- else
- log_warn(LD_DIR,
- "Could not add queued signature to new consensus: %s",
- msg);
- tor_free(sig);
- });
- if (n_sigs)
- log_notice(LD_DIR, "Added %d pending signatures while building "
- "consensus.", n_sigs);
- smartlist_clear(pending_consensus_signature_list);
- }
- log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
- ROUTER_PURPOSE_GENERAL,
- V3_AUTHORITY,
- pending_consensus_signatures,
- strlen(pending_consensus_signatures), 0);
- log_notice(LD_DIR, "Signature(s) posted.");
- smartlist_free(votes);
- return 0;
- err:
- smartlist_free(votes);
- tor_free(consensus_body);
- tor_free(signatures);
- networkstatus_vote_free(consensus);
- return -1;
- }
- static int
- dirvote_add_signatures_to_pending_consensus(
- pending_consensus_t *pc,
- ns_detached_signatures_t *sigs,
- const char **msg_out)
- {
- const char *flavor_name;
- int r = -1;
-
- tor_assert(pc->consensus);
- tor_assert(pc->body);
- tor_assert(pending_consensus_signatures);
- flavor_name = networkstatus_get_flavor_name(pc->consensus->flavor);
- *msg_out = NULL;
- {
- smartlist_t *sig_list = strmap_get(sigs->signatures, flavor_name);
- log_info(LD_DIR, "Have %d signatures for adding to %s consensus.",
- sig_list ? smartlist_len(sig_list) : 0, flavor_name);
- }
- r = networkstatus_add_detached_signatures(pc->consensus, sigs, msg_out);
- log_info(LD_DIR,"Added %d signatures to consensus.", r);
- if (r >= 1) {
- char *new_signatures =
- networkstatus_format_signatures(pc->consensus, 0);
- char *dst, *dst_end;
- size_t new_consensus_len;
- if (!new_signatures) {
- *msg_out = "No signatures to add";
- goto err;
- }
- new_consensus_len =
- strlen(pc->body) + strlen(new_signatures) + 1;
- pc->body = tor_realloc(pc->body, new_consensus_len);
- dst_end = pc->body + new_consensus_len;
- dst = strstr(pc->body, "directory-signature ");
- tor_assert(dst);
- strlcpy(dst, new_signatures, dst_end-dst);
-
- {
- networkstatus_t *v = networkstatus_parse_vote_from_string(
- pc->body, NULL,
- NS_TYPE_CONSENSUS);
- tor_assert(v);
- networkstatus_vote_free(v);
- }
- *msg_out = "Signatures added";
- tor_free(new_signatures);
- } else if (r == 0) {
- *msg_out = "Signatures ignored";
- } else {
- goto err;
- }
- goto done;
- err:
- if (!*msg_out)
- *msg_out = "Unrecognized error while adding detached signatures.";
- done:
- return r;
- }
- static int
- dirvote_add_signatures_to_all_pending_consensuses(
- const char *detached_signatures_body,
- const char **msg_out)
- {
- int r=0, i, n_added = 0, errors = 0;
- ns_detached_signatures_t *sigs;
- tor_assert(detached_signatures_body);
- tor_assert(msg_out);
- tor_assert(pending_consensus_signatures);
- if (!(sigs = networkstatus_parse_detached_signatures(
- detached_signatures_body, NULL))) {
- *msg_out = "Couldn't parse detached signatures.";
- goto err;
- }
- for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
- int res;
- pending_consensus_t *pc = &pending_consensuses[i];
- if (!pc->consensus)
- continue;
- res = dirvote_add_signatures_to_pending_consensus(pc, sigs, msg_out);
- if (res < 0)
- errors++;
- else
- n_added += res;
- }
- if (errors && !n_added) {
- r = -1;
- goto err;
- }
- if (n_added && pending_consensuses[FLAV_NS].consensus) {
- char *new_detached =
- get_detached_signatures_from_pending_consensuses(
- pending_consensuses, N_CONSENSUS_FLAVORS);
- if (new_detached) {
- tor_free(pending_consensus_signatures);
- pending_consensus_signatures = new_detached;
- }
- }
- r = n_added;
- goto done;
- err:
- if (!*msg_out)
- *msg_out = "Unrecognized error while adding detached signatures.";
- done:
- ns_detached_signatures_free(sigs);
-
- return r;
- }
- int
- dirvote_add_signatures(const char *detached_signatures_body,
- const char *source,
- const char **msg)
- {
- if (pending_consensuses[FLAV_NS].consensus) {
- log_notice(LD_DIR, "Got a signature from %s. "
- "Adding it to the pending consensus.", source);
- return dirvote_add_signatures_to_all_pending_consensuses(
- detached_signatures_body, msg);
- } else {
- log_notice(LD_DIR, "Got a signature from %s. "
- "Queuing it for the next consensus.", source);
- if (!pending_consensus_signature_list)
- pending_consensus_signature_list = smartlist_create();
- smartlist_add(pending_consensus_signature_list,
- tor_strdup(detached_signatures_body));
- *msg = "Signature queued";
- return 0;
- }
- }
- static int
- dirvote_publish_consensus(void)
- {
- int i;
-
- for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
- pending_consensus_t *pending = &pending_consensuses[i];
- const char *name;
- name = networkstatus_get_flavor_name(i);
- tor_assert(name);
- if (!pending->consensus ||
- networkstatus_check_consensus_signature(pending->consensus, 1)<0) {
- log_warn(LD_DIR, "Not enough info to publish pending %s consensus",name);
- continue;
- }
- if (networkstatus_set_current_consensus(pending->body, name, 0))
- log_warn(LD_DIR, "Error publishing %s consensus", name);
- else
- log_notice(LD_DIR, "Published %s consensus", name);
- }
- return 0;
- }
- void
- dirvote_free_all(void)
- {
- dirvote_clear_votes(1);
-
- smartlist_free(pending_vote_list);
- pending_vote_list = NULL;
- smartlist_free(previous_vote_list);
- previous_vote_list = NULL;
- dirvote_clear_pending_consensuses();
- tor_free(pending_consensus_signatures);
- if (pending_consensus_signature_list) {
-
- smartlist_free(pending_consensus_signature_list);
- pending_consensus_signature_list = NULL;
- }
- }
- const char *
- dirvote_get_pending_consensus(consensus_flavor_t flav)
- {
- tor_assert(((int)flav) >= 0 && flav < N_CONSENSUS_FLAVORS);
- return pending_consensuses[flav].body;
- }
- const char *
- dirvote_get_pending_detached_signatures(void)
- {
- return pending_consensus_signatures;
- }
- const cached_dir_t *
- dirvote_get_vote(const char *fp, int flags)
- {
- int by_id = flags & DGV_BY_ID;
- const int include_pending = flags & DGV_INCLUDE_PENDING;
- const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
- if (!pending_vote_list && !previous_vote_list)
- return NULL;
- if (fp == NULL) {
- authority_cert_t *c = get_my_v3_authority_cert();
- if (c) {
- fp = c->cache_info.identity_digest;
- by_id = 1;
- } else
- return NULL;
- }
- if (by_id) {
- if (pending_vote_list && include_pending) {
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
- if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
- return pv->vote_body);
- }
- if (previous_vote_list && include_previous) {
- SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
- if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
- return pv->vote_body);
- }
- } else {
- if (pending_vote_list && include_pending) {
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
- if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
- return pv->vote_body);
- }
- if (previous_vote_list && include_previous) {
- SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
- if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
- return pv->vote_body);
- }
- }
- return NULL;
- }
- microdesc_t *
- dirvote_create_microdescriptor(const routerinfo_t *ri)
- {
- microdesc_t *result = NULL;
- char *key = NULL, *summary = NULL, *family = NULL;
- char buf[1024];
- size_t keylen;
- char *out = buf, *end = buf+sizeof(buf);
- if (crypto_pk_write_public_key_to_string(ri->onion_pkey, &key, &keylen)<0)
- goto done;
- summary = policy_summarize(ri->exit_policy);
- if (ri->declared_family)
- family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
- if (tor_snprintf(out, end-out, "onion-key\n%s", key)<0)
- goto done;
- out += strlen(out);
- if (family) {
- if (tor_snprintf(out, end-out, "family %s\n", family)<0)
- goto done;
- out += strlen(out);
- }
- if (summary && strcmp(summary, "reject 1-65535")) {
- if (tor_snprintf(out, end-out, "p %s\n", summary)<0)
- goto done;
- out += strlen(out);
- }
- *out = '\0';
- {
- smartlist_t *lst = microdescs_parse_from_string(buf, out, 0, 1);
- if (smartlist_len(lst) != 1) {
- log_warn(LD_DIR, "We generated a microdescriptor we couldn't parse.");
- SMARTLIST_FOREACH(lst, microdesc_t *, md, microdesc_free(md));
- smartlist_free(lst);
- goto done;
- }
- result = smartlist_get(lst, 0);
- smartlist_free(lst);
- }
- done:
- tor_free(key);
- tor_free(summary);
- tor_free(family);
- return result;
- }
- static char *microdesc_consensus_methods = NULL;
- ssize_t
- dirvote_format_microdesc_vote_line(char *out, size_t out_len,
- const microdesc_t *md)
- {
- char d64[BASE64_DIGEST256_LEN+1];
- if (!microdesc_consensus_methods) {
- microdesc_consensus_methods =
- make_consensus_method_list(MIN_METHOD_FOR_MICRODESC,
- MAX_SUPPORTED_CONSENSUS_METHOD,
- ",");
- tor_assert(microdesc_consensus_methods);
- }
- if (digest256_to_base64(d64, md->digest)<0)
- return -1;
- if (tor_snprintf(out, out_len, "m %s sha256=%s\n",
- microdesc_consensus_methods, d64)<0)
- return -1;
- return strlen(out);
- }
- int
- vote_routerstatus_find_microdesc_hash(char *digest256_out,
- const vote_routerstatus_t *vrs,
- int method,
- digest_algorithm_t alg)
- {
-
- const vote_microdesc_hash_t *h;
- char mstr[64];
- size_t mlen;
- char dstr[64];
- tor_snprintf(mstr, sizeof(mstr), "%d", method);
- mlen = strlen(mstr);
- tor_snprintf(dstr, sizeof(dstr), " %s=",
- crypto_digest_algorithm_get_name(alg));
- for (h = vrs->microdesc; h; h = h->next) {
- const char *cp = h->microdesc_hash_line;
- size_t num_len;
-
- while (1) {
- num_len = strspn(cp, "1234567890");
- if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
-
- char buf[BASE64_DIGEST256_LEN+1];
-
- cp = strstr(cp, dstr);
- if (!cp)
- return -1;
- cp += strlen(dstr);
- strlcpy(buf, cp, sizeof(buf));
- return digest256_from_base64(digest256_out, buf);
- }
- if (num_len == 0 || cp[num_len] != ',')
- break;
- cp += num_len + 1;
- }
- }
- return -1;
- }
|