From ec57de02c7ef512438f5a01ce7805a21388aa244 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 30 Apr 2025 11:33:38 +0200 Subject: [PATCH] socket: open IPv6 socket by default if IPv4 is disabled When no remote and local address is specified, and IPv4 is disabled by the -6 option, open an IPv6 socket. This is used by the Linux-specific timestamping configuration and socket option checking. It enables operation on a system that has no support for IPv4 sockets. --- socket.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/socket.c b/socket.c index 78e54fa..d08b865 100644 --- a/socket.c +++ b/socket.c @@ -212,13 +212,25 @@ get_reusable_socket(int type, IPSockAddr *spec) /* ================================================== */ +static int +get_default_inet_domain(void) +{ +#ifdef FEAT_IPV6 + if (!ip4_enabled && ip6_enabled) + return AF_INET6; +#endif + return AF_INET; +} + +/* ================================================== */ + #if defined(SOCK_CLOEXEC) || defined(SOCK_NONBLOCK) static int check_socket_flag(int sock_flag, int fd_flag, int fs_flag) { int sock_fd, fd_flags, fs_flags; - sock_fd = socket(AF_INET, SOCK_DGRAM | sock_flag, 0); + sock_fd = socket(get_default_inet_domain(), SOCK_DGRAM | sock_flag, 0); if (sock_fd < 0) return 0; @@ -526,7 +538,7 @@ open_ip_socket(IPSockAddr *remote_addr, IPSockAddr *local_addr, const char *ifac else if (remote_addr) family = remote_addr->ip_addr.family; else - family = IPADDR_INET4; + family = !ip4_enabled && ip6_enabled ? IPADDR_INET6 : IPADDR_INET4; switch (family) { case IPADDR_INET4: