protover.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. #define PROTOVER_PRIVATE
  2. #include "protover.h"
  3. #include "compat.h"
  4. #include "torlog.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. /** Return the canonical string containing the list of protocols
  199. * that we support. */
  200. const char *
  201. get_supported_protocols(void)
  202. {
  203. return
  204. "Cons=1-2 "
  205. "Desc=1-2 "
  206. "DirCache=1 "
  207. "HSDir=1 "
  208. "HSMid=1 "
  209. "Link=1-4 "
  210. "LinkAuth=1 "
  211. "Microdesc=1-2 "
  212. "Relay=1-2";
  213. }
  214. /** The protocols from get_supported_protocols(), as parsed into a list of
  215. * proto_entry_t values. Access this via get_supported_protocol_list. */
  216. static smartlist_t *supported_protocol_list = NULL;
  217. /** Return a pointer to a smartlist of proto_entry_t for the protocols
  218. * we support. */
  219. static const smartlist_t *
  220. get_supported_protocol_list(void)
  221. {
  222. if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
  223. supported_protocol_list = parse_protocol_list(get_supported_protocols());
  224. }
  225. return supported_protocol_list;
  226. }
  227. /**
  228. * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
  229. * as one or more newly allocated strings.
  230. */
  231. static void
  232. proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
  233. {
  234. smartlist_add_asprintf(chunks, "%s=", entry->name);
  235. SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
  236. const char *comma = "";
  237. if (range_sl_idx != 0)
  238. comma = ",";
  239. if (range->low == range->high) {
  240. smartlist_add_asprintf(chunks, "%s%lu",
  241. comma, (unsigned long)range->low);
  242. } else {
  243. smartlist_add_asprintf(chunks, "%s%lu-%lu",
  244. comma, (unsigned long)range->low,
  245. (unsigned long)range->high);
  246. }
  247. } SMARTLIST_FOREACH_END(range);
  248. }
  249. /** Given a list of space-separated proto_entry_t items,
  250. * encode it into a newly allocated space-separated string. */
  251. STATIC char *
  252. encode_protocol_list(const smartlist_t *sl)
  253. {
  254. const char *separator = "";
  255. smartlist_t *chunks = smartlist_new();
  256. SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
  257. smartlist_add(chunks, tor_strdup(separator));
  258. proto_entry_encode_into(chunks, ent);
  259. separator = " ";
  260. } SMARTLIST_FOREACH_END(ent);
  261. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  262. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  263. smartlist_free(chunks);
  264. return result;
  265. }
  266. /** Voting helper: Given a list of proto_entry_t, return a newly allocated
  267. * smartlist of newly allocated strings, one for each included protocol
  268. * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
  269. * 'Foo=7'.)
  270. *
  271. * Do not list any protocol version more than once. */
  272. static smartlist_t *
  273. expand_protocol_list(const smartlist_t *protos)
  274. {
  275. // XXXX This can make really huge lists from small inputs; that's a DoS
  276. // problem.
  277. smartlist_t *expanded = smartlist_new();
  278. if (!protos)
  279. return expanded;
  280. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  281. const char *name = ent->name;
  282. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  283. uint32_t u;
  284. for (u = range->low; u <= range->high; ++u) {
  285. smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
  286. }
  287. } SMARTLIST_FOREACH_END(range);
  288. } SMARTLIST_FOREACH_END(ent);
  289. smartlist_sort_strings(expanded);
  290. smartlist_uniq_strings(expanded); // This makes voting work. do not remove
  291. return expanded;
  292. }
  293. /** Voting helper: compare two singleton proto_entry_t items by version
  294. * alone. (A singleton item is one with a single range entry where
  295. * low==high.) */
  296. static int
  297. cmp_single_ent_by_version(const void **a_, const void **b_)
  298. {
  299. const proto_entry_t *ent_a = *a_;
  300. const proto_entry_t *ent_b = *b_;
  301. tor_assert(smartlist_len(ent_a->ranges) == 1);
  302. tor_assert(smartlist_len(ent_b->ranges) == 1);
  303. const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
  304. const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
  305. tor_assert(a->low == a->high);
  306. tor_assert(b->low == b->high);
  307. if (a->low < b->low) {
  308. return -1;
  309. } else if (a->low == b->low) {
  310. return 0;
  311. } else {
  312. return 1;
  313. }
  314. }
  315. /** Voting helper: Given a list of singleton protocol strings (of the form
  316. * Foo=7), return a canonical listing of all the protocol versions listed,
  317. * with as few ranges as possible, with protocol versions sorted lexically and
  318. * versions sorted in numerically increasing order, using as few range entries
  319. * as possible.
  320. **/
  321. static char *
  322. contract_protocol_list(const smartlist_t *proto_strings)
  323. {
  324. // map from name to list of single-version entries
  325. strmap_t *entry_lists_by_name = strmap_new();
  326. // list of protocol names
  327. smartlist_t *all_names = smartlist_new();
  328. // list of strings for the output we're building
  329. smartlist_t *chunks = smartlist_new();
  330. // Parse each item and stick it entry_lists_by_name. Build
  331. // 'all_names' at the same time.
  332. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  333. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  334. if (BUG(!ent))
  335. continue; // LCOV_EXCL_LINE
  336. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  337. if (!lst) {
  338. smartlist_add(all_names, ent->name);
  339. lst = smartlist_new();
  340. strmap_set(entry_lists_by_name, ent->name, lst);
  341. }
  342. smartlist_add(lst, ent);
  343. } SMARTLIST_FOREACH_END(s);
  344. // We want to output the protocols sorted by their name.
  345. smartlist_sort_strings(all_names);
  346. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  347. const int first_entry = (name_sl_idx == 0);
  348. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  349. tor_assert(lst);
  350. // Sort every entry with this name by version. They are
  351. // singletons, so there can't be overlap.
  352. smartlist_sort(lst, cmp_single_ent_by_version);
  353. if (! first_entry)
  354. smartlist_add(chunks, tor_strdup(" "));
  355. /* We're going to construct this entry from the ranges. */
  356. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  357. entry->ranges = smartlist_new();
  358. entry->name = tor_strdup(name);
  359. // Now, find all the ranges of versions start..end where
  360. // all of start, start+1, start+2, ..end are included.
  361. int start_of_cur_series = 0;
  362. while (start_of_cur_series < smartlist_len(lst)) {
  363. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  364. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  365. const uint32_t ver_low = range->low;
  366. uint32_t ver_high = ver_low;
  367. int idx;
  368. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  369. ent = smartlist_get(lst, idx);
  370. range = smartlist_get(ent->ranges, 0);
  371. if (range->low != ver_high + 1)
  372. break;
  373. ver_high += 1;
  374. }
  375. // Now idx is either off the end of the list, or the first sequence
  376. // break in the list.
  377. start_of_cur_series = idx;
  378. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  379. new_range->low = ver_low;
  380. new_range->high = ver_high;
  381. smartlist_add(entry->ranges, new_range);
  382. }
  383. proto_entry_encode_into(chunks, entry);
  384. proto_entry_free(entry);
  385. } SMARTLIST_FOREACH_END(name);
  386. // Build the result...
  387. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  388. // And free all the stuff we allocated.
  389. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  390. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  391. tor_assert(lst);
  392. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  393. smartlist_free(lst);
  394. } SMARTLIST_FOREACH_END(name);
  395. strmap_free(entry_lists_by_name, NULL);
  396. smartlist_free(all_names);
  397. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  398. smartlist_free(chunks);
  399. return result;
  400. }
  401. /**
  402. * Protocol voting implementation.
  403. *
  404. * Given a list of strings describing protocol versions, return a newly
  405. * allocated string encoding all of the protocols that are listed by at
  406. * least <b>threshold</b> of the inputs.
  407. *
  408. * The string is minimal and sorted according to the rules of
  409. * contract_protocol_list above.
  410. */
  411. char *
  412. compute_protover_vote(const smartlist_t *list_of_proto_strings,
  413. int threshold)
  414. {
  415. // XXXX This algorithm can be made to use too much RAM. Fix that.
  416. smartlist_t *all_entries = smartlist_new();
  417. // First, parse the inputs and break them into singleton entries.
  418. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  419. smartlist_t *unexpanded = parse_protocol_list(vote);
  420. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  421. smartlist_add_all(all_entries, this_vote);
  422. smartlist_free(this_vote);
  423. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  424. smartlist_free(unexpanded);
  425. } SMARTLIST_FOREACH_END(vote);
  426. // Now sort the singleton entries
  427. smartlist_sort_strings(all_entries);
  428. // Now find all the strings that appear at least 'threshold' times.
  429. smartlist_t *include_entries = smartlist_new();
  430. const char *cur_entry = smartlist_get(all_entries, 0);
  431. int n_times = 0;
  432. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  433. if (!strcmp(ent, cur_entry)) {
  434. n_times++;
  435. } else {
  436. if (n_times >= threshold)
  437. smartlist_add(include_entries, (void*)cur_entry);
  438. cur_entry = ent;
  439. n_times = 1 ;
  440. }
  441. } SMARTLIST_FOREACH_END(ent);
  442. if (n_times >= threshold)
  443. smartlist_add(include_entries, (void*)cur_entry);
  444. // Finally, compress that list.
  445. char *result = contract_protocol_list(include_entries);
  446. smartlist_free(include_entries);
  447. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  448. smartlist_free(all_entries);
  449. return result;
  450. }
  451. /** Return true if every protocol version described in the string <b>s</b> is
  452. * one that we support, and false otherwise. If <b>missing_out</b> is
  453. * provided, set it to the list of protocols we do not support.
  454. *
  455. * NOTE: This is quadratic, but we don't do it much: only a few times per
  456. * consensus. Checking signatures should be way more expensive than this
  457. * ever would be.
  458. **/
  459. int
  460. protover_all_supported(const char *s, char **missing_out)
  461. {
  462. int all_supported = 1;
  463. smartlist_t *missing;
  464. if (!s) {
  465. return 1;
  466. }
  467. smartlist_t *entries = parse_protocol_list(s);
  468. missing = smartlist_new();
  469. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  470. protocol_type_t tp;
  471. if (str_to_protocol_type(ent->name, &tp) < 0) {
  472. if (smartlist_len(ent->ranges)) {
  473. goto unsupported;
  474. }
  475. continue;
  476. }
  477. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  478. uint32_t i;
  479. for (i = range->low; i <= range->high; ++i) {
  480. if (!protover_is_supported_here(tp, i)) {
  481. goto unsupported;
  482. }
  483. }
  484. } SMARTLIST_FOREACH_END(range);
  485. continue;
  486. unsupported:
  487. all_supported = 0;
  488. smartlist_add(missing, (void*) ent);
  489. } SMARTLIST_FOREACH_END(ent);
  490. if (missing_out && !all_supported) {
  491. tor_assert(0 != smartlist_len(missing));
  492. *missing_out = encode_protocol_list(missing);
  493. }
  494. smartlist_free(missing);
  495. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  496. smartlist_free(entries);
  497. return all_supported;
  498. }
  499. static int
  500. protocol_list_contains(const smartlist_t *protos,
  501. protocol_type_t pr, uint32_t ver)
  502. {
  503. if (BUG(protos == NULL)) {
  504. return 0; // LCOV_EXCL_LINE
  505. }
  506. const char *pr_name = protocol_type_to_str(pr);
  507. if (BUG(pr_name == NULL)) {
  508. return 0; // LCOV_EXCL_LINE
  509. }
  510. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  511. if (strcasecmp(ent->name, pr_name))
  512. continue;
  513. /* name matches; check the ranges */
  514. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  515. if (ver >= range->low && ver <= range->high)
  516. return 1;
  517. } SMARTLIST_FOREACH_END(range);
  518. } SMARTLIST_FOREACH_END(ent);
  519. return 0;
  520. }
  521. void
  522. protover_free_all(void)
  523. {
  524. if (supported_protocol_list) {
  525. smartlist_t *entries = supported_protocol_list;
  526. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  527. smartlist_free(entries);
  528. supported_protocol_list = NULL;
  529. }
  530. }