Calls itself repeatedly: first time, result is NULL */
| 135 | |
| 136 | /* Calls itself repeatedly: first time, result is NULL */ |
| 137 | static struct command_result *send_response(struct command *command UNUSED, |
| 138 | const char *buf UNUSED, |
| 139 | const jsmntok_t *result, |
| 140 | struct reply *reply) |
| 141 | { |
| 142 | size_t msglen = reply->len - reply->off; |
| 143 | u8 *cmd_msg; |
| 144 | enum commando_msgtype msgtype; |
| 145 | struct out_req *req; |
| 146 | |
| 147 | /* Limit is 64k, but there's a little overhead */ |
| 148 | if (msglen > 65000) { |
| 149 | msglen = 65000; |
| 150 | msgtype = COMMANDO_MSG_REPLY_CONTINUES; |
| 151 | /* We need to make a copy first time before we call back, since |
| 152 | * plugin will reuse it! */ |
| 153 | if (!result) |
| 154 | reply->buf = tal_dup_talarr(reply, char, reply->buf); |
| 155 | } else { |
| 156 | if (msglen == 0) { |
| 157 | tal_free(reply); |
| 158 | return command_done(); |
| 159 | } |
| 160 | msgtype = COMMANDO_MSG_REPLY_TERM; |
| 161 | } |
| 162 | |
| 163 | cmd_msg = tal_arr(NULL, u8, 0); |
| 164 | towire_u16(&cmd_msg, msgtype); |
| 165 | towire_u64(&cmd_msg, reply->incoming->id); |
| 166 | towire(&cmd_msg, reply->buf + reply->off, msglen); |
| 167 | reply->off += msglen; |
| 168 | |
| 169 | req = jsonrpc_request_start(plugin, NULL, "sendcustommsg", |
| 170 | send_response, send_response, |
| 171 | reply); |
| 172 | json_add_node_id(req->js, "node_id", &reply->incoming->peer); |
| 173 | json_add_hex_talarr(req->js, "msg", cmd_msg); |
| 174 | tal_free(cmd_msg); |
| 175 | send_outreq(plugin, req); |
| 176 | |
| 177 | return command_done(); |
| 178 | } |
| 179 | |
| 180 | static struct command_result *cmd_done(struct command *command, |
| 181 | const char *buf, |
no test coverage detected