Merge branch 'maint-0.4.5' into maint-0.4.6
[tor.git] / src / core / or / protover.c
blob04df8aeeb8d07e852295b8988ce9ba49224be30e
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 #ifndef HAVE_RUST
33 static const smartlist_t *get_supported_protocol_list(void);
34 static int protocol_list_contains(const smartlist_t *protos,
35 protocol_type_t pr, uint32_t ver);
36 static const proto_entry_t *find_entry_by_name(const smartlist_t *protos,
37 const char *name);
39 /** Mapping between protocol type string and protocol type. */
40 /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
41 static const struct {
42 protocol_type_t protover_type;
43 const char *name;
44 /* If you add a new protocol here, you probably also want to add
45 * parsing for it in summarize_protover_flags(), so that it has a
46 * summary flag in routerstatus_t */
47 } PROTOCOL_NAMES[] = {
48 { PRT_LINK, "Link" },
49 { PRT_LINKAUTH, "LinkAuth" },
50 { PRT_RELAY, "Relay" },
51 { PRT_DIRCACHE, "DirCache" },
52 { PRT_HSDIR, "HSDir" },
53 { PRT_HSINTRO, "HSIntro" },
54 { PRT_HSREND, "HSRend" },
55 { PRT_DESC, "Desc" },
56 { PRT_MICRODESC, "Microdesc"},
57 { PRT_PADDING, "Padding"},
58 { PRT_CONS, "Cons" },
59 { PRT_FLOWCTRL, "FlowCtrl"},
62 #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
64 /* Maximum allowed length of any single subprotocol name. */
65 // C_RUST_COUPLED: src/rust/protover/protover.rs
66 // `MAX_PROTOCOL_NAME_LENGTH`
67 static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
69 /**
70 * Given a protocol_type_t, return the corresponding string used in
71 * descriptors.
73 STATIC const char *
74 protocol_type_to_str(protocol_type_t pr)
76 unsigned i;
77 for (i=0; i < N_PROTOCOL_NAMES; ++i) {
78 if (PROTOCOL_NAMES[i].protover_type == pr)
79 return PROTOCOL_NAMES[i].name;
81 /* LCOV_EXCL_START */
82 tor_assert_nonfatal_unreached_once();
83 return "UNKNOWN";
84 /* LCOV_EXCL_STOP */
87 /**
88 * Release all space held by a single proto_entry_t structure
90 STATIC void
91 proto_entry_free_(proto_entry_t *entry)
93 if (!entry)
94 return;
95 tor_free(entry->name);
96 tor_free(entry);
99 /** The largest possible protocol version. */
100 #define MAX_PROTOCOL_VERSION (63)
103 * Given a string <b>s</b> and optional end-of-string pointer
104 * <b>end_of_range</b>, parse the protocol range and store it in
105 * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
106 * U-U, where U is an unsigned integer between 0 and 63 inclusive.
108 static int
109 parse_version_range(const char *s, const char *end_of_range,
110 uint32_t *low_out, uint32_t *high_out)
112 uint32_t low, high;
113 char *next = NULL;
114 int ok;
116 tor_assert(high_out);
117 tor_assert(low_out);
119 if (BUG(!end_of_range))
120 end_of_range = s + strlen(s); // LCOV_EXCL_LINE
122 /* A range must start with a digit. */
123 if (!TOR_ISDIGIT(*s)) {
124 goto error;
127 /* Note that this wouldn't be safe if we didn't know that eventually,
128 * we'd hit a NUL */
129 low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
130 if (!ok)
131 goto error;
132 if (next > end_of_range)
133 goto error;
134 if (next == end_of_range) {
135 high = low;
136 goto done;
139 if (*next != '-')
140 goto error;
141 s = next+1;
143 /* ibid */
144 if (!TOR_ISDIGIT(*s)) {
145 goto error;
147 high = (uint32_t) tor_parse_ulong(s, 10, 0,
148 MAX_PROTOCOL_VERSION, &ok, &next);
149 if (!ok)
150 goto error;
151 if (next != end_of_range)
152 goto error;
154 if (low > high)
155 goto error;
157 done:
158 *high_out = high;
159 *low_out = low;
160 return 0;
162 error:
163 return -1;
166 static int
167 is_valid_keyword(const char *s, size_t n)
169 for (size_t i = 0; i < n; i++) {
170 if (!TOR_ISALNUM(s[i]) && s[i] != '-')
171 return 0;
173 return 1;
176 /** The x'th bit in a bitmask. */
177 #define BIT(x) (UINT64_C(1)<<(x))
180 * Return a bitmask so that bits 'low' through 'high' inclusive are set,
181 * and all other bits are cleared.
183 static uint64_t
184 bitmask_for_range(uint32_t low, uint32_t high)
186 uint64_t mask = ~(uint64_t)0;
187 mask <<= 63 - high;
188 mask >>= 63 - high + low;
189 mask <<= low;
190 return mask;
193 /** Parse a single protocol entry from <b>s</b> up to an optional
194 * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
195 * on error.
197 * A protocol entry has a keyword, an = sign, and zero or more ranges. */
198 static proto_entry_t *
199 parse_single_entry(const char *s, const char *end_of_entry)
201 proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
202 const char *equals;
204 if (BUG (!end_of_entry))
205 end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
207 /* There must be an =. */
208 equals = memchr(s, '=', end_of_entry - s);
209 if (!equals)
210 goto error;
212 /* The name must be nonempty */
213 if (equals == s)
214 goto error;
216 /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
217 if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
218 log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
219 "protocol name. This is possibly an attack or a bug, unless "
220 "the Tor network truly supports protocol names larger than "
221 "%ud characters. The offending string was: %s",
222 MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
223 goto error;
226 /* The name must contain only alphanumeric characters and hyphens. */
227 if (!is_valid_keyword(s, equals-s))
228 goto error;
230 out->name = tor_strndup(s, equals-s);
232 tor_assert(equals < end_of_entry);
234 s = equals + 1;
235 while (s < end_of_entry) {
236 const char *comma = memchr(s, ',', end_of_entry-s);
237 if (! comma)
238 comma = end_of_entry;
240 uint32_t low=0, high=0;
241 if (parse_version_range(s, comma, &low, &high) < 0) {
242 goto error;
245 out->bitmask |= bitmask_for_range(low,high);
247 s = comma;
248 // Skip the comma separator between ranges. Don't ignore a trailing comma.
249 if (s < (end_of_entry - 1))
250 ++s;
253 return out;
255 error:
256 proto_entry_free(out);
257 return NULL;
261 * Parse the protocol list from <b>s</b> and return it as a smartlist of
262 * proto_entry_t
264 STATIC smartlist_t *
265 parse_protocol_list(const char *s)
267 smartlist_t *entries = smartlist_new();
269 while (*s) {
270 /* Find the next space or the NUL. */
271 const char *end_of_entry = strchr(s, ' ');
272 proto_entry_t *entry;
273 if (!end_of_entry)
274 end_of_entry = s + strlen(s);
276 entry = parse_single_entry(s, end_of_entry);
278 if (! entry)
279 goto error;
281 smartlist_add(entries, entry);
283 s = end_of_entry;
284 while (*s == ' ')
285 ++s;
288 return entries;
290 error:
291 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
292 smartlist_free(entries);
293 return NULL;
297 * Return true if the unparsed protover list in <b>s</b> contains a
298 * parsing error, such as extra commas, a bad number, or an over-long
299 * name.
301 bool
302 protover_list_is_invalid(const char *s)
304 smartlist_t *list = parse_protocol_list(s);
305 if (!list)
306 return true; /* yes, has a dangerous name */
307 SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
308 smartlist_free(list);
309 return false; /* no, looks fine */
313 * Given a protocol type and version number, return true iff we know
314 * how to speak that protocol.
317 protover_is_supported_here(protocol_type_t pr, uint32_t ver)
319 const smartlist_t *ours = get_supported_protocol_list();
320 return protocol_list_contains(ours, pr, ver);
324 * Return true iff "list" encodes a protocol list that includes support for
325 * the indicated protocol and version.
327 * If the protocol list is unparseable, treat it as if it defines no
328 * protocols, and return 0.
331 protocol_list_supports_protocol(const char *list, protocol_type_t tp,
332 uint32_t version)
334 /* NOTE: This is a pretty inefficient implementation. If it ever shows
335 * up in profiles, we should memoize it.
337 smartlist_t *protocols = parse_protocol_list(list);
338 if (!protocols) {
339 return 0;
341 int contains = protocol_list_contains(protocols, tp, version);
343 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
344 smartlist_free(protocols);
345 return contains;
349 * Return true iff "list" encodes a protocol list that includes support for
350 * the indicated protocol and version, or some later version.
352 * If the protocol list is unparseable, treat it as if it defines no
353 * protocols, and return 0.
356 protocol_list_supports_protocol_or_later(const char *list,
357 protocol_type_t tp,
358 uint32_t version)
360 /* NOTE: This is a pretty inefficient implementation. If it ever shows
361 * up in profiles, we should memoize it.
363 smartlist_t *protocols = parse_protocol_list(list);
364 if (!protocols) {
365 return 0;
367 const char *pr_name = protocol_type_to_str(tp);
369 int contains = 0;
370 const uint64_t mask = bitmask_for_range(version, 63);
372 SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
373 if (strcasecmp(proto->name, pr_name))
374 continue;
375 if (0 != (proto->bitmask & mask)) {
376 contains = 1;
377 goto found;
379 } SMARTLIST_FOREACH_END(proto);
381 found:
382 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
383 smartlist_free(protocols);
384 return contains;
388 * XXX START OF HAZARDOUS ZONE XXX
391 /** Return the canonical string containing the list of protocols
392 * that we support.
394 /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
395 const char *
396 protover_get_supported_protocols(void)
398 /* WARNING!
400 * Remember to edit the SUPPORTED_PROTOCOLS list in protover.rs if you
401 * are editing this list.
405 * XXX: WARNING!
407 * Be EXTREMELY CAREFUL when *removing* versions from this list. If you
408 * remove an entry while it still appears as "recommended" in the consensus,
409 * you'll cause all the instances without it to warn.
411 * If you remove an entry while it still appears as "required" in the
412 * consensus, you'll cause all the instances without it to refuse to connect
413 * to the network, and shut down.
415 * If you need to remove a version from this list, you need to make sure that
416 * it is not listed in the _current consensuses_: just removing it from the
417 * required list below is NOT ENOUGH. You need to remove it from the
418 * required list, and THEN let the authorities upgrade and vote on new
419 * consensuses without it. Only once those consensuses are out is it safe to
420 * remove from this list.
422 * One concrete example of a very dangerous race that could occur:
424 * Suppose that the client supports protocols "HsDir=1-2" and the consensus
425 * requires protocols "HsDir=1-2. If the client supported protocol list is
426 * then changed to "HSDir=2", while the consensus stills lists "HSDir=1-2",
427 * then these clients, even very recent ones, will shut down because they
428 * don't support "HSDir=1".
430 * And so, changes need to be done in strict sequence as described above.
432 * XXX: WARNING!
435 return
436 "Cons=1-2 "
437 "Desc=1-2 "
438 "DirCache=2 "
439 "FlowCtrl=1 "
440 "HSDir=1-2 "
441 "HSIntro=3-5 "
442 "HSRend=1-2 "
443 "Link=1-5 "
444 #ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
445 "LinkAuth=1,3 "
446 #else
447 "LinkAuth=3 "
448 #endif
449 "Microdesc=1-2 "
450 "Padding=2 "
451 "Relay=1-3";
455 * XXX: WARNING!
457 * The recommended and required values are hardwired, to avoid disaster. Voting
458 * on the wrong subprotocols here has the potential to take down the network.
460 * In particular, you need to be EXTREMELY CAREFUL before adding new versions
461 * to the required protocol list. Doing so will cause every relay or client
462 * that doesn't support those versions to refuse to connect to the network and
463 * shut down.
465 * Note that this applies to versions, not just protocols! If you say that
466 * Foobar=8-9 is required, and the client only has Foobar=9, it will shut down.
468 * It is okay to do this only for SUPER OLD relays that are not supported on
469 * the network anyway. For clients, we really shouldn't kick them off the
470 * network unless their presence is causing serious active harm.
472 * The following required and recommended lists MUST be changed BEFORE the
473 * supported list above is changed, so that these lists appear in the
474 * consensus BEFORE clients need them.
476 * Please, see the warning in protocol_get_supported_versions().
478 * XXX: WARNING!
481 /** Return the recommended client protocols list that directory authorities
482 * put in the consensus. */
483 const char *
484 protover_get_recommended_client_protocols(void)
486 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
487 "Link=4-5 Microdesc=2 Relay=2";
490 /** Return the recommended relay protocols list that directory authorities
491 * put in the consensus. */
492 const char *
493 protover_get_recommended_relay_protocols(void)
495 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
496 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2";
499 /** Return the required client protocols list that directory authorities
500 * put in the consensus. */
501 const char *
502 protover_get_required_client_protocols(void)
504 return "Cons=2 Desc=2 Link=4 Microdesc=2 Relay=2";
507 /** Return the required relay protocols list that directory authorities
508 * put in the consensus. */
509 const char *
510 protover_get_required_relay_protocols(void)
512 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
513 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2";
517 * XXX END OF HAZARDOUS ZONE XXX
520 /** The protocols from protover_get_supported_protocols(), as parsed into a
521 * list of proto_entry_t values. Access this via
522 * get_supported_protocol_list. */
523 static smartlist_t *supported_protocol_list = NULL;
525 /** Return a pointer to a smartlist of proto_entry_t for the protocols
526 * we support. */
527 static const smartlist_t *
528 get_supported_protocol_list(void)
530 if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
531 supported_protocol_list =
532 parse_protocol_list(protover_get_supported_protocols());
534 return supported_protocol_list;
537 /** Return the number of trailing zeros in x. Undefined if x is 0. */
538 static int
539 trailing_zeros(uint64_t x)
541 #ifdef __GNUC__
542 return __builtin_ctzll((unsigned long long)x);
543 #else
544 int i;
545 for (i = 0; i <= 64; ++i) {
546 if (x&1)
547 return i;
548 x>>=1;
550 return i;
551 #endif /* defined(__GNUC__) */
555 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
556 * as one or more newly allocated strings.
558 static void
559 proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
561 smartlist_add_asprintf(chunks, "%s=", entry->name);
563 uint64_t mask = entry->bitmask;
564 int shift = 0; // how much have we shifted by so far?
565 bool first = true;
566 while (mask) {
567 const char *comma = first ? "" : ",";
568 if (first) {
569 first = false;
571 int zeros = trailing_zeros(mask);
572 mask >>= zeros;
573 shift += zeros;
574 int ones = !mask ? 64 : trailing_zeros(~mask);
575 if (ones == 1) {
576 smartlist_add_asprintf(chunks, "%s%d", comma, shift);
577 } else {
578 smartlist_add_asprintf(chunks, "%s%d-%d", comma,
579 shift, shift + ones - 1);
581 if (ones == 64) {
582 break; // avoid undefined behavior; can't shift by 64.
584 mask >>= ones;
585 shift += ones;
589 /** Given a list of space-separated proto_entry_t items,
590 * encode it into a newly allocated space-separated string. */
591 STATIC char *
592 encode_protocol_list(const smartlist_t *sl)
594 const char *separator = "";
595 smartlist_t *chunks = smartlist_new();
596 SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
597 smartlist_add_strdup(chunks, separator);
599 proto_entry_encode_into(chunks, ent);
601 separator = " ";
602 } SMARTLIST_FOREACH_END(ent);
604 char *result = smartlist_join_strings(chunks, "", 0, NULL);
606 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
607 smartlist_free(chunks);
609 return result;
613 * Protocol voting implementation.
615 * Given a list of strings describing protocol versions, return a newly
616 * allocated string encoding all of the protocols that are listed by at
617 * least <b>threshold</b> of the inputs.
619 * The string is minimal and sorted according to the rules of
620 * contract_protocol_list above.
622 char *
623 protover_compute_vote(const smartlist_t *list_of_proto_strings,
624 int threshold)
626 // we use u8 counters below.
627 tor_assert(smartlist_len(list_of_proto_strings) < 256);
629 if (smartlist_len(list_of_proto_strings) == 0) {
630 return tor_strdup("");
633 smartlist_t *parsed = smartlist_new(); // smartlist of smartlist of entries
634 smartlist_t *proto_names = smartlist_new(); // smartlist of strings
635 smartlist_t *result = smartlist_new(); // smartlist of entries
637 // First, parse the inputs, and accumulate a list of protocol names.
638 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
639 smartlist_t *unexpanded = parse_protocol_list(vote);
640 if (! unexpanded) {
641 log_warn(LD_NET, "I failed with parsing a protocol list from "
642 "an authority. The offending string was: %s",
643 escaped(vote));
644 continue;
646 SMARTLIST_FOREACH_BEGIN(unexpanded, const proto_entry_t *, ent) {
647 if (!smartlist_contains_string(proto_names,ent->name)) {
648 smartlist_add(proto_names, ent->name);
650 } SMARTLIST_FOREACH_END(ent);
651 smartlist_add(parsed, unexpanded);
652 } SMARTLIST_FOREACH_END(vote);
654 // Sort the list of names.
655 smartlist_sort_strings(proto_names);
657 // For each named protocol, compute the consensus.
659 // This is not super-efficient, but it's not critical path.
660 SMARTLIST_FOREACH_BEGIN(proto_names, const char *, name) {
661 uint8_t counts[64];
662 memset(counts, 0, sizeof(counts));
663 // Count how many votes we got for each bit.
664 SMARTLIST_FOREACH_BEGIN(parsed, const smartlist_t *, vote) {
665 const proto_entry_t *ent = find_entry_by_name(vote, name);
666 if (! ent)
667 continue;
669 for (int i = 0; i < 64; ++i) {
670 if ((ent->bitmask & BIT(i)) != 0) {
671 ++ counts[i];
674 } SMARTLIST_FOREACH_END(vote);
676 uint64_t result_bitmask = 0;
677 for (int i = 0; i < 64; ++i) {
678 if (counts[i] >= threshold) {
679 result_bitmask |= BIT(i);
682 if (result_bitmask != 0) {
683 proto_entry_t *newent = tor_malloc_zero(sizeof(proto_entry_t));
684 newent->name = tor_strdup(name);
685 newent->bitmask = result_bitmask;
686 smartlist_add(result, newent);
688 } SMARTLIST_FOREACH_END(name);
690 char *consensus = encode_protocol_list(result);
692 SMARTLIST_FOREACH(result, proto_entry_t *, ent, proto_entry_free(ent));
693 smartlist_free(result);
694 smartlist_free(proto_names); // no need to free members; they are aliases.
695 SMARTLIST_FOREACH_BEGIN(parsed, smartlist_t *, v) {
696 SMARTLIST_FOREACH(v, proto_entry_t *, ent, proto_entry_free(ent));
697 smartlist_free(v);
698 } SMARTLIST_FOREACH_END(v);
699 smartlist_free(parsed);
701 return consensus;
704 /** Return true if every protocol version described in the string <b>s</b> is
705 * one that we support, and false otherwise. If <b>missing_out</b> is
706 * provided, set it to the list of protocols we do not support.
708 * If the protocol version string is unparseable, treat it as if it defines no
709 * protocols, and return 1.
712 protover_all_supported(const char *s, char **missing_out)
714 if (!s) {
715 return 1;
718 smartlist_t *entries = parse_protocol_list(s);
719 if (BUG(entries == NULL)) {
720 log_warn(LD_NET, "Received an unparseable protocol list %s"
721 " from the consensus", escaped(s));
722 return 1;
724 const smartlist_t *supported = get_supported_protocol_list();
725 smartlist_t *missing = smartlist_new();
727 SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
728 const proto_entry_t *mine = find_entry_by_name(supported, ent->name);
729 if (mine == NULL) {
730 if (ent->bitmask != 0) {
731 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
732 m->name = tor_strdup(ent->name);
733 m->bitmask = ent->bitmask;
734 smartlist_add(missing, m);
736 continue;
739 uint64_t missing_mask = ent->bitmask & ~mine->bitmask;
740 if (missing_mask != 0) {
741 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
742 m->name = tor_strdup(ent->name);
743 m->bitmask = missing_mask;
744 smartlist_add(missing, m);
746 } SMARTLIST_FOREACH_END(ent);
748 const int all_supported = (smartlist_len(missing) == 0);
749 if (!all_supported && missing_out) {
750 *missing_out = encode_protocol_list(missing);
753 SMARTLIST_FOREACH(missing, proto_entry_t *, ent, proto_entry_free(ent));
754 smartlist_free(missing);
756 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
757 smartlist_free(entries);
759 return all_supported;
762 /** Helper: return the member of 'protos' whose name is
763 * 'name', or NULL if there is no such member. */
764 static const proto_entry_t *
765 find_entry_by_name(const smartlist_t *protos, const char *name)
767 if (!protos) {
768 return NULL;
770 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
771 if (!strcmp(ent->name, name)) {
772 return ent;
774 } SMARTLIST_FOREACH_END(ent);
776 return NULL;
779 /** Helper: Given a list of proto_entry_t, return true iff
780 * <b>pr</b>=<b>ver</b> is included in that list. */
781 static int
782 protocol_list_contains(const smartlist_t *protos,
783 protocol_type_t pr, uint32_t ver)
785 if (BUG(protos == NULL)) {
786 return 0; // LCOV_EXCL_LINE
788 const char *pr_name = protocol_type_to_str(pr);
789 if (BUG(pr_name == NULL)) {
790 return 0; // LCOV_EXCL_LINE
792 if (ver > MAX_PROTOCOL_VERSION) {
793 return 0;
796 const proto_entry_t *ent = find_entry_by_name(protos, pr_name);
797 if (ent) {
798 return (ent->bitmask & BIT(ver)) != 0;
800 return 0;
803 /** Return a string describing the protocols supported by tor version
804 * <b>version</b>, or an empty string if we cannot tell.
806 * Note that this is only used to infer protocols for Tor versions that
807 * can't declare their own.
809 /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
810 const char *
811 protover_compute_for_old_tor(const char *version)
813 if (version == NULL) {
814 /* No known version; guess the oldest series that is still supported. */
815 version = "0.2.5.15";
818 if (tor_version_as_new_as(version,
819 FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
820 return "";
821 } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
822 /* 0.2.9.1-alpha HSRend=2 */
823 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
824 "Link=1-4 LinkAuth=1 "
825 "Microdesc=1-2 Relay=1-2";
826 } else if (tor_version_as_new_as(version, "0.2.7.5")) {
827 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
828 * ed25519 support. We'll call them present only in "stable" 027,
829 * though. */
830 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
831 "Link=1-4 LinkAuth=1 "
832 "Microdesc=1-2 Relay=1-2";
833 } else if (tor_version_as_new_as(version, "0.2.4.19")) {
834 /* No currently supported Tor server versions are older than this, or
835 * lack these protocols. */
836 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
837 "Link=1-4 LinkAuth=1 "
838 "Microdesc=1 Relay=1-2";
839 } else {
840 /* Cannot infer protocols. */
841 return "";
846 * Release all storage held by static fields in protover.c
848 void
849 protover_free_all(void)
851 if (supported_protocol_list) {
852 smartlist_t *entries = supported_protocol_list;
853 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
854 smartlist_free(entries);
855 supported_protocol_list = NULL;
859 #endif /* !defined(HAVE_RUST) */