Skip to content

Commit

Permalink
ci: fix unix sockets test on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Feb 11, 2021
1 parent 9b4f2ed commit 8952158
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
7 changes: 6 additions & 1 deletion vlib/net/unix/aasocket.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ 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
sun_family int
sun_path charptr
sun_path [104]char
}

struct C.addrinfo {
Expand Down
12 changes: 6 additions & 6 deletions vlib/net/unix/stream_nix.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ fn (mut s StreamSocket) @select(test Select, timeout time.Duration) ?bool {
}

fn (mut s StreamSocket) connect(a string) ? {
if a.len >= 108 {
return error('Socket path too long! Max length: 107 chars.')
if a.len >= max_sun_path {
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
}
mut addr := C.sockaddr_un{}
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
addr.sun_family = C.AF_UNIX
C.strncpy(addr.sun_path, a.str, 108)
unsafe { C.strncpy(addr.sun_path, a.str, max_sun_path) }
size := C.SUN_LEN(&addr)
sockaddr := unsafe { &C.sockaddr(&addr) }
res := C.connect(s.handle, sockaddr, size)
Expand All @@ -89,15 +89,15 @@ fn (mut s StreamSocket) connect(a string) ? {
}

pub fn listen_stream(sock string) ?&StreamListener {
if sock.len >= 108 {
return error('Socket path too long! Max length: 107 chars.')
if sock.len >= max_sun_path {
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
}
mut s := new_stream_socket() ?
s.path = sock
mut addr := C.sockaddr_un{}
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
addr.sun_family = C.AF_UNIX
C.strncpy(addr.sun_path, sock.str, 108)
unsafe { C.strncpy(addr.sun_path, sock.str, max_sun_path) }
size := C.SUN_LEN(&addr)
sockaddr := unsafe { &C.sockaddr(&addr) }
net.socket_error(C.bind(s.handle, sockaddr, size)) ?
Expand Down
6 changes: 2 additions & 4 deletions vlib/net/unix/unix_test.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import net.unix

const (
test_port = 'test'
)
const test_port = 'unix_domain_socket'

fn handle_conn(mut c unix.StreamConn) {
for {
Expand All @@ -27,7 +25,7 @@ fn echo_server(mut l unix.StreamListener) ? {
}

fn echo() ? {
mut c := unix.connect_stream('test') ?
mut c := unix.connect_stream(test_port) ?
defer {
c.close() or { }
}
Expand Down

0 comments on commit 8952158

Please sign in to comment.