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__)
40 #define min(x,y) ((x) > (y) ? (y) : (x))
43 * @brief buffer for storing the unaligned bits for the next write
45 static char buf_unaligned;
48 * @brief number of bits in unaligned buffer
50 static unsigned num_bits_buf_unaligned;
53 to_file_ (const char *file_name, char *line)
55 struct GNUNET_DISK_FileHandle *f;
56 char output_buffer[512];
57 size_t output_buffer_size = 512;
58 char *output_buffer_p;
64 if (NULL == (f = GNUNET_DISK_file_open (file_name,
65 GNUNET_DISK_OPEN_APPEND |
66 GNUNET_DISK_OPEN_WRITE |
67 GNUNET_DISK_OPEN_CREATE,
68 GNUNET_DISK_PERM_USER_READ |
69 GNUNET_DISK_PERM_USER_WRITE |
70 GNUNET_DISK_PERM_GROUP_READ |
71 GNUNET_DISK_PERM_OTHER_READ)))
73 LOG (GNUNET_ERROR_TYPE_WARNING,
74 "Not able to open file %s\n",
78 output_buffer_size = strlen (line) + 18;
79 if (512 < output_buffer_size)
81 output_buffer_p = GNUNET_malloc ((output_buffer_size) * sizeof (char));
83 output_buffer_p = &output_buffer[0];
85 size = GNUNET_snprintf (output_buffer_p,
88 (GNUNET_TIME_absolute_get ().abs_value_us) / 1000000, // microsec -> sec
92 LOG (GNUNET_ERROR_TYPE_WARNING,
93 "Failed to write string to buffer (size: %i)\n",
98 size2 = GNUNET_DISK_file_write (f, output_buffer_p, size);
101 LOG (GNUNET_ERROR_TYPE_WARNING,
102 "Unable to write to file! (Size: %u, size2: %u)\n",
106 if (GNUNET_YES != GNUNET_DISK_file_close (f))
107 LOG (GNUNET_ERROR_TYPE_WARNING,
108 "Unable to close file\n");
113 if (512 < output_buffer_size)
115 GNUNET_free (output_buffer_p);
118 if (GNUNET_YES != GNUNET_DISK_file_close (f))
119 LOG (GNUNET_ERROR_TYPE_WARNING,
120 "Unable to close file\n");
124 to_file_raw (const char *file_name, const char *buf, size_t size_buf)
126 struct GNUNET_DISK_FileHandle *f;
129 if (NULL == (f = GNUNET_DISK_file_open (file_name,
130 GNUNET_DISK_OPEN_APPEND |
131 GNUNET_DISK_OPEN_WRITE |
132 GNUNET_DISK_OPEN_CREATE,
133 GNUNET_DISK_PERM_USER_READ |
134 GNUNET_DISK_PERM_USER_WRITE |
135 GNUNET_DISK_PERM_GROUP_READ |
136 GNUNET_DISK_PERM_OTHER_READ)))
138 LOG (GNUNET_ERROR_TYPE_WARNING,
139 "Not able to open file %s\n",
144 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
145 if (size_buf != size_written)
147 LOG (GNUNET_ERROR_TYPE_WARNING,
148 "Unable to write to file! (Size: %u, size_written: %u)\n",
152 if (GNUNET_YES != GNUNET_DISK_file_close (f))
153 LOG (GNUNET_ERROR_TYPE_WARNING,
154 "Unable to close file\n");
161 to_file_raw_unaligned (const char *file_name,
164 unsigned bits_needed)
167 GNUNET_assert (size_buf >= (bits_needed/8));
168 //if (0 == num_bits_buf_unaligned)
170 // if (0 == (bits_needed % 8))
172 // to_file_raw (file_name, buf, size_buf);
175 // to_file_raw (file_name, buf, size_buf - 1);
176 // buf_unaligned = buf[size_buf - 1];
177 // num_bits_buf_unaligned = bits_needed % 8;
181 char buf_write[size_buf + 1];
182 const unsigned bytes_iter = (0 != bits_needed % 8?
185 // TODO what if no iteration happens?
186 unsigned size_buf_write = 0;
187 buf_write[0] = buf_unaligned;
188 /* Iterate over input bytes */
189 for (unsigned i = 0; i < bytes_iter; i++)
191 /* Number of bits needed in this iteration - 8 for all except last iter */
192 unsigned num_bits_needed_iter;
193 /* Mask for bits to actually use */
194 unsigned mask_bits_needed_iter;
196 /* Number of bits needed to align unaligned byte */
197 unsigned num_bits_to_align;
198 /* Number of bits that are to be moved */
199 unsigned num_bits_to_move;
200 /* Mask for bytes to be moved */
201 char mask_input_to_move;
202 /* Masked bits to be moved */
204 /* The amount of bits needed to fit the bits to shift to the nearest spot */
205 unsigned distance_shift_bits;
206 /* Shifted bits on the move */
208 /* (unaligned) byte being filled with bits */
210 /* mask for needed bits of the input byte that have not been moved */
211 char mask_input_leftover;
212 /* needed bits of the input byte that have not been moved */
213 char byte_input_leftover;
214 unsigned num_bits_leftover;
215 unsigned num_bits_discard;
216 char byte_unaligned_new;
218 if ( (bits_needed - (i * 8)) <= 8)
221 num_bits_needed_iter = bits_needed - (i * 8);
225 num_bits_needed_iter = 8;
227 mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
229 byte_input &= mask_bits_needed_iter;
230 num_bits_to_align = 8 - num_bits_buf_unaligned;
231 num_bits_to_move = min (num_bits_to_align, num_bits_needed_iter);
232 mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
233 bits_to_move = byte_input & mask_input_to_move;
234 distance_shift_bits = num_bits_buf_unaligned;
235 bits_moving = bits_to_move << distance_shift_bits;
236 byte_to_fill = buf_unaligned | bits_moving;
237 if (num_bits_buf_unaligned + num_bits_needed_iter > 8)
239 /* buf_unaligned was aligned by filling
240 * -> can be written to storage */
241 buf_write[i] = byte_to_fill;
244 /* store the leftover, unaligned bits in buffer */
245 mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
246 byte_input_leftover = byte_input & mask_input_leftover;
247 num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
248 num_bits_discard = 8 - num_bits_needed_iter;
249 byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
250 buf_unaligned = byte_unaligned_new;
251 num_bits_buf_unaligned = num_bits_leftover % 8;
255 /* unaligned buffer still unaligned but 'fuller' */
256 buf_unaligned = byte_to_fill;
257 num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
260 to_file_raw (file_name, buf_write, size_buf_write);
261 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
265 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
268 size_t name_buf_size;
272 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
274 name_buf_size = 512 * sizeof (char);
275 name_buf = GNUNET_malloc (name_buf_size);
278 keylen += 5 - keylen % 5;
280 buf = GNUNET_malloc (keylen + 1);
282 end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
283 sizeof (struct GNUNET_CRYPTO_AuthKey),
297 size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
299 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
307 struct GNUNET_CRYPTO_AuthKey
308 string_to_auth_key (const char *str)
310 struct GNUNET_CRYPTO_AuthKey auth_key;
313 GNUNET_STRINGS_string_to_data (str,
316 sizeof (struct GNUNET_CRYPTO_AuthKey)))
318 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
326 create_file (const char *name)
329 size_t name_buf_size;
334 prefix = "/tmp/rps/";
335 name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
336 name_buf = GNUNET_malloc (name_buf_size);
338 size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
340 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
342 if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
344 LOG (GNUNET_ERROR_TYPE_WARNING,
345 "Could not create directory %s.\n",
349 if (NULL == strstr (name, "sampler_el"))
350 {/* only append random string to sampler */
351 if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
352 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
354 GNUNET_free (name_buf);
364 * @brief Try to ensure that `/tmp/rps` exists.
366 * @return #GNUNET_YES on success
367 * #GNUNET_SYSERR on failure
369 static int ensure_folder_exist (void)
371 if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
373 GNUNET_DISK_directory_create ("/tmp/rps");
375 if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
377 return GNUNET_SYSERR;
383 store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
386 unsigned int len_file_name;
387 unsigned int out_size;
389 const char *pid_long;
391 if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
392 pid_long = GNUNET_i2s_full (peer);
393 len_file_name = (strlen (prefix) +
397 file_name = GNUNET_malloc (len_file_name);
398 out_size = GNUNET_snprintf (file_name,
403 if (len_file_name < out_size ||
406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
407 "Failed to write string to buffer (size: %i, out_size: %i)\n",
414 /* end of gnunet-service-rps.c */