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. */
| 2087 | * a new channel, or referring to a channel we no longer want to talk to |
| 2088 | * it about. */ |
| 2089 | void handle_peer_spoke(struct lightningd *ld, const u8 *msg) |
| 2090 | { |
| 2091 | struct node_id id; |
| 2092 | u16 msgtype; |
| 2093 | u64 connectd_counter; |
| 2094 | struct channel *channel; |
| 2095 | struct closed_channel *closed_channel; |
| 2096 | struct closed_channel_map_iter cc_it; |
| 2097 | struct channel_id channel_id; |
| 2098 | struct peer *peer; |
| 2099 | bool dual_fund; |
| 2100 | const u8 *error = NULL; |
| 2101 | int other_fd; |
| 2102 | struct peer_fd *pfd; |
| 2103 | char *errmsg; |
| 2104 | |
| 2105 | if (!fromwire_connectd_peer_spoke(msg, msg, &id, &connectd_counter, &msgtype, &channel_id, &errmsg)) |
| 2106 | fatal("Connectd gave bad CONNECTD_PEER_SPOKE message %s", |
| 2107 | tal_hex(msg, msg)); |
| 2108 | |
| 2109 | /* We must know it, and it must be the right connectd_id */ |
| 2110 | peer = peer_by_id(ld, &id); |
| 2111 | assert(peer->connectd_counter == connectd_counter); |
| 2112 | |
| 2113 | /* Do we know what channel they're talking about? */ |
| 2114 | channel = find_channel_by_id(peer, &channel_id); |
| 2115 | if (channel) { |
| 2116 | /* In this case, we'll send an error below, but send reestablish reply first |
| 2117 | * in case they lost their state and need it */ |
| 2118 | if (msgtype == WIRE_CHANNEL_REESTABLISH && channel_state_closed(channel->state)) { |
| 2119 | /* Maybe we know it's closed, but they don't? Happy to negotiate again. */ |
| 2120 | if (channel->state == CLOSINGD_COMPLETE) { |
| 2121 | pfd = sockpair(tmpctx, channel, &other_fd, &error); |
| 2122 | if (!pfd) |
| 2123 | goto send_error; |
| 2124 | |
| 2125 | /* Tell channeld to handle reestablish, then it will call closingd */ |
| 2126 | if (peer_start_channeld(channel, |
| 2127 | pfd, |
| 2128 | NULL, true)) { |
| 2129 | goto tell_connectd; |
| 2130 | } |
| 2131 | error = towire_warningfmt(tmpctx, &channel_id, |
| 2132 | "Trouble in paradise?"); |
| 2133 | goto send_error; |
| 2134 | } |
| 2135 | send_reestablish(peer, &channel->cid, |
| 2136 | &channel->their_shachain.chain, |
| 2137 | channel->next_index[LOCAL]); |
| 2138 | } |
| 2139 | |
| 2140 | /* If we have a canned error for this channel, send it now */ |
| 2141 | if (channel->error) { |
| 2142 | error = channel->error; |
| 2143 | goto send_error; |
| 2144 | } |
| 2145 | |
| 2146 | if (channel_state_wants_peercomms(channel->state)) { |
no test coverage detected