plugins: use json_scan.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-01-06 16:11:19 +10:30
committed by Christian Decker
parent 35e8949df3
commit b61da8c5a9
4 changed files with 74 additions and 113 deletions

View File

@@ -802,34 +802,36 @@ static struct command_result *handle_init(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
const jsmntok_t *configtok, *rpctok, *dirtok, *opttok, *nettok, *fsettok,
*t;
const jsmntok_t *configtok, *opttok, *t;
struct sockaddr_un addr;
size_t i;
char *dir, *network;
struct plugin *p = cmd->plugin;
bool with_rpc = p->rpc_conn != NULL;
configtok = json_delve(buf, params, ".configuration");
configtok = json_get_member(buf, params, "configuration");
if (!json_scan(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)))
plugin_err(p, "cannot scan init params: %.*s",
json_tok_full_len(params),
json_tok_full(buf, params));
/* Move into lightning directory: other files are relative */
dirtok = json_delve(buf, configtok, ".lightning-dir");
dir = json_strdup(tmpctx, buf, dirtok);
if (chdir(dir) != 0)
plugin_err(p, "chdir to %s: %s", dir, strerror(errno));
nettok = json_delve(buf, configtok, ".network");
network = json_strdup(tmpctx, buf, nettok);
chainparams = chainparams_for_network(network);
fsettok = json_delve(buf, configtok, ".feature_set");
p->our_features = json_to_feature_set(p, buf, fsettok);
/* 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. */
rpctok = json_delve(buf, configtok, ".rpc-file");
p->rpc_location = json_strdup(p, buf, rpctok);
/* FIXME: Move this to its own function so we can initialize at a
* later point in time. */
if (p->rpc_conn != NULL) {
@@ -837,9 +839,7 @@ static struct command_result *handle_init(struct command *cmd,
if (strlen(p->rpc_location) + 1 > sizeof(addr.sun_path))
plugin_err(p, "rpc filename '%s' too long",
p->rpc_location);
memcpy(addr.sun_path, buf + rpctok->start,
rpctok->end - rpctok->start);
addr.sun_path[rpctok->end - rpctok->start] = '\0';
strcpy(addr.sun_path, p->rpc_location);
addr.sun_family = AF_UNIX;
if (connect(p->rpc_conn->fd, (struct sockaddr *)&addr,