| 644 | } |
| 645 | |
| 646 | static struct command_result *json_commando(struct command *cmd, |
| 647 | const char *buffer, |
| 648 | const jsmntok_t *params) |
| 649 | { |
| 650 | struct node_id *peer; |
| 651 | const char *method, *cparams; |
| 652 | const char *rune; |
| 653 | struct commando *ocmd; |
| 654 | struct outgoing *outgoing; |
| 655 | char *json; |
| 656 | size_t jsonlen; |
| 657 | |
| 658 | if (!param(cmd, buffer, params, |
| 659 | p_req("peer_id", param_node_id, &peer), |
| 660 | p_req("method", param_string, &method), |
| 661 | p_opt("params", param_string, &cparams), |
| 662 | p_opt("rune", param_string, &rune), |
| 663 | NULL)) |
| 664 | return command_param_failed(); |
| 665 | |
| 666 | ocmd = tal(cmd, struct commando); |
| 667 | ocmd->cmd = cmd; |
| 668 | ocmd->peer = *peer; |
| 669 | ocmd->contents = tal_arr(ocmd, u8, 0); |
| 670 | do { |
| 671 | ocmd->id = pseudorand_u64(); |
| 672 | } while (find_commando(outgoing_commands, NULL, &ocmd->id)); |
| 673 | tal_arr_expand(&outgoing_commands, ocmd); |
| 674 | tal_add_destructor2(ocmd, destroy_commando, &outgoing_commands); |
| 675 | |
| 676 | json = tal_fmt(tmpctx, |
| 677 | "{\"method\":\"%s\",\"params\":%s", method, |
| 678 | cparams ? cparams : "{}"); |
| 679 | if (rune) |
| 680 | tal_append_fmt(&json, ",\"rune\":\"%s\"", rune); |
| 681 | tal_append_fmt(&json, "}"); |
| 682 | |
| 683 | outgoing = tal(cmd, struct outgoing); |
| 684 | outgoing->peer = *peer; |
| 685 | outgoing->msg_off = 0; |
| 686 | /* 65000 per message gives sufficient headroom. */ |
| 687 | jsonlen = tal_bytelen(json)-1; |
| 688 | outgoing->msgs = tal_arr(cmd, u8 *, (jsonlen + 64999) / 65000); |
| 689 | for (size_t i = 0; i < tal_count(outgoing->msgs); i++) { |
| 690 | u8 *cmd_msg = tal_arr(outgoing, u8, 0); |
| 691 | bool terminal = (i == tal_count(outgoing->msgs) - 1); |
| 692 | size_t off = i * 65000, len; |
| 693 | |
| 694 | if (terminal) |
| 695 | len = jsonlen - off; |
| 696 | else |
| 697 | len = 65000; |
| 698 | |
| 699 | towire_u16(&cmd_msg, |
| 700 | terminal ? COMMANDO_MSG_CMD_TERM |
| 701 | : COMMANDO_MSG_CMD_CONTINUES); |
| 702 | towire_u64(&cmd_msg, ocmd->id); |
| 703 | towire(&cmd_msg, json + off, len); |
nothing calls this directly
no test coverage detected