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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
|
/*
* This file is part of GNUnet
* Copyright (C) 2013 GNUnet e.V.
*
* 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.
*/
/**
* @file psycstore/psycstore_api.c
* @brief API to interact with the PSYCstore service
* @author Gabor X Toth
* @author Christian Grothoff
*/
#include <inttypes.h>
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_constants.h"
#include "gnunet_protocols.h"
#include "gnunet_psycstore_service.h"
#include "gnunet_multicast_service.h"
#include "psycstore.h"
#define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__)
/**
* Handle for an operation with the PSYCstore service.
*/
struct GNUNET_PSYCSTORE_OperationHandle
{
/**
* Main PSYCstore handle.
*/
struct GNUNET_PSYCSTORE_Handle *h;
/**
* Data callbacks.
*/
union {
GNUNET_PSYCSTORE_FragmentCallback fragment_cb;
GNUNET_PSYCSTORE_CountersCallback counters_cb;
GNUNET_PSYCSTORE_StateCallback state_cb;
};
/**
* Closure for callbacks.
*/
void *cls;
/**
* Message envelope.
*/
struct GNUNET_MQ_Envelope *env;
/**
* Operation ID.
*/
uint64_t op_id;
};
/**
* Handle for the service.
*/
struct GNUNET_PSYCSTORE_Handle
{
/**
* Configuration to use.
*/
const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Client connection.
*/
struct GNUNET_MQ_Handle *mq;
/**
* Async operations.
*/
struct GNUNET_OP_Handle *op;
/**
* Task doing exponential back-off trying to reconnect.
*/
struct GNUNET_SCHEDULER_Task *reconnect_task;
/**
* Delay for next connect retry.
*/
struct GNUNET_TIME_Relative reconnect_delay;
GNUNET_PSYCSTORE_FragmentCallback *fragment_cb;
GNUNET_PSYCSTORE_CountersCallback *counters_cb;
GNUNET_PSYCSTORE_StateCallback *state_cb;
/**
* Closure for callbacks.
*/
void *cb_cls;
};
static int
check_result_code (void *cls, const struct OperationResult *opres)
{
uint16_t size = ntohs (opres->header.size);
const char *str = (const char *) &opres[1];
if ( (sizeof (*opres) < size) &&
('\0' != str[size - sizeof (*opres) - 1]) )
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
return GNUNET_OK;
}
static void
handle_result_code (void *cls, const struct OperationResult *opres)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
uint16_t size = ntohs (opres->header.size);
const char *
str = (sizeof (*opres) < size) ? (const char *) &opres[1] : "";
if (GNUNET_YES == GNUNET_OP_result (h->op, GNUNET_ntohll (opres->op_id),
GNUNET_ntohll (opres->result_code) + INT64_MIN,
str, size - sizeof (*opres), (void **) &op))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"handle_result_code: Received result message with OP ID: %" PRIu64 "\n",
GNUNET_ntohll (opres->op_id));
GNUNET_free (op);
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"handle_result_code: No callback registered for OP ID %" PRIu64 ".\n",
GNUNET_ntohll (opres->op_id));
}
h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
}
static void
handle_result_counters (void *cls, const struct CountersResult *cres)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (cres->op_id),
NULL, NULL, (void **) &op))
{
GNUNET_assert (NULL != op);
if (NULL != op->counters_cb)
{
op->counters_cb (op->cls,
ntohl (cres->result_code),
GNUNET_ntohll (cres->max_fragment_id),
GNUNET_ntohll (cres->max_message_id),
GNUNET_ntohll (cres->max_group_generation),
GNUNET_ntohll (cres->max_state_message_id));
}
GNUNET_OP_remove (h->op, GNUNET_ntohll (cres->op_id));
GNUNET_free (op);
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"handle_result_counters: No callback registered for OP ID %" PRIu64 ".\n",
GNUNET_ntohll (cres->op_id));
}
h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
}
static int
check_result_fragment (void *cls, const struct FragmentResult *fres)
{
uint16_t size = ntohs (fres->header.size);
struct GNUNET_MULTICAST_MessageHeader *mmsg =
(struct GNUNET_MULTICAST_MessageHeader *) &fres[1];
if (sizeof (*fres) + sizeof (*mmsg) < size
&& sizeof (*fres) + ntohs (mmsg->header.size) != size)
{
LOG (GNUNET_ERROR_TYPE_ERROR,
"check_result_fragment: Received message with invalid length %lu bytes.\n",
size, sizeof (*fres));
GNUNET_break (0);
return GNUNET_SYSERR;
}
return GNUNET_OK;
}
static void
handle_result_fragment (void *cls, const struct FragmentResult *fres)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (fres->op_id),
NULL, NULL, (void **) &op))
{
GNUNET_assert (NULL != op);
if (NULL != op->fragment_cb)
op->fragment_cb (op->cls,
(struct GNUNET_MULTICAST_MessageHeader *) &fres[1],
ntohl (fres->psycstore_flags));
//GNUNET_OP_remove (h->op, GNUNET_ntohll (fres->op_id));
//GNUNET_free (op);
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"handle_result_fragment: No callback registered for OP ID %" PRIu64 ".\n",
GNUNET_ntohll (fres->op_id));
}
h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
}
static int
check_result_state (void *cls, const struct StateResult *sres)
{
const char *name = (const char *) &sres[1];
uint16_t size = ntohs (sres->header.size);
uint16_t name_size = ntohs (sres->name_size);
if (name_size <= 2
|| size - sizeof (*sres) < name_size
|| '\0' != name[name_size - 1])
{
LOG (GNUNET_ERROR_TYPE_ERROR,
"check_result_state: Received state result message with invalid name.\n");
GNUNET_break (0);
return GNUNET_SYSERR;
}
return GNUNET_OK;
}
static void
handle_result_state (void *cls, const struct StateResult *sres)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
const char *name = (const char *) &sres[1];
uint16_t name_size = ntohs (sres->name_size);
if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (sres->op_id),
NULL, NULL, (void **) &op))
{
GNUNET_assert (NULL != op);
if (NULL != op->state_cb)
op->state_cb (op->cls, name, (char *) &sres[1] + name_size,
ntohs (sres->header.size) - sizeof (*sres) - name_size);
//GNUNET_OP_remove (h->op, GNUNET_ntohll (sres->op_id));
//GNUNET_free (op);
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"handle_result_state: No callback registered for OP ID %" PRIu64 ".\n",
GNUNET_ntohll (sres->op_id));
}
h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
}
static void
reconnect (void *cls);
/**
* Client disconnected from service.
*
* Reconnect after backoff period.=
*/
static void
disconnected (void *cls, enum GNUNET_MQ_Error error)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Origin client disconnected (%d), re-connecting\n",
(int) error);
if (NULL != h->mq)
{
GNUNET_MQ_destroy (h->mq);
GNUNET_OP_destroy (h->op);
h->mq = NULL;
h->op = NULL;
}
h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
&reconnect, h);
h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
}
static void
do_connect (struct GNUNET_PSYCSTORE_Handle *h)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Connecting to PSYCstore service.\n");
struct GNUNET_MQ_MessageHandler handlers[] = {
GNUNET_MQ_hd_var_size (result_code,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE,
struct OperationResult,
h),
GNUNET_MQ_hd_fixed_size (result_counters,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS,
struct CountersResult,
h),
GNUNET_MQ_hd_var_size (result_fragment,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT,
struct FragmentResult,
h),
GNUNET_MQ_hd_var_size (result_state,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE,
struct StateResult,
h),
GNUNET_MQ_handler_end ()
};
h->op = GNUNET_OP_create ();
GNUNET_assert (NULL == h->mq);
h->mq = GNUNET_CLIENT_connect (h->cfg, "psycstore",
handlers, disconnected, h);
GNUNET_assert (NULL != h->mq);
}
/**
* Try again to connect to the PSYCstore service.
*
* @param cls Handle to the PSYCstore service.
*/
static void
reconnect (void *cls)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
h->reconnect_task = NULL;
do_connect (cls);
}
/**
* Connect to the PSYCstore service.
*
* @param cfg The configuration to use
* @return Handle to use
*/
struct GNUNET_PSYCSTORE_Handle *
GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
struct GNUNET_PSYCSTORE_Handle *h
= GNUNET_new (struct GNUNET_PSYCSTORE_Handle);
h->cfg = cfg;
h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
do_connect (h);
return h;
}
/**
* Disconnect from PSYCstore service
*
* @param h Handle to destroy
*/
void
GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h)
{
GNUNET_assert (NULL != h);
if (h->reconnect_task != NULL)
{
GNUNET_SCHEDULER_cancel (h->reconnect_task);
h->reconnect_task = NULL;
}
if (NULL != h->mq)
{
// FIXME: free data structures for pending operations
GNUNET_MQ_destroy (h->mq);
h->mq = NULL;
}
GNUNET_free (h);
}
/**
* Message sent notification.
*
* Remove invalidated envelope pointer.
*/
static void
message_sent (void *cls)
{
struct GNUNET_PSYCSTORE_OperationHandle *op = cls;
op->env = NULL;
}
/**
* Create a new operation.
*/
static struct GNUNET_PSYCSTORE_OperationHandle *
op_create (struct GNUNET_PSYCSTORE_Handle *h,
struct GNUNET_OP_Handle *hop,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct GNUNET_PSYCSTORE_OperationHandle *
op = GNUNET_malloc (sizeof (*op));
op->h = h;
op->op_id = GNUNET_OP_add (hop,
(GNUNET_ResultCallback) result_cb,
cls, op);
return op;
}
/**
* Send a message associated with an operation.
*
* @param h
* PSYCstore handle.
* @param op
* Operation handle.
* @param env
* Message envelope to send.
* @param[out] op_id
* Operation ID to write in network byte order. NULL if not needed.
*
* @return Operation handle.
*
*/
static struct GNUNET_PSYCSTORE_OperationHandle *
op_send (struct GNUNET_PSYCSTORE_Handle *h,
struct GNUNET_PSYCSTORE_OperationHandle *op,
struct GNUNET_MQ_Envelope *env,
uint64_t *op_id)
{
op->env = env;
if (NULL != op_id)
*op_id = GNUNET_htonll (op->op_id);
GNUNET_MQ_notify_sent (env, message_sent, op);
GNUNET_MQ_send (h->mq, env);
return op;
}
/**
* Cancel a PSYCstore operation. Note that the operation MAY still
* be executed; this merely cancels the continuation; if the request
* was already transmitted, the service may still choose to complete
* the operation.
*
* @param op Operation to cancel.
*
* @return #GNUNET_YES if message was not sent yet and got discarded,
* #GNUNET_NO if it was already sent, and only the callbacks got cancelled.
*/
int
GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op)
{
struct GNUNET_PSYCSTORE_Handle *h = op->h;
int ret = GNUNET_NO;
if (NULL != op->env)
{
GNUNET_MQ_send_cancel (op->env);
ret = GNUNET_YES;
}
GNUNET_OP_remove (h->op, op->op_id);
GNUNET_free (op);
return ret;
}
/**
* Store join/leave events for a PSYC channel in order to be able to answer
* membership test queries later.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel where the event happened.
* @param slave_key
* Public key of joining/leaving slave.
* @param did_join
* #GNUNET_YES on join, #GNUNET_NO on part.
* @param announced_at
* ID of the message that announced the membership change.
* @param effective_since
* Message ID this membership change is in effect since.
* For joins it is <= announced_at, for parts it is always 0.
* @param group_generation
* In case of a part, the last group generation the slave has access to.
* It has relevance when a larger message have fragments with different
* group generations.
* @param result_cb
* Callback to call with the result of the storage operation.
* @param cls
* Closure for the callback.
*
* @return Operation handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_membership_store (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
int did_join,
uint64_t announced_at,
uint64_t effective_since,
uint64_t group_generation,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
GNUNET_assert (NULL != h);
GNUNET_assert (NULL != channel_key);
GNUNET_assert (NULL != slave_key);
GNUNET_assert (GNUNET_YES == did_join || GNUNET_NO == did_join);
GNUNET_assert (did_join
? effective_since <= announced_at
: effective_since == 0);
struct MembershipStoreRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE);
req->channel_key = *channel_key;
req->slave_key = *slave_key;
req->did_join = did_join;
req->announced_at = GNUNET_htonll (announced_at);
req->effective_since = GNUNET_htonll (effective_since);
req->group_generation = GNUNET_htonll (group_generation);
return
op_send (h, op_create (h, h->op, result_cb, cls),
env, &req->op_id);
}
/**
* Test if a member was admitted to the channel at the given message ID.
*
* This is useful when relaying and replaying messages to check if a particular
* slave has access to the message fragment with a given group generation. It
* is also used when handling join requests to determine whether the slave is
* currently admitted to the channel.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param slave_key
* Public key of slave whose membership to check.
* @param message_id
* Message ID for which to do the membership test.
* @param group_generation
* Group generation of the fragment of the message to test.
* It has relevance if the message consists of multiple fragments with
* different group generations.
* @param result_cb
* Callback to call with the test result.
* @param cls
* Closure for the callback.
*
* @return Operation handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_membership_test (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
uint64_t message_id,
uint64_t group_generation,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct MembershipTestRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST);
req->channel_key = *channel_key;
req->slave_key = *slave_key;
req->message_id = GNUNET_htonll (message_id);
req->group_generation = GNUNET_htonll (group_generation);
return
op_send (h, op_create (h, h->op, result_cb, cls),
env, &req->op_id);
}
/**
* Store a message fragment sent to a channel.
*
* @param h Handle for the PSYCstore.
* @param channel_key The channel the message belongs to.
* @param message Message to store.
* @param psycstore_flags Flags indicating whether the PSYC message contains
* state modifiers.
* @param result_cb Callback to call with the result of the operation.
* @param cls Closure for the callback.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_fragment_store (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_MULTICAST_MessageHeader *msg,
enum GNUNET_PSYCSTORE_MessageFlags psycstore_flags,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
uint16_t size = ntohs (msg->header.size);
struct FragmentStoreRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (req, size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE);
req->channel_key = *channel_key;
req->psycstore_flags = htonl (psycstore_flags);
GNUNET_memcpy (&req[1], msg, size);
return
op_send (h, op_create (h, h->op, result_cb, cls),
env, &req->op_id);
}
/**
* Retrieve message fragments by fragment ID range.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param slave_key
* The slave requesting the fragment. If not NULL, a membership test is
* performed first and the fragment is only returned if the slave has
* access to it.
* @param first_fragment_id
* First fragment ID to retrieve.
* Use 0 to get the latest message fragment.
* @param last_fragment_id
* Last consecutive fragment ID to retrieve.
* Use 0 to get the latest message fragment.
* @param fragment_limit
* Maximum number of fragments to retrieve.
* @param fragment_cb
* Callback to call with the retrieved fragments.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_fragment_get (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
uint64_t first_fragment_id,
uint64_t last_fragment_id,
GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct FragmentGetRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET);
req->channel_key = *channel_key;
req->first_fragment_id = GNUNET_htonll (first_fragment_id);
req->last_fragment_id = GNUNET_htonll (last_fragment_id);
if (NULL != slave_key)
{
req->slave_key = *slave_key;
req->do_membership_test = GNUNET_YES;
}
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->fragment_cb = fragment_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Retrieve latest message fragments.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param slave_key
* The slave requesting the fragment. If not NULL, a membership test is
* performed first and the fragment is only returned if the slave has
* access to it.
* @param first_fragment_id
* First fragment ID to retrieve.
* Use 0 to get the latest message fragment.
* @param last_fragment_id
* Last consecutive fragment ID to retrieve.
* Use 0 to get the latest message fragment.
* @param fragment_limit
* Maximum number of fragments to retrieve.
* @param fragment_cb
* Callback to call with the retrieved fragments.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_fragment_get_latest (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
uint64_t fragment_limit,
GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct FragmentGetRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET);
req->channel_key = *channel_key;
req->fragment_limit = GNUNET_ntohll (fragment_limit);
if (NULL != slave_key)
{
req->slave_key = *slave_key;
req->do_membership_test = GNUNET_YES;
}
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->fragment_cb = fragment_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Retrieve all fragments of messages in a message ID range.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param slave_key
* The slave requesting the message.
* If not NULL, a membership test is performed first
* and the message is only returned if the slave has access to it.
* @param first_message_id
* First message ID to retrieve.
* @param last_message_id
* Last consecutive message ID to retrieve.
* @param fragment_limit
* Maximum number of fragments to retrieve.
* @param method_prefix
* Retrieve only messages with a matching method prefix.
* @todo Implement method_prefix query.
* @param fragment_cb
* Callback to call with the retrieved fragments.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_message_get (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
uint64_t first_message_id,
uint64_t last_message_id,
uint64_t fragment_limit,
const char *method_prefix,
GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct MessageGetRequest *req;
if (NULL == method_prefix)
method_prefix = "";
uint16_t method_size = strnlen (method_prefix,
GNUNET_MAX_MESSAGE_SIZE
- sizeof (*req)) + 1;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (req, method_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET);
req->channel_key = *channel_key;
req->first_message_id = GNUNET_htonll (first_message_id);
req->last_message_id = GNUNET_htonll (last_message_id);
req->fragment_limit = GNUNET_htonll (fragment_limit);
if (NULL != slave_key)
{
req->slave_key = *slave_key;
req->do_membership_test = GNUNET_YES;
}
GNUNET_memcpy (&req[1], method_prefix, method_size);
((char *) &req[1])[method_size - 1] = '\0';
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->fragment_cb = fragment_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Retrieve all fragments of the latest messages.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param slave_key
* The slave requesting the message.
* If not NULL, a membership test is performed first
* and the message is only returned if the slave has access to it.
* @param message_limit
* Maximum number of messages to retrieve.
* @param method_prefix
* Retrieve only messages with a matching method prefix.
* @todo Implement method_prefix query.
* @param fragment_cb
* Callback to call with the retrieved fragments.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_message_get_latest (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
uint64_t message_limit,
const char *method_prefix,
GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct MessageGetRequest *req;
if (NULL == method_prefix)
method_prefix = "";
uint16_t method_size = strnlen (method_prefix,
GNUNET_MAX_MESSAGE_SIZE
- sizeof (*req)) + 1;
GNUNET_assert ('\0' == method_prefix[method_size - 1]);
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (req, method_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET);
req->channel_key = *channel_key;
req->message_limit = GNUNET_ntohll (message_limit);
if (NULL != slave_key)
{
req->slave_key = *slave_key;
req->do_membership_test = GNUNET_YES;
}
GNUNET_memcpy (&req[1], method_prefix, method_size);
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->fragment_cb = fragment_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Retrieve a fragment of message specified by its message ID and fragment
* offset.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param slave_key
* The slave requesting the message fragment. If not NULL, a membership
* test is performed first and the message fragment is only returned
* if the slave has access to it.
* @param message_id
* Message ID to retrieve. Use 0 to get the latest message.
* @param fragment_offset
* Offset of the fragment to retrieve.
* @param fragment_cb
* Callback to call with the retrieved fragments.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_message_get_fragment (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
uint64_t message_id,
uint64_t fragment_offset,
GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct MessageGetFragmentRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT);
req->channel_key = *channel_key;
req->message_id = GNUNET_htonll (message_id);
req->fragment_offset = GNUNET_htonll (fragment_offset);
if (NULL != slave_key)
{
req->slave_key = *slave_key;
req->do_membership_test = GNUNET_YES;
}
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->fragment_cb = fragment_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Retrieve latest values of counters for a channel master.
*
* The current value of counters are needed when a channel master is restarted,
* so that it can continue incrementing the counters from their last value.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* Public key that identifies the channel.
* @param ccb
* Callback to call with the result.
* @param ccb_cls
* Closure for the @a ccb callback.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h,
struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
GNUNET_PSYCSTORE_CountersCallback counters_cb,
void *cls)
{
struct OperationRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET);
req->channel_key = *channel_key;
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, NULL, NULL);
op->counters_cb = counters_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Apply modifiers of a message to the current channel state.
*
* An error is returned if there are missing messages containing state
* operations before the current one.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param message_id
* ID of the message that contains the @a modifiers.
* @param state_delta
* Value of the _state_delta PSYC header variable of the message.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for @a result_cb.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
uint64_t message_id,
uint64_t state_delta,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct StateModifyRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY);
req->channel_key = *channel_key;
req->message_id = GNUNET_htonll (message_id);
req->state_delta = GNUNET_htonll (state_delta);
return op_send (h, op_create (h, h->op, result_cb, cls),
env, &req->op_id);
}
struct StateSyncClosure
{
GNUNET_PSYCSTORE_ResultCallback result_cb;
void *cls;
uint8_t last;
};
static void
state_sync_result (void *cls, int64_t result,
const char *err_msg, uint16_t err_msg_size)
{
struct StateSyncClosure *ssc = cls;
if (GNUNET_OK != result || ssc->last)
ssc->result_cb (ssc->cls, result, err_msg, err_msg_size);
GNUNET_free (ssc);
}
/**
* Store synchronized state.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param max_state_message_id
* ID of the last stateful message before @a state_hash_message_id.
* @param state_hash_message_id
* ID of the message that contains the state_hash PSYC header variable.
* @param modifier_count
* Number of elements in the @a modifiers array.
* @param modifiers
* Full state to store.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callback.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_state_sync (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
uint64_t max_state_message_id,
uint64_t state_hash_message_id,
size_t modifier_count,
const struct GNUNET_PSYC_Modifier *modifiers,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
size_t i;
for (i = 0; i < modifier_count; i++) {
struct StateSyncRequest *req;
uint16_t name_size = strlen (modifiers[i].name) + 1;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (req,
sizeof (*req) + name_size + modifiers[i].value_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
req->header.size = htons (sizeof (*req) + name_size
+ modifiers[i].value_size);
req->channel_key = *channel_key;
req->max_state_message_id = GNUNET_htonll (max_state_message_id);
req->state_hash_message_id = GNUNET_htonll (state_hash_message_id);
req->name_size = htons (name_size);
req->flags
= (0 == i)
? STATE_OP_FIRST
: (modifier_count - 1 == i)
? STATE_OP_LAST
: 0;
GNUNET_memcpy (&req[1], modifiers[i].name, name_size);
GNUNET_memcpy ((char *) &req[1] + name_size, modifiers[i].value, modifiers[i].value_size);
struct StateSyncClosure *ssc = GNUNET_malloc (sizeof (*ssc));
ssc->last = (req->flags & STATE_OP_LAST);
ssc->result_cb = result_cb;
ssc->cls = cls;
op_send (h, op_create (h, h->op, state_sync_result, ssc),
env, &req->op_id);
}
// FIXME: only one operation is returned,
// add pointers to other operations and make all cancellable.
return op;
}
/**
* Reset the state of a channel.
*
* Delete all state variables stored for the given channel.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callback.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_state_reset (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey
*channel_key,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct OperationRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET);
req->channel_key = *channel_key;
return
op_send (h, op_create (h, h->op, result_cb, cls),
env, &req->op_id);
}
/**
* Update signed values of state variables in the state store.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param message_id
* Message ID that contained the state @a hash.
* @param hash
* Hash of the serialized full state.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callback.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
uint64_t message_id,
const struct GNUNET_HashCode *hash,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
struct StateHashUpdateRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE);
req->channel_key = *channel_key;
req->hash = *hash;
return
op_send (h, op_create (h, h->op, result_cb, cls),
env, &req->op_id);
}
/**
* Retrieve the best matching state variable.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param name
* Name of variable to match, the returned variable might be less specific.
* @param state_cb
* Callback to return the matching state variable.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const char *name,
GNUNET_PSYCSTORE_StateCallback state_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
size_t name_size = strlen (name) + 1;
struct OperationRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (req, name_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET);
req->channel_key = *channel_key;
GNUNET_memcpy (&req[1], name, name_size);
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->state_cb = state_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/**
* Retrieve all state variables for a channel with the given prefix.
*
* @param h
* Handle for the PSYCstore.
* @param channel_key
* The channel we are interested in.
* @param name_prefix
* Prefix of state variable names to match.
* @param state_cb
* Callback to return matching state variables.
* @param result_cb
* Callback to call with the result of the operation.
* @param cls
* Closure for the callbacks.
*
* @return Handle that can be used to cancel the operation.
*/
struct GNUNET_PSYCSTORE_OperationHandle *
GNUNET_PSYCSTORE_state_get_prefix (struct GNUNET_PSYCSTORE_Handle *h,
const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
const char *name_prefix,
GNUNET_PSYCSTORE_StateCallback state_cb,
GNUNET_PSYCSTORE_ResultCallback result_cb,
void *cls)
{
size_t name_size = strlen (name_prefix) + 1;
struct OperationRequest *req;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (req, name_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX);
req->channel_key = *channel_key;
GNUNET_memcpy (&req[1], name_prefix, name_size);
struct GNUNET_PSYCSTORE_OperationHandle *
op = op_create (h, h->op, result_cb, cls);
op->state_cb = state_cb;
op->cls = cls;
return op_send (h, op, env, &req->op_id);
}
/* end of psycstore_api.c */
|