From 1a2ae0aead0f0c14ed07a51d6afeb03ccae2a9d8 Mon Sep 17 00:00:00 2001 From: bettafish04 <55953095+bettafish04@users.noreply.github.com> Date: Mon, 8 Feb 2021 23:48:23 +0100 Subject: [PATCH] net: add basic unix socket support (#8642) --- vlib/net/aasocket.c.v | 18 ++++++++++++++++-- vlib/net/address.v | 28 +++++++++++++--------------- vlib/net/net_nix.c.v | 2 ++ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/vlib/net/aasocket.c.v b/vlib/net/aasocket.c.v index e02f9c3867..f054bbb1dc 100644 --- a/vlib/net/aasocket.c.v +++ b/vlib/net/aasocket.c.v @@ -2,7 +2,9 @@ module net // Select represents a select operation enum Select { - read write except + read + write + except } // SocketType are the available sockets @@ -13,7 +15,8 @@ pub enum SocketType { // SocketFamily are the available address families pub enum SocketFamily { - inet = C. AF_INET + inet = C.AF_INET + unix = C.AF_UNIX } struct C.in_addr { @@ -32,6 +35,11 @@ mut: sin_addr C.in_addr } +struct C.sockaddr_un { +mut: + sun_family int + sun_path charptr +} struct C.addrinfo { mut: @@ -67,9 +75,11 @@ 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 @@ -89,11 +99,15 @@ fn C.getsockname() 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] diff --git a/vlib/net/address.v b/vlib/net/address.v index 41a706dd71..90e24fa60a 100644 --- a/vlib/net/address.v +++ b/vlib/net/address.v @@ -3,22 +3,22 @@ module net // Addr represents an ip address pub struct Addr { addr C.sockaddr - len int + len int pub: saddr string - port int + port int } struct C.addrinfo { } pub fn (a Addr) str() string { - return '${a.saddr}:${a.port}' + return '$a.saddr:$a.port' } const ( max_ipv4_addr_len = 24 - ipv4_addr_size = sizeof(C.sockaddr_in) + ipv4_addr_size = sizeof(C.sockaddr_in) ) fn new_addr(addr C.sockaddr) ?Addr { @@ -29,21 +29,21 @@ fn new_addr(addr C.sockaddr) ?Addr { 0 } // Convert to string representation - buf := []byte{ len: max_ipv4_addr_len, init: 0 } + buf := []byte{len: net.max_ipv4_addr_len, init: 0} $if windows { res := C.WSAAddressToStringA(&addr, addr_len, C.NULL, buf.data, &buf.len) if res != 0 { - socket_error(-1)? + socket_error(-1) ? } } $else { res := C.inet_ntop(SocketFamily.inet, &addr, buf.data, buf.len) if res == 0 { - socket_error(-1)? + socket_error(-1) ? } } mut saddr := buf.bytestr() - hport := unsafe {&C.sockaddr_in(&addr)}.sin_port + hport := unsafe { &C.sockaddr_in(&addr) }.sin_port port := C.ntohs(hport) $if windows { @@ -51,13 +51,11 @@ fn new_addr(addr C.sockaddr) ?Addr { saddr = saddr.split(':')[0] } - return Addr { - addr int(addr_len) saddr port - } + return Addr{addr, int(addr_len), saddr, port} } pub fn resolve_addr(addr string, family SocketFamily, typ SocketType) ?Addr { - address, port := split_address(addr)? + address, port := split_address(addr) ? mut hints := C.addrinfo{} hints.ai_family = int(family) @@ -74,10 +72,10 @@ pub fn resolve_addr(addr string, family SocketFamily, typ SocketType) ?Addr { // This might look silly but is recommended by MSDN $if windows { - socket_error(0-C.getaddrinfo(address.str, sport.str, &hints, &info))? + socket_error(0 - C.getaddrinfo(address.str, sport.str, &hints, &info)) ? } $else { - x := C.getaddrinfo(address.str, sport.str, &hints, &info) - wrap_error(x)? + x := C.getaddrinfo(address.str, sport.str, &hints, &info) + wrap_error(x) ? } return new_addr(*info.ai_addr) diff --git a/vlib/net/net_nix.c.v b/vlib/net/net_nix.c.v index 4230442b19..301d4934e6 100644 --- a/vlib/net/net_nix.c.v +++ b/vlib/net/net_nix.c.v @@ -3,6 +3,7 @@ module net #include #include #include +#include #include #include #include @@ -10,6 +11,7 @@ module net #include #include #flag solaris -lsocket + fn error_code() int { return C.errno }