forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightning-cli.c
654 lines (571 loc) · 16.1 KB
/
lightning-cli.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
/*
* Helper to submit via JSON-RPC and get back response.
*/
#include "config.h"
#include <assert.h>
#include <ccan/asort/asort.h>
#include <ccan/err/err.h>
#include <ccan/json_escape/json_escape.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/str.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/json.h>
#include <common/json_command.h>
#include <common/memleak.h>
#include <common/utils.h>
#include <common/version.h>
#include <libgen.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#define NO_ERROR 0
#define ERROR_FROM_LIGHTNINGD 1
#define ERROR_TALKING_TO_LIGHTNINGD 2
#define ERROR_USAGE 3
struct netaddr;
/* Returns number of tokens digested */
static size_t human_readable(const char *buffer, const jsmntok_t *t, char term)
{
size_t i, n;
switch (t->type) {
case JSMN_PRIMITIVE:
case JSMN_STRING:
for (i = t->start; i < t->end; i++) {
/* We only translate \n and \t. */
if (buffer[i] == '\\' && i + 1 < t->end) {
if (buffer[i+1] == 'n') {
fputc('\n', stdout);
i++;
continue;
} else if (buffer[i+1] == 't') {
fputc('\t', stdout);
i++;
continue;
}
}
fputc(buffer[i], stdout);
}
fputc(term, stdout);
return 1;
case JSMN_ARRAY:
n = 1;
for (i = 0; i < t->size; i++)
n += human_readable(buffer, t + n, '\n');
return n;
case JSMN_OBJECT:
/* Elide single-field objects */
if (t->size == 1)
return human_readable(buffer, t + 2, '\n') + 3;
n = 1;
for (i = 0; i < t->size; i++) {
n += human_readable(buffer, t + n, '=');
n += human_readable(buffer, t + n, '\n');
}
return n;
case JSMN_UNDEFINED:
break;
}
abort();
}
static int compare_tok(const jsmntok_t *a, const jsmntok_t *b,
const char *buffer)
{
int a_len = a->end - a->start, b_len = b->end - b->start, min_len, cmp;
if (a_len > b_len)
min_len = b_len;
else
min_len = a_len;
cmp = memcmp(buffer + a->start, buffer + b->start, min_len);
if (cmp != 0)
return cmp;
/* If equal, shorter one wins. */
return a_len - b_len;
}
static int compare_help(const jsmntok_t *const *a,
const jsmntok_t *const *b,
char *buffer)
{
const jsmntok_t *cat_a, *cat_b;
bool a_is_developer, b_is_developer;
int cmp;
cat_a = json_get_member(buffer, *a, "category");
cat_b = json_get_member(buffer, *b, "category");
/* Just in case it's an older lightningd! */
if (!cat_a)
goto same_category;
/* We always tweak "developer" category to last. */
a_is_developer = json_tok_streq(buffer, cat_a, "developer");
b_is_developer = json_tok_streq(buffer, cat_b, "developer");
if (a_is_developer && b_is_developer)
cmp = 0;
else if (a_is_developer)
cmp = 1;
else if (b_is_developer)
cmp = -1;
else
/* Otherwise we order category alphabetically. */
cmp = compare_tok(cat_a, cat_b, buffer);
if (cmp != 0)
return cmp;
/* After category, we order by name */
same_category:
return compare_tok(json_get_member(buffer, *a, "command"),
json_get_member(buffer, *b, "command"),
buffer);
}
static void human_help(char *buffer, const jsmntok_t *result)
{
unsigned int i;
/* `curr` is used as a temporary token */
const jsmntok_t *curr;
const char *prev_cat;
/* Contains all commands objects, which have the following structure :
* {
* "command": "The command name and usage",
* "category": "The command category",
* "description": "The command's description",
* "verbose": "The command's detailed description"
* }
*/
const jsmntok_t * help_array = json_get_member(buffer, result, "help");
const jsmntok_t **help = tal_arr(NULL, const jsmntok_t *,
help_array->size);
/* Populate an array for easy sorting with asort */
json_for_each_arr(i, curr, help_array)
help[i] = curr;
asort(help, tal_count(help), compare_help, buffer);
prev_cat = "";
for (i = 0; i < tal_count(help); i++) {
const jsmntok_t *category, *command, *desc;
category = json_get_member(buffer, help[i], "category");
if (category && !json_tok_streq(buffer, category, prev_cat)) {
prev_cat = json_strdup(help, buffer, category);
printf("=== %s ===\n\n", prev_cat);
}
command = json_get_member(buffer, help[i], "command");
desc = json_get_member(buffer, help[i], "description");
printf("%.*s\n",
command->end - command->start, buffer + command->start);
printf(" %.*s\n\n",
desc->end - desc->start, buffer + desc->start);
}
tal_free(help);
printf("---\nrun `lightning-cli help <command>` for more information on a specific command\n");
}
enum format {
JSON,
HUMAN,
HELPLIST,
DEFAULT_FORMAT,
RAW
};
static char *opt_set_human(enum format *format)
{
*format = HUMAN;
return NULL;
}
static char *opt_set_json(enum format *format)
{
*format = JSON;
return NULL;
}
static char *opt_set_raw(enum format *format)
{
*format = RAW;
return NULL;
}
enum input {
KEYWORDS,
ORDERED,
DEFAULT_INPUT
};
static char *opt_set_keywords(enum input *input)
{
*input = KEYWORDS;
return NULL;
}
static char *opt_set_ordered(enum input *input)
{
*input = ORDERED;
return NULL;
}
static bool is_literal(const char *arg)
{
size_t arglen = strlen(arg);
if (arglen == 0) {
return false;
}
return strspn(arg, "0123456789") == arglen
|| streq(arg, "true")
|| streq(arg, "false")
|| streq(arg, "null")
|| (arg[0] == '{' && arg[arglen - 1] == '}')
|| (arg[0] == '[' && arg[arglen - 1] == ']')
|| (arg[0] == '"' && arg[arglen - 1] == '"');
}
static void add_input(char **cmd, const char *input,
int i, int argc)
{
/* Numbers, bools, objects and arrays are left unquoted,
* and quoted things left alone. */
if (is_literal(input))
tal_append_fmt(cmd, "%s", input);
else
tal_append_fmt(cmd, "\"%s\"", json_escape(*cmd, input)->s);
if (i != argc - 1)
tal_append_fmt(cmd, ", ");
}
static void
try_exec_man (const char *page, char *relative_to) {
int status;
switch (fork()) {
case -1:
err(1, "Forking man command");
case 0:
/* child, run man command. */
if (relative_to != NULL) {
page = tal_fmt(page, "%s/../doc/%s.7", relative_to, page);
execlp("man", "man", "-l", page, (char *)NULL);
}
else {
execlp("man", "man", page, (char *)NULL);
}
err(1, "Running man command");
default:
break;
}
wait(&status);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
exit(0);
}
static void print_json(const char *str, const jsmntok_t *tok, const char *indent)
{
size_t i;
const jsmntok_t *t;
bool first;
char next_indent[strlen(indent) + 3 + 1];
memset(next_indent, ' ', sizeof(next_indent)-1);
next_indent[sizeof(next_indent)-1] = '\0';
switch (tok->type) {
case JSMN_PRIMITIVE:
case JSMN_STRING:
printf("%.*s", json_tok_full_len(tok), json_tok_full(str, tok));
return;
case JSMN_ARRAY:
first = true;
json_for_each_arr(i, t, tok) {
if (first)
printf("[\n%s", next_indent);
else
printf(",\n%s", next_indent);
print_json(str, t, next_indent);
first = false;
}
if (first)
printf("[]");
else
printf("\n%s]", indent);
return;
case JSMN_OBJECT:
first = true;
json_for_each_obj(i, t, tok) {
if (first)
printf("{\n%s", next_indent);
else
printf(",\n%s", next_indent);
print_json(str, t, next_indent);
printf(": ");
print_json(str, t + 1, next_indent);
first = false;
}
if (first)
printf("{}");
else
printf("\n%s}", indent);
return;
case JSMN_UNDEFINED:
break;
}
abort();
}
/* Always returns a positive number < len. len must be > 0! */
static size_t read_nofail(int fd, void *buf, size_t len)
{
ssize_t i;
assert(len > 0);
i = read(fd, buf, len);
if (i == 0)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"reading response: socket closed");
else if (i < 0)
err(ERROR_TALKING_TO_LIGHTNINGD, "reading response");
return i;
}
/* We rely on the fact that lightningd terminates all JSON RPC responses with
* "\n\n", so we can stream even if we can't parse. */
static void oom_dump(int fd, char *resp, size_t off)
{
warnx("Out of memory: sending raw output");
/* Note: resp does not already end in '\n\n', and resp_len is > 0 */
do {
/* Keep last char, to avoid splitting \n\n */
write_all(STDOUT_FILENO, resp, off-1);
resp[0] = resp[off-1];
off = 1 + read_nofail(fd, resp + 1, tal_bytelen(resp) - 1);
} while (resp[off-2] != '\n' || resp[off-1] != '\n');
write_all(STDOUT_FILENO, resp, off-1);
/* We assume giant answer means "success" */
exit(0);
}
/* We want to return failure if tal_resize fails */
static void tal_error(const char *msg)
{
if (streq(msg, "Reallocation failure"))
return;
abort();
}
static enum format delete_format_hint(const char *resp,
jsmntok_t **toks,
jsmntok_t *result)
{
const jsmntok_t *hint;
enum format format = JSON;
hint = json_get_member(resp, result, "format-hint");
if (!hint)
return format;
if (json_tok_streq(resp, hint, "simple"))
format = HUMAN;
/* Don't let hint appear in the output! */
json_tok_remove(toks, result, hint-1, 1);
return format;
}
static enum format choose_format(const char *resp,
jsmntok_t **toks,
const char *method,
const char *command,
enum format format)
{
/* If they specify a format, that's what we use. */
if (format != DEFAULT_FORMAT)
return format;
/* This works best when we order it. */
if (streq(method, "help") && command == NULL)
format = HELPLIST;
else {
const jsmntok_t *result = json_get_member(resp, *toks, "result");
if (result)
/* Use offset of result to get non-const ptr */
format = delete_format_hint(resp, toks,
/* const-washing */
*toks + (result - *toks));
else
format = JSON;
}
return format;
}
int main(int argc, char *argv[])
{
setup_locale();
int fd, i;
size_t off;
const char *method;
char *cmd, *resp, *idstr;
struct sockaddr_un addr;
jsmntok_t *toks;
const jsmntok_t *result, *error, *id;
const tal_t *ctx = tal(NULL, char);
char *config_filename, *lightning_dir, *net_dir, *rpc_filename;
jsmn_parser parser;
int parserr;
enum format format = DEFAULT_FORMAT;
enum input input = DEFAULT_INPUT;
char *command = NULL;
err_set_progname(argv[0]);
jsmn_init(&parser);
tal_set_backend(NULL, NULL, NULL, tal_error);
setup_option_allocators();
initial_config_opts(ctx, argc, argv,
&config_filename, &lightning_dir, &net_dir,
&rpc_filename);
opt_register_noarg("--help|-h", opt_usage_and_exit,
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
opt_register_noarg("-H|--human-readable", opt_set_human, &format,
"Human-readable output (default for 'help')");
opt_register_noarg("-J|--json", opt_set_json, &format,
"JSON output (default unless 'help')");
opt_register_noarg("-R|--raw", opt_set_raw, &format,
"Raw, unformatted JSON output");
opt_register_noarg("-k|--keywords", opt_set_keywords, &input,
"Use format key=value for <params>");
opt_register_noarg("-o|--order", opt_set_ordered, &input,
"Use params in order for <params>");
opt_register_version();
opt_early_parse(argc, argv, opt_log_stderr_exit);
opt_parse(&argc, argv, opt_log_stderr_exit);
method = argv[1];
if (!method) {
char *usage = opt_usage(argv[0], NULL);
printf("%s\n", usage);
tal_free(usage);
printf("Querying lightningd for available RPC commands (\"lightning-cli help\"):\n\n");
method = "help";
}
/* Launch a manpage if we have a help command with an argument. We do
* not need to have lightningd running in this case. */
if (streq(method, "help") && format == DEFAULT_FORMAT && argc >= 3) {
command = argv[2];
char *page = tal_fmt(ctx, "lightning-%s", command);
try_exec_man(page, NULL);
/* Try to find the page relative to this executable.
* This handles the common scenario where lightning-cli
* was built from source and hasn't been installed yet */
try_exec_man(page, dirname(argv[0]));
tal_free(page);
}
/* If an absolute path to the RPC socket is given, it takes over other
* configuration options. */
if (path_is_abs(rpc_filename)) {
net_dir = path_dirname(ctx, rpc_filename);
rpc_filename = path_basename(ctx, rpc_filename);
}
if (chdir(net_dir) != 0)
err(ERROR_TALKING_TO_LIGHTNINGD, "Moving into '%s'",
net_dir);
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (strlen(rpc_filename) + 1 > sizeof(addr.sun_path))
errx(ERROR_USAGE, "rpc filename '%s' too long", rpc_filename);
strcpy(addr.sun_path, rpc_filename);
addr.sun_family = AF_UNIX;
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
err(ERROR_TALKING_TO_LIGHTNINGD,
"Connecting to '%s'", rpc_filename);
idstr = tal_fmt(ctx, "lightning-cli-%i", getpid());
cmd = tal_fmt(ctx,
"{ \"jsonrpc\" : \"2.0\", \"method\" : \"%s\", \"id\" : \"%s\", \"params\" :",
json_escape(ctx, method)->s, idstr);
if (input == DEFAULT_INPUT) {
/* Hacky autodetect; only matters if more than single arg */
if (argc > 2 && strchr(argv[2], '='))
input = KEYWORDS;
else
input = ORDERED;
}
if (input == KEYWORDS) {
tal_append_fmt(&cmd, "{ ");
for (i = 2; i < argc; i++) {
const char *eq = strchr(argv[i], '=');
if (!eq)
err(ERROR_USAGE, "Expected key=value in '%s'",
argv[i]);
tal_append_fmt(&cmd, "\"%.*s\" : ",
(int)(eq - argv[i]), argv[i]);
add_input(&cmd, eq + 1, i, argc);
}
tal_append_fmt(&cmd, "} }");
} else {
tal_append_fmt(&cmd, "[ ");
for (i = 2; i < argc; i++)
add_input(&cmd, argv[i], i, argc);
tal_append_fmt(&cmd, "] }");
}
if (!write_all(fd, cmd, strlen(cmd)))
err(ERROR_TALKING_TO_LIGHTNINGD, "Writing command");
/* Start with 1000 characters, 100 tokens. */
resp = tal_arr(ctx, char, 1000);
toks = tal_arr(ctx, jsmntok_t, 100);
off = 0;
parserr = 0;
while (parserr <= 0) {
/* Read more if parser says, or we have 0 tokens. */
if (parserr == 0 || parserr == JSMN_ERROR_PART) {
ssize_t i = read(fd, resp + off, tal_bytelen(resp) - 1 - off);
if (i == 0)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"reading response: socket closed");
else if (i < 0)
err(ERROR_TALKING_TO_LIGHTNINGD,
"reading response");
off += i;
/* NUL terminate */
resp[off] = '\0';
}
/* (Continue) parsing */
parserr = jsmn_parse(&parser, resp, off, toks, tal_count(toks));
switch (parserr) {
case JSMN_ERROR_INVAL:
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Malformed response '%s'", resp);
case JSMN_ERROR_NOMEM: {
/* Need more tokens, double it */
if (!tal_resize(&toks, tal_count(toks) * 2))
oom_dump(fd, resp, off);
break;
}
case JSMN_ERROR_PART:
/* Need more data: make room if necessary */
if (off == tal_bytelen(resp) - 1) {
if (!tal_resize(&resp, tal_count(resp) * 2))
oom_dump(fd, resp, off);
}
break;
}
}
if (toks->type != JSMN_OBJECT)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Non-object response '%s'", resp);
/* This can rellocate toks, so call before getting pointers to tokens */
format = choose_format(resp, &toks, method, command, format);
result = json_get_member(resp, toks, "result");
error = json_get_member(resp, toks, "error");
if (!error && !result)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Either 'result' or 'error' must be returned in response '%s'", resp);
id = json_get_member(resp, toks, "id");
if (!id)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Missing 'id' in response '%s'", resp);
if (!json_tok_streq(resp, id, idstr))
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Incorrect 'id' in response: %.*s",
json_tok_full_len(id), json_tok_full(resp, id));
if (!error || json_tok_is_null(resp, error)) {
switch (format) {
case HELPLIST:
human_help(resp, result);
break;
case HUMAN:
human_readable(resp, result, '\n');
break;
case JSON:
print_json(resp, result, "");
printf("\n");
break;
case RAW:
printf("%.*s\n",
json_tok_full_len(result),
json_tok_full(resp, result));
break;
default:
abort();
}
tal_free(ctx);
opt_free_table();
return 0;
}
if (format == RAW)
printf("%.*s\n",
json_tok_full_len(error), json_tok_full(resp, error));
else {
print_json(resp, error, "");
printf("\n");
}
tal_free(ctx);
opt_free_table();
return 1;
}