protover.c 20 KB

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