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 | } |
35 | |
36 | // AddrFamily are the available address families |
37 | pub enum AddrFamily { |
38 | unix = C.AF_UNIX |
39 | ip = C.AF_INET |
40 | ip6 = C.AF_INET6 |
41 | unspec = C.AF_UNSPEC |
42 | } |
43 | |
44 | fn C.socket(domain AddrFamily, typ SocketType, protocol int) int |
45 | |
46 | // fn C.setsockopt(sockfd int, level int, optname int, optval voidptr, optlen C.socklen_t) int |
47 | fn C.setsockopt(sockfd int, level int, optname int, optval voidptr, optlen u32) int |
48 | |
49 | fn C.htonl(host u32) u32 |
50 | fn C.htons(host u16) u16 |
51 | |
52 | fn C.ntohl(net u32) u32 |
53 | fn C.ntohs(net u16) u16 |
54 | |
55 | // fn C.bind(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int |
56 | // use voidptr for arg 2 because sockaddr is a generic descriptor for any kind of socket operation, |
57 | // it can also take sockaddr_in depending on the type of socket used in arg 1 |
58 | fn C.bind(sockfd int, addr &Addr, addrlen u32) int |
59 | |
60 | fn C.listen(sockfd int, backlog int) int |
61 | |
62 | // fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int |
63 | fn C.accept(sockfd int, addr &Addr, addrlen &u32) int |
64 | |
65 | fn C.getaddrinfo(node &char, service &char, hints &C.addrinfo, res &&C.addrinfo) int |
66 | |
67 | fn C.freeaddrinfo(info &C.addrinfo) |
68 | |
69 | // fn C.connect(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int |
70 | fn C.connect(sockfd int, addr &Addr, addrlen u32) int |
71 | |
72 | // fn C.send(sockfd int, buf voidptr, len usize, flags int) usize |
73 | fn C.send(sockfd int, buf voidptr, len usize, flags int) int |
74 | |
75 | // fn C.sendto(sockfd int, buf voidptr, len usize, flags int, dest_add &C.sockaddr, addrlen C.socklen_t) usize |
76 | fn C.sendto(sockfd int, buf voidptr, len usize, flags int, dest_add &Addr, addrlen u32) int |
77 | |
78 | // fn C.recv(sockfd int, buf voidptr, len usize, flags int) usize |
79 | fn C.recv(sockfd int, buf voidptr, len usize, flags int) int |
80 | |
81 | // fn C.recvfrom(sockfd int, buf voidptr, len usize, flags int, src_addr &C.sockaddr, addrlen &C.socklen_t) usize |
82 | fn C.recvfrom(sockfd int, buf voidptr, len usize, flags int, src_addr &Addr, addrlen &u32) int |
83 | |
84 | fn C.shutdown(socket int, how int) int |
85 | |
86 | // fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &C.socklen_t) int |
87 | fn C.getpeername(sockfd int, addr &Addr, addlen &u32) int |
88 | |
89 | fn C.inet_ntop(af AddrFamily, src voidptr, dst &char, dst_size int) &char |
90 | |
91 | fn C.WSAAddressToStringA(lpsaAddress &Addr, dwAddressLength u32, lpProtocolInfo voidptr, lpszAddressString &char, lpdwAddressStringLength &u32) int |
92 | |
93 | // fn C.getsockname(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int |
94 | fn C.getsockname(sockfd int, addr &C.sockaddr, addrlen &u32) int |
95 | |
96 | fn C.getsockopt(sockfd int, level int, optname int, optval voidptr, optlen &u32) int |
97 | |
98 | // defined in builtin |
99 | // fn C.read() int |
100 | // fn C.close() int |
101 | |
102 | fn C.ioctlsocket(s int, cmd int, argp &u32) int |
103 | |
104 | fn C.fcntl(fd int, cmd int, arg ...voidptr) int |
105 | |
106 | fn C.@select(ndfs int, readfds &C.fd_set, writefds &C.fd_set, exceptfds &C.fd_set, timeout &C.timeval) int |
107 | |
108 | fn C.FD_ZERO(fdset &C.fd_set) |
109 | |
110 | fn C.FD_SET(fd int, fdset &C.fd_set) |
111 | |
112 | fn C.FD_ISSET(fd int, fdset &C.fd_set) bool |
113 | |
114 | fn C.inet_pton(family AddrFamily, saddr &char, addr voidptr) int |
115 | |
116 | [typedef] |
117 | pub struct C.fd_set {} |