protover.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file protover.c
  5. * \brief Versioning information for different pieces of the Tor protocol.
  6. *
  7. * Starting in version 0.2.9.3-alpha, Tor places separate version numbers on
  8. * each of the different components of its protocol. Relays use these numbers
  9. * to advertise what versions of the protocols they can support, and clients
  10. * use them to find what they can ask a given relay to do. Authorities vote
  11. * on the supported protocol versions for each relay, and also vote on the
  12. * which protocols you should have to support in order to be on the Tor
  13. * network. All Tor instances use these required/recommended protocol versions
  14. * to tell what level of support for recent protocols each relay has, and
  15. * to decide whether they should be running given their current protocols.
  16. *
  17. * The main advantage of these protocol versions numbers over using Tor
  18. * version numbers is that they allow different implementations of the Tor
  19. * protocols to develop independently, without having to claim compatibility
  20. * with specific versions of Tor.
  21. **/
  22. #define PROTOVER_PRIVATE
  23. #include "or.h"
  24. #include "protover.h"
  25. #include "routerparse.h"
  26. #ifndef HAVE_RUST
  27. static const smartlist_t *get_supported_protocol_list(void);
  28. static int protocol_list_contains(const smartlist_t *protos,
  29. protocol_type_t pr, uint32_t ver);
  30. /** Mapping between protocol type string and protocol type. */
  31. static const struct {
  32. protocol_type_t protover_type;
  33. const char *name;
  34. } PROTOCOL_NAMES[] = {
  35. { PRT_LINK, "Link" },
  36. { PRT_LINKAUTH, "LinkAuth" },
  37. { PRT_RELAY, "Relay" },
  38. { PRT_DIRCACHE, "DirCache" },
  39. { PRT_HSDIR, "HSDir" },
  40. { PRT_HSINTRO, "HSIntro" },
  41. { PRT_HSREND, "HSRend" },
  42. { PRT_DESC, "Desc" },
  43. { PRT_MICRODESC, "Microdesc"},
  44. { PRT_CONS, "Cons" }
  45. };
  46. #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
  47. /**
  48. * Given a protocol_type_t, return the corresponding string used in
  49. * descriptors.
  50. */
  51. STATIC const char *
  52. protocol_type_to_str(protocol_type_t pr)
  53. {
  54. unsigned i;
  55. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  56. if (PROTOCOL_NAMES[i].protover_type == pr)
  57. return PROTOCOL_NAMES[i].name;
  58. }
  59. /* LCOV_EXCL_START */
  60. tor_assert_nonfatal_unreached_once();
  61. return "UNKNOWN";
  62. /* LCOV_EXCL_STOP */
  63. }
  64. /**
  65. * Given a string, find the corresponding protocol type and store it in
  66. * <b>pr_out</b>. Return 0 on success, -1 on failure.
  67. */
  68. STATIC int
  69. str_to_protocol_type(const char *s, protocol_type_t *pr_out)
  70. {
  71. if (BUG(!pr_out))
  72. return -1;
  73. unsigned i;
  74. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  75. if (0 == strcmp(s, PROTOCOL_NAMES[i].name)) {
  76. *pr_out = PROTOCOL_NAMES[i].protover_type;
  77. return 0;
  78. }
  79. }
  80. return -1;
  81. }
  82. /**
  83. * Release all space held by a single proto_entry_t structure
  84. */
  85. STATIC void
  86. proto_entry_free(proto_entry_t *entry)
  87. {
  88. if (!entry)
  89. return;
  90. tor_free(entry->name);
  91. SMARTLIST_FOREACH(entry->ranges, proto_range_t *, r, tor_free(r));
  92. smartlist_free(entry->ranges);
  93. tor_free(entry);
  94. }
  95. /**
  96. * Given a string <b>s</b> and optional end-of-string pointer
  97. * <b>end_of_range</b>, parse the protocol range and store it in
  98. * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
  99. * U-U, where U is an unsigned 32-bit integer.
  100. */
  101. static int
  102. parse_version_range(const char *s, const char *end_of_range,
  103. uint32_t *low_out, uint32_t *high_out)
  104. {
  105. uint32_t low, high;
  106. char *next = NULL;
  107. int ok;
  108. tor_assert(high_out);
  109. tor_assert(low_out);
  110. if (BUG(!end_of_range))
  111. end_of_range = s + strlen(s); // LCOV_EXCL_LINE
  112. /* Note that this wouldn't be safe if we didn't know that eventually,
  113. * we'd hit a NUL */
  114. low = (uint32_t) tor_parse_ulong(s, 10, 0, UINT32_MAX, &ok, &next);
  115. if (!ok)
  116. goto error;
  117. if (next > end_of_range)
  118. goto error;
  119. if (next == end_of_range) {
  120. high = low;
  121. goto done;
  122. }
  123. if (*next != '-')
  124. goto error;
  125. s = next+1;
  126. /* ibid */
  127. high = (uint32_t) tor_parse_ulong(s, 10, 0, UINT32_MAX, &ok, &next);
  128. if (!ok)
  129. goto error;
  130. if (next != end_of_range)
  131. goto error;
  132. done:
  133. *high_out = high;
  134. *low_out = low;
  135. return 0;
  136. error:
  137. return -1;
  138. }
  139. /** Parse a single protocol entry from <b>s</b> up to an optional
  140. * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
  141. * on error.
  142. *
  143. * A protocol entry has a keyword, an = sign, and zero or more ranges. */
  144. static proto_entry_t *
  145. parse_single_entry(const char *s, const char *end_of_entry)
  146. {
  147. proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
  148. const char *equals;
  149. out->ranges = smartlist_new();
  150. if (BUG (!end_of_entry))
  151. end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
  152. /* There must be an =. */
  153. equals = memchr(s, '=', end_of_entry - s);
  154. if (!equals)
  155. goto error;
  156. /* The name must be nonempty */
  157. if (equals == s)
  158. goto error;
  159. out->name = tor_strndup(s, equals-s);
  160. tor_assert(equals < end_of_entry);
  161. s = equals + 1;
  162. while (s < end_of_entry) {
  163. const char *comma = memchr(s, ',', end_of_entry-s);
  164. proto_range_t *range = tor_malloc_zero(sizeof(proto_range_t));
  165. if (! comma)
  166. comma = end_of_entry;
  167. smartlist_add(out->ranges, range);
  168. if (parse_version_range(s, comma, &range->low, &range->high) < 0) {
  169. goto error;
  170. }
  171. if (range->low > range->high) {
  172. goto error;
  173. }
  174. s = comma;
  175. while (*s == ',' && s < end_of_entry)
  176. ++s;
  177. }
  178. return out;
  179. error:
  180. proto_entry_free(out);
  181. return NULL;
  182. }
  183. /**
  184. * Parse the protocol list from <b>s</b> and return it as a smartlist of
  185. * proto_entry_t
  186. */
  187. STATIC smartlist_t *
  188. parse_protocol_list(const char *s)
  189. {
  190. smartlist_t *entries = smartlist_new();
  191. while (*s) {
  192. /* Find the next space or the NUL. */
  193. const char *end_of_entry = strchr(s, ' ');
  194. proto_entry_t *entry;
  195. if (!end_of_entry)
  196. end_of_entry = s + strlen(s);
  197. entry = parse_single_entry(s, end_of_entry);
  198. if (! entry)
  199. goto error;
  200. smartlist_add(entries, entry);
  201. s = end_of_entry;
  202. while (*s == ' ')
  203. ++s;
  204. }
  205. return entries;
  206. error:
  207. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  208. smartlist_free(entries);
  209. return NULL;
  210. }
  211. /**
  212. * Given a protocol type and version number, return true iff we know
  213. * how to speak that protocol.
  214. */
  215. int
  216. protover_is_supported_here(protocol_type_t pr, uint32_t ver)
  217. {
  218. const smartlist_t *ours = get_supported_protocol_list();
  219. return protocol_list_contains(ours, pr, ver);
  220. }
  221. /**
  222. * Return true iff "list" encodes a protocol list that includes support for
  223. * the indicated protocol and version.
  224. */
  225. int
  226. protocol_list_supports_protocol(const char *list, protocol_type_t tp,
  227. uint32_t version)
  228. {
  229. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  230. * up in profiles, we should memoize it.
  231. */
  232. smartlist_t *protocols = parse_protocol_list(list);
  233. if (!protocols) {
  234. return 0;
  235. }
  236. int contains = protocol_list_contains(protocols, tp, version);
  237. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  238. smartlist_free(protocols);
  239. return contains;
  240. }
  241. /** Return the canonical string containing the list of protocols
  242. * that we support. */
  243. const char *
  244. protover_get_supported_protocols(void)
  245. {
  246. return
  247. "Cons=1-2 "
  248. "Desc=1-2 "
  249. "DirCache=1-2 "
  250. "HSDir=1-2 "
  251. "HSIntro=3-4 "
  252. "HSRend=1-2 "
  253. "Link=1-4 "
  254. "LinkAuth=1,3 "
  255. "Microdesc=1-2 "
  256. "Relay=1-2";
  257. }
  258. /** The protocols from protover_get_supported_protocols(), as parsed into a
  259. * list of proto_entry_t values. Access this via
  260. * get_supported_protocol_list. */
  261. static smartlist_t *supported_protocol_list = NULL;
  262. /** Return a pointer to a smartlist of proto_entry_t for the protocols
  263. * we support. */
  264. static const smartlist_t *
  265. get_supported_protocol_list(void)
  266. {
  267. if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
  268. supported_protocol_list =
  269. parse_protocol_list(protover_get_supported_protocols());
  270. }
  271. return supported_protocol_list;
  272. }
  273. /**
  274. * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
  275. * as one or more newly allocated strings.
  276. */
  277. static void
  278. proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
  279. {
  280. smartlist_add_asprintf(chunks, "%s=", entry->name);
  281. SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
  282. const char *comma = "";
  283. if (range_sl_idx != 0)
  284. comma = ",";
  285. if (range->low == range->high) {
  286. smartlist_add_asprintf(chunks, "%s%lu",
  287. comma, (unsigned long)range->low);
  288. } else {
  289. smartlist_add_asprintf(chunks, "%s%lu-%lu",
  290. comma, (unsigned long)range->low,
  291. (unsigned long)range->high);
  292. }
  293. } SMARTLIST_FOREACH_END(range);
  294. }
  295. /** Given a list of space-separated proto_entry_t items,
  296. * encode it into a newly allocated space-separated string. */
  297. STATIC char *
  298. encode_protocol_list(const smartlist_t *sl)
  299. {
  300. const char *separator = "";
  301. smartlist_t *chunks = smartlist_new();
  302. SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
  303. smartlist_add_strdup(chunks, separator);
  304. proto_entry_encode_into(chunks, ent);
  305. separator = " ";
  306. } SMARTLIST_FOREACH_END(ent);
  307. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  308. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  309. smartlist_free(chunks);
  310. return result;
  311. }
  312. /* We treat any protocol list with more than this many subprotocols in it
  313. * as a DoS attempt. */
  314. static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
  315. /** Voting helper: Given a list of proto_entry_t, return a newly allocated
  316. * smartlist of newly allocated strings, one for each included protocol
  317. * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
  318. * 'Foo=7'.)
  319. *
  320. * Do not list any protocol version more than once.
  321. *
  322. * Return NULL if the list would be too big.
  323. */
  324. static smartlist_t *
  325. expand_protocol_list(const smartlist_t *protos)
  326. {
  327. smartlist_t *expanded = smartlist_new();
  328. if (!protos)
  329. return expanded;
  330. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  331. const char *name = ent->name;
  332. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  333. uint32_t u;
  334. for (u = range->low; u <= range->high; ++u) {
  335. smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
  336. if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
  337. goto too_many;
  338. }
  339. } SMARTLIST_FOREACH_END(range);
  340. } SMARTLIST_FOREACH_END(ent);
  341. smartlist_sort_strings(expanded);
  342. smartlist_uniq_strings(expanded); // This makes voting work. do not remove
  343. return expanded;
  344. too_many:
  345. SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
  346. smartlist_free(expanded);
  347. return NULL;
  348. }
  349. /** Voting helper: compare two singleton proto_entry_t items by version
  350. * alone. (A singleton item is one with a single range entry where
  351. * low==high.) */
  352. static int
  353. cmp_single_ent_by_version(const void **a_, const void **b_)
  354. {
  355. const proto_entry_t *ent_a = *a_;
  356. const proto_entry_t *ent_b = *b_;
  357. tor_assert(smartlist_len(ent_a->ranges) == 1);
  358. tor_assert(smartlist_len(ent_b->ranges) == 1);
  359. const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
  360. const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
  361. tor_assert(a->low == a->high);
  362. tor_assert(b->low == b->high);
  363. if (a->low < b->low) {
  364. return -1;
  365. } else if (a->low == b->low) {
  366. return 0;
  367. } else {
  368. return 1;
  369. }
  370. }
  371. /** Voting helper: Given a list of singleton protocol strings (of the form
  372. * Foo=7), return a canonical listing of all the protocol versions listed,
  373. * with as few ranges as possible, with protocol versions sorted lexically and
  374. * versions sorted in numerically increasing order, using as few range entries
  375. * as possible.
  376. **/
  377. static char *
  378. contract_protocol_list(const smartlist_t *proto_strings)
  379. {
  380. // map from name to list of single-version entries
  381. strmap_t *entry_lists_by_name = strmap_new();
  382. // list of protocol names
  383. smartlist_t *all_names = smartlist_new();
  384. // list of strings for the output we're building
  385. smartlist_t *chunks = smartlist_new();
  386. // Parse each item and stick it entry_lists_by_name. Build
  387. // 'all_names' at the same time.
  388. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  389. if (BUG(!s))
  390. continue;// LCOV_EXCL_LINE
  391. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  392. if (BUG(!ent))
  393. continue; // LCOV_EXCL_LINE
  394. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  395. if (!lst) {
  396. smartlist_add(all_names, ent->name);
  397. lst = smartlist_new();
  398. strmap_set(entry_lists_by_name, ent->name, lst);
  399. }
  400. smartlist_add(lst, ent);
  401. } SMARTLIST_FOREACH_END(s);
  402. // We want to output the protocols sorted by their name.
  403. smartlist_sort_strings(all_names);
  404. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  405. const int first_entry = (name_sl_idx == 0);
  406. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  407. tor_assert(lst);
  408. // Sort every entry with this name by version. They are
  409. // singletons, so there can't be overlap.
  410. smartlist_sort(lst, cmp_single_ent_by_version);
  411. if (! first_entry)
  412. smartlist_add_strdup(chunks, " ");
  413. /* We're going to construct this entry from the ranges. */
  414. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  415. entry->ranges = smartlist_new();
  416. entry->name = tor_strdup(name);
  417. // Now, find all the ranges of versions start..end where
  418. // all of start, start+1, start+2, ..end are included.
  419. int start_of_cur_series = 0;
  420. while (start_of_cur_series < smartlist_len(lst)) {
  421. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  422. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  423. const uint32_t ver_low = range->low;
  424. uint32_t ver_high = ver_low;
  425. int idx;
  426. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  427. ent = smartlist_get(lst, idx);
  428. range = smartlist_get(ent->ranges, 0);
  429. if (range->low != ver_high + 1)
  430. break;
  431. ver_high += 1;
  432. }
  433. // Now idx is either off the end of the list, or the first sequence
  434. // break in the list.
  435. start_of_cur_series = idx;
  436. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  437. new_range->low = ver_low;
  438. new_range->high = ver_high;
  439. smartlist_add(entry->ranges, new_range);
  440. }
  441. proto_entry_encode_into(chunks, entry);
  442. proto_entry_free(entry);
  443. } SMARTLIST_FOREACH_END(name);
  444. // Build the result...
  445. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  446. // And free all the stuff we allocated.
  447. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  448. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  449. tor_assert(lst);
  450. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  451. smartlist_free(lst);
  452. } SMARTLIST_FOREACH_END(name);
  453. strmap_free(entry_lists_by_name, NULL);
  454. smartlist_free(all_names);
  455. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  456. smartlist_free(chunks);
  457. return result;
  458. }
  459. /**
  460. * Protocol voting implementation.
  461. *
  462. * Given a list of strings describing protocol versions, return a newly
  463. * allocated string encoding all of the protocols that are listed by at
  464. * least <b>threshold</b> of the inputs.
  465. *
  466. * The string is minimal and sorted according to the rules of
  467. * contract_protocol_list above.
  468. */
  469. char *
  470. protover_compute_vote(const smartlist_t *list_of_proto_strings,
  471. int threshold)
  472. {
  473. smartlist_t *all_entries = smartlist_new();
  474. // First, parse the inputs and break them into singleton entries.
  475. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  476. smartlist_t *unexpanded = parse_protocol_list(vote);
  477. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  478. if (this_vote == NULL) {
  479. log_warn(LD_NET, "When expanding a protocol list from an authority, I "
  480. "got too many protocols. This is possibly an attack or a bug, "
  481. "unless the Tor network truly has expanded to support over %d "
  482. "different subprotocol versions. The offending string was: %s",
  483. MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
  484. } else {
  485. smartlist_add_all(all_entries, this_vote);
  486. smartlist_free(this_vote);
  487. }
  488. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  489. smartlist_free(unexpanded);
  490. } SMARTLIST_FOREACH_END(vote);
  491. // Now sort the singleton entries
  492. smartlist_sort_strings(all_entries);
  493. // Now find all the strings that appear at least 'threshold' times.
  494. smartlist_t *include_entries = smartlist_new();
  495. const char *cur_entry = smartlist_get(all_entries, 0);
  496. int n_times = 0;
  497. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  498. if (!strcmp(ent, cur_entry)) {
  499. n_times++;
  500. } else {
  501. if (n_times >= threshold && cur_entry)
  502. smartlist_add(include_entries, (void*)cur_entry);
  503. cur_entry = ent;
  504. n_times = 1 ;
  505. }
  506. } SMARTLIST_FOREACH_END(ent);
  507. if (n_times >= threshold && cur_entry)
  508. smartlist_add(include_entries, (void*)cur_entry);
  509. // Finally, compress that list.
  510. char *result = contract_protocol_list(include_entries);
  511. smartlist_free(include_entries);
  512. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  513. smartlist_free(all_entries);
  514. return result;
  515. }
  516. /** Return true if every protocol version described in the string <b>s</b> is
  517. * one that we support, and false otherwise. If <b>missing_out</b> is
  518. * provided, set it to the list of protocols we do not support.
  519. *
  520. * NOTE: This is quadratic, but we don't do it much: only a few times per
  521. * consensus. Checking signatures should be way more expensive than this
  522. * ever would be.
  523. **/
  524. int
  525. protover_all_supported(const char *s, char **missing_out)
  526. {
  527. int all_supported = 1;
  528. smartlist_t *missing;
  529. if (!s) {
  530. return 1;
  531. }
  532. smartlist_t *entries = parse_protocol_list(s);
  533. missing = smartlist_new();
  534. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  535. protocol_type_t tp;
  536. if (str_to_protocol_type(ent->name, &tp) < 0) {
  537. if (smartlist_len(ent->ranges)) {
  538. goto unsupported;
  539. }
  540. continue;
  541. }
  542. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  543. uint32_t i;
  544. for (i = range->low; i <= range->high; ++i) {
  545. if (!protover_is_supported_here(tp, i)) {
  546. goto unsupported;
  547. }
  548. }
  549. } SMARTLIST_FOREACH_END(range);
  550. continue;
  551. unsupported:
  552. all_supported = 0;
  553. smartlist_add(missing, (void*) ent);
  554. } SMARTLIST_FOREACH_END(ent);
  555. if (missing_out && !all_supported) {
  556. tor_assert(0 != smartlist_len(missing));
  557. *missing_out = encode_protocol_list(missing);
  558. }
  559. smartlist_free(missing);
  560. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  561. smartlist_free(entries);
  562. return all_supported;
  563. }
  564. /** Helper: Given a list of proto_entry_t, return true iff
  565. * <b>pr</b>=<b>ver</b> is included in that list. */
  566. static int
  567. protocol_list_contains(const smartlist_t *protos,
  568. protocol_type_t pr, uint32_t ver)
  569. {
  570. if (BUG(protos == NULL)) {
  571. return 0; // LCOV_EXCL_LINE
  572. }
  573. const char *pr_name = protocol_type_to_str(pr);
  574. if (BUG(pr_name == NULL)) {
  575. return 0; // LCOV_EXCL_LINE
  576. }
  577. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  578. if (strcasecmp(ent->name, pr_name))
  579. continue;
  580. /* name matches; check the ranges */
  581. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  582. if (ver >= range->low && ver <= range->high)
  583. return 1;
  584. } SMARTLIST_FOREACH_END(range);
  585. } SMARTLIST_FOREACH_END(ent);
  586. return 0;
  587. }
  588. /** Return a string describing the protocols supported by tor version
  589. * <b>version</b>, or an empty string if we cannot tell.
  590. *
  591. * Note that this is only used to infer protocols for Tor versions that
  592. * can't declare their own.
  593. **/
  594. const char *
  595. protover_compute_for_old_tor(const char *version)
  596. {
  597. if (tor_version_as_new_as(version,
  598. FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
  599. return "";
  600. } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
  601. /* 0.2.9.1-alpha HSRend=2 */
  602. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
  603. "Link=1-4 LinkAuth=1 "
  604. "Microdesc=1-2 Relay=1-2";
  605. } else if (tor_version_as_new_as(version, "0.2.7.5")) {
  606. /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
  607. * ed25519 support. We'll call them present only in "stable" 027,
  608. * though. */
  609. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  610. "Link=1-4 LinkAuth=1 "
  611. "Microdesc=1-2 Relay=1-2";
  612. } else if (tor_version_as_new_as(version, "0.2.4.19")) {
  613. /* No currently supported Tor server versions are older than this, or
  614. * lack these protocols. */
  615. return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  616. "Link=1-4 LinkAuth=1 "
  617. "Microdesc=1 Relay=1-2";
  618. } else {
  619. /* Cannot infer protocols. */
  620. return "";
  621. }
  622. }
  623. /**
  624. * Release all storage held by static fields in protover.c
  625. */
  626. void
  627. protover_free_all(void)
  628. {
  629. if (supported_protocol_list) {
  630. smartlist_t *entries = supported_protocol_list;
  631. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  632. smartlist_free(entries);
  633. supported_protocol_list = NULL;
  634. }
  635. }
  636. #endif