2020-08-21 00:01:37 +03:00
|
|
|
module net
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2020-12-15 19:31:39 +03:00
|
|
|
const (
|
2021-02-02 11:36:51 +03:00
|
|
|
udp_default_read_timeout = time.second / 10
|
|
|
|
udp_default_write_timeout = time.second / 10
|
2020-12-15 19:31:39 +03:00
|
|
|
)
|
|
|
|
|
2021-02-02 11:36:51 +03:00
|
|
|
struct UdpSocket {
|
2022-02-22 11:34:38 +03:00
|
|
|
Socket
|
|
|
|
l Addr
|
2021-06-13 23:53:38 +03:00
|
|
|
// TODO(emily): replace with option again
|
|
|
|
// when i figure out how to coerce it properly
|
|
|
|
mut:
|
|
|
|
has_r bool
|
|
|
|
r Addr
|
2021-02-02 11:36:51 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
pub struct UdpConn {
|
2021-01-20 13:11:01 +03:00
|
|
|
pub mut:
|
|
|
|
sock UdpSocket
|
2020-08-21 00:01:37 +03:00
|
|
|
mut:
|
|
|
|
write_deadline time.Time
|
2021-01-08 19:41:52 +03:00
|
|
|
read_deadline time.Time
|
|
|
|
read_timeout time.Duration
|
|
|
|
write_timeout time.Duration
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
pub fn dial_udp(raddr string) ?&UdpConn {
|
|
|
|
addrs := resolve_addrs_fuzzy(raddr, .udp) ?
|
|
|
|
|
|
|
|
for addr in addrs {
|
|
|
|
// create a local socket for this
|
|
|
|
// bind to any port (or file) (we dont care in this
|
|
|
|
// case because we only care about the remote)
|
|
|
|
if sock := new_udp_socket_for_remote(addr) {
|
|
|
|
return &UdpConn{
|
|
|
|
sock: sock
|
|
|
|
read_timeout: net.udp_default_read_timeout
|
|
|
|
write_timeout: net.udp_default_write_timeout
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
return none
|
2021-01-30 00:11:05 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
// pub fn dial_udp(laddr string, raddr string) ?&UdpConn {
|
|
|
|
// local := resolve_addr(laddr, .inet, .udp) ?
|
|
|
|
|
|
|
|
// sbase := new_udp_socket() ?
|
|
|
|
|
|
|
|
// sock := UdpSocket{
|
|
|
|
// handle: sbase.handle
|
|
|
|
// l: local
|
|
|
|
// r: resolve_wrapper(raddr)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2021-04-27 08:18:48 +03:00
|
|
|
pub fn (mut c UdpConn) write_ptr(b &byte, len int) ?int {
|
2021-01-08 19:41:52 +03:00
|
|
|
remote := c.sock.remote() or { return err_no_udp_remote }
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_to_ptr(remote, b, len)
|
|
|
|
}
|
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (mut c UdpConn) write(buf []u8) ?int {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_ptr(buf.data, buf.len)
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:25:51 +03:00
|
|
|
pub fn (mut c UdpConn) write_string(s string) ?int {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_ptr(s.str, s.len)
|
|
|
|
}
|
|
|
|
|
2021-04-27 08:18:48 +03:00
|
|
|
pub fn (mut c UdpConn) write_to_ptr(addr Addr, b &byte, len int) ?int {
|
2021-06-13 23:53:38 +03:00
|
|
|
res := C.sendto(c.sock.handle, b, len, 0, voidptr(&addr), addr.len())
|
2020-08-21 00:01:37 +03:00
|
|
|
if res >= 0 {
|
2021-02-27 11:29:18 +03:00
|
|
|
return res
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
code := error_code()
|
2021-01-08 19:41:52 +03:00
|
|
|
if code == int(error_ewouldblock) {
|
|
|
|
c.wait_for_write() ?
|
2021-06-13 23:53:38 +03:00
|
|
|
socket_error(C.sendto(c.sock.handle, b, len, 0, voidptr(&addr), addr.len())) ?
|
2021-01-08 19:41:52 +03:00
|
|
|
} else {
|
|
|
|
wrap_error(code) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_to blocks and writes the buf to the remote addr specified
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (mut c UdpConn) write_to(addr Addr, buf []u8) ?int {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_to_ptr(addr, buf.data, buf.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_to_string blocks and writes the buf to the remote addr specified
|
2021-02-27 11:29:18 +03:00
|
|
|
pub fn (mut c UdpConn) write_to_string(addr Addr, s string) ?int {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_to_ptr(addr, s.str, s.len)
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
// read reads from the socket into buf up to buf.len returning the number of bytes read
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (mut c UdpConn) read(mut buf []u8) ?(int, Addr) {
|
2021-06-13 23:53:38 +03:00
|
|
|
mut addr := Addr{
|
2021-07-20 11:17:08 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
len := sizeof(Addr)
|
|
|
|
mut res := wrap_read_result(C.recvfrom(c.sock.handle, voidptr(buf.data), buf.len,
|
|
|
|
0, voidptr(&addr), &len)) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
if res > 0 {
|
2020-08-21 00:01:37 +03:00
|
|
|
return res, addr
|
|
|
|
}
|
|
|
|
code := error_code()
|
2021-01-08 19:41:52 +03:00
|
|
|
if code == int(error_ewouldblock) {
|
|
|
|
c.wait_for_read() ?
|
|
|
|
// same setup as in tcp
|
2021-06-13 23:53:38 +03:00
|
|
|
res = wrap_read_result(C.recvfrom(c.sock.handle, voidptr(buf.data), buf.len, 0,
|
|
|
|
voidptr(&addr), &len)) ?
|
2021-01-08 19:41:52 +03:00
|
|
|
res2 := socket_error(res) ?
|
|
|
|
return res2, addr
|
|
|
|
} else {
|
|
|
|
wrap_error(code) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) read_deadline() ?time.Time {
|
2020-08-21 00:01:37 +03:00
|
|
|
if c.read_deadline.unix == 0 {
|
|
|
|
return c.read_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c UdpConn) set_read_deadline(deadline time.Time) {
|
|
|
|
c.read_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) write_deadline() ?time.Time {
|
2020-08-21 00:01:37 +03:00
|
|
|
if c.write_deadline.unix == 0 {
|
|
|
|
return c.write_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c UdpConn) set_write_deadline(deadline time.Time) {
|
|
|
|
c.write_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) read_timeout() time.Duration {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.read_timeout
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:41:52 +03:00
|
|
|
pub fn (mut c UdpConn) set_read_timeout(t time.Duration) {
|
2020-08-21 00:01:37 +03:00
|
|
|
c.read_timeout = t
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) write_timeout() time.Duration {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c UdpConn) set_write_timeout(t time.Duration) {
|
|
|
|
c.write_timeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) wait_for_read() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) wait_for_write() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) str() string {
|
2020-08-21 00:01:37 +03:00
|
|
|
// TODO
|
|
|
|
return 'UdpConn'
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) close() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.sock.close()
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
pub fn listen_udp(laddr string) ?&UdpConn {
|
|
|
|
addrs := resolve_addrs_fuzzy(laddr, .udp) ?
|
|
|
|
// TODO(emily):
|
|
|
|
// here we are binding to the first address
|
|
|
|
// and that is probably not ideal
|
|
|
|
addr := addrs[0]
|
2021-01-20 13:11:01 +03:00
|
|
|
return &UdpConn{
|
2021-06-13 23:53:38 +03:00
|
|
|
sock: new_udp_socket(addr) ?
|
2021-01-26 17:43:10 +03:00
|
|
|
read_timeout: net.udp_default_read_timeout
|
|
|
|
write_timeout: net.udp_default_write_timeout
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn new_udp_socket(local_addr Addr) ?&UdpSocket {
|
|
|
|
family := local_addr.family()
|
|
|
|
|
|
|
|
sockfd := socket_error(C.socket(family, SocketType.udp, 0)) ?
|
2021-01-20 13:11:01 +03:00
|
|
|
mut s := &UdpSocket{
|
2020-08-21 00:01:37 +03:00
|
|
|
handle: sockfd
|
2021-06-13 23:53:38 +03:00
|
|
|
l: local_addr
|
|
|
|
r: Addr{
|
2021-07-20 11:17:08 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2021-01-08 19:41:52 +03:00
|
|
|
s.set_option_bool(.reuse_addr, true) ?
|
2021-06-13 23:53:38 +03:00
|
|
|
|
|
|
|
if family == .ip6 {
|
|
|
|
s.set_dualstack(true) ?
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:10:34 +03:00
|
|
|
$if !net_blocking_sockets ? {
|
|
|
|
// NOTE: refer to comments in tcp.v
|
|
|
|
$if windows {
|
|
|
|
t := u32(1) // true
|
|
|
|
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
|
|
|
|
} $else {
|
|
|
|
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ?
|
|
|
|
}
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
// cast to the correct type
|
2021-06-13 23:53:38 +03:00
|
|
|
socket_error(C.bind(s.handle, voidptr(&local_addr), local_addr.len())) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn new_udp_socket_for_remote(raddr Addr) ?&UdpSocket {
|
|
|
|
// Invent a sutible local address for this remote addr
|
2021-07-21 09:58:40 +03:00
|
|
|
// Appease compiler
|
|
|
|
mut addr := Addr{
|
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match raddr.family() {
|
2021-12-06 12:10:25 +03:00
|
|
|
.ip {
|
|
|
|
// Use ip dualstack
|
|
|
|
addr = new_ip(0, addr_ip_any)
|
|
|
|
}
|
|
|
|
.ip6 {
|
2021-06-13 23:53:38 +03:00
|
|
|
// Use ip6 dualstack
|
2021-07-21 09:58:40 +03:00
|
|
|
addr = new_ip6(0, addr_ip6_any)
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
.unix {
|
2021-07-21 09:58:40 +03:00
|
|
|
addr = temp_unix() ?
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('Invalid family')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut sock := new_udp_socket(addr) ?
|
|
|
|
sock.has_r = true
|
|
|
|
sock.r = raddr
|
|
|
|
|
|
|
|
return sock
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
// TODO reenable when this `in` operation works again
|
|
|
|
// if opt !in opts_can_set {
|
|
|
|
// return err_option_not_settable
|
|
|
|
// }
|
|
|
|
// if opt !in opts_bool {
|
|
|
|
// return err_option_wrong_type
|
|
|
|
// }
|
2021-03-18 17:23:29 +03:00
|
|
|
x := int(value)
|
|
|
|
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &x, sizeof(int))) ?
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut s UdpSocket) set_dualstack(on bool) ? {
|
|
|
|
x := int(!on)
|
|
|
|
socket_error(C.setsockopt(s.handle, C.IPPROTO_IPV6, int(SocketOption.ipv6_only), &x,
|
|
|
|
sizeof(int))) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
fn (mut s UdpSocket) close() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return shutdown(s.handle)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
fn (mut s UdpSocket) @select(test Select, timeout time.Duration) ?bool {
|
2020-08-21 00:01:37 +03:00
|
|
|
return @select(s.handle, test, timeout)
|
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
|
|
|
fn (s &UdpSocket) remote() ?Addr {
|
|
|
|
if s.has_r {
|
|
|
|
return s.r
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|