2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_JSONRPC_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_JSONRPC_H
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "config.h"
|
2018-02-24 15:59:39 +01:00
|
|
|
#include <bitcoin/chainparams.h>
|
2017-01-04 04:38:15 +01:00
|
|
|
#include <ccan/autodata/autodata.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <ccan/list/list.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/json.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <lightningd/json_stream.h>
|
2018-10-19 03:17:48 +02:00
|
|
|
#include <stdarg.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-09-12 17:56:39 +02:00
|
|
|
/* The command mode tells param() how to process. */
|
|
|
|
enum command_mode {
|
|
|
|
/* Normal command processing */
|
|
|
|
CMD_NORMAL,
|
|
|
|
/* Create command usage string, nothing else. */
|
2018-11-27 00:48:18 +01:00
|
|
|
CMD_USAGE,
|
|
|
|
/* Check parameters, nothing else. */
|
|
|
|
CMD_CHECK
|
2018-09-12 17:56:39 +02:00
|
|
|
};
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Context for a command (from JSON, but might outlive the connection!). */
|
|
|
|
/* FIXME: move definition into jsonrpc.c */
|
2016-01-21 21:11:48 +01:00
|
|
|
struct command {
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Off json_cmd->commands */
|
|
|
|
struct list_node list;
|
2016-01-21 21:11:48 +01:00
|
|
|
/* The global state */
|
2017-08-28 18:09:01 +02:00
|
|
|
struct lightningd *ld;
|
2016-01-21 21:11:48 +01:00
|
|
|
/* The 'id' which we need to include in the response. */
|
|
|
|
const char *id;
|
2018-07-26 23:19:37 +02:00
|
|
|
/* What command we're running (for logging) */
|
|
|
|
const struct json_command *json_cmd;
|
2016-01-21 21:11:48 +01:00
|
|
|
/* The connection, or NULL if it closed. */
|
|
|
|
struct json_connection *jcon;
|
2017-12-15 11:15:54 +01:00
|
|
|
/* Have we been marked by command_still_pending? For debugging... */
|
|
|
|
bool pending;
|
2018-09-12 17:56:39 +02:00
|
|
|
/* Tell param() how to process the command */
|
|
|
|
enum command_mode mode;
|
|
|
|
/* This is created if mode is CMD_USAGE */
|
|
|
|
const char *usage;
|
2018-09-15 19:12:15 +02:00
|
|
|
bool *ok;
|
2018-11-26 16:30:37 +01:00
|
|
|
/* Do not report unused parameters as errors (default false). */
|
|
|
|
bool allow_unused;
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Have we started a json stream already? For debugging. */
|
|
|
|
bool have_json_stream;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_command {
|
|
|
|
const char *name;
|
2016-01-21 21:11:48 +01:00
|
|
|
void (*dispatch)(struct command *,
|
2018-12-08 01:34:56 +01:00
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj,
|
|
|
|
const jsmntok_t *params);
|
2016-01-21 21:11:48 +01:00
|
|
|
const char *description;
|
2018-01-16 20:43:14 +01:00
|
|
|
bool deprecated;
|
2018-01-29 01:30:15 +01:00
|
|
|
const char *verbose;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/**
|
|
|
|
* json_stream_success - start streaming a successful json result.
|
|
|
|
* @cmd: the command we're running.
|
|
|
|
*
|
|
|
|
* The returned value should go to command_success() when done.
|
|
|
|
* json_add_* will be placed into the 'result' field of the JSON reply.
|
|
|
|
*/
|
|
|
|
struct json_stream *json_stream_success(struct command *cmd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* json_stream_fail - start streaming a failed json result.
|
|
|
|
* @cmd: the command we're running.
|
|
|
|
* @code: the error code from lightningd/jsonrpc_errors.h
|
|
|
|
* @errmsg: the error string.
|
|
|
|
*
|
|
|
|
* The returned value should go to command_failed() when done;
|
|
|
|
* json_add_* will be placed into the 'data' field of the 'error' JSON reply.
|
|
|
|
*/
|
|
|
|
struct json_stream *json_stream_fail(struct command *cmd,
|
|
|
|
int code,
|
|
|
|
const char *errmsg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* json_stream_fail_nodata - start streaming a failed json result.
|
|
|
|
* @cmd: the command we're running.
|
|
|
|
* @code: the error code from lightningd/jsonrpc_errors.h
|
|
|
|
* @errmsg: the error string.
|
|
|
|
*
|
|
|
|
* This is used by command_fail(), which doesn't add any JSON data.
|
|
|
|
*/
|
|
|
|
struct json_stream *json_stream_fail_nodata(struct command *cmd,
|
|
|
|
int code,
|
|
|
|
const char *errmsg);
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *null_response(struct command *cmd);
|
|
|
|
void command_success(struct command *cmd, struct json_stream *response);
|
|
|
|
void command_failed(struct command *cmd, struct json_stream *result);
|
2018-05-24 23:40:18 +02:00
|
|
|
void PRINTF_FMT(3, 4) command_fail(struct command *cmd, int code,
|
|
|
|
const char *fmt, ...);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2017-12-15 11:15:54 +01:00
|
|
|
/* Mainly for documentation, that we plan to close this later. */
|
|
|
|
void command_still_pending(struct command *cmd);
|
|
|
|
|
2018-11-22 11:37:08 +01:00
|
|
|
/**
|
|
|
|
* Create a new jsonrpc to wrap all related information.
|
|
|
|
*
|
|
|
|
* This doesn't setup the listener yet, see `jsonrpc_listen` for
|
|
|
|
* that. This just creates the container for all jsonrpc-related
|
|
|
|
* information so we can start gathering it before actually starting.
|
|
|
|
*/
|
|
|
|
struct jsonrpc *jsonrpc_new(const tal_t *ctx, struct lightningd *ld);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listeing on ld->rpc_filename.
|
|
|
|
*
|
|
|
|
* Sets up the listener effectively starting the RPC interface.
|
|
|
|
*/
|
|
|
|
void jsonrpc_listen(struct jsonrpc *rpc, struct lightningd *ld);
|
2018-11-14 08:12:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new command/method to the JSON-RPC interface.
|
|
|
|
*
|
|
|
|
* Returns true if the command was added correctly, false if adding
|
|
|
|
* this would clobber a command name.
|
|
|
|
*/
|
|
|
|
bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-11-22 23:11:26 +01:00
|
|
|
/**
|
|
|
|
* Remove a command/method from the JSON-RPC.
|
|
|
|
*
|
|
|
|
* Used to dynamically remove a `struct json_command` from the
|
|
|
|
* JSON-RPC dispatch table by its name.
|
|
|
|
*/
|
|
|
|
void jsonrpc_command_remove(struct jsonrpc *rpc, const char *method);
|
|
|
|
|
2017-01-04 04:38:15 +01:00
|
|
|
AUTODATA_TYPE(json_command, struct json_command);
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_JSONRPC_H */
|