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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
|
/*
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 General Public License as published
by the Free Software Foundation; either version 3, 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/**
* @file rps/gnunet-service-rps_sampler.c
* @brief sampler implementation
* @author Julius Bünger
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include "rps.h"
#include "gnunet-service-rps_sampler.h"
#include "gnunet-service-rps_sampler_elem.h"
#include <math.h>
#include <inttypes.h>
#include "rps-test_util.h"
#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
// multiple 'clients'?
// TODO check for overflows
// TODO align message structs
// hist_size_init, hist_size_max
/***********************************************************************
* WARNING: This section needs to be reviewed regarding the use of
* functions providing (pseudo)randomness!
***********************************************************************/
// TODO care about invalid input of the caller (size 0 or less...)
/**
* Callback that is called from _get_rand_peer() when the PeerID is ready.
*
* @param cls the closure given alongside this function.
* @param id the PeerID that was returned
*/
typedef void
(*RPS_sampler_rand_peer_ready_cont) (void *cls,
const struct GNUNET_PeerIdentity *id);
/**
* Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
*/
struct GetPeerCls
{
/**
* DLL
*/
struct GetPeerCls *next;
/**
* DLL
*/
struct GetPeerCls *prev;
/**
* The sampler this function operates on.
*/
struct RPS_Sampler *sampler;
/**
* The task for this function.
*/
struct GNUNET_SCHEDULER_Task *get_peer_task;
/**
* The callback
*/
RPS_sampler_rand_peer_ready_cont cont;
/**
* The closure to the callback @e cont
*/
void *cont_cls;
/**
* The address of the id to be stored at
*/
struct GNUNET_PeerIdentity *id;
};
/**
* Type of function used to differentiate between modified and not modified
* Sampler.
*/
typedef void
(*RPS_get_peers_type) (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc);
/**
* Get one random peer out of the sampled peers.
*
* We might want to reinitialise this sampler after giving the
* corrsponding peer to the client.
* Only used internally
*/
static void
sampler_get_rand_peer (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc);
/**
* Get one random peer out of the sampled peers.
*
* We might want to reinitialise this sampler after giving the
* corrsponding peer to the client.
*/
static void
sampler_mod_get_rand_peer (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc);
/**
* Sampler with its own array of SamplerElements
*/
struct RPS_Sampler
{
/**
* Number of sampler elements we hold.
*/
unsigned int sampler_size;
//size_t size;
/**
* All sampler elements in one array.
*/
struct RPS_SamplerElement **sampler_elements;
/**
* Maximum time a round takes
*
* Used in the context of RPS
*/
struct GNUNET_TIME_Relative max_round_interval;
/**
* Stores the function to return peers. Which one it is depends on whether
* the Sampler is the modified one or not.
*/
RPS_get_peers_type get_peers;
/**
* Head for the DLL to store the closures to pending requests.
*/
struct GetPeerCls *gpc_head;
/**
* Tail for the DLL to store the closures to pending requests.
*/
struct GetPeerCls *gpc_tail;
#ifdef TO_FILE
/**
* File name to log to
*/
char *file_name;
#endif /* TO_FILE */
};
/**
* Closure to _get_n_rand_peers_ready_cb()
*/
struct NRandPeersReadyCls
{
/**
* Number of peers we are waiting for.
*/
uint32_t num_peers;
/**
* Number of peers we currently have.
*/
uint32_t cur_num_peers;
/**
* Pointer to the array holding the ids.
*/
struct GNUNET_PeerIdentity *ids;
/**
* Callback to be called when all ids are available.
*/
RPS_sampler_n_rand_peers_ready_cb callback;
/**
* Closure given to the callback
*/
void *cls;
};
///**
// * Global sampler variable.
// */
//struct RPS_Sampler *sampler;
/**
* The minimal size for the extended sampler elements.
*/
static size_t min_size;
/**
* The maximal size the extended sampler elements should grow to.
*/
static size_t max_size;
/**
* The size the extended sampler elements currently have.
*/
//static size_t extra_size;
/**
* Inedex to the sampler element that is the next to be returned
*/
static uint32_t client_get_index;
/**
* Callback to _get_rand_peer() used by _get_n_rand_peers().
*
* Checks whether all n peers are available. If they are,
* give those back.
*/
static void
check_n_peers_ready (void *cls,
const struct GNUNET_PeerIdentity *id)
{
struct NRandPeersReadyCls *n_peers_cls = cls;
n_peers_cls->cur_num_peers++;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Got %" PRIX32 ". of %" PRIX32 " peers\n",
n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
{ /* All peers are ready -- return those to the client */
GNUNET_assert (NULL != n_peers_cls->callback);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"returning %" PRIX32 " peers to the client\n",
n_peers_cls->num_peers);
n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
GNUNET_free (n_peers_cls);
}
}
/**
* Get the size of the sampler.
*
* @param sampler the sampler to return the size of.
* @return the size of the sampler
*/
unsigned int
RPS_sampler_get_size (struct RPS_Sampler *sampler)
{
return sampler->sampler_size;
}
/**
* Grow or shrink the size of the sampler.
*
* @param sampler the sampler to resize.
* @param new_size the new size of the sampler
*/
static void
sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
{
unsigned int old_size;
uint32_t i;
// TODO check min and max size
old_size = sampler->sampler_size;
if (old_size > new_size)
{ /* Shrinking */
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Shrinking sampler %d -> %d\n",
old_size,
new_size);
to_file (sampler->file_name,
"Shrinking sampler %d -> %d",
old_size,
new_size);
for (i = new_size ; i < old_size ; i++)
{
to_file (sampler->file_name,
"-%" PRIu32 ": %s",
i,
sampler->sampler_elements[i]->file_name);
}
GNUNET_array_grow (sampler->sampler_elements,
sampler->sampler_size,
new_size);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"sampler->sampler_elements now points to %p\n",
sampler->sampler_elements);
}
else if (old_size < new_size)
{ /* Growing */
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Growing sampler %d -> %d\n",
old_size,
new_size);
to_file (sampler->file_name,
"Growing sampler %d -> %d",
old_size,
new_size);
GNUNET_array_grow (sampler->sampler_elements,
sampler->sampler_size,
new_size);
for (i = old_size ; i < new_size ; i++)
{ /* Add new sampler elements */
sampler->sampler_elements[i] = RPS_sampler_elem_create ();
to_file (sampler->file_name,
"+%" PRIu32 ": %s",
i,
sampler->sampler_elements[i]->file_name);
}
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
return;
}
GNUNET_assert (sampler->sampler_size == new_size);
}
/**
* Grow or shrink the size of the sampler.
*
* @param sampler the sampler to resize.
* @param new_size the new size of the sampler
*/
void
RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
{
GNUNET_assert (0 < new_size);
sampler_resize (sampler, new_size);
}
/**
* Empty the sampler.
*
* @param sampler the sampler to empty.
* @param new_size the new size of the sampler
*/
static void
sampler_empty (struct RPS_Sampler *sampler)
{
sampler_resize (sampler, 0);
}
/**
* Initialise a tuple of sampler elements.
*
* @param init_size the size the sampler is initialised with
* @param max_round_interval maximum time a round takes
* @return a handle to a sampler that consists of sampler elements.
*/
struct RPS_Sampler *
RPS_sampler_init (size_t init_size,
struct GNUNET_TIME_Relative max_round_interval)
{
struct RPS_Sampler *sampler;
/* Initialise context around extended sampler */
min_size = 10; // TODO make input to _samplers_init()
max_size = 1000; // TODO make input to _samplers_init()
sampler = GNUNET_new (struct RPS_Sampler);
#ifdef TO_FILE
sampler->file_name = create_file ("sampler-");
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Initialised sampler %s\n",
sampler->file_name);
#endif /* TO_FILE */
sampler->sampler_size = 0;
sampler->sampler_elements = NULL;
sampler->max_round_interval = max_round_interval;
sampler->get_peers = sampler_get_rand_peer;
sampler->gpc_head = NULL;
sampler->gpc_tail = NULL;
//sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
//GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
RPS_sampler_resize (sampler, init_size);
client_get_index = 0;
//GNUNET_assert (init_size == sampler->sampler_size);
return sampler;
}
/**
* Initialise a modified tuple of sampler elements.
*
* @param init_size the size the sampler is initialised with
* @param max_round_interval maximum time a round takes
* @return a handle to a sampler that consists of sampler elements.
*/
struct RPS_Sampler *
RPS_sampler_mod_init (size_t init_size,
struct GNUNET_TIME_Relative max_round_interval)
{
struct RPS_Sampler *sampler;
sampler = RPS_sampler_init (init_size, max_round_interval);
sampler->get_peers = sampler_mod_get_rand_peer;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Initialised modified sampler %s\n",
sampler->file_name);
to_file (sampler->file_name,
"This is a modified sampler");
return sampler;
}
/**
* A fuction to update every sampler in the given list
*
* @param sampler the sampler to update.
* @param id the PeerID that is put in the sampler
*/
void
RPS_sampler_update (struct RPS_Sampler *sampler,
const struct GNUNET_PeerIdentity *id)
{
uint32_t i;
to_file (sampler->file_name,
"Got %s",
GNUNET_i2s_full (id));
for (i = 0 ; i < sampler->sampler_size ; i++)
{
RPS_sampler_elem_next (sampler->sampler_elements[i],
id);
}
}
/**
* Reinitialise all previously initialised sampler elements with the given value.
*
* Used to get rid of a PeerID.
*
* @param sampler the sampler to reinitialise a sampler element in.
* @param id the id of the sampler elements to update.
*/
void
RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
const struct GNUNET_PeerIdentity *id)
{
uint32_t i;
struct RPS_SamplerElement *trash_entry;
for ( i = 0 ; i < sampler->sampler_size ; i++ )
{
if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
trash_entry = GNUNET_new (struct RPS_SamplerElement);
*trash_entry = *(sampler->sampler_elements[i]);
to_file (trash_entry->file_name,
"--- non-active");
RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
}
}
}
/**
* Get one random peer out of the sampled peers.
*
* We might want to reinitialise this sampler after giving the
* corrsponding peer to the client.
* Only used internally
*/
static void
sampler_get_rand_peer (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GetPeerCls *gpc = cls;
uint32_t r_index;
gpc->get_peer_task = NULL;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
/**;
* Choose the r_index of the peer we want to return
* at random from the interval of the gossip list
*/
r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
gpc->sampler->sampler_size);
if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty)
{
//LOG (GNUNET_ERROR_TYPE_DEBUG,
// "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
/* FIXME no active wait - get notified, when new id arrives?
* Might also be a freshly emptied one. Others might still contain ids.
* Counter?
*/
gpc->get_peer_task =
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
GNUNET_TIME_UNIT_SECONDS, 0.1),
&sampler_get_rand_peer,
cls);
return;
}
*gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
gpc->cont (gpc->cont_cls, gpc->id);
GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
gpc->sampler->gpc_tail,
gpc);
GNUNET_free (gpc);
}
/**
* Get one random peer out of the sampled peers.
*
* We might want to reinitialise this sampler after giving the
* corrsponding peer to the client.
*/
static void
sampler_mod_get_rand_peer (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GetPeerCls *gpc = cls;
struct RPS_SamplerElement *s_elem;
struct GNUNET_TIME_Relative last_request_diff;
gpc->get_peer_task = NULL;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
/* Cycle the #client_get_index one step further */
client_get_index = (client_get_index + 1) % gpc->sampler->sampler_size;
s_elem = gpc->sampler->sampler_elements[client_get_index];
*gpc->id = s_elem->peer_id;
GNUNET_assert (NULL != s_elem);
if (EMPTY == s_elem->is_empty)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "Sampler_mod element empty, rescheduling.\n");
GNUNET_assert (NULL == gpc->get_peer_task);
gpc->get_peer_task =
GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
&sampler_mod_get_rand_peer,
cls);
return;
}
/* Check whether we may use this sampler to give it back to the client */
if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
{
last_request_diff =
GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
GNUNET_TIME_absolute_get ());
/* We're not going to give it back now if it was
* already requested by a client this round */
if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
///* How many time remains untile the next round has started? */
//inv_last_request_diff =
// GNUNET_TIME_absolute_get_difference (last_request_diff,
// sampler->max_round_interval);
// add a little delay
/* Schedule it one round later */
GNUNET_assert (NULL == gpc->get_peer_task);
gpc->get_peer_task =
GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
&sampler_mod_get_rand_peer,
cls);
return;
}
// TODO add other reasons to wait here
}
s_elem->last_client_request = GNUNET_TIME_absolute_get ();
GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
gpc->sampler->gpc_tail,
gpc);
gpc->cont (gpc->cont_cls, gpc->id);
GNUNET_free (gpc);
}
/**
* Get n random peers out of the sampled peers.
*
* We might want to reinitialise this sampler after giving the
* corrsponding peer to the client.
* Random with or without consumption?
*
* @param sampler the sampler to get peers from.
* @param cb callback that will be called once the ids are ready.
* @param cls closure given to @a cb
* @param for_client #GNUNET_YES if result is used for client,
* #GNUNET_NO if used internally
* @param num_peers the number of peers requested
*/
void
RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
RPS_sampler_n_rand_peers_ready_cb cb,
void *cls, uint32_t num_peers)
{
GNUNET_assert (0 != sampler->sampler_size);
if (0 == num_peers)
return;
// TODO check if we have too much (distinct) sampled peers
uint32_t i;
struct NRandPeersReadyCls *cb_cls;
struct GetPeerCls *gpc;
cb_cls = GNUNET_new (struct NRandPeersReadyCls);
cb_cls->num_peers = num_peers;
cb_cls->cur_num_peers = 0;
cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
cb_cls->callback = cb;
cb_cls->cls = cls;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Scheduling requests for %" PRIu32 " peers\n", num_peers);
for (i = 0 ; i < num_peers ; i++)
{
gpc = GNUNET_new (struct GetPeerCls);
gpc->sampler = sampler;
gpc->cont = check_n_peers_ready;
gpc->cont_cls = cb_cls;
gpc->id = &cb_cls->ids[i];
// maybe add a little delay
gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
GNUNET_CONTAINER_DLL_insert (sampler->gpc_head,
sampler->gpc_tail,
gpc);
}
}
/**
* Counts how many Samplers currently hold a given PeerID.
*
* @param sampler the sampler to count ids in.
* @param id the PeerID to count.
*
* @return the number of occurrences of id.
*/
uint32_t
RPS_sampler_count_id (struct RPS_Sampler *sampler,
const struct GNUNET_PeerIdentity *id)
{
uint32_t count;
uint32_t i;
count = 0;
for ( i = 0 ; i < sampler->sampler_size ; i++ )
{
if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
&& EMPTY != sampler->sampler_elements[i]->is_empty)
count++;
}
return count;
}
/**
* Cleans the sampler.
*/
void
RPS_sampler_destroy (struct RPS_Sampler *sampler)
{
struct GetPeerCls *i;
for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head)
{
GNUNET_CONTAINER_DLL_remove (sampler->gpc_head,
sampler->gpc_tail,
i);
GNUNET_SCHEDULER_cancel (i->get_peer_task);
GNUNET_free (i);
}
sampler_empty (sampler);
GNUNET_free (sampler);
}
/* end of gnunet-service-rps.c */
|