2020-08-20 23:01:37 +02:00
|
|
|
module net
|
|
|
|
|
|
|
|
// Select represents a select operation
|
|
|
|
enum Select {
|
2021-02-08 23:48:23 +01:00
|
|
|
read
|
|
|
|
write
|
|
|
|
except
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SocketType are the available sockets
|
|
|
|
pub enum SocketType {
|
|
|
|
udp = C.SOCK_DGRAM
|
|
|
|
tcp = C.SOCK_STREAM
|
2021-03-05 15:41:11 +01:00
|
|
|
dgram = C.SOCK_DGRAM
|
|
|
|
stream = C.SOCK_STREAM
|
|
|
|
seqpacket = C.SOCK_SEQPACKET
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SocketFamily are the available address families
|
|
|
|
pub enum SocketFamily {
|
2021-03-05 15:41:11 +01:00
|
|
|
unix = C.AF_UNIX
|
2021-02-08 23:48:23 +01:00
|
|
|
inet = C.AF_INET
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct C.in_addr {
|
|
|
|
mut:
|
|
|
|
s_addr int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.sockaddr {
|
2020-11-15 21:54:47 +01:00
|
|
|
sa_family u16
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct C.sockaddr_in {
|
|
|
|
mut:
|
|
|
|
sin_family int
|
|
|
|
sin_port int
|
|
|
|
sin_addr C.in_addr
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:48:23 +01:00
|
|
|
struct C.sockaddr_un {
|
|
|
|
mut:
|
|
|
|
sun_family int
|
|
|
|
sun_path charptr
|
|
|
|
}
|
2020-08-20 23:01:37 +02:00
|
|
|
|
|
|
|
struct C.addrinfo {
|
|
|
|
mut:
|
|
|
|
ai_family int
|
|
|
|
ai_socktype int
|
|
|
|
ai_flags int
|
|
|
|
ai_protocol int
|
|
|
|
ai_addrlen int
|
|
|
|
ai_addr voidptr
|
|
|
|
ai_canonname voidptr
|
|
|
|
ai_next voidptr
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.sockaddr_storage {
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.socket(domain SocketFamily, typ SocketType, protocol int) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01: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-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.htonl(hostlong u32) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.htons(netshort u16) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01: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
|
|
|
|
fn C.bind(sockfd int, addr voidptr, addrlen u32) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.listen(sockfd int, backlog int) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int
|
|
|
|
fn C.accept(sockfd int, addr &C.sockaddr, addrlen &u32) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.getaddrinfo(node charptr, service charptr, hints &C.addrinfo, res &&C.addrinfo) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.connect(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int
|
|
|
|
fn C.connect(sockfd int, addr &C.sockaddr, addrlen u32) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.send(sockfd int, buf voidptr, len size_t, flags int) size_t
|
|
|
|
fn C.send(sockfd int, buf voidptr, len size_t, flags int) int
|
2021-02-08 23:48:23 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.sendto(sockfd int, buf voidptr, len size_t, flags int, dest_add &C.sockaddr, addrlen C.socklen_t) size_t
|
|
|
|
fn C.sendto(sockfd int, buf voidptr, len size_t, flags int, dest_add &C.sockaddr, addrlen u32) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.recv(sockfd int, buf voidptr, len size_t, flags int) size_t
|
|
|
|
fn C.recv(sockfd int, buf voidptr, len size_t, flags int) int
|
2021-02-08 23:48:23 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.recvfrom(sockfd int, buf voidptr, len size_t, flags int, src_addr &C.sockaddr, addrlen &C.socklen_t) size_t
|
|
|
|
fn C.recvfrom(sockfd int, buf voidptr, len size_t, flags int, src_addr &C.sockaddr, addrlen &u32) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.shutdown(socket int, how int) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.ntohs(netshort u16) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &C.socklen_t) int
|
|
|
|
fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &u32) int
|
2020-11-15 21:54:47 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.inet_ntop(af SocketFamily, src voidptr, dst charptr, dst_size int) charptr
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.WSAAddressToStringA(lpsaAddress &C.sockaddr, dwAddressLength u32, lpProtocolInfo voidptr, lpszAddressString charptr, lpdwAddressStringLength &u32) int
|
2020-09-08 15:15:35 +02:00
|
|
|
|
2021-03-05 15:41:11 +01: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-20 23:01:37 +02:00
|
|
|
|
|
|
|
// defined in builtin
|
|
|
|
// fn C.read() int
|
|
|
|
// fn C.close() int
|
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.ioctlsocket(s int, cmd int, argp &u32) int
|
2021-02-08 23:48:23 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.fcntl(fd int, cmd int, arg ...voidptr) int
|
2020-08-20 23:01:37 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.@select(ndfs int, readfds &C.fd_set, writefds &C.fd_set, exceptfds &C.fd_set, timeout &C.timeval) int
|
2021-02-08 23:48:23 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.FD_ZERO(fdset &C.fd_set)
|
2021-02-08 23:48:23 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.FD_SET(fd int, fdset &C.fd_set)
|
2021-02-08 23:48:23 +01:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
fn C.FD_ISSET(fd int, fdset &C.fd_set) bool
|
2020-08-20 23:01:37 +02:00
|
|
|
|
|
|
|
[typedef]
|
2020-08-22 00:50:38 +02:00
|
|
|
pub struct C.fd_set {}
|