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

Function try_connect_peer

connectd/connectd.c:1846–1915  ·  view source on GitHub ↗

~ Try to connect to a single peer, given some addresses (in order) */

Source from the content-addressed store, hash-verified

1844
1845/*~ Try to connect to a single peer, given some addresses (in order) */
1846static void try_connect_peer(struct daemon *daemon,
1847 const struct node_id *id,
1848 struct wireaddr_internal *addrs TAKES,
1849 const char *reason TAKES)
1850{
1851 struct connecting *connect;
1852 struct peer *peer;
1853
1854 /* In case we return without using it, make sure it's freed. */
1855 if (taken(addrs))
1856 tal_steal(tmpctx, addrs);
1857 if (taken(reason))
1858 tal_steal(tmpctx, reason);
1859
1860 /* Already existing? Must have crossed over, it'll know soon. */
1861 peer = peer_htable_get(daemon->peers, id);
1862 if (peer)
1863 return;
1864
1865 /* If we're trying to connect it right now, that's OK. */
1866 if ((connect = find_connecting(daemon, id))) {
1867 /* If we've been passed in new connection details
1868 * for this connection, update our addrhint + add
1869 * to addresses to check */
1870 for (size_t i = 0; i < tal_count(addrs); i++) {
1871 if (!addr_in(&addrs[i], connect->addrs))
1872 tal_arr_expand(&connect->addrs, addrs[i]);
1873 }
1874
1875 return;
1876 }
1877
1878 /* Still no address? Fail immediately. Important ones get
1879 * retried; an address may get gossiped. */
1880 if (tal_count(addrs) == 0) {
1881 connect_failed(daemon, id, false, reason, time_mono(),
1882 CONNECT_NO_KNOWN_ADDRESS,
1883 "Unable to connect, no address known for peer");
1884 return;
1885 }
1886
1887 /* Start connecting to it: since this is the only place we allocate
1888 * a 'struct connecting' we don't write a separate new_connecting(). */
1889 connect = tal(daemon, struct connecting);
1890 connect->reason = tal_strdup(connect, reason);
1891 connect->daemon = daemon;
1892 connect->id = *id;
1893 connect->addrs = tal_dup_talarr(connect, struct wireaddr_internal, addrs);
1894 connect->addrnum = 0;
1895 /* connstate is supposed to be updated as we go, to give context for
1896 * errors which occur. We miss it in a few places; would be nice to
1897 * fix! */
1898 connect->connstate = "Connection establishment";
1899 connect->errors = tal_strdup(connect, "");
1900 connect->conn = NULL;
1901 connect->connect_attempted = false;
1902 connecting_htable_add(daemon->connecting, connect);
1903 tal_add_destructor(connect, destroy_connecting);

Callers 2

reconnectFunction · 0.85
connect_to_peerFunction · 0.85

Calls 6

takenFunction · 0.85
find_connectingFunction · 0.85
addr_inFunction · 0.85
time_monoFunction · 0.85
try_connect_one_addrFunction · 0.85
connect_failedFunction · 0.70

Tested by

no test coverage detected