aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-test_util.c
blob: 9a1dfe0d838a5002efada5a548d34cd4a9458e3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
     This file is part of GNUnet.
     Copyright (C)

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file rps/rps-test_util.c
 * @brief Some utils faciliating the view into the internals for the sampler
 *        needed for evaluation
 *
 * @author Julius Bünger
 */

#include "platform.h"
#include "gnunet_util_lib.h"

#include <inttypes.h>

#define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)

#ifndef TO_FILE
#define TO_FILE
#endif /* TO_FILE */

#ifdef TO_FILE

#define min(x,y) ((x) > (y) ? (y) : (x))

/**
 * @brief buffer for storing the unaligned bits for the next write
 */
static char buf_unaligned;

/**
 * @brief number of bits in unaligned buffer
 */
static unsigned num_bits_buf_unaligned;

void
to_file_ (const char *file_name, char *line)
{
  struct GNUNET_DISK_FileHandle *f;
  char output_buffer[512];
  size_t output_buffer_size = 512;
  char *output_buffer_p;
  //size_t size;
  int size;
  size_t size2;


  if (NULL == (f = GNUNET_DISK_file_open (file_name,
                                          GNUNET_DISK_OPEN_APPEND |
                                          GNUNET_DISK_OPEN_WRITE |
                                          GNUNET_DISK_OPEN_CREATE,
                                          GNUNET_DISK_PERM_USER_READ |
                                          GNUNET_DISK_PERM_USER_WRITE |
                                          GNUNET_DISK_PERM_GROUP_READ |
                                          GNUNET_DISK_PERM_OTHER_READ)))
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Not able to open file %s\n",
         file_name);
    return;
  }
  output_buffer_size = strlen (line) + 18;
  if (512 < output_buffer_size)
  {
    output_buffer_p = GNUNET_malloc ((output_buffer_size) * sizeof (char));
  } else {
    output_buffer_p = &output_buffer[0];
  }
  size = GNUNET_snprintf (output_buffer_p,
                          output_buffer_size,
                          "%llu %s\n",
                          (GNUNET_TIME_absolute_get ().abs_value_us) / 1000000, // microsec -> sec
                          line);
  if (0 > size)
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Failed to write string to buffer (size: %i)\n",
         size);
    return;
  }

  size2 = GNUNET_DISK_file_write (f, output_buffer_p, size);
  if (size != size2)
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Unable to write to file! (Size: %u, size2: %u)\n",
         size,
         size2);

    if (GNUNET_YES != GNUNET_DISK_file_close (f))
      LOG (GNUNET_ERROR_TYPE_WARNING,
           "Unable to close file\n");

    return;
  }

  if (512 < output_buffer_size)
  {
    GNUNET_free (output_buffer_p);
  }

  if (GNUNET_YES != GNUNET_DISK_file_close (f))
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Unable to close file\n");
}

void
to_file_raw (const char *file_name, const char *buf, size_t size_buf)
{
  struct GNUNET_DISK_FileHandle *f;
  size_t size_written;

  if (NULL == (f = GNUNET_DISK_file_open (file_name,
                                          GNUNET_DISK_OPEN_APPEND |
                                          GNUNET_DISK_OPEN_WRITE |
                                          GNUNET_DISK_OPEN_CREATE,
                                          GNUNET_DISK_PERM_USER_READ |
                                          GNUNET_DISK_PERM_USER_WRITE |
                                          GNUNET_DISK_PERM_GROUP_READ |
                                          GNUNET_DISK_PERM_OTHER_READ)))
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Not able to open file %s\n",
         file_name);
    return;
  }

  size_written = GNUNET_DISK_file_write (f, buf, size_buf);
  if (size_buf != size_written)
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Unable to write to file! (Size: %u, size_written: %u)\n",
         size_buf,
         size_written);

    if (GNUNET_YES != GNUNET_DISK_file_close (f))
      LOG (GNUNET_ERROR_TYPE_WARNING,
           "Unable to close file\n");

    return;
  }
}

void
to_file_raw_unaligned (const char *file_name,
                       const char *buf,
                       size_t size_buf,
                       unsigned bits_needed)
{
  // TODO endianness!
  GNUNET_assert (size_buf >= (bits_needed/8));
  //if (0 == num_bits_buf_unaligned)
  //{
  //  if (0 == (bits_needed % 8))
  //  {
  //    to_file_raw (file_name, buf, size_buf);
  //    return;
  //  }
  //  to_file_raw (file_name, buf, size_buf - 1);
  //  buf_unaligned = buf[size_buf - 1];
  //  num_bits_buf_unaligned = bits_needed % 8;
  //  return;
  //}

  char buf_write[size_buf + 1];
  const unsigned bytes_iter = (0 != bits_needed % 8?
                               (bits_needed/8)+1:
                               bits_needed/8);
  // TODO what if no iteration happens?
  unsigned size_buf_write = 0;
  buf_write[0] = buf_unaligned;
  /* Iterate over input bytes */
  for (unsigned i = 0; i < bytes_iter; i++)
  {
    /* Number of bits needed in this iteration - 8 for all except last iter */
    unsigned num_bits_needed_iter;
    /* Mask for bits to actually use */
    unsigned mask_bits_needed_iter;
    char byte_input;
    /* Number of bits needed to align unaligned byte */
    unsigned num_bits_to_align;
    /* Number of bits that are to be moved */
    unsigned num_bits_to_move;
    /* Mask for bytes to be moved */
    char mask_input_to_move;
    /* Masked bits to be moved */
    char bits_to_move;
    /* The amount of bits needed to fit the bits to shift to the nearest spot */
    unsigned distance_shift_bits;
    /* Shifted bits on the move */
    char bits_moving;
    /* (unaligned) byte being filled with bits */
    char byte_to_fill;
    /* mask for needed bits of the input byte that have not been moved */
    char mask_input_leftover;
    /* needed bits of the input byte that have not been moved */
    char byte_input_leftover;
    unsigned num_bits_leftover;
    unsigned num_bits_discard;
    char byte_unaligned_new;

    if ( (bits_needed - (i * 8)) <= 8)
    {
      /* last iteration */
      num_bits_needed_iter = bits_needed - (i * 8);
    }
    else
    {
      num_bits_needed_iter = 8;
    }
    mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
    byte_input = buf[i];
    byte_input &= mask_bits_needed_iter;
    num_bits_to_align = 8 - num_bits_buf_unaligned;
    num_bits_to_move  = min (num_bits_to_align, num_bits_needed_iter);
    mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
    bits_to_move = byte_input & mask_input_to_move;
    distance_shift_bits = num_bits_buf_unaligned;
    bits_moving = bits_to_move << distance_shift_bits;
    byte_to_fill = buf_unaligned | bits_moving;
    if (num_bits_buf_unaligned + num_bits_needed_iter > 8)
    {
      /* buf_unaligned was aligned by filling
       * -> can be written to storage */
      buf_write[i] = byte_to_fill;
      size_buf_write++;

      /* store the leftover, unaligned bits in buffer */
      mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
      byte_input_leftover = byte_input & mask_input_leftover;
      num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
      num_bits_discard = 8 - num_bits_needed_iter;
      byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
      buf_unaligned = byte_unaligned_new;
      num_bits_buf_unaligned = num_bits_leftover % 8;
    }
    else
    {
      /* unaligned buffer still unaligned but 'fuller' */
      buf_unaligned = byte_to_fill;
      num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
    }
  }
  to_file_raw (file_name, buf_write, size_buf_write);
  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
}

char *
auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
{
  int size;
  size_t name_buf_size;
  char *end;
  char *buf;
  char *name_buf;
  size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;

  name_buf_size = 512 * sizeof (char);
  name_buf = GNUNET_malloc (name_buf_size);

  if (keylen % 5 > 0)
    keylen += 5 - keylen % 5;
  keylen /= 5;
  buf = GNUNET_malloc (keylen + 1);

  end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
      sizeof (struct GNUNET_CRYPTO_AuthKey),
      buf,
      keylen);

  if (NULL == end)
  {
    GNUNET_free (buf);
    GNUNET_break (0);
  }
  else
  {
    *end = '\0';
  }

  size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
  if (0 > size)
    LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");

  GNUNET_free (buf);

  return name_buf;
}


struct GNUNET_CRYPTO_AuthKey
string_to_auth_key (const char *str)
{
  struct GNUNET_CRYPTO_AuthKey auth_key;

  if (GNUNET_OK !=
      GNUNET_STRINGS_string_to_data (str,
                                     strlen (str),
                                     &auth_key.key,
                                     sizeof (struct GNUNET_CRYPTO_AuthKey)))
  {
    LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
  }

  return auth_key;
}


char *
create_file (const char *name)
{
  int size;
  size_t name_buf_size;
  char *name_buf;
  char *prefix;
  char *file_name;

  prefix = "/tmp/rps/";
  name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
  name_buf = GNUNET_malloc (name_buf_size);

  size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
  if (0 > size)
    LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");

  if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Could not create directory %s.\n",
         prefix);
  }

  if (NULL == strstr (name, "sampler_el"))
  {/* only append random string to sampler */
    if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
          LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");

  GNUNET_free (name_buf);
  return file_name;
  }

  return name_buf;
}

#endif /* TO_FILE */

/**
 * @brief Try to ensure that `/tmp/rps` exists.
 *
 * @return #GNUNET_YES on success
 *         #GNUNET_SYSERR on failure
 */
static int ensure_folder_exist (void)
{
  if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
  {
    GNUNET_DISK_directory_create ("/tmp/rps");
  }
  if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
  {
    return GNUNET_SYSERR;
  }
  return GNUNET_YES;
}

const char *
store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
    const char *prefix)
{
  unsigned int len_file_name;
  unsigned int out_size;
  char *file_name;
  const char *pid_long;

  if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
  pid_long = GNUNET_i2s_full (peer);
  len_file_name = (strlen (prefix) +
                   strlen (pid_long) +
                   11)
                     * sizeof (char);
  file_name = GNUNET_malloc (len_file_name);
  out_size = GNUNET_snprintf (file_name,
                              len_file_name,
                              "/tmp/rps/%s-%s",
                              prefix,
                              pid_long);
  if (len_file_name < out_size ||
      0 > out_size)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Failed to write string to buffer (size: %i, out_size: %i)\n",
               len_file_name,
               out_size);
  }
  return file_name;
}

/* end of gnunet-service-rps.c */