2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-09-17 13:03:11 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <bitcoin/privkey.h>
|
2020-01-27 00:35:51 +01:00
|
|
|
#include <ccan/io/io.h>
|
2019-06-12 02:38:54 +02:00
|
|
|
#include <ccan/json_out/json_out.h>
|
2019-01-15 05:13:27 +01:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2022-09-12 23:19:12 +02:00
|
|
|
#include <ccan/tal/path/path.h>
|
2019-01-15 05:13:27 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <common/daemon.h>
|
2022-11-09 04:40:57 +01:00
|
|
|
#include <common/json_filter.h>
|
2022-09-12 23:16:55 +02:00
|
|
|
#include <common/json_parse_simple.h>
|
2020-01-27 00:35:51 +01:00
|
|
|
#include <common/json_stream.h>
|
2021-09-07 06:06:03 +02:00
|
|
|
#include <common/memleak.h>
|
2021-05-22 09:10:01 +02:00
|
|
|
#include <common/route.h>
|
2019-01-15 05:13:27 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <plugins/libplugin.h>
|
2021-09-03 12:16:20 +02:00
|
|
|
#include <stdio.h>
|
2019-01-15 05:13:27 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#define READ_CHUNKSIZE 4096
|
|
|
|
|
2020-02-05 00:16:01 +01:00
|
|
|
bool deprecated_apis;
|
|
|
|
|
2019-05-20 13:10:54 +02:00
|
|
|
struct plugin_timer {
|
|
|
|
struct timer timer;
|
2020-02-03 22:27:46 +01:00
|
|
|
void (*cb)(void *cb_arg);
|
|
|
|
void *cb_arg;
|
2019-05-20 13:10:54 +02:00
|
|
|
};
|
|
|
|
|
2020-02-03 10:14:43 +01:00
|
|
|
struct rpc_conn {
|
|
|
|
int fd;
|
|
|
|
MEMBUF(char) mb;
|
|
|
|
};
|
|
|
|
|
2022-09-19 02:53:00 +02:00
|
|
|
/* We can have more than one of these pending at once. */
|
|
|
|
struct jstream {
|
|
|
|
struct list_node list;
|
|
|
|
struct json_stream *js;
|
|
|
|
};
|
|
|
|
|
2020-02-05 00:21:22 +01:00
|
|
|
struct plugin {
|
|
|
|
/* lightningd interaction */
|
|
|
|
struct io_conn *stdin_conn;
|
|
|
|
struct io_conn *stdout_conn;
|
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
/* to append to all our command ids */
|
|
|
|
const char *id;
|
|
|
|
|
2020-02-05 00:21:22 +01:00
|
|
|
/* To read from lightningd */
|
|
|
|
char *buffer;
|
|
|
|
size_t used, len_read;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_parser parser;
|
|
|
|
jsmntok_t *toks;
|
2020-02-05 00:21:22 +01:00
|
|
|
|
|
|
|
/* To write to lightningd */
|
2022-09-19 02:53:00 +02:00
|
|
|
struct list_head js_list;
|
2020-02-05 00:21:22 +01:00
|
|
|
|
|
|
|
/* Asynchronous RPC interaction */
|
|
|
|
struct io_conn *io_rpc_conn;
|
2022-09-19 02:53:00 +02:00
|
|
|
struct list_head rpc_js_list;
|
2020-02-05 00:21:22 +01:00
|
|
|
char *rpc_buffer;
|
2022-09-19 02:53:00 +02:00
|
|
|
size_t rpc_used, rpc_len_read, rpc_read_offset;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_parser rpc_parser;
|
|
|
|
jsmntok_t *rpc_toks;
|
2020-02-05 00:21:22 +01:00
|
|
|
/* Tracking async RPC requests */
|
2022-09-12 23:19:12 +02:00
|
|
|
STRMAP(struct out_req *) out_reqs;
|
2020-02-05 00:21:22 +01:00
|
|
|
u64 next_outreq_id;
|
|
|
|
|
|
|
|
/* Synchronous RPC interaction */
|
|
|
|
struct rpc_conn *rpc_conn;
|
|
|
|
|
2021-03-14 08:50:40 +01:00
|
|
|
/* Plugin information details */
|
2020-02-05 00:21:22 +01:00
|
|
|
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 ? */
|
2021-01-13 04:00:24 +01:00
|
|
|
const char *(*init)(struct plugin *p,
|
|
|
|
const char *buf, const jsmntok_t *);
|
2020-02-05 00:21:22 +01:00
|
|
|
/* Has the manifest been sent already ? */
|
|
|
|
bool manifested;
|
|
|
|
/* Has init been received ? */
|
|
|
|
bool initialized;
|
2021-09-03 12:16:20 +02:00
|
|
|
/* Are we exiting? */
|
|
|
|
bool exiting;
|
2020-02-05 00:21:22 +01:00
|
|
|
|
|
|
|
/* 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;
|
2020-04-02 06:06:47 +02:00
|
|
|
|
|
|
|
/* Feature set for lightningd */
|
2020-04-03 02:03:59 +02:00
|
|
|
struct feature_set *our_features;
|
2022-02-26 01:50:33 +01:00
|
|
|
/* Features we want to add to lightningd */
|
|
|
|
const struct feature_set *desired_features;
|
2020-07-20 14:39:32 +02:00
|
|
|
|
|
|
|
/* Location of the RPC filename in case we need to defer RPC
|
|
|
|
* initialization or need to recover from a disconnect. */
|
|
|
|
const char *rpc_location;
|
2021-04-28 17:21:14 +02:00
|
|
|
|
2021-04-28 17:28:27 +02:00
|
|
|
const char **notif_topics;
|
|
|
|
size_t num_notif_topics;
|
2021-09-07 06:06:05 +02:00
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Lets them remove ptrs from leak detection. */
|
|
|
|
void (*mark_mem)(struct plugin *plugin, struct htable *memtable);
|
|
|
|
#endif
|
2020-02-05 00:21:22 +01:00
|
|
|
};
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
/* 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).
|
2020-01-27 00:42:00 +01:00
|
|
|
* Here we use two values: complete (cmd freed) and pending (still going) */
|
2019-01-15 05:13:27 +01:00
|
|
|
struct command_result {
|
|
|
|
char c;
|
|
|
|
};
|
|
|
|
static struct command_result complete, pending;
|
|
|
|
|
|
|
|
struct command_result *command_param_failed(void)
|
|
|
|
{
|
|
|
|
return &complete;
|
|
|
|
}
|
|
|
|
|
2021-04-19 21:26:23 +02:00
|
|
|
struct command_result *command_done(void)
|
|
|
|
{
|
|
|
|
return &complete;
|
|
|
|
}
|
2020-01-27 00:43:01 +01:00
|
|
|
|
2022-11-04 04:30:48 +01:00
|
|
|
struct json_filter **command_filter_ptr(struct command *cmd)
|
|
|
|
{
|
2022-11-09 04:40:57 +01:00
|
|
|
return &cmd->filter;
|
2022-11-04 04:30:48 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
static void ld_send(struct plugin *plugin, struct json_stream *stream)
|
|
|
|
{
|
2022-09-19 02:53:00 +02:00
|
|
|
struct jstream *jstr = tal(plugin, struct jstream);
|
|
|
|
jstr->js = tal_steal(jstr, stream);
|
|
|
|
list_add_tail(&plugin->js_list, &jstr->list);
|
2020-01-27 00:43:01 +01:00
|
|
|
io_wake(plugin);
|
|
|
|
}
|
|
|
|
|
2020-01-26 23:54:33 +01:00
|
|
|
static void ld_rpc_send(struct plugin *plugin, struct json_stream *stream)
|
|
|
|
{
|
2022-09-19 02:53:00 +02:00
|
|
|
struct jstream *jstr = tal(plugin, struct jstream);
|
|
|
|
jstr->js = tal_steal(jstr, stream);
|
|
|
|
list_add_tail(&plugin->rpc_js_list, &jstr->list);
|
2020-01-26 23:54:33 +01:00
|
|
|
io_wake(plugin->io_rpc_conn);
|
|
|
|
}
|
|
|
|
|
2022-07-21 03:49:30 +02:00
|
|
|
|
|
|
|
/* 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;
|
2022-07-25 21:51:32 +02:00
|
|
|
/* Called because cmd got free'd */
|
|
|
|
out->cmd = NULL;
|
2022-07-21 03:49:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-03 05:23:28 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-12-27 08:09:34 +01:00
|
|
|
|
2023-01-03 05:23:28 +01:00
|
|
|
static const char *append_json_id(const tal_t *ctx,
|
|
|
|
struct plugin *plugin,
|
|
|
|
const char *method,
|
|
|
|
const char *prefix)
|
|
|
|
{
|
2022-12-27 08:09:34 +01:00
|
|
|
return tal_fmt(ctx, "\"%s%s:%s#%"PRIu64"\"",
|
|
|
|
prefix, plugin->id, method, plugin->next_outreq_id++);
|
2022-09-12 23:19:12 +02:00
|
|
|
}
|
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
static void destroy_out_req(struct out_req *out_req, struct plugin *plugin)
|
|
|
|
{
|
|
|
|
strmap_del(&plugin->out_reqs, out_req->id, NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
/* FIXME: Move lightningd/jsonrpc to common/ ? */
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *
|
|
|
|
jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
|
|
|
|
const char *method,
|
2023-01-03 05:23:28 +01:00
|
|
|
const char *id_prefix,
|
2020-01-30 00:55:55 +01:00
|
|
|
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;
|
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
out = tal(cmd, struct out_req);
|
2023-01-03 05:23:28 +01:00
|
|
|
out->id = append_json_id(out, plugin, method, id_prefix);
|
2020-01-30 00:55:55 +01:00
|
|
|
out->cmd = cmd;
|
|
|
|
out->cb = cb;
|
|
|
|
out->errcb = errcb;
|
|
|
|
out->arg = arg;
|
2022-09-12 23:19:12 +02:00
|
|
|
strmap_add(&plugin->out_reqs, out->id, out);
|
2022-09-12 23:19:12 +02:00
|
|
|
tal_add_destructor2(out, destroy_out_req, plugin);
|
2020-01-30 00:55:55 +01:00
|
|
|
|
2022-07-21 03:49:30 +02:00
|
|
|
/* If command goes away, don't call callbacks! */
|
|
|
|
if (out->cmd)
|
|
|
|
tal_add_destructor2(out->cmd, disable_request_cb, out);
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
out->js = new_json_stream(NULL, cmd, NULL);
|
|
|
|
json_object_start(out->js, NULL);
|
|
|
|
json_add_string(out->js, "jsonrpc", "2.0");
|
2022-12-27 08:09:34 +01:00
|
|
|
json_add_id(out->js, out->id);
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(out->js, "method", method);
|
2022-07-16 07:00:17 +02:00
|
|
|
if (out->errcb)
|
|
|
|
json_object_start(out->js, "params");
|
2020-01-30 00:55:55 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:06:47 +02:00
|
|
|
const struct feature_set *plugin_feature_set(const struct plugin *p)
|
|
|
|
{
|
2020-04-03 02:03:59 +02:00
|
|
|
return p->our_features;
|
2020-04-02 06:06:47 +02:00
|
|
|
}
|
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
static void jsonrpc_finish_and_send(struct plugin *p, struct json_stream *js)
|
|
|
|
{
|
2022-07-03 13:07:20 +02:00
|
|
|
json_object_end(js);
|
2020-01-26 15:44:16 +01:00
|
|
|
json_stream_close(js, NULL);
|
|
|
|
ld_send(p, js);
|
|
|
|
}
|
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
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");
|
2022-12-27 08:09:34 +01:00
|
|
|
json_add_id(js, cmd->id);
|
2020-01-27 00:43:01 +01:00
|
|
|
|
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:28:25 +01:00
|
|
|
struct json_stream *jsonrpc_stream_success(struct command *cmd)
|
|
|
|
{
|
|
|
|
struct json_stream *js = jsonrpc_stream_start(cmd);
|
|
|
|
|
|
|
|
json_object_start(js, "result");
|
2022-11-09 04:40:57 +01:00
|
|
|
if (cmd->filter)
|
|
|
|
json_stream_attach_filter(js, cmd->filter);
|
2020-01-28 12:28:25 +01:00
|
|
|
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");
|
2022-07-04 05:52:35 +02:00
|
|
|
json_add_primitive_fmt(js, "code", "%d", code);
|
2020-01-28 12:28:25 +01:00
|
|
|
json_add_string(js, "message", err);
|
2022-11-09 04:40:57 +01:00
|
|
|
cmd->filter = tal_free(cmd->filter);
|
2020-01-28 12:28:25 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
static struct command_result *command_complete(struct command *cmd,
|
|
|
|
struct json_stream *result)
|
|
|
|
{
|
|
|
|
/* Global object */
|
2022-07-03 13:07:20 +02:00
|
|
|
json_object_end(result);
|
2020-01-27 00:43:01 +01:00
|
|
|
json_stream_close(result, cmd);
|
|
|
|
ld_send(cmd->plugin, result);
|
|
|
|
tal_free(cmd);
|
|
|
|
|
|
|
|
return &complete;
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:23:05 +02:00
|
|
|
struct command_result *command_finished(struct command *cmd,
|
|
|
|
struct json_stream *response)
|
2020-01-28 12:28:25 +01:00
|
|
|
{
|
2022-11-09 04:40:57 +01:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:28:25 +01:00
|
|
|
/* "result" or "error" object */
|
|
|
|
json_object_end(response);
|
|
|
|
|
|
|
|
return command_complete(cmd, response);
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:20:23 +01:00
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
command_still_pending(struct command *cmd)
|
|
|
|
{
|
2022-09-16 05:15:03 +02:00
|
|
|
if (cmd)
|
|
|
|
notleak_with_children(cmd);
|
2020-02-02 14:20:23 +01:00
|
|
|
return &pending;
|
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2020-01-27 00:42:00 +01:00
|
|
|
static int read_json_from_rpc(struct plugin *p)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
/* We rely on the double-\n marker which only terminates JSON top
|
|
|
|
* levels. Thanks lightningd! */
|
2020-02-03 10:14:43 +01:00
|
|
|
while ((end = memmem(membuf_elems(&p->rpc_conn->mb),
|
|
|
|
membuf_num_elems(&p->rpc_conn->mb), "\n\n", 2))
|
2019-01-15 05:13:27 +01:00
|
|
|
== NULL) {
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
/* Make sure we've room for at least READ_CHUNKSIZE. */
|
2020-02-03 10:14:43 +01:00
|
|
|
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));
|
2019-01-15 05:13:27 +01:00
|
|
|
/* lightningd goes away, we go away. */
|
|
|
|
if (r == 0)
|
|
|
|
exit(0);
|
|
|
|
if (r < 0)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(p, "Reading JSON input: %s", strerror(errno));
|
2020-02-03 10:14:43 +01:00
|
|
|
membuf_added(&p->rpc_conn->mb, r);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:14:43 +01:00
|
|
|
return end + 2 - membuf_elems(&p->rpc_conn->mb);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
/* This closes a JSON response and writes it out. */
|
|
|
|
static void finish_and_send_json(int fd, struct json_out *jout)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2019-06-12 02:38:54 +02:00
|
|
|
size_t len;
|
|
|
|
const char *p;
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
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);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
/* str is raw JSON from RPC output. */
|
2019-01-15 05:13:27 +01:00
|
|
|
static struct command_result *WARN_UNUSED_RESULT
|
2019-06-12 02:38:54 +02:00
|
|
|
command_done_raw(struct command *cmd,
|
|
|
|
const char *label,
|
|
|
|
const char *str, int size)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2020-01-27 00:43:01 +01:00
|
|
|
struct json_stream *js = jsonrpc_stream_start(cmd);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
memcpy(json_out_member_direct(js->jout, label, size), str, size);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
return command_complete(cmd, js);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
command_success(struct command *cmd, const struct json_out *result)
|
2019-06-12 02:38:54 +02:00
|
|
|
{
|
2020-01-27 00:43:01 +01:00
|
|
|
struct json_stream *js = jsonrpc_stream_start(cmd);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
json_out_add_splice(js->jout, "result", result);
|
|
|
|
return command_complete(cmd, js);
|
2019-06-12 02:38:54 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
struct command_result *command_done_err(struct command *cmd,
|
2022-09-18 02:20:50 +02:00
|
|
|
enum jsonrpc_errcode code,
|
2019-01-15 05:13:27 +01:00
|
|
|
const char *errmsg,
|
2019-06-12 02:38:54 +02:00
|
|
|
const struct json_out *data)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2020-01-27 00:43:01 +01:00
|
|
|
struct json_stream *js = jsonrpc_stream_start(cmd);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
json_object_start(js, "error");
|
2022-09-18 02:20:50 +02:00
|
|
|
json_add_jsonrpc_errcode(js, "code", code);
|
2020-01-27 00:43:01 +01:00
|
|
|
json_add_string(js, "message", errmsg);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
if (data)
|
2020-01-27 00:43:01 +01:00
|
|
|
json_out_add_splice(js->jout, "data", data);
|
|
|
|
json_object_end(js);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
return command_complete(cmd, js);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2019-08-28 05:38:12 +02:00
|
|
|
struct command_result *command_err_raw(struct command *cmd,
|
|
|
|
const char *json_str)
|
|
|
|
{
|
|
|
|
return command_done_raw(cmd, "error",
|
|
|
|
json_str, strlen(json_str));
|
|
|
|
}
|
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
struct command_result *timer_complete(struct plugin *p)
|
2019-05-20 13:10:54 +02:00
|
|
|
{
|
2020-01-24 14:57:12 +01:00
|
|
|
assert(p->in_timer > 0);
|
|
|
|
p->in_timer--;
|
2019-05-20 13:10:54 +02:00
|
|
|
return &complete;
|
|
|
|
}
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
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,
|
2022-09-18 02:20:50 +02:00
|
|
|
enum jsonrpc_errcode code, const char *fmt, ...)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-05 01:17:11 +01:00
|
|
|
/* We invoke param for usage at registration time. */
|
2019-01-15 05:13:27 +01:00
|
|
|
bool command_usage_only(const struct command *cmd)
|
|
|
|
{
|
2019-05-20 13:09:48 +02:00
|
|
|
return cmd->usage_only;
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 01:17:11 +01:00
|
|
|
/* FIXME: would be good to support this! */
|
2019-01-15 05:13:27 +01:00
|
|
|
bool command_check_only(const struct command *cmd)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-05 01:17:11 +01:00
|
|
|
void command_set_usage(struct command *cmd, const char *usage TAKES)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2019-02-05 01:17:11 +01:00
|
|
|
usage = tal_strdup(NULL, usage);
|
2020-01-24 14:57:12 +01:00
|
|
|
if (!strmap_add(&cmd->plugin->usagemap, cmd->methodname, usage))
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "Two usages for command %s?",
|
|
|
|
cmd->methodname);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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,
|
2020-01-27 00:42:00 +01:00
|
|
|
struct plugin *plugin,
|
2019-01-15 05:13:27 +01:00
|
|
|
const jsmntok_t **contents,
|
|
|
|
bool *error,
|
|
|
|
int *reqlen)
|
|
|
|
{
|
|
|
|
const jsmntok_t *toks;
|
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
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"));
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2020-02-03 10:14:43 +01:00
|
|
|
*contents = json_get_member(membuf_elems(&plugin->rpc_conn->mb), toks, "error");
|
2019-01-15 05:13:27 +01:00
|
|
|
if (*contents)
|
|
|
|
*error = true;
|
|
|
|
else {
|
2020-02-03 10:14:43 +01:00
|
|
|
*contents = json_get_member(membuf_elems(&plugin->rpc_conn->mb), toks,
|
2019-01-15 05:13:27 +01:00
|
|
|
"result");
|
|
|
|
if (!*contents)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "JSON reply with no 'result' nor 'error'? '%.*s'",
|
2020-02-03 10:14:43 +01:00
|
|
|
*reqlen, membuf_elems(&plugin->rpc_conn->mb));
|
2019-01-15 05:13:27 +01:00
|
|
|
*error = false;
|
|
|
|
}
|
|
|
|
return toks;
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:54:23 +02:00
|
|
|
/* Send request, return response, set resp/len to reponse */
|
|
|
|
static const jsmntok_t *sync_req(const tal_t *ctx,
|
2022-07-16 07:00:19 +02:00
|
|
|
struct plugin *plugin,
|
|
|
|
const char *method,
|
|
|
|
const struct json_out *params TAKES,
|
2022-09-19 02:54:23 +02:00
|
|
|
const char **resp)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
|
|
|
bool error;
|
2021-01-06 06:41:20 +01:00
|
|
|
const jsmntok_t *contents;
|
2019-01-15 05:13:27 +01:00
|
|
|
int reqlen;
|
2022-09-12 23:19:12 +02:00
|
|
|
struct json_out *jout = json_out_new(tmpctx);
|
2023-01-03 05:23:28 +01:00
|
|
|
const char *id = append_json_id(tmpctx, plugin, method, "init/");
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
json_out_start(jout, NULL, '{');
|
|
|
|
json_out_addstr(jout, "jsonrpc", "2.0");
|
2022-12-27 08:09:34 +01:00
|
|
|
/* Copy in id *literally* */
|
|
|
|
memcpy(json_out_member_direct(jout, "id", strlen(id)), id, strlen(id));
|
2022-09-12 23:19:12 +02:00
|
|
|
json_out_addstr(jout, "method", method);
|
|
|
|
json_out_add_splice(jout, "params", params);
|
|
|
|
if (taken(params))
|
|
|
|
tal_free(params);
|
2020-02-03 10:14:43 +01:00
|
|
|
finish_and_send_json(plugin->rpc_conn->fd, jout);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2022-09-19 02:54:23 +02:00
|
|
|
read_rpc_reply(ctx, plugin, &contents, &error, &reqlen);
|
2019-01-15 05:13:27 +01:00
|
|
|
if (error)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "Got error reply to %s: '%.*s'",
|
2022-09-19 02:54:23 +02:00
|
|
|
method, reqlen, membuf_elems(&plugin->rpc_conn->mb));
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2022-09-19 02:54:23 +02:00
|
|
|
*resp = membuf_consume(&plugin->rpc_conn->mb, reqlen);
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2022-07-16 07:00:19 +02:00
|
|
|
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;
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2021-01-06 06:41:20 +01:00
|
|
|
va_start(ap, guide);
|
2022-07-16 07:00:19 +02:00
|
|
|
err = rpc_scan_core(tmpctx, plugin, method, params, guide, ap);
|
2021-01-06 06:41:20 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2021-01-07 06:34:43 +01:00
|
|
|
if (err)
|
2022-07-16 07:00:19 +02:00
|
|
|
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 bool rpc_scan_datastore(struct plugin *plugin,
|
|
|
|
const char *path,
|
|
|
|
const char *hex_or_string,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
const char *guide;
|
|
|
|
struct json_out *params;
|
|
|
|
const char *err;
|
|
|
|
|
|
|
|
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);
|
|
|
|
/* FIXME: Could be some other error, but that's probably a caller bug! */
|
|
|
|
err = rpc_scan_core(tmpctx, plugin, "listdatastore", take(params), guide, ap);
|
|
|
|
if (!err)
|
|
|
|
return true;
|
|
|
|
plugin_log(plugin, LOG_DBG, "listdatastore error %s: %s", path, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rpc_scan_datastore_str(struct plugin *plugin,
|
|
|
|
const char *path,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
|
|
|
ret = rpc_scan_datastore(plugin, path, "string", ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This variant scans the hex encoding, not the string */
|
|
|
|
bool rpc_scan_datastore_hex(struct plugin *plugin,
|
|
|
|
const char *path,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
|
|
|
ret = rpc_scan_datastore(plugin, path, "hex", ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:54:23 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-09-08 03:09:13 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-07-16 07:00:19 +02:00
|
|
|
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;
|
|
|
|
|
2022-09-08 03:09:13 +02:00
|
|
|
if (!cb)
|
|
|
|
cb = ignore_cb;
|
|
|
|
if (!errcb)
|
|
|
|
errcb = datastore_fail;
|
|
|
|
|
2022-07-16 07:00:19 +02:00
|
|
|
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);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2022-12-12 05:07:46 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-26 23:54:33 +01:00
|
|
|
static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2020-01-26 23:54:33 +01:00
|
|
|
const jsmntok_t *idtok, *contenttok;
|
2019-01-15 05:13:27 +01:00
|
|
|
struct out_req *out;
|
|
|
|
struct command_result *res;
|
2022-09-19 02:53:00 +02:00
|
|
|
const char *buf = plugin->rpc_buffer + plugin->rpc_read_offset;
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2022-09-19 02:53:00 +02:00
|
|
|
idtok = json_get_member(buf, toks, "id");
|
2020-01-26 23:54:33 +01:00
|
|
|
if (!idtok)
|
2020-10-12 07:33:50 +02:00
|
|
|
/* FIXME: Don't simply ignore notifications! */
|
|
|
|
return;
|
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
out = strmap_getn(&plugin->out_reqs,
|
2022-12-27 08:09:34 +01:00
|
|
|
json_tok_full(buf, idtok),
|
|
|
|
json_tok_full_len(idtok));
|
2022-09-19 02:49:52 +02:00
|
|
|
if (!out) {
|
|
|
|
/* This can actually happen, if they free req! */
|
|
|
|
plugin_log(plugin, LOG_DBG, "JSON reply with unknown id '%.*s'",
|
2020-01-26 23:54:33 +01:00
|
|
|
json_tok_full_len(toks),
|
2022-09-19 02:53:00 +02:00
|
|
|
json_tok_full(buf, toks));
|
2022-09-19 02:49:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2022-07-21 03:49:30 +02:00
|
|
|
/* Remove destructor if one existed */
|
|
|
|
if (out->cmd)
|
|
|
|
tal_del_destructor2(out->cmd, disable_request_cb, out);
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
/* We want to free this if callback doesn't. */
|
|
|
|
tal_steal(tmpctx, out);
|
|
|
|
|
2022-09-19 02:53:00 +02:00
|
|
|
contenttok = json_get_member(buf, toks, "error");
|
2022-07-16 07:00:17 +02:00
|
|
|
if (contenttok) {
|
|
|
|
if (out->errcb)
|
2022-09-19 02:53:00 +02:00
|
|
|
res = out->errcb(out->cmd, buf, contenttok, out->arg);
|
2022-07-16 07:00:17 +02:00
|
|
|
else
|
2022-09-19 02:53:00 +02:00
|
|
|
res = out->cb(out->cmd, buf, toks, out->arg);
|
2022-07-16 07:00:17 +02:00
|
|
|
} else {
|
2022-09-19 02:53:00 +02:00
|
|
|
contenttok = json_get_member(buf, toks, "result");
|
2020-01-26 23:54:33 +01:00
|
|
|
if (!contenttok)
|
|
|
|
plugin_err(plugin, "Bad JSONRPC, no 'error' nor 'result': '%.*s'",
|
|
|
|
json_tok_full_len(toks),
|
2022-09-19 02:53:00 +02:00
|
|
|
json_tok_full(buf, toks));
|
2022-07-16 07:00:17 +02:00
|
|
|
/* errcb is NULL if it's a single whole-object callback */
|
|
|
|
if (out->errcb)
|
2022-09-19 02:53:00 +02:00
|
|
|
res = out->cb(out->cmd, buf, contenttok, out->arg);
|
2022-07-16 07:00:17 +02:00
|
|
|
else
|
2022-09-19 02:53:00 +02:00
|
|
|
res = out->cb(out->cmd, buf, toks, out->arg);
|
2020-01-26 23:54:33 +01:00
|
|
|
}
|
2019-01-15 05:13:27 +01:00
|
|
|
|
|
|
|
assert(res == &pending || res == &complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *
|
2020-01-30 00:55:55 +01:00
|
|
|
send_outreq(struct plugin *plugin, const struct out_req *req)
|
|
|
|
{
|
|
|
|
/* The "param" object. */
|
2022-07-16 07:00:17 +02:00
|
|
|
if (req->errcb)
|
|
|
|
json_object_end(req->js);
|
2022-07-03 13:07:20 +02:00
|
|
|
json_object_end(req->js);
|
2020-01-30 00:55:55 +01:00
|
|
|
json_stream_close(req->js, req->cmd);
|
2020-01-26 23:54:33 +01:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
ld_rpc_send(plugin, req->js);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2022-09-16 05:15:03 +02:00
|
|
|
if (req->cmd != NULL)
|
|
|
|
notleak_with_children(req->cmd);
|
2019-01-15 05:13:27 +01:00
|
|
|
return &pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *
|
2020-08-06 02:27:53 +02:00
|
|
|
handle_getmanifest(struct command *getmanifest_cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *getmanifest_params)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2020-01-28 18:30:27 +01:00
|
|
|
struct json_stream *params = jsonrpc_stream_success(getmanifest_cmd);
|
2020-01-27 00:35:51 +01:00
|
|
|
struct plugin *p = getmanifest_cmd->plugin;
|
2020-08-06 02:27:53 +02:00
|
|
|
const jsmntok_t *dep;
|
2021-09-07 06:06:03 +02:00
|
|
|
bool has_shutdown_notif;
|
2020-08-06 02:27:53 +02:00
|
|
|
|
|
|
|
/* 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));
|
|
|
|
}
|
2019-05-20 13:09:45 +02:00
|
|
|
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_start(params, "options");
|
2020-01-20 12:10:33 +01:00
|
|
|
for (size_t i = 0; i < tal_count(p->opts); i++) {
|
2020-01-28 18:30:27 +01:00
|
|
|
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);
|
2020-08-06 02:30:51 +02:00
|
|
|
json_add_bool(params, "deprecated", p->opts[i].deprecated);
|
2020-01-28 18:30:27 +01:00
|
|
|
json_object_end(params);
|
2019-05-20 13:09:45 +02:00
|
|
|
}
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_end(params);
|
2019-05-20 13:09:45 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
json_array_start(params, "rpcmethods");
|
2020-01-20 12:10:33 +01:00
|
|
|
for (size_t i = 0; i < p->num_commands; i++) {
|
2020-01-28 18:30:27 +01:00
|
|
|
json_object_start(params, NULL);
|
|
|
|
json_add_string(params, "name", p->commands[i].name);
|
|
|
|
json_add_string(params, "usage",
|
2020-01-24 14:57:12 +01:00
|
|
|
strmap_get(&p->usagemap, p->commands[i].name));
|
2020-01-28 18:30:27 +01:00
|
|
|
json_add_string(params, "description", p->commands[i].description);
|
2020-01-20 12:10:33 +01:00
|
|
|
if (p->commands[i].long_description)
|
2020-01-28 18:30:27 +01:00
|
|
|
json_add_string(params, "long_description",
|
2020-01-20 12:10:33 +01:00
|
|
|
p->commands[i].long_description);
|
2020-08-06 02:30:51 +02:00
|
|
|
json_add_bool(params, "deprecated", p->commands[i].deprecated);
|
2020-01-28 18:30:27 +01:00
|
|
|
json_object_end(params);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_end(params);
|
2019-12-04 18:19:14 +01:00
|
|
|
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_start(params, "subscriptions");
|
2021-09-07 06:06:03 +02:00
|
|
|
has_shutdown_notif = false;
|
|
|
|
for (size_t i = 0; i < p->num_notif_subs; i++) {
|
2020-01-28 18:30:27 +01:00
|
|
|
json_add_string(params, NULL, p->notif_subs[i].name);
|
2021-09-07 06:06:03 +02:00
|
|
|
if (streq(p->notif_subs[i].name, "shutdown"))
|
|
|
|
has_shutdown_notif = true;
|
|
|
|
}
|
|
|
|
#if DEVELOPER
|
|
|
|
/* For memleak detection, always get notified of shutdown. */
|
|
|
|
if (!has_shutdown_notif)
|
|
|
|
json_add_string(params, NULL, "shutdown");
|
|
|
|
#else
|
|
|
|
/* Don't care this is unused: compiler don't complain! */
|
|
|
|
(void)has_shutdown_notif;
|
|
|
|
#endif
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_end(params);
|
2019-12-04 18:19:14 +01:00
|
|
|
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_start(params, "hooks");
|
2020-10-30 02:13:42 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-01-28 18:30:27 +01:00
|
|
|
json_array_end(params);
|
2019-12-05 11:28:32 +01:00
|
|
|
|
2022-02-26 01:50:33 +01:00
|
|
|
if (p->desired_features != NULL) {
|
2020-04-27 22:33:50 +02:00
|
|
|
json_object_start(params, "featurebits");
|
2020-03-31 15:34:02 +02:00
|
|
|
for (size_t i = 0; i < NUM_FEATURE_PLACE; i++) {
|
2022-02-26 01:50:33 +01:00
|
|
|
u8 *f = p->desired_features->bits[i];
|
2020-03-31 15:34:02 +02:00
|
|
|
const char *fieldname = feature_place_names[i];
|
|
|
|
if (fieldname == NULL)
|
|
|
|
continue;
|
|
|
|
json_add_hex(params, fieldname, f, tal_bytelen(f));
|
|
|
|
}
|
|
|
|
json_object_end(params);
|
|
|
|
}
|
|
|
|
|
2020-01-31 19:13:59 +01:00
|
|
|
json_add_bool(params, "dynamic", p->restartability == PLUGIN_RESTARTABLE);
|
2022-11-21 02:53:26 +01:00
|
|
|
json_add_bool(params, "nonnumericids", true);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2021-04-28 18:27:44 +02:00
|
|
|
json_array_start(params, "notifications");
|
|
|
|
for (size_t i = 0; p->notif_topics && i < p->num_notif_topics; i++) {
|
|
|
|
json_object_start(params, NULL);
|
|
|
|
json_add_string(params, "method", p->notif_topics[i]);
|
|
|
|
json_object_end(params);
|
|
|
|
}
|
|
|
|
json_array_end(params);
|
|
|
|
|
2020-01-28 18:30:27 +01:00
|
|
|
return command_finished(getmanifest_cmd, params);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 23:54:33 +01:00
|
|
|
static void rpc_conn_finished(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin_err(plugin, "Lost connection to the RPC socket.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool rpc_read_response_one(struct plugin *plugin)
|
|
|
|
{
|
2020-08-20 15:40:34 +02:00
|
|
|
const jsmntok_t *jrtok;
|
|
|
|
bool complete;
|
2020-08-20 02:15:55 +02:00
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
if (!json_parse_input(&plugin->rpc_parser, &plugin->rpc_toks,
|
2022-09-19 02:53:00 +02:00
|
|
|
plugin->rpc_buffer + plugin->rpc_read_offset,
|
|
|
|
plugin->rpc_used - plugin->rpc_read_offset,
|
|
|
|
&complete)) {
|
2020-08-20 02:16:55 +02:00
|
|
|
plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'",
|
2022-09-19 02:53:00 +02:00
|
|
|
(int)(plugin->rpc_used - plugin->rpc_read_offset),
|
|
|
|
plugin->rpc_buffer + plugin->rpc_read_offset);
|
2020-01-26 23:54:33 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
if (!complete) {
|
|
|
|
/* We need more. */
|
2022-09-19 02:53:00 +02:00
|
|
|
goto compact;
|
2020-08-20 15:40:34 +02:00
|
|
|
}
|
|
|
|
|
2020-01-26 23:54:33 +01:00
|
|
|
/* Empty buffer? (eg. just whitespace). */
|
2020-08-20 15:40:34 +02:00
|
|
|
if (tal_count(plugin->rpc_toks) == 1) {
|
|
|
|
jsmn_init(&plugin->rpc_parser);
|
|
|
|
toks_reset(plugin->rpc_toks);
|
2022-09-19 02:53:00 +02:00
|
|
|
goto compact;
|
2020-01-26 23:54:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-19 02:53:00 +02:00
|
|
|
jrtok = json_get_member(plugin->rpc_buffer + plugin->rpc_read_offset,
|
|
|
|
plugin->rpc_toks, "jsonrpc");
|
2020-01-26 23:54:33 +01:00
|
|
|
if (!jrtok) {
|
2020-02-13 00:17:08 +01:00
|
|
|
plugin_err(plugin, "JSON-RPC message does not contain \"jsonrpc\" field: '%.*s'",
|
2022-09-19 02:53:00 +02:00
|
|
|
(int)(plugin->rpc_used - plugin->rpc_read_offset),
|
|
|
|
plugin->rpc_buffer + plugin->rpc_read_offset);
|
2020-01-26 23:54:33 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
handle_rpc_reply(plugin, plugin->rpc_toks);
|
2020-01-26 23:54:33 +01:00
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
/* Move this object out of the buffer */
|
2022-09-19 02:53:00 +02:00
|
|
|
plugin->rpc_read_offset += plugin->rpc_toks[0].end;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_init(&plugin->rpc_parser);
|
|
|
|
toks_reset(plugin->rpc_toks);
|
2020-01-26 23:54:33 +01:00
|
|
|
return true;
|
2022-09-19 02:53:00 +02:00
|
|
|
|
|
|
|
compact:
|
|
|
|
memmove(plugin->rpc_buffer, plugin->rpc_buffer + plugin->rpc_read_offset,
|
|
|
|
plugin->rpc_used - plugin->rpc_read_offset);
|
|
|
|
plugin->rpc_used -= plugin->rpc_read_offset;
|
|
|
|
plugin->rpc_read_offset = 0;
|
|
|
|
return false;
|
2020-01-26 23:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *rpc_conn_read_response(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin->rpc_used += plugin->rpc_len_read;
|
|
|
|
if (plugin->rpc_used == tal_count(plugin->rpc_buffer))
|
|
|
|
tal_resize(&plugin->rpc_buffer, plugin->rpc_used * 2);
|
|
|
|
|
|
|
|
/* Read and process all messages from the connection */
|
|
|
|
while (rpc_read_response_one(plugin))
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Read more, if there is. */
|
|
|
|
return io_read_partial(plugin->io_rpc_conn,
|
|
|
|
plugin->rpc_buffer + plugin->rpc_used,
|
|
|
|
tal_bytelen(plugin->rpc_buffer) - plugin->rpc_used,
|
|
|
|
&plugin->rpc_len_read,
|
|
|
|
rpc_conn_read_response, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *rpc_conn_write_request(struct io_conn *conn,
|
|
|
|
struct plugin *plugin);
|
|
|
|
|
|
|
|
static struct io_plan *
|
|
|
|
rpc_stream_complete(struct io_conn *conn, struct json_stream *js,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
2022-09-19 02:53:00 +02:00
|
|
|
struct jstream *jstr = list_pop(&plugin->rpc_js_list, struct jstream, list);
|
|
|
|
assert(jstr);
|
|
|
|
assert(jstr->js == js);
|
|
|
|
tal_free(jstr);
|
2020-01-26 23:54:33 +01:00
|
|
|
|
|
|
|
return rpc_conn_write_request(conn, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *rpc_conn_write_request(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
2022-09-19 02:53:00 +02:00
|
|
|
struct jstream *jstr = list_top(&plugin->rpc_js_list, struct jstream, list);
|
|
|
|
if (jstr)
|
|
|
|
return json_stream_output(jstr->js, conn,
|
2020-01-26 23:54:33 +01:00
|
|
|
rpc_stream_complete, plugin);
|
|
|
|
|
|
|
|
return io_out_wait(conn, plugin->io_rpc_conn,
|
|
|
|
rpc_conn_write_request, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *rpc_conn_init(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin->io_rpc_conn = conn;
|
|
|
|
io_set_finish(conn, rpc_conn_finished, plugin);
|
|
|
|
return io_duplex(conn,
|
|
|
|
rpc_conn_read_response(conn, plugin),
|
|
|
|
rpc_conn_write_request(conn, plugin));
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:06:47 +02:00
|
|
|
static struct feature_set *json_to_feature_set(struct plugin *plugin,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *features)
|
|
|
|
{
|
|
|
|
struct feature_set *fset = talz(plugin, struct feature_set);
|
|
|
|
const jsmntok_t *t;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
json_for_each_obj(i, t, features) {
|
|
|
|
enum feature_place p;
|
|
|
|
if (json_tok_streq(buf, t, "init"))
|
|
|
|
p = INIT_FEATURE;
|
|
|
|
else if (json_tok_streq(buf, t, "node"))
|
|
|
|
p = NODE_ANNOUNCE_FEATURE;
|
|
|
|
else if (json_tok_streq(buf, t, "channel"))
|
|
|
|
p = CHANNEL_FEATURE;
|
|
|
|
else if (json_tok_streq(buf, t, "invoice"))
|
|
|
|
p = BOLT11_FEATURE;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
fset->bits[p] = json_tok_bin_from_hex(fset, buf, t + 1);
|
|
|
|
}
|
|
|
|
return fset;
|
|
|
|
}
|
|
|
|
|
2020-01-27 00:35:51 +01:00
|
|
|
static struct command_result *handle_init(struct command *cmd,
|
2019-01-15 05:13:27 +01:00
|
|
|
const char *buf,
|
2020-01-27 00:35:51 +01:00
|
|
|
const jsmntok_t *params)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2021-01-06 06:41:19 +01:00
|
|
|
const jsmntok_t *configtok, *opttok, *t;
|
2019-01-15 05:13:27 +01:00
|
|
|
struct sockaddr_un addr;
|
2019-05-20 13:09:45 +02:00
|
|
|
size_t i;
|
2019-09-17 13:03:11 +02:00
|
|
|
char *dir, *network;
|
2020-01-27 00:35:51 +01:00
|
|
|
struct plugin *p = cmd->plugin;
|
2020-07-20 14:39:32 +02:00
|
|
|
bool with_rpc = p->rpc_conn != NULL;
|
2021-01-07 06:34:43 +01:00
|
|
|
const char *err;
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2021-01-06 06:41:19 +01:00
|
|
|
configtok = json_get_member(buf, params, "configuration");
|
2021-01-07 06:34:43 +01:00
|
|
|
err = json_scan(tmpctx, buf, configtok,
|
|
|
|
"{lightning-dir:%"
|
|
|
|
",network:%"
|
|
|
|
",feature_set:%"
|
|
|
|
",rpc-file:%}",
|
|
|
|
JSON_SCAN_TAL(tmpctx, json_strdup, &dir),
|
|
|
|
JSON_SCAN_TAL(tmpctx, json_strdup, &network),
|
|
|
|
JSON_SCAN_TAL(p, json_to_feature_set, &p->our_features),
|
|
|
|
JSON_SCAN_TAL(p, json_strdup, &p->rpc_location));
|
|
|
|
if (err)
|
|
|
|
plugin_err(p, "cannot scan init params: %s: %.*s",
|
|
|
|
err, json_tok_full_len(params),
|
2021-01-06 06:41:19 +01:00
|
|
|
json_tok_full(buf, params));
|
2019-07-29 14:14:19 +02:00
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
/* Move into lightning directory: other files are relative */
|
|
|
|
if (chdir(dir) != 0)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(p, "chdir to %s: %s", dir, strerror(errno));
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2019-09-17 13:03:11 +02:00
|
|
|
chainparams = chainparams_for_network(network);
|
|
|
|
|
2020-07-20 14:39:32 +02:00
|
|
|
/* Only attempt to connect if the plugin has configured the rpc_conn
|
|
|
|
* already, if that's not the case we were told to run without an RPC
|
|
|
|
* connection, so don't even log an error. */
|
|
|
|
/* FIXME: Move this to its own function so we can initialize at a
|
|
|
|
* later point in time. */
|
|
|
|
if (p->rpc_conn != NULL) {
|
|
|
|
p->rpc_conn->fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (strlen(p->rpc_location) + 1 > sizeof(addr.sun_path))
|
|
|
|
plugin_err(p, "rpc filename '%s' too long",
|
|
|
|
p->rpc_location);
|
2021-01-06 06:41:19 +01:00
|
|
|
strcpy(addr.sun_path, p->rpc_location);
|
2020-07-20 14:39:32 +02:00
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
if (connect(p->rpc_conn->fd, (struct sockaddr *)&addr,
|
|
|
|
sizeof(addr)) != 0) {
|
|
|
|
with_rpc = false;
|
|
|
|
plugin_log(p, LOG_UNUSUAL,
|
|
|
|
"Could not connect to '%s': %s",
|
|
|
|
p->rpc_location, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
membuf_init(&p->rpc_conn->mb, tal_arr(p, char, READ_CHUNKSIZE),
|
|
|
|
READ_CHUNKSIZE, membuf_tal_realloc);
|
|
|
|
|
2020-01-07 18:03:10 +01:00
|
|
|
}
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2019-05-20 13:09:45 +02:00
|
|
|
opttok = json_get_member(buf, params, "options");
|
|
|
|
json_for_each_obj(i, t, opttok) {
|
|
|
|
char *opt = json_strdup(NULL, buf, t);
|
2022-07-25 09:00:09 +02:00
|
|
|
for (size_t optnum = 0; optnum < tal_count(p->opts); optnum++) {
|
2019-05-20 13:09:45 +02:00
|
|
|
char *problem;
|
2022-07-25 09:00:09 +02:00
|
|
|
if (!streq(p->opts[optnum].name, opt))
|
2019-05-20 13:09:45 +02:00
|
|
|
continue;
|
2022-07-25 09:00:09 +02:00
|
|
|
problem = p->opts[optnum].handle(json_strdup(opt, buf, t+1),
|
|
|
|
p->opts[optnum].arg);
|
2019-05-20 13:09:45 +02:00
|
|
|
if (problem)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(p, "option '%s': %s",
|
2022-07-25 09:00:09 +02:00
|
|
|
p->opts[optnum].name, problem);
|
2019-05-20 13:09:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tal_free(opt);
|
|
|
|
}
|
|
|
|
|
2021-01-13 04:00:24 +01:00
|
|
|
if (p->init) {
|
|
|
|
const char *disable = p->init(p, buf, configtok);
|
|
|
|
if (disable)
|
|
|
|
return command_success(cmd, json_out_obj(cmd, "disable",
|
|
|
|
disable));
|
|
|
|
}
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2020-01-07 18:03:10 +01:00
|
|
|
if (with_rpc)
|
|
|
|
io_new_conn(p, p->rpc_conn->fd, rpc_conn_init, p);
|
2020-01-26 23:54:33 +01:00
|
|
|
|
2021-05-26 07:44:01 +02:00
|
|
|
return command_success(cmd, json_out_obj(cmd, NULL, NULL));
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 13:09:45 +02:00
|
|
|
char *u64_option(const char *arg, u64 *i)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
/* This is how the manpage says to do it. Yech. */
|
|
|
|
errno = 0;
|
|
|
|
*i = strtol(arg, &endp, 0);
|
|
|
|
if (*endp || !arg[0])
|
|
|
|
return tal_fmt(NULL, "'%s' is not a number", arg);
|
|
|
|
if (errno)
|
|
|
|
return tal_fmt(NULL, "'%s' is out of range", arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:36:05 +01:00
|
|
|
char *u32_option(const char *arg, u32 *i)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
u64 n;
|
|
|
|
|
2021-05-29 20:56:03 +02:00
|
|
|
errno = 0;
|
|
|
|
n = strtoul(arg, &endp, 0);
|
|
|
|
if (*endp || !arg[0])
|
|
|
|
return tal_fmt(NULL, "'%s' is not a number", arg);
|
|
|
|
if (errno)
|
|
|
|
return tal_fmt(NULL, "'%s' is out of range", arg);
|
|
|
|
|
|
|
|
*i = n;
|
|
|
|
if (*i != n)
|
|
|
|
return tal_fmt(NULL, "'%s' is too large (overflow)", arg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *u16_option(const char *arg, u16 *i)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
u64 n;
|
|
|
|
|
2020-03-11 13:36:05 +01:00
|
|
|
errno = 0;
|
|
|
|
n = strtoul(arg, &endp, 0);
|
|
|
|
if (*endp || !arg[0])
|
|
|
|
return tal_fmt(NULL, "'%s' is not a number", arg);
|
|
|
|
if (errno)
|
|
|
|
return tal_fmt(NULL, "'%s' is out of range", arg);
|
|
|
|
|
|
|
|
*i = n;
|
|
|
|
if (*i != n)
|
|
|
|
return tal_fmt(NULL, "'%s' is too large (overflow)", arg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:48:00 +02:00
|
|
|
char *bool_option(const char *arg, bool *i)
|
|
|
|
{
|
|
|
|
if (!streq(arg, "true") && !streq(arg, "false"))
|
|
|
|
return tal_fmt(NULL, "'%s' is not a bool, must be \"true\" or \"false\"", arg);
|
|
|
|
|
|
|
|
*i = streq(arg, "true");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *flag_option(const char *arg, bool *i)
|
|
|
|
{
|
|
|
|
/* We only get called if the flag was provided, so *i should be false
|
|
|
|
* by default */
|
|
|
|
assert(*i == false);
|
|
|
|
if (!streq(arg, "true"))
|
|
|
|
return tal_fmt(NULL, "Invalid argument '%s' passed to a flag", arg);
|
|
|
|
|
|
|
|
*i = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-20 13:09:45 +02:00
|
|
|
char *charp_option(const char *arg, char **p)
|
|
|
|
{
|
|
|
|
*p = tal_strdup(NULL, arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
static void setup_command_usage(struct plugin *p)
|
2019-02-05 01:17:11 +01:00
|
|
|
{
|
|
|
|
struct command *usage_cmd = tal(tmpctx, struct command);
|
|
|
|
|
|
|
|
/* This is how common/param can tell it's just a usage request */
|
2019-05-20 13:09:48 +02:00
|
|
|
usage_cmd->usage_only = true;
|
2020-01-24 14:57:12 +01:00
|
|
|
usage_cmd->plugin = p;
|
|
|
|
for (size_t i = 0; i < p->num_commands; i++) {
|
2019-02-05 01:17:11 +01:00
|
|
|
struct command_result *res;
|
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
usage_cmd->methodname = p->commands[i].name;
|
|
|
|
res = p->commands[i].handle(usage_cmd, NULL, NULL);
|
2019-09-18 12:14:44 +02:00
|
|
|
assert(res == &complete);
|
2020-01-24 14:57:12 +01:00
|
|
|
assert(strmap_get(&p->usagemap, p->commands[i].name));
|
2019-02-05 01:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
static void call_plugin_timer(struct plugin *p, struct timer *timer)
|
2019-05-20 13:10:54 +02:00
|
|
|
{
|
|
|
|
struct plugin_timer *t = container_of(timer, struct plugin_timer, timer);
|
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
p->in_timer++;
|
2019-05-21 09:32:31 +02:00
|
|
|
/* Free this if they don't. */
|
|
|
|
tal_steal(tmpctx, t);
|
2020-02-03 22:27:46 +01:00
|
|
|
t->cb(t->cb_arg);
|
2019-05-20 13:10:54 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
static void destroy_plugin_timer(struct plugin_timer *timer, struct plugin *p)
|
2019-05-20 13:10:54 +02:00
|
|
|
{
|
2020-01-24 14:57:12 +01:00
|
|
|
timer_del(&p->timers, &timer->timer);
|
2019-05-20 13:10:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 12:31:47 +01:00
|
|
|
struct plugin_timer *plugin_timer_(struct plugin *p, struct timerel t,
|
|
|
|
void (*cb)(void *cb_arg),
|
|
|
|
void *cb_arg)
|
2019-05-20 13:10:54 +02:00
|
|
|
{
|
2021-09-07 06:06:06 +02:00
|
|
|
struct plugin_timer *timer = notleak(tal(NULL, struct plugin_timer));
|
2019-05-20 13:10:54 +02:00
|
|
|
timer->cb = cb;
|
2020-02-03 22:27:46 +01:00
|
|
|
timer->cb_arg = cb_arg;
|
2019-05-20 13:10:54 +02:00
|
|
|
timer_init(&timer->timer);
|
2020-01-24 14:57:12 +01:00
|
|
|
timer_addrel(&p->timers, &timer->timer, t);
|
|
|
|
tal_add_destructor2(timer, destroy_plugin_timer, p);
|
2019-05-20 13:10:54 +02:00
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
static void plugin_logv(struct plugin *p, enum log_level l,
|
|
|
|
const char *fmt, va_list ap)
|
2019-05-21 09:32:37 +02:00
|
|
|
{
|
2020-01-26 15:44:16 +01:00
|
|
|
struct json_stream *js = new_json_stream(NULL, NULL, NULL);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, "jsonrpc", "2.0");
|
|
|
|
json_add_string(js, "method", "log");
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
json_object_start(js, "params");
|
|
|
|
json_add_string(js, "level",
|
2019-06-12 02:38:54 +02:00
|
|
|
l == LOG_DBG ? "debug"
|
|
|
|
: l == LOG_INFORM ? "info"
|
|
|
|
: l == LOG_UNUSUAL ? "warn"
|
|
|
|
: "error");
|
2020-01-26 15:44:16 +01:00
|
|
|
json_out_addv(js->jout, "message", true, fmt, ap);
|
|
|
|
json_object_end(js);
|
2019-06-12 02:38:54 +02:00
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
jsonrpc_finish_and_send(p, js);
|
2019-05-21 09:32:37 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:36:19 +02:00
|
|
|
struct json_stream *plugin_notification_start(struct plugin *plugin,
|
|
|
|
const char *method)
|
|
|
|
{
|
|
|
|
struct json_stream *js = new_json_stream(plugin, NULL, NULL);
|
|
|
|
|
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, "jsonrpc", "2.0");
|
|
|
|
json_add_string(js, "method", method);
|
|
|
|
|
|
|
|
json_object_start(js, "params");
|
|
|
|
return js;
|
|
|
|
}
|
|
|
|
void plugin_notification_end(struct plugin *plugin,
|
|
|
|
struct json_stream *stream)
|
|
|
|
{
|
|
|
|
json_object_end(stream);
|
|
|
|
jsonrpc_finish_and_send(plugin, stream);
|
|
|
|
}
|
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
struct json_stream *plugin_notify_start(struct command *cmd, const char *method)
|
|
|
|
{
|
|
|
|
struct json_stream *js = new_json_stream(cmd, NULL, NULL);
|
|
|
|
|
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, "jsonrpc", "2.0");
|
|
|
|
json_add_string(js, "method", method);
|
|
|
|
|
|
|
|
json_object_start(js, "params");
|
2022-12-27 08:09:34 +01:00
|
|
|
json_add_id(js, cmd->id);
|
2020-10-12 07:33:50 +02:00
|
|
|
|
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
|
|
|
void plugin_notify_end(struct command *cmd, struct json_stream *js)
|
|
|
|
{
|
|
|
|
json_object_end(js);
|
|
|
|
|
|
|
|
jsonrpc_finish_and_send(cmd->plugin, js);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convenience wrapper for notify with "message" */
|
|
|
|
void plugin_notify_message(struct command *cmd,
|
|
|
|
enum log_level level,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2021-07-01 06:29:01 +02:00
|
|
|
struct json_stream *js;
|
|
|
|
const char *msg;
|
2020-10-12 07:33:50 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-07-01 06:29:01 +02:00
|
|
|
msg = tal_vfmt(tmpctx, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Also log, debug level */
|
|
|
|
plugin_log(cmd->plugin, LOG_DBG, "notify msg %s: %s",
|
|
|
|
log_level_name(level), msg);
|
|
|
|
|
|
|
|
js = plugin_notify_start(cmd, "message");
|
2020-10-12 07:33:50 +02:00
|
|
|
json_add_string(js, "level", log_level_name(level));
|
|
|
|
|
|
|
|
/* In case we're OOM */
|
|
|
|
if (js->jout)
|
2021-07-01 06:29:01 +02:00
|
|
|
json_out_addstr(js->jout, "message", msg);
|
2020-10-12 07:33:50 +02:00
|
|
|
|
|
|
|
plugin_notify_end(cmd, js);
|
|
|
|
}
|
|
|
|
|
|
|
|
void plugin_notify_progress(struct command *cmd,
|
|
|
|
u32 num_stages, u32 stage,
|
|
|
|
u32 num_progress, u32 progress)
|
|
|
|
{
|
|
|
|
struct json_stream *js = plugin_notify_start(cmd, "progress");
|
|
|
|
|
|
|
|
assert(progress < num_progress);
|
|
|
|
json_add_u32(js, "num", progress);
|
|
|
|
json_add_u32(js, "total", num_progress);
|
|
|
|
if (num_stages > 0) {
|
|
|
|
assert(stage < num_stages);
|
|
|
|
json_object_start(js, "stage");
|
|
|
|
json_add_u32(js, "num", stage);
|
|
|
|
json_add_u32(js, "total", num_stages);
|
|
|
|
json_object_end(js);
|
|
|
|
}
|
|
|
|
plugin_notify_end(cmd, js);
|
|
|
|
}
|
|
|
|
|
2021-09-03 12:16:20 +02:00
|
|
|
void NORETURN plugin_exit(struct plugin *p, int exitcode)
|
|
|
|
{
|
|
|
|
p->exiting = true;
|
|
|
|
io_conn_out_exclusive(p->stdout_conn, true);
|
|
|
|
io_wake(p);
|
|
|
|
io_loop(NULL, NULL);
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2022-07-19 09:34:31 +02:00
|
|
|
void NORETURN plugin_errv(struct plugin *p, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
va_list ap2;
|
|
|
|
|
|
|
|
/* In case it gets consumed, make a copy. */
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
|
|
|
|
plugin_logv(p, LOG_BROKEN, fmt, ap);
|
|
|
|
vfprintf(stderr, fmt, ap2);
|
|
|
|
plugin_exit(p, 1);
|
|
|
|
va_end(ap2);
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
void NORETURN plugin_err(struct plugin *p, const char *fmt, ...)
|
2019-05-21 09:32:37 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2022-07-19 09:34:31 +02:00
|
|
|
plugin_errv(p, fmt, ap);
|
2019-05-21 09:32:37 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...)
|
2019-05-21 09:32:37 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_logv(p, l, fmt, ap);
|
2019-05-21 09:32:37 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2021-09-07 06:06:03 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
/* Hack since we have no extra ptr to log_memleak */
|
|
|
|
static struct plugin *memleak_plugin;
|
|
|
|
static void PRINTF_FMT(1,2) log_memleak(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-09-07 06:06:06 +02:00
|
|
|
plugin_logv(memleak_plugin, LOG_BROKEN, fmt, ap);
|
2021-09-07 06:06:03 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void memleak_check(struct plugin *plugin, struct command *cmd)
|
|
|
|
{
|
|
|
|
struct htable *memtable;
|
|
|
|
|
2022-09-16 05:15:03 +02:00
|
|
|
memtable = memleak_start(tmpctx);
|
|
|
|
|
|
|
|
/* cmd in use right now */
|
|
|
|
memleak_ptr(memtable, cmd);
|
|
|
|
memleak_ignore_children(memtable, cmd);
|
2021-09-07 06:06:03 +02:00
|
|
|
|
|
|
|
/* Now delete plugin and anything it has pointers to. */
|
2022-09-16 05:14:39 +02:00
|
|
|
memleak_scan_obj(memtable, plugin);
|
2021-09-07 06:06:03 +02:00
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
/* Memleak needs some help to see into strmaps */
|
2022-09-16 05:14:39 +02:00
|
|
|
memleak_scan_strmap(memtable, &plugin->out_reqs);
|
2022-02-26 01:50:34 +01:00
|
|
|
|
2021-09-07 06:06:03 +02:00
|
|
|
/* We know usage strings are referred to. */
|
2022-09-16 05:14:39 +02:00
|
|
|
memleak_scan_strmap(memtable, &cmd->plugin->usagemap);
|
2021-09-07 06:06:03 +02:00
|
|
|
|
2021-09-07 06:06:05 +02:00
|
|
|
if (plugin->mark_mem)
|
|
|
|
plugin->mark_mem(plugin, memtable);
|
|
|
|
|
2021-09-07 06:06:03 +02:00
|
|
|
memleak_plugin = plugin;
|
|
|
|
dump_memleak(memtable, log_memleak);
|
|
|
|
}
|
2021-09-07 06:06:05 +02:00
|
|
|
|
|
|
|
void plugin_set_memleak_handler(struct plugin *plugin,
|
|
|
|
void (*mark_mem)(struct plugin *plugin,
|
|
|
|
struct htable *memtable))
|
|
|
|
{
|
|
|
|
plugin->mark_mem = mark_mem;
|
|
|
|
}
|
2021-09-07 06:06:03 +02:00
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2020-01-27 00:35:51 +01:00
|
|
|
static void ld_command_handle(struct plugin *plugin,
|
|
|
|
const jsmntok_t *toks)
|
|
|
|
{
|
2022-11-09 04:40:57 +01:00
|
|
|
const jsmntok_t *methtok, *paramstok, *filtertok;
|
2021-09-07 06:06:06 +02:00
|
|
|
struct command *cmd;
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
methtok = json_get_member(plugin->buffer, toks, "method");
|
|
|
|
paramstok = json_get_member(plugin->buffer, toks, "params");
|
2022-11-09 04:40:57 +01:00
|
|
|
filtertok = json_get_member(plugin->buffer, toks, "filter");
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
if (!methtok || !paramstok)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "Malformed JSON-RPC notification missing "
|
2020-01-27 00:35:51 +01:00
|
|
|
"\"method\" or \"params\": %.*s",
|
|
|
|
json_tok_full_len(toks),
|
|
|
|
json_tok_full(plugin->buffer, toks));
|
|
|
|
|
2021-09-07 06:06:06 +02:00
|
|
|
cmd = tal(plugin, struct command);
|
2020-01-27 00:35:51 +01:00
|
|
|
cmd->plugin = plugin;
|
|
|
|
cmd->usage_only = false;
|
2022-11-09 04:40:57 +01:00
|
|
|
cmd->filter = NULL;
|
2020-01-27 00:35:51 +01:00
|
|
|
cmd->methodname = json_strdup(cmd, plugin->buffer, methtok);
|
2022-09-12 23:16:55 +02:00
|
|
|
cmd->id = json_get_id(cmd, plugin->buffer, toks);
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
if (!plugin->manifested) {
|
|
|
|
if (streq(cmd->methodname, "getmanifest")) {
|
2020-08-06 02:27:53 +02:00
|
|
|
handle_getmanifest(cmd, plugin->buffer, paramstok);
|
2020-01-27 00:35:51 +01:00
|
|
|
plugin->manifested = true;
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "Did not receive 'getmanifest' yet, but got '%s'"
|
2020-01-27 00:35:51 +01:00
|
|
|
" instead", cmd->methodname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plugin->initialized) {
|
|
|
|
if (streq(cmd->methodname, "init")) {
|
|
|
|
handle_init(cmd, plugin->buffer, paramstok);
|
|
|
|
plugin->initialized = true;
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "Did not receive 'init' yet, but got '%s'"
|
2020-01-27 00:35:51 +01:00
|
|
|
" instead", cmd->methodname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If that's a notification. */
|
|
|
|
if (!cmd->id) {
|
2021-09-07 06:06:03 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
bool is_shutdown = streq(cmd->methodname, "shutdown");
|
|
|
|
if (is_shutdown)
|
|
|
|
memleak_check(plugin, cmd);
|
|
|
|
#endif
|
2020-01-27 00:35:51 +01:00
|
|
|
for (size_t i = 0; i < plugin->num_notif_subs; i++) {
|
|
|
|
if (streq(cmd->methodname,
|
|
|
|
plugin->notif_subs[i].name)) {
|
|
|
|
plugin->notif_subs[i].handle(cmd,
|
|
|
|
plugin->buffer,
|
|
|
|
paramstok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 06:06:03 +02:00
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* We subscribe them to this always */
|
|
|
|
if (is_shutdown)
|
|
|
|
plugin_exit(plugin, 0);
|
|
|
|
#endif
|
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "Unregistered notification %.*s",
|
2020-01-27 00:35:51 +01:00
|
|
|
json_tok_full_len(methtok),
|
|
|
|
json_tok_full(plugin->buffer, methtok));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < plugin->num_hook_subs; i++) {
|
|
|
|
if (streq(cmd->methodname, plugin->hook_subs[i].name)) {
|
|
|
|
plugin->hook_subs[i].handle(cmd,
|
|
|
|
plugin->buffer,
|
|
|
|
paramstok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 04:40:57 +01:00
|
|
|
if (filtertok) {
|
|
|
|
/* On error, this fails cmd */
|
|
|
|
if (parse_filter(cmd, "filter", plugin->buffer, filtertok)
|
|
|
|
!= NULL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-27 00:35:51 +01:00
|
|
|
for (size_t i = 0; i < plugin->num_commands; i++) {
|
|
|
|
if (streq(cmd->methodname, plugin->commands[i].name)) {
|
|
|
|
plugin->commands[i].handle(cmd,
|
|
|
|
plugin->buffer,
|
|
|
|
paramstok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(plugin, "Unknown command '%s'", cmd->methodname);
|
2020-01-27 00:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to parse a complete message from lightningd's buffer, and return true
|
|
|
|
* if we could handle it.
|
|
|
|
*/
|
|
|
|
static bool ld_read_json_one(struct plugin *plugin)
|
|
|
|
{
|
2020-08-20 15:40:32 +02:00
|
|
|
bool complete;
|
2020-01-27 00:35:51 +01:00
|
|
|
|
2020-08-20 15:40:34 +02:00
|
|
|
if (!json_parse_input(&plugin->parser, &plugin->toks,
|
|
|
|
plugin->buffer, plugin->used,
|
2020-08-20 15:40:32 +02:00
|
|
|
&complete)) {
|
|
|
|
plugin_err(plugin, "Failed to parse JSON response '%.*s'",
|
|
|
|
(int)plugin->used, plugin->buffer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!complete) {
|
2020-01-27 00:35:51 +01:00
|
|
|
/* We need more. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty buffer? (eg. just whitespace). */
|
2020-08-20 15:40:34 +02:00
|
|
|
if (tal_count(plugin->toks) == 1) {
|
|
|
|
toks_reset(plugin->toks);
|
|
|
|
jsmn_init(&plugin->parser);
|
2020-01-27 00:35:51 +01:00
|
|
|
plugin->used = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:17:08 +01:00
|
|
|
/* FIXME: Spark doesn't create proper jsonrpc 2.0! So we don't
|
|
|
|
* check for "jsonrpc" here. */
|
2021-09-07 06:06:06 +02:00
|
|
|
ld_command_handle(plugin, plugin->toks);
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
/* Move this object out of the buffer */
|
2020-08-20 15:40:34 +02:00
|
|
|
memmove(plugin->buffer, plugin->buffer + plugin->toks[0].end,
|
|
|
|
tal_count(plugin->buffer) - plugin->toks[0].end);
|
|
|
|
plugin->used -= plugin->toks[0].end;
|
|
|
|
toks_reset(plugin->toks);
|
|
|
|
jsmn_init(&plugin->parser);
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *ld_read_json(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin->used += plugin->len_read;
|
|
|
|
if (plugin->used && plugin->used == tal_count(plugin->buffer))
|
|
|
|
tal_resize(&plugin->buffer, plugin->used * 2);
|
|
|
|
|
|
|
|
/* Read and process all messages from the connection */
|
|
|
|
while (ld_read_json_one(plugin))
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Now read more from the connection */
|
|
|
|
return io_read_partial(plugin->stdin_conn,
|
|
|
|
plugin->buffer + plugin->used,
|
|
|
|
tal_count(plugin->buffer) - plugin->used,
|
|
|
|
&plugin->len_read, ld_read_json, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *ld_write_json(struct io_conn *conn,
|
|
|
|
struct plugin *plugin);
|
|
|
|
|
|
|
|
static struct io_plan *
|
|
|
|
ld_stream_complete(struct io_conn *conn, struct json_stream *js,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
2022-09-19 02:53:00 +02:00
|
|
|
struct jstream *jstr = list_pop(&plugin->js_list, struct jstream, list);
|
|
|
|
assert(jstr);
|
|
|
|
assert(jstr->js == js);
|
|
|
|
tal_free(jstr);
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
return ld_write_json(conn, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *ld_write_json(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
2022-09-19 02:53:00 +02:00
|
|
|
struct jstream *jstr = list_top(&plugin->js_list, struct jstream, list);
|
|
|
|
if (jstr)
|
|
|
|
return json_stream_output(jstr->js, plugin->stdout_conn,
|
2020-01-27 00:35:51 +01:00
|
|
|
ld_stream_complete, plugin);
|
|
|
|
|
2021-09-03 12:16:20 +02:00
|
|
|
/* If we were simply flushing final output, stop now. */
|
|
|
|
if (plugin->exiting)
|
|
|
|
io_break(plugin);
|
2020-01-27 00:35:51 +01:00
|
|
|
return io_out_wait(conn, plugin, ld_write_json, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ld_conn_finish(struct io_conn *conn, struct plugin *plugin)
|
|
|
|
{
|
|
|
|
/* Without one of the conns there is no reason to stay alive. That
|
|
|
|
* certainly means lightningd died, since there is no cleaner way
|
|
|
|
* to stop, return 0. */
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lightningd writes on our stdin */
|
|
|
|
static struct io_plan *stdin_conn_init(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin->stdin_conn = conn;
|
|
|
|
io_set_finish(conn, ld_conn_finish, plugin);
|
|
|
|
return io_read_partial(plugin->stdin_conn, plugin->buffer,
|
|
|
|
tal_bytelen(plugin->buffer), &plugin->len_read,
|
|
|
|
ld_read_json, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lightningd reads from our stdout */
|
|
|
|
static struct io_plan *stdout_conn_init(struct io_conn *conn,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin->stdout_conn = conn;
|
|
|
|
io_set_finish(conn, ld_conn_finish, plugin);
|
|
|
|
return io_wait(plugin->stdout_conn, plugin, ld_write_json, plugin);
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:10:33 +01:00
|
|
|
static struct plugin *new_plugin(const tal_t *ctx,
|
2022-09-12 23:19:12 +02:00
|
|
|
const char *argv0,
|
2021-01-13 04:00:24 +01:00
|
|
|
const char *(*init)(struct plugin *p,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *),
|
2020-01-20 12:10:33 +01:00
|
|
|
const enum plugin_restartability restartability,
|
2020-07-20 14:39:32 +02:00
|
|
|
bool init_rpc,
|
2021-09-06 14:41:27 +02:00
|
|
|
struct feature_set *features STEALS,
|
2021-09-06 14:42:27 +02:00
|
|
|
const struct plugin_command *commands TAKES,
|
2020-01-20 12:10:33 +01:00
|
|
|
size_t num_commands,
|
2021-09-06 14:42:27 +02:00
|
|
|
const struct plugin_notification *notif_subs TAKES,
|
2020-01-20 12:10:33 +01:00
|
|
|
size_t num_notif_subs,
|
2021-09-06 14:42:27 +02:00
|
|
|
const struct plugin_hook *hook_subs TAKES,
|
2020-01-20 12:10:33 +01:00
|
|
|
size_t num_hook_subs,
|
2021-09-06 14:42:27 +02:00
|
|
|
const char **notif_topics TAKES,
|
2021-04-28 17:28:27 +02:00
|
|
|
size_t num_notif_topics,
|
2020-01-20 12:10:33 +01:00
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
const char *optname;
|
|
|
|
struct plugin *p = tal(ctx, struct plugin);
|
2022-09-12 23:19:12 +02:00
|
|
|
char *name;
|
2020-01-20 12:10:33 +01:00
|
|
|
|
2022-09-12 23:19:12 +02:00
|
|
|
/* id is our name, without extension (not that we expect any, in C!) */
|
|
|
|
name = path_basename(p, argv0);
|
|
|
|
name[path_ext_off(name)] = '\0';
|
|
|
|
p->id = name;
|
2020-01-27 00:35:51 +01:00
|
|
|
p->buffer = tal_arr(p, char, 64);
|
2022-09-19 02:53:00 +02:00
|
|
|
list_head_init(&p->js_list);
|
2020-01-27 00:35:51 +01:00
|
|
|
p->used = 0;
|
|
|
|
p->len_read = 0;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_init(&p->parser);
|
|
|
|
p->toks = toks_alloc(p);
|
2020-01-26 23:54:33 +01:00
|
|
|
/* Async RPC */
|
|
|
|
p->rpc_buffer = tal_arr(p, char, 64);
|
2022-09-19 02:53:00 +02:00
|
|
|
list_head_init(&p->rpc_js_list);
|
2020-01-26 23:54:33 +01:00
|
|
|
p->rpc_used = 0;
|
2022-09-19 02:53:00 +02:00
|
|
|
p->rpc_read_offset = 0;
|
2020-01-26 23:54:33 +01:00
|
|
|
p->rpc_len_read = 0;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_init(&p->rpc_parser);
|
|
|
|
p->rpc_toks = toks_alloc(p);
|
2020-01-26 23:54:33 +01:00
|
|
|
p->next_outreq_id = 0;
|
2022-09-12 23:19:12 +02:00
|
|
|
strmap_init(&p->out_reqs);
|
2020-03-31 15:34:02 +02:00
|
|
|
|
2022-02-26 01:50:33 +01:00
|
|
|
p->desired_features = tal_steal(p, features);
|
2020-07-20 14:39:32 +02:00
|
|
|
if (init_rpc) {
|
|
|
|
/* Sync RPC FIXME: maybe go full async ? */
|
|
|
|
p->rpc_conn = tal(p, struct rpc_conn);
|
|
|
|
} else {
|
|
|
|
p->rpc_conn = NULL;
|
|
|
|
}
|
2020-01-27 00:35:51 +01:00
|
|
|
|
|
|
|
p->init = init;
|
2021-09-03 12:16:20 +02:00
|
|
|
p->manifested = p->initialized = p->exiting = false;
|
2020-01-20 12:10:33 +01:00
|
|
|
p->restartability = restartability;
|
2020-01-24 14:57:12 +01:00
|
|
|
strmap_init(&p->usagemap);
|
|
|
|
p->in_timer = 0;
|
2020-01-27 00:35:51 +01:00
|
|
|
|
2020-01-20 12:10:33 +01:00
|
|
|
p->commands = commands;
|
2021-09-06 14:42:27 +02:00
|
|
|
if (taken(commands))
|
|
|
|
tal_steal(p, commands);
|
2020-01-20 12:10:33 +01:00
|
|
|
p->num_commands = num_commands;
|
2021-04-28 17:28:27 +02:00
|
|
|
p->notif_topics = notif_topics;
|
2021-09-06 14:42:27 +02:00
|
|
|
if (taken(notif_topics))
|
|
|
|
tal_steal(p, notif_topics);
|
2021-04-28 17:28:27 +02:00
|
|
|
p->num_notif_topics = num_notif_topics;
|
2020-01-20 12:10:33 +01:00
|
|
|
p->notif_subs = notif_subs;
|
2021-09-06 14:42:27 +02:00
|
|
|
if (taken(notif_subs))
|
|
|
|
tal_steal(p, notif_subs);
|
2020-01-20 12:10:33 +01:00
|
|
|
p->num_notif_subs = num_notif_subs;
|
|
|
|
p->hook_subs = hook_subs;
|
2021-09-06 14:42:27 +02:00
|
|
|
if (taken(hook_subs))
|
|
|
|
tal_steal(p, hook_subs);
|
2020-01-20 12:10:33 +01:00
|
|
|
p->num_hook_subs = num_hook_subs;
|
|
|
|
p->opts = tal_arr(p, struct plugin_option, 0);
|
|
|
|
|
|
|
|
while ((optname = va_arg(ap, const char *)) != NULL) {
|
|
|
|
struct plugin_option o;
|
|
|
|
o.name = optname;
|
|
|
|
o.type = va_arg(ap, const char *);
|
|
|
|
o.description = va_arg(ap, const char *);
|
|
|
|
o.handle = va_arg(ap, char *(*)(const char *str, void *arg));
|
|
|
|
o.arg = va_arg(ap, void *);
|
2020-08-06 02:30:51 +02:00
|
|
|
o.deprecated = va_arg(ap, int); /* bool gets promoted! */
|
2020-01-20 12:10:33 +01:00
|
|
|
tal_arr_expand(&p->opts, o);
|
|
|
|
}
|
|
|
|
|
2021-09-07 06:06:05 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
p->mark_mem = NULL;
|
|
|
|
#endif
|
2020-01-20 12:10:33 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
void plugin_main(char *argv[],
|
2021-01-13 04:00:24 +01:00
|
|
|
const char *(*init)(struct plugin *p,
|
|
|
|
const char *buf, const jsmntok_t *),
|
2019-07-29 13:46:48 +02:00
|
|
|
const enum plugin_restartability restartability,
|
2020-07-20 14:39:32 +02:00
|
|
|
bool init_rpc,
|
2021-09-06 14:41:27 +02:00
|
|
|
struct feature_set *features STEALS,
|
2021-09-06 14:42:27 +02:00
|
|
|
const struct plugin_command *commands TAKES,
|
2019-12-04 18:19:14 +01:00
|
|
|
size_t num_commands,
|
2021-09-06 14:42:27 +02:00
|
|
|
const struct plugin_notification *notif_subs TAKES,
|
2019-12-04 18:19:14 +01:00
|
|
|
size_t num_notif_subs,
|
2021-09-06 14:42:27 +02:00
|
|
|
const struct plugin_hook *hook_subs TAKES,
|
2019-12-05 11:28:32 +01:00
|
|
|
size_t num_hook_subs,
|
2021-09-06 14:42:27 +02:00
|
|
|
const char **notif_topics TAKES,
|
2021-04-28 17:28:27 +02:00
|
|
|
size_t num_notif_topics,
|
2019-12-04 18:19:14 +01:00
|
|
|
...)
|
2019-01-15 05:13:27 +01:00
|
|
|
{
|
2020-01-20 12:10:33 +01:00
|
|
|
struct plugin *plugin;
|
2019-05-20 13:09:45 +02:00
|
|
|
va_list ap;
|
2019-01-15 05:13:27 +01:00
|
|
|
|
|
|
|
setup_locale();
|
|
|
|
|
|
|
|
daemon_maybe_debug(argv);
|
|
|
|
|
|
|
|
/* Note this already prints to stderr, which is enough for now */
|
|
|
|
daemon_setup(argv[0], NULL, NULL);
|
|
|
|
|
2021-04-28 17:28:27 +02:00
|
|
|
va_start(ap, num_notif_topics);
|
2022-09-12 23:19:12 +02:00
|
|
|
plugin = new_plugin(NULL, argv[0],
|
|
|
|
init, restartability, init_rpc, features, commands,
|
2020-03-31 15:34:02 +02:00
|
|
|
num_commands, notif_subs, num_notif_subs, hook_subs,
|
2021-04-28 17:28:27 +02:00
|
|
|
num_hook_subs, notif_topics, num_notif_topics, ap);
|
2020-01-20 12:10:33 +01:00
|
|
|
va_end(ap);
|
2020-01-24 14:57:12 +01:00
|
|
|
setup_command_usage(plugin);
|
2020-01-20 12:10:33 +01:00
|
|
|
|
2020-01-24 14:57:12 +01:00
|
|
|
timers_init(&plugin->timers, time_mono());
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2020-01-27 00:35:51 +01:00
|
|
|
io_new_conn(plugin, STDIN_FILENO, stdin_conn_init, plugin);
|
|
|
|
io_new_conn(plugin, STDOUT_FILENO, stdout_conn_init, plugin);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2020-01-27 00:35:51 +01:00
|
|
|
struct timer *expired = NULL;
|
2019-05-20 13:10:54 +02:00
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
clean_tmpctx();
|
|
|
|
|
2020-01-27 00:35:51 +01:00
|
|
|
/* Will only exit if a timer has expired. */
|
2020-01-24 14:57:12 +01:00
|
|
|
io_loop(&plugin->timers, &expired);
|
|
|
|
call_plugin_timer(plugin, expired);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
2020-01-20 12:10:33 +01:00
|
|
|
|
|
|
|
tal_free(plugin);
|
2019-01-15 05:13:27 +01:00
|
|
|
}
|
2020-06-09 18:30:14 +02:00
|
|
|
|
|
|
|
static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
struct listpeers_channel *chan;
|
|
|
|
const jsmntok_t *privtok = json_get_member(buffer, tok, "private"),
|
|
|
|
*statetok = json_get_member(buffer, tok, "state"),
|
|
|
|
*ftxidtok =
|
|
|
|
json_get_member(buffer, tok, "funding_txid"),
|
|
|
|
*scidtok =
|
|
|
|
json_get_member(buffer, tok, "short_channel_id"),
|
|
|
|
*dirtok = json_get_member(buffer, tok, "direction"),
|
2022-04-28 17:55:15 +02:00
|
|
|
*tmsattok = json_get_member(buffer, tok, "total_msat"),
|
2020-06-09 18:30:14 +02:00
|
|
|
*smsattok =
|
2022-04-28 17:55:15 +02:00
|
|
|
json_get_member(buffer, tok, "spendable_msat"),
|
2023-01-12 02:20:10 +01:00
|
|
|
*aliastok = json_get_member(buffer, tok, "alias"),
|
|
|
|
*max_htlcs = json_get_member(buffer, tok, "max_accepted_htlcs"),
|
2023-01-12 02:21:10 +01:00
|
|
|
*htlcstok = json_get_member(buffer, tok, "htlcs"),
|
2023-01-12 02:22:10 +01:00
|
|
|
*idtok = json_get_member(buffer, tok, "peer_id"),
|
|
|
|
*conntok = json_get_member(buffer, tok, "peer_connected");
|
2020-06-09 18:30:14 +02:00
|
|
|
|
|
|
|
chan = tal(ctx, struct listpeers_channel);
|
|
|
|
|
2023-01-12 02:21:10 +01:00
|
|
|
json_to_node_id(buffer, idtok, &chan->id);
|
|
|
|
json_to_bool(buffer, conntok, &chan->connected);
|
2020-06-09 18:30:14 +02:00
|
|
|
json_to_bool(buffer, privtok, &chan->private);
|
|
|
|
chan->state = json_strdup(chan, buffer, statetok);
|
|
|
|
json_to_txid(buffer, ftxidtok, &chan->funding_txid);
|
|
|
|
if (scidtok != NULL) {
|
|
|
|
assert(dirtok != NULL);
|
|
|
|
chan->scid = tal(chan, struct short_channel_id);
|
|
|
|
json_to_short_channel_id(buffer, scidtok, chan->scid);
|
2022-05-04 15:51:11 +02:00
|
|
|
} else {
|
2020-06-09 18:30:14 +02:00
|
|
|
chan->scid = NULL;
|
2022-05-04 15:51:11 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 17:55:15 +02:00
|
|
|
if (aliastok != NULL) {
|
|
|
|
const jsmntok_t *loctok =
|
|
|
|
json_get_member(buffer, aliastok, "local"),
|
|
|
|
*remtok =
|
|
|
|
json_get_member(buffer, aliastok, "remote");
|
|
|
|
if (loctok) {
|
|
|
|
chan->alias[LOCAL] = tal(chan, struct short_channel_id);
|
|
|
|
json_to_short_channel_id(buffer, loctok,
|
|
|
|
chan->alias[LOCAL]);
|
|
|
|
} else
|
|
|
|
chan->alias[LOCAL] = NULL;
|
|
|
|
|
|
|
|
if (remtok) {
|
|
|
|
chan->alias[REMOTE] = tal(chan, struct short_channel_id);
|
|
|
|
json_to_short_channel_id(buffer, loctok,
|
|
|
|
chan->alias[REMOTE]);
|
|
|
|
} else
|
|
|
|
chan->alias[REMOTE] = NULL;
|
2022-04-28 17:57:55 +02:00
|
|
|
} else {
|
|
|
|
chan->alias[LOCAL] = NULL;
|
|
|
|
chan->alias[REMOTE] = NULL;
|
2022-04-28 17:55:15 +02:00
|
|
|
}
|
2020-06-09 18:30:14 +02:00
|
|
|
|
2023-01-12 02:19:10 +01:00
|
|
|
/* If we catch a channel during opening, these might not be set.
|
|
|
|
* It's not a real channel (yet), so ignore it! */
|
|
|
|
if (!chan->scid && !chan->alias[LOCAL])
|
|
|
|
return tal_free(chan);
|
|
|
|
|
|
|
|
json_to_int(buffer, dirtok, &chan->direction);
|
2020-06-09 18:30:14 +02:00
|
|
|
json_to_msat(buffer, tmsattok, &chan->total_msat);
|
|
|
|
json_to_msat(buffer, smsattok, &chan->spendable_msat);
|
2023-01-12 02:20:10 +01:00
|
|
|
json_to_u16(buffer, max_htlcs, &chan->max_accepted_htlcs);
|
|
|
|
chan->num_htlcs = htlcstok->size;
|
2020-06-09 18:30:14 +02:00
|
|
|
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2023-01-12 02:18:10 +01:00
|
|
|
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok)
|
2020-06-09 18:30:14 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const jsmntok_t *iter;
|
2023-01-12 02:21:10 +01:00
|
|
|
const jsmntok_t *channelstok = json_get_member(buffer, tok, "channels");
|
2023-01-12 02:18:10 +01:00
|
|
|
struct listpeers_channel **chans;
|
2020-06-09 18:30:14 +02:00
|
|
|
|
2023-01-12 02:18:10 +01:00
|
|
|
chans = tal_arr(ctx, struct listpeers_channel *, 0);
|
2023-01-12 02:21:10 +01:00
|
|
|
json_for_each_arr(i, iter, channelstok) {
|
|
|
|
struct listpeers_channel *chan;
|
|
|
|
|
|
|
|
chan = json_to_listpeers_channel(chans, buffer, iter);
|
|
|
|
if (!chan)
|
|
|
|
continue;
|
|
|
|
tal_arr_expand(&chans, chan);
|
|
|
|
}
|
2023-01-12 02:18:10 +01:00
|
|
|
return chans;
|
2020-06-09 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct createonion_response *json_to_createonion_response(const tal_t *ctx,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *toks)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
struct createonion_response *resp;
|
|
|
|
const jsmntok_t *oniontok = json_get_member(buffer, toks, "onion");
|
|
|
|
const jsmntok_t *secretstok = json_get_member(buffer, toks, "shared_secrets");
|
|
|
|
const jsmntok_t *cursectok;
|
|
|
|
|
|
|
|
if (oniontok == NULL || secretstok == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
resp = tal(ctx, struct createonion_response);
|
|
|
|
|
|
|
|
if (oniontok->type != JSMN_STRING)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
resp->onion = json_tok_bin_from_hex(resp, buffer, oniontok);
|
|
|
|
resp->shared_secrets = tal_arr(resp, struct secret, secretstok->size);
|
|
|
|
|
|
|
|
json_for_each_arr(i, cursectok, secretstok) {
|
|
|
|
if (cursectok->type != JSMN_STRING)
|
|
|
|
goto fail;
|
|
|
|
json_to_secret(buffer, cursectok, &resp->shared_secrets[i]);
|
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return tal_free(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool json_to_route_hop_inplace(struct route_hop *dst, const char *buffer,
|
|
|
|
const jsmntok_t *toks)
|
|
|
|
{
|
|
|
|
const jsmntok_t *idtok = json_get_member(buffer, toks, "id");
|
|
|
|
const jsmntok_t *channeltok = json_get_member(buffer, toks, "channel");
|
|
|
|
const jsmntok_t *directiontok = json_get_member(buffer, toks, "direction");
|
|
|
|
const jsmntok_t *amounttok = json_get_member(buffer, toks, "amount_msat");
|
|
|
|
const jsmntok_t *delaytok = json_get_member(buffer, toks, "delay");
|
|
|
|
const jsmntok_t *styletok = json_get_member(buffer, toks, "style");
|
|
|
|
|
|
|
|
if (idtok == NULL || channeltok == NULL || directiontok == NULL ||
|
|
|
|
amounttok == NULL || delaytok == NULL || styletok == NULL)
|
|
|
|
return false;
|
|
|
|
|
2021-05-22 09:10:00 +02:00
|
|
|
json_to_node_id(buffer, idtok, &dst->node_id);
|
|
|
|
json_to_short_channel_id(buffer, channeltok, &dst->scid);
|
2020-06-09 18:30:14 +02:00
|
|
|
json_to_int(buffer, directiontok, &dst->direction);
|
|
|
|
json_to_msat(buffer, amounttok, &dst->amount);
|
|
|
|
json_to_number(buffer, delaytok, &dst->delay);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct route_hop *json_to_route(const tal_t *ctx, const char *buffer,
|
|
|
|
const jsmntok_t *toks)
|
|
|
|
{
|
|
|
|
size_t num = toks->size, i;
|
|
|
|
struct route_hop *hops;
|
|
|
|
const jsmntok_t *rtok;
|
|
|
|
if (toks->type != JSMN_ARRAY)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hops = tal_arr(ctx, struct route_hop, num);
|
|
|
|
json_for_each_arr(i, rtok, toks) {
|
|
|
|
if (!json_to_route_hop_inplace(&hops[i], buffer, rtok))
|
|
|
|
return tal_free(hops);
|
|
|
|
}
|
|
|
|
return hops;
|
|
|
|
}
|
2020-11-25 01:18:46 +01:00
|
|
|
|
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
command_hook_success(struct command *cmd)
|
|
|
|
{
|
|
|
|
struct json_stream *response = jsonrpc_stream_success(cmd);
|
|
|
|
json_add_string(response, "result", "continue");
|
|
|
|
return command_finished(cmd, response);
|
|
|
|
}
|
plugins/libplugin.c: Allow freeing notification `struct command *`.
We always allocate a new `struct command` when we get a full JSON
object from stdin:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1229-L1233
If it happens to be a notification, we pass the `struct command` to
the handler, and not free it ourselves:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1270-L1275
There are only nine points in `plugins/libplugin.c` where we `tal_free`
anything, and only one of them frees a `struct command`:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L224-L234
The above function `command_complete` is not appropriate for
notification handlers; the above function sends out a response
to our stdout, which a notification handler should not do.
However, as-is, it does mean that notification handling leaks
`struct command` objects, which can be problematic if we ever
have future built-in plugins which are significantly more
dependent on notifications.
This commit changes notification handlers to return
`struct command_result *`, because possibly in the future
notification handlers may want to perform `send_outreq`, so we
might as well use our standard convention for callbacks, and
to encourage future developers to check how to properly
terminate notification handlers (and free up the
`struct command`).
We also now provide a `notification_handled` function which a
notification handler must eventually call, as well as a
`notification_handler_pending` which is just a snowclone of
`command_still_pending`.
2021-10-01 01:59:40 +02:00
|
|
|
|
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
notification_handled(struct command *cmd)
|
|
|
|
{
|
|
|
|
tal_free(cmd);
|
|
|
|
return &complete;
|
|
|
|
}
|