protover.c 21 KB

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