protover.c 20 KB

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