Connectd tells us a peer has connected: it never hands us duplicates, since * it holds them until we say peer_disconnected. */
| 1357 | /* Connectd tells us a peer has connected: it never hands us duplicates, since |
| 1358 | * it holds them until we say peer_disconnected. */ |
| 1359 | void peer_connected(struct lightningd *ld, const u8 *msg) |
| 1360 | { |
| 1361 | struct node_id id; |
| 1362 | u8 *their_features; |
| 1363 | struct peer *peer; |
| 1364 | struct peer_connected_hook_payload *hook_payload; |
| 1365 | u64 connectd_counter; |
| 1366 | const char *cmd_id; |
| 1367 | |
| 1368 | hook_payload = tal(NULL, struct peer_connected_hook_payload); |
| 1369 | hook_payload->ld = ld; |
| 1370 | hook_payload->error = NULL; |
| 1371 | if (!fromwire_connectd_peer_connected(hook_payload, msg, |
| 1372 | &id, &connectd_counter, |
| 1373 | &hook_payload->addr, |
| 1374 | &hook_payload->remote_addr, |
| 1375 | &hook_payload->incoming, |
| 1376 | &their_features)) |
| 1377 | fatal("Connectd gave bad CONNECT_PEER_CONNECTED message %s", |
| 1378 | tal_hex(msg, msg)); |
| 1379 | |
| 1380 | /* When a peer disconnects, we give subds time to clean themselves up |
| 1381 | * (this lets connectd ensure they've seen the final messages). But |
| 1382 | * now it's reconnected, we've gotta force them out. */ |
| 1383 | peer_channels_cleanup(ld, &id); |
| 1384 | |
| 1385 | /* If we're already dealing with this peer, hand off to correct |
| 1386 | * subdaemon. Otherwise, we'll hand to openingd to wait there. */ |
| 1387 | peer = peer_by_id(ld, &id); |
| 1388 | if (!peer) |
| 1389 | peer = new_peer(ld, 0, &id, &hook_payload->addr, |
| 1390 | hook_payload->incoming); |
| 1391 | |
| 1392 | /* We track this, because messages can race between connectd and us. |
| 1393 | * For example, we could tell it to attach a subd, but it's actually |
| 1394 | * already reconnected: we would tell it again when we read the |
| 1395 | * "peer_connected" message, and it would get upset (plus, our first |
| 1396 | * subd wouldn't die as expected. So we echo this back to connectd |
| 1397 | * on peer commands, and it knows to ignore if it's wrong. */ |
| 1398 | peer->connectd_counter = connectd_counter; |
| 1399 | |
| 1400 | /* We mark peer in "connecting" state until hooks have passed. */ |
| 1401 | assert(peer->connected == PEER_DISCONNECTED); |
| 1402 | peer->connected = PEER_CONNECTING; |
| 1403 | |
| 1404 | /* Update peer address and direction */ |
| 1405 | peer->addr = hook_payload->addr; |
| 1406 | peer->connected_incoming = hook_payload->incoming; |
| 1407 | if (peer->remote_addr) |
| 1408 | tal_free(peer->remote_addr); |
| 1409 | peer->remote_addr = NULL; |
| 1410 | peer_update_features(peer, their_features); |
| 1411 | |
| 1412 | tal_steal(peer, hook_payload); |
| 1413 | hook_payload->peer = peer; |
| 1414 | |
| 1415 | /* If there's a connect command, use its id as basis for hook id */ |
| 1416 | cmd_id = connect_any_cmd_id(tmpctx, ld, peer); |
no test coverage detected