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"
29 #include "rps-test_util.h"
33 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
35 #define B2B_PAT "%c%c%c%c%c%c%c%c"
37 (byte & 0x80 ? '1' : '0'), \
38 (byte & 0x40 ? '1' : '0'), \
39 (byte & 0x20 ? '1' : '0'), \
40 (byte & 0x10 ? '1' : '0'), \
41 (byte & 0x08 ? '1' : '0'), \
42 (byte & 0x04 ? '1' : '0'), \
43 (byte & 0x02 ? '1' : '0'), \
44 (byte & 0x01 ? '1' : '0')
50 * @brief buffer for storing the unaligned bits for the next write
52 static char buf_unaligned;
55 * @brief number of bits in unaligned buffer
57 static unsigned num_bits_buf_unaligned;
61 to_file_raw (const char *file_name, const char *buf, size_t size_buf)
63 struct GNUNET_DISK_FileHandle *f;
66 if (NULL == (f = GNUNET_DISK_file_open (file_name,
67 GNUNET_DISK_OPEN_APPEND |
68 GNUNET_DISK_OPEN_WRITE |
69 GNUNET_DISK_OPEN_CREATE,
70 GNUNET_DISK_PERM_USER_READ |
71 GNUNET_DISK_PERM_USER_WRITE |
72 GNUNET_DISK_PERM_GROUP_READ |
73 GNUNET_DISK_PERM_OTHER_READ)))
75 LOG (GNUNET_ERROR_TYPE_WARNING,
76 "Not able to open file %s\n",
81 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
82 if (size_buf != size_written)
84 LOG (GNUNET_ERROR_TYPE_WARNING,
85 "Unable to write to file! (Size: %u, size_written: %u)\n",
89 if (GNUNET_YES != GNUNET_DISK_file_close (f))
90 LOG (GNUNET_ERROR_TYPE_WARNING,
91 "Unable to close file\n");
95 LOG (GNUNET_ERROR_TYPE_WARNING,
96 "Wrote %u bytes raw.\n",
98 if (GNUNET_YES != GNUNET_DISK_file_close (f))
99 LOG (GNUNET_ERROR_TYPE_WARNING,
100 "Unable to close file\n");
104 to_file_raw_unaligned (const char *file_name,
107 unsigned bits_needed)
110 GNUNET_assert (size_buf >= (bits_needed/8));
111 //if (0 == num_bits_buf_unaligned)
113 // if (0 == (bits_needed % 8))
115 // to_file_raw (file_name, buf, size_buf);
118 // to_file_raw (file_name, buf, size_buf - 1);
119 // buf_unaligned = buf[size_buf - 1];
120 // num_bits_buf_unaligned = bits_needed % 8;
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Was asked to write %u bits\n", bits_needed);
126 char buf_write[size_buf + 1];
127 const unsigned bytes_iter = (0 != bits_needed % 8?
130 // TODO what if no iteration happens?
131 unsigned size_buf_write = 0;
132 LOG (GNUNET_ERROR_TYPE_DEBUG,
133 "num_bits_buf_unaligned: %u\n",
134 num_bits_buf_unaligned);
135 LOG (GNUNET_ERROR_TYPE_DEBUG,
136 "ua args: size_buf: %u, bits_needed: %u -> iter: %u\n",
140 buf_write[0] = buf_unaligned;
141 /* Iterate over input bytes */
142 for (unsigned i = 0; i < bytes_iter; i++)
144 /* Number of bits needed in this iteration - 8 for all except last iter */
145 unsigned num_bits_needed_iter;
146 /* Mask for bits to actually use */
147 unsigned mask_bits_needed_iter;
149 /* Number of bits needed to align unaligned byte */
150 unsigned num_bits_to_align;
151 /* Number of bits that are to be moved */
152 unsigned num_bits_to_move;
153 /* Mask for bytes to be moved */
154 char mask_input_to_move;
155 /* Masked bits to be moved */
157 /* The amount of bits needed to fit the bits to shift to the nearest spot */
158 unsigned distance_shift_bits;
159 /* Shifted bits on the move */
161 /* (unaligned) byte being filled with bits */
163 /* mask for needed bits of the input byte that have not been moved */
164 char mask_input_leftover;
165 /* needed bits of the input byte that have not been moved */
166 char byte_input_leftover;
167 unsigned num_bits_leftover;
168 //unsigned num_bits_discard;
169 char byte_unaligned_new;
171 if ( (bits_needed - (i * 8)) <= 8)
174 num_bits_needed_iter = bits_needed - (i * 8);
178 num_bits_needed_iter = 8;
180 LOG (GNUNET_ERROR_TYPE_DEBUG,
181 "number of bits needed in this iteration: %u\n",
182 num_bits_needed_iter);
183 mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
184 LOG (GNUNET_ERROR_TYPE_DEBUG,
185 "mask needed bits (current iter): "B2B_PAT"\n",
186 B2B(mask_bits_needed_iter));
187 LOG (GNUNET_ERROR_TYPE_DEBUG,
188 "Unaligned byte: "B2B_PAT" (%u bits)\n",
190 num_bits_buf_unaligned);
192 LOG (GNUNET_ERROR_TYPE_DEBUG,
193 "next whole input byte: "B2B_PAT"\n",
195 byte_input &= mask_bits_needed_iter;
196 num_bits_to_align = 8 - num_bits_buf_unaligned;
197 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "input byte, needed bits: "B2B_PAT"\n",
200 LOG (GNUNET_ERROR_TYPE_DEBUG,
201 "number of bits needed to align unaligned bit: %u\n",
203 num_bits_to_move = GNUNET_MIN (num_bits_to_align, num_bits_needed_iter);
204 LOG (GNUNET_ERROR_TYPE_DEBUG,
205 "number of bits of new byte to move: %u\n",
207 mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "mask of bits of new byte to take for moving: "B2B_PAT"\n",
210 B2B(mask_input_to_move));
211 bits_to_move = byte_input & mask_input_to_move;
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "masked bits of new byte to take for moving: "B2B_PAT"\n",
215 distance_shift_bits = num_bits_buf_unaligned;
216 LOG (GNUNET_ERROR_TYPE_DEBUG,
217 "distance needed to shift bits to their correct spot: %u\n",
218 distance_shift_bits);
219 bits_moving = bits_to_move << distance_shift_bits;
220 LOG (GNUNET_ERROR_TYPE_DEBUG,
221 "shifted, masked bits of new byte being moved: "B2B_PAT"\n",
223 byte_to_fill = buf_unaligned | bits_moving;
224 LOG (GNUNET_ERROR_TYPE_DEBUG,
225 "byte being filled: "B2B_PAT"\n",
227 LOG (GNUNET_ERROR_TYPE_DEBUG,
228 "pending bytes: %u\n",
229 num_bits_buf_unaligned + num_bits_needed_iter);
230 if (num_bits_buf_unaligned + num_bits_needed_iter >= 8)
232 /* buf_unaligned was aligned by filling
233 * -> can be written to storage */
234 buf_write[i] = byte_to_fill;
237 /* store the leftover, unaligned bits in buffer */
238 mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "mask of leftover bits of new byte: "B2B_PAT"\n",
241 B2B(mask_input_leftover));
242 byte_input_leftover = byte_input & mask_input_leftover;
243 LOG (GNUNET_ERROR_TYPE_DEBUG,
244 "masked, leftover bits of new byte: "B2B_PAT"\n",
245 B2B(byte_input_leftover));
246 num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
247 LOG (GNUNET_ERROR_TYPE_DEBUG,
248 "number of unaligned bits left: %u\n",
250 //num_bits_discard = 8 - num_bits_needed_iter;
251 byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "new unaligned byte: "B2B_PAT"\n",
254 B2B(byte_unaligned_new));
255 buf_unaligned = byte_unaligned_new;
256 num_bits_buf_unaligned = num_bits_leftover % 8;
260 /* unaligned buffer still unaligned but 'fuller' */
261 buf_unaligned = byte_to_fill;
262 num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
265 to_file_raw (file_name, buf_write, size_buf_write);
266 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
270 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
273 size_t name_buf_size;
277 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
279 name_buf_size = 512 * sizeof (char);
280 name_buf = GNUNET_malloc (name_buf_size);
283 keylen += 5 - keylen % 5;
285 buf = GNUNET_malloc (keylen + 1);
287 end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
288 sizeof (struct GNUNET_CRYPTO_AuthKey),
302 size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
304 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
314 create_file (const char *name)
317 size_t name_buf_size;
322 prefix = "/tmp/rps/";
323 name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
324 name_buf = GNUNET_malloc (name_buf_size);
326 size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
328 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
330 if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
332 LOG (GNUNET_ERROR_TYPE_WARNING,
333 "Could not create directory %s.\n",
337 if (NULL == strstr (name, "sampler_el"))
338 {/* only append random string to sampler */
339 if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
340 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
342 GNUNET_free (name_buf);
352 struct GNUNET_CRYPTO_AuthKey
353 string_to_auth_key (const char *str)
355 struct GNUNET_CRYPTO_AuthKey auth_key;
358 GNUNET_STRINGS_string_to_data (str,
361 sizeof (struct GNUNET_CRYPTO_AuthKey)))
363 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
371 * @brief Try to ensure that `/tmp/rps` exists.
373 * @return #GNUNET_YES on success
374 * #GNUNET_SYSERR on failure
376 static int ensure_folder_exist (void)
378 if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
380 GNUNET_DISK_directory_create ("/tmp/rps");
382 if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
384 return GNUNET_SYSERR;
390 store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
396 const char *pid_long;
398 if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
399 pid_long = GNUNET_i2s_full (peer);
400 len_file_name = (strlen (prefix) +
404 file_name = GNUNET_malloc (len_file_name);
405 out_size = GNUNET_snprintf (file_name,
410 if (len_file_name < out_size ||
413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
414 "Failed to write string to buffer (size: %i, out_size: %i)\n",
421 /* end of gnunet-service-rps.c */