forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightning-cli.c
182 lines (155 loc) · 4.81 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
/*
* Helper to submit via JSON-RPC and get back response.
*/
#include "configdir.h"
#include "controlled_time.h"
#include "json.h"
#include "version.h"
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/str.h>
#include <ccan/tal/str/str.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#define NO_ERROR 0
#define ERROR_FROM_LIGHTNINGD 1
#define ERROR_TALKING_TO_LIGHTNINGD 2
#define ERROR_USAGE 3
/* Tal wrappers for opt. */
static void *opt_allocfn(size_t size)
{
return tal_alloc_(NULL, size, false, TAL_LABEL("opt_allocfn", ""));
}
static void *tal_reallocfn(void *ptr, size_t size)
{
if (!ptr)
return opt_allocfn(size);
tal_resize_(&ptr, 1, size, false);
return ptr;
}
static void tal_freefn(void *ptr)
{
tal_free(ptr);
}
struct timeabs controlled_time(void)
{
return time_now();
}
int main(int argc, char *argv[])
{
int fd, i, off;
const char *method;
char *cmd, *resp, *idstr, *rpc_filename;
char *result_end;
struct sockaddr_un addr;
jsmntok_t *toks;
const jsmntok_t *result, *error, *id;
char *lightning_dir;
const tal_t *ctx = tal(NULL, char);
size_t num_opens, num_closes;
bool valid;
err_set_progname(argv[0]);
opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn);
configdir_register_opts(ctx, &lightning_dir, &rpc_filename);
opt_register_noarg("--help|-h", opt_usage_and_exit,
"<command> [<params>...]", "Show this message");
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)
errx(ERROR_USAGE, "Need at least one argument\n%s",
opt_usage(argv[0], NULL));
if (chdir(lightning_dir) != 0)
err(ERROR_TALKING_TO_LIGHTNINGD, "Moving into '%s'",
lightning_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,
"{ \"method\" : \"%s\", \"id\" : \"%s\", \"params\" : [ ",
method, idstr);
for (i = 2; i < argc; i++) {
/* Numbers are left unquoted, and quoted things left alone. */
if (strspn(argv[i], "0123456789") == strlen(argv[i])
|| argv[i][0] == '"')
tal_append_fmt(&cmd, "%s", argv[i]);
else
tal_append_fmt(&cmd, "\"%s\"", argv[i]);
if (i != argc - 1)
tal_append_fmt(&cmd, ", ");
}
tal_append_fmt(&cmd, "] }");
if (!write_all(fd, cmd, strlen(cmd)))
err(ERROR_TALKING_TO_LIGHTNINGD, "Writing command");
resp = tal_arr(cmd, char, 100);
off = 0;
num_opens = num_closes = 0;
while ((i = read(fd, resp + off, tal_count(resp) - 1 - off)) > 0) {
resp[off + i] = '\0';
num_opens += strcount(resp + off, "{");
num_closes += strcount(resp + off, "}");
off += i;
if (off == tal_count(resp) - 1)
tal_resize(&resp, tal_count(resp) * 2);
/* parsing huge outputs is slow: do quick check first. */
if (num_opens == num_closes && strstr(resp, "\"result\""))
break;
}
if (i < 0)
err(ERROR_TALKING_TO_LIGHTNINGD, "reading response");
/* Parsing huge results is too slow, so hack fastpath common case */
result_end = tal_fmt(ctx, ", \"error\" : null, \"id\" : \"%s\" }\n",
idstr);
if (strstarts(resp, "{ \"result\" : ") && strends(resp, result_end)) {
/* Result is OK, so dump it */
resp += strlen("{ \"result\" : ");
printf("%.*s\n", (int)(strlen(resp) - strlen(result_end)), resp);
tal_free(ctx);
return 0;
}
toks = json_parse_input(resp, off, &valid);
if (!toks || !valid)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Malformed response '%s'", resp);
if (toks->type != JSMN_OBJECT)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Non-object response '%s'", resp);
result = json_get_member(resp, toks, "result");
if (!result)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Missing 'result' in response '%s'", resp);
error = json_get_member(resp, toks, "error");
if (!error)
errx(ERROR_TALKING_TO_LIGHTNINGD,
"Missing 'error' 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_len(id), json_tok_contents(resp, id));
if (json_tok_is_null(resp, error)) {
printf("%.*s\n",
json_tok_len(result),
json_tok_contents(resp, result));
tal_free(ctx);
return 0;
}
printf("%.*s\n",
json_tok_len(error), json_tok_contents(resp, error));
tal_free(ctx);
return 1;
}