#ifndef LIGHTNING_PLUGINS_LIBPLUGIN_H
#define LIGHTNING_PLUGINS_LIBPLUGIN_H
#include "config.h"
#include <bitcoin/tx.h>
#include <ccan/intmap/intmap.h>
#include <ccan/membuf/membuf.h>
#include <ccan/strmap/strmap.h>
#include <ccan/time/time.h>
#include <ccan/timer/timer.h>
#include <common/errcode.h>
#include <common/features.h>
#include <common/htlc.h>
#include <common/json_command.h>
#include <common/jsonrpc_errors.h>
#include <common/node_id.h>
#include <common/status_levels.h>
#include <common/utils.h>
#include <stdarg.h>
struct json_out;
struct htable;
struct plugin;
struct rpc_conn;
extern bool deprecated_apis;
enum plugin_restartability {
PLUGIN_STATIC,
PLUGIN_RESTARTABLE
};
struct out_req {
const char *id;
struct command *cmd;
struct json_stream *js;
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 *error,
void *arg);
void *arg;
};
struct command {
const char *id;
const char *methodname;
bool usage_only;
struct plugin *plugin;
struct json_filter *filter;
};
struct plugin_command {
const char *name;
const char *category;
const char *description;
const char *long_description;
struct command_result *(*handle)(struct command *cmd,
const char *buf,
const jsmntok_t *params);
bool deprecated;
bool dev_only;
};
struct plugin_option {
const char *name;
const char *type;
const char *description;
char *(*handle)(struct plugin *plugin, const char *str, void *arg);
void *arg;
bool dev_only;
bool deprecated;
bool dynamic;
};
struct plugin_notification {
const char *name;
struct command_result* (*handle)(struct command *cmd,
const char *buf,
const jsmntok_t *params);
};
struct plugin_hook {
const char *name;
struct command_result *(*handle)(struct command *cmd,
const char *buf,
const jsmntok_t *params);
const char **before, **after;
};
const struct feature_set *plugin_feature_set(const struct plugin *p);
struct out_req *jsonrpc_request_start_(struct plugin *plugin,
struct command *cmd,
const char *method,
const char *id_prefix,
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);
#define jsonrpc_request_start(plugin, cmd, method, cb, errcb, arg) \
jsonrpc_request_start_((plugin), (cmd), (method), \
json_id_prefix(tmpctx, (cmd)), \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
typesafe_cb_preargs(struct command_result *, void *, \
(errcb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
(arg))
#define jsonrpc_request_whole_object_start(plugin, cmd, method, id_prefix, cb, arg) \
jsonrpc_request_start_((plugin), (cmd), (method), (id_prefix), \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
NULL, \
(arg))
struct json_stream *jsonrpc_stream_success(struct command *cmd);
struct json_stream *jsonrpc_stream_fail(struct command *cmd,
int code,
const char *err);
struct json_stream *jsonrpc_stream_fail_data(struct command *cmd,
int code,
const char *err);
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);
#define jsonrpc_set_datastore_string(plugin, cmd, path, str, mode, cb, errcb, arg) \
jsonrpc_set_datastore_((plugin), (cmd), (path), (str), true, (mode), \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
typesafe_cb_preargs(struct command_result *, void *, \
(errcb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
(arg))
#define jsonrpc_set_datastore_binary(plugin, cmd, path, tal_ptr, mode, cb, errcb, arg) \
jsonrpc_set_datastore_((plugin), (cmd), (path), (tal_ptr), false, (mode), \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
typesafe_cb_preargs(struct command_result *, void *, \
(errcb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
(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);
#define jsonrpc_get_datastore_string(plugin, cmd, path, cb, arg) \
jsonrpc_get_datastore_((plugin), (cmd), (path), \
typesafe_cb_preargs(struct command_result *, \
void *, \
(cb), (arg), \
struct command *command, \
const char *val), \
NULL, \
(arg))
#define jsonrpc_get_datastore_binary(plugin, cmd, path, cb, arg) \
jsonrpc_get_datastore_((plugin), (cmd), (path), \
NULL, \
typesafe_cb_preargs(struct command_result *, \
void *, \
(cb), (arg), \
struct command *command, \
const u8 *val), \
(arg))
WARN_UNUSED_RESULT
struct command_result *command_finished(struct command *cmd, struct json_stream *response);
WARN_UNUSED_RESULT
struct command_result *command_still_pending(struct command *cmd);
struct json_out *json_out_obj(const tal_t *ctx,
const char *fieldname,
const char *str);
struct command_result *command_param_failed(void);
void NORETURN plugin_err(struct plugin *p, const char *fmt, ...);
void NORETURN plugin_errv(struct plugin *p, const char *fmt, va_list ap);
void NORETURN plugin_exit(struct plugin *p, int exitcode);
struct command_result *WARN_UNUSED_RESULT
command_done_err(struct command *cmd,
enum jsonrpc_errcode code,
const char *errmsg,
const struct json_out *data);
struct command_result *command_err_raw(struct command *cmd,
const char *json_str);
struct command_result *WARN_UNUSED_RESULT
command_success(struct command *cmd, const struct json_out *result);
struct command_result *WARN_UNUSED_RESULT
command_hook_success(struct command *cmd);
struct command_result *WARN_UNUSED_RESULT
notification_handled(struct command *cmd);
#define notification_handler_pending(cmd) command_still_pending(cmd)
void rpc_scan(struct plugin *plugin,
const char *method,
const struct json_out *params TAKES,
const char *guide,
...);
const char *rpc_scan_datastore_str(const tal_t *ctx,
struct plugin *plugin,
const char *path,
...);
const char *rpc_scan_datastore_hex(const tal_t *ctx,
struct plugin *plugin,
const char *path,
...);
void rpc_enable_batching(struct plugin *plugin);
struct command_result *send_outreq(struct plugin *plugin,
const struct out_req *req);
struct command_result *forward_error(struct command *cmd,
const char *buf,
const jsmntok_t *error,
void *arg);
struct command_result *forward_result(struct command *cmd,
const char *buf,
const jsmntok_t *result,
void *arg);
struct command_result *timer_complete(struct plugin *p);
struct command_result *command_done(void);
struct plugin_timer *plugin_timer_(struct plugin *p,
struct timerel t,
void (*cb)(void *cb_arg),
void *cb_arg);
#define plugin_timer(plugin, time, cb, cb_arg) \
plugin_timer_((plugin), (time), \
typesafe_cb(void, void *, \
(cb), (cb_arg)), \
(cb_arg)) \
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4);
void plugin_logv(struct plugin *p, enum log_level l, const char *fmt, va_list ap);
struct json_stream *plugin_notify_start(struct command *cmd, const char *method);
void plugin_notify_end(struct command *cmd, struct json_stream *js);
struct json_stream *plugin_notification_start(struct plugin *plugins,
const char *method);
void plugin_notification_end(struct plugin *plugin,
struct json_stream *stream TAKES);
void plugin_notify_message(struct command *cmd,
enum log_level level,
const char *fmt, ...)
PRINTF_FMT(3, 4);
void plugin_notify_progress(struct command *cmd,
u32 num_stages, u32 stage,
u32 num_progress, u32 progress);
static inline void *plugin_option_cb_check(char *(*set)(struct plugin *plugin,
const char *arg, void *))
{
return set;
}
bool plugin_developer_mode(const struct plugin *plugin);
#define plugin_option_(name, type, description, set, arg, dev_only, deprecated, dynamic) \
(name), \
(type), \
(description), \
plugin_option_cb_check(typesafe_cb_preargs(char *, void *, \
(set), (arg), \
struct plugin *, \
const char *)), \
(arg), \
(dev_only), \
(deprecated), \
(dynamic)
#define plugin_option(name, type, description, set, arg) \
plugin_option_((name), (type), (description), (set), (arg), false, false, false)
#define plugin_option_dev(name, type, description, set, arg) \
plugin_option_((name), (type), (description), (set), (arg), true, false, false)
#define plugin_option_dynamic(name, type, description, set, arg) \
plugin_option_((name), (type), (description), (set), (arg), false, false, true)
#define plugin_option_deprecated(name, type, description, set, arg) \
plugin_option_((name), (type), (description), (set), (arg), false, true, false)
char *u64_option(struct plugin *plugin, const char *arg, u64 *i);
char *u32_option(struct plugin *plugin, const char *arg, u32 *i);
char *u16_option(struct plugin *plugin, const char *arg, u16 *i);
char *bool_option(struct plugin *plugin, const char *arg, bool *i);
char *charp_option(struct plugin *plugin, const char *arg, char **p);
char *flag_option(struct plugin *plugin, const char *arg, bool *i);
void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
const char *(*init)(struct plugin *p,
const char *buf,
const jsmntok_t *),
const enum plugin_restartability restartability,
bool init_rpc,
struct feature_set *features STEALS,
const struct plugin_command *commands TAKES,
size_t num_commands,
const struct plugin_notification *notif_subs TAKES,
size_t num_notif_subs,
const struct plugin_hook *hook_subs TAKES,
size_t num_hook_subs,
const char **notif_topics TAKES,
size_t num_notif_topics,
...);
struct listpeers_channel {
struct node_id id;
bool connected;
bool private;
struct bitcoin_txid funding_txid;
const char *state;
struct short_channel_id *alias[NUM_SIDES];
struct short_channel_id *scid;
int direction;
struct amount_msat total_msat;
struct amount_msat spendable_msat;
u16 max_accepted_htlcs;
size_t num_htlcs;
};
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok);
struct createonion_response {
u8 *onion;
struct secret *shared_secrets;
};
struct createonion_response *json_to_createonion_response(const tal_t *ctx,
const char *buffer,
const jsmntok_t *toks);
struct route_hop *json_to_route(const tal_t *ctx, const char *buffer,
const jsmntok_t *toks);
const char *json_id_prefix(const tal_t *ctx, const struct command *cmd);
void plugin_set_memleak_handler(struct plugin *plugin,
void (*mark_mem)(struct plugin *plugin,
struct htable *memtable));
const jsmntok_t *jsonrpc_request_sync(const tal_t *ctx, struct plugin *plugin,
const char *method,
const struct json_out *params TAKES,
const char **resp);
#endif