consdiff.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /* Copyright (c) 2014, Daniel Martí
  2. * Copyright (c) 2014, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file consdiff.c
  6. * \brief Consensus diff implementation, including both the generation and the
  7. * application of diffs in a minimal ed format.
  8. *
  9. * The consensus diff application is done in consdiff_apply_diff, which relies
  10. * on apply_ed_diff for the main ed diff part and on some digest helper
  11. * functions to check the digest hashes found in the consensus diff header.
  12. *
  13. * The consensus diff generation is more complex. consdiff_gen_diff generates
  14. * it, relying on gen_ed_diff to generate the ed diff and some digest helper
  15. * functions to generate the digest hashes.
  16. *
  17. * gen_ed_diff is the tricky bit. In it simplest form, it will take quadratic
  18. * time and linear space to generate an ed diff given two smartlists. As shown
  19. * in its comment section, calling calc_changes on the entire two consensuses
  20. * will calculate what is to be added and what is to be deleted in the diff.
  21. * Its comment section briefly explains how it works.
  22. *
  23. * In our case specific to consensuses, we take advantage of the fact that
  24. * consensuses list routers sorted by their identities. We use that
  25. * information to avoid running calc_changes on the whole smartlists.
  26. * gen_ed_diff will navigate through the two consensuses identity by identity
  27. * and will send small couples of slices to calc_changes, keeping the running
  28. * time near-linear. This is explained in more detail in the gen_ed_diff
  29. * comments.
  30. **/
  31. #define CONSDIFF_PRIVATE
  32. #include "or.h"
  33. #include "consdiff.h"
  34. #include "routerparse.h"
  35. static const char* ns_diff_version = "network-status-diff-version 1";
  36. static const char* hash_token = "hash";
  37. static char *consensus_join_lines(const smartlist_t *inp);
  38. /** DOCDOC */
  39. MOCK_IMPL(STATIC int,
  40. consensus_compute_digest,(const char *cons,
  41. consensus_digest_t *digest_out))
  42. {
  43. int r = crypto_digest256((char*)digest_out->sha3_256,
  44. cons, strlen(cons), DIGEST_SHA3_256);
  45. return r;
  46. }
  47. /** Create (allocate) a new slice from a smartlist. Assumes that the start
  48. * and the end indexes are within the bounds of the initial smartlist. The end
  49. * element is not part of the resulting slice. If end is -1, the slice is to
  50. * reach the end of the smartlist.
  51. */
  52. STATIC smartlist_slice_t *
  53. smartlist_slice(const smartlist_t *list, int start, int end)
  54. {
  55. int list_len = smartlist_len(list);
  56. tor_assert(start >= 0);
  57. tor_assert(start <= list_len);
  58. if (end == -1) {
  59. end = list_len;
  60. }
  61. tor_assert(start <= end);
  62. smartlist_slice_t *slice = tor_malloc(sizeof(smartlist_slice_t));
  63. slice->list = list;
  64. slice->offset = start;
  65. slice->len = end - start;
  66. return slice;
  67. }
  68. /** Helper: Compute the longest common subsequence lengths for the two slices.
  69. * Used as part of the diff generation to find the column at which to split
  70. * slice2 while still having the optimal solution.
  71. * If direction is -1, the navigation is reversed. Otherwise it must be 1.
  72. * The length of the resulting integer array is that of the second slice plus
  73. * one.
  74. */
  75. STATIC int *
  76. lcs_lengths(const smartlist_slice_t *slice1, const smartlist_slice_t *slice2,
  77. int direction)
  78. {
  79. size_t a_size = sizeof(int) * (slice2->len+1);
  80. /* Resulting lcs lengths. */
  81. int *result = tor_malloc_zero(a_size);
  82. /* Copy of the lcs lengths from the last iteration. */
  83. int *prev = tor_malloc(a_size);
  84. tor_assert(direction == 1 || direction == -1);
  85. int si = slice1->offset;
  86. if (direction == -1) {
  87. si += (slice1->len-1);
  88. }
  89. for (int i = 0; i < slice1->len; ++i, si+=direction) {
  90. const char *line1 = smartlist_get(slice1->list, si);
  91. /* Store the last results. */
  92. memcpy(prev, result, a_size);
  93. int sj = slice2->offset;
  94. if (direction == -1) {
  95. sj += (slice2->len-1);
  96. }
  97. for (int j = 0; j < slice2->len; ++j, sj+=direction) {
  98. const char *line2 = smartlist_get(slice2->list, sj);
  99. if (!strcmp(line1, line2)) {
  100. /* If the lines are equal, the lcs is one line longer. */
  101. result[j + 1] = prev[j] + 1;
  102. } else {
  103. /* If not, see what lcs parent path is longer. */
  104. result[j + 1] = MAX(result[j], prev[j + 1]);
  105. }
  106. }
  107. }
  108. tor_free(prev);
  109. return result;
  110. }
  111. /** Helper: Trim any number of lines that are equally at the start or the end
  112. * of both slices.
  113. */
  114. STATIC void
  115. trim_slices(smartlist_slice_t *slice1, smartlist_slice_t *slice2)
  116. {
  117. while (slice1->len>0 && slice2->len>0) {
  118. const char *line1 = smartlist_get(slice1->list, slice1->offset);
  119. const char *line2 = smartlist_get(slice2->list, slice2->offset);
  120. if (strcmp(line1, line2)) {
  121. break;
  122. }
  123. slice1->offset++; slice1->len--;
  124. slice2->offset++; slice2->len--;
  125. }
  126. int i1 = (slice1->offset+slice1->len)-1;
  127. int i2 = (slice2->offset+slice2->len)-1;
  128. while (slice1->len>0 && slice2->len>0) {
  129. const char *line1 = smartlist_get(slice1->list, i1);
  130. const char *line2 = smartlist_get(slice2->list, i2);
  131. if (strcmp(line1, line2)) {
  132. break;
  133. }
  134. i1--;
  135. slice1->len--;
  136. i2--;
  137. slice2->len--;
  138. }
  139. }
  140. /** Like smartlist_string_pos, but limited to the bounds of the slice.
  141. */
  142. STATIC int
  143. smartlist_slice_string_pos(const smartlist_slice_t *slice, const char *string)
  144. {
  145. int end = slice->offset + slice->len;
  146. for (int i = slice->offset; i < end; ++i) {
  147. const char *el = smartlist_get(slice->list, i);
  148. if (!strcmp(el, string)) {
  149. return i;
  150. }
  151. }
  152. return -1;
  153. }
  154. /** Helper: Set all the appropriate changed booleans to true. The first slice
  155. * must be of length 0 or 1. All the lines of slice1 and slice2 which are not
  156. * present in the other slice will be set to changed in their bool array.
  157. * The two changed bool arrays are passed in the same order as the slices.
  158. */
  159. STATIC void
  160. set_changed(bitarray_t *changed1, bitarray_t *changed2,
  161. const smartlist_slice_t *slice1, const smartlist_slice_t *slice2)
  162. {
  163. int toskip = -1;
  164. tor_assert(slice1->len == 0 || slice1->len == 1);
  165. if (slice1->len == 1) {
  166. const char *line_common = smartlist_get(slice1->list, slice1->offset);
  167. toskip = smartlist_slice_string_pos(slice2, line_common);
  168. if (toskip == -1) {
  169. bitarray_set(changed1, slice1->offset);
  170. }
  171. }
  172. int end = slice2->offset + slice2->len;
  173. for (int i = slice2->offset; i < end; ++i) {
  174. if (i != toskip) {
  175. bitarray_set(changed2, i);
  176. }
  177. }
  178. }
  179. /*
  180. * Helper: Given that slice1 has been split by half into top and bot, we want
  181. * to fetch the column at which to split slice2 so that we are still on track
  182. * to the optimal diff solution, i.e. the shortest one. We use lcs_lengths
  183. * since the shortest diff is just another way to say the longest common
  184. * subsequence.
  185. */
  186. static int
  187. optimal_column_to_split(const smartlist_slice_t *top,
  188. const smartlist_slice_t *bot,
  189. const smartlist_slice_t *slice2)
  190. {
  191. int *lens_top = lcs_lengths(top, slice2, 1);
  192. int *lens_bot = lcs_lengths(bot, slice2, -1);
  193. int column=0, max_sum=-1;
  194. for (int i = 0; i < slice2->len+1; ++i) {
  195. int sum = lens_top[i] + lens_bot[slice2->len-i];
  196. if (sum > max_sum) {
  197. column = i;
  198. max_sum = sum;
  199. }
  200. }
  201. tor_free(lens_top);
  202. tor_free(lens_bot);
  203. return column;
  204. }
  205. /**
  206. * Helper: Figure out what elements are new or gone on the second smartlist
  207. * relative to the first smartlist, and store the booleans in the bitarrays.
  208. * True on the first bitarray means the element is gone, true on the second
  209. * bitarray means it's new.
  210. *
  211. * In its base case, either of the smartlists is of length <= 1 and we can
  212. * quickly see what elements are new or are gone. In the other case, we will
  213. * split one smartlist by half and we'll use optimal_column_to_split to find
  214. * the optimal column at which to split the second smartlist so that we are
  215. * finding the smallest diff possible.
  216. */
  217. STATIC void
  218. calc_changes(smartlist_slice_t *slice1,
  219. smartlist_slice_t *slice2,
  220. bitarray_t *changed1, bitarray_t *changed2)
  221. {
  222. trim_slices(slice1, slice2);
  223. if (slice1->len <= 1) {
  224. set_changed(changed1, changed2, slice1, slice2);
  225. } else if (slice2->len <= 1) {
  226. set_changed(changed2, changed1, slice2, slice1);
  227. /* Keep on splitting the slices in two. */
  228. } else {
  229. smartlist_slice_t *top, *bot, *left, *right;
  230. /* Split the first slice in half. */
  231. int mid = slice1->len/2;
  232. top = smartlist_slice(slice1->list, slice1->offset, slice1->offset+mid);
  233. bot = smartlist_slice(slice1->list, slice1->offset+mid,
  234. slice1->offset+slice1->len);
  235. /* Split the second slice by the optimal column. */
  236. int mid2 = optimal_column_to_split(top, bot, slice2);
  237. left = smartlist_slice(slice2->list, slice2->offset, slice2->offset+mid2);
  238. right = smartlist_slice(slice2->list, slice2->offset+mid2,
  239. slice2->offset+slice2->len);
  240. calc_changes(top, left, changed1, changed2);
  241. calc_changes(bot, right, changed1, changed2);
  242. tor_free(top);
  243. tor_free(bot);
  244. tor_free(left);
  245. tor_free(right);
  246. }
  247. }
  248. /* This table is from crypto.c. The SP and PAD defines are different. */
  249. #define X 255
  250. #define SP X
  251. #define PAD X
  252. static const uint8_t base64_compare_table[256] = {
  253. X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X,
  254. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  255. SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63,
  256. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X,
  257. X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  258. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X,
  259. X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  260. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X,
  261. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  262. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  263. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  264. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  265. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  266. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  267. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  268. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  269. };
  270. /** Helper: Get the identity hash from a router line, assuming that the line
  271. * at least appears to be a router line and thus starts with "r ".
  272. */
  273. STATIC const char *
  274. get_id_hash(const char *r_line)
  275. {
  276. r_line += strlen("r ");
  277. /* Skip the router name. */
  278. const char *hash = strchr(r_line, ' ');
  279. if (!hash) {
  280. return NULL;
  281. }
  282. hash++;
  283. const char *hash_end = hash;
  284. /* Stop when the first non-base64 character is found. Use unsigned chars to
  285. * avoid negative indexes causing crashes.
  286. */
  287. while (base64_compare_table[*((unsigned char*)hash_end)] != X) {
  288. hash_end++;
  289. }
  290. /* Empty hash. */
  291. if (hash_end == hash) {
  292. return NULL;
  293. }
  294. return hash;
  295. }
  296. /** Helper: Check that a line is a valid router entry. We must at least be
  297. * able to fetch a proper identity hash from it for it to be valid.
  298. */
  299. STATIC int
  300. is_valid_router_entry(const char *line)
  301. {
  302. if (strcmpstart(line, "r ") != 0) {
  303. return 0;
  304. }
  305. return (get_id_hash(line) != NULL);
  306. }
  307. /** Helper: Find the next router line starting at the current position.
  308. * Assumes that cur is lower than the length of the smartlist, i.e. it is a
  309. * line within the bounds of the consensus. The only exception is when we
  310. * don't want to skip the first line, in which case cur will be -1.
  311. */
  312. STATIC int
  313. next_router(const smartlist_t *cons, int cur)
  314. {
  315. int len = smartlist_len(cons);
  316. tor_assert(cur >= -1 && cur < len);
  317. if (++cur >= len) {
  318. return len;
  319. }
  320. const char *line = smartlist_get(cons, cur);
  321. while (!is_valid_router_entry(line)) {
  322. if (++cur >= len) {
  323. return len;
  324. }
  325. line = smartlist_get(cons, cur);
  326. }
  327. return cur;
  328. }
  329. /** Helper: compare two base64-encoded identity hashes which may be of
  330. * different lengths. Comparison ends when the first non-base64 char is found.
  331. */
  332. STATIC int
  333. base64cmp(const char *hash1, const char *hash2)
  334. {
  335. /* NULL is always lower, useful for last_hash which starts at NULL. */
  336. if (!hash1 && !hash2) {
  337. return 0;
  338. }
  339. if (!hash1) {
  340. return -1;
  341. }
  342. if (!hash2) {
  343. return 1;
  344. }
  345. /* Don't index with a char; char may be signed. */
  346. const unsigned char *a = (unsigned char*)hash1;
  347. const unsigned char *b = (unsigned char*)hash2;
  348. while (1) {
  349. uint8_t av = base64_compare_table[*a];
  350. uint8_t bv = base64_compare_table[*b];
  351. if (av == X) {
  352. if (bv == X) {
  353. /* Both ended with exactly the same characters. */
  354. return 0;
  355. } else {
  356. /* hash2 goes on longer than hash1 and thus hash1 is lower. */
  357. return -1;
  358. }
  359. } else if (bv == X) {
  360. /* hash1 goes on longer than hash2 and thus hash1 is greater. */
  361. return 1;
  362. } else if (av < bv) {
  363. /* The first difference shows that hash1 is lower. */
  364. return -1;
  365. } else if (av > bv) {
  366. /* The first difference shows that hash1 is greater. */
  367. return 1;
  368. } else {
  369. a++;
  370. b++;
  371. }
  372. }
  373. }
  374. /** Generate an ed diff as a smartlist from two consensuses, also given as
  375. * smartlists. Will return NULL if the diff could not be generated, which can
  376. * happen if any lines the script had to add matched "." or if the routers
  377. * were not properly ordered.
  378. *
  379. * This implementation is consensus-specific. To generate an ed diff for any
  380. * given input in quadratic time, you can replace all the code until the
  381. * navigation in reverse order with the following:
  382. *
  383. * int len1 = smartlist_len(cons1);
  384. * int len2 = smartlist_len(cons2);
  385. * bitarray_t *changed1 = bitarray_init_zero(len1);
  386. * bitarray_t *changed2 = bitarray_init_zero(len2);
  387. * cons1_sl = smartlist_slice(cons1, 0, -1);
  388. * cons2_sl = smartlist_slice(cons2, 0, -1);
  389. * calc_changes(cons1_sl, cons2_sl, changed1, changed2);
  390. */
  391. STATIC smartlist_t *
  392. gen_ed_diff(const smartlist_t *cons1, const smartlist_t *cons2)
  393. {
  394. int len1 = smartlist_len(cons1);
  395. int len2 = smartlist_len(cons2);
  396. smartlist_t *result = smartlist_new();
  397. /* Initialize the changed bitarrays to zero, so that calc_changes only needs
  398. * to set the ones that matter and leave the rest untouched.
  399. */
  400. bitarray_t *changed1 = bitarray_init_zero(len1);
  401. bitarray_t *changed2 = bitarray_init_zero(len2);
  402. int i1=-1, i2=-1;
  403. int start1=0, start2=0;
  404. const char *hash1 = NULL;
  405. const char *hash2 = NULL;
  406. /* To check that hashes are ordered properly */
  407. const char *last_hash1 = NULL;
  408. const char *last_hash2 = NULL;
  409. /* i1 and i2 are initialized at the first line of each consensus. They never
  410. * reach past len1 and len2 respectively, since next_router doesn't let that
  411. * happen. i1 and i2 are advanced by at least one line at each iteration as
  412. * long as they have not yet reached len1 and len2, so the loop is
  413. * guaranteed to end, and each pair of (i1,i2) will be inspected at most
  414. * once.
  415. */
  416. while (i1 < len1 || i2 < len2) {
  417. /* Advance each of the two navigation positions by one router entry if not
  418. * yet at the end.
  419. */
  420. if (i1 < len1) {
  421. i1 = next_router(cons1, i1);
  422. if (i1 != len1) {
  423. last_hash1 = hash1;
  424. hash1 = get_id_hash(smartlist_get(cons1, i1));
  425. if (base64cmp(hash1, last_hash1) <= 0) {
  426. log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
  427. "the base consensus doesn't have its router entries "
  428. "sorted properly.");
  429. goto error_cleanup;
  430. }
  431. }
  432. }
  433. if (i2 < len2) {
  434. i2 = next_router(cons2, i2);
  435. if (i2 != len2) {
  436. last_hash2 = hash2;
  437. hash2 = get_id_hash(smartlist_get(cons2, i2));
  438. if (base64cmp(hash2, last_hash2) <= 0) {
  439. log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
  440. "the target consensus doesn't have its router entries "
  441. "sorted properly.");
  442. goto error_cleanup;
  443. }
  444. }
  445. }
  446. /* If we have reached the end of both consensuses, there is no need to
  447. * compare hashes anymore, since this is the last iteration.
  448. */
  449. if (i1 < len1 || i2 < len2) {
  450. /* Keep on advancing the lower (by identity hash sorting) position until
  451. * we have two matching positions. The only other possible outcome is
  452. * that a lower position reaches the end of the consensus before it can
  453. * reach a hash that is no longer the lower one. Since there will always
  454. * be a lower hash for as long as the loop runs, one of the two indexes
  455. * will always be incremented, thus assuring that the loop must end
  456. * after a finite number of iterations. If that cannot be because said
  457. * consensus has already reached the end, both are extended to their
  458. * respecting ends since we are done.
  459. */
  460. int cmp = base64cmp(hash1, hash2);
  461. while (cmp != 0) {
  462. if (i1 < len1 && cmp < 0) {
  463. i1 = next_router(cons1, i1);
  464. if (i1 == len1) {
  465. /* We finished the first consensus, so grab all the remaining
  466. * lines of the second consensus and finish up.
  467. */
  468. i2 = len2;
  469. break;
  470. }
  471. last_hash1 = hash1;
  472. hash1 = get_id_hash(smartlist_get(cons1, i1));
  473. if (base64cmp(hash1, last_hash1) <= 0) {
  474. log_warn(LD_CONSDIFF, "Refusing to generate consensus diff "
  475. "because the base consensus doesn't have its router entries "
  476. "sorted properly.");
  477. goto error_cleanup;
  478. }
  479. } else if (i2 < len2 && cmp > 0) {
  480. i2 = next_router(cons2, i2);
  481. if (i2 == len2) {
  482. /* We finished the second consensus, so grab all the remaining
  483. * lines of the first consensus and finish up.
  484. */
  485. i1 = len1;
  486. break;
  487. }
  488. last_hash2 = hash2;
  489. hash2 = get_id_hash(smartlist_get(cons2, i2));
  490. if (base64cmp(hash2, last_hash2) <= 0) {
  491. log_warn(LD_CONSDIFF, "Refusing to generate consensus diff "
  492. "because the target consensus doesn't have its router entries "
  493. "sorted properly.");
  494. goto error_cleanup;
  495. }
  496. } else {
  497. i1 = len1;
  498. i2 = len2;
  499. break;
  500. }
  501. cmp = base64cmp(hash1, hash2);
  502. }
  503. }
  504. /* Make slices out of these chunks (up to the common router entry) and
  505. * calculate the changes for them.
  506. * Error if any of the two slices are longer than 10K lines. That should
  507. * never happen with any pair of real consensuses. Feeding more than 10K
  508. * lines to calc_changes would be very slow anyway.
  509. */
  510. #define MAX_LINE_COUNT (10000)
  511. if (i1-start1 > MAX_LINE_COUNT || i2-start2 > MAX_LINE_COUNT) {
  512. log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
  513. "we found too few common router ids.");
  514. goto error_cleanup;
  515. }
  516. smartlist_slice_t *cons1_sl = smartlist_slice(cons1, start1, i1);
  517. smartlist_slice_t *cons2_sl = smartlist_slice(cons2, start2, i2);
  518. calc_changes(cons1_sl, cons2_sl, changed1, changed2);
  519. tor_free(cons1_sl);
  520. tor_free(cons2_sl);
  521. start1 = i1, start2 = i2;
  522. }
  523. /* Navigate the changes in reverse order and generate one ed command for
  524. * each chunk of changes.
  525. */
  526. i1=len1-1, i2=len2-1;
  527. while (i1 > 0 || i2 > 0) {
  528. int start1x, start2x, end1, end2, added, deleted;
  529. /* We are at a point were no changed bools are true, so just keep going. */
  530. if (!(i1 >= 0 && bitarray_is_set(changed1, i1)) &&
  531. !(i2 >= 0 && bitarray_is_set(changed2, i2))) {
  532. if (i1 >= 0) {
  533. i1--;
  534. }
  535. if (i2 >= 0) {
  536. i2--;
  537. }
  538. continue;
  539. }
  540. end1 = i1, end2 = i2;
  541. /* Grab all contiguous changed lines */
  542. while (i1 >= 0 && bitarray_is_set(changed1, i1)) {
  543. i1--;
  544. }
  545. while (i2 >= 0 && bitarray_is_set(changed2, i2)) {
  546. i2--;
  547. }
  548. start1x = i1+1, start2x = i2+1;
  549. added = end2-i2, deleted = end1-i1;
  550. if (added == 0) {
  551. if (deleted == 1) {
  552. smartlist_add_asprintf(result, "%id", start1x+1);
  553. } else {
  554. smartlist_add_asprintf(result, "%i,%id", start1x+1, start1x+deleted);
  555. }
  556. } else {
  557. int i;
  558. if (deleted == 0) {
  559. smartlist_add_asprintf(result, "%ia", start1x);
  560. } else if (deleted == 1) {
  561. smartlist_add_asprintf(result, "%ic", start1x+1);
  562. } else {
  563. smartlist_add_asprintf(result, "%i,%ic", start1x+1, start1x+deleted);
  564. }
  565. for (i = start2x; i <= end2; ++i) {
  566. const char *line = smartlist_get(cons2, i);
  567. if (!strcmp(line, ".")) {
  568. log_warn(LD_CONSDIFF, "Cannot generate consensus diff because "
  569. "one of the lines to be added is \".\".");
  570. goto error_cleanup;
  571. }
  572. smartlist_add(result, tor_strdup(line));
  573. }
  574. smartlist_add_asprintf(result, ".");
  575. }
  576. }
  577. bitarray_free(changed1);
  578. bitarray_free(changed2);
  579. return result;
  580. error_cleanup:
  581. bitarray_free(changed1);
  582. bitarray_free(changed2);
  583. SMARTLIST_FOREACH(result, char*, line, tor_free(line));
  584. smartlist_free(result);
  585. return NULL;
  586. }
  587. /** Apply the ed diff, starting at <b>diff_starting_line</b>, to the consensus
  588. * and return a new consensus, also as a line-based smartlist. Will return
  589. * NULL if the ed diff is not properly formatted.
  590. */
  591. STATIC smartlist_t *
  592. apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff,
  593. int diff_starting_line)
  594. {
  595. int diff_len = smartlist_len(diff);
  596. int j = smartlist_len(cons1);
  597. smartlist_t *cons2 = smartlist_new();
  598. for (int i=diff_starting_line; i<diff_len; ++i) {
  599. const char *diff_line = smartlist_get(diff, i);
  600. char *endptr1, *endptr2;
  601. int start = (int)strtol(diff_line, &endptr1, 10);
  602. int end;
  603. if (endptr1 == diff_line) {
  604. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  605. "an ed command was missing a line number.");
  606. goto error_cleanup;
  607. }
  608. /* Two-item range */
  609. if (*endptr1 == ',') {
  610. end = (int)strtol(endptr1+1, &endptr2, 10);
  611. if (endptr2 == endptr1+1) {
  612. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  613. "an ed command was missing a range end line number.");
  614. goto error_cleanup;
  615. }
  616. /* Incoherent range. */
  617. if (end <= start) {
  618. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  619. "an invalid range was found in an ed command.");
  620. goto error_cleanup;
  621. }
  622. /* We'll take <n1> as <n1>,<n1> for simplicity. */
  623. } else {
  624. endptr2 = endptr1;
  625. end = start;
  626. }
  627. if (end > j) {
  628. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  629. "its commands are not properly sorted in reverse order.");
  630. goto error_cleanup;
  631. }
  632. if (*endptr2 == '\0') {
  633. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  634. "a line with no ed command was found");
  635. goto error_cleanup;
  636. }
  637. if (*(endptr2+1) != '\0') {
  638. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  639. "an ed command longer than one char was found.");
  640. goto error_cleanup;
  641. }
  642. char action = *endptr2;
  643. switch (action) {
  644. case 'a':
  645. case 'c':
  646. case 'd':
  647. break;
  648. default:
  649. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  650. "an unrecognised ed command was found.");
  651. goto error_cleanup;
  652. }
  653. /* Add unchanged lines. */
  654. for (; j > end; --j) {
  655. const char *cons_line = smartlist_get(cons1, j-1);
  656. smartlist_add(cons2, tor_strdup(cons_line));
  657. }
  658. /* Ignore removed lines. */
  659. if (action == 'c' || action == 'd') {
  660. while (--j >= start) {
  661. /* Skip line */
  662. }
  663. }
  664. /* Add new lines in reverse order, since it will all be reversed at the
  665. * end.
  666. */
  667. if (action == 'a' || action == 'c') {
  668. int added_end = i;
  669. i++; /* Skip the line with the range and command. */
  670. while (i < diff_len) {
  671. if (!strcmp(smartlist_get(diff, i), ".")) {
  672. break;
  673. }
  674. if (++i == diff_len) {
  675. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  676. "it has lines to be inserted that don't end with a \".\".");
  677. goto error_cleanup;
  678. }
  679. }
  680. int added_i = i-1;
  681. /* It would make no sense to add zero new lines. */
  682. if (added_i == added_end) {
  683. log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
  684. "it has an ed command that tries to insert zero lines.");
  685. goto error_cleanup;
  686. }
  687. while (added_i > added_end) {
  688. const char *added_line = smartlist_get(diff, added_i--);
  689. smartlist_add(cons2, tor_strdup(added_line));
  690. }
  691. }
  692. }
  693. /* Add remaining unchanged lines. */
  694. for (; j > 0; --j) {
  695. const char *cons_line = smartlist_get(cons1, j-1);
  696. smartlist_add(cons2, tor_strdup(cons_line));
  697. }
  698. /* Reverse the whole thing since we did it from the end. */
  699. smartlist_reverse(cons2);
  700. return cons2;
  701. error_cleanup:
  702. SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
  703. smartlist_free(cons2);
  704. return NULL;
  705. }
  706. /** Generate a consensus diff as a smartlist from two given consensuses, also
  707. * as smartlists. Will return NULL if the consensus diff could not be
  708. * generated. Neither of the two consensuses are modified in any way, so it's
  709. * up to the caller to free their resources.
  710. */
  711. smartlist_t *
  712. consdiff_gen_diff(const smartlist_t *cons1, const smartlist_t *cons2,
  713. const consensus_digest_t *digests1,
  714. const consensus_digest_t *digests2)
  715. {
  716. smartlist_t *ed_diff = gen_ed_diff(cons1, cons2);
  717. /* ed diff could not be generated - reason already logged by gen_ed_diff. */
  718. if (!ed_diff) {
  719. goto error_cleanup;
  720. }
  721. /* See that the script actually produces what we want. */
  722. smartlist_t *ed_cons2 = apply_ed_diff(cons1, ed_diff, 0);
  723. if (!ed_cons2) {
  724. /* LCOV_EXCL_START -- impossible if diff generation is correct */
  725. log_warn(LD_BUG|LD_CONSDIFF, "Refusing to generate consensus diff because "
  726. "the generated ed diff could not be tested to successfully generate "
  727. "the target consensus.");
  728. goto error_cleanup;
  729. /* LCOV_EXCL_STOP */
  730. }
  731. int cons2_eq = smartlist_strings_eq(cons2, ed_cons2);
  732. SMARTLIST_FOREACH(ed_cons2, char*, line, tor_free(line));
  733. smartlist_free(ed_cons2);
  734. if (!cons2_eq) {
  735. /* LCOV_EXCL_START -- impossible if diff generation is correct. */
  736. log_warn(LD_BUG|LD_CONSDIFF, "Refusing to generate consensus diff because "
  737. "the generated ed diff did not generate the target consensus "
  738. "successfully when tested.");
  739. goto error_cleanup;
  740. /* LCOV_EXCL_STOP */
  741. }
  742. char cons1_hash_hex[HEX_DIGEST256_LEN+1];
  743. char cons2_hash_hex[HEX_DIGEST256_LEN+1];
  744. base16_encode(cons1_hash_hex, HEX_DIGEST256_LEN+1,
  745. (const char*)digests1->sha3_256, DIGEST256_LEN);
  746. base16_encode(cons2_hash_hex, HEX_DIGEST256_LEN+1,
  747. (const char*)digests2->sha3_256, DIGEST256_LEN);
  748. /* Create the resulting consensus diff. */
  749. smartlist_t *result = smartlist_new();
  750. smartlist_add_asprintf(result, "%s", ns_diff_version);
  751. smartlist_add_asprintf(result, "%s %s %s", hash_token,
  752. cons1_hash_hex, cons2_hash_hex);
  753. smartlist_add_all(result, ed_diff);
  754. smartlist_free(ed_diff);
  755. return result;
  756. error_cleanup:
  757. if (ed_diff) {
  758. /* LCOV_EXCL_START -- ed_diff is NULL except in unreachable cases above */
  759. SMARTLIST_FOREACH(ed_diff, char *, cp, tor_free(cp));
  760. smartlist_free(ed_diff);
  761. /* LCOV_EXCL_STOP */
  762. }
  763. return NULL;
  764. }
  765. /** Fetch the digest of the base consensus in the consensus diff, encoded in
  766. * base16 as found in the diff itself. digest1_out and digest2_out must be of
  767. * length DIGEST256_LEN or larger if not NULL.
  768. */
  769. int
  770. consdiff_get_digests(const smartlist_t *diff,
  771. char *digest1_out,
  772. char *digest2_out)
  773. {
  774. smartlist_t *hash_words = NULL;
  775. const char *format;
  776. char cons1_hash[DIGEST256_LEN], cons2_hash[DIGEST256_LEN];
  777. char *cons1_hash_hex, *cons2_hash_hex;
  778. if (smartlist_len(diff) < 2) {
  779. log_info(LD_CONSDIFF, "The provided consensus diff is too short.");
  780. goto error_cleanup;
  781. }
  782. /* Check that it's the format and version we know. */
  783. format = smartlist_get(diff, 0);
  784. if (strcmp(format, ns_diff_version)) {
  785. log_warn(LD_CONSDIFF, "The provided consensus diff format is not known.");
  786. goto error_cleanup;
  787. }
  788. /* Grab the base16 digests. */
  789. hash_words = smartlist_new();
  790. smartlist_split_string(hash_words, smartlist_get(diff, 1), " ", 0, 0);
  791. /* There have to be three words, the first of which must be hash_token. */
  792. if (smartlist_len(hash_words) != 3 ||
  793. strcmp(smartlist_get(hash_words, 0), hash_token)) {
  794. log_info(LD_CONSDIFF, "The provided consensus diff does not include "
  795. "the necessary digests.");
  796. goto error_cleanup;
  797. }
  798. /* Expected hashes as found in the consensus diff header. They must be of
  799. * length HEX_DIGEST256_LEN, normally 64 hexadecimal characters.
  800. * If any of the decodings fail, error to make sure that the hashes are
  801. * proper base16-encoded digests.
  802. */
  803. cons1_hash_hex = smartlist_get(hash_words, 1);
  804. cons2_hash_hex = smartlist_get(hash_words, 2);
  805. if (strlen(cons1_hash_hex) != HEX_DIGEST256_LEN ||
  806. strlen(cons2_hash_hex) != HEX_DIGEST256_LEN) {
  807. log_info(LD_CONSDIFF, "The provided consensus diff includes "
  808. "base16-encoded digests of incorrect size.");
  809. goto error_cleanup;
  810. }
  811. if (base16_decode(cons1_hash, DIGEST256_LEN,
  812. cons1_hash_hex, HEX_DIGEST256_LEN) != DIGEST256_LEN ||
  813. base16_decode(cons2_hash, DIGEST256_LEN,
  814. cons2_hash_hex, HEX_DIGEST256_LEN) != DIGEST256_LEN) {
  815. log_info(LD_CONSDIFF, "The provided consensus diff includes "
  816. "malformed digests.");
  817. goto error_cleanup;
  818. }
  819. if (digest1_out) {
  820. memcpy(digest1_out, cons1_hash, DIGEST256_LEN);
  821. }
  822. if (digest2_out) {
  823. memcpy(digest2_out, cons2_hash, DIGEST256_LEN);
  824. }
  825. SMARTLIST_FOREACH(hash_words, char *, cp, tor_free(cp));
  826. smartlist_free(hash_words);
  827. return 0;
  828. error_cleanup:
  829. if (hash_words) {
  830. SMARTLIST_FOREACH(hash_words, char *, cp, tor_free(cp));
  831. smartlist_free(hash_words);
  832. }
  833. return 1;
  834. }
  835. /** Apply the consensus diff to the given consensus and return a new
  836. * consensus, also as a line-based smartlist. Will return NULL if the diff
  837. * could not be applied. Neither the consensus nor the diff are modified in
  838. * any way, so it's up to the caller to free their resources.
  839. */
  840. char *
  841. consdiff_apply_diff(const smartlist_t *cons1,
  842. const smartlist_t *diff,
  843. const consensus_digest_t *digests1)
  844. {
  845. smartlist_t *cons2 = NULL;
  846. char *cons2_str = NULL;
  847. char e_cons1_hash[DIGEST256_LEN];
  848. char e_cons2_hash[DIGEST256_LEN];
  849. if (consdiff_get_digests(diff, e_cons1_hash, e_cons2_hash) != 0) {
  850. goto error_cleanup;
  851. }
  852. /* See that the consensus that was given to us matches its hash. */
  853. if (fast_memneq(digests1->sha3_256, e_cons1_hash,
  854. DIGEST256_LEN)) {
  855. char hex_digest1[HEX_DIGEST256_LEN+1];
  856. char e_hex_digest1[HEX_DIGEST256_LEN+1];
  857. log_warn(LD_CONSDIFF, "Refusing to apply consensus diff because "
  858. "the base consensus doesn't match the digest as found in "
  859. "the consensus diff header.");
  860. base16_encode(hex_digest1, HEX_DIGEST256_LEN+1,
  861. (const char *)digests1->sha3_256, DIGEST256_LEN);
  862. base16_encode(e_hex_digest1, HEX_DIGEST256_LEN+1,
  863. e_cons1_hash, DIGEST256_LEN);
  864. log_warn(LD_CONSDIFF, "Expected: %s; found: %s",
  865. hex_digest1, e_hex_digest1);
  866. goto error_cleanup;
  867. }
  868. /* Grab the ed diff and calculate the resulting consensus. */
  869. /* Skip the first two lines. */
  870. cons2 = apply_ed_diff(cons1, diff, 2);
  871. /* ed diff could not be applied - reason already logged by apply_ed_diff. */
  872. if (!cons2) {
  873. goto error_cleanup;
  874. }
  875. cons2_str = consensus_join_lines(cons2);
  876. consensus_digest_t cons2_digests;
  877. if (consensus_compute_digest(cons2_str, &cons2_digests) < 0) {
  878. /* LCOV_EXCL_START -- digest can't fail */
  879. log_warn(LD_CONSDIFF, "Could not compute digests of the consensus "
  880. "resulting from applying a consensus diff.");
  881. goto error_cleanup;
  882. /* LCOV_EXCL_STOP */
  883. }
  884. /* See that the resulting consensus matches its hash. */
  885. if (fast_memneq(cons2_digests.sha3_256, e_cons2_hash,
  886. DIGEST256_LEN)) {
  887. log_warn(LD_CONSDIFF, "Refusing to apply consensus diff because "
  888. "the resulting consensus doesn't match the digest as found in "
  889. "the consensus diff header.");
  890. char hex_digest2[HEX_DIGEST256_LEN+1];
  891. char e_hex_digest2[HEX_DIGEST256_LEN+1];
  892. base16_encode(hex_digest2, HEX_DIGEST256_LEN+1,
  893. (const char *)cons2_digests.sha3_256, DIGEST256_LEN);
  894. base16_encode(e_hex_digest2, HEX_DIGEST256_LEN+1,
  895. e_cons2_hash, DIGEST256_LEN);
  896. log_warn(LD_CONSDIFF, "Expected: %s; found: %s",
  897. hex_digest2, e_hex_digest2);
  898. goto error_cleanup;
  899. }
  900. goto done;
  901. error_cleanup:
  902. tor_free(cons2_str); /* Sets it to NULL */
  903. done:
  904. if (cons2) {
  905. SMARTLIST_FOREACH(cons2, char *, cp, tor_free(cp));
  906. smartlist_free(cons2);
  907. }
  908. return cons2_str;
  909. }
  910. /**DOCDOC*/
  911. static int
  912. consensus_split_lines(smartlist_t *out, const char *s)
  913. {
  914. /* XXXX If we used string slices, we could avoid a bunch of copies here. */
  915. while (*s) {
  916. const char *eol = strchr(s, '\n');
  917. if (!eol) {
  918. /* File doesn't end with newline. */
  919. return -1;
  920. }
  921. smartlist_add(out, tor_strndup(s, eol-s));
  922. s = eol+1;
  923. }
  924. return 0;
  925. }
  926. /** DOCDOC */
  927. static char *
  928. consensus_join_lines(const smartlist_t *inp)
  929. {
  930. size_t n = 0;
  931. SMARTLIST_FOREACH(inp, const char *, cp, n += strlen(cp) + 1);
  932. n += 1;
  933. char *result = tor_malloc(n);
  934. char *out = result;
  935. SMARTLIST_FOREACH_BEGIN(inp, const char *, cp) {
  936. size_t len = strlen(cp);
  937. memcpy(out, cp, len);
  938. out += len;
  939. *out++ = '\n';
  940. } SMARTLIST_FOREACH_END(cp);
  941. *out++ = '\0';
  942. tor_assert(out == result+n);
  943. return result;
  944. }
  945. /**DOCDOC */
  946. char *
  947. consensus_diff_generate(const char *cons1,
  948. const char *cons2)
  949. {
  950. consensus_digest_t d1, d2;
  951. smartlist_t *lines1 = NULL, *lines2 = NULL, *result_lines = NULL;
  952. int r1, r2;
  953. char *result = NULL;
  954. r1 = consensus_compute_digest(cons1, &d1);
  955. r2 = consensus_compute_digest(cons2, &d2);
  956. if (BUG(r1 < 0 || r2 < 0))
  957. return NULL; // LCOV_EXCL_LINE
  958. lines1 = smartlist_new();
  959. lines2 = smartlist_new();
  960. if (consensus_split_lines(lines1, cons1) < 0)
  961. goto done;
  962. if (consensus_split_lines(lines2, cons2) < 0)
  963. goto done;
  964. result_lines = consdiff_gen_diff(lines1, lines2, &d1, &d2);
  965. done:
  966. SMARTLIST_FOREACH(lines1, char *, cp, tor_free(cp));
  967. smartlist_free(lines1);
  968. SMARTLIST_FOREACH(lines2, char *, cp, tor_free(cp));
  969. smartlist_free(lines2);
  970. if (result_lines) {
  971. result = consensus_join_lines(result_lines);
  972. SMARTLIST_FOREACH(result_lines, char *, cp, tor_free(cp));
  973. smartlist_free(result_lines);
  974. }
  975. return result;
  976. }
  977. /** DOCDOC */
  978. char *
  979. consensus_diff_apply(const char *consensus,
  980. const char *diff)
  981. {
  982. consensus_digest_t d1;
  983. smartlist_t *lines1 = NULL, *lines2 = NULL;
  984. int r1;
  985. char *result = NULL;
  986. r1 = consensus_compute_digest(consensus, &d1);
  987. if (BUG(r1 < 0))
  988. return NULL; // LCOV_EXCL_LINE
  989. lines1 = smartlist_new();
  990. lines2 = smartlist_new();
  991. if (consensus_split_lines(lines1, consensus) < 0)
  992. goto done;
  993. if (consensus_split_lines(lines2, diff) < 0)
  994. goto done;
  995. result = consdiff_apply_diff(lines1, lines2, &d1);
  996. done:
  997. SMARTLIST_FOREACH(lines1, char *, cp, tor_free(cp));
  998. smartlist_free(lines1);
  999. SMARTLIST_FOREACH(lines2, char *, cp, tor_free(cp));
  1000. smartlist_free(lines2);
  1001. return result;
  1002. }