2 This file is part of GNUnet.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file rps/rps-test_util.c
21 * @brief Some utils faciliating the view into the internals for the sampler
22 * needed for evaluation
24 * @author Julius Bünger
28 #include "gnunet_util_lib.h"
32 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
34 #define B2B_PAT "%c%c%c%c%c%c%c%c"
36 (byte & 0x80 ? '1' : '0'), \
37 (byte & 0x40 ? '1' : '0'), \
38 (byte & 0x20 ? '1' : '0'), \
39 (byte & 0x10 ? '1' : '0'), \
40 (byte & 0x08 ? '1' : '0'), \
41 (byte & 0x04 ? '1' : '0'), \
42 (byte & 0x02 ? '1' : '0'), \
43 (byte & 0x01 ? '1' : '0')
51 #define min(x,y) ((x) > (y) ? (y) : (x))
54 * @brief buffer for storing the unaligned bits for the next write
56 static char buf_unaligned;
59 * @brief number of bits in unaligned buffer
61 static unsigned num_bits_buf_unaligned;
64 to_file_ (const char *file_name, char *line)
66 struct GNUNET_DISK_FileHandle *f;
67 char output_buffer[512];
68 size_t output_buffer_size = 512;
69 char *output_buffer_p;
75 if (NULL == (f = GNUNET_DISK_file_open (file_name,
76 GNUNET_DISK_OPEN_APPEND |
77 GNUNET_DISK_OPEN_WRITE |
78 GNUNET_DISK_OPEN_CREATE,
79 GNUNET_DISK_PERM_USER_READ |
80 GNUNET_DISK_PERM_USER_WRITE |
81 GNUNET_DISK_PERM_GROUP_READ |
82 GNUNET_DISK_PERM_OTHER_READ)))
84 LOG (GNUNET_ERROR_TYPE_WARNING,
85 "Not able to open file %s\n",
89 output_buffer_size = strlen (line) + 18;
90 if (512 < output_buffer_size)
92 output_buffer_p = GNUNET_malloc ((output_buffer_size) * sizeof (char));
94 output_buffer_p = &output_buffer[0];
96 size = GNUNET_snprintf (output_buffer_p,
99 (GNUNET_TIME_absolute_get ().abs_value_us) / 1000000, // microsec -> sec
103 LOG (GNUNET_ERROR_TYPE_WARNING,
104 "Failed to write string to buffer (size: %i)\n",
109 size2 = GNUNET_DISK_file_write (f, output_buffer_p, size);
112 LOG (GNUNET_ERROR_TYPE_WARNING,
113 "Unable to write to file! (Size: %u, size2: %u)\n",
117 if (GNUNET_YES != GNUNET_DISK_file_close (f))
118 LOG (GNUNET_ERROR_TYPE_WARNING,
119 "Unable to close file\n");
124 if (512 < output_buffer_size)
126 GNUNET_free (output_buffer_p);
129 if (GNUNET_YES != GNUNET_DISK_file_close (f))
130 LOG (GNUNET_ERROR_TYPE_WARNING,
131 "Unable to close file\n");
135 to_file_raw (const char *file_name, const char *buf, size_t size_buf)
137 struct GNUNET_DISK_FileHandle *f;
140 if (NULL == (f = GNUNET_DISK_file_open (file_name,
141 GNUNET_DISK_OPEN_APPEND |
142 GNUNET_DISK_OPEN_WRITE |
143 GNUNET_DISK_OPEN_CREATE,
144 GNUNET_DISK_PERM_USER_READ |
145 GNUNET_DISK_PERM_USER_WRITE |
146 GNUNET_DISK_PERM_GROUP_READ |
147 GNUNET_DISK_PERM_OTHER_READ)))
149 LOG (GNUNET_ERROR_TYPE_WARNING,
150 "Not able to open file %s\n",
155 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
156 if (size_buf != size_written)
158 LOG (GNUNET_ERROR_TYPE_WARNING,
159 "Unable to write to file! (Size: %u, size_written: %u)\n",
163 if (GNUNET_YES != GNUNET_DISK_file_close (f))
164 LOG (GNUNET_ERROR_TYPE_WARNING,
165 "Unable to close file\n");
169 LOG (GNUNET_ERROR_TYPE_WARNING,
170 "Wrote %u bytes raw.\n",
172 if (GNUNET_YES != GNUNET_DISK_file_close (f))
173 LOG (GNUNET_ERROR_TYPE_WARNING,
174 "Unable to close file\n");
178 to_file_raw_unaligned (const char *file_name,
181 unsigned bits_needed)
184 GNUNET_assert (size_buf >= (bits_needed/8));
185 //if (0 == num_bits_buf_unaligned)
187 // if (0 == (bits_needed % 8))
189 // to_file_raw (file_name, buf, size_buf);
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;
197 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "Was asked to write %u bits\n", bits_needed);
200 char buf_write[size_buf + 1];
201 const unsigned bytes_iter = (0 != bits_needed % 8?
204 // TODO what if no iteration happens?
205 unsigned size_buf_write = 0;
206 LOG (GNUNET_ERROR_TYPE_DEBUG,
207 "num_bits_buf_unaligned: %u\n",
208 num_bits_buf_unaligned);
209 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "ua args: size_buf: %u, bits_needed: %u -> iter: %u\n",
214 buf_write[0] = buf_unaligned;
215 /* Iterate over input bytes */
216 for (unsigned i = 0; i < bytes_iter; i++)
218 /* Number of bits needed in this iteration - 8 for all except last iter */
219 unsigned num_bits_needed_iter;
220 /* Mask for bits to actually use */
221 unsigned mask_bits_needed_iter;
223 /* Number of bits needed to align unaligned byte */
224 unsigned num_bits_to_align;
225 /* Number of bits that are to be moved */
226 unsigned num_bits_to_move;
227 /* Mask for bytes to be moved */
228 char mask_input_to_move;
229 /* Masked bits to be moved */
231 /* The amount of bits needed to fit the bits to shift to the nearest spot */
232 unsigned distance_shift_bits;
233 /* Shifted bits on the move */
235 /* (unaligned) byte being filled with bits */
237 /* mask for needed bits of the input byte that have not been moved */
238 char mask_input_leftover;
239 /* needed bits of the input byte that have not been moved */
240 char byte_input_leftover;
241 unsigned num_bits_leftover;
242 unsigned num_bits_discard;
243 char byte_unaligned_new;
245 if ( (bits_needed - (i * 8)) <= 8)
248 num_bits_needed_iter = bits_needed - (i * 8);
252 num_bits_needed_iter = 8;
254 LOG (GNUNET_ERROR_TYPE_DEBUG,
255 "number of bits needed in this iteration: %u\n",
256 num_bits_needed_iter);
257 mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "mask needed bits (current iter): "B2B_PAT"\n",
260 B2B(mask_bits_needed_iter));
261 LOG (GNUNET_ERROR_TYPE_DEBUG,
262 "Unaligned byte: "B2B_PAT" (%u bits)\n",
264 num_bits_buf_unaligned);
266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "next whole input byte: "B2B_PAT"\n",
269 byte_input &= mask_bits_needed_iter;
270 num_bits_to_align = 8 - num_bits_buf_unaligned;
271 LOG (GNUNET_ERROR_TYPE_DEBUG,
272 "input byte, needed bits: "B2B_PAT"\n",
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "number of bits needed to align unaligned bit: %u\n",
277 num_bits_to_move = min (num_bits_to_align, num_bits_needed_iter);
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "number of bits of new byte to move: %u\n",
281 mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
282 LOG (GNUNET_ERROR_TYPE_DEBUG,
283 "mask of bits of new byte to take for moving: "B2B_PAT"\n",
284 B2B(mask_input_to_move));
285 bits_to_move = byte_input & mask_input_to_move;
286 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "masked bits of new byte to take for moving: "B2B_PAT"\n",
289 distance_shift_bits = num_bits_buf_unaligned;
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "distance needed to shift bits to their correct spot: %u\n",
292 distance_shift_bits);
293 bits_moving = bits_to_move << distance_shift_bits;
294 LOG (GNUNET_ERROR_TYPE_DEBUG,
295 "shifted, masked bits of new byte being moved: "B2B_PAT"\n",
297 byte_to_fill = buf_unaligned | bits_moving;
298 LOG (GNUNET_ERROR_TYPE_DEBUG,
299 "byte being filled: "B2B_PAT"\n",
301 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "pending bytes: %u\n",
303 num_bits_buf_unaligned + num_bits_needed_iter);
304 if (num_bits_buf_unaligned + num_bits_needed_iter >= 8)
306 /* buf_unaligned was aligned by filling
307 * -> can be written to storage */
308 buf_write[i] = byte_to_fill;
311 /* store the leftover, unaligned bits in buffer */
312 mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "mask of leftover bits of new byte: "B2B_PAT"\n",
315 B2B(mask_input_leftover));
316 byte_input_leftover = byte_input & mask_input_leftover;
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "masked, leftover bits of new byte: "B2B_PAT"\n",
319 B2B(byte_input_leftover));
320 num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "number of unaligned bits left: %u\n",
324 //num_bits_discard = 8 - num_bits_needed_iter;
325 byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "new unaligned byte: "B2B_PAT"\n",
328 B2B(byte_unaligned_new));
329 buf_unaligned = byte_unaligned_new;
330 num_bits_buf_unaligned = num_bits_leftover % 8;
334 /* unaligned buffer still unaligned but 'fuller' */
335 buf_unaligned = byte_to_fill;
336 num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
339 to_file_raw (file_name, buf_write, size_buf_write);
340 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
344 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
347 size_t name_buf_size;
351 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
353 name_buf_size = 512 * sizeof (char);
354 name_buf = GNUNET_malloc (name_buf_size);
357 keylen += 5 - keylen % 5;
359 buf = GNUNET_malloc (keylen + 1);
361 end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
362 sizeof (struct GNUNET_CRYPTO_AuthKey),
376 size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
378 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
386 struct GNUNET_CRYPTO_AuthKey
387 string_to_auth_key (const char *str)
389 struct GNUNET_CRYPTO_AuthKey auth_key;
392 GNUNET_STRINGS_string_to_data (str,
395 sizeof (struct GNUNET_CRYPTO_AuthKey)))
397 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
405 create_file (const char *name)
408 size_t name_buf_size;
413 prefix = "/tmp/rps/";
414 name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
415 name_buf = GNUNET_malloc (name_buf_size);
417 size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
419 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
421 if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
423 LOG (GNUNET_ERROR_TYPE_WARNING,
424 "Could not create directory %s.\n",
428 if (NULL == strstr (name, "sampler_el"))
429 {/* only append random string to sampler */
430 if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
431 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
433 GNUNET_free (name_buf);
443 * @brief Try to ensure that `/tmp/rps` exists.
445 * @return #GNUNET_YES on success
446 * #GNUNET_SYSERR on failure
448 static int ensure_folder_exist (void)
450 if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
452 GNUNET_DISK_directory_create ("/tmp/rps");
454 if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
456 return GNUNET_SYSERR;
462 store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
465 unsigned int len_file_name;
468 const char *pid_long;
470 if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
471 pid_long = GNUNET_i2s_full (peer);
472 len_file_name = (strlen (prefix) +
476 file_name = GNUNET_malloc (len_file_name);
477 out_size = GNUNET_snprintf (file_name,
482 if (len_file_name < out_size ||
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 "Failed to write string to buffer (size: %i, out_size: %i)\n",
493 /* end of gnunet-service-rps.c */