tor  0.4.2.0-alpha-dev
protover.c
Go to the documentation of this file.
1 /* Copyright (c) 2016-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
24 #define PROTOVER_PRIVATE
25 
26 #include "core/or/or.h"
27 #include "core/or/protover.h"
28 #include "core/or/versions.h"
29 #include "lib/tls/tortls.h"
30 
31 #ifndef HAVE_RUST
32 
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 
38 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 routerstatus_parse_entry_from_string() so that
44  * it is set 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 };
59 
60 #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
61 
62 /* Maximum allowed length of any single subprotocol name. */
63 // C_RUST_COUPLED: src/rust/protover/protover.rs
64 // `MAX_PROTOCOL_NAME_LENGTH`
65 static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
66 
71 STATIC const char *
73 {
74  unsigned i;
75  for (i=0; i < N_PROTOCOL_NAMES; ++i) {
76  if (PROTOCOL_NAMES[i].protover_type == pr)
77  return PROTOCOL_NAMES[i].name;
78  }
79  /* LCOV_EXCL_START */
80  tor_assert_nonfatal_unreached_once();
81  return "UNKNOWN";
82  /* LCOV_EXCL_STOP */
83 }
84 
89 STATIC int
90 str_to_protocol_type(const char *s, protocol_type_t *pr_out)
91 {
92  if (BUG(!pr_out))
93  return -1;
94 
95  unsigned i;
96  for (i=0; i < N_PROTOCOL_NAMES; ++i) {
97  if (0 == strcmp(s, PROTOCOL_NAMES[i].name)) {
98  *pr_out = PROTOCOL_NAMES[i].protover_type;
99  return 0;
100  }
101  }
102 
103  return -1;
104 }
105 
109 STATIC void
110 proto_entry_free_(proto_entry_t *entry)
111 {
112  if (!entry)
113  return;
114  tor_free(entry->name);
115  SMARTLIST_FOREACH(entry->ranges, proto_range_t *, r, tor_free(r));
116  smartlist_free(entry->ranges);
117  tor_free(entry);
118 }
119 
121 #define MAX_PROTOCOL_VERSION (UINT32_MAX-1)
122 
129 static int
130 parse_version_range(const char *s, const char *end_of_range,
131  uint32_t *low_out, uint32_t *high_out)
132 {
133  uint32_t low, high;
134  char *next = NULL;
135  int ok;
136 
137  tor_assert(high_out);
138  tor_assert(low_out);
139 
140  if (BUG(!end_of_range))
141  end_of_range = s + strlen(s); // LCOV_EXCL_LINE
142 
143  /* A range must start with a digit. */
144  if (!TOR_ISDIGIT(*s)) {
145  goto error;
146  }
147 
148  /* Note that this wouldn't be safe if we didn't know that eventually,
149  * we'd hit a NUL */
150  low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
151  if (!ok)
152  goto error;
153  if (next > end_of_range)
154  goto error;
155  if (next == end_of_range) {
156  high = low;
157  goto done;
158  }
159 
160  if (*next != '-')
161  goto error;
162  s = next+1;
163 
164  /* ibid */
165  if (!TOR_ISDIGIT(*s)) {
166  goto error;
167  }
168  high = (uint32_t) tor_parse_ulong(s, 10, 0,
169  MAX_PROTOCOL_VERSION, &ok, &next);
170  if (!ok)
171  goto error;
172  if (next != end_of_range)
173  goto error;
174 
175  if (low > high)
176  goto error;
177 
178  done:
179  *high_out = high;
180  *low_out = low;
181  return 0;
182 
183  error:
184  return -1;
185 }
186 
187 static int
188 is_valid_keyword(const char *s, size_t n)
189 {
190  for (size_t i = 0; i < n; i++) {
191  if (!TOR_ISALNUM(s[i]) && s[i] != '-')
192  return 0;
193  }
194  return 1;
195 }
196 
202 static proto_entry_t *
203 parse_single_entry(const char *s, const char *end_of_entry)
204 {
205  proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
206  const char *equals;
207 
208  out->ranges = smartlist_new();
209 
210  if (BUG (!end_of_entry))
211  end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
212 
213  /* There must be an =. */
214  equals = memchr(s, '=', end_of_entry - s);
215  if (!equals)
216  goto error;
217 
218  /* The name must be nonempty */
219  if (equals == s)
220  goto error;
221 
222  /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
223  if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
224  log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
225  "protocol name. This is possibly an attack or a bug, unless "
226  "the Tor network truly supports protocol names larger than "
227  "%ud characters. The offending string was: %s",
228  MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
229  goto error;
230  }
231 
232  /* The name must contain only alphanumeric characters and hyphens. */
233  if (!is_valid_keyword(s, equals-s))
234  goto error;
235 
236  out->name = tor_strndup(s, equals-s);
237 
238  tor_assert(equals < end_of_entry);
239 
240  s = equals + 1;
241  while (s < end_of_entry) {
242  const char *comma = memchr(s, ',', end_of_entry-s);
243  proto_range_t *range = tor_malloc_zero(sizeof(proto_range_t));
244  if (! comma)
245  comma = end_of_entry;
246 
247  smartlist_add(out->ranges, range);
248  if (parse_version_range(s, comma, &range->low, &range->high) < 0) {
249  goto error;
250  }
251 
252  s = comma;
253  while (*s == ',' && s < end_of_entry)
254  ++s;
255  }
256 
257  return out;
258 
259  error:
260  proto_entry_free(out);
261  return NULL;
262 }
263 
268 STATIC smartlist_t *
269 parse_protocol_list(const char *s)
270 {
271  smartlist_t *entries = smartlist_new();
272 
273  while (*s) {
274  /* Find the next space or the NUL. */
275  const char *end_of_entry = strchr(s, ' ');
276  proto_entry_t *entry;
277  if (!end_of_entry)
278  end_of_entry = s + strlen(s);
279 
280  entry = parse_single_entry(s, end_of_entry);
281 
282  if (! entry)
283  goto error;
284 
285  smartlist_add(entries, entry);
286 
287  s = end_of_entry;
288  while (*s == ' ')
289  ++s;
290  }
291 
292  return entries;
293 
294  error:
295  SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
296  smartlist_free(entries);
297  return NULL;
298 }
299 
304 bool
306 {
307  smartlist_t *list = parse_protocol_list(s);
308  if (!list)
309  return true; /* yes, has a dangerous name */
310  SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
311  smartlist_free(list);
312  return false; /* no, looks fine */
313 }
314 
319 int
321 {
323  return protocol_list_contains(ours, pr, ver);
324 }
325 
330 int
332  uint32_t version)
333 {
334  /* NOTE: This is a pretty inefficient implementation. If it ever shows
335  * up in profiles, we should memoize it.
336  */
337  smartlist_t *protocols = parse_protocol_list(list);
338  if (!protocols) {
339  return 0;
340  }
341  int contains = protocol_list_contains(protocols, tp, version);
342 
343  SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
344  smartlist_free(protocols);
345  return contains;
346 }
347 
352 int
354  protocol_type_t tp,
355  uint32_t version)
356 {
357  /* NOTE: This is a pretty inefficient implementation. If it ever shows
358  * up in profiles, we should memoize it.
359  */
360  smartlist_t *protocols = parse_protocol_list(list);
361  if (!protocols) {
362  return 0;
363  }
364  const char *pr_name = protocol_type_to_str(tp);
365 
366  int contains = 0;
367  SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
368  if (strcasecmp(proto->name, pr_name))
369  continue;
370  SMARTLIST_FOREACH_BEGIN(proto->ranges, const proto_range_t *, range) {
371  if (range->high >= version) {
372  contains = 1;
373  goto found;
374  }
375  } SMARTLIST_FOREACH_END(range);
376  } SMARTLIST_FOREACH_END(proto);
377 
378  found:
379  SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
380  smartlist_free(protocols);
381  return contains;
382 }
383 
386 const char *
389 {
390  return
391  "Cons=1-2 "
392  "Desc=1-2 "
393  "DirCache=1-2 "
394  "HSDir=1-2 "
395  "HSIntro=3-4 "
396  "HSRend=1-2 "
397  "Link=1-5 "
398 #ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
399  "LinkAuth=1,3 "
400 #else
401  "LinkAuth=3 "
402 #endif
403  "Microdesc=1-2 "
404  "Relay=1-2 "
405  "Padding=1 "
406  "FlowCtrl=1";
407 }
408 
413 
416 static const smartlist_t *
418 {
419  if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
422  }
424 }
425 
430 static void
431 proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
432 {
433  smartlist_add_asprintf(chunks, "%s=", entry->name);
434 
435  SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
436  const char *comma = "";
437  if (range_sl_idx != 0)
438  comma = ",";
439 
440  if (range->low == range->high) {
441  smartlist_add_asprintf(chunks, "%s%lu",
442  comma, (unsigned long)range->low);
443  } else {
444  smartlist_add_asprintf(chunks, "%s%lu-%lu",
445  comma, (unsigned long)range->low,
446  (unsigned long)range->high);
447  }
448  } SMARTLIST_FOREACH_END(range);
449 }
450 
453 STATIC char *
455 {
456  const char *separator = "";
457  smartlist_t *chunks = smartlist_new();
458  SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
459  smartlist_add_strdup(chunks, separator);
460 
461  proto_entry_encode_into(chunks, ent);
462 
463  separator = " ";
464  } SMARTLIST_FOREACH_END(ent);
465 
466  char *result = smartlist_join_strings(chunks, "", 0, NULL);
467 
468  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
469  smartlist_free(chunks);
470 
471  return result;
472 }
473 
474 /* We treat any protocol list with more than this many subprotocols in it
475  * as a DoS attempt. */
478 static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
479 
489 static smartlist_t *
491 {
492  smartlist_t *expanded = smartlist_new();
493  if (!protos)
494  return expanded;
495 
496  SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
497  const char *name = ent->name;
498  if (strlen(name) > MAX_PROTOCOL_NAME_LENGTH) {
499  log_warn(LD_NET, "When expanding a protocol entry, I got a very large "
500  "protocol name. This is possibly an attack or a bug, unless "
501  "the Tor network truly supports protocol names larger than "
502  "%ud characters. The offending string was: %s",
503  MAX_PROTOCOL_NAME_LENGTH, escaped(name));
504  continue;
505  }
506  SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
507  uint32_t u;
508  for (u = range->low; u <= range->high; ++u) {
509  smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
510  if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
511  goto too_many;
512  }
513  } SMARTLIST_FOREACH_END(range);
514  } SMARTLIST_FOREACH_END(ent);
515 
516  smartlist_sort_strings(expanded);
517  smartlist_uniq_strings(expanded); // This makes voting work. do not remove
518  return expanded;
519 
520  too_many:
521  SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
522  smartlist_free(expanded);
523  return NULL;
524 }
525 
529 static int
530 cmp_single_ent_by_version(const void **a_, const void **b_)
531 {
532  const proto_entry_t *ent_a = *a_;
533  const proto_entry_t *ent_b = *b_;
534 
535  tor_assert(smartlist_len(ent_a->ranges) == 1);
536  tor_assert(smartlist_len(ent_b->ranges) == 1);
537 
538  const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
539  const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
540 
541  tor_assert(a->low == a->high);
542  tor_assert(b->low == b->high);
543 
544  if (a->low < b->low) {
545  return -1;
546  } else if (a->low == b->low) {
547  return 0;
548  } else {
549  return 1;
550  }
551 }
552 
559 static char *
560 contract_protocol_list(const smartlist_t *proto_strings)
561 {
562  if (smartlist_len(proto_strings) == 0) {
563  return tor_strdup("");
564  }
565 
566  // map from name to list of single-version entries
567  strmap_t *entry_lists_by_name = strmap_new();
568  // list of protocol names
569  smartlist_t *all_names = smartlist_new();
570  // list of strings for the output we're building
571  smartlist_t *chunks = smartlist_new();
572 
573  // Parse each item and stick it entry_lists_by_name. Build
574  // 'all_names' at the same time.
575  SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
576  if (BUG(!s))
577  continue;// LCOV_EXCL_LINE
578  proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
579  if (BUG(!ent))
580  continue; // LCOV_EXCL_LINE
581  smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
582  if (!lst) {
583  smartlist_add(all_names, ent->name);
584  lst = smartlist_new();
585  strmap_set(entry_lists_by_name, ent->name, lst);
586  }
587  smartlist_add(lst, ent);
588  } SMARTLIST_FOREACH_END(s);
589 
590  // We want to output the protocols sorted by their name.
591  smartlist_sort_strings(all_names);
592 
593  SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
594  const int first_entry = (name_sl_idx == 0);
595  smartlist_t *lst = strmap_get(entry_lists_by_name, name);
596  tor_assert(lst);
597  // Sort every entry with this name by version. They are
598  // singletons, so there can't be overlap.
600 
601  if (! first_entry)
602  smartlist_add_strdup(chunks, " ");
603 
604  /* We're going to construct this entry from the ranges. */
605  proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
606  entry->ranges = smartlist_new();
607  entry->name = tor_strdup(name);
608 
609  // Now, find all the ranges of versions start..end where
610  // all of start, start+1, start+2, ..end are included.
611  int start_of_cur_series = 0;
612  while (start_of_cur_series < smartlist_len(lst)) {
613  const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
614  const proto_range_t *range = smartlist_get(ent->ranges, 0);
615  const uint32_t ver_low = range->low;
616  uint32_t ver_high = ver_low;
617 
618  int idx;
619  for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
620  ent = smartlist_get(lst, idx);
621  range = smartlist_get(ent->ranges, 0);
622  if (range->low != ver_high + 1)
623  break;
624  ver_high += 1;
625  }
626 
627  // Now idx is either off the end of the list, or the first sequence
628  // break in the list.
629  start_of_cur_series = idx;
630 
631  proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
632  new_range->low = ver_low;
633  new_range->high = ver_high;
634  smartlist_add(entry->ranges, new_range);
635  }
636  proto_entry_encode_into(chunks, entry);
637  proto_entry_free(entry);
638 
639  } SMARTLIST_FOREACH_END(name);
640 
641  // Build the result...
642  char *result = smartlist_join_strings(chunks, "", 0, NULL);
643 
644  // And free all the stuff we allocated.
645  SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
646  smartlist_t *lst = strmap_get(entry_lists_by_name, name);
647  tor_assert(lst);
648  SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
649  smartlist_free(lst);
650  } SMARTLIST_FOREACH_END(name);
651 
652  strmap_free(entry_lists_by_name, NULL);
653  smartlist_free(all_names);
654  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
655  smartlist_free(chunks);
656 
657  return result;
658 }
659 
670 char *
671 protover_compute_vote(const smartlist_t *list_of_proto_strings,
672  int threshold)
673 {
674  if (smartlist_len(list_of_proto_strings) == 0) {
675  return tor_strdup("");
676  }
677 
678  smartlist_t *all_entries = smartlist_new();
679 
680  // First, parse the inputs and break them into singleton entries.
681  SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
682  smartlist_t *unexpanded = parse_protocol_list(vote);
683  if (! unexpanded) {
684  log_warn(LD_NET, "I failed with parsing a protocol list from "
685  "an authority. The offending string was: %s",
686  escaped(vote));
687  continue;
688  }
689  smartlist_t *this_vote = expand_protocol_list(unexpanded);
690  if (this_vote == NULL) {
691  log_warn(LD_NET, "When expanding a protocol list from an authority, I "
692  "got too many protocols. This is possibly an attack or a bug, "
693  "unless the Tor network truly has expanded to support over %d "
694  "different subprotocol versions. The offending string was: %s",
696  } else {
697  smartlist_add_all(all_entries, this_vote);
698  smartlist_free(this_vote);
699  }
700  SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
701  smartlist_free(unexpanded);
702  } SMARTLIST_FOREACH_END(vote);
703 
704  if (smartlist_len(all_entries) == 0) {
705  smartlist_free(all_entries);
706  return tor_strdup("");
707  }
708 
709  // Now sort the singleton entries
710  smartlist_sort_strings(all_entries);
711 
712  // Now find all the strings that appear at least 'threshold' times.
713  smartlist_t *include_entries = smartlist_new();
714  const char *cur_entry = smartlist_get(all_entries, 0);
715  int n_times = 0;
716  SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
717  if (!strcmp(ent, cur_entry)) {
718  n_times++;
719  } else {
720  if (n_times >= threshold && cur_entry)
721  smartlist_add(include_entries, (void*)cur_entry);
722  cur_entry = ent;
723  n_times = 1 ;
724  }
725  } SMARTLIST_FOREACH_END(ent);
726 
727  if (n_times >= threshold && cur_entry)
728  smartlist_add(include_entries, (void*)cur_entry);
729 
730  // Finally, compress that list.
731  char *result = contract_protocol_list(include_entries);
732  smartlist_free(include_entries);
733  SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
734  smartlist_free(all_entries);
735 
736  return result;
737 }
738 
747 int
748 protover_all_supported(const char *s, char **missing_out)
749 {
750  int all_supported = 1;
751  smartlist_t *missing_some;
752  smartlist_t *missing_completely;
753  smartlist_t *missing_all;
754 
755  if (!s) {
756  return 1;
757  }
758 
759  smartlist_t *entries = parse_protocol_list(s);
760  if (BUG(entries == NULL)) {
761  log_warn(LD_NET, "Received an unparseable protocol list %s"
762  " from the consensus", escaped(s));
763  return 1;
764  }
765 
766  missing_some = smartlist_new();
767  missing_completely = smartlist_new();
768 
769  SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
770  protocol_type_t tp;
771  if (str_to_protocol_type(ent->name, &tp) < 0) {
772  if (smartlist_len(ent->ranges)) {
773  goto unsupported;
774  }
775  continue;
776  }
777 
778  SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
779  proto_entry_t *unsupported = tor_malloc_zero(sizeof(proto_entry_t));
780  proto_range_t *versions = tor_malloc_zero(sizeof(proto_range_t));
781  uint32_t i;
782 
783  unsupported->name = tor_strdup(ent->name);
784  unsupported->ranges = smartlist_new();
785 
786  for (i = range->low; i <= range->high; ++i) {
787  if (!protover_is_supported_here(tp, i)) {
788  if (versions->low == 0 && versions->high == 0) {
789  versions->low = i;
790  /* Pre-emptively add the high now, just in case we're in a single
791  * version range (e.g. "Link=999"). */
792  versions->high = i;
793  }
794  /* If the last one to be unsupported is one less than the current
795  * one, we're in a continuous range, so set the high field. */
796  if ((versions->high && versions->high == i - 1) ||
797  /* Similarly, if the last high wasn't set and we're currently
798  * one higher than the low, add current index as the highest
799  * known high. */
800  (!versions->high && versions->low == i - 1)) {
801  versions->high = i;
802  continue;
803  }
804  } else {
805  /* If we hit a supported version, and we previously had a range,
806  * we've hit a non-continuity. Copy the previous range and add it to
807  * the unsupported->ranges list and zero-out the previous range for
808  * the next iteration. */
809  if (versions->low != 0 && versions->high != 0) {
810  proto_range_t *versions_to_add = tor_malloc(sizeof(proto_range_t));
811 
812  versions_to_add->low = versions->low;
813  versions_to_add->high = versions->high;
814  smartlist_add(unsupported->ranges, versions_to_add);
815 
816  versions->low = 0;
817  versions->high = 0;
818  }
819  }
820  }
821  /* Once we've run out of versions to check, see if we had any unsupported
822  * ones and, if so, add them to unsupported->ranges. */
823  if (versions->low != 0 && versions->high != 0) {
824  smartlist_add(unsupported->ranges, versions);
825  } else {
826  tor_free(versions);
827  }
828  /* Finally, if we had something unsupported, add it to the list of
829  * missing_some things and mark that there was something missing. */
830  if (smartlist_len(unsupported->ranges) != 0) {
831  smartlist_add(missing_some, (void*) unsupported);
832  all_supported = 0;
833  } else {
834  proto_entry_free(unsupported);
835  }
836  } SMARTLIST_FOREACH_END(range);
837 
838  continue;
839 
840  unsupported:
841  all_supported = 0;
842  smartlist_add(missing_completely, (void*) ent);
843  } SMARTLIST_FOREACH_END(ent);
844 
845  /* We keep the two smartlists separate so that we can free the proto_entry_t
846  * we created and put in missing_some, so here we add them together to build
847  * the string. */
848  missing_all = smartlist_new();
849  smartlist_add_all(missing_all, missing_some);
850  smartlist_add_all(missing_all, missing_completely);
851 
852  if (missing_out && !all_supported) {
853  tor_assert(smartlist_len(missing_all) != 0);
854  *missing_out = encode_protocol_list(missing_all);
855  }
856  SMARTLIST_FOREACH(missing_some, proto_entry_t *, ent, proto_entry_free(ent));
857  smartlist_free(missing_some);
858  smartlist_free(missing_completely);
859  smartlist_free(missing_all);
860 
861  SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
862  smartlist_free(entries);
863 
864  return all_supported;
865 }
866 
869 static int
871  protocol_type_t pr, uint32_t ver)
872 {
873  if (BUG(protos == NULL)) {
874  return 0; // LCOV_EXCL_LINE
875  }
876  const char *pr_name = protocol_type_to_str(pr);
877  if (BUG(pr_name == NULL)) {
878  return 0; // LCOV_EXCL_LINE
879  }
880 
881  SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
882  if (strcasecmp(ent->name, pr_name))
883  continue;
884  /* name matches; check the ranges */
885  SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
886  if (ver >= range->low && ver <= range->high)
887  return 1;
888  } SMARTLIST_FOREACH_END(range);
889  } SMARTLIST_FOREACH_END(ent);
890 
891  return 0;
892 }
893 
900 const char *
902 protover_compute_for_old_tor(const char *version)
903 {
904  if (version == NULL) {
905  /* No known version; guess the oldest series that is still supported. */
906  version = "0.2.5.15";
907  }
908 
909  if (tor_version_as_new_as(version,
911  return "";
912  } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
913  /* 0.2.9.1-alpha HSRend=2 */
914  return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
915  "Link=1-4 LinkAuth=1 "
916  "Microdesc=1-2 Relay=1-2";
917  } else if (tor_version_as_new_as(version, "0.2.7.5")) {
918  /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
919  * ed25519 support. We'll call them present only in "stable" 027,
920  * though. */
921  return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
922  "Link=1-4 LinkAuth=1 "
923  "Microdesc=1-2 Relay=1-2";
924  } else if (tor_version_as_new_as(version, "0.2.4.19")) {
925  /* No currently supported Tor server versions are older than this, or
926  * lack these protocols. */
927  return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
928  "Link=1-4 LinkAuth=1 "
929  "Microdesc=1 Relay=1-2";
930  } else {
931  /* Cannot infer protocols. */
932  return "";
933  }
934 }
935 
939 void
941 {
944  SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
945  smartlist_free(entries);
947  }
948 }
949 
950 #endif /* !defined(HAVE_RUST) */
#define FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS
Definition: protover.h:23
void protover_free_all(void)
Definition: protover.c:940
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
STATIC smartlist_t * parse_protocol_list(const char *s)
Definition: protover.c:269
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
static int cmp_single_ent_by_version(const void **a_, const void **b_)
Definition: protover.c:530
static int protocol_list_contains(const smartlist_t *protos, protocol_type_t pr, uint32_t ver)
Definition: protover.c:870
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: parse_int.c:75
int tor_version_as_new_as(const char *platform, const char *cutoff)
Definition: versions.c:171
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_uniq_strings(smartlist_t *sl)
Definition: smartlist.c:574
#define tor_free(p)
Definition: malloc.h:52
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
static proto_entry_t * parse_single_entry(const char *s, const char *end_of_entry)
Definition: protover.c:203
Header file for versions.c.
tor_assert(buffer)
protocol_type_t
Definition: protover.h:35
static const struct @13 PROTOCOL_NAMES[]
C_RUST_COUPLED: src/rust/protover/protover.rs PROTOCOL_NAMES
static const smartlist_t * get_supported_protocol_list(void)
Definition: protover.c:417
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
static smartlist_t * expand_protocol_list(const smartlist_t *protos)
Definition: protover.c:490
Master header file for Tor-specific functionality.
static smartlist_t * supported_protocol_list
Definition: protover.c:412
STATIC int str_to_protocol_type(const char *s, protocol_type_t *pr_out)
Definition: protover.c:90
int protocol_list_supports_protocol(const char *list, protocol_type_t tp, uint32_t version)
Definition: protover.c:331
STATIC void proto_entry_free_(proto_entry_t *entry)
Definition: protover.c:110
#define MAX_PROTOCOL_VERSION
Definition: protover.c:121
Headers and type declarations for protover.c.
bool protover_contains_long_protocol_names(const char *s)
Definition: protover.c:305
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
static const int MAX_PROTOCOLS_TO_EXPAND
Definition: protover.c:478
#define SMARTLIST_FOREACH(sl, type, var, cmd)
const char * escaped(const char *s)
Definition: escape.c:126
static char * contract_protocol_list(const smartlist_t *proto_strings)
Definition: protover.c:560
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
Headers for tortls.c.
int protover_all_supported(const char *s, char **missing_out)
Definition: protover.c:748
STATIC const char * protocol_type_to_str(protocol_type_t pr)
Definition: protover.c:72
const char * protover_compute_for_old_tor(const char *version)
C_RUST_COUPLED: src/rust/protover/protover.rs compute_for_old_tor
Definition: protover.c:902
static void proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
Definition: protover.c:431
int protocol_list_supports_protocol_or_later(const char *list, protocol_type_t tp, uint32_t version)
Definition: protover.c:353
static int parse_version_range(const char *s, const char *end_of_range, uint32_t *low_out, uint32_t *high_out)
Definition: protover.c:130
#define LD_NET
Definition: log.h:63
int protover_is_supported_here(protocol_type_t pr, uint32_t ver)
Definition: protover.c:320
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
char * protover_compute_vote(const smartlist_t *list_of_proto_strings, int threshold)
Definition: protover.c:671
const char * protover_get_supported_protocols(void)
C_RUST_COUPLED: src/rust/protover/protover.rs SUPPORTED_PROTOCOLS
Definition: protover.c:388
STATIC char * encode_protocol_list(const smartlist_t *sl)
Definition: protover.c:454