|
1 | 1 | #include "config.h" |
2 | 2 | #include <ccan/array_size/array_size.h> |
| 3 | +#include <ccan/cast/cast.h> |
3 | 4 | #include <ccan/crypto/siphash24/siphash24.h> |
4 | 5 | #include <ccan/htable/htable_type.h> |
5 | 6 | #include <ccan/json_escape/json_escape.h> |
@@ -963,6 +964,30 @@ static struct command_result *save_rune(struct command *cmd, |
963 | 964 | forward_error, rune); |
964 | 965 | } |
965 | 966 |
|
| 967 | +static void towire_blacklist(u8 **pptr, const struct blacklist *b) |
| 968 | +{ |
| 969 | + for (size_t i = 0; i < tal_count(b); i++) { |
| 970 | + towire_u64(pptr, b[i].start); |
| 971 | + towire_u64(pptr, b[i].end); |
| 972 | + } |
| 973 | +} |
| 974 | + |
| 975 | +static struct blacklist *fromwire_blacklist(const tal_t *ctx, |
| 976 | + const u8 **cursor, |
| 977 | + size_t *max) |
| 978 | +{ |
| 979 | + struct blacklist *blist = tal_arr(ctx, struct blacklist, 0); |
| 980 | + while (*max > 0) { |
| 981 | + struct blacklist b; |
| 982 | + b.start = fromwire_u64(cursor, max); |
| 983 | + b.end = fromwire_u64(cursor, max); |
| 984 | + tal_arr_expand(&blist, b); |
| 985 | + } |
| 986 | + if (!*cursor) { |
| 987 | + return tal_free(blist); |
| 988 | + } |
| 989 | + return blist; |
| 990 | +} |
966 | 991 |
|
967 | 992 | static struct command_result *json_commando_rune(struct command *cmd, |
968 | 993 | const char *buffer, |
@@ -1092,11 +1117,20 @@ static struct command_result *list_blacklist(struct command *cmd) |
1092 | 1117 | return command_finished(cmd, js); |
1093 | 1118 | } |
1094 | 1119 |
|
| 1120 | +static struct command_result *blacklist_save_done(struct command *cmd, |
| 1121 | + const char *buf, |
| 1122 | + const jsmntok_t *result, |
| 1123 | + void *unused) |
| 1124 | +{ |
| 1125 | + return list_blacklist(cmd); |
| 1126 | +} |
| 1127 | + |
1095 | 1128 | static struct command_result *json_commando_blacklist(struct command *cmd, |
1096 | 1129 | const char *buffer, |
1097 | 1130 | const jsmntok_t *params) |
1098 | 1131 | { |
1099 | 1132 | u64 *start, *end; |
| 1133 | + u8 *bwire; |
1100 | 1134 | struct blacklist *entry, *newblacklist; |
1101 | 1135 |
|
1102 | 1136 | if (!param(cmd, buffer, params, |
@@ -1145,7 +1179,9 @@ static struct command_result *json_commando_blacklist(struct command *cmd, |
1145 | 1179 | } |
1146 | 1180 | tal_free(blacklist); |
1147 | 1181 | blacklist = newblacklist; |
1148 | | - return list_blacklist(cmd); |
| 1182 | + bwire = tal_arr(tmpctx, u8, 0); |
| 1183 | + towire_blacklist(&bwire, blacklist); |
| 1184 | + return jsonrpc_set_datastore_binary(cmd->plugin, cmd, "commando/blacklist", bwire, "create-or-replace", blacklist_save_done, NULL, NULL); |
1149 | 1185 | } |
1150 | 1186 |
|
1151 | 1187 | static struct command_result *json_commando_listrunes(struct command *cmd, |
@@ -1186,7 +1222,19 @@ static const char *init(struct plugin *p, |
1186 | 1222 | { |
1187 | 1223 | struct secret rune_secret; |
1188 | 1224 | const char *err; |
1189 | | - |
| 1225 | + u8 *bwire; |
| 1226 | + |
| 1227 | + if (rpc_scan_datastore_hex(tmpctx, p, "commando/blacklist", |
| 1228 | + JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex, |
| 1229 | + &bwire)) == NULL) { |
| 1230 | + size_t max = tal_bytelen(bwire); |
| 1231 | + blacklist = fromwire_blacklist(p, cast_const2(const u8 **, |
| 1232 | + &bwire), |
| 1233 | + &max); |
| 1234 | + if (blacklist == NULL) { |
| 1235 | + plugin_err(p, "Invalid commando/blacklist"); |
| 1236 | + } |
| 1237 | + } |
1190 | 1238 | outgoing_commands = tal_arr(p, struct commando *, 0); |
1191 | 1239 | incoming_commands = tal_arr(p, struct commando *, 0); |
1192 | 1240 | usage_table = tal(p, struct usage_table); |
|
0 commit comments