2016-01-21 21:11:48 +01:00
|
|
|
/* Code for JSON_RPC API */
|
|
|
|
/* eg: { "method" : "dev-echo", "params" : [ "hello", "Arabella!" ], "id" : "1" } */
|
2016-10-07 05:31:18 +02:00
|
|
|
#include "chaintopology.h"
|
2016-08-18 06:55:14 +02:00
|
|
|
#include "controlled_time.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "json.h"
|
|
|
|
#include "jsonrpc.h"
|
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
2016-08-18 06:55:14 +02:00
|
|
|
#include "peer.h"
|
2016-09-13 21:52:41 +02:00
|
|
|
#include "version.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/array_size/array_size.h>
|
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/io/io.h>
|
2016-01-21 21:15:27 +01:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
struct json_output {
|
|
|
|
struct list_node list;
|
|
|
|
const char *json;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void finish_jcon(struct io_conn *conn, struct json_connection *jcon)
|
|
|
|
{
|
2016-01-21 21:11:48 +01:00
|
|
|
log_debug(jcon->log, "Closing (%s)", strerror(errno));
|
|
|
|
if (jcon->current) {
|
|
|
|
log_unusual(jcon->log, "Abandoning current command");
|
|
|
|
jcon->current->jcon = NULL;
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_help(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
static const struct json_command help_command = {
|
|
|
|
"help",
|
|
|
|
json_help,
|
|
|
|
"describe commands",
|
|
|
|
"[<command>] if specified gives details about a single command."
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_stop(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
|
|
|
|
/* This can't have closed yet! */
|
|
|
|
cmd->jcon->stop = true;
|
2016-01-21 21:11:48 +01:00
|
|
|
json_add_string(response, NULL, "Shutting down");
|
2016-01-21 21:11:48 +01:00
|
|
|
command_success(cmd, response);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command stop_command = {
|
|
|
|
"stop",
|
|
|
|
json_stop,
|
|
|
|
"Shutdown the lightningd process",
|
|
|
|
"What part of shutdown wasn't clear?"
|
|
|
|
};
|
|
|
|
|
|
|
|
struct log_info {
|
|
|
|
enum log_level level;
|
|
|
|
struct json_result *response;
|
|
|
|
unsigned int num_skipped;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void add_skipped(struct log_info *info)
|
|
|
|
{
|
|
|
|
if (info->num_skipped) {
|
2016-09-19 14:08:32 +02:00
|
|
|
json_object_start(info->response, NULL);
|
2016-01-21 21:11:48 +01:00
|
|
|
json_add_string(info->response, "type", "SKIPPED");
|
|
|
|
json_add_num(info->response, "num_skipped", info->num_skipped);
|
2016-09-19 14:08:32 +02:00
|
|
|
json_object_end(info->response);
|
2016-01-21 21:11:48 +01:00
|
|
|
info->num_skipped = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void json_add_time(struct json_result *result, const char *fieldname,
|
|
|
|
struct timespec ts)
|
|
|
|
{
|
|
|
|
char timebuf[100];
|
|
|
|
|
|
|
|
sprintf(timebuf, "%lu.%09u",
|
|
|
|
(unsigned long)ts.tv_sec,
|
|
|
|
(unsigned)ts.tv_nsec);
|
|
|
|
json_add_string(result, fieldname, timebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_to_json(unsigned int skipped,
|
|
|
|
struct timerel diff,
|
|
|
|
enum log_level level,
|
|
|
|
const char *prefix,
|
|
|
|
const char *log,
|
|
|
|
struct log_info *info)
|
|
|
|
{
|
|
|
|
info->num_skipped += skipped;
|
|
|
|
|
|
|
|
if (level < info->level) {
|
|
|
|
info->num_skipped++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_skipped(info);
|
|
|
|
|
2016-09-19 14:08:32 +02:00
|
|
|
json_object_start(info->response, NULL);
|
2016-01-21 21:11:48 +01:00
|
|
|
json_add_string(info->response, "type",
|
|
|
|
level == LOG_BROKEN ? "BROKEN"
|
|
|
|
: level == LOG_UNUSUAL ? "UNUSUAL"
|
|
|
|
: level == LOG_INFORM ? "INFO"
|
|
|
|
: level == LOG_DBG ? "DEBUG"
|
|
|
|
: level == LOG_IO ? "IO"
|
|
|
|
: "UNKNOWN");
|
|
|
|
json_add_time(info->response, "time", diff.ts);
|
|
|
|
json_add_string(info->response, "source", prefix);
|
|
|
|
if (level == LOG_IO) {
|
|
|
|
if (log[0])
|
|
|
|
json_add_string(info->response, "direction", "IN");
|
|
|
|
else
|
|
|
|
json_add_string(info->response, "direction", "OUT");
|
|
|
|
|
|
|
|
json_add_hex(info->response, "data", log+1, tal_count(log)-1);
|
|
|
|
} else
|
|
|
|
json_add_string(info->response, "log", log);
|
|
|
|
|
2016-09-19 14:08:32 +02:00
|
|
|
json_object_end(info->response);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_getlog(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct log_info info;
|
2016-01-21 21:11:49 +01:00
|
|
|
struct log_record *lr = cmd->dstate->log_record;
|
2016-01-21 21:11:48 +01:00
|
|
|
jsmntok_t *level;
|
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
json_get_params(buffer, params, "?level", &level, NULL);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
info.num_skipped = 0;
|
|
|
|
|
|
|
|
if (!level)
|
|
|
|
info.level = LOG_INFORM;
|
2016-01-21 21:11:48 +01:00
|
|
|
else if (json_tok_streq(buffer, level, "io"))
|
2016-01-21 21:11:48 +01:00
|
|
|
info.level = LOG_IO;
|
2016-01-21 21:11:48 +01:00
|
|
|
else if (json_tok_streq(buffer, level, "debug"))
|
2016-01-21 21:11:48 +01:00
|
|
|
info.level = LOG_DBG;
|
2016-01-21 21:11:48 +01:00
|
|
|
else if (json_tok_streq(buffer, level, "info"))
|
2016-01-21 21:11:48 +01:00
|
|
|
info.level = LOG_INFORM;
|
2016-01-21 21:11:48 +01:00
|
|
|
else if (json_tok_streq(buffer, level, "unusual"))
|
2016-01-21 21:11:48 +01:00
|
|
|
info.level = LOG_UNUSUAL;
|
2016-01-21 21:11:48 +01:00
|
|
|
else {
|
|
|
|
command_fail(cmd, "Invalid level param");
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
info.response = new_json_result(cmd);
|
|
|
|
json_object_start(info.response, NULL);
|
|
|
|
json_add_time(info.response, "creation_time", log_init_time(lr)->ts);
|
|
|
|
json_add_num(info.response, "bytes_used", (unsigned int)log_used(lr));
|
|
|
|
json_add_num(info.response, "bytes_max", (unsigned int)log_max_mem(lr));
|
2016-09-19 14:08:32 +02:00
|
|
|
json_array_start(info.response, "log");
|
2016-01-21 21:11:48 +01:00
|
|
|
log_each_line(lr, log_to_json, &info);
|
2016-09-19 14:08:32 +02:00
|
|
|
json_array_end(info.response);
|
2016-01-21 21:11:48 +01:00
|
|
|
json_object_end(info.response);
|
|
|
|
command_success(cmd, info.response);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command getlog_command = {
|
|
|
|
"getlog",
|
|
|
|
json_getlog,
|
|
|
|
"Get logs, with optional level: [io|debug|info|unusual]",
|
|
|
|
"Returns log array"
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
static void json_rhash(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
jsmntok_t *secrettok;
|
|
|
|
struct sha256 secret;
|
|
|
|
|
2016-03-15 07:37:30 +01:00
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"secret", &secrettok,
|
|
|
|
NULL)) {
|
2016-01-21 21:15:27 +01:00
|
|
|
command_fail(cmd, "Need secret");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hex_decode(buffer + secrettok->start,
|
|
|
|
secrettok->end - secrettok->start,
|
|
|
|
&secret, sizeof(secret))) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid 32-byte hex value",
|
|
|
|
(int)(secrettok->end - secrettok->start),
|
|
|
|
buffer + secrettok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hash in place. */
|
|
|
|
sha256(&secret, &secret, sizeof(secret));
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_hex(response, "rhash", &secret, sizeof(secret));
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static const struct json_command dev_rhash_command = {
|
2016-01-21 21:15:27 +01:00
|
|
|
"dev-rhash",
|
|
|
|
json_rhash,
|
|
|
|
"SHA256 of {secret}",
|
|
|
|
"Returns a hash value"
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
static void json_crash(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
fatal("Crash at user request");
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static const struct json_command dev_crash_command = {
|
2016-01-21 21:15:28 +01:00
|
|
|
"dev-crash",
|
|
|
|
json_crash,
|
|
|
|
"Call fatal().",
|
|
|
|
"Simple crash test for developers"
|
|
|
|
};
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
static void json_restart(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
const jsmntok_t *p, *end;
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
if (params->type != JSMN_ARRAY) {
|
|
|
|
command_fail(cmd, "Need array to reexec");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
end = json_next(params);
|
|
|
|
|
|
|
|
cmd->dstate->reexec = tal_arrz(cmd->dstate, char *, n+1);
|
|
|
|
for (p = params + 1; p != end; p = json_next(p)) {
|
|
|
|
tal_resizez(&cmd->dstate->reexec, n+2);
|
|
|
|
cmd->dstate->reexec[n++] = tal_strndup(cmd->dstate->reexec,
|
|
|
|
buffer + p->start,
|
|
|
|
p->end - p->start);
|
|
|
|
}
|
|
|
|
debug_dump_peers(cmd->dstate);
|
|
|
|
io_break(cmd->dstate);
|
|
|
|
command_success(cmd, null_response(cmd));
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static const struct json_command dev_restart_command = {
|
2016-08-18 06:55:14 +02:00
|
|
|
"dev-restart",
|
|
|
|
json_restart,
|
|
|
|
"Re-exec the given {binary}.",
|
|
|
|
"Simple restart test for developers"
|
|
|
|
};
|
|
|
|
|
2016-09-12 20:07:07 +02:00
|
|
|
static void json_getinfo(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_pubkey(response, cmd->dstate->secpctx, "id", &cmd->dstate->id);
|
|
|
|
/* FIXME: Keep netaddrs and list them all. */
|
|
|
|
if (cmd->dstate->portnum)
|
|
|
|
json_add_num(response, "port", cmd->dstate->portnum);
|
2016-10-07 05:30:18 +02:00
|
|
|
json_add_bool(response, "testnet", cmd->dstate->testnet);
|
2016-09-13 21:52:41 +02:00
|
|
|
json_add_string(response, "version", version());
|
2016-10-07 05:31:18 +02:00
|
|
|
json_add_num(response, "blockheight", get_block_height(cmd->dstate));
|
2016-09-12 20:07:07 +02:00
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command getinfo_command = {
|
|
|
|
"getinfo",
|
|
|
|
json_getinfo,
|
|
|
|
"Get general information about this node",
|
|
|
|
"Returns {id}, {port}, {testnet}, etc."
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static const struct json_command *cmdlist[] = {
|
|
|
|
&help_command,
|
|
|
|
&stop_command,
|
|
|
|
&getlog_command,
|
2016-01-21 21:11:48 +01:00
|
|
|
&connect_command,
|
2016-01-21 21:11:48 +01:00
|
|
|
&getpeers_command,
|
2016-09-30 15:30:19 +02:00
|
|
|
&getnodes_command,
|
2016-08-30 12:39:57 +02:00
|
|
|
&gethtlcs_command,
|
2016-01-21 21:15:28 +01:00
|
|
|
&close_command,
|
2016-04-12 05:37:03 +02:00
|
|
|
&newaddr_command,
|
2016-09-06 09:17:49 +02:00
|
|
|
&invoice_command,
|
2016-09-06 09:17:49 +02:00
|
|
|
&listinvoice_command,
|
2016-09-06 09:17:49 +02:00
|
|
|
&delinvoice_command,
|
2016-09-06 09:17:49 +02:00
|
|
|
&waitinvoice_command,
|
2016-10-02 12:22:28 +02:00
|
|
|
&getchannels_command,
|
2016-08-31 08:36:08 +02:00
|
|
|
&getroute_command,
|
|
|
|
&sendpay_command,
|
2016-09-13 21:45:53 +02:00
|
|
|
&getinfo_command,
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Developer/debugging options. */
|
2016-09-06 09:17:49 +02:00
|
|
|
&dev_newhtlc_command,
|
|
|
|
&dev_fulfillhtlc_command,
|
|
|
|
&dev_failhtlc_command,
|
|
|
|
&dev_commit_command,
|
|
|
|
&dev_feerate_command,
|
|
|
|
&dev_rhash_command,
|
|
|
|
&dev_mocktime_command,
|
|
|
|
&dev_crash_command,
|
|
|
|
&dev_restart_command,
|
|
|
|
&dev_disconnect_command,
|
|
|
|
&dev_reconnect_command,
|
|
|
|
&dev_signcommit_command,
|
|
|
|
&dev_output_command,
|
|
|
|
&dev_add_route_command,
|
|
|
|
&dev_routefail_command,
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_help(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_result *response = new_json_result(cmd);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
json_array_start(response, NULL);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(cmdlist); i++) {
|
|
|
|
json_add_object(response,
|
|
|
|
"command", JSMN_STRING,
|
|
|
|
cmdlist[i]->name,
|
|
|
|
"description", JSMN_STRING,
|
|
|
|
cmdlist[i]->description,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
2016-01-21 21:11:48 +01:00
|
|
|
command_success(cmd, response);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command *find_cmd(const char *buffer,
|
|
|
|
const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* cmdlist[i]->name can be NULL in test code. */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(cmdlist); i++)
|
|
|
|
if (cmdlist[i]->name
|
|
|
|
&& json_tok_streq(buffer, tok, cmdlist[i]->name))
|
|
|
|
return cmdlist[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static void json_result(struct json_connection *jcon,
|
|
|
|
const char *id, const char *res, const char *err)
|
|
|
|
{
|
|
|
|
struct json_output *out = tal(jcon, struct json_output);
|
|
|
|
|
|
|
|
out->json = tal_fmt(out,
|
|
|
|
"{ \"result\" : %s,"
|
|
|
|
" \"error\" : %s,"
|
|
|
|
" \"id\" : %s }\n",
|
|
|
|
res, err, id);
|
|
|
|
|
|
|
|
/* Queue for writing, and wake writer (and maybe reader). */
|
|
|
|
list_add_tail(&jcon->output, &out->list);
|
|
|
|
io_wake(jcon);
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
struct json_result *null_response(const tal_t *ctx)
|
|
|
|
{
|
|
|
|
struct json_result *response;
|
|
|
|
|
|
|
|
response = new_json_result(ctx);
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_object_end(response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
void command_success(struct command *cmd, struct json_result *result)
|
|
|
|
{
|
|
|
|
struct json_connection *jcon = cmd->jcon;
|
|
|
|
|
|
|
|
if (!jcon) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(cmd->dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Command returned result after jcon close");
|
|
|
|
tal_free(cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(jcon->current == cmd);
|
|
|
|
json_result(jcon, cmd->id, json_result_string(result), "null");
|
2016-08-18 06:55:14 +02:00
|
|
|
log_debug(jcon->log, "Success");
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->current = tal_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_fail(struct command *cmd, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char *quote, *error;
|
|
|
|
struct json_connection *jcon = cmd->jcon;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (!jcon) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(cmd->dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Command failed after jcon close");
|
|
|
|
tal_free(cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error = tal_vfmt(cmd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
log_debug(jcon->log, "Failing: %s", error);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Remove " */
|
|
|
|
while ((quote = strchr(error, '"')) != NULL)
|
|
|
|
*quote = '\'';
|
|
|
|
|
|
|
|
/* Now surround in quotes. */
|
|
|
|
quote = tal_fmt(cmd, "\"%s\"", error);
|
|
|
|
|
|
|
|
assert(jcon->current == cmd);
|
|
|
|
json_result(jcon, cmd->id, "null", quote);
|
|
|
|
jcon->current = tal_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void json_command_malformed(struct json_connection *jcon,
|
|
|
|
const char *id,
|
|
|
|
const char *error)
|
|
|
|
{
|
|
|
|
return json_result(jcon, id, "null", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
const jsmntok_t *method, *id, *params;
|
|
|
|
const struct json_command *cmd;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
assert(!jcon->current);
|
2016-01-21 21:11:48 +01:00
|
|
|
if (tok[0].type != JSMN_OBJECT) {
|
2016-01-21 21:11:48 +01:00
|
|
|
json_command_malformed(jcon, "null",
|
|
|
|
"Expected {} for json command");
|
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
method = json_get_member(jcon->buffer, tok, "method");
|
|
|
|
params = json_get_member(jcon->buffer, tok, "params");
|
|
|
|
id = json_get_member(jcon->buffer, tok, "id");
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!id) {
|
|
|
|
json_command_malformed(jcon, "null", "No id");
|
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
if (id->type != JSMN_STRING && id->type != JSMN_PRIMITIVE) {
|
2016-01-21 21:11:48 +01:00
|
|
|
json_command_malformed(jcon, "null",
|
|
|
|
"Expected string/primitive for id");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a convenient tal parent for durarion of command
|
|
|
|
* (which may outlive the conn!). */
|
2016-01-21 21:11:49 +01:00
|
|
|
jcon->current = tal(jcon->dstate, struct command);
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->current->jcon = jcon;
|
2016-01-21 21:11:49 +01:00
|
|
|
jcon->current->dstate = jcon->dstate;
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->current->id = tal_strndup(jcon->current,
|
|
|
|
json_tok_contents(jcon->buffer, id),
|
|
|
|
json_tok_len(id));
|
|
|
|
|
|
|
|
if (!method || !params) {
|
|
|
|
command_fail(jcon->current, method ? "No params" : "No method");
|
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method->type != JSMN_STRING) {
|
2016-01-21 21:11:48 +01:00
|
|
|
command_fail(jcon->current, "Expected string for method");
|
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = find_cmd(jcon->buffer, method);
|
|
|
|
if (!cmd) {
|
2016-01-21 21:11:48 +01:00
|
|
|
command_fail(jcon->current,
|
|
|
|
"Unknown command '%.*s'",
|
|
|
|
(int)(method->end - method->start),
|
|
|
|
jcon->buffer + method->start);
|
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (params->type != JSMN_ARRAY && params->type != JSMN_OBJECT) {
|
2016-01-21 21:11:48 +01:00
|
|
|
command_fail(jcon->current,
|
|
|
|
"Expected array or object for params");
|
|
|
|
return;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
cmd->dispatch(jcon->current, jcon->buffer, params);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *write_json(struct io_conn *conn,
|
|
|
|
struct json_connection *jcon)
|
|
|
|
{
|
|
|
|
struct json_output *out;
|
|
|
|
|
|
|
|
out = list_pop(&jcon->output, struct json_output, list);
|
|
|
|
if (!out) {
|
|
|
|
if (jcon->stop) {
|
|
|
|
log_unusual(jcon->log, "JSON-RPC shutdown");
|
|
|
|
/* Return us to toplevel lightningd.c */
|
2016-01-21 21:11:49 +01:00
|
|
|
io_break(jcon->dstate);
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reader can go again now. */
|
|
|
|
io_wake(jcon);
|
|
|
|
return io_out_wait(conn, jcon, write_json, jcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
jcon->outbuf = tal_steal(jcon, out->json);
|
|
|
|
tal_free(out);
|
|
|
|
|
|
|
|
log_io(jcon->log, false, jcon->outbuf, strlen(jcon->outbuf));
|
|
|
|
return io_write(conn,
|
|
|
|
jcon->outbuf, strlen(jcon->outbuf), write_json, jcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *read_json(struct io_conn *conn,
|
|
|
|
struct json_connection *jcon)
|
|
|
|
{
|
|
|
|
jsmntok_t *toks;
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
log_io(jcon->log, true, jcon->buffer + jcon->used, jcon->len_read);
|
|
|
|
|
|
|
|
/* Resize larger if we're full. */
|
|
|
|
jcon->used += jcon->len_read;
|
|
|
|
if (jcon->used == tal_count(jcon->buffer))
|
|
|
|
tal_resize(&jcon->buffer, jcon->used * 2);
|
|
|
|
|
|
|
|
again:
|
|
|
|
toks = json_parse_input(jcon->buffer, jcon->used, &valid);
|
|
|
|
if (!toks) {
|
|
|
|
if (!valid) {
|
2016-01-21 21:11:49 +01:00
|
|
|
log_unusual(jcon->dstate->base_log,
|
2016-01-21 21:11:48 +01:00
|
|
|
"Invalid token in json input: '%.*s'",
|
|
|
|
(int)jcon->used, jcon->buffer);
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
/* We need more. */
|
|
|
|
goto read_more;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty buffer? (eg. just whitespace). */
|
|
|
|
if (tal_count(toks) == 1) {
|
|
|
|
jcon->used = 0;
|
|
|
|
goto read_more;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
parse_request(jcon, toks);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* Remove first {}. */
|
|
|
|
memmove(jcon->buffer, jcon->buffer + toks[0].end,
|
|
|
|
tal_count(jcon->buffer) - toks[0].end);
|
|
|
|
jcon->used -= toks[0].end;
|
|
|
|
tal_free(toks);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Need to wait for command to finish? */
|
|
|
|
if (jcon->current) {
|
|
|
|
jcon->len_read = 0;
|
|
|
|
return io_wait(conn, jcon, read_json, jcon);
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* See if we can parse the rest. */
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
read_more:
|
|
|
|
tal_free(toks);
|
|
|
|
return io_read_partial(conn, jcon->buffer + jcon->used,
|
|
|
|
tal_count(jcon->buffer) - jcon->used,
|
|
|
|
&jcon->len_read, read_json, jcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *jcon_connected(struct io_conn *conn,
|
2016-01-21 21:11:49 +01:00
|
|
|
struct lightningd_state *dstate)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct json_connection *jcon;
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
jcon = tal(dstate, struct json_connection);
|
|
|
|
jcon->dstate = dstate;
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->used = 0;
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->buffer = tal_arr(jcon, char, 64);
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->stop = false;
|
2016-01-21 21:11:48 +01:00
|
|
|
jcon->current = NULL;
|
2016-01-21 21:11:49 +01:00
|
|
|
jcon->log = new_log(jcon, dstate->log_record, "%sjcon fd %i:",
|
|
|
|
log_prefix(dstate->base_log), io_conn_fd(conn));
|
2016-01-21 21:11:48 +01:00
|
|
|
list_head_init(&jcon->output);
|
|
|
|
|
|
|
|
io_set_finish(conn, finish_jcon, jcon);
|
|
|
|
|
|
|
|
return io_duplex(conn,
|
|
|
|
io_read_partial(conn, jcon->buffer,
|
|
|
|
tal_count(jcon->buffer),
|
|
|
|
&jcon->len_read, read_json, jcon),
|
|
|
|
write_json(conn, jcon));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *incoming_jcon_connected(struct io_conn *conn,
|
2016-01-21 21:11:49 +01:00
|
|
|
struct lightningd_state *dstate)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-01-21 21:11:49 +01:00
|
|
|
log_info(dstate->base_log, "Connected json input");
|
|
|
|
return jcon_connected(conn, dstate);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
void setup_jsonrpc(struct lightningd_state *dstate, const char *rpc_filename)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int fd, old_umask;
|
|
|
|
|
|
|
|
if (streq(rpc_filename, ""))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (streq(rpc_filename, "/dev/tty")) {
|
|
|
|
fd = open(rpc_filename, O_RDWR);
|
|
|
|
if (fd == -1)
|
|
|
|
err(1, "Opening %s", rpc_filename);
|
2016-01-21 21:11:49 +01:00
|
|
|
io_new_conn(dstate, fd, jcon_connected, dstate);
|
2016-01-21 21:11:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (strlen(rpc_filename) + 1 > sizeof(addr.sun_path))
|
|
|
|
errx(1, "rpc filename '%s' too long", rpc_filename);
|
|
|
|
strcpy(addr.sun_path, rpc_filename);
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
/* Of course, this is racy! */
|
|
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0)
|
|
|
|
errx(1, "rpc filename '%s' in use", rpc_filename);
|
|
|
|
unlink(rpc_filename);
|
|
|
|
|
|
|
|
/* This file is only rw by us! */
|
|
|
|
old_umask = umask(0177);
|
|
|
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
|
|
|
|
err(1, "Binding rpc socket to '%s'", rpc_filename);
|
|
|
|
umask(old_umask);
|
|
|
|
|
|
|
|
if (listen(fd, 1) != 0)
|
|
|
|
err(1, "Listening on '%s'", rpc_filename);
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
io_new_listener(dstate, fd, incoming_jcon_connected, dstate);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|