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 <david(a)gibson.dropbear.id.au> --- 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