From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id F02875A026D for ; Thu, 7 Dec 2023 15:31:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1701959502; bh=Jx8iY5XCk1sQ+VGMSgmnkD3eJQDOrspH5u+N4n6Rr0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PkwKrQC/Jo8ODhknqnV/4Sds+jDQPk1sHe4x+PaVXJgi90RRqIQBh6zIw7IKxtkEh pmsS+4VC7B+ZsFojdALsHSPo9tcyc1kKPDO+X2BWo3XmICnvf6/d9kujNC4aexMARD WLj7bXA8a1BEE8OoXZ/1031/W5vKRJn2+IawX+TCj+6TVu8iDzHpDDhoujS1xobxCM qSVpzh/NsoxfsCFLwU0BEPELLBNNnqct5LATeFu3huGZ2nqrKydI0YiPyTf/x7kCW7 ruse05Z1Xc6Qc42eTPdRHWV5mZMe2Hdy6/8zYhZeUHH2VF42vFsJQHUxb7ON8xq3TG rIXLgpYs48xdQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4SmGtQ1r8Xz4xPL; Fri, 8 Dec 2023 01:31:42 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 5/8] util: Improve sockaddr initialisation in sock_l4() Date: Fri, 8 Dec 2023 01:31:37 +1100 Message-ID: <20231207143140.1851378-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231207143140.1851378-1-david@gibson.dropbear.id.au> References: <20231207143140.1851378-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: VRKYWU4QSEJHARGBHDD7UG7V6526SMZS X-Message-ID-Hash: VRKYWU4QSEJHARGBHDD7UG7V6526SMZS 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: Currently we initialise the address field of the sockaddrs we construct to the any/unspecified address, but not in a very clear way: we use explicit 0 values, which is only interpretable if you know the order of fields in the sockaddr structures. Use explicit field names, and explicit initialiser macros for the address. Because we initialise to this default value, we don't need to explicitly set the any/unspecified address later on if the caller didn't pass an overriding bind address. Signed-off-by: David Gibson --- util.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/util.c b/util.c index d465e48..0152ae6 100644 --- a/util.c +++ b/util.c @@ -105,12 +105,12 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto, struct sockaddr_in addr4 = { .sin_family = AF_INET, .sin_port = htons(port), - { 0 }, { 0 }, + .sin_addr = IN4ADDR_ANY_INIT, }; struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6, .sin6_port = htons(port), - 0, IN6ADDR_ANY_INIT, 0, + .sin6_addr = IN6ADDR_ANY_INIT, }; const struct sockaddr *sa; bool dual_stack = false; @@ -162,8 +162,6 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto, if (af == AF_INET) { if (bind_addr) addr4.sin_addr.s_addr = *(in_addr_t *)bind_addr; - else - addr4.sin_addr.s_addr = htonl(INADDR_ANY); sa = (const struct sockaddr *)&addr4; sl = sizeof(addr4); @@ -174,8 +172,6 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto, if (!memcmp(bind_addr, &c->ip6.addr_ll, sizeof(c->ip6.addr_ll))) addr6.sin6_scope_id = c->ifi6; - } else { - addr6.sin6_addr = in6addr_any; } sa = (const struct sockaddr *)&addr6; -- 2.43.0