connectd tells us a peer has a message and we've not already attached * a subd. Normally this is a race, but it happens for real when opening * a new channel, or referring to a channel we no longer want to talk to * it about. */
| 2003 | * a new channel, or referring to a channel we no longer want to talk to |
| 2004 | * it about. */ |
| 2005 | void handle_peer_spoke(struct lightningd *ld, const u8 *msg) |
| 2006 | { |
| 2007 | struct node_id id; |
| 2008 | u16 msgtype; |
| 2009 | u64 connectd_counter; |
| 2010 | struct channel *channel; |
| 2011 | struct closed_channel *closed_channel; |
| 2012 | struct channel_id channel_id; |
| 2013 | struct peer *peer; |
| 2014 | bool dual_fund; |
| 2015 | const u8 *error; |
| 2016 | int other_fd; |
| 2017 | struct peer_fd *pfd; |
| 2018 | char *errmsg; |
| 2019 | |
| 2020 | if (!fromwire_connectd_peer_spoke(msg, msg, &id, &connectd_counter, &msgtype, &channel_id, &errmsg)) |
| 2021 | fatal("Connectd gave bad CONNECTD_PEER_SPOKE message %s", |
| 2022 | tal_hex(msg, msg)); |
| 2023 | |
| 2024 | /* We must know it, and it must be the right connectd_id */ |
| 2025 | peer = peer_by_id(ld, &id); |
| 2026 | assert(peer->connectd_counter == connectd_counter); |
| 2027 | |
| 2028 | /* Do we know what channel they're talking about? */ |
| 2029 | channel = find_channel_by_id(peer, &channel_id); |
| 2030 | if (channel) { |
| 2031 | /* In this case, we'll send an error below, but send reestablish reply first |
| 2032 | * in case they lost their state and need it */ |
| 2033 | if (msgtype == WIRE_CHANNEL_REESTABLISH && channel_state_closed(channel->state)) { |
| 2034 | /* Maybe we know it's closed, but they don't? Happy to negotiate again. */ |
| 2035 | if (channel->state == CLOSINGD_COMPLETE) { |
| 2036 | pfd = sockpair(tmpctx, channel, &other_fd, &error); |
| 2037 | if (!pfd) |
| 2038 | goto send_error; |
| 2039 | |
| 2040 | /* Tell channeld to handle reestablish, then it will call closingd */ |
| 2041 | if (peer_start_channeld(channel, |
| 2042 | pfd, |
| 2043 | NULL, true)) { |
| 2044 | goto tell_connectd; |
| 2045 | } |
| 2046 | error = towire_warningfmt(tmpctx, &channel_id, |
| 2047 | "Trouble in paradise?"); |
| 2048 | goto send_error; |
| 2049 | } |
| 2050 | send_reestablish(peer, &channel->cid, |
| 2051 | &channel->their_shachain.chain, |
| 2052 | channel->next_index[LOCAL]); |
| 2053 | } |
| 2054 | |
| 2055 | /* If we have a canned error for this channel, send it now */ |
| 2056 | if (channel->error) { |
| 2057 | error = channel->error; |
| 2058 | goto send_error; |
| 2059 | } |
| 2060 | |
| 2061 | if (channel_state_wants_peercomms(channel->state)) { |
| 2062 | /* If they send an error, handle it immediately. */ |
no test coverage detected