Merge branch 'maint-0.4.8'
[tor.git] / src / core / or / protover.c
blob1ac32bf06cb8542756bcaf8f9e26f2c16e95e5a9
1 /* Copyright (c) 2016-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file protover.c
6 * \brief Versioning information for different pieces of the Tor protocol.
8 * Starting in version 0.2.9.3-alpha, Tor places separate version numbers on
9 * each of the different components of its protocol. Relays use these numbers
10 * to advertise what versions of the protocols they can support, and clients
11 * use them to find what they can ask a given relay to do. Authorities vote
12 * on the supported protocol versions for each relay, and also vote on the
13 * which protocols you should have to support in order to be on the Tor
14 * network. All Tor instances use these required/recommended protocol versions
15 * to tell what level of support for recent protocols each relay has, and
16 * to decide whether they should be running given their current protocols.
18 * The main advantage of these protocol versions numbers over using Tor
19 * version numbers is that they allow different implementations of the Tor
20 * protocols to develop independently, without having to claim compatibility
21 * with specific versions of Tor.
22 **/
24 #define PROTOVER_PRIVATE
26 #include "core/or/or.h"
27 #include "core/or/protover.h"
28 #include "core/or/versions.h"
29 #include "lib/tls/tortls.h"
31 static const smartlist_t *get_supported_protocol_list(void);
32 static int protocol_list_contains(const smartlist_t *protos,
33 protocol_type_t pr, uint32_t ver);
34 static const proto_entry_t *find_entry_by_name(const smartlist_t *protos,
35 const char *name);
37 /** Mapping between protocol type string and protocol type. */
38 /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
39 static const struct {
40 protocol_type_t protover_type;
41 const char *name;
42 /* If you add a new protocol here, you probably also want to add
43 * parsing for it in summarize_protover_flags(), so that it has a
44 * summary flag in routerstatus_t */
45 } PROTOCOL_NAMES[] = {
46 { PRT_LINK, "Link" },
47 { PRT_LINKAUTH, "LinkAuth" },
48 { PRT_RELAY, "Relay" },
49 { PRT_DIRCACHE, "DirCache" },
50 { PRT_HSDIR, "HSDir" },
51 { PRT_HSINTRO, "HSIntro" },
52 { PRT_HSREND, "HSRend" },
53 { PRT_DESC, "Desc" },
54 { PRT_MICRODESC, "Microdesc"},
55 { PRT_PADDING, "Padding"},
56 { PRT_CONS, "Cons" },
57 { PRT_FLOWCTRL, "FlowCtrl"},
58 { PRT_CONFLUX, "Conflux"},
61 #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
63 /* Maximum allowed length of any single subprotocol name. */
64 // C_RUST_COUPLED: src/rust/protover/protover.rs
65 // `MAX_PROTOCOL_NAME_LENGTH`
66 static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
68 /**
69 * Given a protocol_type_t, return the corresponding string used in
70 * descriptors.
72 STATIC const char *
73 protocol_type_to_str(protocol_type_t pr)
75 unsigned i;
76 for (i=0; i < N_PROTOCOL_NAMES; ++i) {
77 if (PROTOCOL_NAMES[i].protover_type == pr)
78 return PROTOCOL_NAMES[i].name;
80 /* LCOV_EXCL_START */
81 tor_assert_nonfatal_unreached_once();
82 return "UNKNOWN";
83 /* LCOV_EXCL_STOP */
86 /**
87 * Release all space held by a single proto_entry_t structure
89 STATIC void
90 proto_entry_free_(proto_entry_t *entry)
92 if (!entry)
93 return;
94 tor_free(entry->name);
95 tor_free(entry);
98 /** The largest possible protocol version. */
99 #define MAX_PROTOCOL_VERSION (63)
102 * Given a string <b>s</b> and optional end-of-string pointer
103 * <b>end_of_range</b>, parse the protocol range and store it in
104 * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
105 * U-U, where U is an unsigned integer between 0 and 63 inclusive.
107 static int
108 parse_version_range(const char *s, const char *end_of_range,
109 uint32_t *low_out, uint32_t *high_out)
111 uint32_t low, high;
112 char *next = NULL;
113 int ok;
115 tor_assert(high_out);
116 tor_assert(low_out);
118 if (BUG(!end_of_range))
119 end_of_range = s + strlen(s); // LCOV_EXCL_LINE
121 /* A range must start with a digit. */
122 if (!TOR_ISDIGIT(*s)) {
123 goto error;
126 /* Note that this wouldn't be safe if we didn't know that eventually,
127 * we'd hit a NUL */
128 low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
129 if (!ok)
130 goto error;
131 if (next > end_of_range)
132 goto error;
133 if (next == end_of_range) {
134 high = low;
135 goto done;
138 if (*next != '-')
139 goto error;
140 s = next+1;
142 /* ibid */
143 if (!TOR_ISDIGIT(*s)) {
144 goto error;
146 high = (uint32_t) tor_parse_ulong(s, 10, 0,
147 MAX_PROTOCOL_VERSION, &ok, &next);
148 if (!ok)
149 goto error;
150 if (next != end_of_range)
151 goto error;
153 if (low > high)
154 goto error;
156 done:
157 *high_out = high;
158 *low_out = low;
159 return 0;
161 error:
162 return -1;
165 static int
166 is_valid_keyword(const char *s, size_t n)
168 for (size_t i = 0; i < n; i++) {
169 if (!TOR_ISALNUM(s[i]) && s[i] != '-')
170 return 0;
172 return 1;
175 /** The x'th bit in a bitmask. */
176 #define BIT(x) (UINT64_C(1)<<(x))
179 * Return a bitmask so that bits 'low' through 'high' inclusive are set,
180 * and all other bits are cleared.
182 static uint64_t
183 bitmask_for_range(uint32_t low, uint32_t high)
185 uint64_t mask = ~(uint64_t)0;
186 mask <<= 63 - high;
187 mask >>= 63 - high + low;
188 mask <<= low;
189 return mask;
192 /** Parse a single protocol entry from <b>s</b> up to an optional
193 * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
194 * on error.
196 * A protocol entry has a keyword, an = sign, and zero or more ranges. */
197 static proto_entry_t *
198 parse_single_entry(const char *s, const char *end_of_entry)
200 proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
201 const char *equals;
203 if (BUG (!end_of_entry))
204 end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
206 /* There must be an =. */
207 equals = memchr(s, '=', end_of_entry - s);
208 if (!equals)
209 goto error;
211 /* The name must be nonempty */
212 if (equals == s)
213 goto error;
215 /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
216 if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
217 log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
218 "protocol name. This is possibly an attack or a bug, unless "
219 "the Tor network truly supports protocol names larger than "
220 "%ud characters. The offending string was: %s",
221 MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
222 goto error;
225 /* The name must contain only alphanumeric characters and hyphens. */
226 if (!is_valid_keyword(s, equals-s))
227 goto error;
229 out->name = tor_strndup(s, equals-s);
231 tor_assert(equals < end_of_entry);
233 s = equals + 1;
234 while (s < end_of_entry) {
235 const char *comma = memchr(s, ',', end_of_entry-s);
236 if (! comma)
237 comma = end_of_entry;
239 uint32_t low=0, high=0;
240 if (parse_version_range(s, comma, &low, &high) < 0) {
241 goto error;
244 out->bitmask |= bitmask_for_range(low,high);
246 s = comma;
247 // Skip the comma separator between ranges. Don't ignore a trailing comma.
248 if (s < (end_of_entry - 1))
249 ++s;
252 return out;
254 error:
255 proto_entry_free(out);
256 return NULL;
260 * Parse the protocol list from <b>s</b> and return it as a smartlist of
261 * proto_entry_t
263 STATIC smartlist_t *
264 parse_protocol_list(const char *s)
266 smartlist_t *entries = smartlist_new();
268 while (*s) {
269 /* Find the next space or the NUL. */
270 const char *end_of_entry = strchr(s, ' ');
271 proto_entry_t *entry;
272 if (!end_of_entry)
273 end_of_entry = s + strlen(s);
275 entry = parse_single_entry(s, end_of_entry);
277 if (! entry)
278 goto error;
280 smartlist_add(entries, entry);
282 s = end_of_entry;
283 while (*s == ' ')
284 ++s;
287 return entries;
289 error:
290 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
291 smartlist_free(entries);
292 return NULL;
296 * Return true if the unparsed protover list in <b>s</b> contains a
297 * parsing error, such as extra commas, a bad number, or an over-long
298 * name.
300 bool
301 protover_list_is_invalid(const char *s)
303 smartlist_t *list = parse_protocol_list(s);
304 if (!list)
305 return true; /* yes, has a dangerous name */
306 SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
307 smartlist_free(list);
308 return false; /* no, looks fine */
312 * Given a protocol type and version number, return true iff we know
313 * how to speak that protocol.
316 protover_is_supported_here(protocol_type_t pr, uint32_t ver)
318 const smartlist_t *ours = get_supported_protocol_list();
319 return protocol_list_contains(ours, pr, ver);
323 * Return true iff "list" encodes a protocol list that includes support for
324 * the indicated protocol and version.
326 * If the protocol list is unparseable, treat it as if it defines no
327 * protocols, and return 0.
330 protocol_list_supports_protocol(const char *list, protocol_type_t tp,
331 uint32_t version)
333 /* NOTE: This is a pretty inefficient implementation. If it ever shows
334 * up in profiles, we should memoize it.
336 smartlist_t *protocols = parse_protocol_list(list);
337 if (!protocols) {
338 return 0;
340 int contains = protocol_list_contains(protocols, tp, version);
342 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
343 smartlist_free(protocols);
344 return contains;
348 * Return true iff "list" encodes a protocol list that includes support for
349 * the indicated protocol and version, or some later version.
351 * If the protocol list is unparseable, treat it as if it defines no
352 * protocols, and return 0.
355 protocol_list_supports_protocol_or_later(const char *list,
356 protocol_type_t tp,
357 uint32_t version)
359 /* NOTE: This is a pretty inefficient implementation. If it ever shows
360 * up in profiles, we should memoize it.
362 smartlist_t *protocols = parse_protocol_list(list);
363 if (!protocols) {
364 return 0;
366 const char *pr_name = protocol_type_to_str(tp);
368 int contains = 0;
369 const uint64_t mask = bitmask_for_range(version, 63);
371 SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
372 if (strcasecmp(proto->name, pr_name))
373 continue;
374 if (0 != (proto->bitmask & mask)) {
375 contains = 1;
376 goto found;
378 } SMARTLIST_FOREACH_END(proto);
380 found:
381 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
382 smartlist_free(protocols);
383 return contains;
387 * XXX START OF HAZARDOUS ZONE XXX
389 /* All protocol version that this relay version supports. */
390 #define PR_CONFLUX_V "1"
391 #define PR_CONS_V "1-2"
392 #define PR_DESC_V "1-3"
393 #define PR_DIRCACHE_V "2"
394 #define PR_FLOWCTRL_V "1-2"
395 #define PR_HSDIR_V "2"
396 #define PR_HSINTRO_V "4-5"
397 #define PR_HSREND_V "1-2"
398 #define PR_LINK_V "1-5"
399 #ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
400 #define PR_LINKAUTH_V "1,3"
401 #else
402 #define PR_LINKAUTH_V "3"
403 #endif
404 #define PR_MICRODESC_V "1-3"
405 #define PR_PADDING_V "2"
406 #define PR_RELAY_V "2-4"
408 /** Return the string containing the supported version for the given protocol
409 * type. */
410 const char *
411 protover_get_supported(const protocol_type_t type)
413 switch (type) {
414 case PRT_CONFLUX: return PR_CONFLUX_V;
415 case PRT_CONS: return PR_CONS_V;
416 case PRT_DESC: return PR_DESC_V;
417 case PRT_DIRCACHE: return PR_DIRCACHE_V;
418 case PRT_FLOWCTRL: return PR_FLOWCTRL_V;
419 case PRT_HSDIR: return PR_HSDIR_V;
420 case PRT_HSINTRO: return PR_HSINTRO_V;
421 case PRT_HSREND: return PR_HSREND_V;
422 case PRT_LINK: return PR_LINK_V;
423 case PRT_LINKAUTH: return PR_LINKAUTH_V;
424 case PRT_MICRODESC: return PR_MICRODESC_V;
425 case PRT_PADDING: return PR_PADDING_V;
426 case PRT_RELAY: return PR_RELAY_V;
427 default:
428 tor_assert_unreached();
432 /** Return the canonical string containing the list of protocols
433 * that we support.
435 /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
436 const char *
437 protover_get_supported_protocols(void)
439 /* WARNING!
441 * Remember to edit the SUPPORTED_PROTOCOLS list in protover.rs if you
442 * are editing this list.
446 * XXX: WARNING!
448 * Be EXTREMELY CAREFUL when *removing* versions from this list. If you
449 * remove an entry while it still appears as "recommended" in the consensus,
450 * you'll cause all the instances without it to warn.
452 * If you remove an entry while it still appears as "required" in the
453 * consensus, you'll cause all the instances without it to refuse to connect
454 * to the network, and shut down.
456 * If you need to remove a version from this list, you need to make sure that
457 * it is not listed in the _current consensuses_: just removing it from the
458 * required list below is NOT ENOUGH. You need to remove it from the
459 * required list, and THEN let the authorities upgrade and vote on new
460 * consensuses without it. Only once those consensuses are out is it safe to
461 * remove from this list.
463 * One concrete example of a very dangerous race that could occur:
465 * Suppose that the client supports protocols "HsDir=1-2" and the consensus
466 * requires protocols "HsDir=1-2. If the client supported protocol list is
467 * then changed to "HSDir=2", while the consensus stills lists "HSDir=1-2",
468 * then these clients, even very recent ones, will shut down because they
469 * don't support "HSDir=1".
471 * And so, changes need to be done in strict sequence as described above.
473 * XXX: WARNING!
476 return
477 "Conflux=" PR_CONFLUX_V " "
478 "Cons=" PR_CONS_V " "
479 "Desc=" PR_DESC_V " "
480 "DirCache=" PR_DIRCACHE_V " "
481 "FlowCtrl=" PR_FLOWCTRL_V " "
482 "HSDir=" PR_HSDIR_V " "
483 "HSIntro=" PR_HSINTRO_V " "
484 "HSRend=" PR_HSREND_V " "
485 "Link=" PR_LINK_V " "
486 "LinkAuth=" PR_LINKAUTH_V " "
487 "Microdesc=" PR_MICRODESC_V " "
488 "Padding=" PR_PADDING_V " "
489 "Relay=" PR_RELAY_V;
493 * XXX: WARNING!
495 * The recommended and required values are hardwired, to avoid disaster. Voting
496 * on the wrong subprotocols here has the potential to take down the network.
498 * In particular, you need to be EXTREMELY CAREFUL before adding new versions
499 * to the required protocol list. Doing so will cause every relay or client
500 * that doesn't support those versions to refuse to connect to the network and
501 * shut down.
503 * Note that this applies to versions, not just protocols! If you say that
504 * Foobar=8-9 is required, and the client only has Foobar=9, it will shut down.
506 * It is okay to do this only for SUPER OLD relays that are not supported on
507 * the network anyway. For clients, we really shouldn't kick them off the
508 * network unless their presence is causing serious active harm.
510 * The following required and recommended lists MUST be changed BEFORE the
511 * supported list above is changed, so that these lists appear in the
512 * consensus BEFORE clients need them.
514 * Please, see the warning in protocol_get_supported_versions().
516 * XXX: WARNING!
519 /** Return the recommended client protocols list that directory authorities
520 * put in the consensus. */
521 const char *
522 protover_get_recommended_client_protocols(void)
524 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
525 "Link=4-5 Microdesc=2 Relay=2";
528 /** Return the recommended relay protocols list that directory authorities
529 * put in the consensus. */
530 const char *
531 protover_get_recommended_relay_protocols(void)
533 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
534 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2";
537 /** Return the required client protocols list that directory authorities
538 * put in the consensus. */
539 const char *
540 protover_get_required_client_protocols(void)
542 return "Cons=2 Desc=2 Link=4 Microdesc=2 Relay=2";
545 /** Return the required relay protocols list that directory authorities
546 * put in the consensus. */
547 const char *
548 protover_get_required_relay_protocols(void)
550 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
551 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2";
555 * XXX END OF HAZARDOUS ZONE XXX
558 /** The protocols from protover_get_supported_protocols(), as parsed into a
559 * list of proto_entry_t values. Access this via
560 * get_supported_protocol_list. */
561 static smartlist_t *supported_protocol_list = NULL;
563 /** Return a pointer to a smartlist of proto_entry_t for the protocols
564 * we support. */
565 static const smartlist_t *
566 get_supported_protocol_list(void)
568 if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
569 supported_protocol_list =
570 parse_protocol_list(protover_get_supported_protocols());
572 return supported_protocol_list;
575 /** Return the number of trailing zeros in x. Undefined if x is 0. */
576 static int
577 trailing_zeros(uint64_t x)
579 #ifdef __GNUC__
580 return __builtin_ctzll((unsigned long long)x);
581 #else
582 int i;
583 for (i = 0; i <= 64; ++i) {
584 if (x&1)
585 return i;
586 x>>=1;
588 return i;
589 #endif /* defined(__GNUC__) */
593 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
594 * as one or more newly allocated strings.
596 static void
597 proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
599 smartlist_add_asprintf(chunks, "%s=", entry->name);
601 uint64_t mask = entry->bitmask;
602 int shift = 0; // how much have we shifted by so far?
603 bool first = true;
604 while (mask) {
605 const char *comma = first ? "" : ",";
606 if (first) {
607 first = false;
609 int zeros = trailing_zeros(mask);
610 mask >>= zeros;
611 shift += zeros;
612 int ones = !mask ? 64 : trailing_zeros(~mask);
613 if (ones == 1) {
614 smartlist_add_asprintf(chunks, "%s%d", comma, shift);
615 } else {
616 smartlist_add_asprintf(chunks, "%s%d-%d", comma,
617 shift, shift + ones - 1);
619 if (ones == 64) {
620 break; // avoid undefined behavior; can't shift by 64.
622 mask >>= ones;
623 shift += ones;
627 /** Given a list of space-separated proto_entry_t items,
628 * encode it into a newly allocated space-separated string. */
629 STATIC char *
630 encode_protocol_list(const smartlist_t *sl)
632 const char *separator = "";
633 smartlist_t *chunks = smartlist_new();
634 SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
635 smartlist_add_strdup(chunks, separator);
637 proto_entry_encode_into(chunks, ent);
639 separator = " ";
640 } SMARTLIST_FOREACH_END(ent);
642 char *result = smartlist_join_strings(chunks, "", 0, NULL);
644 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
645 smartlist_free(chunks);
647 return result;
651 * Protocol voting implementation.
653 * Given a list of strings describing protocol versions, return a newly
654 * allocated string encoding all of the protocols that are listed by at
655 * least <b>threshold</b> of the inputs.
657 * The string is minimal and sorted according to the rules of
658 * contract_protocol_list above.
660 char *
661 protover_compute_vote(const smartlist_t *list_of_proto_strings,
662 int threshold)
664 // we use u8 counters below.
665 tor_assert(smartlist_len(list_of_proto_strings) < 256);
667 if (smartlist_len(list_of_proto_strings) == 0) {
668 return tor_strdup("");
671 smartlist_t *parsed = smartlist_new(); // smartlist of smartlist of entries
672 smartlist_t *proto_names = smartlist_new(); // smartlist of strings
673 smartlist_t *result = smartlist_new(); // smartlist of entries
675 // First, parse the inputs, and accumulate a list of protocol names.
676 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
677 smartlist_t *unexpanded = parse_protocol_list(vote);
678 if (! unexpanded) {
679 log_warn(LD_NET, "I failed with parsing a protocol list from "
680 "an authority. The offending string was: %s",
681 escaped(vote));
682 continue;
684 SMARTLIST_FOREACH_BEGIN(unexpanded, const proto_entry_t *, ent) {
685 if (!smartlist_contains_string(proto_names,ent->name)) {
686 smartlist_add(proto_names, ent->name);
688 } SMARTLIST_FOREACH_END(ent);
689 smartlist_add(parsed, unexpanded);
690 } SMARTLIST_FOREACH_END(vote);
692 // Sort the list of names.
693 smartlist_sort_strings(proto_names);
695 // For each named protocol, compute the consensus.
697 // This is not super-efficient, but it's not critical path.
698 SMARTLIST_FOREACH_BEGIN(proto_names, const char *, name) {
699 uint8_t counts[64];
700 memset(counts, 0, sizeof(counts));
701 // Count how many votes we got for each bit.
702 SMARTLIST_FOREACH_BEGIN(parsed, const smartlist_t *, vote) {
703 const proto_entry_t *ent = find_entry_by_name(vote, name);
704 if (! ent)
705 continue;
707 for (int i = 0; i < 64; ++i) {
708 if ((ent->bitmask & BIT(i)) != 0) {
709 ++ counts[i];
712 } SMARTLIST_FOREACH_END(vote);
714 uint64_t result_bitmask = 0;
715 for (int i = 0; i < 64; ++i) {
716 if (counts[i] >= threshold) {
717 result_bitmask |= BIT(i);
720 if (result_bitmask != 0) {
721 proto_entry_t *newent = tor_malloc_zero(sizeof(proto_entry_t));
722 newent->name = tor_strdup(name);
723 newent->bitmask = result_bitmask;
724 smartlist_add(result, newent);
726 } SMARTLIST_FOREACH_END(name);
728 char *consensus = encode_protocol_list(result);
730 SMARTLIST_FOREACH(result, proto_entry_t *, ent, proto_entry_free(ent));
731 smartlist_free(result);
732 smartlist_free(proto_names); // no need to free members; they are aliases.
733 SMARTLIST_FOREACH_BEGIN(parsed, smartlist_t *, v) {
734 SMARTLIST_FOREACH(v, proto_entry_t *, ent, proto_entry_free(ent));
735 smartlist_free(v);
736 } SMARTLIST_FOREACH_END(v);
737 smartlist_free(parsed);
739 return consensus;
742 /** Return true if every protocol version described in the string <b>s</b> is
743 * one that we support, and false otherwise. If <b>missing_out</b> is
744 * provided, set it to the list of protocols we do not support.
746 * If the protocol version string is unparseable, treat it as if it defines no
747 * protocols, and return 1.
750 protover_all_supported(const char *s, char **missing_out)
752 if (!s) {
753 return 1;
756 smartlist_t *entries = parse_protocol_list(s);
757 if (BUG(entries == NULL)) {
758 log_warn(LD_NET, "Received an unparseable protocol list %s"
759 " from the consensus", escaped(s));
760 return 1;
762 const smartlist_t *supported = get_supported_protocol_list();
763 smartlist_t *missing = smartlist_new();
765 SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
766 const proto_entry_t *mine = find_entry_by_name(supported, ent->name);
767 if (mine == NULL) {
768 if (ent->bitmask != 0) {
769 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
770 m->name = tor_strdup(ent->name);
771 m->bitmask = ent->bitmask;
772 smartlist_add(missing, m);
774 continue;
777 uint64_t missing_mask = ent->bitmask & ~mine->bitmask;
778 if (missing_mask != 0) {
779 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
780 m->name = tor_strdup(ent->name);
781 m->bitmask = missing_mask;
782 smartlist_add(missing, m);
784 } SMARTLIST_FOREACH_END(ent);
786 const int all_supported = (smartlist_len(missing) == 0);
787 if (!all_supported && missing_out) {
788 *missing_out = encode_protocol_list(missing);
791 SMARTLIST_FOREACH(missing, proto_entry_t *, ent, proto_entry_free(ent));
792 smartlist_free(missing);
794 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
795 smartlist_free(entries);
797 return all_supported;
800 /** Helper: return the member of 'protos' whose name is
801 * 'name', or NULL if there is no such member. */
802 static const proto_entry_t *
803 find_entry_by_name(const smartlist_t *protos, const char *name)
805 if (!protos) {
806 return NULL;
808 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
809 if (!strcmp(ent->name, name)) {
810 return ent;
812 } SMARTLIST_FOREACH_END(ent);
814 return NULL;
817 /** Helper: Given a list of proto_entry_t, return true iff
818 * <b>pr</b>=<b>ver</b> is included in that list. */
819 static int
820 protocol_list_contains(const smartlist_t *protos,
821 protocol_type_t pr, uint32_t ver)
823 if (BUG(protos == NULL)) {
824 return 0; // LCOV_EXCL_LINE
826 const char *pr_name = protocol_type_to_str(pr);
827 if (BUG(pr_name == NULL)) {
828 return 0; // LCOV_EXCL_LINE
830 if (ver > MAX_PROTOCOL_VERSION) {
831 return 0;
834 const proto_entry_t *ent = find_entry_by_name(protos, pr_name);
835 if (ent) {
836 return (ent->bitmask & BIT(ver)) != 0;
838 return 0;
841 /** Return a string describing the protocols supported by tor version
842 * <b>version</b>, or an empty string if we cannot tell.
844 * Note that this is only used to infer protocols for Tor versions that
845 * can't declare their own.
847 /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
848 const char *
849 protover_compute_for_old_tor(const char *version)
851 if (version == NULL) {
852 /* No known version; guess the oldest series that is still supported. */
853 version = "0.2.5.15";
856 if (tor_version_as_new_as(version,
857 FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
858 return "";
859 } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
860 /* 0.2.9.1-alpha HSRend=2 */
861 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
862 "Link=1-4 LinkAuth=1 "
863 "Microdesc=1-2 Relay=1-2";
864 } else if (tor_version_as_new_as(version, "0.2.7.5")) {
865 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
866 * ed25519 support. We'll call them present only in "stable" 027,
867 * though. */
868 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
869 "Link=1-4 LinkAuth=1 "
870 "Microdesc=1-2 Relay=1-2";
871 } else if (tor_version_as_new_as(version, "0.2.4.19")) {
872 /* No currently supported Tor server versions are older than this, or
873 * lack these protocols. */
874 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
875 "Link=1-4 LinkAuth=1 "
876 "Microdesc=1 Relay=1-2";
877 } else {
878 /* Cannot infer protocols. */
879 return "";
884 * Release all storage held by static fields in protover.c
886 void
887 protover_free_all(void)
889 if (supported_protocol_list) {
890 smartlist_t *entries = supported_protocol_list;
891 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
892 smartlist_free(entries);
893 supported_protocol_list = NULL;