nodefamily.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file nodefamily.c
  8. * \brief Code to manipulate encoded, reference-counted node families. We
  9. * use these tricks to save space, since these families would otherwise
  10. * require a large number of tiny allocations.
  11. **/
  12. #include "core/or/or.h"
  13. #include "feature/nodelist/nickname.h"
  14. #include "feature/nodelist/nodefamily.h"
  15. #include "feature/nodelist/nodefamily_st.h"
  16. #include "feature/nodelist/nodelist.h"
  17. #include "feature/relay/router.h"
  18. #include "feature/nodelist/routerlist.h"
  19. #include "ht.h"
  20. #include "siphash.h"
  21. #include "lib/container/smartlist.h"
  22. #include "lib/ctime/di_ops.h"
  23. #include "lib/defs/digest_sizes.h"
  24. #include "lib/log/util_bug.h"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. /**
  28. * Allocate and return a blank node family with space to hold <b>n_members</b>
  29. * members.
  30. */
  31. static nodefamily_t *
  32. nodefamily_alloc(int n_members)
  33. {
  34. size_t alloc_len = offsetof(nodefamily_t, family_members) +
  35. NODEFAMILY_ARRAY_SIZE(n_members);
  36. nodefamily_t *nf = tor_malloc_zero(alloc_len);
  37. nf->n_members = n_members;
  38. return nf;
  39. }
  40. /**
  41. * Hashtable hash implementation.
  42. */
  43. static inline unsigned int
  44. nodefamily_hash(const nodefamily_t *nf)
  45. {
  46. return (unsigned) siphash24g(nf->family_members,
  47. NODEFAMILY_ARRAY_SIZE(nf->n_members));
  48. }
  49. /**
  50. * Hashtable equality implementation.
  51. */
  52. static inline unsigned int
  53. nodefamily_eq(const nodefamily_t *a, const nodefamily_t *b)
  54. {
  55. return (a->n_members == b->n_members) &&
  56. fast_memeq(a->family_members, b->family_members,
  57. NODEFAMILY_ARRAY_SIZE(a->n_members));
  58. }
  59. static HT_HEAD(nodefamily_map, nodefamily_t) the_node_families
  60. = HT_INITIALIZER();
  61. HT_PROTOTYPE(nodefamily_map, nodefamily_t, ht_ent, nodefamily_hash,
  62. nodefamily_eq)
  63. HT_GENERATE2(nodefamily_map, nodefamily_t, ht_ent, nodefamily_hash,
  64. node_family_eq, 0.6, tor_reallocarray_, tor_free_)
  65. /**
  66. * Parse the family declaration in <b>s</b>, returning the canonical
  67. * <b>nodefamily_t</b> for its members. Return NULL on error.
  68. *
  69. * If <b>rsa_id_self</b> is provided, it is a DIGEST_LEN-byte digest
  70. * for the router that declared this family: insert it into the
  71. * family declaration if it is not there already.
  72. *
  73. * If NF_WARN_MALFORMED is set in <b>flags</b>, warn about any
  74. * elements that we can't parse. (By default, we log at info.)
  75. *
  76. * If NF_REJECT_MALFORMED is set in <b>flags</b>, treat any unparseable
  77. * elements as an error. (By default, we simply omit them.)
  78. **/
  79. nodefamily_t *
  80. nodefamily_parse(const char *s, const uint8_t *rsa_id_self,
  81. unsigned flags)
  82. {
  83. smartlist_t *sl = smartlist_new();
  84. smartlist_split_string(sl, s, NULL, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  85. nodefamily_t *result = nodefamily_from_members(sl, rsa_id_self, flags, NULL);
  86. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  87. smartlist_free(sl);
  88. return result;
  89. }
  90. /**
  91. * Canonicalize the family list <b>s</b>, returning a newly allocated string.
  92. *
  93. * The canonicalization rules are fully specified in dir-spec.txt, but,
  94. * briefly: $hexid entries are put in caps, $hexid[=~]foo entries are
  95. * truncated, nicknames are put into lowercase, unrecognized entries are left
  96. * alone, and everything is sorted.
  97. **/
  98. char *
  99. nodefamily_canonicalize(const char *s, const uint8_t *rsa_id_self,
  100. unsigned flags)
  101. {
  102. smartlist_t *sl = smartlist_new();
  103. smartlist_t *result_members = smartlist_new();
  104. smartlist_split_string(sl, s, NULL, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  105. nodefamily_t *nf = nodefamily_from_members(sl, rsa_id_self, flags,
  106. result_members);
  107. char *formatted = nodefamily_format(nf);
  108. smartlist_split_string(result_members, formatted, NULL,
  109. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  110. smartlist_sort_strings(result_members);
  111. char *combined = smartlist_join_strings(result_members, " ", 0, NULL);
  112. nodefamily_free(nf);
  113. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  114. smartlist_free(sl);
  115. SMARTLIST_FOREACH(result_members, char *, cp, tor_free(cp));
  116. smartlist_free(result_members);
  117. tor_free(formatted);
  118. return combined;
  119. }
  120. /**
  121. * qsort helper for encoded nodefamily elements.
  122. **/
  123. static int
  124. compare_members(const void *a, const void *b)
  125. {
  126. return fast_memcmp(a, b, NODEFAMILY_MEMBER_LEN);
  127. }
  128. /**
  129. * Parse the member strings in <b>members</b>, returning their canonical
  130. * <b>nodefamily_t</b>. Return NULL on error.
  131. *
  132. * If <b>rsa_id_self</b> is provided, it is a DIGEST_LEN-byte digest
  133. * for the router that declared this family: insert it into the
  134. * family declaration if it is not there already.
  135. *
  136. * The <b>flags</b> element is interpreted as in nodefamily_parse().
  137. *
  138. * If <b>unrecognized</b> is provided, fill it copies of any unrecognized
  139. * members. (Note that malformed $hexids are not considered unrecognized.)
  140. **/
  141. nodefamily_t *
  142. nodefamily_from_members(const smartlist_t *members,
  143. const uint8_t *rsa_id_self,
  144. unsigned flags,
  145. smartlist_t *unrecognized_out)
  146. {
  147. const int n_self = rsa_id_self ? 1 : 0;
  148. int n_bad_elements = 0;
  149. int n_members = smartlist_len(members) + n_self;
  150. nodefamily_t *tmp = nodefamily_alloc(n_members);
  151. uint8_t *ptr = NODEFAMILY_MEMBER_PTR(tmp, 0);
  152. SMARTLIST_FOREACH_BEGIN(members, const char *, cp) {
  153. bool bad_element = true;
  154. if (is_legal_nickname(cp)) {
  155. ptr[0] = NODEFAMILY_BY_NICKNAME;
  156. tor_assert(strlen(cp) < DIGEST_LEN); // guaranteed by is_legal_nickname
  157. memcpy(ptr+1, cp, strlen(cp));
  158. tor_strlower((char*) ptr+1);
  159. bad_element = false;
  160. } else if (is_legal_hexdigest(cp)) {
  161. char digest_buf[DIGEST_LEN];
  162. char nn_buf[MAX_NICKNAME_LEN+1];
  163. char nn_char=0;
  164. if (hex_digest_nickname_decode(cp, digest_buf, &nn_char, nn_buf)==0) {
  165. bad_element = false;
  166. ptr[0] = NODEFAMILY_BY_RSA_ID;
  167. memcpy(ptr+1, digest_buf, DIGEST_LEN);
  168. }
  169. } else {
  170. if (unrecognized_out)
  171. smartlist_add_strdup(unrecognized_out, cp);
  172. }
  173. if (bad_element) {
  174. const int severity = (flags & NF_WARN_MALFORMED) ? LOG_WARN : LOG_INFO;
  175. log_fn(severity, LD_GENERAL,
  176. "Bad element %s while parsing a node family.",
  177. escaped(cp));
  178. ++n_bad_elements;
  179. } else {
  180. ptr += NODEFAMILY_MEMBER_LEN;
  181. }
  182. } SMARTLIST_FOREACH_END(cp);
  183. if (n_bad_elements && (flags & NF_REJECT_MALFORMED))
  184. goto err;
  185. if (rsa_id_self) {
  186. /* Add self. */
  187. ptr[0] = NODEFAMILY_BY_RSA_ID;
  188. memcpy(ptr+1, rsa_id_self, DIGEST_LEN);
  189. }
  190. n_members -= n_bad_elements;
  191. /* Sort tmp into canonical order. */
  192. qsort(tmp->family_members, n_members, NODEFAMILY_MEMBER_LEN,
  193. compare_members);
  194. /* Remove duplicates. */
  195. int i;
  196. for (i = 0; i < n_members-1; ++i) {
  197. uint8_t *thisptr = NODEFAMILY_MEMBER_PTR(tmp, i);
  198. uint8_t *nextptr = NODEFAMILY_MEMBER_PTR(tmp, i+1);
  199. if (fast_memeq(thisptr, nextptr, NODEFAMILY_MEMBER_LEN)) {
  200. memmove(thisptr, nextptr, (n_members-i-1)*NODEFAMILY_MEMBER_LEN);
  201. --n_members;
  202. --i;
  203. }
  204. }
  205. int n_members_alloc = tmp->n_members;
  206. tmp->n_members = n_members;
  207. /* See if we already allocated this family. */
  208. nodefamily_t *found = HT_FIND(nodefamily_map, &the_node_families, tmp);
  209. if (found) {
  210. /* If we did, great: incref it and return it. */
  211. ++found->refcnt;
  212. tor_free(tmp);
  213. return found;
  214. } else {
  215. /* If not, insert it into the hashtable. */
  216. if (n_members_alloc != n_members) {
  217. /* Compact the family if needed */
  218. nodefamily_t *tmp2 = nodefamily_alloc(n_members);
  219. memcpy(tmp2->family_members, tmp->family_members,
  220. n_members * NODEFAMILY_MEMBER_LEN);
  221. tor_free(tmp);
  222. tmp = tmp2;
  223. }
  224. tmp->refcnt = 1;
  225. HT_INSERT(nodefamily_map, &the_node_families, tmp);
  226. return tmp;
  227. }
  228. err:
  229. tor_free(tmp);
  230. return NULL;
  231. }
  232. /**
  233. * Drop our reference to <b>family</b>, freeing it if there are no more
  234. * references.
  235. */
  236. void
  237. nodefamily_free_(nodefamily_t *family)
  238. {
  239. if (family == NULL)
  240. return;
  241. --family->refcnt;
  242. if (family->refcnt == 0) {
  243. HT_REMOVE(nodefamily_map, &the_node_families, family);
  244. tor_free(family);
  245. }
  246. }
  247. /**
  248. * Return true iff <b>family</b> contains the SHA1 RSA1024 identity
  249. * <b>rsa_id</b>.
  250. */
  251. bool
  252. nodefamily_contains_rsa_id(const nodefamily_t *family,
  253. const uint8_t *rsa_id)
  254. {
  255. if (family == NULL)
  256. return false;
  257. unsigned i;
  258. for (i = 0; i < family->n_members; ++i) {
  259. const uint8_t *ptr = NODEFAMILY_MEMBER_PTR(family, i);
  260. if (ptr[0] == NODEFAMILY_BY_RSA_ID &&
  261. fast_memeq(ptr+1, rsa_id, DIGEST_LEN)) {
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. /**
  268. * Return true iff <b>family</b> contains the nickname <b>name</b>.
  269. */
  270. bool
  271. nodefamily_contains_nickname(const nodefamily_t *family,
  272. const char *name)
  273. {
  274. if (family == NULL)
  275. return false;
  276. unsigned i;
  277. for (i = 0; i < family->n_members; ++i) {
  278. const uint8_t *ptr = NODEFAMILY_MEMBER_PTR(family, i);
  279. // note that the strcasecmp() is safe because there is always at least one
  280. // NUL in the encoded nickname, because all legal nicknames are less than
  281. // DIGEST_LEN bytes long.
  282. if (ptr[0] == NODEFAMILY_BY_NICKNAME && !strcasecmp((char*)ptr+1, name)) {
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. /**
  289. * Return true if <b>family</b> contains the nickname or the RSA ID for
  290. * <b>node</b>
  291. **/
  292. bool
  293. nodefamily_contains_node(const nodefamily_t *family,
  294. const node_t *node)
  295. {
  296. return
  297. nodefamily_contains_nickname(family, node_get_nickname(node))
  298. ||
  299. nodefamily_contains_rsa_id(family, node_get_rsa_id_digest(node));
  300. }
  301. /**
  302. * Look up every entry in <b>family</b>, and add add the corresponding
  303. * node_t to <b>out</b>.
  304. **/
  305. void
  306. nodefamily_add_nodes_to_smartlist(const nodefamily_t *family,
  307. smartlist_t *out)
  308. {
  309. if (!family)
  310. return;
  311. unsigned i;
  312. for (i = 0; i < family->n_members; ++i) {
  313. const uint8_t *ptr = NODEFAMILY_MEMBER_PTR(family, i);
  314. const node_t *node = NULL;
  315. switch (ptr[0]) {
  316. case NODEFAMILY_BY_NICKNAME:
  317. node = node_get_by_nickname((char*)ptr+1, NNF_NO_WARN_UNNAMED);
  318. break;
  319. case NODEFAMILY_BY_RSA_ID:
  320. node = node_get_by_id((char*)ptr+1);
  321. break;
  322. default:
  323. /* LCOV_EXCL_START */
  324. tor_assert_nonfatal_unreached();
  325. break;
  326. /* LCOV_EXCL_STOP */
  327. }
  328. if (node)
  329. smartlist_add(out, (void *)node);
  330. }
  331. }
  332. /**
  333. * Encode <b>family</b> as a space-separated string.
  334. */
  335. char *
  336. nodefamily_format(const nodefamily_t *family)
  337. {
  338. if (!family)
  339. return tor_strdup("");
  340. unsigned i;
  341. smartlist_t *sl = smartlist_new();
  342. for (i = 0; i < family->n_members; ++i) {
  343. const uint8_t *ptr = NODEFAMILY_MEMBER_PTR(family, i);
  344. switch (ptr[0]) {
  345. case NODEFAMILY_BY_NICKNAME:
  346. smartlist_add_strdup(sl, (char*)ptr+1);
  347. break;
  348. case NODEFAMILY_BY_RSA_ID: {
  349. char buf[HEX_DIGEST_LEN+2];
  350. buf[0]='$';
  351. base16_encode(buf+1, sizeof(buf)-1, (char*)ptr+1, DIGEST_LEN);
  352. tor_strupper(buf);
  353. smartlist_add_strdup(sl, buf);
  354. break;
  355. }
  356. default:
  357. /* LCOV_EXCL_START */
  358. tor_assert_nonfatal_unreached();
  359. break;
  360. /* LCOV_EXCL_STOP */
  361. }
  362. }
  363. char *result = smartlist_join_strings(sl, " ", 0, NULL);
  364. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  365. smartlist_free(sl);
  366. return result;
  367. }
  368. /**
  369. * Free all storage held in the nodefamily map.
  370. **/
  371. void
  372. nodefamily_free_all(void)
  373. {
  374. HT_CLEAR(nodefamily_map, &the_node_families);
  375. }