forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonrpc.h
271 lines (237 loc) · 8.27 KB
/
jsonrpc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef LIGHTNING_LIGHTNINGD_JSONRPC_H
#define LIGHTNING_LIGHTNINGD_JSONRPC_H
#include "config.h"
#include <ccan/list/list.h>
#include <common/autodata.h>
#include <common/json_stream.h>
#include <common/status_levels.h>
struct jsonrpc;
/* The command mode tells param() how to process. */
enum command_mode {
/* Normal command processing */
CMD_NORMAL,
/* Create command usage string, nothing else. */
CMD_USAGE,
/* Check parameters, nothing else. */
CMD_CHECK
};
/* Context for a command (from JSON, but might outlive the connection!). */
/* FIXME: move definition into jsonrpc.c */
struct command {
/* Off list jcon->commands */
struct list_node list;
/* The global state */
struct lightningd *ld;
/* The 'id' which we need to include in the response. */
const char *id;
/* If 'id' needs to be quoted (i.e. it's a string) */
bool id_is_string;
/* What command we're running (for logging) */
const struct json_command *json_cmd;
/* The connection, or NULL if it closed. */
struct json_connection *jcon;
/* Does this want notifications? */
bool send_notifications;
/* Have we been marked by command_still_pending? For debugging... */
bool pending;
/* Tell param() how to process the command */
enum command_mode mode;
/* Have we started a json stream already? For debugging. */
struct json_stream *json_stream;
};
/**
* Dummy structure to make sure you call one of
* command_success / command_failed / command_still_pending.
*/
struct command_result;
struct json_command {
const char *name;
const char *category;
struct command_result *(*dispatch)(struct command *,
const char *buffer,
const jsmntok_t *obj,
const jsmntok_t *params);
const char *description;
bool deprecated;
const char *verbose;
};
struct jsonrpc_notification {
/* The topic that this notification is for. Internally this
* will be serialized as "method", hence the different name
* here */
const char *method;
struct json_stream *stream;
};
struct jsonrpc_request {
const char *id;
const char *method;
struct json_stream *stream;
void (*notify_cb)(const char *buffer,
const jsmntok_t *idtok,
const jsmntok_t *methodtok,
const jsmntok_t *paramtoks,
void *);
void (*response_cb)(const char *buffer, const jsmntok_t *toks,
const jsmntok_t *idtok, void *);
void *response_cb_arg;
};
/**
* json_stream_success - start streaming a successful json result object.
* @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, with data object.
* @cmd: the command we're running.
* @code: the error code from common/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.
* You need to json_object_end() once you're done!
*/
struct json_stream *json_stream_fail(struct command *cmd,
enum jsonrpc_errcode 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 common/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,
enum jsonrpc_errcode code,
const char *errmsg);
/* These returned values are never NULL. */
struct command_result *command_success(struct command *cmd,
struct json_stream *response)
WARN_UNUSED_RESULT;
struct command_result *command_failed(struct command *cmd,
struct json_stream *result)
WARN_UNUSED_RESULT;
/* Mainly for documentation, that we plan to close this later. */
struct command_result *command_still_pending(struct command *cmd)
WARN_UNUSED_RESULT;
/* For low-level JSON stream access: */
struct json_stream *json_stream_raw_for_cmd(struct command *cmd);
void json_stream_log_suppress_for_cmd(struct json_stream *js,
const struct command *cmd);
struct command_result *command_raw_complete(struct command *cmd,
struct json_stream *result);
/* Logging point to use for this command (usually, the JSON connection). */
struct log *command_log(struct command *cmd);
/* To return if param() fails. */
extern struct command_result *command_param_failed(void)
WARN_UNUSED_RESULT;
/* Wrapper for pending commands (ignores return) */
static inline void was_pending(const struct command_result *res)
{
assert(res);
}
/* Transition for ignoring command */
static inline void fixme_ignore(const struct command_result *res)
{
}
/* Notifier to the caller. */
void json_notify_fmt(struct command *cmd,
enum log_level level,
const char *fmt, ...)
PRINTF_FMT(3, 4);
/* FIXME: For the few cases where return value is indeterminate */
struct command_result *command_its_complicated(const char *why);
/**
* 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.
*
* It initializes ld->jsonrpc.
*/
void jsonrpc_setup(struct lightningd *ld);
/**
* Start listening on ld->rpc_filename.
*
* Sets up the listener effectively starting the RPC interface.
*/
void jsonrpc_listen(struct jsonrpc *rpc, struct lightningd *ld);
/**
* Stop listening on ld->rpc_filename.
*
* No new connections from here in.
*/
void jsonrpc_stop_listening(struct jsonrpc *jsonrpc);
/**
* Kill any remaining JSON-RPC connections.
*/
void jsonrpc_stop_all(struct lightningd *ld);
/**
* 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.
*
* Free @command to remove it.
*/
bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command,
const char *usage TAKES);
/**
* Begin a JSON-RPC notification with the specified topic.
*
* Automatically starts the `params` object, hence only key-value
* based params are supported at the moment.
*/
struct jsonrpc_notification *jsonrpc_notification_start(const tal_t *ctx, const char *topic);
/**
* Counterpart to jsonrpc_notification_start.
*/
void jsonrpc_notification_end(struct jsonrpc_notification *n);
/**
* start a JSONRPC request; id_prefix is non-NULL if this was triggered by
* another JSONRPC request.
*/
#define jsonrpc_request_start(ctx, method, id_prefix, log, notify_cb, response_cb, response_cb_arg) \
jsonrpc_request_start_( \
(ctx), (method), (id_prefix), (log), true, \
typesafe_cb_preargs(void, void *, (notify_cb), (response_cb_arg), \
const char *buffer, \
const jsmntok_t *idtok, \
const jsmntok_t *methodtok, \
const jsmntok_t *paramtoks), \
typesafe_cb_preargs(void, void *, (response_cb), (response_cb_arg), \
const char *buffer, \
const jsmntok_t *toks, \
const jsmntok_t *idtok), \
(response_cb_arg))
#define jsonrpc_request_start_raw(ctx, method, id_prefix, log, notify_cb, response_cb, response_cb_arg) \
jsonrpc_request_start_( \
(ctx), (method), (id_prefix), (log), false, \
typesafe_cb_preargs(void, void *, (notify_cb), (response_cb_arg), \
const char *buffer, \
const jsmntok_t *idtok, \
const jsmntok_t *methodtok, \
const jsmntok_t *paramtoks), \
typesafe_cb_preargs(void, void *, (response_cb), (response_cb_arg), \
const char *buffer, \
const jsmntok_t *toks, \
const jsmntok_t *idtok), \
(response_cb_arg))
struct jsonrpc_request *jsonrpc_request_start_(
const tal_t *ctx, const char *method,
const char *id_prefix TAKES, struct log *log, bool add_header,
void (*notify_cb)(const char *buffer,
const jsmntok_t *idtok,
const jsmntok_t *methodtok,
const jsmntok_t *paramtoks,
void *),
void (*response_cb)(const char *buffer, const jsmntok_t *toks,
const jsmntok_t *idtok, void *),
void *response_cb_arg);
void jsonrpc_request_end(struct jsonrpc_request *request);
AUTODATA_TYPE(json_command, struct json_command);
#endif /* LIGHTNING_LIGHTNINGD_JSONRPC_H */