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