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

Function connect_to_peer

connectd/connectd.c:1923–1973  ·  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

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