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 207C65A0277 for ; Mon, 29 Jan 2024 05:36:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1706502959; bh=V1jicipkcAtMTko9UTOlS/a5UuVyFH4HdVPg/yF+MY4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BIa+OpuK5YWSRNtIpexlb50/x2QoIWnl5P+DF+rcJ8StWh2cN0cwJyPeAUnLk8OWr C7JCmBc/viU3HEHrTXqL3pPqXeuSL2LdHkje2vEqMu6hYfCUJrGGiNDS4AsnwqxixW dxmemAykWTgfjkUTpZ7l+AvLX4DLpu+rhubSLFIku6NDUI8MR+SBV7LvDsLlgH/H/Q cir6mhx7vs3BmP3XFV3yBoejq9NfK84GEoyD1lap1bmRagBmjtPWgJdVvZbrxzfvQy 1+s3Sfn7vxGm3EvxI66EZjG7YWS7NpLxbmg+CEoPO6ZVwcrJl74N5Ahqy2rx8VbMd6 mbkKQdBPxVwJA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TNb8b6g2cz4x7q; Mon, 29 Jan 2024 15:35:59 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 13/16] util: Provide global constants for IPv4 loopback and unspecified address Date: Mon, 29 Jan 2024 15:35:54 +1100 Message-ID: <20240129043557.823451-14-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240129043557.823451-1-david@gibson.dropbear.id.au> References: <20240129043557.823451-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: OYOXHF6X5BUG7KRN4OGHW2JNO7RIN7QA X-Message-ID-Hash: OYOXHF6X5BUG7KRN4OGHW2JNO7RIN7QA 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: For various reasons, the standard library doesn't provide IPv4 loopback and unspecified addresses as struct in_addr constants, the way it does in6addr_loopback and in6addr_any for IPv6. This lack means that in several places we initialise a local with a macro just so that we can take the address of an IPv4 loopback or unspecified address. Provide our own in4addr_any and in4addr_loopback global constants to make this a bit simpler. Signed-off-by: David Gibson --- tcp.c | 4 ++-- udp.c | 5 ++--- util.c | 3 +++ util.h | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tcp.c b/tcp.c index 052bf7cb..bb914bff 100644 --- a/tcp.c +++ b/tcp.c @@ -2944,12 +2944,12 @@ static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port) .port = port, .pif = PIF_SPLICE, }; - struct in_addr loopback = IN4ADDR_LOOPBACK_INIT; int s; ASSERT(c->mode == MODE_PASTA); - s = sock_l4(c, AF_INET, IPPROTO_TCP, &loopback, NULL, port, tref.u32); + s = sock_l4(c, AF_INET, IPPROTO_TCP, &in4addr_loopback, NULL, port, + tref.u32); if (s >= 0) tcp_sock_set_bufsize(c, s); else diff --git a/udp.c b/udp.c index 02cb7889..1104f374 100644 --- a/udp.c +++ b/udp.c @@ -1012,9 +1012,8 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af, udp_tap_map[V4][uref.port].sock = s < 0 ? -1 : s; udp_splice_init[V4][port].sock = s < 0 ? -1 : s; } else { - struct in_addr loopback = IN4ADDR_LOOPBACK_INIT; - - r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, &loopback, + r4 = s = sock_l4(c, AF_INET, IPPROTO_UDP, + &in4addr_loopback, ifname, port, uref.u32); udp_splice_ns[V4][port].sock = s < 0 ? -1 : s; } diff --git a/util.c b/util.c index 8acce233..f1f68a03 100644 --- a/util.c +++ b/util.c @@ -30,6 +30,9 @@ #include "packet.h" #include "log.h" +const struct in_addr in4addr_loopback = IN4ADDR_LOOPBACK_INIT; +const struct in_addr in4addr_any = IN4ADDR_ANY_INIT; + #define IPV6_NH_OPT(nh) \ ((nh) == 0 || (nh) == 43 || (nh) == 44 || (nh) == 50 || \ (nh) == 51 || (nh) == 60 || (nh) == 135 || (nh) == 139 || \ diff --git a/util.h b/util.h index e0df26c6..4a71904b 100644 --- a/util.h +++ b/util.h @@ -125,6 +125,8 @@ #define IN4ADDR_ANY_INIT \ { .s_addr = htonl_constant(INADDR_ANY) } +extern const struct in_addr in4addr_loopback; +extern const struct in_addr in4addr_any; #define NS_FN_STACK_SIZE (RLIMIT_STACK_VAL * 1024 / 8) int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags, -- 2.43.0