MCPcopy Create free account
hub / github.com/ElementsProject/lightning / connect_to_peer

Function connect_to_peer

connectd/connectd.c:1930–1980  ·  view source on GitHub ↗

lightningd tells us to connect to a peer by id, with optional addresses to try. */

Source from the content-addressed store, hash-verified

1928
1929/* lightningd tells us to connect to a peer by id, with optional addresses to try. */
1930static void connect_to_peer(struct daemon *daemon, const u8 *msg)
1931{
1932 struct node_id id;
1933 struct wireaddr_internal *addrs;
1934 bool transient;
1935 struct important_id *imp;
1936 char *reason;
1937
1938 if (!fromwire_connectd_connect_to_peer(tmpctx, msg,
1939 &id, &addrs,
1940 &transient, &reason))
1941 master_badmsg(WIRE_CONNECTD_CONNECT_TO_PEER, msg);
1942
1943 /* fromwire doesn't allocate empty arrays! */
1944 if (!addrs)
1945 addrs = tal_arr(tmpctx, struct wireaddr_internal, 0);
1946
1947
1948 /* If it's important, it stays important, but harvest any new addresses */
1949 imp = important_id_htable_get(daemon->important_ids, &id);
1950 if (imp) {
1951 status_peer_debug(&id, "Adding %zu addresses to important peer", tal_count(addrs));
1952 /* Append any new addresses, and we're done */
1953 for (size_t i = 0; i < tal_count(addrs); i++) {
1954 if (!addr_in(&addrs[i], imp->addrs))
1955 tal_arr_expand(&imp->addrs, addrs[i]);
1956 }
1957 transient = false;
1958 } else {
1959 /* If it's not transient, note it for reconnection (with addresses!) */
1960 if (!transient) {
1961 imp = tal(daemon, struct important_id);
1962 imp->daemon = daemon;
1963 imp->id = id;
1964 status_peer_debug(&id, "Initializing important peer with %zu addresses", tal_count(addrs));
1965 imp->addrs = tal_dup_talarr(imp,
1966 struct wireaddr_internal,
1967 addrs);
1968 imp->reconnect_secs = INITIAL_WAIT_SECONDS;
1969 imp->reconnect_timer = NULL;
1970 tal_add_destructor(imp, destroy_important_id);
1971 important_id_htable_add(daemon->important_ids, imp);
1972 } else
1973 status_peer_debug(&id, "Transient connection to peer (not important)");
1974 }
1975
1976 /* Do gossmap lookup to find any addresses from there, and append. */
1977 append_gossmap_addresses(&addrs, daemon, &id);
1978
1979 try_connect_peer(daemon, &id, addrs, take(reason));
1980}
1981
1982/* lightningd tells us a peer should be disconnected. */
1983static void peer_disconnect(struct daemon *daemon, const u8 *msg)

Callers 1

recv_reqFunction · 0.85

Calls 4

addr_inFunction · 0.85
append_gossmap_addressesFunction · 0.85
try_connect_peerFunction · 0.85
master_badmsgFunction · 0.50

Tested by

no test coverage detected