| 1796 | } |
| 1797 | |
| 1798 | void peer_connect_subd(struct daemon *daemon, const u8 *msg, int fd) |
| 1799 | { |
| 1800 | struct node_id id; |
| 1801 | u64 counter; |
| 1802 | struct peer *peer; |
| 1803 | struct channel_id channel_id; |
| 1804 | struct subd *subd; |
| 1805 | |
| 1806 | if (!fromwire_connectd_peer_connect_subd(msg, &id, &counter, &channel_id)) |
| 1807 | master_badmsg(WIRE_CONNECTD_PEER_CONNECT_SUBD, msg); |
| 1808 | |
| 1809 | /* If receiving fd failed, fd will be -1. Log and ignore |
| 1810 | * (subd will see immediate hangup). */ |
| 1811 | if (fd == -1) { |
| 1812 | static bool recvfd_logged = false; |
| 1813 | status_broken_once(&recvfd_logged, |
| 1814 | "receiving lightningd fd failed for %s: %s", |
| 1815 | fmt_node_id(tmpctx, &id), |
| 1816 | strerror(errno)); |
| 1817 | /* Maybe free up some fds by closing something. */ |
| 1818 | close_random_connection(daemon); |
| 1819 | return; |
| 1820 | } |
| 1821 | |
| 1822 | /* Races can happen: this might be gone by now (or reconnected!). */ |
| 1823 | peer = peer_htable_get(daemon->peers, &id); |
| 1824 | if (!peer || peer->counter != counter) { |
| 1825 | close(fd); |
| 1826 | return; |
| 1827 | } |
| 1828 | |
| 1829 | /* Could be disconnecting now */ |
| 1830 | if (!peer->to_peer || peer->draining_state != NOT_DRAINING) { |
| 1831 | close(fd); |
| 1832 | return; |
| 1833 | } |
| 1834 | |
| 1835 | /* If peer said something, we created this and queued msg. */ |
| 1836 | subd = find_subd(peer, &channel_id); |
| 1837 | if (!subd) { |
| 1838 | subd = new_subd(peer, &channel_id); |
| 1839 | /* Implies lightningd is ready for another peer. */ |
| 1840 | release_one_waiting_connection(peer->daemon, |
| 1841 | tal_fmt(tmpctx, "%s given a subd", |
| 1842 | fmt_node_id(tmpctx, &id))); |
| 1843 | } |
| 1844 | |
| 1845 | /* We only keep one connection per channel_id. If one is already |
| 1846 | * attached for this channel_id, drop this fd rather than replacing |
| 1847 | * it. */ |
| 1848 | if (subd->conn) { |
| 1849 | status_peer_debug(&id, |
| 1850 | "Already have a subd for channel_id %s: ignoring", |
| 1851 | fmt_channel_id(tmpctx, &channel_id)); |
| 1852 | close(fd); |
| 1853 | return; |
| 1854 | } |
| 1855 |
no test coverage detected