protover.c 17 KB

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