From c9867a9ae4a07162f11b4d1c77a9f3057cf58b08 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 18 Feb 2022 11:23:45 +0200 Subject: [PATCH] net: ensure that `net` and `net.unix` can be imported together in the same program --- vlib/net/aasocket.c.v | 13 +++ vlib/net/address_android.c.v | 3 + vlib/net/address_default.c.v | 3 + vlib/net/address_linux.c.v | 3 + vlib/net/address_test.v | 13 --- vlib/net/address_windows.c.v | 3 + vlib/net/unix/aasocket.c.v | 92 ++----------------- vlib/net/unix/stream_nix.v | 4 +- .../unix/use_net_and_net_unix_together_test.v | 48 ++++++++++ 9 files changed, 81 insertions(+), 101 deletions(-) create mode 100644 vlib/net/unix/use_net_and_net_unix_together_test.v diff --git a/vlib/net/aasocket.c.v b/vlib/net/aasocket.c.v index 4979f6aa05..49967bb707 100644 --- a/vlib/net/aasocket.c.v +++ b/vlib/net/aasocket.c.v @@ -6,6 +6,19 @@ $if windows { #include "@VMODROOT/vlib/net/ipv6_v6only.h" } +$if windows { + $if msvc { + // Force these to be included before afunix! + #include + #include + #include + } $else { + #include "@VMODROOT/vlib/net/afunix.h" + } +} $else { + #include +} + // Select represents a select operation enum Select { read diff --git a/vlib/net/address_android.c.v b/vlib/net/address_android.c.v index a6f7a97aa2..7f4149fcbc 100644 --- a/vlib/net/address_android.c.v +++ b/vlib/net/address_android.c.v @@ -15,18 +15,21 @@ mut: } struct C.sockaddr_in { +mut: sin_family u16 sin_port u16 sin_addr u32 } struct C.sockaddr_in6 { +mut: sin6_family u16 sin6_port u16 sin6_addr [4]u32 } struct C.sockaddr_un { +mut: sun_family u16 sun_path [max_unix_path]char } diff --git a/vlib/net/address_default.c.v b/vlib/net/address_default.c.v index 95942cc940..43c33e115b 100644 --- a/vlib/net/address_default.c.v +++ b/vlib/net/address_default.c.v @@ -15,18 +15,21 @@ mut: } struct C.sockaddr_in { +mut: sin_family byte sin_port u16 sin_addr u32 } struct C.sockaddr_in6 { +mut: sin6_family byte sin6_port u16 sin6_addr [4]u32 } struct C.sockaddr_un { +mut: sun_family byte sun_path [max_unix_path]char } diff --git a/vlib/net/address_linux.c.v b/vlib/net/address_linux.c.v index a6f7a97aa2..7f4149fcbc 100644 --- a/vlib/net/address_linux.c.v +++ b/vlib/net/address_linux.c.v @@ -15,18 +15,21 @@ mut: } struct C.sockaddr_in { +mut: sin_family u16 sin_port u16 sin_addr u32 } struct C.sockaddr_in6 { +mut: sin6_family u16 sin6_port u16 sin6_addr [4]u32 } struct C.sockaddr_un { +mut: sun_family u16 sun_path [max_unix_path]char } diff --git a/vlib/net/address_test.v b/vlib/net/address_test.v index de472b95ac..cbd0ce5dfa 100644 --- a/vlib/net/address_test.v +++ b/vlib/net/address_test.v @@ -1,18 +1,5 @@ module net -$if windows { - $if msvc { - // Force these to be included before afunix! - #include - #include - #include - } $else { - #include "@VMODROOT/vlib/net/afunix.h" - } -} $else { - #include -} - fn test_diagnostics() { dump(aoffset) eprintln('--------') diff --git a/vlib/net/address_windows.c.v b/vlib/net/address_windows.c.v index e50fdb4e9d..aa3bdcc4a1 100644 --- a/vlib/net/address_windows.c.v +++ b/vlib/net/address_windows.c.v @@ -15,18 +15,21 @@ mut: } struct C.sockaddr_in { +mut: sin_family u16 sin_port u16 sin_addr u32 } struct C.sockaddr_in6 { +mut: sin6_family u16 sin6_port u16 sin6_addr [4]u32 } struct C.sockaddr_un { +mut: sun_family u16 sun_path [max_unix_path]char } diff --git a/vlib/net/unix/aasocket.c.v b/vlib/net/unix/aasocket.c.v index 929463883e..506830b046 100644 --- a/vlib/net/unix/aasocket.c.v +++ b/vlib/net/unix/aasocket.c.v @@ -1,6 +1,11 @@ module unix -#include +import net + +const use_net = net.no_timeout + +// 104 for macos, 108 for linux => use the minimum +const max_sun_path = 104 // Select represents a select operation enum Select { @@ -15,88 +20,3 @@ enum Select { // stream = C.SOCK_STREAM // seqpacket = C.SOCK_SEQPACKET // } - -struct C.sockaddr { - sa_family u16 -} - -const max_sun_path = 104 - -// 104 for macos, 108 for linux => use the minimum - -struct C.sockaddr_un { -mut: - // sun_len byte // only on macos - sun_family int - sun_path [104]char // on linux that is 108 -} - -struct C.addrinfo { -mut: - ai_family int - ai_socktype int - ai_flags int - ai_protocol int - ai_addrlen int - ai_addr voidptr - ai_canonname voidptr - ai_next voidptr -} - -struct C.sockaddr_storage { -} - -// fn C.socket() int - -// fn C.setsockopt() int - -// fn C.htonl() int - -// fn C.htons() int - -// fn C.bind() int - -// fn C.listen() int - -// fn C.accept() int - -// fn C.getaddrinfo() int - -// fn C.connect() int - -// fn C.send() int - -// fn C.sendto() int - -// fn C.recv() int - -// fn C.recvfrom() int - -// fn C.shutdown() int - -// fn C.getpeername() int - -// fn C.inet_ntop(af int, src voidptr, dst charptr, dst_size int) charptr - -fn C.WSAAddressToStringA() int - -// fn C.getsockname() int - -// defined in builtin -// fn C.read() int -// fn C.close() int - -fn C.ioctlsocket() int - -// fn C.fcntl() int - -// fn C.@select() int - -// fn C.FD_ZERO() - -// fn C.FD_SET() - -// fn C.FD_ISSET() bool - -[typedef] -struct C.fd_set {} diff --git a/vlib/net/unix/stream_nix.v b/vlib/net/unix/stream_nix.v index e73acb7b8a..6552d41fc6 100644 --- a/vlib/net/unix/stream_nix.v +++ b/vlib/net/unix/stream_nix.v @@ -62,7 +62,7 @@ fn (mut s StreamSocket) connect(a string) ? { } mut addr := C.sockaddr_un{} unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) } - addr.sun_family = C.AF_UNIX + addr.sun_family = u16(C.AF_UNIX) unsafe { C.strncpy(&addr.sun_path[0], &char(a.str), max_sun_path) } size := C.SUN_LEN(&addr) res := C.connect(s.handle, voidptr(&addr), size) @@ -94,7 +94,7 @@ pub fn listen_stream(sock string) ?&StreamListener { s.path = sock mut addr := C.sockaddr_un{} unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) } - addr.sun_family = C.AF_UNIX + addr.sun_family = u16(C.AF_UNIX) unsafe { C.strncpy(&addr.sun_path[0], &char(sock.str), max_sun_path) } size := C.SUN_LEN(&addr) if os.exists(sock) { diff --git a/vlib/net/unix/use_net_and_net_unix_together_test.v b/vlib/net/unix/use_net_and_net_unix_together_test.v new file mode 100644 index 0000000000..f4cdf77661 --- /dev/null +++ b/vlib/net/unix/use_net_and_net_unix_together_test.v @@ -0,0 +1,48 @@ +import os +import net.unix +import net + +// ensure that `net` is used, i.e. no warnings +const use_net = net.no_timeout + +const test_port = os.join_path(os.temp_dir(), 'unix_domain_socket') + +fn test_that_net_and_net_unix_can_be_imported_together_without_conflicts() ? { + mut l := unix.listen_stream(test_port) or { panic(err) } + go echo_server(mut l) + defer { + l.close() ? + } + // + mut c := unix.connect_stream(test_port) ? + defer { + c.close() ? + } + // + data := 'Hello from vlib/net!' + c.write_string(data) ? + mut buf := []byte{len: 100} + assert c.read(mut buf) ? == data.len + eprintln('< client read back buf: |${buf[0..data.len].bytestr()}|') + assert buf[0..data.len] == data.bytes() +} + +fn perror(s string) ? { + println(s) +} + +fn handle_conn(mut c unix.StreamConn) ? { + for { + mut buf := []byte{len: 100, init: 0} + read := c.read(mut buf) or { return perror('Server: connection dropped') } + eprintln('> server read ${read:3}, buf: |$buf.bytestr()|') + c.write(buf[..read]) or { return perror('Server: connection dropped') } + } +} + +fn echo_server(mut l unix.StreamListener) ? { + for { + mut new_conn := l.accept() or { continue } + handle_conn(mut new_conn) or {} + } +}