1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506 |
- const char dirserv_c_id[] =
- "$Id$";
- #include "or.h"
- #define ROUTER_ALLOW_SKEW (60*60*12)
- #define DIR_REGEN_SLACK_TIME 30
- #define MAX_UNTRUSTED_NETWORKSTATUSES 16
- #define MAX_V1_DIRECTORY_AGE (30*24*60*60)
- #define MAX_V1_RR_AGE (7*24*60*60)
- static int the_directory_is_dirty = 1;
- static int runningrouters_is_dirty = 1;
- static int the_v2_networkstatus_is_dirty = 1;
- static void directory_remove_invalid(void);
- static cached_dir_t *dirserv_regenerate_directory(void);
- static char *format_versions_list(config_line_t *ln);
- struct authdir_config_t;
- static int add_fingerprint_to_dir(const char *nickname, const char *fp,
- struct authdir_config_t *list);
- static uint32_t dirserv_router_get_status(const routerinfo_t *router,
- const char **msg);
- static uint32_t
- dirserv_get_status_impl(const char *fp, const char *nickname,
- const char *address,
- uint32_t addr, uint16_t or_port,
- const char *platform, const char *contact,
- const char **msg, int should_log);
- static int dirserv_thinks_router_is_reachable(routerinfo_t *router,
- time_t now);
- static void clear_cached_dir(cached_dir_t *d);
- static int dirserv_add_extrainfo(extrainfo_t *ei, const char **msg);
- #define FP_NAMED 1
- #define FP_INVALID 2
- #define FP_REJECT 4
- #define FP_BADEXIT 8
- typedef struct router_status_t {
- char nickname[MAX_NICKNAME_LEN+1];
- uint32_t status;
- } router_status_t;
- typedef struct authdir_config_t {
- strmap_t *fp_by_name;
- digestmap_t *status_by_digest;
- } authdir_config_t;
- static authdir_config_t *fingerprint_list = NULL;
- static authdir_config_t *
- authdir_config_new(void)
- {
- authdir_config_t *list = tor_malloc_zero(sizeof(authdir_config_t));
- list->fp_by_name = strmap_new();
- list->status_by_digest = digestmap_new();
- return list;
- }
- int
- add_fingerprint_to_dir(const char *nickname, const char *fp,
- authdir_config_t *list)
- {
- char *fingerprint;
- char d[DIGEST_LEN];
- router_status_t *status;
- tor_assert(nickname);
- tor_assert(fp);
- tor_assert(list);
- fingerprint = tor_strdup(fp);
- tor_strstrip(fingerprint, " ");
- if (base16_decode(d, DIGEST_LEN, fingerprint, strlen(fingerprint))) {
- log_warn(LD_DIRSERV, "Couldn't decode fingerprint \"%s\"",
- escaped(fp));
- tor_free(fingerprint);
- return 0;
- }
- if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME)) {
- log_warn(LD_DIRSERV, "Tried to add a mapping for reserved nickname %s",
- UNNAMED_ROUTER_NICKNAME);
- tor_free(fingerprint);
- return 0;
- }
- status = digestmap_get(list->status_by_digest, d);
- if (!status) {
- status = tor_malloc_zero(sizeof(router_status_t));
- digestmap_set(list->status_by_digest, d, status);
- }
- if (nickname[0] != '!') {
- char *old_fp = strmap_get_lc(list->fp_by_name, nickname);
- if (old_fp && !strcasecmp(fingerprint, old_fp)) {
- tor_free(fingerprint);
- } else {
- tor_free(old_fp);
- strmap_set_lc(list->fp_by_name, nickname, fingerprint);
- }
- status->status |= FP_NAMED;
- strlcpy(status->nickname, nickname, sizeof(status->nickname));
- } else {
- tor_free(fingerprint);
- if (!strcasecmp(nickname, "!reject")) {
- status->status |= FP_REJECT;
- } else if (!strcasecmp(nickname, "!invalid")) {
- status->status |= FP_INVALID;
- } else if (!strcasecmp(nickname, "!badexit")) {
- status->status |= FP_BADEXIT;
- }
- }
- return 0;
- }
- int
- dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk)
- {
- char fp[FINGERPRINT_LEN+1];
- if (crypto_pk_get_fingerprint(pk, fp, 0)<0) {
- log_err(LD_BUG, "Error computing fingerprint");
- return -1;
- }
- if (!fingerprint_list)
- fingerprint_list = authdir_config_new();
- add_fingerprint_to_dir(nickname, fp, fingerprint_list);
- return 0;
- }
- int
- dirserv_load_fingerprint_file(void)
- {
- char fname[512];
- char *cf;
- char *nickname, *fingerprint;
- authdir_config_t *fingerprint_list_new;
- int result;
- config_line_t *front=NULL, *list;
- or_options_t *options = get_options();
- tor_snprintf(fname, sizeof(fname),
- "%s/approved-routers", options->DataDirectory);
- log_info(LD_GENERAL,
- "Reloading approved fingerprints from \"%s\"...", fname);
- cf = read_file_to_str(fname, 0, NULL);
- if (!cf) {
- if (options->NamingAuthoritativeDir) {
- log_warn(LD_FS, "Cannot open fingerprint file '%s'. Failing.", fname);
- return -1;
- } else {
- log_info(LD_FS, "Cannot open fingerprint file '%s'. Returning.", fname);
- return 0;
- }
- }
- result = config_get_lines(cf, &front);
- tor_free(cf);
- if (result < 0) {
- log_warn(LD_CONFIG, "Error reading from fingerprint file");
- return -1;
- }
- fingerprint_list_new = authdir_config_new();
- for (list=front; list; list=list->next) {
- nickname = list->key; fingerprint = list->value;
- if (strlen(nickname) > MAX_NICKNAME_LEN) {
- log_notice(LD_CONFIG,
- "Nickname '%s' too long in fingerprint file. Skipping.",
- nickname);
- continue;
- }
- if (!is_legal_nickname(nickname) &&
- strcasecmp(nickname, "!reject") &&
- strcasecmp(nickname, "!invalid") &&
- strcasecmp(nickname, "!badexit")) {
- log_notice(LD_CONFIG,
- "Invalid nickname '%s' in fingerprint file. Skipping.",
- nickname);
- continue;
- }
- if (strlen(fingerprint) != FINGERPRINT_LEN ||
- !crypto_pk_check_fingerprint_syntax(fingerprint)) {
- log_notice(LD_CONFIG,
- "Invalid fingerprint (nickname '%s', "
- "fingerprint %s). Skipping.",
- nickname, fingerprint);
- continue;
- }
- if (0==strcasecmp(nickname, DEFAULT_CLIENT_NICKNAME)) {
-
- log_notice(LD_CONFIG,
- "Authorizing nickname '%s' would break "
- "many clients; skipping.",
- DEFAULT_CLIENT_NICKNAME);
- continue;
- }
- if (0==strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME)) {
-
- log_notice(LD_CONFIG,
- "Authorizing nickname '%s' is not allowed; skipping.",
- UNNAMED_ROUTER_NICKNAME);
- continue;
- }
- if (add_fingerprint_to_dir(nickname, fingerprint, fingerprint_list_new)
- != 0)
- log_notice(LD_CONFIG, "Duplicate nickname '%s'.", nickname);
- }
- config_free_lines(front);
- dirserv_free_fingerprint_list();
- fingerprint_list = fingerprint_list_new;
-
- directory_remove_invalid();
- return 0;
- }
- static uint32_t
- dirserv_router_get_status(const routerinfo_t *router, const char **msg)
- {
- char d[DIGEST_LEN];
- if (crypto_pk_get_digest(router->identity_pkey, d)) {
- log_warn(LD_BUG,"Error computing fingerprint");
- if (msg)
- *msg = "Bug: Error computing fingerprint";
- return FP_REJECT;
- }
- return dirserv_get_status_impl(d, router->nickname,
- router->address,
- router->addr, router->or_port,
- router->platform, router->contact_info,
- msg, 1);
- }
- int
- dirserv_would_reject_router(routerstatus_t *rs)
- {
- uint32_t res;
- res = dirserv_get_status_impl(rs->identity_digest, rs->nickname,
- "",
- rs->addr, rs->or_port,
- NULL, NULL,
- NULL, 0);
- return (res & FP_REJECT) != 0;
- }
- static uint32_t
- dirserv_get_status_impl(const char *id_digest, const char *nickname,
- const char *address,
- uint32_t addr, uint16_t or_port,
- const char *platform, const char *contact,
- const char **msg, int should_log)
- {
- char fp[HEX_DIGEST_LEN+1];
- int reject_unlisted = get_options()->AuthDirRejectUnlisted;
- uint32_t result = 0;
- router_status_t *status_by_digest;
- char *fp_by_name;
- if (!fingerprint_list)
- fingerprint_list = authdir_config_new();
- base16_encode(fp, sizeof(fp), id_digest, DIGEST_LEN);
- if (should_log)
- log_debug(LD_DIRSERV, "%d fingerprints, %d digests known.",
- strmap_size(fingerprint_list->fp_by_name),
- digestmap_size(fingerprint_list->status_by_digest));
- if ((fp_by_name =
- strmap_get_lc(fingerprint_list->fp_by_name, nickname))) {
- if (!strcasecmp(fp, fp_by_name)) {
- result |= FP_NAMED;
- if (should_log)
- log_debug(LD_DIRSERV,"Good fingerprint for '%s'",nickname);
- } else {
- if (should_log) {
- char *esc_contact = esc_for_log(contact);
- log_warn(LD_DIRSERV,
- "Mismatched fingerprint for '%s': expected '%s' got '%s'. "
- "ContactInfo '%s', platform '%s'.)",
- nickname, fp_by_name, fp,
- esc_contact,
- platform ? escaped(platform) : "");
- tor_free(esc_contact);
- }
- if (msg)
- *msg = "Rejected: There is already a named server with this nickname "
- "and a different fingerprint.";
- return FP_REJECT;
- }
- }
- status_by_digest = digestmap_get(fingerprint_list->status_by_digest,
- id_digest);
- if (status_by_digest)
- result |= (status_by_digest->status & ~FP_NAMED);
- if (result & FP_REJECT) {
- if (msg)
- *msg = "Fingerprint is marked rejected";
- return FP_REJECT;
- } else if (result & FP_INVALID) {
- if (msg)
- *msg = "Fingerprint is marked invalid";
- }
- if (authdir_policy_badexit_address(addr, or_port)) {
- if (should_log)
- log_info(LD_DIRSERV, "Marking '%s' as bad exit because of address '%s'",
- nickname, address);
- result |= FP_BADEXIT;
- }
- if (!(result & FP_NAMED)) {
- if (!authdir_policy_permits_address(addr, or_port)) {
- if (should_log)
- log_info(LD_DIRSERV, "Rejecting '%s' because of address '%s'",
- nickname, address);
- if (msg)
- *msg = "Authdir is rejecting routers in this range.";
- return FP_REJECT;
- }
- if (!authdir_policy_valid_address(addr, or_port)) {
- if (should_log)
- log_info(LD_DIRSERV, "Not marking '%s' valid because of address '%s'",
- nickname, address);
- result |= FP_INVALID;
- }
- if (reject_unlisted) {
- if (msg)
- *msg = "Authdir rejects unknown routers.";
- return FP_REJECT;
- }
-
- if (platform && !tor_version_as_new_as(platform,"0.1.0.2-rc"))
- result |= FP_INVALID;
- }
- return result;
- }
- const char *
- dirserv_get_nickname_by_digest(const char *digest)
- {
- router_status_t *status;
- if (!fingerprint_list)
- return NULL;
- tor_assert(digest);
- status = digestmap_get(fingerprint_list->status_by_digest, digest);
- return status ? status->nickname : NULL;
- }
- void
- dirserv_free_fingerprint_list(void)
- {
- if (!fingerprint_list)
- return;
- strmap_free(fingerprint_list->fp_by_name, _tor_free);
- digestmap_free(fingerprint_list->status_by_digest, _tor_free);
- tor_free(fingerprint_list);
- }
- static int
- dirserv_router_has_valid_address(routerinfo_t *ri)
- {
- struct in_addr iaddr;
- if (get_options()->DirAllowPrivateAddresses)
- return 0;
- if (!tor_inet_aton(ri->address, &iaddr)) {
- log_info(LD_DIRSERV,"Router '%s' published non-IP address '%s'. Refusing.",
- ri->nickname, ri->address);
- return -1;
- }
- if (is_internal_IP(ntohl(iaddr.s_addr), 0)) {
- log_info(LD_DIRSERV,
- "Router '%s' published internal IP address '%s'. Refusing.",
- ri->nickname, ri->address);
- return -1;
- }
- return 0;
- }
- int
- authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
- int complain)
- {
-
- uint32_t status = dirserv_router_get_status(ri, msg);
- time_t now;
- int severity = complain ? LOG_NOTICE : LOG_INFO;
- tor_assert(msg);
- if (status & FP_REJECT)
- return -1;
-
- now = time(NULL);
- if (ri->cache_info.published_on > now+ROUTER_ALLOW_SKEW) {
- log_fn(severity, LD_DIRSERV, "Publication time for nickname '%s' is too "
- "far (%d minutes) in the future; possible clock skew. Not adding "
- "(%s)",
- ri->nickname, (int)((ri->cache_info.published_on-now)/60),
- esc_router_info(ri));
- *msg = "Rejected: Your clock is set too far in the future, or your "
- "timezone is not correct.";
- return -1;
- }
- if (ri->cache_info.published_on < now-ROUTER_MAX_AGE_TO_PUBLISH) {
- log_fn(severity, LD_DIRSERV,
- "Publication time for router with nickname '%s' is too far "
- "(%d minutes) in the past. Not adding (%s)",
- ri->nickname, (int)((now-ri->cache_info.published_on)/60),
- esc_router_info(ri));
- *msg = "Rejected: Server is expired, or your clock is too far in the past,"
- " or your timezone is not correct.";
- return -1;
- }
- if (dirserv_router_has_valid_address(ri) < 0) {
- log_fn(severity, LD_DIRSERV,
- "Router with nickname '%s' has invalid address '%s'. "
- "Not adding (%s).",
- ri->nickname, ri->address,
- esc_router_info(ri));
- *msg = "Rejected: Address is not an IP, or IP is a private address.";
- return -1;
- }
-
- ri->is_named = (status & FP_NAMED) ? 1 : 0;
- ri->is_valid = (status & FP_INVALID) ? 0 : 1;
- ri->is_bad_exit = (status & FP_BADEXIT) ? 1 : 0;
- return 0;
- }
- int
- dirserv_add_multiple_descriptors(const char *desc, const char **msg)
- {
- int r=100;
- int r_tmp;
- const char *msg_out;
- smartlist_t *list;
- const char *s;
- int n_parsed = 0;
- tor_assert(msg);
- s = desc;
- list = smartlist_create();
- if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 0)) {
- SMARTLIST_FOREACH(list, routerinfo_t *, ri, {
- msg_out = NULL;
- r_tmp = dirserv_add_descriptor(ri, &msg_out);
- if (r_tmp < r) {
- r = r_tmp;
- *msg = msg_out;
- }
- });
- }
- n_parsed += smartlist_len(list);
- smartlist_clear(list);
- s = desc;
- if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 1)) {
- SMARTLIST_FOREACH(list, extrainfo_t *, ei, {
- msg_out = NULL;
- r_tmp = dirserv_add_extrainfo(ei, &msg_out);
- if (r_tmp < r) {
- r = r_tmp;
- *msg = msg_out;
- }
- });
- }
- n_parsed += smartlist_len(list);
- smartlist_free(list);
- if (! *msg) {
- if (!n_parsed) {
- *msg = "No descriptors found in your POST.";
- if (r > -1)
- r = -1;
- } else {
- *msg = "(no message)";
- }
- }
- return r <= 2 ? r : 2;
- }
- int
- dirserv_add_descriptor(routerinfo_t *ri, const char **msg)
- {
- int r;
- routerinfo_t *ri_old;
- char *desc = NULL;
- size_t desclen = 0;
-
- ri_old = router_get_by_digest(ri->cache_info.identity_digest);
- if (ri_old && ri_old->cache_info.published_on < ri->cache_info.published_on
- && router_differences_are_cosmetic(ri_old, ri)
- && !router_is_me(ri)) {
- log_info(LD_DIRSERV,
- "Not replacing descriptor from '%s'; differences are cosmetic.",
- ri->nickname);
- *msg = "Not replacing router descriptor; no information has changed since "
- "the last one with this identity.";
- control_event_or_authdir_new_descriptor("DROPPED",
- ri->cache_info.signed_descriptor_body,
- ri->cache_info.signed_descriptor_len, *msg);
- routerinfo_free(ri);
- return 0;
- }
- if (control_event_is_interesting(EVENT_AUTHDIR_NEWDESCS)) {
-
- desclen = ri->cache_info.signed_descriptor_len;
- desc = tor_strndup(ri->cache_info.signed_descriptor_body, desclen);
- }
- if ((r = router_add_to_routerlist(ri, msg, 0, 0))<0) {
- if (r < -1 && desc)
- control_event_or_authdir_new_descriptor("REJECTED", desc, desclen, *msg);
- tor_free(desc);
- return r == -1 ? 0 : -1;
- } else {
- smartlist_t *changed;
- control_event_or_authdir_new_descriptor("ACCEPTED", desc, desclen, *msg);
- changed = smartlist_create();
- smartlist_add(changed, ri);
- control_event_descriptors_changed(changed);
- smartlist_free(changed);
- if (!*msg) {
- *msg = ri->is_valid ? "Descriptor for valid server accepted" :
- "Descriptor for invalid server accepted";
- }
- tor_free(desc);
- return r == 0 ? 2 : 1;
- }
- }
- static int
- dirserv_add_extrainfo(extrainfo_t *ei, const char **msg)
- {
- routerinfo_t *ri;
- int r;
- tor_assert(msg);
- *msg = NULL;
- ri = router_get_by_digest(ei->cache_info.identity_digest);
- if (!ri) {
- *msg = "No corresponding router descriptor for extra-info descriptor";
- extrainfo_free(ei);
- return -1;
- }
- if ((r = routerinfo_incompatible_with_extrainfo(ri, ei, msg))) {
- extrainfo_free(ei);
- return r < 0 ? 0 : -1;
- }
- router_add_extrainfo_to_routerlist(ei, msg, 0, 0);
- return 2;
- }
- static void
- directory_remove_invalid(void)
- {
- int i;
- int changed = 0;
- routerlist_t *rl = router_get_routerlist();
- routerlist_assert_ok(rl);
- for (i = 0; i < smartlist_len(rl->routers); ++i) {
- const char *msg;
- routerinfo_t *ent = smartlist_get(rl->routers, i);
- uint32_t r = dirserv_router_get_status(ent, &msg);
- if (r & FP_REJECT) {
- log_info(LD_DIRSERV, "Router '%s' is now rejected: %s",
- ent->nickname, msg?msg:"");
- routerlist_remove(rl, ent, i--, 0);
- changed = 1;
- continue;
- }
- if (bool_neq((r & FP_NAMED), ent->is_named)) {
- log_info(LD_DIRSERV,
- "Router '%s' is now %snamed.", ent->nickname,
- (r&FP_NAMED)?"":"un");
- ent->is_named = (r&FP_NAMED)?1:0;
- changed = 1;
- }
- if (bool_neq((r & FP_INVALID), !ent->is_valid)) {
- log_info(LD_DIRSERV, "Router '%s' is now %svalid.", ent->nickname,
- (r&FP_INVALID) ? "in" : "");
- ent->is_valid = (r&FP_INVALID)?0:1;
- changed = 1;
- }
- if (bool_neq((r & FP_BADEXIT), ent->is_bad_exit)) {
- log_info(LD_DIRSERV, "Router '%s' is now a %s exit", ent->nickname,
- (r & FP_BADEXIT) ? "bad" : "good");
- ent->is_bad_exit = (r&FP_BADEXIT) ? 1: 0;
- changed = 1;
- }
- }
- if (changed)
- directory_set_dirty();
- routerlist_assert_ok(rl);
- }
- int
- getinfo_helper_dirserv_unregistered(control_connection_t *control_conn,
- const char *question, char **answer_out)
- {
- smartlist_t *answerlist;
- char buf[1024];
- char *answer;
- int min_bw = atoi(question);
- routerlist_t *rl = router_get_routerlist();
- (void) control_conn;
- if (strcmpstart(question, "unregistered-servers-"))
- return 0;
- question += strlen("unregistered-servers-");
- answerlist = smartlist_create();
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ent, {
- uint32_t r = dirserv_router_get_status(ent, NULL);
- if (router_get_advertised_bandwidth(ent) >= (size_t)min_bw &&
- !(r & FP_NAMED)) {
-
- tor_snprintf(buf, sizeof(buf),
- "%s: BW %d on '%s'.",
- ent->nickname, router_get_advertised_bandwidth(ent),
- ent->platform ? ent->platform : "");
- smartlist_add(answerlist, tor_strdup(buf));
- }
- });
- answer = smartlist_join_strings(answerlist, "\r\n", 0, NULL);
- SMARTLIST_FOREACH(answerlist, char *, cp, tor_free(cp));
- smartlist_free(answerlist);
- *answer_out = answer;
- return 0;
- }
- void
- directory_set_dirty(void)
- {
- time_t now = time(NULL);
- if (!the_directory_is_dirty)
- the_directory_is_dirty = now;
- if (!runningrouters_is_dirty)
- runningrouters_is_dirty = now;
- if (!the_v2_networkstatus_is_dirty)
- the_v2_networkstatus_is_dirty = now;
- }
- static char *
- list_single_server_status(routerinfo_t *desc, int is_live)
- {
- char buf[MAX_NICKNAME_LEN+HEX_DIGEST_LEN+4];
- char *cp;
- tor_assert(desc);
- cp = buf;
- if (!is_live) {
- *cp++ = '!';
- }
- if (desc->is_valid) {
- strlcpy(cp, desc->nickname, sizeof(buf)-(cp-buf));
- cp += strlen(cp);
- *cp++ = '=';
- }
- *cp++ = '$';
- base16_encode(cp, HEX_DIGEST_LEN+1, desc->cache_info.identity_digest,
- DIGEST_LEN);
- return tor_strdup(buf);
- }
- #define REACHABLE_TIMEOUT (45*60)
- static int
- dirserv_thinks_router_is_reachable(routerinfo_t *router, time_t now)
- {
- if (router_is_me(router) && !we_are_hibernating())
- return 1;
- return get_options()->AssumeReachable ||
- now < router->last_reachable + REACHABLE_TIMEOUT;
- }
- int
- dirserv_thinks_router_is_blatantly_unreachable(routerinfo_t *router,
- time_t now)
- {
- if (router->is_hibernating)
- return 0;
- if (now >= router->last_reachable + 5*REACHABLE_TIMEOUT &&
- router->testing_since &&
- now >= router->testing_since + 5*REACHABLE_TIMEOUT)
- return 1;
- return 0;
- }
- int
- list_server_status(smartlist_t *routers, char **router_status_out,
- int for_controller)
- {
-
- smartlist_t *rs_entries;
- time_t now = time(NULL);
- time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
- or_options_t *options = get_options();
-
- int authdir = authdir_mode_handles_descs(options);
- tor_assert(router_status_out);
- rs_entries = smartlist_create();
- SMARTLIST_FOREACH(routers, routerinfo_t *, ri,
- {
- if (authdir) {
-
- ri->is_running = dirserv_thinks_router_is_reachable(ri, now);
- }
- if (for_controller == 1 || ri->cache_info.published_on >= cutoff)
- smartlist_add(rs_entries, list_single_server_status(ri, ri->is_running));
- else if (for_controller > 2) {
- char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
- char *cp = name_buf;
- if (!ri->is_running)
- *cp++ = '!';
- router_get_verbose_nickname(cp, ri);
- smartlist_add(rs_entries, tor_strdup(name_buf));
- }
- });
- *router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
- SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
- smartlist_free(rs_entries);
- return 0;
- }
- static char *
- format_versions_list(config_line_t *ln)
- {
- smartlist_t *versions;
- char *result;
- versions = smartlist_create();
- for ( ; ln; ln = ln->next) {
- smartlist_split_string(versions, ln->value, ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- }
- sort_version_list(versions, 1);
- result = smartlist_join_strings(versions,",",0,NULL);
- SMARTLIST_FOREACH(versions,char *,s,tor_free(s));
- smartlist_free(versions);
- return result;
- }
- static int
- router_is_active(routerinfo_t *ri, time_t now)
- {
- time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
- if (ri->cache_info.published_on < cutoff)
- return 0;
- if (!ri->is_running || !ri->is_valid || ri->is_hibernating)
- return 0;
- return 1;
- }
- int
- dirserv_dump_directory_to_string(char **dir_out,
- crypto_pk_env_t *private_key, int complete)
- {
- char *cp;
- char *router_status;
- char *identity_pkey;
- char *recommended_versions;
- char digest[DIGEST_LEN];
- char published[ISO_TIME_LEN+1];
- char *buf = NULL;
- size_t buf_len;
- size_t identity_pkey_len;
- routerlist_t *rl = router_get_routerlist();
- time_t now = time(NULL);
- tor_assert(dir_out);
- *dir_out = NULL;
- if (list_server_status(rl->routers, &router_status, 0))
- return -1;
- if (crypto_pk_write_public_key_to_string(private_key,&identity_pkey,
- &identity_pkey_len)<0) {
- log_warn(LD_BUG,"write identity_pkey to string failed!");
- return -1;
- }
- recommended_versions =
- format_versions_list(get_options()->RecommendedVersions);
- format_iso_time(published, now);
- buf_len = 2048+strlen(recommended_versions)+
- strlen(router_status);
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
- if (complete || router_is_active(ri, now))
- buf_len += ri->cache_info.signed_descriptor_len+1);
- buf = tor_malloc(buf_len);
-
- tor_snprintf(buf, buf_len,
- "signed-directory\n"
- "published %s\n"
- "recommended-software %s\n"
- "router-status %s\n"
- "dir-signing-key\n%s\n",
- published, recommended_versions, router_status,
- identity_pkey);
- tor_free(recommended_versions);
- tor_free(router_status);
- tor_free(identity_pkey);
- cp = buf + strlen(buf);
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
- {
- size_t len = ri->cache_info.signed_descriptor_len;
- const char *body;
- if (!complete && !router_is_active(ri, now))
- continue;
- if (cp+len+1 >= buf+buf_len)
- goto truncated;
- body = signed_descriptor_get_body(&ri->cache_info);
- memcpy(cp, body, len);
- cp += len;
- *cp++ = '\n';
- });
- *cp = '\0';
-
- if (strlcat(buf, "directory-signature ", buf_len) >= buf_len)
- goto truncated;
- if (strlcat(buf, get_options()->Nickname, buf_len) >= buf_len)
- goto truncated;
- if (strlcat(buf, "\n", buf_len) >= buf_len)
- goto truncated;
- if (router_get_dir_hash(buf,digest)) {
- log_warn(LD_BUG,"couldn't compute digest");
- tor_free(buf);
- return -1;
- }
- note_crypto_pk_op(SIGN_DIR);
- if (router_append_dirobj_signature(buf,buf_len,digest,private_key)<0) {
- tor_free(buf);
- return -1;
- }
- *dir_out = buf;
- return 0;
- truncated:
- log_warn(LD_BUG,"tried to exceed string length.");
- tor_free(buf);
- return -1;
- }
- static cached_dir_t *the_directory = NULL;
- static cached_dir_t *cached_directory = NULL;
- static cached_dir_t cached_runningrouters = { NULL, NULL, 0, 0, 0, -1 };
- static digestmap_t *cached_v2_networkstatus = NULL;
- static void
- set_cached_dir(cached_dir_t *d, char *directory, time_t when)
- {
- time_t now = time(NULL);
- if (when<=d->published) {
- log_info(LD_DIRSERV, "Ignoring old directory; not caching.");
- tor_free(directory);
- } else if (when>=now+ROUTER_MAX_AGE_TO_PUBLISH) {
- log_info(LD_DIRSERV, "Ignoring future directory; not caching.");
- tor_free(directory);
- } else {
-
- log_debug(LD_DIRSERV, "Caching directory.");
- tor_free(d->dir);
- d->dir = directory;
- d->dir_len = strlen(directory);
- tor_free(d->dir_z);
- if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
- ZLIB_METHOD)) {
- log_warn(LD_BUG,"Error compressing cached directory");
- }
- d->published = when;
- }
- }
- void
- cached_dir_decref(cached_dir_t *d)
- {
- if (!d || --d->refcnt > 0)
- return;
- clear_cached_dir(d);
- tor_free(d);
- }
- static cached_dir_t *
- new_cached_dir(char *s, time_t published)
- {
- cached_dir_t *d = tor_malloc_zero(sizeof(cached_dir_t));
- d->refcnt = 1;
- d->dir = s;
- d->dir_len = strlen(s);
- d->published = published;
- if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
- ZLIB_METHOD)) {
- log_warn(LD_BUG, "Error compressing directory");
- }
- return d;
- }
- static void
- clear_cached_dir(cached_dir_t *d)
- {
- tor_free(d->dir);
- tor_free(d->dir_z);
- memset(d, 0, sizeof(cached_dir_t));
- }
- static void
- _free_cached_dir(void *_d)
- {
- cached_dir_t *d = (cached_dir_t *)_d;
- cached_dir_decref(d);
- }
- void
- dirserv_set_cached_directory(const char *directory, time_t published,
- int is_running_routers)
- {
- time_t now = time(NULL);
- if (is_running_routers) {
- if (published >= now - MAX_V1_RR_AGE)
- set_cached_dir(&cached_runningrouters, tor_strdup(directory), published);
- } else {
- if (published >= now - MAX_V1_DIRECTORY_AGE) {
- cached_dir_decref(cached_directory);
- cached_directory = new_cached_dir(tor_strdup(directory), published);
- }
- }
- }
- void
- dirserv_set_cached_networkstatus_v2(const char *networkstatus,
- const char *identity,
- time_t published)
- {
- cached_dir_t *d, *old_d;
- smartlist_t *trusted_dirs;
- if (!cached_v2_networkstatus)
- cached_v2_networkstatus = digestmap_new();
- old_d = digestmap_get(cached_v2_networkstatus, identity);
- if (!old_d && !networkstatus)
- return;
- if (networkstatus) {
- if (!old_d || published > old_d->published) {
- d = new_cached_dir(tor_strdup(networkstatus), published);
- digestmap_set(cached_v2_networkstatus, identity, d);
- if (old_d)
- cached_dir_decref(old_d);
- }
- } else {
- if (old_d) {
- digestmap_remove(cached_v2_networkstatus, identity);
- cached_dir_decref(old_d);
- }
- }
-
- trusted_dirs = router_get_trusted_dir_servers();
- if (digestmap_size(cached_v2_networkstatus) >
- smartlist_len(trusted_dirs) + MAX_UNTRUSTED_NETWORKSTATUSES) {
-
- const char *oldest = NULL;
- time_t oldest_published = TIME_MAX;
- digestmap_iter_t *iter;
- for (iter = digestmap_iter_init(cached_v2_networkstatus);
- !digestmap_iter_done(iter);
- iter = digestmap_iter_next(cached_v2_networkstatus, iter)) {
- const char *ident;
- void *val;
- digestmap_iter_get(iter, &ident, &val);
- d = val;
- if (d->published < oldest_published &&
- !router_digest_is_trusted_dir(ident)) {
- oldest = ident;
- oldest_published = d->published;
- }
- }
- tor_assert(oldest);
- d = digestmap_remove(cached_v2_networkstatus, oldest);
- if (d)
- cached_dir_decref(d);
- }
- }
- void
- dirserv_clear_old_networkstatuses(time_t cutoff)
- {
- digestmap_iter_t *iter;
- if (!cached_v2_networkstatus)
- return;
- for (iter = digestmap_iter_init(cached_v2_networkstatus);
- !digestmap_iter_done(iter); ) {
- const char *ident;
- void *val;
- cached_dir_t *dir;
- digestmap_iter_get(iter, &ident, &val);
- dir = val;
- if (dir->published < cutoff) {
- char *fname;
- iter = digestmap_iter_next_rmv(cached_v2_networkstatus, iter);
- fname = networkstatus_get_cache_filename(ident);
- if (file_status(fname) == FN_FILE) {
- log_info(LD_DIR, "Removing too-old untrusted networkstatus in %s",
- fname);
- unlink(fname);
- }
- tor_free(fname);
- cached_dir_decref(dir);
- } else {
- iter = digestmap_iter_next(cached_v2_networkstatus, iter);
- }
- }
- }
- void
- dirserv_clear_old_v1_info(time_t now)
- {
- if (cached_directory &&
- cached_directory->published < (now - MAX_V1_DIRECTORY_AGE)) {
- cached_dir_decref(cached_directory);
- cached_directory = NULL;
- }
- if (cached_runningrouters.published < (now - MAX_V1_RR_AGE)) {
- clear_cached_dir(&cached_runningrouters);
- }
- }
- static cached_dir_t *
- dirserv_pick_cached_dir_obj(cached_dir_t *cache_src,
- cached_dir_t *auth_src,
- time_t dirty, cached_dir_t *(*regenerate)(void),
- const char *name,
- authority_type_t auth_type)
- {
- or_options_t *options = get_options();
- int authority = (auth_type == V1_AUTHORITY && authdir_mode_v1(options)) ||
- (auth_type == V2_AUTHORITY && authdir_mode_v2(options));
- if (!authority || authdir_mode_bridge(options)) {
- return cache_src;
- } else {
-
- if (regenerate != NULL) {
- if (dirty && dirty + DIR_REGEN_SLACK_TIME < time(NULL)) {
- if (!(auth_src = regenerate())) {
- log_err(LD_BUG, "Couldn't generate %s?", name);
- exit(1);
- }
- } else {
- log_info(LD_DIRSERV, "The %s is still clean; reusing.", name);
- }
- }
- return auth_src ? auth_src : cache_src;
- }
- }
- static size_t
- dirserv_get_obj(const char **out,
- int compress,
- cached_dir_t *cache_src,
- cached_dir_t *auth_src,
- time_t dirty, cached_dir_t *(*regenerate)(void),
- const char *name,
- authority_type_t auth_type)
- {
- cached_dir_t *d = dirserv_pick_cached_dir_obj(
- cache_src, auth_src,
- dirty, regenerate, name, auth_type);
- if (!d)
- return 0;
- *out = compress ? d->dir_z : d->dir;
- if (*out) {
- return compress ? d->dir_z_len : d->dir_len;
- } else {
-
- return 0;
- }
- }
- cached_dir_t *
- dirserv_get_directory(void)
- {
- return dirserv_pick_cached_dir_obj(cached_directory, the_directory,
- the_directory_is_dirty,
- dirserv_regenerate_directory,
- "server directory", V1_AUTHORITY);
- }
- static cached_dir_t *
- dirserv_regenerate_directory(void)
- {
- char *new_directory=NULL;
- if (dirserv_dump_directory_to_string(&new_directory,
- get_identity_key(), 0)) {
- log_warn(LD_BUG, "Error creating directory.");
- tor_free(new_directory);
- return NULL;
- }
- cached_dir_decref(the_directory);
- the_directory = new_cached_dir(new_directory, time(NULL));
- log_info(LD_DIRSERV,"New directory (size %d) has been built.",
- (int)the_directory->dir_len);
- log_debug(LD_DIRSERV,"New directory (size %d):\n%s",
- (int)the_directory->dir_len, the_directory->dir);
- the_directory_is_dirty = 0;
-
- dirserv_set_cached_directory(the_directory->dir, time(NULL), 0);
- return the_directory;
- }
- static cached_dir_t the_runningrouters = { NULL, NULL, 0, 0, 0, -1 };
- static cached_dir_t *
- generate_runningrouters(void)
- {
- char *s=NULL;
- char *router_status=NULL;
- char digest[DIGEST_LEN];
- char published[ISO_TIME_LEN+1];
- size_t len;
- crypto_pk_env_t *private_key = get_identity_key();
- char *identity_pkey;
- size_t identity_pkey_len;
- routerlist_t *rl = router_get_routerlist();
- if (list_server_status(rl->routers, &router_status, 0)) {
- goto err;
- }
- if (crypto_pk_write_public_key_to_string(private_key,&identity_pkey,
- &identity_pkey_len)<0) {
- log_warn(LD_BUG,"write identity_pkey to string failed!");
- goto err;
- }
- format_iso_time(published, time(NULL));
- len = 2048+strlen(router_status);
- s = tor_malloc_zero(len);
- tor_snprintf(s, len,
- "network-status\n"
- "published %s\n"
- "router-status %s\n"
- "dir-signing-key\n%s"
- "directory-signature %s\n",
- published, router_status, identity_pkey,
- get_options()->Nickname);
- tor_free(router_status);
- tor_free(identity_pkey);
- if (router_get_runningrouters_hash(s,digest)) {
- log_warn(LD_BUG,"couldn't compute digest");
- goto err;
- }
- note_crypto_pk_op(SIGN_DIR);
- if (router_append_dirobj_signature(s, len, digest, private_key)<0)
- goto err;
- set_cached_dir(&the_runningrouters, s, time(NULL));
- runningrouters_is_dirty = 0;
- return &the_runningrouters;
- err:
- tor_free(s);
- tor_free(router_status);
- return NULL;
- }
- size_t
- dirserv_get_runningrouters(const char **rr, int compress)
- {
- return dirserv_get_obj(rr, compress,
- &cached_runningrouters, &the_runningrouters,
- runningrouters_is_dirty,
- generate_runningrouters,
- "v1 network status list", V1_AUTHORITY);
- }
- static cached_dir_t *the_v2_networkstatus = NULL;
- static cached_dir_t *the_v3_networkstatus_vote = NULL;
- static int
- should_generate_v2_networkstatus(void)
- {
- return authdir_mode_v2(get_options()) &&
- !authdir_mode_bridge(get_options()) &&
- the_v2_networkstatus_is_dirty &&
- the_v2_networkstatus_is_dirty + DIR_REGEN_SLACK_TIME < time(NULL);
- }
- #define UPTIME_TO_GUARANTEE_STABLE (3600*24*30)
- static uint32_t stable_uptime = 0;
- static uint32_t fast_bandwidth = 0;
- static uint32_t guard_bandwidth_including_exits = 0;
- static uint32_t guard_bandwidth_excluding_exits = 0;
- static uint64_t total_bandwidth = 0;
- static uint64_t total_exit_bandwidth = 0;
- static INLINE int
- real_uptime(routerinfo_t *router, time_t now)
- {
- if (now < router->cache_info.published_on)
- return router->uptime;
- else
- return router->uptime + (now - router->cache_info.published_on);
- }
- static int
- dirserv_thinks_router_is_unreliable(time_t now,
- routerinfo_t *router,
- int need_uptime, int need_capacity)
- {
- if (need_uptime) {
- int uptime = real_uptime(router, now);
- if ((unsigned)uptime < stable_uptime &&
- (unsigned)uptime < UPTIME_TO_GUARANTEE_STABLE)
- return 1;
- }
- if (need_capacity &&
- router_get_advertised_bandwidth(router) < fast_bandwidth)
- return 1;
- return 0;
- }
- static int
- _compare_uint32(const void **a, const void **b)
- {
- uint32_t first = **(uint32_t **)a, second = **(uint32_t **)b;
- if (first < second) return -1;
- if (first > second) return 1;
- return 0;
- }
- static void
- dirserv_compute_performance_thresholds(routerlist_t *rl)
- {
- smartlist_t *uptimes, *bandwidths, *bandwidths_excluding_exits;
- time_t now = time(NULL);
-
- stable_uptime = 0;
- fast_bandwidth = 0;
- guard_bandwidth_including_exits = 0;
- guard_bandwidth_excluding_exits = 0;
- total_bandwidth = 0;
- total_exit_bandwidth = 0;
- uptimes = smartlist_create();
- bandwidths = smartlist_create();
- bandwidths_excluding_exits = smartlist_create();
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
- if (router_is_active(ri, now)) {
- uint32_t *up = tor_malloc(sizeof(uint32_t));
- uint32_t *bw = tor_malloc(sizeof(uint32_t));
- ri->is_exit = exit_policy_is_general_exit(ri->exit_policy);
- *up = (uint32_t) real_uptime(ri, now);
- smartlist_add(uptimes, up);
- *bw = router_get_advertised_bandwidth(ri);
- total_bandwidth += *bw;
- if (ri->is_exit && !ri->is_bad_exit) {
- total_exit_bandwidth += *bw;
- } else {
- uint32_t *bw_not_exit = tor_malloc(sizeof(uint32_t));
- *bw_not_exit = *bw;
- smartlist_add(bandwidths_excluding_exits, bw_not_exit);
- }
- smartlist_add(bandwidths, bw);
- }
- });
- smartlist_sort(uptimes, _compare_uint32);
- smartlist_sort(bandwidths, _compare_uint32);
- smartlist_sort(bandwidths_excluding_exits, _compare_uint32);
- if (smartlist_len(uptimes))
- stable_uptime = *(uint32_t*)smartlist_get(uptimes,
- smartlist_len(uptimes)/2);
- if (smartlist_len(bandwidths)) {
- fast_bandwidth = *(uint32_t*)smartlist_get(bandwidths,
- smartlist_len(bandwidths)/8);
- if (fast_bandwidth < ROUTER_REQUIRED_MIN_BANDWIDTH)
- fast_bandwidth = *(uint32_t*)smartlist_get(bandwidths,
- smartlist_len(bandwidths)/4);
- guard_bandwidth_including_exits =
- *(uint32_t*)smartlist_get(bandwidths, smartlist_len(bandwidths)/2);
- }
- if (smartlist_len(bandwidths_excluding_exits)) {
- guard_bandwidth_excluding_exits =
- *(uint32_t*)smartlist_get(bandwidths_excluding_exits,
- smartlist_len(bandwidths_excluding_exits)/2);
- }
- log(LOG_INFO, LD_DIRSERV,
- "Cutoffs: %lus uptime, %lu b/s fast, %lu or %lu b/s guard.",
- (unsigned long)stable_uptime,
- (unsigned long)fast_bandwidth,
- (unsigned long)guard_bandwidth_including_exits,
- (unsigned long)guard_bandwidth_excluding_exits);
- SMARTLIST_FOREACH(uptimes, uint32_t *, up, tor_free(up));
- SMARTLIST_FOREACH(bandwidths, uint32_t *, bw, tor_free(bw));
- SMARTLIST_FOREACH(bandwidths_excluding_exits, uint32_t *, bw, tor_free(bw));
- smartlist_free(uptimes);
- smartlist_free(bandwidths);
- smartlist_free(bandwidths_excluding_exits);
- }
- int
- routerstatus_format_entry(char *buf, size_t buf_len,
- routerstatus_t *rs, const char *platform)
- {
- int r;
- struct in_addr in;
- int f_authority;
- char published[ISO_TIME_LEN+1];
- char ipaddr[INET_NTOA_BUF_LEN];
- char identity64[BASE64_DIGEST_LEN+1];
- char digest64[BASE64_DIGEST_LEN+1];
- format_iso_time(published, rs->published_on);
- digest_to_base64(identity64, rs->identity_digest);
- digest_to_base64(digest64, rs->descriptor_digest);
- in.s_addr = htonl(rs->addr);
- tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
- f_authority = router_digest_is_trusted_dir(rs->identity_digest);
- r = tor_snprintf(buf, buf_len,
- "r %s %s %s %s %s %d %d\n"
- "s%s%s%s%s%s%s%s%s%s%s\n",
- rs->nickname,
- identity64,
- digest64,
- published,
- ipaddr,
- (int)rs->or_port,
- (int)rs->dir_port,
-
- f_authority?" Authority":"",
- rs->is_bad_exit?" BadExit":"",
- rs->is_exit?" Exit":"",
- rs->is_fast?" Fast":"",
- rs->is_possible_guard?" Guard":"",
- rs->is_named?" Named":"",
- rs->is_stable?" Stable":"",
- rs->is_running?" Running":"",
- rs->is_valid?" Valid":"",
- rs->is_v2_dir?" V2Dir":"");
- if (r<0) {
- log_warn(LD_BUG, "Not enough space in buffer.");
- return -1;
- }
- if (platform && !strcmpstart(platform, "Tor ")) {
- const char *eos = find_whitespace(platform+4);
- if (eos && !strcmpstart(eos, " (r")) {
-
- eos = find_whitespace(eos+1);
- }
- if (eos) {
- char *p = tor_strndup(platform, eos-platform);
- size_t ln = strlen(buf);
- buf += ln;
- buf_len -= ln;
- if (tor_snprintf(buf, buf_len, "opt v %s\n", p)<0) {
- tor_free(p);
- log_warn(LD_BUG, "Unable to print router version.");
- return -1;
- }
- tor_free(p);
- }
- }
- return 0;
- }
- static int
- _compare_routerinfo_by_id_digest(const void **a, const void **b)
- {
- routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
- return memcmp(first->cache_info.identity_digest,
- second->cache_info.identity_digest,
- DIGEST_LEN);
- }
- static cached_dir_t *
- generate_networkstatus_opinion(int v2)
- {
- #define LONGEST_STATUS_FLAG_NAME_LEN 9
- #define N_STATUS_FLAGS 10
- #define RS_ENTRY_LEN \
- ( \
- MAX_NICKNAME_LEN+BASE64_DIGEST_LEN*2+ISO_TIME_LEN+INET_NTOA_BUF_LEN+ \
- 5*2 + 10 + \
- \
- (LONGEST_STATUS_FLAG_NAME_LEN+1)*N_STATUS_FLAGS + 2)
- cached_dir_t *r = NULL;
- size_t len, identity_pkey_len;
- char *status = NULL, *client_versions = NULL, *server_versions = NULL,
- *identity_pkey = NULL, *hostname = NULL;
- char *outp, *endp;
- or_options_t *options = get_options();
- char fingerprint[FINGERPRINT_LEN+1];
- char ipaddr[INET_NTOA_BUF_LEN];
- char published[ISO_TIME_LEN+1];
- char digest[DIGEST_LEN];
- struct in_addr in;
- uint32_t addr;
- crypto_pk_env_t *private_key;
- routerlist_t *rl = router_get_routerlist();
- time_t now = time(NULL);
- time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
- int naming = options->NamingAuthoritativeDir;
- int versioning = options->VersioningAuthoritativeDir;
- int listbadexits = options->AuthDirListBadExits;
- int exits_can_be_guards;
- const char *contact;
- authority_cert_t *cert = NULL;
- char *version_lines = NULL;
- smartlist_t *routers = NULL;
- if (v2) {
- private_key = get_identity_key();
- } else {
- private_key = get_my_v3_authority_signing_key();
- cert = get_my_v3_authority_cert();
- if (!private_key || !cert) {
- log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
- goto done;
- }
- }
- if (resolve_my_address(LOG_WARN, options, &addr, &hostname)<0) {
- log_warn(LD_NET, "Couldn't resolve my hostname");
- goto done;
- }
- in.s_addr = htonl(addr);
- tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
- format_iso_time(published, now);
- client_versions = format_versions_list(options->RecommendedClientVersions);
- server_versions = format_versions_list(options->RecommendedServerVersions);
- if (crypto_pk_write_public_key_to_string(private_key, &identity_pkey,
- &identity_pkey_len)<0) {
- log_warn(LD_BUG,"Writing public key to string failed.");
- goto done;
- }
- if (v2) {
- if (crypto_pk_get_fingerprint(private_key, fingerprint, 0)<0) {
- log_err(LD_BUG, "Error computing fingerprint");
- goto done;
- }
- } else {
- base16_encode(fingerprint, sizeof(fingerprint),
- cert->cache_info.identity_digest, DIGEST_LEN);
- }
- contact = get_options()->ContactInfo;
- if (!contact)
- contact = "(none)";
- if (versioning) {
- size_t v_len = 64+strlen(client_versions)+strlen(server_versions);
- version_lines = tor_malloc(v_len);
- tor_snprintf(version_lines, v_len,
- "client-versions %s\nserver-versions %s\n",
- client_versions, server_versions);
- } else {
- version_lines = tor_strdup("");
- }
- len = 4096+strlen(client_versions)+strlen(server_versions);
- len += identity_pkey_len*2;
- len += (RS_ENTRY_LEN)*smartlist_len(rl->routers);
- if (!v2) {
- len += cert->cache_info.signed_descriptor_len;
- }
- status = tor_malloc(len);
- if (v2) {
- tor_snprintf(status, len,
- "network-status-version 2\n"
- "dir-source %s %s %d\n"
- "fingerprint %s\n"
- "contact %s\n"
- "published %s\n"
- "dir-options%s%s%s\n"
- "%s"
- "dir-signing-key\n%s",
- hostname, ipaddr, (int)options->DirPort,
- fingerprint,
- contact,
- published,
- naming ? " Names" : "",
- listbadexits ? " BadExits" : "",
- versioning ? " Versions" : "",
- version_lines,
- identity_pkey);
- outp = status + strlen(status);
- endp = status + len;
- } else {
- tor_snprintf(status, len,
- "network-status-version 3\n"
- "vote-status vote\n"
- "published %s\n"
- "valid-after %s\n"
- "fresh-until %s\n"
- "valid-until %s\n"
- "%s"
- "known-flags Authority%s Exit Fast Guard%s Running Stable "
- "Valid V2Dir\n"
- "dir-source %s %s %s %s %d %d\n"
- "contact %s\n",
- published,
- published,
- published,
- published,
- version_lines,
- listbadexits ? " BadExit" : "",
- naming ? " Named" : "",
- options->Nickname, fingerprint, options->Address,
- ipaddr, (int)options->DirPort, (int)options->ORPort,
- contact);
- outp = status + strlen(status);
- endp = status + len;
- 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(rl->routers, routerinfo_t *, ri, {
- ri->is_running = dirserv_thinks_router_is_reachable(ri, now);
- });
- dirserv_compute_performance_thresholds(rl);
-
- exits_can_be_guards = total_exit_bandwidth >= (total_bandwidth / 3);
- routers = smartlist_create();
- smartlist_add_all(routers, rl->routers);
- smartlist_sort(routers, _compare_routerinfo_by_id_digest);
- SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
- if (ri->cache_info.published_on >= cutoff) {
- routerstatus_t rs;
-
- int unstable_version =
- tor_version_as_new_as(ri->platform,"0.1.1.10-alpha") &&
- !tor_version_as_new_as(ri->platform,"0.1.1.16-rc-cvs");
- memset(&rs, 0, sizeof(rs));
-
- rs.is_exit = ri->is_exit;
- rs.is_stable = ri->is_stable =
- router_is_active(ri, now) &&
- !dirserv_thinks_router_is_unreliable(now, ri, 1, 0) &&
- !unstable_version;
- rs.is_fast = ri->is_fast =
- router_is_active(ri, now) &&
- !dirserv_thinks_router_is_unreliable(now, ri, 0, 1);
- rs.is_running = ri->is_running;
-
- rs.is_named = naming && ri->is_named;
- rs.is_valid = ri->is_valid;
- rs.is_possible_guard = rs.is_fast && rs.is_stable &&
- (!rs.is_exit || exits_can_be_guards) &&
- router_get_advertised_bandwidth(ri) >=
- (exits_can_be_guards ? guard_bandwidth_including_exits :
- guard_bandwidth_excluding_exits);
- rs.is_bad_exit = listbadexits && ri->is_bad_exit;
-
- rs.is_v2_dir = ri->dir_port &&
- tor_version_as_new_as(ri->platform,"0.1.1.9-alpha");
- if (!strcasecmp(ri->nickname, UNNAMED_ROUTER_NICKNAME))
- rs.is_named = 0;
- rs.published_on = ri->cache_info.published_on;
- memcpy(rs.identity_digest, ri->cache_info.identity_digest, DIGEST_LEN);
- memcpy(rs.descriptor_digest, ri->cache_info.signed_descriptor_digest,
- DIGEST_LEN);
- rs.addr = ri->addr;
- strlcpy(rs.nickname, ri->nickname, sizeof(rs.nickname));
- rs.or_port = ri->or_port;
- rs.dir_port = ri->dir_port;
- if (routerstatus_format_entry(outp, endp-outp, &rs, ri->platform) < 0) {
- log_warn(LD_BUG, "Unable to print router status.");
- goto done;
- }
- outp += strlen(outp);
- }
- });
- if (v2) {
- if (tor_snprintf(outp, endp-outp, "directory-signature %s\n",
- get_options()->Nickname)<0) {
- log_warn(LD_BUG, "Unable to write signature line.");
- goto done;
- }
- if (router_get_networkstatus_v2_hash(status, digest)<0) {
- log_warn(LD_BUG, "Unable to hash network status");
- goto done;
- }
- outp += strlen(outp);
- } else {
- char hex_digest[HEX_DIGEST_LEN+1];
- if (tor_snprintf(outp, endp-outp, "directory-signature %s ",
- fingerprint)<0) {
- log_warn(LD_BUG, "Unable to start signature line.");
- goto done;
- }
- if (router_get_networkstatus_v3_hash(status, digest)<0) {
- log_warn(LD_BUG, "Unable to hash network status vote");
- goto done;
- }
- base16_encode(hex_digest, sizeof(hex_digest), digest, DIGEST_LEN);
- outp += strlen(outp);
- if (tor_snprintf(outp, endp-outp, "%s\n", hex_digest)<0) {
- log_warn(LD_BUG, "Unable to end signature line.");
- goto done;
- }
- outp += strlen(outp);
- }
- note_crypto_pk_op(SIGN_DIR);
- if (router_append_dirobj_signature(outp,endp-outp,digest,private_key)<0) {
- log_warn(LD_BUG, "Unable to sign router status.");
- goto done;
- }
- {
- cached_dir_t **ns_ptr =
- v2 ? &the_v2_networkstatus : &the_v3_networkstatus_vote;
- if (*ns_ptr)
- cached_dir_decref(*ns_ptr);
- *ns_ptr = new_cached_dir(status, now);
- status = NULL;
- if (v2)
- the_v2_networkstatus_is_dirty = 0;
- router_set_networkstatus((*ns_ptr)->dir, now, NS_GENERATED, NULL);
- r = *ns_ptr;
- }
- done:
- tor_free(client_versions);
- tor_free(server_versions);
- tor_free(version_lines);
- tor_free(status);
- tor_free(hostname);
- tor_free(identity_pkey);
- if (routers)
- smartlist_free(routers);
- return r;
- }
- void
- dirserv_get_networkstatus_v2_fingerprints(smartlist_t *result,
- const char *key)
- {
- tor_assert(result);
- if (!cached_v2_networkstatus)
- cached_v2_networkstatus = digestmap_new();
- if (should_generate_v2_networkstatus())
- generate_networkstatus_opinion(1);
- if (!strcmp(key,"authority")) {
- if (authdir_mode_v2(get_options())) {
- routerinfo_t *me = router_get_my_routerinfo();
- if (me)
- smartlist_add(result,
- tor_memdup(me->cache_info.identity_digest, DIGEST_LEN));
- }
- } else if (!strcmp(key, "all")) {
- if (digestmap_size(cached_v2_networkstatus)) {
- digestmap_iter_t *iter;
- iter = digestmap_iter_init(cached_v2_networkstatus);
- while (!digestmap_iter_done(iter)) {
- const char *ident;
- void *val;
- digestmap_iter_get(iter, &ident, &val);
- smartlist_add(result, tor_memdup(ident, DIGEST_LEN));
- iter = digestmap_iter_next(cached_v2_networkstatus, iter);
- }
- } else {
- SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
- trusted_dir_server_t *, ds,
- if (ds->type & V2_AUTHORITY)
- smartlist_add(result, tor_memdup(ds->digest, DIGEST_LEN)));
- }
- smartlist_sort_digests(result);
- if (smartlist_len(result) == 0)
- log_warn(LD_DIRSERV,
- "Client requested 'all' network status objects; we have none.");
- } else if (!strcmpstart(key, "fp/")) {
- dir_split_resource_into_fingerprints(key+3, result, NULL, 1, 1);
- }
- }
- void
- dirserv_get_networkstatus_v2(smartlist_t *result,
- const char *key)
- {
- cached_dir_t *cached;
- smartlist_t *fingerprints = smartlist_create();
- tor_assert(result);
- if (!cached_v2_networkstatus)
- cached_v2_networkstatus = digestmap_new();
- dirserv_get_networkstatus_v2_fingerprints(fingerprints, key);
- SMARTLIST_FOREACH(fingerprints, const char *, fp,
- {
- if (router_digest_is_me(fp) && should_generate_v2_networkstatus())
- generate_networkstatus_opinion(1);
- cached = digestmap_get(cached_v2_networkstatus, fp);
- if (cached) {
- smartlist_add(result, cached);
- } else {
- char hexbuf[HEX_DIGEST_LEN+1];
- base16_encode(hexbuf, sizeof(hexbuf), fp, DIGEST_LEN);
- log_info(LD_DIRSERV, "Don't know about any network status with "
- "fingerprint '%s'", hexbuf);
- }
- });
- SMARTLIST_FOREACH(fingerprints, char *, cp, tor_free(cp));
- smartlist_free(fingerprints);
- }
- int
- dirserv_get_routerdesc_fingerprints(smartlist_t *fps_out, const char *key,
- const char **msg)
- {
- *msg = NULL;
- if (!strcmp(key, "all")) {
- routerlist_t *rl = router_get_routerlist();
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
- smartlist_add(fps_out,
- tor_memdup(r->cache_info.identity_digest, DIGEST_LEN)));
- } else if (!strcmp(key, "authority")) {
- routerinfo_t *ri = router_get_my_routerinfo();
- if (ri)
- smartlist_add(fps_out,
- tor_memdup(ri->cache_info.identity_digest, DIGEST_LEN));
- } else if (!strcmpstart(key, "d/")) {
- key += strlen("d/");
- dir_split_resource_into_fingerprints(key, fps_out, NULL, 1, 1);
- } else if (!strcmpstart(key, "fp/")) {
- key += strlen("fp/");
- dir_split_resource_into_fingerprints(key, fps_out, NULL, 1, 1);
- } else {
- *msg = "Key not recognized";
- return -1;
- }
- if (!smartlist_len(fps_out)) {
- *msg = "Servers unavailable";
- return -1;
- }
- return 0;
- }
- int
- dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
- const char **msg)
- {
- *msg = NULL;
- if (!strcmp(key, "/tor/server/all")) {
- routerlist_t *rl = router_get_routerlist();
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
- smartlist_add(descs_out, &(r->cache_info)));
- } else if (!strcmp(key, "/tor/server/authority")) {
- routerinfo_t *ri = router_get_my_routerinfo();
- if (ri)
- smartlist_add(descs_out, &(ri->cache_info));
- } else if (!strcmpstart(key, "/tor/server/d/")) {
- smartlist_t *digests = smartlist_create();
- key += strlen("/tor/server/d/");
- dir_split_resource_into_fingerprints(key, digests, NULL, 1, 1);
- SMARTLIST_FOREACH(digests, const char *, d,
- {
- signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
- if (sd)
- smartlist_add(descs_out,sd);
- });
- SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
- smartlist_free(digests);
- } else if (!strcmpstart(key, "/tor/server/fp/")) {
- smartlist_t *digests = smartlist_create();
- time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
- key += strlen("/tor/server/fp/");
- dir_split_resource_into_fingerprints(key, digests, NULL, 1, 1);
- SMARTLIST_FOREACH(digests, const char *, d,
- {
- if (router_digest_is_me(d)) {
- smartlist_add(descs_out, &(router_get_my_routerinfo()->cache_info));
- } else {
- routerinfo_t *ri = router_get_by_digest(d);
-
- if (ri && ri->cache_info.published_on > cutoff)
- smartlist_add(descs_out, &(ri->cache_info));
- }
- });
- SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
- smartlist_free(digests);
- } else {
- *msg = "Key not recognized";
- return -1;
- }
- if (!smartlist_len(descs_out)) {
- *msg = "Servers unavailable";
- return -1;
- }
- return 0;
- }
- void
- dirserv_orconn_tls_done(const char *address,
- uint16_t or_port,
- const char *digest_rcvd,
- int as_advertised)
- {
- routerlist_t *rl = router_get_routerlist();
- tor_assert(address);
- tor_assert(digest_rcvd);
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
- if (!strcasecmp(address, ri->address) && or_port == ri->or_port &&
- as_advertised &&
- !memcmp(ri->cache_info.identity_digest, digest_rcvd, DIGEST_LEN)) {
-
- log_info(LD_DIRSERV, "Found router %s to be reachable. Yay.",
- ri->nickname);
- ri->last_reachable = time(NULL);
- ri->num_unreachable_notifications = 0;
- }
- });
-
- }
- void
- dirserv_test_reachability(int try_all)
- {
- time_t now = time(NULL);
-
- routerlist_t *rl = router_get_routerlist();
- static char ctr = 0;
- SMARTLIST_FOREACH(rl->routers, routerinfo_t *, router, {
- const char *id_digest = router->cache_info.identity_digest;
- if (router_is_me(router))
- continue;
- if (try_all || (((uint8_t)id_digest[0]) % 128) == ctr) {
- log_debug(LD_OR,"Testing reachability of %s at %s:%u.",
- router->nickname, router->address, router->or_port);
-
- if (!router->testing_since)
- router->testing_since = now;
- connection_or_connect(router->addr, router->or_port,
- id_digest);
- }
- });
- if (!try_all)
- ctr = (ctr + 1) % 128;
- }
- size_t
- dirserv_estimate_data_size(smartlist_t *fps, int is_serverdescs,
- int compressed)
- {
- size_t result;
- tor_assert(fps);
- if (is_serverdescs) {
- int n = smartlist_len(fps);
- routerinfo_t *me = router_get_my_routerinfo();
- result = (me?me->cache_info.signed_descriptor_len:2048) * n;
- if (compressed)
- result /= 2;
- } else {
- result = 0;
- SMARTLIST_FOREACH(fps, const char *, d, {
- cached_dir_t *dir = digestmap_get(cached_v2_networkstatus, d);
- if (dir)
- result += compressed ? dir->dir_z_len : dir->dir_len;
- });
- }
- return result;
- }
- #define DIRSERV_BUFFER_MIN 16384
- static int
- connection_dirserv_finish_spooling(dir_connection_t *conn)
- {
- if (conn->zlib_state) {
- connection_write_to_buf_zlib("", 0, conn, 1);
- tor_zlib_free(conn->zlib_state);
- conn->zlib_state = NULL;
- }
- conn->dir_spool_src = DIR_SPOOL_NONE;
- return 0;
- }
- static int
- connection_dirserv_add_servers_to_outbuf(dir_connection_t *conn)
- {
- int by_fp = (conn->dir_spool_src == DIR_SPOOL_SERVER_BY_FP ||
- conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_FP);
- int extra = (conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_FP ||
- conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_DIGEST);
- time_t publish_cutoff = time(NULL)-ROUTER_MAX_AGE_TO_PUBLISH;
- while (smartlist_len(conn->fingerprint_stack) &&
- buf_datalen(conn->_base.outbuf) < DIRSERV_BUFFER_MIN) {
- const char *body;
- char *fp = smartlist_pop_last(conn->fingerprint_stack);
- signed_descriptor_t *sd = NULL;
- if (by_fp) {
- if (router_digest_is_me(fp)) {
- if (extra)
- sd = &(router_get_my_extrainfo()->cache_info);
- else
- sd = &(router_get_my_routerinfo()->cache_info);
- } else {
- routerinfo_t *ri = router_get_by_digest(fp);
- if (ri &&
- ri->cache_info.published_on > publish_cutoff) {
- if (extra)
- sd = extrainfo_get_by_descriptor_digest(
- ri->cache_info.extra_info_digest);
- else
- sd = &ri->cache_info;
- }
- }
- } else {
- sd = extra ? extrainfo_get_by_descriptor_digest(fp)
- : router_get_by_descriptor_digest(fp);
- }
- tor_free(fp);
- if (!sd)
- continue;
- body = signed_descriptor_get_body(sd);
- if (conn->zlib_state) {
- int last = ! smartlist_len(conn->fingerprint_stack);
- connection_write_to_buf_zlib(body, sd->signed_descriptor_len, conn,
- last);
- if (last) {
- tor_zlib_free(conn->zlib_state);
- conn->zlib_state = NULL;
- }
- } else {
- connection_write_to_buf(body,
- sd->signed_descriptor_len,
- TO_CONN(conn));
- }
- }
- if (!smartlist_len(conn->fingerprint_stack)) {
-
- conn->dir_spool_src = DIR_SPOOL_NONE;
- smartlist_free(conn->fingerprint_stack);
- conn->fingerprint_stack = NULL;
- }
- return 0;
- }
- static int
- connection_dirserv_add_dir_bytes_to_outbuf(dir_connection_t *conn)
- {
- ssize_t bytes;
- int64_t remaining;
- bytes = DIRSERV_BUFFER_MIN - buf_datalen(conn->_base.outbuf);
- tor_assert(bytes > 0);
- tor_assert(conn->cached_dir);
- if (bytes < 8192)
- bytes = 8192;
- remaining = conn->cached_dir->dir_z_len - conn->cached_dir_offset;
- if (bytes > remaining)
- bytes = (ssize_t) remaining;
- if (conn->zlib_state) {
- connection_write_to_buf_zlib(
- conn->cached_dir->dir_z + conn->cached_dir_offset,
- bytes, conn, bytes == remaining);
- } else {
- connection_write_to_buf(conn->cached_dir->dir_z + conn->cached_dir_offset,
- bytes, TO_CONN(conn));
- }
- conn->cached_dir_offset += bytes;
- if (conn->cached_dir_offset == (int)conn->cached_dir->dir_z_len) {
-
- connection_dirserv_finish_spooling(conn);
- cached_dir_decref(conn->cached_dir);
- conn->cached_dir = NULL;
- }
- return 0;
- }
- static int
- connection_dirserv_add_networkstatus_bytes_to_outbuf(dir_connection_t *conn)
- {
- while (buf_datalen(conn->_base.outbuf) < DIRSERV_BUFFER_MIN) {
- if (conn->cached_dir) {
- int uncompressing = (conn->zlib_state != NULL);
- int r = connection_dirserv_add_dir_bytes_to_outbuf(conn);
- if (conn->dir_spool_src == DIR_SPOOL_NONE) {
-
- conn->dir_spool_src = DIR_SPOOL_NETWORKSTATUS;
-
- if (uncompressing && ! conn->zlib_state &&
- conn->fingerprint_stack &&
- smartlist_len(conn->fingerprint_stack)) {
- conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD);
- }
- }
- if (r) return r;
- } else if (conn->fingerprint_stack &&
- smartlist_len(conn->fingerprint_stack)) {
-
- char *fp = smartlist_pop_last(conn->fingerprint_stack);
- cached_dir_t *d;
- if (router_digest_is_me(fp))
- d = the_v2_networkstatus;
- else
- d = digestmap_get(cached_v2_networkstatus, fp);
- tor_free(fp);
- if (d) {
- ++d->refcnt;
- conn->cached_dir = d;
- conn->cached_dir_offset = 0;
- }
- } else {
- connection_dirserv_finish_spooling(conn);
- if (conn->fingerprint_stack)
- smartlist_free(conn->fingerprint_stack);
- conn->fingerprint_stack = NULL;
- return 0;
- }
- }
- return 0;
- }
- int
- connection_dirserv_flushed_some(dir_connection_t *conn)
- {
- tor_assert(conn->_base.state == DIR_CONN_STATE_SERVER_WRITING);
- if (buf_datalen(conn->_base.outbuf) >= DIRSERV_BUFFER_MIN)
- return 0;
- switch (conn->dir_spool_src) {
- case DIR_SPOOL_EXTRA_BY_DIGEST:
- case DIR_SPOOL_EXTRA_BY_FP:
- case DIR_SPOOL_SERVER_BY_DIGEST:
- case DIR_SPOOL_SERVER_BY_FP:
- return connection_dirserv_add_servers_to_outbuf(conn);
- case DIR_SPOOL_CACHED_DIR:
- return connection_dirserv_add_dir_bytes_to_outbuf(conn);
- case DIR_SPOOL_NETWORKSTATUS:
- return connection_dirserv_add_networkstatus_bytes_to_outbuf(conn);
- case DIR_SPOOL_NONE:
- default:
- return 0;
- }
- }
- void
- dirserv_free_all(void)
- {
- dirserv_free_fingerprint_list();
- cached_dir_decref(the_directory);
- clear_cached_dir(&the_runningrouters);
- cached_dir_decref(the_v2_networkstatus);
- cached_dir_decref(cached_directory);
- clear_cached_dir(&cached_runningrouters);
- if (cached_v2_networkstatus) {
- digestmap_free(cached_v2_networkstatus, _free_cached_dir);
- cached_v2_networkstatus = NULL;
- }
- }
|