| 1 | module net |
| 2 | |
| 3 | $if windows { |
| 4 | // This is mainly here for tcc on windows |
| 5 | // which apparently doesnt have this definition |
| 6 | #include "@VMODROOT/vlib/net/ipv6_v6only.h" |
| 7 | } |
| 8 | |
| 9 | $if windows { |
| 10 | $if msvc { |
| 11 | // Force these to be included before afunix! |
| 12 | #include <winsock2.h> |
| 13 | #include <ws2tcpip.h> |
| 14 | #include <afunix.h> |
| 15 | } $else { |
| 16 | #include "@VMODROOT/vlib/net/afunix.h" |
| 17 | } |
| 18 | } $else { |
| 19 | #include <sys/un.h> |
| 20 | } |
| 21 | |
| 22 | // Select represents a select operation |
| 23 | enum Select { |
| 24 | read |
| 25 | write |
| 26 | except |
| 27 | } |
| 28 | |
| 29 | // SocketType are the available sockets |
| 30 | pub enum SocketType { |
| 31 | udp = C.SOCK_DGRAM |
| 32 | tcp = C.SOCK_STREAM |
| 33 | seqpacket = C.SOCK_SEQPACKET |
| 34 | raw = C.SOCK_RAW |
| 35 | } |
| 36 | |
| 37 | // AddrFamily are the available address families |
| 38 | pub enum AddrFamily { |
| 39 | unix = C.AF_UNIX |
| 40 | ip = C.AF_INET |
| 41 | ip6 = C.AF_INET6 |
| 42 | unspec = C.AF_UNSPEC |
| 43 | } |
| 44 | |
| 45 | fn C.socket(domain i32, typ i32, protocol i32) i32 |
| 46 | |
| 47 | // fn C.setsockopt(sockfd int, level int, optname int, optval voidptr, optlen C.socklen_t) int |
| 48 | fn C.setsockopt(sockfd i32, level i32, optname i32, optval voidptr, optlen u32) i32 |
| 49 | |
| 50 | fn C.htonl(host u32) u32 |
| 51 | fn C.htons(host u16) u16 |
| 52 | |
| 53 | fn C.ntohl(net u32) u32 |
| 54 | fn C.ntohs(net u16) u16 |
| 55 | |
| 56 | // fn C.bind(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int |
| 57 | // use voidptr for arg 2 because sockaddr is a generic descriptor for any kind of socket operation, |
| 58 | // it can also take sockaddr_in depending on the type of socket used in arg 1 |
| 59 | fn C.bind(sockfd i32, addr voidptr, addrlen u32) i32 |
| 60 | |
| 61 | fn C.listen(sockfd i32, backlog i32) i32 |
| 62 | |
| 63 | // fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int |
| 64 | fn C.accept(sockfd i32, addr voidptr, addrlen &u32) i32 |
| 65 | |
| 66 | fn C.getaddrinfo(node &char, service &char, hints &C.addrinfo, res &&C.addrinfo) i32 |
| 67 | |
| 68 | fn C.freeaddrinfo(info &C.addrinfo) |
| 69 | |
| 70 | $if !windows { |
| 71 | fn C.gai_strerror(code i32) &char |
| 72 | } |
| 73 | |
| 74 | // fn C.connect(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int |
| 75 | fn C.connect(sockfd i32, addr voidptr, addrlen u32) i32 |
| 76 | |
| 77 | // fn C.send(sockfd int, buf voidptr, len usize, flags int) usize |
| 78 | fn C.send(sockfd i32, buf voidptr, len usize, flags i32) i32 |
| 79 | |
| 80 | // fn C.sendto(sockfd int, buf voidptr, len usize, flags int, dest_add &C.sockaddr, addrlen C.socklen_t) usize |
| 81 | fn C.sendto(sockfd i32, buf voidptr, len usize, flags i32, dest_add voidptr, addrlen u32) i32 |
| 82 | |
| 83 | // fn C.recv(sockfd int, buf voidptr, len usize, flags int) usize |
| 84 | fn C.recv(sockfd i32, buf voidptr, len usize, flags i32) i32 |
| 85 | |
| 86 | // fn C.recvfrom(sockfd int, buf voidptr, len usize, flags int, src_addr &C.sockaddr, addrlen &C.socklen_t) usize |
| 87 | fn C.recvfrom(sockfd i32, buf voidptr, len usize, flags i32, src_addr voidptr, addrlen &u32) i32 |
| 88 | |
| 89 | fn C.shutdown(socket i32, how i32) i32 |
| 90 | |
| 91 | // fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &C.socklen_t) int |
| 92 | fn C.getpeername(sockfd i32, addr voidptr, addlen &u32) i32 |
| 93 | |
| 94 | fn C.inet_ntop(af i32, src voidptr, dst &char, dst_size i32) &char |
| 95 | |
| 96 | fn C.WSAAddressToStringA(lpsaAddress voidptr, dwAddressLength u32, lpProtocolInfo voidptr, lpszAddressString &char, |
| 97 | lpdwAddressStringLength &u32) i32 |
| 98 | |
| 99 | // fn C.getsockname(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int |
| 100 | fn C.getsockname(sockfd i32, addr &C.sockaddr, addrlen &u32) i32 |
| 101 | |
| 102 | fn C.getsockopt(sockfd i32, level i32, optname i32, optval voidptr, optlen &u32) i32 |
| 103 | |
| 104 | // defined in builtin |
| 105 | // fn C.read() int |
| 106 | // fn C.close() int |
| 107 | |
| 108 | fn C.ioctlsocket(s i32, cmd i32, argp &u32) i32 |
| 109 | |
| 110 | fn C.fcntl(fd i32, cmd i32, arg ...voidptr) i32 |
| 111 | |
| 112 | fn C.select(ndfs i32, readfds &C.fd_set, writefds &C.fd_set, exceptfds &C.fd_set, timeout &C.timeval) i32 |
| 113 | |
| 114 | fn C.FD_ZERO(fdset &C.fd_set) |
| 115 | |
| 116 | fn C.FD_SET(fd i32, fdset &C.fd_set) |
| 117 | |
| 118 | fn C.FD_ISSET(fd i32, fdset &C.fd_set) i32 |
| 119 | |
| 120 | fn C.inet_pton(family i32, saddr &char, addr voidptr) i32 |
| 121 | |
| 122 | fn C.photon_socket(domain i32, typ i32, protocol i32) i32 |
| 123 | fn C.photon_connect(i32, voidptr, u32, timeout u64) i32 |
| 124 | fn C.photon_accept(i32, voidptr, i32, timeout u64) i32 |
| 125 | fn C.photon_send(i32, voidptr, i32, i32, timeout u64) i32 |
| 126 | fn C.photon_recv(i32, voidptr, i32, i32, timeout u64) i32 |
| 127 | |
| 128 | @[typedef] |
| 129 | pub struct C.fd_set {} |
| 130 | |