~ Try to connect to a single peer, given some addresses (in order) */
| 1899 | |
| 1900 | /*~ Try to connect to a single peer, given some addresses (in order) */ |
| 1901 | static void try_connect_peer(struct daemon *daemon, |
| 1902 | const struct node_id *id, |
| 1903 | struct wireaddr_internal *addrs TAKES, |
| 1904 | const char *reason TAKES) |
| 1905 | { |
| 1906 | struct connecting *connect; |
| 1907 | struct peer *peer; |
| 1908 | |
| 1909 | /* In case we return without using it, make sure it's freed. */ |
| 1910 | if (taken(addrs)) |
| 1911 | tal_steal(tmpctx, addrs); |
| 1912 | if (taken(reason)) |
| 1913 | tal_steal(tmpctx, reason); |
| 1914 | |
| 1915 | /* Already existing? Must have crossed over, it'll know soon. */ |
| 1916 | peer = peer_htable_get(daemon->peers, id); |
| 1917 | if (peer) |
| 1918 | return; |
| 1919 | |
| 1920 | /* If we're trying to connect it right now, that's OK. */ |
| 1921 | if ((connect = find_connecting(daemon, id))) { |
| 1922 | /* If we've been passed in new connection details |
| 1923 | * for this connection, update our addrhint + add |
| 1924 | * to addresses to check */ |
| 1925 | for (size_t i = 0; i < tal_count(addrs); i++) { |
| 1926 | if (!addr_in(&addrs[i], connect->addrs)) |
| 1927 | tal_arr_expand(&connect->addrs, addrs[i]); |
| 1928 | } |
| 1929 | |
| 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | /* Still no address? Fail immediately. Important ones get |
| 1934 | * retried; an address may get gossiped. */ |
| 1935 | if (tal_count(addrs) == 0) { |
| 1936 | connect_failed(daemon, id, false, reason, time_mono(), |
| 1937 | CONNECT_NO_KNOWN_ADDRESS, |
| 1938 | "Unable to connect, no address known for peer"); |
| 1939 | return; |
| 1940 | } |
| 1941 | |
| 1942 | /* Start connecting to it: since this is the only place we allocate |
| 1943 | * a 'struct connecting' we don't write a separate new_connecting(). */ |
| 1944 | connect = tal(daemon, struct connecting); |
| 1945 | connect->reason = tal_strdup(connect, reason); |
| 1946 | connect->daemon = daemon; |
| 1947 | connect->id = *id; |
| 1948 | connect->addrs = tal_dup_talarr(connect, struct wireaddr_internal, addrs); |
| 1949 | connect->addrnum = 0; |
| 1950 | /* connstate is supposed to be updated as we go, to give context for |
| 1951 | * errors which occur. We miss it in a few places; would be nice to |
| 1952 | * fix! */ |
| 1953 | connect->connstate = "Connection establishment"; |
| 1954 | connect->errors = tal_strdup(connect, ""); |
| 1955 | connect->conn = NULL; |
| 1956 | connect->connect_attempted = false; |
| 1957 | connecting_htable_add(daemon->connecting, connect); |
| 1958 | tal_add_destructor(connect, destroy_connecting); |
no test coverage detected