-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.c
884 lines (739 loc) · 27.6 KB
/
api.c
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
/*
* Copyright (c) 2017 Sam Kumar <[email protected]>
* Copyright (c) 2017 Michael P Andersen <[email protected]>
* Copyright (c) 2017 University of California, Berkeley
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University of California, Berkeley nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "api.h"
#include "daemon.h"
#include "errors.h"
#include "frame.h"
#include "objects.h"
#include "ponames.h"
#include "utils.h"
/* Macros that allocate memory in the current stack frame. */
#define BW2_REQUEST_ADD_AUTOCHAIN(PARAMPTR, REQPTR) \
struct bw2_header autochain; \
if ((PARAMPTR)->autochain) { \
bw2_KVInit(&autochain, "autochain", "true", 0); \
bw2_appendKV((REQPTR), &autochain); \
}
#define BW2_REQUEST_ADD_EXPIRY(PARAMPTR, REQPTR) \
struct bw2_header expiry; \
union { \
char timeabsbuf[BW2_FRAME_MAX_TIMESTR_LENGTH + 1]; \
char timedeltabuf[BW2_FRAME_MAX_TIME_DIGITS + 4]; \
} timebuf; \
if ((PARAMPTR)->expiry != NULL) { \
bw2_format_time_rfc3339(timebuf.timeabsbuf, sizeof(timebuf.timeabsbuf), (PARAMPTR)->expiry); \
bw2_KVInit(&expiry, "expiry", timebuf.timeabsbuf, 0); \
bw2_appendKV(REQPTR, &expiry); \
} else if ((PARAMPTR)->expiryDelta != 0) { \
bw2_format_timedelta(timebuf.timedeltabuf, sizeof(timebuf.timedeltabuf), (PARAMPTR)->expiryDelta); \
bw2_KVInit(&expiry, "expirydelta", timebuf.timedeltabuf, 0); \
bw2_appendKV(REQPTR, &expiry); \
}
#define BW2_REQUEST_ADD_URI(PARAMPTR, REQPTR) \
struct bw2_header uri; \
bw2_KVInit(&uri, "uri", (PARAMPTR)->uri, 0); \
bw2_appendKV(REQPTR, &uri);
#define BW2_REQUEST_ADD_PRIMARY_ACCESS_CHAIN(PARAMPTR, REQPTR) \
struct bw2_header pac; \
if ((PARAMPTR)->primaryAccessChain != NULL) { \
bw2_KVInit(&pac, "primary_access_chain", (PARAMPTR)->primaryAccessChain->dotchainhash, (PARAMPTR)->primaryAccessChain->dotchainhashlen); \
bw2_appendKV(REQPTR, &pac); \
}
#define BW2_REQUEST_ADD_ROUTING_OBJECTS(PARAMPTR, REQPTR) \
if ((PARAMPTR)->routingObjects != NULL) { \
bw2_appendRO(REQPTR, (PARAMPTR)->routingObjects); \
}
#define BW2_REQUEST_ADD_PAYLOAD_OBJECTS(PARAMPTR, REQPTR) \
if ((PARAMPTR)->payloadObjects != NULL) { \
bw2_appendPO(REQPTR, (PARAMPTR)->payloadObjects); \
}
#define BW2_REQUEST_ADD_ELABORATE_PAC(PARAMPTR, REQPTR) \
struct bw2_header elaboratePAC; \
bw2_KVInit(&elaboratePAC, "elaborate_pac", (PARAMPTR)->elaboratePAC != NULL ? (PARAMPTR)->elaboratePAC : BW2_ELABORATE_DEFAULT, 0); \
bw2_appendKV(REQPTR, &elaboratePAC);
#define BW2_REQUEST_ADD_VERIFY(PARAMPTR, REQPTR) \
struct bw2_header doverify; \
bw2_KVInit(&doverify, "doverify", (PARAMPTR)->doNotVerify ? "false" : "true", 0); \
bw2_appendKV(REQPTR, &doverify);
#define BW2_REQUEST_ADD_PERSIST(PARAMPTR, REQPTR) \
struct bw2_header persist; \
bw2_KVInit(&persist, "persist", (PARAMPTR)->persist ? "true" : "false", 0); \
bw2_appendKV(REQPTR, &persist);
#define BW2_REQUEST_ADD_LEAVE_PACKED(PARAMPTR, REQPTR) \
struct bw2_header leavePacked; \
if (!(PARAMPTR)->leavePacked) { \
bw2_KVInit(&leavePacked, "unpack", "true", 0); \
bw2_appendKV(REQPTR, &leavePacked); \
}
#define BW2_REQUEST_ADD_CONTACT(PARAMPTR, REQPTR) \
struct bw2_header contact; \
if ((PARAMPTR)->contact != NULL) { \
bw2_KVInit(&contact, "contact", (PARAMPTR)->contact, 0); \
bw2_appendKV(REQPTR, &contact); \
}
#define BW2_REQUEST_ADD_COMMENT(PARAMPTR, REQPTR) \
struct bw2_header comment; \
if ((PARAMPTR)->comment != NULL) { \
bw2_KVInit(&comment, "comment", (PARAMPTR)->comment, 0); \
bw2_appendKV(REQPTR, &comment); \
}
#define BW2_REQUEST_ADD_REVOKERS(PARAMPTR, REQPTR) \
struct bw2_vkHash* currrevoker; \
for (currrevoker = (PARAMPTR)->revokers; currrevoker != NULL; currrevoker = currrevoker->next) { \
bw2_KVInit(&currrevoker->hdr, "revoker", currrevoker->vkhash, currrevoker->vkhashlen); \
bw2_appendKV(REQPTR, &currrevoker->hdr); \
}
#define BW2_REQUEST_ADD_OMIT_CREATION_DATE(PARAMPTR, REQPTR) \
struct bw2_header omitcreationdate; \
if ((PARAMPTR)->omitCreationDate) { \
bw2_KVInit(&omitcreationdate, "omitcreationdate", (PARAMPTR)->omitCreationDate ? "true" : "false", 0); \
bw2_appendKV(REQPTR, &omitcreationdate); \
}
#define BW2_REQUEST_ADD_TTL(PARAMPTR, REQPTR) \
struct bw2_header ttl; \
char ttlbuf[BW2_FRAME_MAX_TTL_LENGTH + 1]; \
snprintf(ttlbuf, sizeof(ttlbuf), "%" PRIu8, (PARAMPTR)->ttl); \
bw2_KVInit(&ttl, "ttl", ttlbuf, 0); \
bw2_appendKV(REQPTR, &ttl);
#define BW2_REQUEST_ADD_TO(PARAMPTR, REQPTR) \
struct bw2_header to; \
bw2_KVInit(&to, "to", (PARAMPTR)->to->vkhash, (PARAMPTR)->to->vkhashlen); \
bw2_appendKV(REQPTR, &to);
#define BW2_REQUEST_ADD_IS_PERMISSION(PARAMPTR, REQPTR) \
struct bw2_header ispermission; \
bw2_KVInit(&ispermission, "ispermission", (PARAMPTR)->isPermission ? "true" : "false", 0); \
bw2_appendKV(REQPTR, &ispermission);
#define BW2_REQUEST_ADD_ACCESS_PERMISSIONS(PARAMPTR, REQPTR) \
struct bw2_header accesspermissions; \
bw2_KVInit(&accesspermissions, "accesspermissions", (PARAMPTR)->accessPermissions, 0); \
bw2_appendKV(REQPTR, &accesspermissions);
#define BW2_REQUEST_ADD_UNELABORATE(PARAMPTR, REQPTR) \
struct bw2_header unelaborate; \
bw2_KVInit(&unelaborate, "unelaborate", (PARAMPTR)->unElaborate ? "true" : "false", 0); \
bw2_appendKV(REQPTR, &unelaborate);
#define BW2_REQUEST_ADD_DOTS(PARAMPTR, REQPTR) \
struct bw2_dotHash* currdot; \
for (currdot = (PARAMPTR)->dots; currdot != NULL; currdot = currdot->next) { \
bw2_KVInit(&currdot->hdr, "dot", currdot->dothash, currdot->dothashlen); \
bw2_appendKV(REQPTR, &currdot->hdr); \
}
int32_t _bw2_getSeqNo(struct bw2_client* client) {
int32_t seqno;
bw2_mutexLock(&client->seqnolock);
seqno = client->curseqno;
client->curseqno = (client->curseqno + 1) & (int32_t) 0x7FFFFFFF;
bw2_mutexUnlock(&client->seqnolock);
return seqno;
}
int bw2_clientInit(struct bw2_client* client) {
int rv;
memset(client, 0x00, sizeof(struct bw2_client));
rv = bw2_mutexInit(&client->outlock);
if (rv != 0) {
goto error1;
}
rv = bw2_mutexInit(&client->reqslock);
if (rv != 0) {
goto error2;
}
rv = bw2_mutexInit(&client->seqnolock);
if (rv != 0) {
goto error3;
}
return 0;
error3:
bw2_mutexDestroy(&client->reqslock);
error2:
bw2_mutexDestroy(&client->outlock);
error1:
return BW2_ERROR_SYNCHRONIZATION;
}
bool bw2_isConnected(struct bw2_client* client) {
bool rv;
bw2_mutexLock(&client->outlock);
rv = client->connected;
bw2_mutexUnlock(&client->outlock);
return rv;
}
struct bw2_daemon_info {
struct bw2_client* client;
size_t heapsize;
bool has_heap;
};
void* _bw2_daemon_trampoline(void* arg) {
struct bw2_daemon_info* info = arg;
char* frameheap;
struct bw2_client* client = info->client;
size_t heapsize = info->heapsize;
if (info->has_heap) {
/* The daemon args are stored in the frame heap itself. */
frameheap = arg;
} else {
/* No heap was provided, so we fall back to malloc. */
frameheap = NULL;
free(info);
}
bw2_daemon(client, frameheap, heapsize);
return NULL;
}
int bw2_connect(struct bw2_client* client, const struct sockaddr* addr, socklen_t addrlen, char* frameheap, size_t heapsize, char* threadstack, size_t stacksize) {
int sock = socket(addr->sa_family, SOCK_STREAM, 0);
if (sock == -1) {
return BW2_ERROR_SYSTEM_RESOURCE_UNAVAILABLE;
}
int rv = connect(sock, addr, addrlen);
if (rv != 0) {
rv = BW2_ERROR_CONNECTION_LOST;
goto closeanderror;
}
client->connfd = sock;
struct bw2_frame frame;
rv = bw2_readFrame(&frame, frameheap, heapsize, sock);
if (rv != 0) {
goto closeanderror;
}
if (memcmp(frame.cmd, BW2_FRAME_CMD_HELLO, 4) != 0) {
rv = BW2_ERROR_UNEXPECTED_FRAME;
goto closeanderror;
}
struct bw2_header* versionhdr = bw2_getFirstHeader(&frame, "version");
if (versionhdr == NULL) {
rv = BW2_ERROR_MISSING_HEADER;
goto closeanderror;
}
bw2_logf("Connected to BOSSWAVE router version %.*s\n", (int) versionhdr->len, versionhdr->value);
if (frameheap == NULL) {
/* The frame was actually malloc'd, so we need to free it... */
bw2_frameFreeResources(&frame);
}
struct bw2_daemon_info* dargs;
if (frameheap != NULL) {
dargs = (struct bw2_daemon_info*) frameheap;
dargs->has_heap = true;
} else {
dargs = malloc(sizeof(struct bw2_daemon_info));
dargs->has_heap = false;
}
dargs->client = client;
dargs->heapsize = heapsize;
rv = bw2_threadCreate(threadstack, stacksize, _bw2_daemon_trampoline, dargs, NULL);
if (rv != 0) {
goto closeanderror;
}
client->connected = true;
return 0;
closeanderror:
close(sock);
return rv;
}
int bw2_disconnect(struct bw2_client* client) {
bw2_mutexLock(&client->outlock);
if (client->connected) {
client->connected = false;
close(client->connfd);
}
bw2_mutexUnlock(&client->outlock);
return 0;
}
bool _bw2_simpleReq_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
(void) final;
(void) ctx;
if (frame != NULL) {
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return true;
}
bool _bw2_setEntity_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
(void) final;
if (frame != NULL) {
struct bw2_vkHash* vkhash = ctx;
if (vkhash != NULL) {
struct bw2_header* vkhdr = bw2_getFirstHeader(frame, "vk");
if (vkhdr != NULL) {
bw2_vkHash_set(vkhash, vkhdr->value, vkhdr->len);
}
}
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return true;
}
int bw2_setEntity(struct bw2_client* client, char* entity, size_t entitylen, struct bw2_vkHash* vkhash) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_SET_ENTITY, _bw2_getSeqNo(client));
struct bw2_payloadobj po;
bw2_POInit(&po, BW2_PO_NUM_ROENTITYWKEY, entity, entitylen);
bw2_appendPO(&req, &po);
struct bw2_reqctx reqctx;
bw2_reqctxInit(&reqctx, _bw2_setEntity_cb, vkhash);
int rv = bw2_transact(client, &req, &reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&reqctx);
rv = reqctx.rv;
done:
bw2_reqctxDestroy(&reqctx);
return rv;
}
int bw2_publish(struct bw2_client* client, struct bw2_publishParams* p) {
struct bw2_frame req;
if (p->persist) {
bw2_frameInit(&req, BW2_FRAME_CMD_PERSIST, _bw2_getSeqNo(client));
} else {
bw2_frameInit(&req, BW2_FRAME_CMD_PUBLISH, _bw2_getSeqNo(client));
}
BW2_REQUEST_ADD_AUTOCHAIN(p, &req)
BW2_REQUEST_ADD_EXPIRY(p, &req)
BW2_REQUEST_ADD_URI(p, &req)
BW2_REQUEST_ADD_PRIMARY_ACCESS_CHAIN(p, &req)
BW2_REQUEST_ADD_ROUTING_OBJECTS(p, &req)
BW2_REQUEST_ADD_PAYLOAD_OBJECTS(p, &req)
BW2_REQUEST_ADD_ELABORATE_PAC(p, &req)
BW2_REQUEST_ADD_VERIFY(p, &req)
BW2_REQUEST_ADD_PERSIST(p, &req)
struct bw2_reqctx reqctx;
bw2_reqctxInit(&reqctx, _bw2_simpleReq_cb, NULL);
int rv = bw2_transact(client, &req, &reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&reqctx);
rv = reqctx.rv;
done:
bw2_reqctxDestroy(&reqctx);
return rv;
}
void _bw2_simplemsg_from_frame(struct bw2_simpleMessage* sm, struct bw2_frame* frame) {
struct bw2_header* fromhdr = bw2_getFirstHeader(frame, "from");
struct bw2_header* urihdr = bw2_getFirstHeader(frame, "uri");
if (fromhdr != NULL && urihdr != NULL) {
sm->from = fromhdr->value;
sm->from_len = fromhdr->len;
sm->uri = urihdr->value;
sm->uri_len = urihdr->len;
sm->error = BW2_ERROR_MISSING_HEADER;
} else {
sm->from = NULL;
sm->from_len = 0;
sm->uri = NULL;
sm->uri_len = 0;
sm->error = 0;
}
sm->pos = frame->pos;
sm->ros = frame->ros;
}
bool _bw2_simpleMessage_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
bool gotResp;
bw2_reqctxSignalled(rctx, &gotResp);
if (gotResp) {
/* Deliver this frame to the application via the on_message function. */
struct bw2_simplemsg_ctx* smctx = ctx;
if (frame == NULL) {
if (smctx->on_message != NULL) {
smctx->on_message(NULL, final, rctx->rv, smctx->ctx);
}
return true;
} else if (smctx->on_message != NULL) {
struct bw2_simpleMessage sm;
_bw2_simplemsg_from_frame(&sm, frame);
return smctx->on_message(&sm, final, 0, smctx->ctx);
}
return false;
} else {
/* This should be the RESP frame. */
if (frame != NULL) {
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return (rctx->rv != 0);
}
}
struct bw2_subscribe_ctx {
struct bw2_simplemsg_ctx* smctx;
struct bw2_subscriptionHandle* handle;
};
/* This callback is used for the first frame after a subscribe message. */
bool _bw2_subscribe_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
struct bw2_subscribe_ctx* sparams = ctx;
/* This should be the RESP frame. */
if (frame != NULL) {
rctx->rv = bw2_frameMustResponse(frame);
}
if (rctx->rv == 0 && sparams->handle != NULL) {
struct bw2_header* handlehdr = bw2_getFirstHeader(frame, "handle");
if (handlehdr == NULL) {
rctx->rv = BW2_ERROR_MISSING_HEADER;
} else {
bw2_subscriptionHandle_set(sparams->handle, handlehdr->value, handlehdr->len);
}
}
/* Future frames should be handled by the Simple Message. */
rctx->onframe = _bw2_simpleMessage_cb;
rctx->ctx = sparams->smctx;
bw2_reqctxSignal(rctx);
return (rctx->rv != 0);
}
int bw2_subscribe(struct bw2_client* client, struct bw2_subscribeParams* p, struct bw2_simplemsg_ctx* subctx, struct bw2_subscriptionHandle* handle) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_SUBSCRIBE, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_AUTOCHAIN(p, &req)
BW2_REQUEST_ADD_EXPIRY(p, &req)
BW2_REQUEST_ADD_URI(p, &req)
BW2_REQUEST_ADD_PRIMARY_ACCESS_CHAIN(p, &req)
BW2_REQUEST_ADD_ROUTING_OBJECTS(p, &req)
BW2_REQUEST_ADD_ELABORATE_PAC(p, &req)
BW2_REQUEST_ADD_LEAVE_PACKED(p, &req)
BW2_REQUEST_ADD_VERIFY(p, &req)
struct bw2_subscribe_ctx sparams;
sparams.smctx = subctx;
sparams.handle = handle;
bw2_reqctxInit(&subctx->reqctx, _bw2_subscribe_cb, &sparams);
int rv = bw2_transact(client, &req, &subctx->reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&subctx->reqctx);
done:
bw2_reqctxDestroy(&subctx->reqctx);
return subctx->reqctx.rv;
}
int bw2_query(struct bw2_client* client, struct bw2_queryParams* p, struct bw2_simplemsg_ctx* qctx) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_QUERY, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_AUTOCHAIN(p, &req)
BW2_REQUEST_ADD_EXPIRY(p, &req)
BW2_REQUEST_ADD_URI(p, &req)
BW2_REQUEST_ADD_PRIMARY_ACCESS_CHAIN(p, &req)
BW2_REQUEST_ADD_ROUTING_OBJECTS(p, &req)
BW2_REQUEST_ADD_ELABORATE_PAC(p, &req)
BW2_REQUEST_ADD_LEAVE_PACKED(p, &req)
BW2_REQUEST_ADD_VERIFY(p, &req)
bw2_reqctxInit(&qctx->reqctx, _bw2_simpleMessage_cb, qctx);
bw2_transact(client, &req, &qctx->reqctx);
bw2_reqctxWait(&qctx->reqctx);
bw2_reqctxDestroy(&qctx->reqctx);
return qctx->reqctx.rv;
}
bool _bw2_list_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
bool gotResp;
bw2_reqctxSignalled(rctx, &gotResp);
if (gotResp) {
/* Deliver this frame to the application via the on_message function. */
struct bw2_chararr_ctx* lctx = ctx;
if (frame == NULL) {
if (lctx->on_message != NULL) {
lctx->on_message(NULL, 0, final, rctx->rv, lctx->ctx);
}
return true;
} else if (lctx->on_message != NULL) {
struct bw2_header* childhdr = bw2_getFirstHeader(frame, "child");
char* childuri;
size_t childuri_len;
if (childhdr == NULL) {
childuri = NULL;
childuri_len = 0;
} else {
childuri = childhdr->value;
childuri_len = childhdr->len;
}
return lctx->on_message(childuri, childuri_len, final, 0, lctx->ctx);
}
return false;
} else {
/* This should be the RESP frame. */
if (frame != NULL) {
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return (rctx->rv != 0);
}
}
int bw2_list(struct bw2_client* client, struct bw2_listParams* p, struct bw2_chararr_ctx* lctx) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_LIST, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_AUTOCHAIN(p, &req)
BW2_REQUEST_ADD_EXPIRY(p, &req)
BW2_REQUEST_ADD_URI(p, &req)
BW2_REQUEST_ADD_PRIMARY_ACCESS_CHAIN(p, &req)
BW2_REQUEST_ADD_ROUTING_OBJECTS(p, &req)
BW2_REQUEST_ADD_ELABORATE_PAC(p, &req)
BW2_REQUEST_ADD_VERIFY(p, &req)
bw2_reqctxInit(&lctx->reqctx, _bw2_list_cb, lctx);
int rv = bw2_transact(client, &req, &lctx->reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&lctx->reqctx);
done:
bw2_reqctxDestroy(&lctx->reqctx);
return lctx->reqctx.rv;
}
struct bw2_createDOT_ctx {
struct bw2_dotHash* dothash;
struct bw2_dot* dot;
};
bool _bw2_createDOT_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
(void) final;
if (frame != NULL) {
struct bw2_createDOT_ctx* cdctx = ctx;
if (cdctx->dothash != NULL) {
struct bw2_header* hashhdr = bw2_getFirstHeader(frame, "hash");
if (hashhdr != NULL) {
bw2_dotHash_set(cdctx->dothash, hashhdr->value, hashhdr->len);
}
}
if (cdctx->dot != NULL) {
struct bw2_payloadobj* dotpo = frame->pos;
if (dotpo != NULL) {
bw2_dot_set(cdctx->dot, dotpo->po, dotpo->polen);
}
}
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return true;
}
int bw2_createDOT(struct bw2_client* client, struct bw2_createDOTParams* p, struct bw2_dotHash* dothash, struct bw2_dot* dot) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_MAKE_DOT, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_EXPIRY(p, &req)
BW2_REQUEST_ADD_CONTACT(p, &req)
BW2_REQUEST_ADD_COMMENT(p, &req)
BW2_REQUEST_ADD_REVOKERS(p, &req)
BW2_REQUEST_ADD_OMIT_CREATION_DATE(p, &req)
BW2_REQUEST_ADD_TTL(p, &req)
BW2_REQUEST_ADD_TO(p, &req)
BW2_REQUEST_ADD_IS_PERMISSION(p, &req)
if (p->isPermission) {
return BW2_ERROR_OPERATION_NOT_SUPPORTED;
}
BW2_REQUEST_ADD_URI(p, &req)
BW2_REQUEST_ADD_ACCESS_PERMISSIONS(p, &req)
struct bw2_createDOT_ctx cdctx;
cdctx.dothash = dothash;
cdctx.dot = dot;
struct bw2_reqctx reqctx;
bw2_reqctxInit(&reqctx, _bw2_createDOT_cb, &cdctx);
int rv = bw2_transact(client, &req, &reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&reqctx);
rv = reqctx.rv;
done:
bw2_reqctxDestroy(&reqctx);
return reqctx.rv;
}
struct bw2_createEntity_ctx {
struct bw2_vkHash* vkhash;
struct bw2_vk* vk;
};
bool _bw2_createEntity_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
(void) final;
if (frame != NULL) {
struct bw2_createEntity_ctx* cectx = ctx;
if (cectx->vkhash != NULL) {
struct bw2_header* vkhdr = bw2_getFirstHeader(frame, "vk");
if (vkhdr != NULL) {
bw2_vkHash_set(cectx->vkhash, vkhdr->value, vkhdr->len);
}
}
if (cectx->vk != NULL) {
struct bw2_payloadobj* vkpo = frame->pos;
if (vkpo != NULL) {
bw2_vk_set(cectx->vk, vkpo->po, vkpo->polen);
}
}
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return true;
}
int bw2_createEntity(struct bw2_client* client, struct bw2_createEntityParams* p, struct bw2_vkHash* vkhash, struct bw2_vk* vk) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_MAKE_ENTITY, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_EXPIRY(p, &req)
BW2_REQUEST_ADD_CONTACT(p, &req)
BW2_REQUEST_ADD_COMMENT(p, &req)
BW2_REQUEST_ADD_REVOKERS(p, &req)
BW2_REQUEST_ADD_OMIT_CREATION_DATE(p, &req)
struct bw2_createEntity_ctx cectx;
cectx.vkhash = vkhash;
cectx.vk = vk;
struct bw2_reqctx reqctx;
bw2_reqctxInit(&reqctx, _bw2_createEntity_cb, &cectx);
int rv = bw2_transact(client, &req, &reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&reqctx);
rv = reqctx.rv;
done:
bw2_reqctxDestroy(&reqctx);
return reqctx.rv;
}
bool _bw2_createDOTChain_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
(void) final;
if (frame != NULL) {
struct bw2_dotChainHash* dotchainhash = ctx;
if (dotchainhash != NULL) {
struct bw2_header* hashhdr = bw2_getFirstHeader(frame, "hash");
if (hashhdr != NULL) {
bw2_dotChainHash_set(dotchainhash, hashhdr->value, hashhdr->len);
}
}
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return true;
}
int bw2_createDOTChain(struct bw2_client* client, struct bw2_createDOTChainParams* p, struct bw2_dotChainHash* dotchainhash) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_MAKE_CHAIN, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_IS_PERMISSION(p, &req)
BW2_REQUEST_ADD_UNELABORATE(p, &req)
BW2_REQUEST_ADD_DOTS(p, &req)
struct bw2_reqctx reqctx;
bw2_reqctxInit(&reqctx, _bw2_createDOTChain_cb, dotchainhash);
int rv = bw2_transact(client, &req, &reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&reqctx);
rv = reqctx.rv;
done:
bw2_reqctxDestroy(&reqctx);
return reqctx.rv;
}
bool _bw2_buildChain_cb(struct bw2_frame* frame, bool final, struct bw2_reqctx* rctx, void* ctx) {
bool gotResp;
bw2_reqctxSignalled(rctx, &gotResp);
if (gotResp) {
/* Deliver this frame to the application via the on_chain function. */
struct bw2_simplechain_ctx* scctx = ctx;
if (frame == NULL) {
if (scctx->on_chain != NULL) {
scctx->on_chain(NULL, final, rctx->rv, scctx->ctx);
}
return true;
} else if (scctx->on_chain != NULL) {
struct bw2_header* hashhdr = bw2_getFirstHeader(frame, "hash");
if (hashhdr != NULL) {
struct bw2_simpleChain sc;
struct bw2_header* permissionshdr = bw2_getFirstHeader(frame, "permissions");
struct bw2_header* tohdr = bw2_getFirstHeader(frame, "to");
struct bw2_header* urihdr = bw2_getFirstHeader(frame, "uri");
struct bw2_payloadobj* contentpo = frame->pos;
sc.hash = hashhdr->value;
sc.hash_len = hashhdr->len;
if (permissionshdr != NULL) {
sc.permissions = permissionshdr->value;
sc.permissions_len = permissionshdr->len;
} else {
sc.permissions = NULL;
sc.permissions_len = 0;
}
if (tohdr != NULL) {
sc.to = tohdr->value;
sc.to_len = tohdr->len;
} else {
sc.to = NULL;
sc.to_len = 0;
}
if (urihdr != NULL) {
sc.uri = urihdr->value;
sc.uri_len = urihdr->len;
} else {
sc.uri = NULL;
sc.uri_len = 0;
}
if (contentpo != NULL) {
sc.content = contentpo->po;
sc.content_len = contentpo->polen;
} else {
sc.content = NULL;
sc.content_len = 0;
}
return scctx->on_chain(&sc, final, 0, scctx->ctx);
} else if (final) {
return scctx->on_chain(NULL, final, 0, scctx->ctx);
}
}
return false;
} else {
/* This should be the RESP frame. */
if (frame != NULL) {
rctx->rv = bw2_frameMustResponse(frame);
}
bw2_reqctxSignal(rctx);
return (rctx->rv != 0);
}
}
int bw2_buildChain(struct bw2_client* client, struct bw2_buildChainParams* p, struct bw2_simplechain_ctx* scctx) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_BUILD_CHAIN, _bw2_getSeqNo(client));
BW2_REQUEST_ADD_URI(p, &req)
BW2_REQUEST_ADD_TO(p, &req)
BW2_REQUEST_ADD_ACCESS_PERMISSIONS(p, &req)
bw2_reqctxInit(&scctx->reqctx, _bw2_buildChain_cb, NULL);
int rv = bw2_transact(client, &req, &scctx->reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&scctx->reqctx);
done:
bw2_reqctxDestroy(&scctx->reqctx);
return scctx->reqctx.rv;
}
int bw2_unsubscribe(struct bw2_client* client, struct bw2_subscriptionHandle* handle) {
struct bw2_frame req;
bw2_frameInit(&req, BW2_FRAME_CMD_UNSUBSCRIBE, _bw2_getSeqNo(client));
struct bw2_header handlehdr;
bw2_KVInit(&handlehdr, "handle", handle->handle, handle->handlelen);
bw2_appendKV(&req, &handlehdr);
struct bw2_reqctx reqctx;
bw2_reqctxInit(&reqctx, _bw2_simpleReq_cb, NULL);
int rv = bw2_transact(client, &req, &reqctx);
if (rv != 0) {
goto done;
}
bw2_reqctxWait(&reqctx);
done:
bw2_reqctxDestroy(&reqctx);
return reqctx.rv;
}