diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 9256151bf..1cb3e84e4 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -323,7 +323,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) switch ((enum common_wire_type)t) { #if DEVELOPER case WIRE_CUSTOMMSG_IN: - /* TODO(cdecker) Add handling of custom messages. */ + handle_custommsg_in(sd->ld, sd->node_id, msg); break; #else case WIRE_CUSTOMMSG_IN: diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 66eaed5f8..810a45fe5 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -926,7 +926,7 @@ static unsigned int openingd_msg(struct subd *openingd, switch ((enum common_wire_type)t) { #if DEVELOPER case WIRE_CUSTOMMSG_IN: - /* TODO(cdecker) Add handling of custom messages. */ + handle_custommsg_in(openingd->ld, openingd->node_id, msg); return 0; #else case WIRE_CUSTOMMSG_IN: diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 3de82c74a..bda3ff474 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -2375,6 +2375,45 @@ void peer_dev_memleak(struct command *cmd) peer_memleak_req_next(cmd, NULL); } +struct custommsg_payload { + struct node_id peer_id; + const u8 *msg; +}; + +static void custommsg_callback(struct custommsg_payload *payload, + const char *buffer, const jsmntok_t *toks) +{ + tal_free(payload); +} + +static void custommsg_payload_serialize(struct custommsg_payload *payload, + struct json_stream *stream) +{ + json_add_hex_talarr(stream, "message", payload->msg); + json_add_node_id(stream, "peer_id", &payload->peer_id); +} + +REGISTER_PLUGIN_HOOK(custommsg, custommsg_callback, struct custommsg_payload *, + custommsg_payload_serialize, struct custommsg_payload *); + +void handle_custommsg_in(struct lightningd *ld, const struct node_id *peer_id, + const u8 *msg) +{ + struct custommsg_payload *p = tal(NULL, struct custommsg_payload); + u8 *custommsg; + + if (!fromwire_custommsg_in(NULL, msg, &custommsg)) { + log_broken(ld->log, "Malformed custommsg from peer %s: %s", + type_to_string(tmpctx, struct node_id, peer_id), + tal_hex(tmpctx, msg)); + return; + } + + p->peer_id = *peer_id; + p->msg = tal_steal(p, custommsg); + plugin_hook_call_custommsg(ld, p, p); +} + static struct command_result *json_sendcustommsg(struct command *cmd, const char *buffer, const jsmntok_t *obj UNNEEDED, diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 100756699..0d53760a9 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -93,6 +93,8 @@ struct htlc_in_map *load_channels_from_wallet(struct lightningd *ld); #if DEVELOPER void peer_dev_memleak(struct command *cmd); +void handle_custommsg_in(struct lightningd *ld, const struct node_id *peer_id, + const u8 *msg); #endif /* DEVELOPER */ /* Triggered at each new block. */ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 4c0090a84..b6f5fcbef 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -111,6 +111,9 @@ bool fromwire_channel_sending_commitsig(const tal_t *ctx UNNEEDED, const void *p /* Generated stub for fromwire_connect_peer_connected */ bool fromwire_connect_peer_connected(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, struct wireaddr_internal *addr UNNEEDED, struct per_peer_state **pps UNNEEDED, u8 **features UNNEEDED) { fprintf(stderr, "fromwire_connect_peer_connected called!\n"); abort(); } +/* Generated stub for fromwire_custommsg_in */ +bool fromwire_custommsg_in(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u8 **msg UNNEEDED) +{ fprintf(stderr, "fromwire_custommsg_in called!\n"); abort(); } /* Generated stub for fromwire_gossip_get_channel_peer_reply */ bool fromwire_gossip_get_channel_peer_reply(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id **peer_id UNNEEDED) { fprintf(stderr, "fromwire_gossip_get_channel_peer_reply called!\n"); abort(); }