protover.c 21 KB

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