| 1753 | } |
| 1754 | |
| 1755 | void peer_connect_subd(struct daemon *daemon, const u8 *msg, int fd) |
| 1756 | { |
| 1757 | struct node_id id; |
| 1758 | u64 counter; |
| 1759 | struct peer *peer; |
| 1760 | struct channel_id channel_id; |
| 1761 | struct subd *subd; |
| 1762 | |
| 1763 | if (!fromwire_connectd_peer_connect_subd(msg, &id, &counter, &channel_id)) |
| 1764 | master_badmsg(WIRE_CONNECTD_PEER_CONNECT_SUBD, msg); |
| 1765 | |
| 1766 | /* If receiving fd failed, fd will be -1. Log and ignore |
| 1767 | * (subd will see immediate hangup). */ |
| 1768 | if (fd == -1) { |
| 1769 | static bool recvfd_logged = false; |
| 1770 | status_broken_once(&recvfd_logged, |
| 1771 | "receiving lightningd fd failed for %s: %s", |
| 1772 | fmt_node_id(tmpctx, &id), |
| 1773 | strerror(errno)); |
| 1774 | /* Maybe free up some fds by closing something. */ |
| 1775 | close_random_connection(daemon); |
| 1776 | return; |
| 1777 | } |
| 1778 | |
| 1779 | /* Races can happen: this might be gone by now (or reconnected!). */ |
| 1780 | peer = peer_htable_get(daemon->peers, &id); |
| 1781 | if (!peer || peer->counter != counter) { |
| 1782 | close(fd); |
| 1783 | return; |
| 1784 | } |
| 1785 | |
| 1786 | /* Could be disconnecting now */ |
| 1787 | if (!peer->to_peer || peer->draining_state != NOT_DRAINING) { |
| 1788 | close(fd); |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | /* If peer said something, we created this and queued msg. */ |
| 1793 | subd = find_subd(peer, &channel_id); |
| 1794 | if (!subd) { |
| 1795 | subd = new_subd(peer, &channel_id); |
| 1796 | /* Implies lightningd is ready for another peer. */ |
| 1797 | release_one_waiting_connection(peer->daemon, |
| 1798 | tal_fmt(tmpctx, "%s given a subd", |
| 1799 | fmt_node_id(tmpctx, &id))); |
| 1800 | } |
| 1801 | |
| 1802 | /* We only keep one connection per channel_id. If one is already |
| 1803 | * attached for this channel_id, drop this fd rather than replacing |
| 1804 | * it. */ |
| 1805 | if (subd->conn) { |
| 1806 | status_peer_debug(&id, |
| 1807 | "Already have a subd for channel_id %s: ignoring", |
| 1808 | fmt_channel_id(tmpctx, &channel_id)); |
| 1809 | close(fd); |
| 1810 | return; |
| 1811 | } |
| 1812 |
no test coverage detected