1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net: ipv6 support, merge unix+ip;[pack:x] attribute (#9904)

This commit is contained in:
Emily Hudson
2021-06-13 21:53:38 +01:00
committed by GitHub
parent fa9fa77a5f
commit 535dcac8fa
52 changed files with 1277 additions and 524 deletions

View File

@@ -10,8 +10,7 @@ const (
[heap]
pub struct TcpConn {
pub mut:
sock TcpSocket
max_write_chunk_size int = 4096
sock TcpSocket
mut:
write_deadline time.Time
read_deadline time.Time
@@ -20,30 +19,36 @@ mut:
}
pub fn dial_tcp(address string) ?&TcpConn {
mut s := new_tcp_socket() ?
s.connect(address) ?
return &TcpConn{
sock: s
read_timeout: net.tcp_default_read_timeout
write_timeout: net.tcp_default_write_timeout
addrs := resolve_addrs_fuzzy(address, .tcp) ?
// Very simple dialer
for addr in addrs {
mut s := new_tcp_socket(addr.family()) ?
s.connect(addr) or {
// Connection failed
s.close() or { continue }
continue
}
return &TcpConn{
sock: s
read_timeout: net.tcp_default_read_timeout
write_timeout: net.tcp_default_write_timeout
}
}
// failed
return error('dial_tcp failed')
}
pub fn (mut c TcpConn) close() ? {
$if trace_tcp ? {
eprintln(' TcpConn.close | c.sock.handle: ${c.sock.handle:6}')
}
c.sock.close() ?
return none
}
// write_ptr blocks and attempts to write all data
pub fn (mut c TcpConn) write_ptr(b &byte, len int) ?int {
$if trace_tcp ? {
eprintln('>>> TcpConn.write_ptr | c.sock.handle: ${c.sock.handle:6} | b: ${ptr_str(b)} len: ${len:6}')
}
$if trace_tcp_data_write ? {
eprintln('>>> TcpConn.write_ptr | data.len: ${len:6} | data: ' +
eprintln(
'>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
unsafe { b.vstring_with_len(len) })
}
unsafe {
@@ -51,14 +56,8 @@ pub fn (mut c TcpConn) write_ptr(b &byte, len int) ?int {
mut total_sent := 0
for total_sent < len {
ptr := ptr_base + total_sent
mut chunk_size := len - total_sent
if chunk_size > c.max_write_chunk_size {
chunk_size = c.max_write_chunk_size
}
mut sent := C.send(c.sock.handle, ptr, chunk_size, msg_nosignal)
$if trace_tcp_data_write ? {
eprintln('>>> TcpConn.write_ptr | data chunk, total_sent: ${total_sent:6}, chunk_size: ${chunk_size:6}, sent: ${sent:6}, ptr: ${ptr_str(ptr)}')
}
remaining := len - total_sent
mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal)
if sent < 0 {
code := error_code()
if code == int(error_ewouldblock) {
@@ -91,29 +90,19 @@ pub fn (mut c TcpConn) write_string(s string) ?int {
}
pub fn (mut c TcpConn) read_ptr(buf_ptr &byte, len int) ?int {
mut res := wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
mut res := wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0)) ?
$if trace_tcp ? {
eprintln('<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle:6} | buf_ptr: ${ptr_str(buf_ptr):8} len: ${len:6} | res: ${res:6}')
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
}
if res > 0 {
$if trace_tcp_data_read ? {
eprintln('<<< TcpConn.read_ptr | 1 data.len: ${res:6} | data: ' +
unsafe { buf_ptr.vstring_with_len(res) })
}
return res
}
code := error_code()
if code == int(error_ewouldblock) {
c.wait_for_read() ?
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
res = wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0)) ?
$if trace_tcp ? {
eprintln('<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle:6} | buf_ptr: ${ptr_str(buf_ptr):8} len: ${len:6} | res: ${res:6}')
}
$if trace_tcp_data_read ? {
if res > 0 {
eprintln('<<< TcpConn.read_ptr | 2 data.len: ${res:6} | data: ' +
unsafe { buf_ptr.vstring_with_len(res) })
}
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
}
return socket_error(res)
} else {
@@ -175,23 +164,22 @@ pub fn (mut c TcpConn) wait_for_write() ? {
}
pub fn (c &TcpConn) peer_addr() ?Addr {
mut addr := C.sockaddr{}
len := sizeof(C.sockaddr)
socket_error(C.getpeername(c.sock.handle, &addr, &len)) ?
return new_addr(addr)
mut addr := Addr{
addr: {
Ip6: {}
}
}
mut size := sizeof(Addr)
socket_error(C.getpeername(c.sock.handle, voidptr(&addr), &size)) ?
return addr
}
pub fn (c &TcpConn) peer_ip() ?string {
buf := [44]char{}
peeraddr := C.sockaddr_in{}
speeraddr := sizeof(peeraddr)
socket_error(C.getpeername(c.sock.handle, unsafe { &C.sockaddr(&peeraddr) }, &speeraddr)) ?
cstr := &char(C.inet_ntop(SocketFamily.inet, &peeraddr.sin_addr, &buf[0], sizeof(buf)))
if cstr == 0 {
return error('net.peer_ip: inet_ntop failed')
}
res := unsafe { cstring_to_vstring(cstr) }
return res
return c.peer_addr() ?.str()
}
pub fn (c &TcpConn) addr() ?Addr {
return c.sock.address()
}
pub fn (c TcpConn) str() string {
@@ -207,17 +195,18 @@ mut:
accept_deadline time.Time
}
pub fn listen_tcp(port int) ?&TcpListener {
s := new_tcp_socket() ?
validate_port(port) ?
mut addr := C.sockaddr_in{}
addr.sin_family = int(SocketFamily.inet)
addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := sizeof(C.sockaddr_in)
pub fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener {
s := new_tcp_socket(family) ?
addrs := resolve_addrs(saddr, family, .tcp) ?
// TODO(logic to pick here)
addr := addrs[0]
// cast to the correct type
sockaddr := unsafe { &C.sockaddr(&addr) }
socket_error(C.bind(s.handle, sockaddr, size)) ?
alen := addr.len()
bindres := C.bind(s.handle, voidptr(&addr), alen)
socket_error(bindres) ?
socket_error(C.listen(s.handle, 128)) ?
return &TcpListener{
sock: s
@@ -227,26 +216,21 @@ pub fn listen_tcp(port int) ?&TcpListener {
}
pub fn (mut l TcpListener) accept() ?&TcpConn {
$if trace_tcp ? {
eprintln(' TcpListener.accept | l.sock.handle: ${l.sock.handle:6}')
addr := Addr{
addr: {
Ip6: {}
}
}
addr := C.sockaddr_storage{}
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_storage)) }
size := sizeof(C.sockaddr_storage)
// cast to correct type
sock_addr := unsafe { &C.sockaddr(&addr) }
mut new_handle := C.accept(l.sock.handle, sock_addr, &size)
size := sizeof(Addr)
mut new_handle := C.accept(l.sock.handle, voidptr(&addr), &size)
if new_handle <= 0 {
l.wait_for_accept() ?
new_handle = C.accept(l.sock.handle, sock_addr, &size)
new_handle = C.accept(l.sock.handle, voidptr(&addr), &size)
if new_handle == -1 || new_handle == 0 {
return none
return error('accept failed')
}
}
new_sock := tcp_socket_from_handle(new_handle) ?
$if trace_tcp ? {
eprintln(' TcpListener.accept | << new_sock.handle: ${new_sock.handle:6}')
}
return &TcpConn{
sock: new_sock
read_timeout: net.tcp_default_read_timeout
@@ -258,7 +242,7 @@ pub fn (c &TcpListener) accept_deadline() ?time.Time {
if c.accept_deadline.unix != 0 {
return c.accept_deadline
}
return none
return error('invalid deadline')
}
pub fn (mut c TcpListener) set_accept_deadline(deadline time.Time) {
@@ -278,14 +262,10 @@ pub fn (mut c TcpListener) wait_for_accept() ? {
}
pub fn (mut c TcpListener) close() ? {
$if trace_tcp ? {
eprintln(' TcpListener.close | c.sock.handle: ${c.sock.handle:6}')
}
c.sock.close() ?
return none
}
pub fn (c &TcpListener) address() ?Addr {
pub fn (c &TcpListener) addr() ?Addr {
return c.sock.address()
}
@@ -294,23 +274,23 @@ pub:
handle int
}
fn new_tcp_socket() ?TcpSocket {
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0)) ?
fn new_tcp_socket(family AddrFamily) ?TcpSocket {
handle := socket_error(C.socket(family, SocketType.tcp, 0)) ?
mut s := TcpSocket{
handle: sockfd
handle: handle
}
$if trace_tcp ? {
eprintln(' new_tcp_socket | s.handle: ${s.handle:6}')
}
// s.set_option_bool(.reuse_addr, true)?
// TODO(emily):
// we shouldnt be using ioctlsocket in the 21st century
// use the non-blocking socket option instead please :)
// TODO(emily):
// Move this to its own function on the socket
s.set_option_int(.reuse_addr, 1) ?
$if !net_blocking_sockets ? {
$if windows {
t := u32(1) // true
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
}
$if windows {
t := u32(1) // true
socket_error(C.ioctlsocket(handle, fionbio, &t)) ?
} $else {
socket_error(C.fcntl(handle, C.F_SETFL, C.fcntl(handle, C.F_GETFL) | C.O_NONBLOCK)) ?
}
return s
}
@@ -319,18 +299,16 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
mut s := TcpSocket{
handle: sockfd
}
$if trace_tcp ? {
eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}')
}
// s.set_option_bool(.reuse_addr, true)?
s.set_option_int(.reuse_addr, 1) ?
$if !net_blocking_sockets ? {
$if windows {
t := u32(1) // true
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
}
s.set_dualstack(true) or {
// Not ipv6, we dont care
}
$if windows {
t := u32(1) // true
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
}
return s
}
@@ -345,18 +323,19 @@ pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
// }
x := int(value)
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &x, sizeof(int))) ?
return none
}
pub fn (mut s TcpSocket) set_dualstack(on bool) ? {
x := int(!on)
socket_error(C.setsockopt(s.handle, C.IPPROTO_IPV6, int(SocketOption.ipv6_only), &x,
sizeof(int))) ?
}
pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ? {
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int))) ?
return none
}
fn (mut s TcpSocket) close() ? {
$if trace_tcp ? {
eprintln(' TcpSocket.close | s.handle: ${s.handle:6}')
}
return shutdown(s.handle)
}
@@ -368,35 +347,41 @@ const (
connect_timeout = 5 * time.second
)
fn (mut s TcpSocket) connect(a string) ? {
$if trace_tcp ? {
eprintln(' TcpSocket.connect | s.handle: ${s.handle:6} | a: $a')
}
addr := resolve_addr(a, .inet, .tcp) ?
res := C.connect(s.handle, &addr.addr, addr.len)
fn (mut s TcpSocket) connect(a Addr) ? {
res := C.connect(s.handle, voidptr(&a), a.len())
if res == 0 {
return none
return
}
_ := error_code()
// The socket is nonblocking and the connection cannot be completed
// immediately. (UNIX domain sockets failed with EAGAIN instead.)
// It is possible to select(2) or poll(2) for completion by selecting
// the socket for writing. After select(2) indicates writability,
// use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to
// determine whether connect() completed successfully (SO_ERROR is zero) or
// unsuccessfully (SO_ERROR is one of the usual error codes listed here,
// ex plaining the reason for the failure).
write_result := s.@select(.write, net.connect_timeout) ?
if write_result {
// succeeded
return none
}
except_result := s.@select(.except, net.connect_timeout) ?
if except_result {
return err_connect_failed
err := 0
len := sizeof(err)
socket_error(C.getsockopt(s.handle, C.SOL_SOCKET, C.SO_ERROR, &err, &len)) ?
if err != 0 {
return wrap_error(err)
}
// Succeeded
return
}
// Get the error
socket_error(C.connect(s.handle, voidptr(&a), a.len())) ?
// otherwise we timed out
return err_connect_timed_out
}
// address gets the address of a socket
pub fn (s &TcpSocket) address() ?Addr {
mut addr := C.sockaddr_in{}
size := sizeof(C.sockaddr_in)
// cast to the correct type
sockaddr := unsafe { &C.sockaddr(&addr) }
C.getsockname(s.handle, sockaddr, &size)
return new_addr(sockaddr)
return addr_from_socket_handle(s.handle)
}