2020-08-21 00:01:37 +03:00
|
|
|
module net
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
$if windows {
|
|
|
|
// This is mainly here for tcc on windows
|
|
|
|
// which apparently doesnt have this definition
|
2021-09-19 16:16:31 +03:00
|
|
|
#include "@VMODROOT/vlib/net/ipv6_v6only.h"
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
// Select represents a select operation
|
|
|
|
enum Select {
|
2021-02-09 01:48:23 +03:00
|
|
|
read
|
|
|
|
write
|
|
|
|
except
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SocketType are the available sockets
|
|
|
|
pub enum SocketType {
|
|
|
|
udp = C.SOCK_DGRAM
|
|
|
|
tcp = C.SOCK_STREAM
|
2021-03-05 17:41:11 +03:00
|
|
|
seqpacket = C.SOCK_SEQPACKET
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
// AddrFamily are the available address families
|
|
|
|
pub enum AddrFamily {
|
2021-03-05 17:41:11 +03:00
|
|
|
unix = C.AF_UNIX
|
2021-06-13 23:53:38 +03:00
|
|
|
ip = C.AF_INET
|
|
|
|
ip6 = C.AF_INET6
|
|
|
|
unspec = C.AF_UNSPEC
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.socket(domain AddrFamily, typ SocketType, protocol int) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
// fn C.setsockopt(sockfd int, level int, optname int, optval voidptr, optlen C.socklen_t) int
|
|
|
|
fn C.setsockopt(sockfd int, level int, optname int, optval voidptr, optlen u32) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-10-01 12:57:47 +03:00
|
|
|
fn C.htonl(host u32) u32
|
|
|
|
fn C.htons(host u16) u16
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-10-01 12:57:47 +03:00
|
|
|
fn C.ntohl(net u32) u32
|
|
|
|
fn C.ntohs(net u16) u16
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
// fn C.bind(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int
|
|
|
|
// use voidptr for arg 2 becasue sockaddr is a generic descriptor for any kind of socket operation,
|
|
|
|
// it can also take sockaddr_in depending on the type of socket used in arg 1
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.bind(sockfd int, addr &Addr, addrlen u32) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.listen(sockfd int, backlog int) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
// fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.accept(sockfd int, addr &Addr, addrlen &u32) int
|
|
|
|
|
|
|
|
fn C.getaddrinfo(node &char, service &char, hints &C.addrinfo, res &&C.addrinfo) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.freeaddrinfo(info &C.addrinfo)
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
// fn C.connect(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.connect(sockfd int, addr &Addr, addrlen u32) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
// fn C.send(sockfd int, buf voidptr, len usize, flags int) usize
|
|
|
|
fn C.send(sockfd int, buf voidptr, len usize, flags int) int
|
2021-02-09 01:48:23 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
// fn C.sendto(sockfd int, buf voidptr, len usize, flags int, dest_add &C.sockaddr, addrlen C.socklen_t) usize
|
|
|
|
fn C.sendto(sockfd int, buf voidptr, len usize, flags int, dest_add &Addr, addrlen u32) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
// fn C.recv(sockfd int, buf voidptr, len usize, flags int) usize
|
|
|
|
fn C.recv(sockfd int, buf voidptr, len usize, flags int) int
|
2021-02-09 01:48:23 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
// fn C.recvfrom(sockfd int, buf voidptr, len usize, flags int, src_addr &C.sockaddr, addrlen &C.socklen_t) usize
|
|
|
|
fn C.recvfrom(sockfd int, buf voidptr, len usize, flags int, src_addr &Addr, addrlen &u32) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.shutdown(socket int, how int) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
// fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &C.socklen_t) int
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.getpeername(sockfd int, addr &Addr, addlen &u32) int
|
2020-11-15 23:54:47 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.inet_ntop(af AddrFamily, src voidptr, dst &char, dst_size int) &char
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.WSAAddressToStringA(lpsaAddress &Addr, dwAddressLength u32, lpProtocolInfo voidptr, lpszAddressString &char, lpdwAddressStringLength &u32) int
|
2020-09-08 16:15:35 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
// fn C.getsockname(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int
|
|
|
|
fn C.getsockname(sockfd int, addr &C.sockaddr, addrlen &u32) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.getsockopt(sockfd int, level int, optname int, optval voidptr, optlen &u32) int
|
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
// defined in builtin
|
|
|
|
// fn C.read() int
|
|
|
|
// fn C.close() int
|
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.ioctlsocket(s int, cmd int, argp &u32) int
|
2021-02-09 01:48:23 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.fcntl(fd int, cmd int, arg ...voidptr) int
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.@select(ndfs int, readfds &C.fd_set, writefds &C.fd_set, exceptfds &C.fd_set, timeout &C.timeval) int
|
2021-02-09 01:48:23 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.FD_ZERO(fdset &C.fd_set)
|
2021-02-09 01:48:23 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.FD_SET(fd int, fdset &C.fd_set)
|
2021-02-09 01:48:23 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.FD_ISSET(fd int, fdset &C.fd_set) bool
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.inet_pton(family AddrFamily, saddr &char, addr voidptr) int
|
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
[typedef]
|
2020-08-22 01:50:38 +03:00
|
|
|
pub struct C.fd_set {}
|