fix memory leak in protover.c
[tor.git] / src / or / protover.c
blob5145881ba96880f10f6357612e97991adea6f738
1 /* Copyright (c) 2016-2017, 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 "or.h"
27 #include "protover.h"
28 #include "routerparse.h"
30 #ifndef HAVE_RUST
32 static const smartlist_t *get_supported_protocol_list(void);
33 static int protocol_list_contains(const smartlist_t *protos,
34 protocol_type_t pr, uint32_t ver);
36 /** Mapping between protocol type string and protocol type. */
37 /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
38 static const struct {
39 protocol_type_t protover_type;
40 const char *name;
41 } PROTOCOL_NAMES[] = {
42 { PRT_LINK, "Link" },
43 { PRT_LINKAUTH, "LinkAuth" },
44 { PRT_RELAY, "Relay" },
45 { PRT_DIRCACHE, "DirCache" },
46 { PRT_HSDIR, "HSDir" },
47 { PRT_HSINTRO, "HSIntro" },
48 { PRT_HSREND, "HSRend" },
49 { PRT_DESC, "Desc" },
50 { PRT_MICRODESC, "Microdesc"},
51 { PRT_CONS, "Cons" }
54 #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
56 /* Maximum allowed length of any single subprotocol name. */
57 // C_RUST_COUPLED: src/rust/protover/protover.rs
58 // `MAX_PROTOCOL_NAME_LENGTH`
59 static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
61 /**
62 * Given a protocol_type_t, return the corresponding string used in
63 * descriptors.
65 STATIC const char *
66 protocol_type_to_str(protocol_type_t pr)
68 unsigned i;
69 for (i=0; i < N_PROTOCOL_NAMES; ++i) {
70 if (PROTOCOL_NAMES[i].protover_type == pr)
71 return PROTOCOL_NAMES[i].name;
73 /* LCOV_EXCL_START */
74 tor_assert_nonfatal_unreached_once();
75 return "UNKNOWN";
76 /* LCOV_EXCL_STOP */
79 /**
80 * Given a string, find the corresponding protocol type and store it in
81 * <b>pr_out</b>. Return 0 on success, -1 on failure.
83 STATIC int
84 str_to_protocol_type(const char *s, protocol_type_t *pr_out)
86 if (BUG(!pr_out))
87 return -1;
89 unsigned i;
90 for (i=0; i < N_PROTOCOL_NAMES; ++i) {
91 if (0 == strcmp(s, PROTOCOL_NAMES[i].name)) {
92 *pr_out = PROTOCOL_NAMES[i].protover_type;
93 return 0;
97 return -1;
101 * Release all space held by a single proto_entry_t structure
103 STATIC void
104 proto_entry_free_(proto_entry_t *entry)
106 if (!entry)
107 return;
108 tor_free(entry->name);
109 SMARTLIST_FOREACH(entry->ranges, proto_range_t *, r, tor_free(r));
110 smartlist_free(entry->ranges);
111 tor_free(entry);
114 /** The largest possible protocol version. */
115 #define MAX_PROTOCOL_VERSION (UINT32_MAX-1)
118 * Given a string <b>s</b> and optional end-of-string pointer
119 * <b>end_of_range</b>, parse the protocol range and store it in
120 * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
121 * U-U, where U is an unsigned 32-bit integer.
123 static int
124 parse_version_range(const char *s, const char *end_of_range,
125 uint32_t *low_out, uint32_t *high_out)
127 uint32_t low, high;
128 char *next = NULL;
129 int ok;
131 tor_assert(high_out);
132 tor_assert(low_out);
134 if (BUG(!end_of_range))
135 end_of_range = s + strlen(s); // LCOV_EXCL_LINE
137 /* A range must start with a digit. */
138 if (!TOR_ISDIGIT(*s)) {
139 goto error;
142 /* Note that this wouldn't be safe if we didn't know that eventually,
143 * we'd hit a NUL */
144 low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
145 if (!ok)
146 goto error;
147 if (next > end_of_range)
148 goto error;
149 if (next == end_of_range) {
150 high = low;
151 goto done;
154 if (*next != '-')
155 goto error;
156 s = next+1;
158 /* ibid */
159 if (!TOR_ISDIGIT(*s)) {
160 goto error;
162 high = (uint32_t) tor_parse_ulong(s, 10, 0,
163 MAX_PROTOCOL_VERSION, &ok, &next);
164 if (!ok)
165 goto error;
166 if (next != end_of_range)
167 goto error;
169 if (low > high)
170 goto error;
172 done:
173 *high_out = high;
174 *low_out = low;
175 return 0;
177 error:
178 return -1;
181 /** Parse a single protocol entry from <b>s</b> up to an optional
182 * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
183 * on error.
185 * A protocol entry has a keyword, an = sign, and zero or more ranges. */
186 static proto_entry_t *
187 parse_single_entry(const char *s, const char *end_of_entry)
189 proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
190 const char *equals;
192 out->ranges = smartlist_new();
194 if (BUG (!end_of_entry))
195 end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
197 /* There must be an =. */
198 equals = memchr(s, '=', end_of_entry - s);
199 if (!equals)
200 goto error;
202 /* The name must be nonempty */
203 if (equals == s)
204 goto error;
206 /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
207 if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
208 log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
209 "protocol name. This is possibly an attack or a bug, unless "
210 "the Tor network truly supports protocol names larger than "
211 "%ud characters. The offending string was: %s",
212 MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
213 goto error;
215 out->name = tor_strndup(s, equals-s);
217 tor_assert(equals < end_of_entry);
219 s = equals + 1;
220 while (s < end_of_entry) {
221 const char *comma = memchr(s, ',', end_of_entry-s);
222 proto_range_t *range = tor_malloc_zero(sizeof(proto_range_t));
223 if (! comma)
224 comma = end_of_entry;
226 smartlist_add(out->ranges, range);
227 if (parse_version_range(s, comma, &range->low, &range->high) < 0) {
228 goto error;
231 s = comma;
232 while (*s == ',' && s < end_of_entry)
233 ++s;
236 return out;
238 error:
239 proto_entry_free(out);
240 return NULL;
244 * Parse the protocol list from <b>s</b> and return it as a smartlist of
245 * proto_entry_t
247 STATIC smartlist_t *
248 parse_protocol_list(const char *s)
250 smartlist_t *entries = smartlist_new();
252 while (*s) {
253 /* Find the next space or the NUL. */
254 const char *end_of_entry = strchr(s, ' ');
255 proto_entry_t *entry;
256 if (!end_of_entry)
257 end_of_entry = s + strlen(s);
259 entry = parse_single_entry(s, end_of_entry);
261 if (! entry)
262 goto error;
264 smartlist_add(entries, entry);
266 s = end_of_entry;
267 while (*s == ' ')
268 ++s;
271 return entries;
273 error:
274 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
275 smartlist_free(entries);
276 return NULL;
280 * Return true if the unparsed protover in <b>s</b> would contain a protocol
281 * name longer than MAX_PROTOCOL_NAME_LENGTH, and false otherwise.
283 bool
284 protover_contains_long_protocol_names(const char *s)
286 smartlist_t *list = parse_protocol_list(s);
287 if (!list)
288 return true; /* yes, has a dangerous name */
289 SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
290 smartlist_free(list);
291 return false; /* no, looks fine */
295 * Given a protocol type and version number, return true iff we know
296 * how to speak that protocol.
299 protover_is_supported_here(protocol_type_t pr, uint32_t ver)
301 const smartlist_t *ours = get_supported_protocol_list();
302 return protocol_list_contains(ours, pr, ver);
306 * Return true iff "list" encodes a protocol list that includes support for
307 * the indicated protocol and version.
310 protocol_list_supports_protocol(const char *list, protocol_type_t tp,
311 uint32_t version)
313 /* NOTE: This is a pretty inefficient implementation. If it ever shows
314 * up in profiles, we should memoize it.
316 smartlist_t *protocols = parse_protocol_list(list);
317 if (!protocols) {
318 return 0;
320 int contains = protocol_list_contains(protocols, tp, version);
322 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
323 smartlist_free(protocols);
324 return contains;
328 * Return true iff "list" encodes a protocol list that includes support for
329 * the indicated protocol and version, or some later version.
332 protocol_list_supports_protocol_or_later(const char *list,
333 protocol_type_t tp,
334 uint32_t version)
336 /* NOTE: This is a pretty inefficient implementation. If it ever shows
337 * up in profiles, we should memoize it.
339 smartlist_t *protocols = parse_protocol_list(list);
340 if (!protocols) {
341 return 0;
343 const char *pr_name = protocol_type_to_str(tp);
345 int contains = 0;
346 SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
347 if (strcasecmp(proto->name, pr_name))
348 continue;
349 SMARTLIST_FOREACH_BEGIN(proto->ranges, const proto_range_t *, range) {
350 if (range->high >= version) {
351 contains = 1;
352 goto found;
354 } SMARTLIST_FOREACH_END(range);
355 } SMARTLIST_FOREACH_END(proto);
357 found:
358 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
359 smartlist_free(protocols);
360 return contains;
363 /** Return the canonical string containing the list of protocols
364 * that we support. */
365 /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
366 const char *
367 protover_get_supported_protocols(void)
369 return
370 "Cons=1-2 "
371 "Desc=1-2 "
372 "DirCache=1-2 "
373 "HSDir=1-2 "
374 "HSIntro=3-4 "
375 "HSRend=1-2 "
376 "Link=1-5 "
377 "LinkAuth=1,3 "
378 "Microdesc=1-2 "
379 "Relay=1-2";
382 /** The protocols from protover_get_supported_protocols(), as parsed into a
383 * list of proto_entry_t values. Access this via
384 * get_supported_protocol_list. */
385 static smartlist_t *supported_protocol_list = NULL;
387 /** Return a pointer to a smartlist of proto_entry_t for the protocols
388 * we support. */
389 static const smartlist_t *
390 get_supported_protocol_list(void)
392 if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
393 supported_protocol_list =
394 parse_protocol_list(protover_get_supported_protocols());
396 return supported_protocol_list;
400 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
401 * as one or more newly allocated strings.
403 static void
404 proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
406 smartlist_add_asprintf(chunks, "%s=", entry->name);
408 SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
409 const char *comma = "";
410 if (range_sl_idx != 0)
411 comma = ",";
413 if (range->low == range->high) {
414 smartlist_add_asprintf(chunks, "%s%lu",
415 comma, (unsigned long)range->low);
416 } else {
417 smartlist_add_asprintf(chunks, "%s%lu-%lu",
418 comma, (unsigned long)range->low,
419 (unsigned long)range->high);
421 } SMARTLIST_FOREACH_END(range);
424 /** Given a list of space-separated proto_entry_t items,
425 * encode it into a newly allocated space-separated string. */
426 STATIC char *
427 encode_protocol_list(const smartlist_t *sl)
429 const char *separator = "";
430 smartlist_t *chunks = smartlist_new();
431 SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
432 smartlist_add_strdup(chunks, separator);
434 proto_entry_encode_into(chunks, ent);
436 separator = " ";
437 } SMARTLIST_FOREACH_END(ent);
439 char *result = smartlist_join_strings(chunks, "", 0, NULL);
441 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
442 smartlist_free(chunks);
444 return result;
447 /* We treat any protocol list with more than this many subprotocols in it
448 * as a DoS attempt. */
449 /// C_RUST_COUPLED: src/rust/protover/protover.rs
450 /// `MAX_PROTOCOLS_TO_EXPAND`
451 static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
453 /** Voting helper: Given a list of proto_entry_t, return a newly allocated
454 * smartlist of newly allocated strings, one for each included protocol
455 * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
456 * 'Foo=7'.)
458 * Do not list any protocol version more than once.
460 * Return NULL if the list would be too big.
462 static smartlist_t *
463 expand_protocol_list(const smartlist_t *protos)
465 smartlist_t *expanded = smartlist_new();
466 if (!protos)
467 return expanded;
469 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
470 const char *name = ent->name;
471 if (strlen(name) > MAX_PROTOCOL_NAME_LENGTH) {
472 log_warn(LD_NET, "When expanding a protocol entry, I got a very large "
473 "protocol name. This is possibly an attack or a bug, unless "
474 "the Tor network truly supports protocol names larger than "
475 "%ud characters. The offending string was: %s",
476 MAX_PROTOCOL_NAME_LENGTH, escaped(name));
477 continue;
479 SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
480 uint32_t u;
481 for (u = range->low; u <= range->high; ++u) {
482 smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
483 if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
484 goto too_many;
486 } SMARTLIST_FOREACH_END(range);
487 } SMARTLIST_FOREACH_END(ent);
489 smartlist_sort_strings(expanded);
490 smartlist_uniq_strings(expanded); // This makes voting work. do not remove
491 return expanded;
493 too_many:
494 SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
495 smartlist_free(expanded);
496 return NULL;
499 /** Voting helper: compare two singleton proto_entry_t items by version
500 * alone. (A singleton item is one with a single range entry where
501 * low==high.) */
502 static int
503 cmp_single_ent_by_version(const void **a_, const void **b_)
505 const proto_entry_t *ent_a = *a_;
506 const proto_entry_t *ent_b = *b_;
508 tor_assert(smartlist_len(ent_a->ranges) == 1);
509 tor_assert(smartlist_len(ent_b->ranges) == 1);
511 const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
512 const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
514 tor_assert(a->low == a->high);
515 tor_assert(b->low == b->high);
517 if (a->low < b->low) {
518 return -1;
519 } else if (a->low == b->low) {
520 return 0;
521 } else {
522 return 1;
526 /** Voting helper: Given a list of singleton protocol strings (of the form
527 * Foo=7), return a canonical listing of all the protocol versions listed,
528 * with as few ranges as possible, with protocol versions sorted lexically and
529 * versions sorted in numerically increasing order, using as few range entries
530 * as possible.
532 static char *
533 contract_protocol_list(const smartlist_t *proto_strings)
535 if (smartlist_len(proto_strings) == 0) {
536 return tor_strdup("");
539 // map from name to list of single-version entries
540 strmap_t *entry_lists_by_name = strmap_new();
541 // list of protocol names
542 smartlist_t *all_names = smartlist_new();
543 // list of strings for the output we're building
544 smartlist_t *chunks = smartlist_new();
546 // Parse each item and stick it entry_lists_by_name. Build
547 // 'all_names' at the same time.
548 SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
549 if (BUG(!s))
550 continue;// LCOV_EXCL_LINE
551 proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
552 if (BUG(!ent))
553 continue; // LCOV_EXCL_LINE
554 smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
555 if (!lst) {
556 smartlist_add(all_names, ent->name);
557 lst = smartlist_new();
558 strmap_set(entry_lists_by_name, ent->name, lst);
560 smartlist_add(lst, ent);
561 } SMARTLIST_FOREACH_END(s);
563 // We want to output the protocols sorted by their name.
564 smartlist_sort_strings(all_names);
566 SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
567 const int first_entry = (name_sl_idx == 0);
568 smartlist_t *lst = strmap_get(entry_lists_by_name, name);
569 tor_assert(lst);
570 // Sort every entry with this name by version. They are
571 // singletons, so there can't be overlap.
572 smartlist_sort(lst, cmp_single_ent_by_version);
574 if (! first_entry)
575 smartlist_add_strdup(chunks, " ");
577 /* We're going to construct this entry from the ranges. */
578 proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
579 entry->ranges = smartlist_new();
580 entry->name = tor_strdup(name);
582 // Now, find all the ranges of versions start..end where
583 // all of start, start+1, start+2, ..end are included.
584 int start_of_cur_series = 0;
585 while (start_of_cur_series < smartlist_len(lst)) {
586 const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
587 const proto_range_t *range = smartlist_get(ent->ranges, 0);
588 const uint32_t ver_low = range->low;
589 uint32_t ver_high = ver_low;
591 int idx;
592 for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
593 ent = smartlist_get(lst, idx);
594 range = smartlist_get(ent->ranges, 0);
595 if (range->low != ver_high + 1)
596 break;
597 ver_high += 1;
600 // Now idx is either off the end of the list, or the first sequence
601 // break in the list.
602 start_of_cur_series = idx;
604 proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
605 new_range->low = ver_low;
606 new_range->high = ver_high;
607 smartlist_add(entry->ranges, new_range);
609 proto_entry_encode_into(chunks, entry);
610 proto_entry_free(entry);
612 } SMARTLIST_FOREACH_END(name);
614 // Build the result...
615 char *result = smartlist_join_strings(chunks, "", 0, NULL);
617 // And free all the stuff we allocated.
618 SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
619 smartlist_t *lst = strmap_get(entry_lists_by_name, name);
620 tor_assert(lst);
621 SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
622 smartlist_free(lst);
623 } SMARTLIST_FOREACH_END(name);
625 strmap_free(entry_lists_by_name, NULL);
626 smartlist_free(all_names);
627 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
628 smartlist_free(chunks);
630 return result;
634 * Protocol voting implementation.
636 * Given a list of strings describing protocol versions, return a newly
637 * allocated string encoding all of the protocols that are listed by at
638 * least <b>threshold</b> of the inputs.
640 * The string is minimal and sorted according to the rules of
641 * contract_protocol_list above.
643 char *
644 protover_compute_vote(const smartlist_t *list_of_proto_strings,
645 int threshold)
647 if (smartlist_len(list_of_proto_strings) == 0) {
648 return tor_strdup("");
651 smartlist_t *all_entries = smartlist_new();
653 // First, parse the inputs and break them into singleton entries.
654 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
655 smartlist_t *unexpanded = parse_protocol_list(vote);
656 if (! unexpanded) {
657 log_warn(LD_NET, "I failed with parsing a protocol list from "
658 "an authority. The offending string was: %s",
659 escaped(vote));
660 continue;
662 smartlist_t *this_vote = expand_protocol_list(unexpanded);
663 if (this_vote == NULL) {
664 log_warn(LD_NET, "When expanding a protocol list from an authority, I "
665 "got too many protocols. This is possibly an attack or a bug, "
666 "unless the Tor network truly has expanded to support over %d "
667 "different subprotocol versions. The offending string was: %s",
668 MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
669 } else {
670 smartlist_add_all(all_entries, this_vote);
671 smartlist_free(this_vote);
673 SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
674 smartlist_free(unexpanded);
675 } SMARTLIST_FOREACH_END(vote);
677 if (smartlist_len(all_entries) == 0) {
678 smartlist_free(all_entries);
679 return tor_strdup("");
682 // Now sort the singleton entries
683 smartlist_sort_strings(all_entries);
685 // Now find all the strings that appear at least 'threshold' times.
686 smartlist_t *include_entries = smartlist_new();
687 const char *cur_entry = smartlist_get(all_entries, 0);
688 int n_times = 0;
689 SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
690 if (!strcmp(ent, cur_entry)) {
691 n_times++;
692 } else {
693 if (n_times >= threshold && cur_entry)
694 smartlist_add(include_entries, (void*)cur_entry);
695 cur_entry = ent;
696 n_times = 1 ;
698 } SMARTLIST_FOREACH_END(ent);
700 if (n_times >= threshold && cur_entry)
701 smartlist_add(include_entries, (void*)cur_entry);
703 // Finally, compress that list.
704 char *result = contract_protocol_list(include_entries);
705 smartlist_free(include_entries);
706 SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
707 smartlist_free(all_entries);
709 return result;
712 /** Return true if every protocol version described in the string <b>s</b> is
713 * one that we support, and false otherwise. If <b>missing_out</b> is
714 * provided, set it to the list of protocols we do not support.
716 * NOTE: This is quadratic, but we don't do it much: only a few times per
717 * consensus. Checking signatures should be way more expensive than this
718 * ever would be.
721 protover_all_supported(const char *s, char **missing_out)
723 int all_supported = 1;
724 smartlist_t *missing_some;
725 smartlist_t *missing_completely;
726 smartlist_t *missing_all;
728 if (!s) {
729 return 1;
732 smartlist_t *entries = parse_protocol_list(s);
733 if (BUG(entries == NULL)) {
734 log_warn(LD_NET, "Received an unparseable protocol list %s"
735 " from the consensus", escaped(s));
736 return 1;
739 missing_some = smartlist_new();
740 missing_completely = smartlist_new();
742 SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
743 protocol_type_t tp;
744 if (str_to_protocol_type(ent->name, &tp) < 0) {
745 if (smartlist_len(ent->ranges)) {
746 goto unsupported;
748 continue;
751 SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
752 proto_entry_t *unsupported = tor_malloc_zero(sizeof(proto_entry_t));
753 proto_range_t *versions = tor_malloc_zero(sizeof(proto_range_t));
754 uint32_t i;
756 unsupported->name = tor_strdup(ent->name);
757 unsupported->ranges = smartlist_new();
759 for (i = range->low; i <= range->high; ++i) {
760 if (!protover_is_supported_here(tp, i)) {
761 if (versions->low == 0 && versions->high == 0) {
762 versions->low = i;
763 /* Pre-emptively add the high now, just in case we're in a single
764 * version range (e.g. "Link=999"). */
765 versions->high = i;
767 /* If the last one to be unsupported is one less than the current
768 * one, we're in a continous range, so set the high field. */
769 if ((versions->high && versions->high == i - 1) ||
770 /* Similarly, if the last high wasn't set and we're currently
771 * one higher than the low, add current index as the highest
772 * known high. */
773 (!versions->high && versions->low == i - 1)) {
774 versions->high = i;
775 continue;
777 } else {
778 /* If we hit a supported version, and we previously had a range,
779 * we've hit a non-continuity. Copy the previous range and add it to
780 * the unsupported->ranges list and zero-out the previous range for
781 * the next iteration. */
782 if (versions->low != 0 && versions->high != 0) {
783 proto_range_t *versions_to_add = tor_malloc(sizeof(proto_range_t));
785 versions_to_add->low = versions->low;
786 versions_to_add->high = versions->high;
787 smartlist_add(unsupported->ranges, versions_to_add);
789 versions->low = 0;
790 versions->high = 0;
794 /* Once we've run out of versions to check, see if we had any unsupported
795 * ones and, if so, add them to unsupported->ranges. */
796 if (versions->low != 0 && versions->high != 0) {
797 smartlist_add(unsupported->ranges, versions);
799 /* Finally, if we had something unsupported, add it to the list of
800 * missing_some things and mark that there was something missing. */
801 if (smartlist_len(unsupported->ranges) != 0) {
802 smartlist_add(missing_some, (void*) unsupported);
803 all_supported = 0;
804 } else {
805 proto_entry_free(unsupported);
806 tor_free(versions);
808 } SMARTLIST_FOREACH_END(range);
810 continue;
812 unsupported:
813 all_supported = 0;
814 smartlist_add(missing_completely, (void*) ent);
815 } SMARTLIST_FOREACH_END(ent);
817 /* We keep the two smartlists separate so that we can free the proto_entry_t
818 * we created and put in missing_some, so here we add them together to build
819 * the string. */
820 missing_all = smartlist_new();
821 smartlist_add_all(missing_all, missing_some);
822 smartlist_add_all(missing_all, missing_completely);
824 if (missing_out && !all_supported) {
825 tor_assert(smartlist_len(missing_all) != 0);
826 *missing_out = encode_protocol_list(missing_all);
828 SMARTLIST_FOREACH(missing_some, proto_entry_t *, ent, proto_entry_free(ent));
829 smartlist_free(missing_some);
830 smartlist_free(missing_completely);
831 smartlist_free(missing_all);
833 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
834 smartlist_free(entries);
836 return all_supported;
839 /** Helper: Given a list of proto_entry_t, return true iff
840 * <b>pr</b>=<b>ver</b> is included in that list. */
841 static int
842 protocol_list_contains(const smartlist_t *protos,
843 protocol_type_t pr, uint32_t ver)
845 if (BUG(protos == NULL)) {
846 return 0; // LCOV_EXCL_LINE
848 const char *pr_name = protocol_type_to_str(pr);
849 if (BUG(pr_name == NULL)) {
850 return 0; // LCOV_EXCL_LINE
853 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
854 if (strcasecmp(ent->name, pr_name))
855 continue;
856 /* name matches; check the ranges */
857 SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
858 if (ver >= range->low && ver <= range->high)
859 return 1;
860 } SMARTLIST_FOREACH_END(range);
861 } SMARTLIST_FOREACH_END(ent);
863 return 0;
866 /** Return a string describing the protocols supported by tor version
867 * <b>version</b>, or an empty string if we cannot tell.
869 * Note that this is only used to infer protocols for Tor versions that
870 * can't declare their own.
872 /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
873 const char *
874 protover_compute_for_old_tor(const char *version)
876 if (version == NULL) {
877 /* No known version; guess the oldest series that is still supported. */
878 version = "0.2.5.15";
881 if (tor_version_as_new_as(version,
882 FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
883 return "";
884 } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
885 /* 0.2.9.1-alpha HSRend=2 */
886 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
887 "Link=1-4 LinkAuth=1 "
888 "Microdesc=1-2 Relay=1-2";
889 } else if (tor_version_as_new_as(version, "0.2.7.5")) {
890 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
891 * ed25519 support. We'll call them present only in "stable" 027,
892 * though. */
893 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
894 "Link=1-4 LinkAuth=1 "
895 "Microdesc=1-2 Relay=1-2";
896 } else if (tor_version_as_new_as(version, "0.2.4.19")) {
897 /* No currently supported Tor server versions are older than this, or
898 * lack these protocols. */
899 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
900 "Link=1-4 LinkAuth=1 "
901 "Microdesc=1 Relay=1-2";
902 } else {
903 /* Cannot infer protocols. */
904 return "";
909 * Release all storage held by static fields in protover.c
911 void
912 protover_free_all(void)
914 if (supported_protocol_list) {
915 smartlist_t *entries = supported_protocol_list;
916 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
917 smartlist_free(entries);
918 supported_protocol_list = NULL;
922 #endif /* !defined(HAVE_RUST) */