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
|
/**
* 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/gnunet-service-psycstore.c
* @brief 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_statistics_service.h"
#include "gnunet_psyc_util_lib.h"
#include "gnunet_psycstore_service.h"
#include "gnunet_psycstore_plugin.h"
#include "psycstore.h"
/**
* Handle to our current configuration.
*/
static const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Service handle.
*/
static struct GNUNET_SERVICE_Handle *service;
/**
* Handle to the statistics service.
*/
static struct GNUNET_STATISTICS_Handle *stats;
/**
* Database handle
*/
static struct GNUNET_PSYCSTORE_PluginFunctions *db;
/**
* Name of the database plugin
*/
static char *db_lib_name;
/**
* Task run during shutdown.
*
* @param cls unused
*/
static void
shutdown_task (void *cls)
{
if (NULL != stats)
{
GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
stats = NULL;
}
GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
GNUNET_free (db_lib_name);
db_lib_name = NULL;
}
/**
* Send a result code back to the client.
*
* @param client
* Client that should receive the result code.
* @param result_code
* Code to transmit.
* @param op_id
* Operation ID in network byte order.
* @param err_msg
* Error message to include (or NULL for none).
*/
static void
send_result_code (struct GNUNET_SERVICE_Client *client,
uint64_t op_id,
int64_t result_code,
const char *err_msg)
{
struct OperationResult *res;
size_t err_size = 0;
if (NULL != err_msg)
err_size = strnlen (err_msg,
GNUNET_MAX_MESSAGE_SIZE - sizeof (*res) - 1) + 1;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (res, err_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
res->result_code = GNUNET_htonll (result_code - INT64_MIN);
res->op_id = op_id;
if (0 < err_size)
{
GNUNET_memcpy (&res[1], err_msg, err_size);
((char *) &res[1])[err_size - 1] = '\0';
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Sending result to client: %" PRId64 " (%s)\n",
result_code, err_msg);
GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
}
enum
{
MEMBERSHIP_TEST_NOT_NEEDED = 0,
MEMBERSHIP_TEST_NEEDED = 1,
MEMBERSHIP_TEST_DONE = 2,
} MessageMembershipTest;
struct SendClosure
{
struct GNUNET_SERVICE_Client *client;
/**
* Channel's public key.
*/
struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
/**
* Slave's public key.
*/
struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
/**
* Operation ID.
*/
uint64_t op_id;
/**
* Membership test result.
*/
int membership_test_result;
/**
* Do membership test with @a slave_key before returning fragment?
* @see enum MessageMembershipTest
*/
uint8_t membership_test;
};
static int
send_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
enum GNUNET_PSYCSTORE_MessageFlags flags)
{
struct SendClosure *sc = cls;
struct FragmentResult *res;
if (MEMBERSHIP_TEST_NEEDED == sc->membership_test)
{
sc->membership_test = MEMBERSHIP_TEST_DONE;
sc->membership_test_result
= db->membership_test (db->cls, &sc->channel_key, &sc->slave_key,
GNUNET_ntohll (msg->message_id));
switch (sc->membership_test_result)
{
case GNUNET_YES:
break;
case GNUNET_NO:
case GNUNET_SYSERR:
return GNUNET_NO;
}
}
size_t msg_size = ntohs (msg->header.size);
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (res, msg_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT);
res->op_id = sc->op_id;
res->psycstore_flags = htonl (flags);
GNUNET_memcpy (&res[1], msg, msg_size);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Sending fragment %llu to client\n",
(unsigned long long) GNUNET_ntohll (msg->fragment_id));
GNUNET_free (msg);
GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (sc->client), env);
return GNUNET_YES;
}
static int
send_state_var (void *cls, const char *name,
const void *value, uint32_t value_size)
{
struct SendClosure *sc = cls;
struct StateResult *res;
size_t name_size = strlen (name) + 1;
/** @todo FIXME: split up value into 64k chunks */
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg_extra (res, name_size + value_size,
GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
res->op_id = sc->op_id;
res->name_size = htons (name_size);
GNUNET_memcpy (&res[1], name, name_size);
GNUNET_memcpy ((char *) &res[1] + name_size, value, value_size);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Sending state variable %s to client\n", name);
GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (sc->client), env);
return GNUNET_OK;
}
static void
handle_client_membership_store (void *cls,
const struct MembershipStoreRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
int ret = db->membership_store (db->cls, &req->channel_key, &req->slave_key,
req->did_join,
GNUNET_ntohll (req->announced_at),
GNUNET_ntohll (req->effective_since),
GNUNET_ntohll (req->group_generation));
if (ret != GNUNET_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to store membership information!\n"));
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static void
handle_client_membership_test (void *cls,
const struct MembershipTestRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
int ret = db->membership_test (db->cls, &req->channel_key, &req->slave_key,
GNUNET_ntohll (req->message_id));
switch (ret)
{
case GNUNET_YES:
case GNUNET_NO:
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to test membership!\n"));
}
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static int
check_client_fragment_store (void *cls,
const struct FragmentStoreRequest *req)
{
return GNUNET_OK;
}
static void
handle_client_fragment_store (void *cls,
const struct FragmentStoreRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
const struct GNUNET_MessageHeader *
msg = GNUNET_MQ_extract_nested_mh (req);
if (NULL == msg
|| ntohs (msg->size) < sizeof (struct GNUNET_MULTICAST_MessageHeader))
{
GNUNET_break (0);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Dropping invalid fragment\n"));
GNUNET_SERVICE_client_drop (client);
return;
}
int ret = db->fragment_store (db->cls, &req->channel_key,
(const struct GNUNET_MULTICAST_MessageHeader *)
msg, ntohl (req->psycstore_flags));
if (ret != GNUNET_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to store fragment\n"));
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static void
handle_client_fragment_get (void *cls,
const struct FragmentGetRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
struct SendClosure
sc = { .op_id = req->op_id,
.client = client,
.channel_key = req->channel_key,
.slave_key = req->slave_key,
.membership_test = req->do_membership_test };
int64_t ret;
uint64_t ret_frags = 0;
uint64_t first_fragment_id = GNUNET_ntohll (req->first_fragment_id);
uint64_t last_fragment_id = GNUNET_ntohll (req->last_fragment_id);
uint64_t limit = GNUNET_ntohll (req->fragment_limit);
if (0 == limit)
ret = db->fragment_get (db->cls, &req->channel_key,
first_fragment_id, last_fragment_id,
&ret_frags, send_fragment, &sc);
else
ret = db->fragment_get_latest (db->cls, &req->channel_key, limit,
&ret_frags, send_fragment, &sc);
switch (ret)
{
case GNUNET_YES:
case GNUNET_NO:
if (MEMBERSHIP_TEST_DONE == sc.membership_test)
{
switch (sc.membership_test_result)
{
case GNUNET_YES:
break;
case GNUNET_NO:
ret = GNUNET_PSYCSTORE_MEMBERSHIP_TEST_FAILED;
break;
case GNUNET_SYSERR:
ret = GNUNET_SYSERR;
break;
}
}
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to get fragment!\n"));
}
send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
GNUNET_SERVICE_client_continue (client);
}
static int
check_client_message_get (void *cls,
const struct MessageGetRequest *req)
{
return GNUNET_OK;
}
static void
handle_client_message_get (void *cls,
const struct MessageGetRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
uint16_t size = ntohs (req->header.size);
const char *method_prefix = (const char *) &req[1];
if (size < sizeof (*req) + 1
|| '\0' != method_prefix[size - sizeof (*req) - 1])
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Message get: invalid method prefix. size: %u < %u?\n",
size,
(unsigned int) (sizeof (*req) + 1));
GNUNET_break (0);
GNUNET_SERVICE_client_drop (client);
return;
}
struct SendClosure
sc = { .op_id = req->op_id,
.client = client,
.channel_key = req->channel_key,
.slave_key = req->slave_key,
.membership_test = req->do_membership_test };
int64_t ret;
uint64_t ret_frags = 0;
uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
uint64_t msg_limit = GNUNET_ntohll (req->message_limit);
uint64_t frag_limit = GNUNET_ntohll (req->fragment_limit);
/** @todo method_prefix */
if (0 == msg_limit)
ret = db->message_get (db->cls, &req->channel_key,
first_message_id, last_message_id, frag_limit,
&ret_frags, send_fragment, &sc);
else
ret = db->message_get_latest (db->cls, &req->channel_key, msg_limit,
&ret_frags, send_fragment, &sc);
switch (ret)
{
case GNUNET_YES:
case GNUNET_NO:
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to get message!\n"));
}
send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
GNUNET_SERVICE_client_continue (client);
}
static void
handle_client_message_get_fragment (void *cls,
const struct MessageGetFragmentRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
struct SendClosure
sc = { .op_id = req->op_id, .client = client,
.channel_key = req->channel_key, .slave_key = req->slave_key,
.membership_test = req->do_membership_test };
int ret = db->message_get_fragment (db->cls, &req->channel_key,
GNUNET_ntohll (req->message_id),
GNUNET_ntohll (req->fragment_offset),
&send_fragment, &sc);
switch (ret)
{
case GNUNET_YES:
case GNUNET_NO:
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to get message fragment!\n"));
}
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static void
handle_client_counters_get (void *cls,
const struct OperationRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
struct CountersResult *res;
struct GNUNET_MQ_Envelope *
env = GNUNET_MQ_msg (res, GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
int ret = db->counters_message_get (db->cls, &req->channel_key,
&res->max_fragment_id, &res->max_message_id,
&res->max_group_generation);
switch (ret)
{
case GNUNET_OK:
ret = db->counters_state_get (db->cls, &req->channel_key,
&res->max_state_message_id);
case GNUNET_NO:
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to get master counters!\n"));
}
res->result_code = htonl (ret);
res->op_id = req->op_id;
res->max_fragment_id = GNUNET_htonll (res->max_fragment_id);
res->max_message_id = GNUNET_htonll (res->max_message_id);
res->max_group_generation = GNUNET_htonll (res->max_group_generation);
res->max_state_message_id = GNUNET_htonll (res->max_state_message_id);
GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
GNUNET_SERVICE_client_continue (client);
}
struct StateModifyClosure
{
const struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
struct GNUNET_PSYC_ReceiveHandle *recv;
enum GNUNET_PSYC_MessageState msg_state;
char mod_oper;
char *mod_name;
char *mod_value;
uint32_t mod_value_size;
uint32_t mod_value_remaining;
};
static void
recv_state_message_part (void *cls,
const struct GNUNET_PSYC_MessageHeader *msg,
const struct GNUNET_MessageHeader *pmsg)
{
struct StateModifyClosure *scls = cls;
uint16_t psize;
if (NULL == msg)
{ // FIXME: error on unknown message
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"recv_state_message_part() message_id: %" PRIu64
", fragment_offset: %" PRIu64 ", flags: %u\n",
GNUNET_ntohll (msg->message_id),
GNUNET_ntohll (msg->fragment_offset),
ntohl (msg->flags));
if (NULL == pmsg)
{
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
return;
}
switch (ntohs (pmsg->type))
{
case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
{
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
break;
}
case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
{
struct GNUNET_PSYC_MessageModifier *
pmod = (struct GNUNET_PSYC_MessageModifier *) pmsg;
psize = ntohs (pmod->header.size);
uint16_t name_size = ntohs (pmod->name_size);
uint32_t value_size = ntohl (pmod->value_size);
const char *name = (const char *) &pmod[1];
const void *value = name + name_size;
if (GNUNET_PSYC_OP_SET != pmod->oper)
{ // Apply non-transient operation.
if (psize == sizeof (*pmod) + name_size + value_size)
{
db->state_modify_op (db->cls, &scls->channel_key,
pmod->oper, name, value, value_size);
}
else
{
scls->mod_oper = pmod->oper;
scls->mod_name = GNUNET_malloc (name_size);
GNUNET_memcpy (scls->mod_name, name, name_size);
scls->mod_value_size = value_size;
scls->mod_value = GNUNET_malloc (scls->mod_value_size);
scls->mod_value_remaining
= scls->mod_value_size - (psize - sizeof (*pmod) - name_size);
GNUNET_memcpy (scls->mod_value, value, value_size - scls->mod_value_remaining);
}
}
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
break;
}
case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
if (GNUNET_PSYC_OP_SET != scls->mod_oper)
{
if (scls->mod_value_remaining == 0)
{
GNUNET_break_op (0);
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
}
psize = ntohs (pmsg->size);
GNUNET_memcpy (scls->mod_value + (scls->mod_value_size - scls->mod_value_remaining),
&pmsg[1], psize - sizeof (*pmsg));
scls->mod_value_remaining -= psize - sizeof (*pmsg);
if (0 == scls->mod_value_remaining)
{
db->state_modify_op (db->cls, &scls->channel_key,
scls->mod_oper, scls->mod_name,
scls->mod_value, scls->mod_value_size);
GNUNET_free (scls->mod_name);
GNUNET_free (scls->mod_value);
scls->mod_oper = 0;
scls->mod_name = NULL;
scls->mod_value = NULL;
scls->mod_value_size = 0;
}
}
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
break;
case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
break;
case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
break;
default:
scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
}
}
static int
recv_state_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
enum GNUNET_PSYCSTORE_MessageFlags flags)
{
struct StateModifyClosure *scls = cls;
if (NULL == scls->recv)
{
scls->recv = GNUNET_PSYC_receive_create (NULL, recv_state_message_part,
scls);
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"recv_state_fragment: %" PRIu64 "\n", GNUNET_ntohll (msg->fragment_id));
struct GNUNET_PSYC_MessageHeader *
pmsg = GNUNET_PSYC_message_header_create (msg, flags);
GNUNET_PSYC_receive_message (scls->recv, pmsg);
GNUNET_free (pmsg);
return GNUNET_YES;
}
static void
handle_client_state_modify (void *cls,
const struct StateModifyRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
uint64_t message_id = GNUNET_ntohll (req->message_id);
uint64_t state_delta = GNUNET_ntohll (req->state_delta);
uint64_t ret_frags = 0;
struct StateModifyClosure
scls = { .channel_key = req->channel_key };
int ret = db->state_modify_begin (db->cls, &req->channel_key,
message_id, state_delta);
if (GNUNET_OK != ret)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Failed to begin modifying state: %d\n"), ret);
}
else
{
ret = db->message_get (db->cls, &req->channel_key,
message_id, message_id, 0,
&ret_frags, recv_state_fragment, &scls);
if (GNUNET_OK != ret)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to modify state: %d\n"), ret);
GNUNET_break (0);
}
else
{
if (GNUNET_OK != db->state_modify_end (db->cls, &req->channel_key, message_id))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to end modifying state!\n"));
GNUNET_break (0);
}
}
if (NULL != scls.recv)
{
GNUNET_PSYC_receive_destroy (scls.recv);
}
}
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static int
check_client_state_sync (void *cls,
const struct StateSyncRequest *req)
{
return GNUNET_OK;
}
/** @todo FIXME: stop processing further state sync messages after an error */
static void
handle_client_state_sync (void *cls,
const struct StateSyncRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
int ret = GNUNET_SYSERR;
const char *name = (const char *) &req[1];
uint16_t name_size = ntohs (req->name_size);
if (name_size <= 2 || '\0' != name[name_size - 1])
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Tried to set invalid state variable name!\n"));
GNUNET_break_op (0);
}
else
{
ret = GNUNET_OK;
if (req->flags & STATE_OP_FIRST)
{
ret = db->state_sync_begin (db->cls, &req->channel_key);
}
if (ret != GNUNET_OK)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to begin synchronizing state!\n"));
}
else
{
ret = db->state_sync_assign (db->cls, &req->channel_key, name,
name + ntohs (req->name_size),
ntohs (req->header.size) - sizeof (*req)
- ntohs (req->name_size));
}
if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
{
ret = db->state_sync_end (db->cls, &req->channel_key,
GNUNET_ntohll (req->max_state_message_id),
GNUNET_ntohll (req->state_hash_message_id));
if (ret != GNUNET_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to end synchronizing state!\n"));
}
}
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static void
handle_client_state_reset (void *cls,
const struct OperationRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
int ret = db->state_reset (db->cls, &req->channel_key);
if (ret != GNUNET_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to reset state!\n"));
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static void
handle_client_state_hash_update (void *cls,
const struct StateHashUpdateRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
int ret = db->state_reset (db->cls, &req->channel_key);
if (ret != GNUNET_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to reset state!\n"));
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static int
check_client_state_get (void *cls,
const struct OperationRequest *req)
{
return GNUNET_OK;
}
static void
handle_client_state_get (void *cls,
const struct OperationRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
struct SendClosure sc = { .op_id = req->op_id, .client = client };
int64_t ret = GNUNET_SYSERR;
const char *name = (const char *) &req[1];
uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
if (name_size <= 2 || '\0' != name[name_size - 1])
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Tried to get invalid state variable name!\n"));
GNUNET_break (0);
}
else
{
ret = db->state_get (db->cls, &req->channel_key, name,
&send_state_var, &sc);
if (GNUNET_NO == ret && name_size >= 5) /* min: _a_b\0 */
{
char *p, *n = GNUNET_malloc (name_size);
GNUNET_memcpy (n, name, name_size);
while (&n[1] < (p = strrchr (n, '_')) && GNUNET_NO == ret)
{
*p = '\0';
ret = db->state_get (db->cls, &req->channel_key, n,
&send_state_var, &sc);
}
GNUNET_free (n);
}
}
switch (ret)
{
case GNUNET_OK:
case GNUNET_NO:
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to get state variable!\n"));
}
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
static int
check_client_state_get_prefix (void *cls,
const struct OperationRequest *req)
{
return GNUNET_OK;
}
static void
handle_client_state_get_prefix (void *cls,
const struct OperationRequest *req)
{
struct GNUNET_SERVICE_Client *client = cls;
struct SendClosure sc = { .op_id = req->op_id, .client = client };
int64_t ret = GNUNET_SYSERR;
const char *name = (const char *) &req[1];
uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
if (name_size <= 1 || '\0' != name[name_size - 1])
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Tried to get invalid state variable name!\n"));
GNUNET_break (0);
}
else
{
ret = db->state_get_prefix (db->cls, &req->channel_key, name,
&send_state_var, &sc);
}
switch (ret)
{
case GNUNET_OK:
case GNUNET_NO:
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to get state variable!\n"));
}
send_result_code (client, req->op_id, ret, NULL);
GNUNET_SERVICE_client_continue (client);
}
/**
* A new client connected.
*
* @param cls NULL
* @param client client to add
* @param mq message queue for @a client
* @return @a client
*/
static void *
client_notify_connect (void *cls,
struct GNUNET_SERVICE_Client *client,
struct GNUNET_MQ_Handle *mq)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
return client;
}
/**
* Called whenever a client is disconnected.
* Frees our resources associated with that client.
*
* @param cls closure
* @param client identification of the client
* @param app_ctx must match @a client
*/
static void
client_notify_disconnect (void *cls,
struct GNUNET_SERVICE_Client *client,
void *app_ctx)
{
}
/**
* Initialize the PSYCstore service.
*
* @param cls Closure.
* @param server The initialized server.
* @param c Configuration to use.
*/
static void
run (void *cls,
const struct GNUNET_CONFIGURATION_Handle *c,
struct GNUNET_SERVICE_Handle *svc)
{
cfg = c;
service = svc;
/* Loading database plugin */
char *database;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
&database))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
"psycstore",
"database");
}
else
{
GNUNET_asprintf (&db_lib_name,
"libgnunet_plugin_psycstore_%s",
database);
db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
GNUNET_free (database);
}
if (NULL == db)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Could not load database backend `%s'\n",
db_lib_name);
GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
return;
}
stats = GNUNET_STATISTICS_create ("psycstore", cfg);
GNUNET_SCHEDULER_add_shutdown (shutdown_task,
NULL);
}
/**
* Define "main" method using service macro.
*/
GNUNET_SERVICE_MAIN
("psycstore",
GNUNET_SERVICE_OPTION_NONE,
run,
client_notify_connect,
client_notify_disconnect,
NULL,
GNUNET_MQ_hd_fixed_size (client_membership_store,
GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
struct MembershipStoreRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_membership_test,
GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
struct MembershipTestRequest,
NULL),
GNUNET_MQ_hd_var_size (client_fragment_store,
GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE,
struct FragmentStoreRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_fragment_get,
GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
struct FragmentGetRequest,
NULL),
GNUNET_MQ_hd_var_size (client_message_get,
GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET,
struct MessageGetRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_message_get_fragment,
GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
struct MessageGetFragmentRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_counters_get,
GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
struct OperationRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_state_modify,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY,
struct StateModifyRequest,
NULL),
GNUNET_MQ_hd_var_size (client_state_sync,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC,
struct StateSyncRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_state_reset,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
struct OperationRequest,
NULL),
GNUNET_MQ_hd_fixed_size (client_state_hash_update,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
struct StateHashUpdateRequest,
NULL),
GNUNET_MQ_hd_var_size (client_state_get,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET,
struct OperationRequest,
NULL),
GNUNET_MQ_hd_var_size (client_state_get_prefix,
GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX,
struct OperationRequest,
NULL));
/* end of gnunet-service-psycstore.c */
|