forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libplugin.c
2138 lines (1816 loc) · 56.8 KB
/
libplugin.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
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
#include "config.h"
#include <bitcoin/chainparams.h>
#include <bitcoin/privkey.h>
#include <ccan/io/io.h>
#include <ccan/json_out/json_out.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/daemon.h>
#include <common/json_filter.h>
#include <common/json_parse_simple.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <common/route.h>
#include <errno.h>
#include <plugins/libplugin.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#define READ_CHUNKSIZE 4096
bool deprecated_apis;
struct plugin_timer {
struct timer timer;
void (*cb)(void *cb_arg);
void *cb_arg;
};
struct rpc_conn {
int fd;
MEMBUF(char) mb;
};
/* We can have more than one of these pending at once. */
struct jstream {
struct list_node list;
struct json_stream *js;
};
struct plugin {
/* lightningd interaction */
struct io_conn *stdin_conn;
struct io_conn *stdout_conn;
bool developer;
/* to append to all our command ids */
const char *id;
/* To read from lightningd */
char *buffer;
size_t used, len_read;
jsmn_parser parser;
jsmntok_t *toks;
/* To write to lightningd */
struct list_head js_list;
/* Asynchronous RPC interaction */
struct io_conn *io_rpc_conn;
struct list_head rpc_js_list;
char *rpc_buffer;
size_t rpc_used, rpc_len_read, rpc_read_offset;
jsmn_parser rpc_parser;
jsmntok_t *rpc_toks;
/* Tracking async RPC requests */
STRMAP(struct out_req *) out_reqs;
u64 next_outreq_id;
/* Synchronous RPC interaction */
struct rpc_conn *rpc_conn;
/* Plugin information details */
enum plugin_restartability restartability;
const struct plugin_command *commands;
size_t num_commands;
const struct plugin_notification *notif_subs;
size_t num_notif_subs;
const struct plugin_hook *hook_subs;
size_t num_hook_subs;
struct plugin_option *opts;
/* Anything special to do at init ? */
const char *(*init)(struct plugin *p,
const char *buf, const jsmntok_t *);
/* Has the manifest been sent already ? */
bool manifested;
/* Has init been received ? */
bool initialized;
/* Are we exiting? */
bool exiting;
/* Map from json command names to usage strings: we don't put this inside
* struct json_command as it's good practice to have those const. */
STRMAP(const char *) usagemap;
/* Timers */
struct timers timers;
size_t in_timer;
/* Feature set for lightningd */
struct feature_set *our_features;
/* Features we want to add to lightningd */
const struct feature_set *desired_features;
/* Location of the RPC filename in case we need to defer RPC
* initialization or need to recover from a disconnect. */
const char *rpc_location;
const char **notif_topics;
size_t num_notif_topics;
/* Lets them remove ptrs from leak detection. */
void (*mark_mem)(struct plugin *plugin, struct htable *memtable);
};
/* command_result is mainly used as a compile-time check to encourage you
* to return as soon as you get one (and not risk use-after-free of command).
* Here we use two values: complete (cmd freed) and pending (still going) */
struct command_result {
char c;
};
static struct command_result complete, pending;
struct command_result *command_param_failed(void)
{
return &complete;
}
struct command_result *command_done(void)
{
return &complete;
}
struct json_filter **command_filter_ptr(struct command *cmd)
{
return &cmd->filter;
}
static void ld_send(struct plugin *plugin, struct json_stream *stream)
{
struct jstream *jstr = tal(plugin, struct jstream);
jstr->js = tal_steal(jstr, stream);
list_add_tail(&plugin->js_list, &jstr->list);
io_wake(plugin);
}
static void ld_rpc_send(struct plugin *plugin, struct json_stream *stream)
{
struct jstream *jstr = tal(plugin, struct jstream);
jstr->js = tal_steal(jstr, stream);
list_add_tail(&plugin->rpc_js_list, &jstr->list);
io_wake(plugin->io_rpc_conn);
}
/* When cmd for request is gone, we use this as noop callback */
static struct command_result *ignore_cb(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg)
{
return command_done();
}
static void disable_request_cb(struct command *cmd, struct out_req *out)
{
out->errcb = NULL;
out->cb = ignore_cb;
/* Called because cmd got free'd */
out->cmd = NULL;
}
const char *json_id_prefix(const tal_t *ctx, const struct command *cmd)
{
if (!cmd)
return "";
/* Notifications have no cmd->id, use methodname */
if (!cmd->id)
return tal_fmt(ctx, "%s/", cmd->methodname);
/* Strip quotes! */
if (strstarts(cmd->id, "\"")) {
assert(strlen(cmd->id) >= 2);
assert(strends(cmd->id, "\""));
return tal_fmt(ctx, "%.*s/",
(int)strlen(cmd->id) - 2, cmd->id + 1);
}
return tal_fmt(ctx, "%s/", cmd->id);
}
static const char *append_json_id(const tal_t *ctx,
struct plugin *plugin,
const char *method,
const char *prefix)
{
return tal_fmt(ctx, "\"%s%s:%s#%"PRIu64"\"",
prefix, plugin->id, method, plugin->next_outreq_id++);
}
static void destroy_out_req(struct out_req *out_req, struct plugin *plugin)
{
strmap_del(&plugin->out_reqs, out_req->id, NULL);
}
/* FIXME: Move lightningd/jsonrpc to common/ ? */
struct out_req *
jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
const char *method,
const char *id_prefix,
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
struct command_result *(*errcb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
void *arg)
{
struct out_req *out;
out = tal(cmd, struct out_req);
out->id = append_json_id(out, plugin, method, id_prefix);
out->cmd = cmd;
out->cb = cb;
out->errcb = errcb;
out->arg = arg;
strmap_add(&plugin->out_reqs, out->id, out);
tal_add_destructor2(out, destroy_out_req, plugin);
/* If command goes away, don't call callbacks! */
if (out->cmd)
tal_add_destructor2(out->cmd, disable_request_cb, out);
out->js = new_json_stream(NULL, cmd, NULL);
json_object_start(out->js, NULL);
json_add_string(out->js, "jsonrpc", "2.0");
json_add_id(out->js, out->id);
json_add_string(out->js, "method", method);
if (out->errcb)
json_object_start(out->js, "params");
return out;
}
const struct feature_set *plugin_feature_set(const struct plugin *p)
{
return p->our_features;
}
static void jsonrpc_finish_and_send(struct plugin *p, struct json_stream *js)
{
json_object_end(js);
json_stream_close(js, NULL);
ld_send(p, js);
}
static struct json_stream *jsonrpc_stream_start(struct command *cmd)
{
struct json_stream *js = new_json_stream(cmd, cmd, NULL);
json_object_start(js, NULL);
json_add_string(js, "jsonrpc", "2.0");
json_add_id(js, cmd->id);
return js;
}
struct json_stream *jsonrpc_stream_success(struct command *cmd)
{
struct json_stream *js = jsonrpc_stream_start(cmd);
json_object_start(js, "result");
if (cmd->filter)
json_stream_attach_filter(js, cmd->filter);
return js;
}
struct json_stream *jsonrpc_stream_fail(struct command *cmd,
int code,
const char *err)
{
struct json_stream *js = jsonrpc_stream_start(cmd);
json_object_start(js, "error");
json_add_primitive_fmt(js, "code", "%d", code);
json_add_string(js, "message", err);
cmd->filter = tal_free(cmd->filter);
return js;
}
struct json_stream *jsonrpc_stream_fail_data(struct command *cmd,
int code,
const char *err)
{
struct json_stream *js = jsonrpc_stream_fail(cmd, code, err);
json_object_start(js, "data");
return js;
}
static struct command_result *command_complete(struct command *cmd,
struct json_stream *result)
{
/* Global object */
json_object_end(result);
json_stream_close(result, cmd);
ld_send(cmd->plugin, result);
tal_free(cmd);
return &complete;
}
struct command_result *command_finished(struct command *cmd,
struct json_stream *response)
{
/* Detach filter before it complains about closing object it never saw */
if (cmd->filter) {
const char *err = json_stream_detach_filter(tmpctx, response);
if (err)
json_add_string(response, "warning_parameter_filter",
err);
}
/* "result" or "error" object */
json_object_end(response);
return command_complete(cmd, response);
}
struct command_result *WARN_UNUSED_RESULT
command_still_pending(struct command *cmd)
{
if (cmd)
notleak_with_children(cmd);
return &pending;
}
struct json_out *json_out_obj(const tal_t *ctx,
const char *fieldname,
const char *str)
{
struct json_out *jout = json_out_new(ctx);
json_out_start(jout, NULL, '{');
if (str)
json_out_addstr(jout, fieldname, str);
json_out_end(jout, '}');
json_out_finished(jout);
return jout;
}
/* Realloc helper for tal membufs */
static void *membuf_tal_realloc(struct membuf *mb, void *rawelems,
size_t newsize)
{
char *p = rawelems;
tal_resize(&p, newsize);
return p;
}
static int read_json_from_rpc(struct plugin *p)
{
char *end;
/* We rely on the double-\n marker which only terminates JSON top
* levels. Thanks lightningd! */
while ((end = memmem(membuf_elems(&p->rpc_conn->mb),
membuf_num_elems(&p->rpc_conn->mb), "\n\n", 2))
== NULL) {
ssize_t r;
/* Make sure we've room for at least READ_CHUNKSIZE. */
membuf_prepare_space(&p->rpc_conn->mb, READ_CHUNKSIZE);
r = read(p->rpc_conn->fd, membuf_space(&p->rpc_conn->mb),
membuf_num_space(&p->rpc_conn->mb));
/* lightningd goes away, we go away. */
if (r == 0)
exit(0);
if (r < 0)
plugin_err(p, "Reading JSON input: %s", strerror(errno));
membuf_added(&p->rpc_conn->mb, r);
}
return end + 2 - membuf_elems(&p->rpc_conn->mb);
}
/* This closes a JSON response and writes it out. */
static void finish_and_send_json(int fd, struct json_out *jout)
{
size_t len;
const char *p;
json_out_end(jout, '}');
/* We double-\n terminate. Don't need to, but it's more readable. */
memcpy(json_out_direct(jout, 2), "\n\n", 2);
json_out_finished(jout);
p = json_out_contents(jout, &len);
write_all(fd, p, len);
json_out_consume(jout, len);
}
/* str is raw JSON from RPC output. */
static struct command_result *WARN_UNUSED_RESULT
command_done_raw(struct command *cmd,
const char *label,
const char *str, int size)
{
struct json_stream *js = jsonrpc_stream_start(cmd);
memcpy(json_out_member_direct(js->jout, label, size), str, size);
return command_complete(cmd, js);
}
struct command_result *WARN_UNUSED_RESULT
command_success(struct command *cmd, const struct json_out *result)
{
struct json_stream *js = jsonrpc_stream_start(cmd);
json_out_add_splice(js->jout, "result", result);
return command_complete(cmd, js);
}
struct command_result *command_done_err(struct command *cmd,
enum jsonrpc_errcode code,
const char *errmsg,
const struct json_out *data)
{
struct json_stream *js = jsonrpc_stream_start(cmd);
json_object_start(js, "error");
json_add_jsonrpc_errcode(js, "code", code);
json_add_string(js, "message", errmsg);
if (data)
json_out_add_splice(js->jout, "data", data);
json_object_end(js);
return command_complete(cmd, js);
}
struct command_result *command_err_raw(struct command *cmd,
const char *json_str)
{
return command_done_raw(cmd, "error",
json_str, strlen(json_str));
}
struct command_result *timer_complete(struct plugin *p)
{
assert(p->in_timer > 0);
p->in_timer--;
return &complete;
}
struct command_result *forward_error(struct command *cmd,
const char *buf,
const jsmntok_t *error,
void *arg UNNEEDED)
{
/* Push through any errors. */
return command_done_raw(cmd, "error",
buf + error->start, error->end - error->start);
}
struct command_result *forward_result(struct command *cmd,
const char *buf,
const jsmntok_t *result,
void *arg UNNEEDED)
{
/* Push through the result. */
return command_done_raw(cmd, "result",
buf + result->start, result->end - result->start);
}
/* Called by param() directly if it's malformed. */
struct command_result *command_fail(struct command *cmd,
enum jsonrpc_errcode code, const char *fmt, ...)
{
va_list ap;
struct command_result *res;
va_start(ap, fmt);
res = command_done_err(cmd, code, tal_vfmt(cmd, fmt, ap), NULL);
va_end(ap);
return res;
}
/* We invoke param for usage at registration time. */
bool command_usage_only(const struct command *cmd)
{
return cmd->usage_only;
}
bool command_deprecated_apis(const struct command *cmd)
{
return deprecated_apis;
}
bool command_dev_apis(const struct command *cmd)
{
return cmd->plugin->developer;
}
/* FIXME: would be good to support this! */
bool command_check_only(const struct command *cmd)
{
return false;
}
void command_set_usage(struct command *cmd, const char *usage TAKES)
{
usage = tal_strdup(NULL, usage);
if (!strmap_add(&cmd->plugin->usagemap, cmd->methodname, usage))
plugin_err(cmd->plugin, "Two usages for command %s?",
cmd->methodname);
}
/* Reads rpc reply and returns tokens, setting contents to 'error' or
* 'result' (depending on *error). */
static const jsmntok_t *read_rpc_reply(const tal_t *ctx,
struct plugin *plugin,
const jsmntok_t **contents,
bool *error,
int *reqlen)
{
const jsmntok_t *toks;
do {
*reqlen = read_json_from_rpc(plugin);
toks = json_parse_simple(ctx,
membuf_elems(&plugin->rpc_conn->mb),
*reqlen);
if (!toks)
plugin_err(plugin, "Malformed JSON reply '%.*s'",
*reqlen, membuf_elems(&plugin->rpc_conn->mb));
/* FIXME: Don't simply ignore notifications here! */
} while (!json_get_member(membuf_elems(&plugin->rpc_conn->mb), toks,
"id"));
*contents = json_get_member(membuf_elems(&plugin->rpc_conn->mb), toks, "error");
if (*contents)
*error = true;
else {
*contents = json_get_member(membuf_elems(&plugin->rpc_conn->mb), toks,
"result");
if (!*contents)
plugin_err(plugin, "JSON reply with no 'result' nor 'error'? '%.*s'",
*reqlen, membuf_elems(&plugin->rpc_conn->mb));
*error = false;
}
return toks;
}
/* Send request, return response, set resp/len to reponse */
static const jsmntok_t *sync_req(const tal_t *ctx,
struct plugin *plugin,
const char *method,
const struct json_out *params TAKES,
const char **resp)
{
bool error;
const jsmntok_t *contents;
int reqlen;
struct json_out *jout = json_out_new(tmpctx);
const char *id = append_json_id(tmpctx, plugin, method, "init/");
json_out_start(jout, NULL, '{');
json_out_addstr(jout, "jsonrpc", "2.0");
/* Copy in id *literally* */
memcpy(json_out_member_direct(jout, "id", strlen(id)), id, strlen(id));
json_out_addstr(jout, "method", method);
json_out_add_splice(jout, "params", params);
if (taken(params))
tal_free(params);
finish_and_send_json(plugin->rpc_conn->fd, jout);
read_rpc_reply(ctx, plugin, &contents, &error, &reqlen);
if (error)
plugin_err(plugin, "Got error reply to %s: '%.*s'",
method, reqlen, membuf_elems(&plugin->rpc_conn->mb));
*resp = membuf_consume(&plugin->rpc_conn->mb, reqlen);
return contents;
}
const jsmntok_t *jsonrpc_request_sync(const tal_t *ctx, struct plugin *plugin,
const char *method,
const struct json_out *params TAKES,
const char **resp)
{
return sync_req(ctx, plugin, method, params, resp);
}
/* Returns contents of scanning guide on 'result' */
static const char *rpc_scan_core(const tal_t *ctx,
struct plugin *plugin,
const char *method,
const struct json_out *params TAKES,
const char *guide,
va_list ap)
{
const jsmntok_t *contents;
const char *p;
contents = sync_req(tmpctx, plugin, method, params, &p);
return json_scanv(ctx, p, contents, guide, ap);
}
/* Synchronous routine to send command and extract fields from response */
void rpc_scan(struct plugin *plugin,
const char *method,
const struct json_out *params TAKES,
const char *guide,
...)
{
const char *err;
va_list ap;
va_start(ap, guide);
err = rpc_scan_core(tmpctx, plugin, method, params, guide, ap);
va_end(ap);
if (err)
plugin_err(plugin, "Could not parse %s in reply to %s: %s",
guide, method, err);
}
static void json_add_keypath(struct json_out *jout, const char *fieldname, const char *path)
{
char **parts = tal_strsplit(tmpctx, path, "/", STR_EMPTY_OK);
json_out_start(jout, fieldname, '[');
for (size_t i = 0; parts[i]; parts++)
json_out_addstr(jout, NULL, parts[i]);
json_out_end(jout, ']');
}
static const char *rpc_scan_datastore(const tal_t *ctx,
struct plugin *plugin,
const char *path,
const char *hex_or_string,
va_list ap)
{
const char *guide;
struct json_out *params;
params = json_out_new(NULL);
json_out_start(params, NULL, '{');
json_add_keypath(params, "key", path);
json_out_end(params, '}');
json_out_finished(params);
guide = tal_fmt(tmpctx, "{datastore:[0:{%s:%%}]}", hex_or_string);
return rpc_scan_core(ctx, plugin, "listdatastore", take(params),
guide, ap);
}
const char *rpc_scan_datastore_str(const tal_t *ctx,
struct plugin *plugin,
const char *path,
...)
{
const char *ret;
va_list ap;
va_start(ap, path);
ret = rpc_scan_datastore(ctx, plugin, path, "string", ap);
va_end(ap);
return ret;
}
/* This variant scans the hex encoding, not the string */
const char *rpc_scan_datastore_hex(const tal_t *ctx,
struct plugin *plugin,
const char *path,
...)
{
const char *ret;
va_list ap;
va_start(ap, path);
ret = rpc_scan_datastore(ctx, plugin, path, "hex", ap);
va_end(ap);
return ret;
}
void rpc_enable_batching(struct plugin *plugin)
{
const char *p;
struct json_out *params;
params = json_out_new(NULL);
json_out_start(params, NULL, '{');
json_out_add(params, "enable", false, "true");
json_out_end(params, '}');
json_out_finished(params);
/* We don't actually care about (empty) response */
sync_req(tmpctx, plugin, "batching", take(params), &p);
}
static struct command_result *datastore_fail(struct command *command,
const char *buf,
const jsmntok_t *result,
void *unused)
{
plugin_err(command->plugin, "datastore failed: %.*s",
json_tok_full_len(result),
json_tok_full(buf, result));
}
struct command_result *jsonrpc_set_datastore_(struct plugin *plugin,
struct command *cmd,
const char *path,
const void *value,
bool value_is_string,
const char *mode,
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
struct command_result *(*errcb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
void *arg)
{
struct out_req *req;
if (!cb)
cb = ignore_cb;
if (!errcb)
errcb = datastore_fail;
req = jsonrpc_request_start(plugin, cmd, "datastore", cb, errcb, arg);
json_add_keypath(req->js->jout, "key", path);
if (value_is_string)
json_add_string(req->js, "string", value);
else
json_add_hex_talarr(req->js, "hex", value);
json_add_string(req->js, "mode", mode);
return send_outreq(plugin, req);
}
struct get_ds_info {
struct command_result *(*string_cb)(struct command *command,
const char *val,
void *arg);
struct command_result *(*binary_cb)(struct command *command,
const u8 *val,
void *arg);
void *arg;
};
static struct command_result *listdatastore_done(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct get_ds_info *dsi)
{
const jsmntok_t *ds = json_get_member(buf, result, "datastore");
void *val;
if (ds->size == 0)
val = NULL;
else {
/* First element in array is object */
ds = ds + 1;
if (dsi->string_cb) {
const jsmntok_t *s;
s = json_get_member(buf, ds, "string");
if (!s) {
/* Complain loudly, since they
* expected string! */
plugin_log(cmd->plugin, LOG_BROKEN,
"Datastore gave nonstring result %.*s",
json_tok_full_len(result),
json_tok_full(buf, result));
val = NULL;
} else {
val = json_strdup(cmd, buf, s);
}
} else {
const jsmntok_t *hex;
hex = json_get_member(buf, ds, "hex");
val = json_tok_bin_from_hex(cmd, buf, hex);
}
}
if (dsi->string_cb)
return dsi->string_cb(cmd, val, dsi->arg);
return dsi->binary_cb(cmd, val, dsi->arg);
}
struct command_result *jsonrpc_get_datastore_(struct plugin *plugin,
struct command *cmd,
const char *path,
struct command_result *(*string_cb)(struct command *command,
const char *val,
void *arg),
struct command_result *(*binary_cb)(struct command *command,
const u8 *val,
void *arg),
void *arg)
{
struct out_req *req;
struct get_ds_info *dsi = tal(NULL, struct get_ds_info);
dsi->string_cb = string_cb;
dsi->binary_cb = binary_cb;
dsi->arg = arg;
/* listdatastore doesn't fail (except API misuse) */
req = jsonrpc_request_start(plugin, cmd, "listdatastore",
listdatastore_done, datastore_fail, dsi);
tal_steal(req, dsi);
json_add_keypath(req->js->jout, "key", path);
return send_outreq(plugin, req);
}
static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks)
{
const jsmntok_t *idtok, *contenttok;
struct out_req *out;
struct command_result *res;
const char *buf = plugin->rpc_buffer + plugin->rpc_read_offset;
idtok = json_get_member(buf, toks, "id");
if (!idtok)
/* FIXME: Don't simply ignore notifications! */
return;
out = strmap_getn(&plugin->out_reqs,
json_tok_full(buf, idtok),
json_tok_full_len(idtok));
if (!out) {
/* This can actually happen, if they free req! */
plugin_log(plugin, LOG_DBG, "JSON reply with unknown id '%.*s'",
json_tok_full_len(toks),
json_tok_full(buf, toks));
return;
}
/* Remove destructor if one existed */
if (out->cmd)
tal_del_destructor2(out->cmd, disable_request_cb, out);
/* We want to free this if callback doesn't. */
tal_steal(tmpctx, out);
contenttok = json_get_member(buf, toks, "error");
if (contenttok) {
if (out->errcb)
res = out->errcb(out->cmd, buf, contenttok, out->arg);
else
res = out->cb(out->cmd, buf, toks, out->arg);
} else {
contenttok = json_get_member(buf, toks, "result");
if (!contenttok)
plugin_err(plugin, "Bad JSONRPC, no 'error' nor 'result': '%.*s'",
json_tok_full_len(toks),
json_tok_full(buf, toks));
/* errcb is NULL if it's a single whole-object callback */
if (out->errcb)
res = out->cb(out->cmd, buf, contenttok, out->arg);
else
res = out->cb(out->cmd, buf, toks, out->arg);
}
assert(res == &pending || res == &complete);
}
struct command_result *
send_outreq(struct plugin *plugin, const struct out_req *req)
{
/* The "param" object. */
if (req->errcb)
json_object_end(req->js);
json_object_end(req->js);
json_stream_close(req->js, req->cmd);
ld_rpc_send(plugin, req->js);
if (req->cmd != NULL)
notleak_with_children(req->cmd);
return &pending;
}
static struct command_result *
handle_getmanifest(struct command *getmanifest_cmd,
const char *buf,
const jsmntok_t *getmanifest_params)
{
struct json_stream *params = jsonrpc_stream_success(getmanifest_cmd);
struct plugin *p = getmanifest_cmd->plugin;
const jsmntok_t *dep;
bool has_shutdown_notif;
/* This was added post 0.9.0 */
dep = json_get_member(buf, getmanifest_params, "allow-deprecated-apis");
if (!dep)
deprecated_apis = true;
else {
if (!json_to_bool(buf, dep, &deprecated_apis))
plugin_err(p, "Invalid allow-deprecated-apis '%.*s'",
json_tok_full_len(dep),
json_tok_full(buf, dep));
}
json_array_start(params, "options");
for (size_t i = 0; i < tal_count(p->opts); i++) {
if (p->opts[i].dev_only && !p->developer)
continue;
json_object_start(params, NULL);
json_add_string(params, "name", p->opts[i].name);
json_add_string(params, "type", p->opts[i].type);
json_add_string(params, "description", p->opts[i].description);
json_add_bool(params, "deprecated", p->opts[i].deprecated);
json_add_bool(params, "dynamic", p->opts[i].dynamic);
json_object_end(params);
}
json_array_end(params);
json_array_start(params, "rpcmethods");
for (size_t i = 0; i < p->num_commands; i++) {
if (p->commands[i].dev_only && !p->developer)
continue;
json_object_start(params, NULL);
json_add_string(params, "name", p->commands[i].name);
json_add_string(params, "usage",
strmap_get(&p->usagemap, p->commands[i].name));
json_add_string(params, "description", p->commands[i].description);
if (p->commands[i].long_description)
json_add_string(params, "long_description",
p->commands[i].long_description);
json_add_bool(params, "deprecated", p->commands[i].deprecated);
json_object_end(params);
}
json_array_end(params);
json_array_start(params, "subscriptions");
has_shutdown_notif = false;
for (size_t i = 0; i < p->num_notif_subs; i++) {
json_add_string(params, NULL, p->notif_subs[i].name);
if (streq(p->notif_subs[i].name, "shutdown"))
has_shutdown_notif = true;
}
/* For memleak detection, always get notified of shutdown. */
if (!has_shutdown_notif && p->developer)
json_add_string(params, NULL, "shutdown");
json_array_end(params);
json_array_start(params, "hooks");
for (size_t i = 0; i < p->num_hook_subs; i++) {
json_object_start(params, NULL);
json_add_string(params, "name", p->hook_subs[i].name);
if (p->hook_subs[i].before) {
json_array_start(params, "before");
for (size_t j = 0; p->hook_subs[i].before[j]; j++)
json_add_string(params, NULL,
p->hook_subs[i].before[j]);
json_array_end(params);
}
if (p->hook_subs[i].after) {
json_array_start(params, "after");
for (size_t j = 0; p->hook_subs[i].after[j]; j++)
json_add_string(params, NULL,
p->hook_subs[i].after[j]);
json_array_end(params);
}
json_object_end(params);
}
json_array_end(params);
if (p->desired_features != NULL) {
json_object_start(params, "featurebits");
for (size_t i = 0; i < NUM_FEATURE_PLACE; i++) {
u8 *f = p->desired_features->bits[i];
const char *fieldname = feature_place_names[i];
if (fieldname == NULL)
continue;
json_add_hex(params, fieldname, f, tal_bytelen(f));
}
json_object_end(params);
}
json_add_bool(params, "dynamic", p->restartability == PLUGIN_RESTARTABLE);
json_add_bool(params, "nonnumericids", true);