v / vlib / net
Raw file | 117 loc (83 sloc) | 3.51 KB | Latest commit hash c9867a9ae
1module 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
23enum Select {
24 read
25 write
26 except
27}
28
29// SocketType are the available sockets
30pub 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
37pub enum AddrFamily {
38 unix = C.AF_UNIX
39 ip = C.AF_INET
40 ip6 = C.AF_INET6
41 unspec = C.AF_UNSPEC
42}
43
44fn 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
47fn C.setsockopt(sockfd int, level int, optname int, optval voidptr, optlen u32) int
48
49fn C.htonl(host u32) u32
50fn C.htons(host u16) u16
51
52fn C.ntohl(net u32) u32
53fn 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
58fn C.bind(sockfd int, addr &Addr, addrlen u32) int
59
60fn C.listen(sockfd int, backlog int) int
61
62// fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int
63fn C.accept(sockfd int, addr &Addr, addrlen &u32) int
64
65fn C.getaddrinfo(node &char, service &char, hints &C.addrinfo, res &&C.addrinfo) int
66
67fn C.freeaddrinfo(info &C.addrinfo)
68
69// fn C.connect(sockfd int, addr &C.sockaddr, addrlen C.socklen_t) int
70fn C.connect(sockfd int, addr &Addr, addrlen u32) int
71
72// fn C.send(sockfd int, buf voidptr, len usize, flags int) usize
73fn 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
76fn 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
79fn 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
82fn C.recvfrom(sockfd int, buf voidptr, len usize, flags int, src_addr &Addr, addrlen &u32) int
83
84fn C.shutdown(socket int, how int) int
85
86// fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &C.socklen_t) int
87fn C.getpeername(sockfd int, addr &Addr, addlen &u32) int
88
89fn C.inet_ntop(af AddrFamily, src voidptr, dst &char, dst_size int) &char
90
91fn 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
94fn C.getsockname(sockfd int, addr &C.sockaddr, addrlen &u32) int
95
96fn 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
102fn C.ioctlsocket(s int, cmd int, argp &u32) int
103
104fn C.fcntl(fd int, cmd int, arg ...voidptr) int
105
106fn C.@select(ndfs int, readfds &C.fd_set, writefds &C.fd_set, exceptfds &C.fd_set, timeout &C.timeval) int
107
108fn C.FD_ZERO(fdset &C.fd_set)
109
110fn C.FD_SET(fd int, fdset &C.fd_set)
111
112fn C.FD_ISSET(fd int, fdset &C.fd_set) bool
113
114fn C.inet_pton(family AddrFamily, saddr &char, addr voidptr) int
115
116[typedef]
117pub struct C.fd_set {}