protover.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. /** Return true if every protocol version described in the string <b>s</b> is
  262. * one that we support, and false otherwise. If <b>missing_out</b> is
  263. * provided, set it to the list of protocols we do not support.
  264. *
  265. * NOTE: This is quadratic, but we don't do it much: only a few times per
  266. * consensus. Checking signatures should be way more expensive than this
  267. * ever would be.
  268. **/
  269. int
  270. protover_all_supported(const char *s, char **missing_out)
  271. {
  272. int all_supported = 1;
  273. smartlist_t *missing;
  274. if (!s) {
  275. return 1;
  276. }
  277. smartlist_t *entries = parse_protocol_list(s);
  278. missing = smartlist_new();
  279. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  280. protocol_type_t tp;
  281. if (str_to_protocol_type(ent->name, &tp) < 0) {
  282. if (smartlist_len(ent->ranges)) {
  283. goto unsupported;
  284. }
  285. continue;
  286. }
  287. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  288. uint32_t i;
  289. for (i = range->low; i <= range->high; ++i) {
  290. if (!protover_is_supported_here(tp, i)) {
  291. goto unsupported;
  292. }
  293. }
  294. } SMARTLIST_FOREACH_END(range);
  295. continue;
  296. unsupported:
  297. all_supported = 0;
  298. smartlist_add(missing, (void*) ent);
  299. } SMARTLIST_FOREACH_END(ent);
  300. if (missing_out && !all_supported) {
  301. tor_assert(0 != smartlist_len(missing));
  302. *missing_out = encode_protocol_list(missing);
  303. }
  304. smartlist_free(missing);
  305. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  306. smartlist_free(entries);
  307. return all_supported;
  308. }
  309. static int
  310. protocol_list_contains(const smartlist_t *protos,
  311. protocol_type_t pr, uint32_t ver)
  312. {
  313. if (BUG(protos == NULL)) {
  314. return 0;
  315. }
  316. const char *pr_name = protocol_type_to_str(pr);
  317. if (BUG(pr_name == NULL)) {
  318. return 0;
  319. }
  320. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  321. if (strcasecmp(ent->name, pr_name))
  322. continue;
  323. /* name matches; check the ranges */
  324. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  325. if (ver >= range->low && ver <= range->high)
  326. return 1;
  327. } SMARTLIST_FOREACH_END(range);
  328. } SMARTLIST_FOREACH_END(ent);
  329. return 0;
  330. }
  331. void
  332. protover_free_all(void)
  333. {
  334. if (supported_protocol_list) {
  335. smartlist_t *entries = supported_protocol_list;
  336. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  337. smartlist_free(entries);
  338. supported_protocol_list = NULL;
  339. }
  340. }