From f42b0541138761c2a3570ce5ebe4b46d63324c4d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 23:17:47 +0300 Subject: [PATCH] net: fix veb test for listening on both IPv4 and IPv6 by default (fixes #26016) --- vlib/net/address.c.v | 56 +++++++++++++++++++++++++++++---------- vlib/net/address_test.c.v | 7 +++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/vlib/net/address.c.v b/vlib/net/address.c.v index d49a08127..85261b0c8 100644 --- a/vlib/net/address.c.v +++ b/vlib/net/address.c.v @@ -16,31 +16,59 @@ const addr_ip_any = [4]u8{init: u8(0)} // new_ip6 creates a new Addr from the IP6 address family, based on the given port and addr pub fn new_ip6(port u16, addr [16]u8) Addr { n_port := conv.hton16(port) - a := Addr{ - f: u8(AddrFamily.ip6) - addr: AddrData{ - Ip6: Ip6{ - port: n_port + $if macos || freebsd || openbsd || netbsd || dragonfly { + a := Addr{ + len: u8(sizeof(C.sockaddr_in6)) + f: u8(AddrFamily.ip6) + addr: AddrData{ + Ip6: Ip6{ + port: n_port + } + } + } + unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) } + return a + } $else { + a := Addr{ + f: u16(AddrFamily.ip6) + addr: AddrData{ + Ip6: Ip6{ + port: n_port + } } } + unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) } + return a } - unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) } - return a } // new_ip creates a new Addr from the IPv4 address family, based on the given port and addr pub fn new_ip(port u16, addr [4]u8) Addr { n_port := conv.hton16(port) - a := Addr{ - f: u8(AddrFamily.ip) - addr: AddrData{ - Ip: Ip{ - port: n_port + $if macos || freebsd || openbsd || netbsd || dragonfly { + a := Addr{ + len: u8(sizeof(C.sockaddr_in)) + f: u8(AddrFamily.ip) + addr: AddrData{ + Ip: Ip{ + port: n_port + } + } + } + unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) } + return a + } $else { + a := Addr{ + f: u16(AddrFamily.ip) + addr: AddrData{ + Ip: Ip{ + port: n_port + } } } + unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) } + return a } - unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) } - return a } fn temp_unix() !Addr { diff --git a/vlib/net/address_test.c.v b/vlib/net/address_test.c.v index e9140d1fe..cc7bf045c 100644 --- a/vlib/net/address_test.c.v +++ b/vlib/net/address_test.c.v @@ -5,6 +5,13 @@ fn test_ip_port() { assert new_ip6(1234, addr_ip6_any).port()! == 1234 } +fn test_wildcard_constructors_set_sockaddr_len_on_bsd() { + $if macos || freebsd || openbsd || netbsd || dragonfly { + assert new_ip(1234, addr_ip_any).len == u8(sizeof(C.sockaddr_in)) + assert new_ip6(1234, addr_ip6_any).len == u8(sizeof(C.sockaddr_in6)) + } +} + fn test_diagnostics() { dump(aoffset) eprintln('--------') -- 2.39.5