rps-test_util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file rps/rps-test_util.c
  18. * @brief Some utils faciliating the view into the internals for the sampler
  19. * needed for evaluation
  20. *
  21. * @author Julius Bünger
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "rps-test_util.h"
  26. #include <inttypes.h>
  27. #define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
  28. #define B2B_PAT "%c%c%c%c%c%c%c%c"
  29. #define B2B(byte) \
  30. (byte & 0x80 ? '1' : '0'), \
  31. (byte & 0x40 ? '1' : '0'), \
  32. (byte & 0x20 ? '1' : '0'), \
  33. (byte & 0x10 ? '1' : '0'), \
  34. (byte & 0x08 ? '1' : '0'), \
  35. (byte & 0x04 ? '1' : '0'), \
  36. (byte & 0x02 ? '1' : '0'), \
  37. (byte & 0x01 ? '1' : '0')
  38. #ifdef TO_FILE
  39. /**
  40. * @brief buffer for storing the unaligned bits for the next write
  41. */
  42. static char buf_unaligned;
  43. /**
  44. * @brief number of bits in unaligned buffer
  45. */
  46. static unsigned num_bits_buf_unaligned;
  47. static struct GNUNET_CONTAINER_MultiHashMap *open_files;
  48. /**
  49. * @brief Get file handle
  50. *
  51. * If necessary, create file handle and store it with the other file handles.
  52. *
  53. * @param name Name of the file
  54. *
  55. * @return File handle
  56. */
  57. struct GNUNET_DISK_FileHandle *
  58. get_file_handle (const char *name)
  59. {
  60. struct GNUNET_HashCode hash;
  61. struct GNUNET_DISK_FileHandle *fh;
  62. if (NULL == open_files)
  63. {
  64. open_files = GNUNET_CONTAINER_multihashmap_create (16,
  65. GNUNET_NO);
  66. LOG (GNUNET_ERROR_TYPE_DEBUG,
  67. "Created map of open files.\n");
  68. }
  69. GNUNET_CRYPTO_hash (name,
  70. strlen (name),
  71. &hash);
  72. if (NULL != (fh = GNUNET_CONTAINER_multihashmap_get (open_files,
  73. &hash)))
  74. return fh;
  75. fh = GNUNET_DISK_file_open (name,
  76. GNUNET_DISK_OPEN_WRITE |
  77. GNUNET_DISK_OPEN_CREATE |
  78. GNUNET_DISK_OPEN_APPEND,
  79. GNUNET_DISK_PERM_USER_READ |
  80. GNUNET_DISK_PERM_USER_WRITE |
  81. GNUNET_DISK_PERM_GROUP_READ);
  82. if (NULL == fh)
  83. {
  84. LOG (GNUNET_ERROR_TYPE_ERROR,
  85. "Opening file `%s' failed.\n",
  86. name);
  87. GNUNET_assert (0);
  88. }
  89. GNUNET_assert (GNUNET_YES ==
  90. GNUNET_CONTAINER_multihashmap_put (open_files,
  91. &hash,
  92. fh,
  93. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  94. return fh;
  95. }
  96. /**
  97. * @brief Closes the file of the current entry
  98. *
  99. * Implements #GNUNET_CONTAINER_HashMapIterator
  100. *
  101. * @param cls unused
  102. * @param key unused
  103. * @param value the file handle
  104. *
  105. * @return #GNUNET_YES if we should continue to
  106. * iterate,
  107. * #GNUNET_NO if not.
  108. */
  109. int
  110. close_files_iter (void *cls,
  111. const struct GNUNET_HashCode *key,
  112. void *value)
  113. {
  114. (void) cls;
  115. (void) key;
  116. struct GNUNET_DISK_FileHandle *fh = value;
  117. if (NULL != fh)
  118. {
  119. GNUNET_DISK_file_close (fh);
  120. }
  121. return GNUNET_YES;
  122. }
  123. /**
  124. * @brief Close all files that were opened with #get_file_handle
  125. *
  126. * @return Success of iterating over files
  127. */
  128. int
  129. close_all_files ()
  130. {
  131. int ret;
  132. ret = GNUNET_CONTAINER_multihashmap_iterate (open_files,
  133. close_files_iter,
  134. NULL);
  135. GNUNET_CONTAINER_multihashmap_destroy (open_files);
  136. open_files = NULL;
  137. return ret;
  138. }
  139. void
  140. to_file_raw (const char *file_name, const char *buf, size_t size_buf)
  141. {
  142. struct GNUNET_DISK_FileHandle *f;
  143. size_t size_written;
  144. if (NULL == (f = GNUNET_DISK_file_open (file_name,
  145. GNUNET_DISK_OPEN_APPEND |
  146. GNUNET_DISK_OPEN_WRITE |
  147. GNUNET_DISK_OPEN_CREATE,
  148. GNUNET_DISK_PERM_USER_READ |
  149. GNUNET_DISK_PERM_USER_WRITE |
  150. GNUNET_DISK_PERM_GROUP_READ |
  151. GNUNET_DISK_PERM_OTHER_READ)))
  152. {
  153. LOG (GNUNET_ERROR_TYPE_WARNING,
  154. "Not able to open file %s\n",
  155. file_name);
  156. return;
  157. }
  158. size_written = GNUNET_DISK_file_write (f, buf, size_buf);
  159. if (size_buf != size_written)
  160. {
  161. LOG (GNUNET_ERROR_TYPE_WARNING,
  162. "Unable to write to file! (Size: %u, size_written: %u)\n",
  163. size_buf,
  164. size_written);
  165. if (GNUNET_YES != GNUNET_DISK_file_close (f))
  166. LOG (GNUNET_ERROR_TYPE_WARNING,
  167. "Unable to close file\n");
  168. return;
  169. }
  170. LOG (GNUNET_ERROR_TYPE_WARNING,
  171. "Wrote %u bytes raw.\n",
  172. size_written);
  173. if (GNUNET_YES != GNUNET_DISK_file_close (f))
  174. LOG (GNUNET_ERROR_TYPE_WARNING,
  175. "Unable to close file\n");
  176. }
  177. void
  178. to_file_raw_unaligned (const char *file_name,
  179. const char *buf,
  180. size_t size_buf,
  181. unsigned bits_needed)
  182. {
  183. // TODO endianness!
  184. GNUNET_assert (size_buf >= (bits_needed/8));
  185. //if (0 == num_bits_buf_unaligned)
  186. //{
  187. // if (0 == (bits_needed % 8))
  188. // {
  189. // to_file_raw (file_name, buf, size_buf);
  190. // return;
  191. // }
  192. // to_file_raw (file_name, buf, size_buf - 1);
  193. // buf_unaligned = buf[size_buf - 1];
  194. // num_bits_buf_unaligned = bits_needed % 8;
  195. // return;
  196. //}
  197. LOG (GNUNET_ERROR_TYPE_DEBUG,
  198. "Was asked to write %u bits\n", bits_needed);
  199. char buf_write[size_buf + 1];
  200. const unsigned bytes_iter = (0 != bits_needed % 8?
  201. (bits_needed/8)+1:
  202. bits_needed/8);
  203. // TODO what if no iteration happens?
  204. unsigned size_buf_write = 0;
  205. LOG (GNUNET_ERROR_TYPE_DEBUG,
  206. "num_bits_buf_unaligned: %u\n",
  207. num_bits_buf_unaligned);
  208. LOG (GNUNET_ERROR_TYPE_DEBUG,
  209. "ua args: size_buf: %u, bits_needed: %u -> iter: %u\n",
  210. size_buf,
  211. bits_needed,
  212. bytes_iter);
  213. buf_write[0] = buf_unaligned;
  214. /* Iterate over input bytes */
  215. for (unsigned i = 0; i < bytes_iter; i++)
  216. {
  217. /* Number of bits needed in this iteration - 8 for all except last iter */
  218. unsigned num_bits_needed_iter;
  219. /* Mask for bits to actually use */
  220. unsigned mask_bits_needed_iter;
  221. char byte_input;
  222. /* Number of bits needed to align unaligned byte */
  223. unsigned num_bits_to_align;
  224. /* Number of bits that are to be moved */
  225. unsigned num_bits_to_move;
  226. /* Mask for bytes to be moved */
  227. char mask_input_to_move;
  228. /* Masked bits to be moved */
  229. char bits_to_move;
  230. /* The amount of bits needed to fit the bits to shift to the nearest spot */
  231. unsigned distance_shift_bits;
  232. /* Shifted bits on the move */
  233. char bits_moving;
  234. /* (unaligned) byte being filled with bits */
  235. char byte_to_fill;
  236. /* mask for needed bits of the input byte that have not been moved */
  237. char mask_input_leftover;
  238. /* needed bits of the input byte that have not been moved */
  239. char byte_input_leftover;
  240. unsigned num_bits_leftover;
  241. //unsigned num_bits_discard;
  242. char byte_unaligned_new;
  243. if ( (bits_needed - (i * 8)) <= 8)
  244. {
  245. /* last iteration */
  246. num_bits_needed_iter = bits_needed - (i * 8);
  247. }
  248. else
  249. {
  250. num_bits_needed_iter = 8;
  251. }
  252. LOG (GNUNET_ERROR_TYPE_DEBUG,
  253. "number of bits needed in this iteration: %u\n",
  254. num_bits_needed_iter);
  255. mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
  256. LOG (GNUNET_ERROR_TYPE_DEBUG,
  257. "mask needed bits (current iter): "B2B_PAT"\n",
  258. B2B(mask_bits_needed_iter));
  259. LOG (GNUNET_ERROR_TYPE_DEBUG,
  260. "Unaligned byte: "B2B_PAT" (%u bits)\n",
  261. B2B(buf_unaligned),
  262. num_bits_buf_unaligned);
  263. byte_input = buf[i];
  264. LOG (GNUNET_ERROR_TYPE_DEBUG,
  265. "next whole input byte: "B2B_PAT"\n",
  266. B2B(byte_input));
  267. byte_input &= mask_bits_needed_iter;
  268. num_bits_to_align = 8 - num_bits_buf_unaligned;
  269. LOG (GNUNET_ERROR_TYPE_DEBUG,
  270. "input byte, needed bits: "B2B_PAT"\n",
  271. B2B(byte_input));
  272. LOG (GNUNET_ERROR_TYPE_DEBUG,
  273. "number of bits needed to align unaligned bit: %u\n",
  274. num_bits_to_align);
  275. num_bits_to_move = GNUNET_MIN (num_bits_to_align, num_bits_needed_iter);
  276. LOG (GNUNET_ERROR_TYPE_DEBUG,
  277. "number of bits of new byte to move: %u\n",
  278. num_bits_to_move);
  279. mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
  280. LOG (GNUNET_ERROR_TYPE_DEBUG,
  281. "mask of bits of new byte to take for moving: "B2B_PAT"\n",
  282. B2B(mask_input_to_move));
  283. bits_to_move = byte_input & mask_input_to_move;
  284. LOG (GNUNET_ERROR_TYPE_DEBUG,
  285. "masked bits of new byte to take for moving: "B2B_PAT"\n",
  286. B2B(bits_to_move));
  287. distance_shift_bits = num_bits_buf_unaligned;
  288. LOG (GNUNET_ERROR_TYPE_DEBUG,
  289. "distance needed to shift bits to their correct spot: %u\n",
  290. distance_shift_bits);
  291. bits_moving = bits_to_move << distance_shift_bits;
  292. LOG (GNUNET_ERROR_TYPE_DEBUG,
  293. "shifted, masked bits of new byte being moved: "B2B_PAT"\n",
  294. B2B(bits_moving));
  295. byte_to_fill = buf_unaligned | bits_moving;
  296. LOG (GNUNET_ERROR_TYPE_DEBUG,
  297. "byte being filled: "B2B_PAT"\n",
  298. B2B(byte_to_fill));
  299. LOG (GNUNET_ERROR_TYPE_DEBUG,
  300. "pending bytes: %u\n",
  301. num_bits_buf_unaligned + num_bits_needed_iter);
  302. if (num_bits_buf_unaligned + num_bits_needed_iter >= 8)
  303. {
  304. /* buf_unaligned was aligned by filling
  305. * -> can be written to storage */
  306. buf_write[i] = byte_to_fill;
  307. size_buf_write++;
  308. /* store the leftover, unaligned bits in buffer */
  309. mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
  310. LOG (GNUNET_ERROR_TYPE_DEBUG,
  311. "mask of leftover bits of new byte: "B2B_PAT"\n",
  312. B2B(mask_input_leftover));
  313. byte_input_leftover = byte_input & mask_input_leftover;
  314. LOG (GNUNET_ERROR_TYPE_DEBUG,
  315. "masked, leftover bits of new byte: "B2B_PAT"\n",
  316. B2B(byte_input_leftover));
  317. num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
  318. LOG (GNUNET_ERROR_TYPE_DEBUG,
  319. "number of unaligned bits left: %u\n",
  320. num_bits_leftover);
  321. //num_bits_discard = 8 - num_bits_needed_iter;
  322. byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
  323. LOG (GNUNET_ERROR_TYPE_DEBUG,
  324. "new unaligned byte: "B2B_PAT"\n",
  325. B2B(byte_unaligned_new));
  326. buf_unaligned = byte_unaligned_new;
  327. num_bits_buf_unaligned = num_bits_leftover % 8;
  328. }
  329. else
  330. {
  331. /* unaligned buffer still unaligned but 'fuller' */
  332. buf_unaligned = byte_to_fill;
  333. num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
  334. }
  335. }
  336. to_file_raw (file_name, buf_write, size_buf_write);
  337. LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
  338. }
  339. char *
  340. auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
  341. {
  342. int size;
  343. size_t name_buf_size;
  344. char *end;
  345. char *buf;
  346. char *name_buf;
  347. size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
  348. name_buf_size = 512 * sizeof (char);
  349. name_buf = GNUNET_malloc (name_buf_size);
  350. if (keylen % 5 > 0)
  351. keylen += 5 - keylen % 5;
  352. keylen /= 5;
  353. buf = GNUNET_malloc (keylen + 1);
  354. end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
  355. sizeof (struct GNUNET_CRYPTO_AuthKey),
  356. buf,
  357. keylen);
  358. if (NULL == end)
  359. {
  360. GNUNET_free (buf);
  361. GNUNET_break (0);
  362. }
  363. else
  364. {
  365. *end = '\0';
  366. }
  367. size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
  368. if (0 > size)
  369. LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
  370. GNUNET_free (buf);
  371. return name_buf;
  372. }
  373. #endif /* TO_FILE */
  374. struct GNUNET_CRYPTO_AuthKey
  375. string_to_auth_key (const char *str)
  376. {
  377. struct GNUNET_CRYPTO_AuthKey auth_key;
  378. if (GNUNET_OK !=
  379. GNUNET_STRINGS_string_to_data (str,
  380. strlen (str),
  381. &auth_key.key,
  382. sizeof (struct GNUNET_CRYPTO_AuthKey)))
  383. {
  384. LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
  385. }
  386. return auth_key;
  387. }
  388. /**
  389. * @brief Try to ensure that `/tmp/rps` exists.
  390. *
  391. * @return #GNUNET_YES on success
  392. * #GNUNET_SYSERR on failure
  393. */
  394. static int
  395. ensure_folder_exist (void)
  396. {
  397. if (GNUNET_OK !=
  398. GNUNET_DISK_directory_create ("/tmp/rps"))
  399. {
  400. LOG (GNUNET_ERROR_TYPE_ERROR,
  401. "Could not create directory `/tmp/rps'\n");
  402. return GNUNET_SYSERR;
  403. }
  404. return GNUNET_YES;
  405. }
  406. char *
  407. store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
  408. const char *prefix)
  409. {
  410. int len_file_name;
  411. int out_size;
  412. char *file_name;
  413. const char *pid_long;
  414. if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
  415. pid_long = GNUNET_i2s_full (peer);
  416. len_file_name = (strlen (prefix) +
  417. strlen (pid_long) +
  418. 11)
  419. * sizeof (char);
  420. file_name = GNUNET_malloc (len_file_name);
  421. out_size = GNUNET_snprintf (file_name,
  422. len_file_name,
  423. "/tmp/rps/%s-%s",
  424. prefix,
  425. pid_long);
  426. if (len_file_name < out_size ||
  427. 0 > out_size)
  428. {
  429. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  430. "Failed to write string to buffer (size: %i, out_size: %i)\n",
  431. len_file_name,
  432. out_size);
  433. }
  434. return file_name;
  435. }
  436. /**
  437. * @brief Factorial
  438. *
  439. * @param x Number of which to compute the factorial
  440. *
  441. * @return Factorial of @a x
  442. */
  443. uint32_t fac (uint32_t x)
  444. {
  445. if (1 >= x)
  446. {
  447. return x;
  448. }
  449. return x * fac (x - 1);
  450. }
  451. /**
  452. * @brief Binomial coefficient (n choose k)
  453. *
  454. * @param n
  455. * @param k
  456. *
  457. * @return Binomial coefficient of @a n and @a k
  458. */
  459. uint32_t binom (uint32_t n, uint32_t k)
  460. {
  461. //GNUNET_assert (n >= k);
  462. if (k > n) return 0;
  463. /* if (0 > n) return 0; - always false */
  464. /* if (0 > k) return 0; - always false */
  465. if (0 == k) return 1;
  466. return fac (n)
  467. /
  468. fac(k) * fac(n - k);
  469. }
  470. /* end of gnunet-service-rps.c */