protover.c 22 KB

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