From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202512 header.b=VhYz8Bcz; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id A029D5A0774 for ; Fri, 19 Dec 2025 15:19:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1766153948; bh=0z+qB/mHptCYJh1gEm9voVDiQYZOWVAXTJEjvB1BNec=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VhYz8Bcz2cwnWMGMSFSQhHfvuuJ5Ukrfy/supXJWTR5iTBDiWfYU4QmFBW6pdifgn iH3KGB0BTooEqP7BlnlUTZtFEapngAW1JkQJ1ta6uXw6zMFe2reOnAnsewTcIDy1pO 8sTCcNwlbS0E+1AWnKN/MvLPGdWY9vGmcULZI1iWP+l7caMpHe/YXthGJGxaCMImu9 KZyYZ04PqMnZTvdVAIMJZ3huDll3Mj1B22glhotpn3pdHwqAlbAXLY+kfdAskVFjrT mYoMP083LnnWgBIrq9w83CZPJfXdukCNkrqvDCY930qaSmrMSyb4yOOgPS9+WSkfQT rN8kBvjijthQA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dXqR02vXNz4wMG; Sat, 20 Dec 2025 01:19:08 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 06/12] tcp, udp: Make {tcp,udp}_listen() return socket fds Date: Sat, 20 Dec 2025 01:18:58 +1100 Message-ID: <20251219141904.1758072-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251219141904.1758072-1-david@gibson.dropbear.id.au> References: <20251219141904.1758072-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 5ZM4U22CI2ZI73TLDQF7VLDUEFS5PSB7 X-Message-ID-Hash: 5ZM4U22CI2ZI73TLDQF7VLDUEFS5PSB7 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: {tcp,udp}_listen() currently return 0 on success, rather than the socket fd they created. We had that historically because these functions could sometimes create multiple sockets. We've now refactored things to avoid that, so it makes more sense for them to return the socket on success. Signed-off-by: David Gibson --- conf.c | 14 +++++++------- tcp.c | 7 ++----- udp.c | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/conf.c b/conf.c index 9d94e449..3d41a0fb 100644 --- a/conf.c +++ b/conf.c @@ -150,7 +150,7 @@ static void conf_ports_range_except(const struct ctx *c, char optname, unsigned delta = to - first; bool bound_one = false; unsigned base, i; - int ret; + int fd; if (first == 0) { die("Can't forward port 0 for option '-%c %s'", @@ -175,25 +175,25 @@ static void conf_ports_range_except(const struct ctx *c, char optname, fwd->delta[i] = delta; if (!(flags & FWD_SCAN) && optname == 't') - ret = tcp_listen(c, PIF_HOST, addr, ifname, i); + fd = tcp_listen(c, PIF_HOST, addr, ifname, i); else if (!(flags & FWD_SCAN) && optname == 'u') - ret = udp_listen(c, PIF_HOST, addr, ifname, i); + fd = udp_listen(c, PIF_HOST, addr, ifname, i); else /* No way to check in advance for -T and -U */ - ret = 0; + fd = 0; /* dummy */ - if (ret == -ENFILE || ret == -EMFILE) { + if (fd == -ENFILE || fd == -EMFILE) { die( "Can't open enough sockets for port specifier: %s", optarg); } - if (!ret) { + if (fd >= 0) { bound_one = true; } else if (!(flags & FWD_WEAK)) { die( "Failed to bind port %u (%s) for option '-%c %s'", - i, strerror_(-ret), optname, optarg); + i, strerror_(-fd), optname, optarg); } } diff --git a/tcp.c b/tcp.c index 71c6d632..e52f5420 100644 --- a/tcp.c +++ b/tcp.c @@ -2680,7 +2680,7 @@ void tcp_sock_handler(const struct ctx *c, union epoll_ref ref, * @ifname: Name of interface to bind to, NULL for any * @port: Port, host order * - * Return: 0 on success, negative error code on failure + * Return: Socket fd on success, negative error code on failure */ int tcp_listen(const struct ctx *c, uint8_t pif, const union inany_addr *addr, const char *ifname, in_port_t port) @@ -2731,10 +2731,7 @@ int tcp_listen(const struct ctx *c, uint8_t pif, socks[port][V6] = s < 0 ? -1 : s; } - if (s < 0) - return s; - - return 0; + return s; } /** diff --git a/udp.c b/udp.c index eda55c39..6168c36c 100644 --- a/udp.c +++ b/udp.c @@ -1136,7 +1136,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order * - * Return: 0 on success, negative error code on failure + * Return: Socket fd on success, negative error code on failure */ int udp_listen(const struct ctx *c, uint8_t pif, const union inany_addr *addr, const char *ifname, in_port_t port) @@ -1182,7 +1182,7 @@ int udp_listen(const struct ctx *c, uint8_t pif, if (!addr || !inany_v4(addr)) socks[V6][port] = s < 0 ? -1 : s; - return s < 0 ? s : 0; + return s; } /** -- 2.52.0