From 03ffd49045f82086d8247bec1ee3f7418f80a43d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 28 May 2026 14:20:18 +0300 Subject: [PATCH] net: fix cross C generation for socket helpers --- vlib/net/address.c.v | 69 +++++++++-------------- vlib/net/http/backend_vschannel_d_cross.v | 5 ++ vlib/net/net_d_cross.v | 30 ++++++++++ 3 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 vlib/net/http/backend_vschannel_d_cross.v create mode 100644 vlib/net/net_d_cross.v diff --git a/vlib/net/address.c.v b/vlib/net/address.c.v index 39999afbe..cf6865d50 100644 --- a/vlib/net/address.c.v +++ b/vlib/net/address.c.v @@ -13,62 +13,47 @@ union AddrData { const addr_ip6_any = [16]u8{init: u8(0)} const addr_ip_any = [4]u8{init: u8(0)} +fn set_addr_family(mut a Addr, family AddrFamily, sockaddr_size u32) { + unsafe { + // Addr starts with `len, f` on BSD/Darwin, but with `f` elsewhere. + $if macos || freebsd || openbsd || netbsd || dragonfly { + mut raw := &u8(&a) + raw[0] = u8(sockaddr_size) + raw[1] = u8(family) + } $else { + *(&u16(&a)) = u16(family) + } + } +} + // 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) - $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 - } + mut a := Addr{ + addr: AddrData{ + Ip6: Ip6{ + port: n_port } } - unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) } - return a } + set_addr_family(mut a, .ip6, sizeof(C.sockaddr_in6)) + 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) - $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 - } + mut a := Addr{ + addr: AddrData{ + Ip: Ip{ + port: n_port } } - unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) } - return a } + set_addr_family(mut a, .ip, sizeof(C.sockaddr_in)) + unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) } + return a } fn temp_unix() !Addr { diff --git a/vlib/net/http/backend_vschannel_d_cross.v b/vlib/net/http/backend_vschannel_d_cross.v new file mode 100644 index 000000000..b4045c715 --- /dev/null +++ b/vlib/net/http/backend_vschannel_d_cross.v @@ -0,0 +1,5 @@ +module http + +fn vschannel_ssl_do(_req &Request, _port int, _method Method, _host_name string, _path string, _data string, _header Header) !Response { + return error('vschannel is unavailable in cross C generation') +} diff --git a/vlib/net/net_d_cross.v b/vlib/net/net_d_cross.v new file mode 100644 index 000000000..f0562c1cc --- /dev/null +++ b/vlib/net/net_d_cross.v @@ -0,0 +1,30 @@ +module net + +pub const fionbio = u32(0x8004667e) + +// WsaError is the subset of Windows socket errors needed for cross C generation. +pub enum WsaError { + wsaeintr = 10004 + wsaewouldblock = 10035 + wsaeinprogress = 10036 + wsaeafnosupport = 10047 + wsaeaddrnotavail = 10049 + wsaenetdown = 10050 + wsaenetunreach = 10051 + wsaenotconn = 10057 + wsaeconnrefused = 10061 + wsaehostdown = 10064 + wsaehostunreach = 10065 + wsaeprotonosupport = 10043 + wsaetimedout = 10060 + wsahost_not_found = 11001 + wsatry_again = 11002 + wsano_recovery = 11003 + wsano_data = 11004 + wsa_ipsec_name_policy_error = 11033 +} + +// wsa_error casts an int to its WsaError value. +pub fn wsa_error(code int) WsaError { + return unsafe { WsaError(code) } +} -- 2.39.5