r15318@catbus: nickm | 2007-09-24 11:42:25 -0400
[tor.git] / src / or / router.c
blobe0bd1fc47c28c729172d0f383f45a46a65e42c90
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char router_c_id[] =
7 "$Id$";
9 #include "or.h"
11 /**
12 * \file router.c
13 * \brief OR functionality, including key maintenance, generating
14 * and uploading server descriptors, retrying OR connections.
15 **/
17 extern long stats_n_seconds_working;
19 /* Exposed for test.c. */ void get_platform_str(char *platform, size_t len);
21 /************************************************************/
23 /*****
24 * Key management: ORs only.
25 *****/
27 /** Private keys for this OR. There is also an SSL key managed by tortls.c.
29 static tor_mutex_t *key_lock=NULL;
30 static time_t onionkey_set_at=0; /**< When was onionkey last changed? */
31 /** Current private onionskin decryption key: used to decode CREATE cells. */
32 static crypto_pk_env_t *onionkey=NULL;
33 /** Previous private onionskin decription key: used to decode CREATE cells
34 * generated by clients that have an older version of our descriptor. */
35 static crypto_pk_env_t *lastonionkey=NULL;
36 /** Private "identity key": used to sign directory info and TLS
37 * certificates. Never changes. */
38 static crypto_pk_env_t *identitykey=NULL;
39 /** Digest of identitykey. */
40 static char identitykey_digest[DIGEST_LEN];
42 /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
43 * to update onionkey correctly, call rotate_onion_key().
45 static void
46 set_onion_key(crypto_pk_env_t *k)
48 tor_mutex_acquire(key_lock);
49 onionkey = k;
50 onionkey_set_at = time(NULL);
51 tor_mutex_release(key_lock);
52 mark_my_descriptor_dirty();
55 /** Return the current onion key. Requires that the onion key has been
56 * loaded or generated. */
57 crypto_pk_env_t *
58 get_onion_key(void)
60 tor_assert(onionkey);
61 return onionkey;
64 /** Store a copy of the current onion key into *<b>key</b>, and a copy
65 * of the most recent onion key into *<b>last</b>.
67 void
68 dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
70 tor_assert(key);
71 tor_assert(last);
72 tor_mutex_acquire(key_lock);
73 tor_assert(onionkey);
74 *key = crypto_pk_dup_key(onionkey);
75 if (lastonionkey)
76 *last = crypto_pk_dup_key(lastonionkey);
77 else
78 *last = NULL;
79 tor_mutex_release(key_lock);
82 /** Return the time when the onion key was last set. This is either the time
83 * when the process launched, or the time of the most recent key rotation since
84 * the process launched.
86 time_t
87 get_onion_key_set_at(void)
89 return onionkey_set_at;
92 /** Set the current identity key to k.
94 void
95 set_identity_key(crypto_pk_env_t *k)
97 if (identitykey)
98 crypto_free_pk_env(identitykey);
99 identitykey = k;
100 crypto_pk_get_digest(identitykey, identitykey_digest);
103 /** Returns the current identity key; requires that the identity key has been
104 * set.
106 crypto_pk_env_t *
107 get_identity_key(void)
109 tor_assert(identitykey);
110 return identitykey;
113 /** Return true iff the identity key has been set. */
115 identity_key_is_set(void)
117 return identitykey != NULL;
120 /** Replace the previous onion key with the current onion key, and generate
121 * a new previous onion key. Immediately after calling this function,
122 * the OR should:
123 * - schedule all previous cpuworkers to shut down _after_ processing
124 * pending work. (This will cause fresh cpuworkers to be generated.)
125 * - generate and upload a fresh routerinfo.
127 void
128 rotate_onion_key(void)
130 char fname[512];
131 char fname_prev[512];
132 crypto_pk_env_t *prkey;
133 or_state_t *state = get_or_state();
134 time_t now;
135 tor_snprintf(fname,sizeof(fname),
136 "%s/keys/secret_onion_key",get_options()->DataDirectory);
137 tor_snprintf(fname_prev,sizeof(fname_prev),
138 "%s/keys/secret_onion_key.old",get_options()->DataDirectory);
139 if (!(prkey = crypto_new_pk_env())) {
140 log_err(LD_GENERAL,"Error constructing rotated onion key");
141 goto error;
143 if (crypto_pk_generate_key(prkey)) {
144 log_err(LD_BUG,"Error generating onion key");
145 goto error;
147 if (file_status(fname) == FN_FILE) {
148 if (replace_file(fname, fname_prev))
149 goto error;
151 if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
152 log_err(LD_FS,"Couldn't write generated onion key to \"%s\".", fname);
153 goto error;
155 log_info(LD_GENERAL, "Rotating onion key");
156 tor_mutex_acquire(key_lock);
157 if (lastonionkey)
158 crypto_free_pk_env(lastonionkey);
159 lastonionkey = onionkey;
160 onionkey = prkey;
161 now = time(NULL);
162 state->LastRotatedOnionKey = onionkey_set_at = now;
163 tor_mutex_release(key_lock);
164 mark_my_descriptor_dirty();
165 or_state_mark_dirty(state, get_options()->AvoidDiskWrites ? now+3600 : 0);
166 return;
167 error:
168 log_warn(LD_GENERAL, "Couldn't rotate onion key.");
171 /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
172 * create a new RSA key and save it in <b>fname</b>. Return the read/created
173 * key, or NULL on error.
175 crypto_pk_env_t *
176 init_key_from_file(const char *fname)
178 crypto_pk_env_t *prkey = NULL;
179 FILE *file = NULL;
181 if (!(prkey = crypto_new_pk_env())) {
182 log_err(LD_GENERAL,"Error constructing key");
183 goto error;
186 switch (file_status(fname)) {
187 case FN_DIR:
188 case FN_ERROR:
189 log_err(LD_FS,"Can't read key from \"%s\"", fname);
190 goto error;
191 case FN_NOENT:
192 log_info(LD_GENERAL, "No key found in \"%s\"; generating fresh key.",
193 fname);
194 if (crypto_pk_generate_key(prkey)) {
195 log_err(LD_GENERAL,"Error generating onion key");
196 goto error;
198 if (crypto_pk_check_key(prkey) <= 0) {
199 log_err(LD_GENERAL,"Generated key seems invalid");
200 goto error;
202 log_info(LD_GENERAL, "Generated key seems valid");
203 if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
204 log_err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
205 goto error;
207 return prkey;
208 case FN_FILE:
209 if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
210 log_err(LD_GENERAL,"Error loading private key.");
211 goto error;
213 return prkey;
214 default:
215 tor_assert(0);
218 error:
219 if (prkey)
220 crypto_free_pk_env(prkey);
221 if (file)
222 fclose(file);
223 return NULL;
226 /** Initialize all OR private keys, and the TLS context, as necessary.
227 * On OPs, this only initializes the tls context. Return 0 on success,
228 * or -1 if Tor should die.
231 init_keys(void)
233 char keydir[512];
234 char fingerprint[FINGERPRINT_LEN+1];
235 /*nickname<space>fp\n\0 */
236 char fingerprint_line[MAX_NICKNAME_LEN+FINGERPRINT_LEN+3];
237 const char *mydesc, *datadir;
238 crypto_pk_env_t *prkey;
239 char digest[20];
240 char *cp;
241 or_options_t *options = get_options();
243 if (!key_lock)
244 key_lock = tor_mutex_new();
246 /* OP's don't need persistent keys; just make up an identity and
247 * initialize the TLS context. */
248 if (!server_mode(options)) {
249 if (!(prkey = crypto_new_pk_env()))
250 return -1;
251 if (crypto_pk_generate_key(prkey))
252 return -1;
253 set_identity_key(prkey);
254 /* Create a TLS context; default the client nickname to "client". */
255 if (tor_tls_context_new(get_identity_key(),
256 options->Nickname ? options->Nickname : "client",
257 MAX_SSL_KEY_LIFETIME) < 0) {
258 log_err(LD_GENERAL,"Error creating TLS context for Tor client.");
259 return -1;
261 return 0;
263 /* Make sure DataDirectory exists, and is private. */
264 datadir = options->DataDirectory;
265 if (check_private_dir(datadir, CPD_CREATE)) {
266 return -1;
268 /* Check the key directory. */
269 tor_snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
270 if (check_private_dir(keydir, CPD_CREATE)) {
271 return -1;
274 /* 1. Read identity key. Make it if none is found. */
275 tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_id_key",datadir);
276 log_info(LD_GENERAL,"Reading/making identity key \"%s\"...",keydir);
277 prkey = init_key_from_file(keydir);
278 if (!prkey) return -1;
279 set_identity_key(prkey);
280 /* 2. Read onion key. Make it if none is found. */
281 tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key",datadir);
282 log_info(LD_GENERAL,"Reading/making onion key \"%s\"...",keydir);
283 prkey = init_key_from_file(keydir);
284 if (!prkey) return -1;
285 set_onion_key(prkey);
287 if (options->command == CMD_RUN_TOR) {
288 /* Only mess with the state file if we're actually running Tor */
289 or_state_t *state = get_or_state();
290 if (state->LastRotatedOnionKey > 100) { /* allow for some parsing slop. */
291 onionkey_set_at = state->LastRotatedOnionKey;
292 } else {
293 /* We have no LastRotatedOnionKey set; either we just created the key
294 * or it's a holdover from 0.1.2.4-alpha-dev or earlier. In either case,
295 * start the clock ticking now so that we will eventually rotate it even
296 * if we don't stay up for a full MIN_ONION_KEY_LIFETIME. */
297 state->LastRotatedOnionKey = onionkey_set_at = time(NULL);
298 or_state_mark_dirty(state,
299 options->AvoidDiskWrites ? time(NULL)+3600 : 0);
303 tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key.old",datadir);
304 if (file_status(keydir) == FN_FILE) {
305 prkey = init_key_from_file(keydir);
306 if (prkey)
307 lastonionkey = prkey;
310 /* 3. Initialize link key and TLS context. */
311 if (tor_tls_context_new(get_identity_key(), options->Nickname,
312 MAX_SSL_KEY_LIFETIME) < 0) {
313 log_err(LD_GENERAL,"Error initializing TLS context");
314 return -1;
316 /* 4. Build our router descriptor. */
317 /* Must be called after keys are initialized. */
318 mydesc = router_get_my_descriptor();
319 if (authdir_mode(options)) {
320 const char *m;
321 /* We need to add our own fingerprint so it gets recognized. */
322 if (dirserv_add_own_fingerprint(options->Nickname, get_identity_key())) {
323 log_err(LD_GENERAL,"Error adding own fingerprint to approved set");
324 return -1;
326 if (!mydesc) {
327 log_err(LD_GENERAL,"Error initializing descriptor.");
328 return -1;
330 if (dirserv_add_descriptor(mydesc, &m) < 0) {
331 log_err(LD_GENERAL,"Unable to add own descriptor to directory: %s",
332 m?m:"<unknown error>");
333 return -1;
337 /* 5. Dump fingerprint to 'fingerprint' */
338 tor_snprintf(keydir,sizeof(keydir),"%s/fingerprint", datadir);
339 log_info(LD_GENERAL,"Dumping fingerprint to \"%s\"...",keydir);
340 if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint, 1)<0) {
341 log_err(LD_GENERAL,"Error computing fingerprint");
342 return -1;
344 tor_assert(strlen(options->Nickname) <= MAX_NICKNAME_LEN);
345 if (tor_snprintf(fingerprint_line, sizeof(fingerprint_line),
346 "%s %s\n",options->Nickname, fingerprint) < 0) {
347 log_err(LD_GENERAL,"Error writing fingerprint line");
348 return -1;
350 /* Check whether we need to write the fingerprint file. */
351 cp = NULL;
352 if (file_status(keydir) == FN_FILE)
353 cp = read_file_to_str(keydir, 0, NULL);
354 if (!cp || strcmp(cp, fingerprint_line)) {
355 if (write_str_to_file(keydir, fingerprint_line, 0)) {
356 log_err(LD_FS, "Error writing fingerprint line to file");
357 return -1;
360 tor_free(cp);
362 log(LOG_NOTICE, LD_GENERAL,
363 "Your Tor server's identity key fingerprint is '%s %s'",
364 options->Nickname, fingerprint);
365 if (!authdir_mode(options))
366 return 0;
367 /* 6. [authdirserver only] load approved-routers file */
368 if (dirserv_load_fingerprint_file() < 0) {
369 log_err(LD_GENERAL,"Error loading fingerprints");
370 return -1;
372 /* 6b. [authdirserver only] add own key to approved directories. */
373 crypto_pk_get_digest(get_identity_key(), digest);
374 if (!router_digest_is_trusted_dir(digest)) {
375 add_trusted_dir_server(options->Nickname, NULL,
376 (uint16_t)options->DirPort,
377 (uint16_t)options->ORPort,
378 digest,
379 options->V1AuthoritativeDir, /* v1 authority */
380 1, /* v2 authority */
381 options->HSAuthoritativeDir /*hidserv authority*/);
383 return 0; /* success */
386 /* Keep track of whether we should upload our server descriptor,
387 * and what type of server we are.
390 /** Whether we can reach our ORPort from the outside. */
391 static int can_reach_or_port = 0;
392 /** Whether we can reach our DirPort from the outside. */
393 static int can_reach_dir_port = 0;
395 /** Forget what we have learned about our reachability status. */
396 void
397 router_reset_reachability(void)
399 can_reach_or_port = can_reach_dir_port = 0;
402 /** Return 1 if ORPort is known reachable; else return 0. */
404 check_whether_orport_reachable(void)
406 or_options_t *options = get_options();
407 return options->AssumeReachable ||
408 can_reach_or_port;
411 /** Return 1 if we don't have a dirport configured, or if it's reachable. */
413 check_whether_dirport_reachable(void)
415 or_options_t *options = get_options();
416 return !options->DirPort ||
417 options->AssumeReachable ||
418 we_are_hibernating() ||
419 can_reach_dir_port;
422 /** Look at a variety of factors, and return 0 if we don't want to
423 * advertise the fact that we have a DirPort open. Else return the
424 * DirPort we want to advertise.
426 * Log a helpful message if we change our mind about whether to publish
427 * a DirPort.
429 static int
430 decide_to_advertise_dirport(or_options_t *options, routerinfo_t *router)
432 static int advertising=1; /* start out assuming we will advertise */
433 int new_choice=1;
434 const char *reason = NULL;
436 /* Section one: reasons to publish or not publish that aren't
437 * worth mentioning to the user, either because they're obvious
438 * or because they're normal behavior. */
440 if (!router->dir_port) /* short circuit the rest of the function */
441 return 0;
442 if (authdir_mode(options)) /* always publish */
443 return router->dir_port;
444 if (we_are_hibernating())
445 return 0;
446 if (!check_whether_dirport_reachable())
447 return 0;
449 /* Section two: reasons to publish or not publish that the user
450 * might find surprising. These are generally config options that
451 * make us choose not to publish. */
453 if (accounting_is_enabled(options)) {
454 /* if we might potentially hibernate */
455 new_choice = 0;
456 reason = "AccountingMax enabled";
457 } else if (router->bandwidthrate < 51200) {
458 /* if we're advertising a small amount */
459 new_choice = 0;
460 reason = "BandwidthRate under 50KB";
463 if (advertising != new_choice) {
464 if (new_choice == 1) {
465 log(LOG_NOTICE, LD_DIR, "Advertising DirPort as %d", router->dir_port);
466 } else {
467 tor_assert(reason);
468 log(LOG_NOTICE, LD_DIR, "Not advertising DirPort (Reason: %s)", reason);
470 advertising = new_choice;
473 return advertising ? router->dir_port : 0;
476 /** Some time has passed, or we just got new directory information.
477 * See if we currently believe our ORPort or DirPort to be
478 * unreachable. If so, launch a new test for it.
480 * For ORPort, we simply try making a circuit that ends at ourselves.
481 * Success is noticed in onionskin_answer().
483 * For DirPort, we make a connection via Tor to our DirPort and ask
484 * for our own server descriptor.
485 * Success is noticed in connection_dir_client_reached_eof().
487 void
488 consider_testing_reachability(int test_or, int test_dir)
490 routerinfo_t *me = router_get_my_routerinfo();
491 int orport_reachable = check_whether_orport_reachable();
492 if (!me)
493 return;
495 if (test_or && (!orport_reachable || !circuit_enough_testing_circs())) {
496 log_info(LD_CIRC, "Testing %s of my ORPort: %s:%d.",
497 !orport_reachable ? "reachability" : "bandwidth",
498 me->address, me->or_port);
499 circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, 0, me, 0, 1, 1);
500 control_event_server_status(LOG_NOTICE,
501 "CHECKING_REACHABILITY ORADDRESS=%s:%d",
502 me->address, me->or_port);
505 if (test_dir && !check_whether_dirport_reachable() &&
506 !connection_get_by_type_addr_port_purpose(
507 CONN_TYPE_DIR, me->addr, me->dir_port,
508 DIR_PURPOSE_FETCH_SERVERDESC)) {
509 /* ask myself, via tor, for my server descriptor. */
510 directory_initiate_command(me->address, me->addr, me->dir_port,
511 0, me->cache_info.identity_digest,
512 DIR_PURPOSE_FETCH_SERVERDESC,
513 1, "authority", NULL, 0);
515 control_event_server_status(LOG_NOTICE,
516 "CHECKING_REACHABILITY DIRADDRESS=%s:%d",
517 me->address, me->dir_port);
521 /** Annotate that we found our ORPort reachable. */
522 void
523 router_orport_found_reachable(void)
525 if (!can_reach_or_port) {
526 routerinfo_t *me = router_get_my_routerinfo();
527 log_notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
528 "the outside. Excellent.%s",
529 get_options()->PublishServerDescriptor ?
530 " Publishing server descriptor." : "");
531 can_reach_or_port = 1;
532 mark_my_descriptor_dirty();
533 if (!me)
534 return;
535 control_event_server_status(LOG_NOTICE,
536 "REACHABILITY_SUCCEEDED ORADDRESS=%s:%d",
537 me->address, me->or_port);
541 /** Annotate that we found our DirPort reachable. */
542 void
543 router_dirport_found_reachable(void)
545 if (!can_reach_dir_port) {
546 routerinfo_t *me = router_get_my_routerinfo();
547 log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
548 "from the outside. Excellent.");
549 can_reach_dir_port = 1;
550 mark_my_descriptor_dirty();
551 if (!me)
552 return;
553 control_event_server_status(LOG_NOTICE,
554 "REACHABILITY_SUCCEEDED DIRADDRESS=%s:%d",
555 me->address, me->dir_port);
559 /** We have enough testing circuits open. Send a bunch of "drop"
560 * cells down each of them, to exercise our bandwidth. */
561 void
562 router_perform_bandwidth_test(int num_circs, time_t now)
564 int num_cells = (int)(get_options()->BandwidthRate * 10 / CELL_NETWORK_SIZE);
565 int max_cells = num_cells < CIRCWINDOW_START ?
566 num_cells : CIRCWINDOW_START;
567 int cells_per_circuit = max_cells / num_circs;
568 origin_circuit_t *circ = NULL;
570 log_notice(LD_OR,"Performing bandwidth self-test...done.");
571 while ((circ = circuit_get_next_by_pk_and_purpose(circ, NULL,
572 CIRCUIT_PURPOSE_TESTING))) {
573 /* dump cells_per_circuit drop cells onto this circ */
574 int i = cells_per_circuit;
575 if (circ->_base.state != CIRCUIT_STATE_OPEN)
576 continue;
577 circ->_base.timestamp_dirty = now;
578 while (i-- > 0) {
579 if (connection_edge_send_command(NULL, TO_CIRCUIT(circ),
580 RELAY_COMMAND_DROP,
581 NULL, 0, circ->cpath->prev)<0) {
582 return; /* stop if error */
588 /** Return true iff we believe ourselves to be an authoritative
589 * directory server.
592 authdir_mode(or_options_t *options)
594 return options->AuthoritativeDir != 0;
596 /** Return true iff we try to stay connected to all ORs at once.
599 clique_mode(or_options_t *options)
601 return authdir_mode(options);
604 /** Return true iff we are trying to be a server.
607 server_mode(or_options_t *options)
609 if (options->ClientOnly) return 0;
610 return (options->ORPort != 0 || options->ORListenAddress);
613 /** Remember if we've advertised ourselves to the dirservers. */
614 static int server_is_advertised=0;
616 /** Return true iff we have published our descriptor lately.
619 advertised_server_mode(void)
621 return server_is_advertised;
625 * Called with a boolean: set whether we have recently published our
626 * descriptor.
628 static void
629 set_server_advertised(int s)
631 server_is_advertised = s;
634 /** Return true iff we are trying to be a socks proxy. */
636 proxy_mode(or_options_t *options)
638 return (options->SocksPort != 0 || options->SocksListenAddress);
641 /** Decide if we're a publishable server. We are a publishable server if:
642 * - We don't have the ClientOnly option set
643 * and
644 * - We have the PublishServerDescriptor option set
645 * and
646 * - We have ORPort set
647 * and
648 * - We believe we are reachable from the outside; or
649 * - We have the AuthoritativeDirectory option set.
651 static int
652 decide_if_publishable_server(void)
654 or_options_t *options = get_options();
656 if (options->ClientOnly)
657 return 0;
658 if (!options->PublishServerDescriptor)
659 return 0;
660 if (!server_mode(options))
661 return 0;
662 if (options->AuthoritativeDir)
663 return 1;
665 return check_whether_orport_reachable();
668 /** Initiate server descriptor upload as reasonable (if server is publishable,
669 * etc). <b>force</b> is as for router_upload_dir_desc_to_dirservers.
671 * We need to rebuild the descriptor if it's dirty even if we're not
672 * uploading, because our reachability testing *uses* our descriptor to
673 * determine what IP address and ports to test.
675 void
676 consider_publishable_server(int force)
678 int rebuilt;
680 if (!server_mode(get_options()))
681 return;
683 rebuilt = router_rebuild_descriptor(0);
684 if (decide_if_publishable_server()) {
685 set_server_advertised(1);
686 if (rebuilt == 0)
687 router_upload_dir_desc_to_dirservers(force);
688 } else {
689 set_server_advertised(0);
694 * Clique maintenance -- to be phased out.
697 /** Return true iff this OR should try to keep connections open to all
698 * other ORs. */
700 router_is_clique_mode(routerinfo_t *router)
702 if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
703 return 1;
704 return 0;
708 * OR descriptor generation.
711 /** My routerinfo. */
712 static routerinfo_t *desc_routerinfo = NULL;
713 /** Since when has our descriptor been "clean"? 0 if we need to regenerate it
714 * now. */
715 static time_t desc_clean_since = 0;
716 /** Boolean: do we need to regenerate the above? */
717 static int desc_needs_upload = 0;
719 /** OR only: If <b>force</b> is true, or we haven't uploaded this
720 * descriptor successfully yet, try to upload our signed descriptor to
721 * all the directory servers we know about.
723 void
724 router_upload_dir_desc_to_dirservers(int force)
726 const char *s;
728 s = router_get_my_descriptor();
729 if (!s) {
730 log_info(LD_GENERAL, "No descriptor; skipping upload");
731 return;
733 if (!get_options()->PublishServerDescriptor)
734 return;
735 if (!force && !desc_needs_upload)
736 return;
737 desc_needs_upload = 0;
738 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
741 /** OR only: Check whether my exit policy says to allow connection to
742 * conn. Return 0 if we accept; non-0 if we reject.
745 router_compare_to_my_exit_policy(edge_connection_t *conn)
747 if (!router_get_my_routerinfo()) /* make sure desc_routerinfo exists */
748 return -1;
750 /* make sure it's resolved to something. this way we can't get a
751 'maybe' below. */
752 if (!conn->_base.addr)
753 return -1;
755 return compare_addr_to_addr_policy(conn->_base.addr, conn->_base.port,
756 desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
759 /** Return true iff I'm a server and <b>digest</b> is equal to
760 * my identity digest. */
762 router_digest_is_me(const char *digest)
764 return identitykey && !memcmp(identitykey_digest, digest, DIGEST_LEN);
767 /** A wrapper around router_digest_is_me(). */
769 router_is_me(routerinfo_t *router)
771 return router_digest_is_me(router->cache_info.identity_digest);
774 /** Return true iff <b>fp</b> is a hex fingerprint of my identity digest. */
776 router_fingerprint_is_me(const char *fp)
778 char digest[DIGEST_LEN];
779 if (strlen(fp) == HEX_DIGEST_LEN &&
780 base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
781 return router_digest_is_me(digest);
783 return 0;
786 /** Return a routerinfo for this OR, rebuilding a fresh one if
787 * necessary. Return NULL on error, or if called on an OP. */
788 routerinfo_t *
789 router_get_my_routerinfo(void)
791 if (!server_mode(get_options()))
792 return NULL;
793 if (router_rebuild_descriptor(0))
794 return NULL;
795 return desc_routerinfo;
798 /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
799 * one if necessary. Return NULL on error.
801 const char *
802 router_get_my_descriptor(void)
804 const char *body;
805 if (!router_get_my_routerinfo())
806 return NULL;
807 /* Make sure this is nul-terminated. */
808 tor_assert(desc_routerinfo->cache_info.saved_location == SAVED_NOWHERE);
809 body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
810 tor_assert(!body[desc_routerinfo->cache_info.signed_descriptor_len]);
811 log_debug(LD_GENERAL,"my desc is '%s'", body);
812 return body;
815 /** A list of nicknames that we've warned about including in our family
816 * declaration verbatim rather than as digests. */
817 static smartlist_t *warned_nonexistent_family = NULL;
819 static int router_guess_address_from_dir_headers(uint32_t *guess);
821 /** Return our current best guess at our address, either because
822 * it's configured in torrc, or because we've learned it from
823 * dirserver headers. */
825 router_pick_published_address(or_options_t *options, uint32_t *addr)
827 if (resolve_my_address(LOG_INFO, options, addr, NULL) < 0) {
828 log_info(LD_CONFIG, "Could not determine our address locally. "
829 "Checking if directory headers provide any hints.");
830 if (router_guess_address_from_dir_headers(addr) < 0) {
831 log_info(LD_CONFIG, "No hints from directory headers either. "
832 "Will try again later.");
833 return -1;
836 return 0;
839 /** If <b>force</b> is true, or our descriptor is out-of-date, rebuild
840 * a fresh routerinfo and signed server descriptor for this OR.
841 * Return 0 on success, -1 on temporary error.
844 router_rebuild_descriptor(int force)
846 routerinfo_t *ri;
847 uint32_t addr;
848 char platform[256];
849 int hibernating = we_are_hibernating();
850 or_options_t *options = get_options();
852 if (desc_clean_since && !force)
853 return 0;
855 if (router_pick_published_address(options, &addr) < 0) {
856 /* Stop trying to rebuild our descriptor every second. We'll
857 * learn that it's time to try again when server_has_changed_ip()
858 * marks it dirty. */
859 desc_clean_since = time(NULL);
860 return -1;
863 ri = tor_malloc_zero(sizeof(routerinfo_t));
864 ri->routerlist_index = -1;
865 ri->address = tor_dup_addr(addr);
866 ri->nickname = tor_strdup(options->Nickname);
867 ri->addr = addr;
868 ri->or_port = options->ORPort;
869 ri->dir_port = options->DirPort;
870 ri->cache_info.published_on = time(NULL);
871 ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from
872 * main thread */
873 ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
874 if (crypto_pk_get_digest(ri->identity_pkey,
875 ri->cache_info.identity_digest)<0) {
876 routerinfo_free(ri);
877 return -1;
879 get_platform_str(platform, sizeof(platform));
880 ri->platform = tor_strdup(platform);
881 ri->bandwidthrate = (int)options->BandwidthRate;
882 ri->bandwidthburst = (int)options->BandwidthBurst;
883 ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
885 if (options->BandwidthRate > options->MaxAdvertisedBandwidth) {
886 if (options->MaxAdvertisedBandwidth > ROUTER_MAX_DECLARED_BANDWIDTH) {
887 ri->bandwidthrate = ROUTER_MAX_DECLARED_BANDWIDTH;
888 } else {
889 ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
893 policies_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
894 options->ExitPolicyRejectPrivate);
896 if (desc_routerinfo) { /* inherit values */
897 ri->is_valid = desc_routerinfo->is_valid;
898 ri->is_running = desc_routerinfo->is_running;
899 ri->is_named = desc_routerinfo->is_named;
901 if (authdir_mode(options))
902 ri->is_valid = ri->is_named = 1; /* believe in yourself */
903 if (options->MyFamily) {
904 smartlist_t *family;
905 if (!warned_nonexistent_family)
906 warned_nonexistent_family = smartlist_create();
907 family = smartlist_create();
908 ri->declared_family = smartlist_create();
909 smartlist_split_string(family, options->MyFamily, ",",
910 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
911 SMARTLIST_FOREACH(family, char *, name,
913 routerinfo_t *member;
914 if (!strcasecmp(name, options->Nickname))
915 member = ri;
916 else
917 member = router_get_by_nickname(name, 1);
918 if (!member) {
919 if (!smartlist_string_isin(warned_nonexistent_family, name) &&
920 !is_legal_hexdigest(name)) {
921 log_warn(LD_CONFIG,
922 "I have no descriptor for the router named \"%s\" "
923 "in my declared family; I'll use the nickname as is, but "
924 "this may confuse clients.", name);
925 smartlist_add(warned_nonexistent_family, tor_strdup(name));
927 smartlist_add(ri->declared_family, name);
928 name = NULL;
929 } else if (router_is_me(member)) {
930 /* Don't list ourself in our own family; that's redundant */
931 } else {
932 char *fp = tor_malloc(HEX_DIGEST_LEN+2);
933 fp[0] = '$';
934 base16_encode(fp+1,HEX_DIGEST_LEN+1,
935 member->cache_info.identity_digest, DIGEST_LEN);
936 smartlist_add(ri->declared_family, fp);
937 if (smartlist_string_isin(warned_nonexistent_family, name))
938 smartlist_string_remove(warned_nonexistent_family, name);
940 tor_free(name);
943 /* remove duplicates from the list */
944 smartlist_sort_strings(ri->declared_family);
945 smartlist_uniq_strings(ri->declared_family);
947 smartlist_free(family);
949 ri->cache_info.signed_descriptor_body = tor_malloc(8192);
950 if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
951 ri, get_identity_key())<0) {
952 log_warn(LD_BUG, "Bug: Couldn't generate router descriptor.");
953 return -1;
955 ri->cache_info.signed_descriptor_len =
956 strlen(ri->cache_info.signed_descriptor_body);
958 router_get_router_hash(ri->cache_info.signed_descriptor_body,
959 ri->cache_info.signed_descriptor_digest);
961 if (desc_routerinfo)
962 routerinfo_free(desc_routerinfo);
963 desc_routerinfo = ri;
965 desc_clean_since = time(NULL);
966 desc_needs_upload = 1;
967 control_event_my_descriptor_changed();
968 return 0;
971 /** Mark descriptor out of date if it's older than <b>when</b> */
972 void
973 mark_my_descriptor_dirty_if_older_than(time_t when)
975 if (desc_clean_since < when)
976 mark_my_descriptor_dirty();
979 /** Call when the current descriptor is out of date. */
980 void
981 mark_my_descriptor_dirty(void)
983 desc_clean_since = 0;
986 /** How frequently will we republish our descriptor because of large (factor
987 * of 2) shifts in estimated bandwidth? */
988 #define MAX_BANDWIDTH_CHANGE_FREQ (20*60)
990 /** Check whether bandwidth has changed a lot since the last time we announced
991 * bandwidth. If so, mark our descriptor dirty. */
992 void
993 check_descriptor_bandwidth_changed(time_t now)
995 static time_t last_changed = 0;
996 uint64_t prev, cur;
997 if (!desc_routerinfo)
998 return;
1000 prev = desc_routerinfo->bandwidthcapacity;
1001 cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
1002 if ((prev != cur && (!prev || !cur)) ||
1003 cur > prev*2 ||
1004 cur < prev/2) {
1005 if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
1006 log_info(LD_GENERAL,
1007 "Measured bandwidth has changed; rebuilding descriptor.");
1008 mark_my_descriptor_dirty();
1009 last_changed = now;
1014 /** Note at log level severity that our best guess of address has changed from
1015 * <b>prev</b> to <b>cur</b>. */
1016 static void
1017 log_addr_has_changed(int severity, uint32_t prev, uint32_t cur)
1019 char addrbuf_prev[INET_NTOA_BUF_LEN];
1020 char addrbuf_cur[INET_NTOA_BUF_LEN];
1021 struct in_addr in_prev;
1022 struct in_addr in_cur;
1024 in_prev.s_addr = htonl(prev);
1025 tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
1027 in_cur.s_addr = htonl(cur);
1028 tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
1030 if (prev)
1031 log_fn(severity, LD_GENERAL,
1032 "Our IP Address has changed from %s to %s; "
1033 "rebuilding descriptor.",
1034 addrbuf_prev, addrbuf_cur);
1035 else
1036 log_notice(LD_GENERAL,
1037 "Guessed our IP address as %s.",
1038 addrbuf_cur);
1041 /** Check whether our own address as defined by the Address configuration
1042 * has changed. This is for routers that get their address from a service
1043 * like dyndns. If our address has changed, mark our descriptor dirty. */
1044 void
1045 check_descriptor_ipaddress_changed(time_t now)
1047 uint32_t prev, cur;
1048 or_options_t *options = get_options();
1049 (void) now;
1051 if (!desc_routerinfo)
1052 return;
1054 prev = desc_routerinfo->addr;
1055 if (resolve_my_address(LOG_INFO, options, &cur, NULL) < 0) {
1056 log_info(LD_CONFIG,"options->Address didn't resolve into an IP.");
1057 return;
1060 if (prev != cur) {
1061 log_addr_has_changed(LOG_INFO, prev, cur);
1062 ip_address_changed(0);
1066 /** The most recently guessed value of our IP address, based on directory
1067 * headers. */
1068 static uint32_t last_guessed_ip = 0;
1070 /** A directory authority told us our IP address is <b>suggestion</b>.
1071 * If this address is different from the one we think we are now, and
1072 * if our computer doesn't actually know its IP address, then switch. */
1073 void
1074 router_new_address_suggestion(const char *suggestion)
1076 uint32_t addr, cur = 0;
1077 struct in_addr in;
1078 or_options_t *options = get_options();
1080 /* first, learn what the IP address actually is */
1081 if (!tor_inet_aton(suggestion, &in)) {
1082 log_debug(LD_DIR, "Malformed X-Your-Address-Is header %s. Ignoring.",
1083 escaped(suggestion));
1084 return;
1086 addr = ntohl(in.s_addr);
1088 log_debug(LD_DIR, "Got X-Your-Address-Is: %s.", suggestion);
1090 if (!server_mode(options)) {
1091 last_guessed_ip = addr; /* store it in case we need it later */
1092 return;
1095 if (resolve_my_address(LOG_INFO, options, &cur, NULL) >= 0) {
1096 /* We're all set -- we already know our address. Great. */
1097 last_guessed_ip = cur; /* store it in case we need it later */
1098 return;
1100 if (is_internal_IP(addr, 0)) {
1101 /* Don't believe anybody who says our IP is, say, 127.0.0.1. */
1102 return;
1105 /* Okay. We can't resolve our own address, and X-Your-Address-Is is giving
1106 * us an answer different from what we had the last time we managed to
1107 * resolve it. */
1108 if (last_guessed_ip != addr) {
1109 control_event_server_status(LOG_NOTICE,
1110 "EXTERNAL_ADDRESS ADDRESS=%s METHOD=DIRSERV",
1111 suggestion);
1112 log_addr_has_changed(LOG_NOTICE, last_guessed_ip, addr);
1113 ip_address_changed(0);
1114 last_guessed_ip = addr; /* router_rebuild_descriptor() will fetch it */
1118 /** We failed to resolve our address locally, but we'd like to build
1119 * a descriptor and publish / test reachability. If we have a guess
1120 * about our address based on directory headers, answer it and return
1121 * 0; else return -1. */
1122 static int
1123 router_guess_address_from_dir_headers(uint32_t *guess)
1125 if (last_guessed_ip) {
1126 *guess = last_guessed_ip;
1127 return 0;
1129 return -1;
1132 /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
1133 * string describing the version of Tor and the operating system we're
1134 * currently running on.
1136 void
1137 get_platform_str(char *platform, size_t len)
1139 tor_snprintf(platform, len, "Tor %s on %s",
1140 VERSION, get_uname());
1141 return;
1144 /* XXX need to audit this thing and count fenceposts. maybe
1145 * refactor so we don't have to keep asking if we're
1146 * near the end of maxlen?
1148 #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
1150 /** OR only: Given a routerinfo for this router, and an identity key to sign
1151 * with, encode the routerinfo as a signed server descriptor and write the
1152 * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
1153 * failure, and the number of bytes used on success.
1156 router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
1157 crypto_pk_env_t *ident_key)
1159 char *onion_pkey; /* Onion key, PEM-encoded. */
1160 char *identity_pkey; /* Identity key, PEM-encoded. */
1161 char digest[DIGEST_LEN];
1162 char published[ISO_TIME_LEN+1];
1163 char fingerprint[FINGERPRINT_LEN+1];
1164 size_t onion_pkeylen, identity_pkeylen;
1165 size_t written;
1166 int result=0;
1167 addr_policy_t *tmpe;
1168 char *bandwidth_usage;
1169 char *family_line;
1170 #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
1171 char *s_dup;
1172 const char *cp;
1173 routerinfo_t *ri_tmp;
1174 #endif
1175 or_options_t *options = get_options();
1177 /* Make sure the identity key matches the one in the routerinfo. */
1178 if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
1179 log_warn(LD_BUG,"Tried to sign a router with a private key that didn't "
1180 "match router's public key!");
1181 return -1;
1184 /* record our fingerprint, so we can include it in the descriptor */
1185 if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
1186 log_err(LD_BUG,"Error computing fingerprint");
1187 return -1;
1190 /* PEM-encode the onion key */
1191 if (crypto_pk_write_public_key_to_string(router->onion_pkey,
1192 &onion_pkey,&onion_pkeylen)<0) {
1193 log_warn(LD_BUG,"write onion_pkey to string failed!");
1194 return -1;
1197 /* PEM-encode the identity key key */
1198 if (crypto_pk_write_public_key_to_string(router->identity_pkey,
1199 &identity_pkey,&identity_pkeylen)<0) {
1200 log_warn(LD_BUG,"write identity_pkey to string failed!");
1201 tor_free(onion_pkey);
1202 return -1;
1205 /* Encode the publication time. */
1206 format_iso_time(published, router->cache_info.published_on);
1208 /* How busy have we been? */
1209 bandwidth_usage = rep_hist_get_bandwidth_lines();
1211 if (router->declared_family && smartlist_len(router->declared_family)) {
1212 size_t n;
1213 char *s = smartlist_join_strings(router->declared_family, " ", 0, &n);
1214 n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */
1215 family_line = tor_malloc(n);
1216 tor_snprintf(family_line, n, "family %s\n", s);
1217 tor_free(s);
1218 } else {
1219 family_line = tor_strdup("");
1222 /* Generate the easy portion of the router descriptor. */
1223 result = tor_snprintf(s, maxlen,
1224 "router %s %s %d 0 %d\n"
1225 "platform %s\n"
1226 "published %s\n"
1227 "opt fingerprint %s\n"
1228 "uptime %ld\n"
1229 "bandwidth %d %d %d\n"
1230 "onion-key\n%s"
1231 "signing-key\n%s"
1232 #ifndef USE_EVENTDNS
1233 "opt eventdns 0\n"
1234 #endif
1235 "%s%s%s",
1236 router->nickname,
1237 router->address,
1238 router->or_port,
1239 decide_to_advertise_dirport(options, router),
1240 router->platform,
1241 published,
1242 fingerprint,
1243 stats_n_seconds_working,
1244 (int) router->bandwidthrate,
1245 (int) router->bandwidthburst,
1246 (int) router->bandwidthcapacity,
1247 onion_pkey, identity_pkey,
1248 family_line, bandwidth_usage,
1249 we_are_hibernating() ? "opt hibernating 1\n" : "");
1250 tor_free(family_line);
1251 tor_free(onion_pkey);
1252 tor_free(identity_pkey);
1253 tor_free(bandwidth_usage);
1255 if (result < 0) {
1256 log_warn(LD_BUG,"descriptor snprintf #1 ran out of room!");
1257 return -1;
1259 /* From now on, we use 'written' to remember the current length of 's'. */
1260 written = result;
1262 if (options->ContactInfo && strlen(options->ContactInfo)) {
1263 result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
1264 options->ContactInfo);
1265 if (result<0) {
1266 log_warn(LD_BUG,"descriptor snprintf #2 ran out of room!");
1267 return -1;
1269 written += result;
1272 /* Write the exit policy to the end of 's'. */
1273 tmpe = router->exit_policy;
1274 if (dns_seems_to_be_broken()) {
1275 /* DNS is screwed up; don't claim to be an exit. */
1276 strlcat(s+written, "reject *:*\n", maxlen-written);
1277 written += strlen("reject *:*\n");
1278 tmpe = NULL;
1280 for ( ; tmpe; tmpe=tmpe->next) {
1281 result = policy_write_item(s+written, maxlen-written, tmpe);
1282 if (result < 0) {
1283 log_warn(LD_BUG,"descriptor policy_write_item ran out of room!");
1284 return -1;
1286 tor_assert(result == (int)strlen(s+written));
1287 written += result;
1288 if (written+2 > maxlen) {
1289 log_warn(LD_BUG,"descriptor policy_write_item ran out of room (2)!");
1290 return -1;
1292 s[written++] = '\n';
1295 if (written+256 > maxlen) { /* Not enough room for signature. */
1296 log_warn(LD_BUG,"not enough room left in descriptor for signature!");
1297 return -1;
1300 /* Sign the directory */
1301 strlcpy(s+written, "router-signature\n", maxlen-written);
1302 written += strlen(s+written);
1303 s[written] = '\0';
1304 if (router_get_router_hash(s, digest) < 0) {
1305 return -1;
1308 note_crypto_pk_op(SIGN_RTR);
1309 if (router_append_dirobj_signature(s+written,maxlen-written,
1310 digest,ident_key)<0) {
1311 log_warn(LD_BUG, "Couldn't sign router descriptor");
1312 return -1;
1314 written += strlen(s+written);
1316 if (written+2 > maxlen) {
1317 log_warn(LD_BUG,"Not enough room to finish descriptor.");
1318 return -1;
1320 /* include a last '\n' */
1321 s[written] = '\n';
1322 s[written+1] = 0;
1324 #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
1325 cp = s_dup = tor_strdup(s);
1326 ri_tmp = router_parse_entry_from_string(cp, NULL, 1);
1327 if (!ri_tmp) {
1328 log_err(LD_BUG,
1329 "We just generated a router descriptor we can't parse.");
1330 log_err(LD_BUG, "Descriptor was: <<%s>>", s);
1331 return -1;
1333 tor_free(s_dup);
1334 routerinfo_free(ri_tmp);
1335 #endif
1337 return written+1;
1340 /** Return true iff <b>s</b> is a legally valid server nickname. */
1342 is_legal_nickname(const char *s)
1344 size_t len;
1345 tor_assert(s);
1346 len = strlen(s);
1347 return len > 0 && len <= MAX_NICKNAME_LEN &&
1348 strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
1351 /** Return true iff <b>s</b> is a legally valid server nickname or
1352 * hex-encoded identity-key digest. */
1354 is_legal_nickname_or_hexdigest(const char *s)
1356 if (*s!='$')
1357 return is_legal_nickname(s);
1358 else
1359 return is_legal_hexdigest(s);
1362 /** Return true iff <b>s</b> is a legally valid hex-encoded identity-key
1363 * digest. */
1365 is_legal_hexdigest(const char *s)
1367 size_t len;
1368 tor_assert(s);
1369 if (s[0] == '$') s++;
1370 len = strlen(s);
1371 if (len > HEX_DIGEST_LEN) {
1372 if (s[HEX_DIGEST_LEN] == '=' ||
1373 s[HEX_DIGEST_LEN] == '~') {
1374 if (!is_legal_nickname(s+HEX_DIGEST_LEN+1))
1375 return 0;
1376 } else {
1377 return 0;
1380 return (len >= HEX_DIGEST_LEN &&
1381 strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN);
1384 /** Set <b>buf</b> (which must have MAX_VERBOSE_NICKNAME_LEN+1 bytes) to the
1385 * verbose representation of the identity of <b>router</b>. The format is:
1386 * A dollar sign.
1387 * The upper-case hexadecimal encoding of the SHA1 hash of router's identity.
1388 * A "=" if the router is named; a "~" if it is not.
1389 * The router's nickname.
1391 void
1392 router_get_verbose_nickname(char *buf, routerinfo_t *router)
1394 buf[0] = '$';
1395 base16_encode(buf+1, HEX_DIGEST_LEN+1, router->cache_info.identity_digest,
1396 DIGEST_LEN);
1397 buf[1+HEX_DIGEST_LEN] = router->is_named ? '=' : '~';
1398 strlcpy(buf+1+HEX_DIGEST_LEN+1, router->nickname, MAX_NICKNAME_LEN+1);
1401 /** Forget that we have issued any router-related warnings, so that we'll
1402 * warn again if we see the same errors. */
1403 void
1404 router_reset_warnings(void)
1406 if (warned_nonexistent_family) {
1407 SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
1408 smartlist_clear(warned_nonexistent_family);
1412 /** Release all static resources held in router.c */
1413 void
1414 router_free_all(void)
1416 if (onionkey)
1417 crypto_free_pk_env(onionkey);
1418 if (lastonionkey)
1419 crypto_free_pk_env(lastonionkey);
1420 if (identitykey)
1421 crypto_free_pk_env(identitykey);
1422 if (key_lock)
1423 tor_mutex_free(key_lock);
1424 if (desc_routerinfo)
1425 routerinfo_free(desc_routerinfo);
1426 if (warned_nonexistent_family) {
1427 SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
1428 smartlist_free(warned_nonexistent_family);