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. */
| 2123 | * a new channel, or referring to a channel we no longer want to talk to |
| 2124 | * it about. */ |
| 2125 | void handle_peer_spoke(struct lightningd *ld, const u8 *msg) |
| 2126 | { |
| 2127 | struct node_id id; |
| 2128 | u16 msgtype; |
| 2129 | u64 connectd_counter; |
| 2130 | struct channel *channel; |
| 2131 | struct closed_channel *closed_channel; |
| 2132 | struct closed_channel_map_iter cc_it; |
| 2133 | struct channel_id channel_id; |
| 2134 | struct peer *peer; |
| 2135 | bool dual_fund; |
| 2136 | const u8 *error = NULL; |
| 2137 | int other_fd; |
| 2138 | struct peer_fd *pfd; |
| 2139 | char *errmsg; |
| 2140 | |
| 2141 | if (!fromwire_connectd_peer_spoke(msg, msg, &id, &connectd_counter, &msgtype, &channel_id, &errmsg)) |
| 2142 | fatal("Connectd gave bad CONNECTD_PEER_SPOKE message %s", |
| 2143 | tal_hex(msg, msg)); |
| 2144 | |
| 2145 | /* We must know it, and it must be the right connectd_id */ |
| 2146 | peer = peer_by_id(ld, &id); |
| 2147 | assert(peer->connectd_counter == connectd_counter); |
| 2148 | |
| 2149 | /* Do we know what channel they're talking about? */ |
| 2150 | channel = find_channel_by_id(peer, &channel_id); |
| 2151 | if (channel) { |
| 2152 | /* In this case, we'll send an error below, but send reestablish reply first |
| 2153 | * in case they lost their state and need it */ |
| 2154 | if (msgtype == WIRE_CHANNEL_REESTABLISH && channel_state_closed(channel->state)) { |
| 2155 | /* Maybe we know it's closed, but they don't? Happy to negotiate again. */ |
| 2156 | if (channel->state == CLOSINGD_COMPLETE) { |
| 2157 | pfd = sockpair(tmpctx, channel, &other_fd, &error); |
| 2158 | if (!pfd) |
| 2159 | goto send_error; |
| 2160 | |
| 2161 | /* Tell channeld to handle reestablish, then it will call closingd */ |
| 2162 | if (peer_start_channeld(channel, |
| 2163 | pfd, |
| 2164 | NULL, true)) { |
| 2165 | goto tell_connectd; |
| 2166 | } |
| 2167 | error = towire_warningfmt(tmpctx, &channel_id, |
| 2168 | "Trouble in paradise?"); |
| 2169 | goto send_error; |
| 2170 | } |
| 2171 | send_reestablish(peer, &channel->cid, |
| 2172 | &channel->their_shachain.chain, |
| 2173 | channel->next_index[LOCAL]); |
| 2174 | } |
| 2175 | |
| 2176 | /* If we have a canned error for this channel, send it now */ |
| 2177 | if (channel->error) { |
| 2178 | error = channel->error; |
| 2179 | goto send_error; |
| 2180 | } |
| 2181 | |
| 2182 | if (channel_state_wants_peercomms(channel->state)) { |
no test coverage detected