v / vlib / net / aasocket.c.v
129 lines · 93 sloc · 3.88 KB · 9e1d3ca05db333f60c4201e1d35db705c319d4dd
Raw
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 raw = C.SOCK_RAW
35}
36
37// AddrFamily are the available address families
38pub enum AddrFamily {
39 unix = C.AF_UNIX
40 ip = C.AF_INET
41 ip6 = C.AF_INET6
42 unspec = C.AF_UNSPEC
43}
44
45fn 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
48fn C.setsockopt(sockfd i32, level i32, optname i32, optval voidptr, optlen u32) i32
49
50fn C.htonl(host u32) u32
51fn C.htons(host u16) u16
52
53fn C.ntohl(net u32) u32
54fn 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
59fn C.bind(sockfd i32, addr voidptr, addrlen u32) i32
60
61fn C.listen(sockfd i32, backlog i32) i32
62
63// fn C.accept(sockfd int, addr &C.sockaddr, addrlen &C.socklen_t) int
64fn C.accept(sockfd i32, addr voidptr, addrlen &u32) i32
65
66fn C.getaddrinfo(node &char, service &char, hints &C.addrinfo, res &&C.addrinfo) i32
67
68fn 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
75fn C.connect(sockfd i32, addr voidptr, addrlen u32) i32
76
77// fn C.send(sockfd int, buf voidptr, len usize, flags int) usize
78fn 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
81fn 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
84fn 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
87fn C.recvfrom(sockfd i32, buf voidptr, len usize, flags i32, src_addr voidptr, addrlen &u32) i32
88
89fn C.shutdown(socket i32, how i32) i32
90
91// fn C.getpeername(sockfd int, addr &C.sockaddr, addlen &C.socklen_t) int
92fn C.getpeername(sockfd i32, addr voidptr, addlen &u32) i32
93
94fn C.inet_ntop(af i32, src voidptr, dst &char, dst_size i32) &char
95
96fn 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
100fn C.getsockname(sockfd i32, addr &C.sockaddr, addrlen &u32) i32
101
102fn 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
108fn C.ioctlsocket(s i32, cmd i32, argp &u32) i32
109
110fn C.fcntl(fd i32, cmd i32, arg ...voidptr) i32
111
112fn C.select(ndfs i32, readfds &C.fd_set, writefds &C.fd_set, exceptfds &C.fd_set, timeout &C.timeval) i32
113
114fn C.FD_ZERO(fdset &C.fd_set)
115
116fn C.FD_SET(fd i32, fdset &C.fd_set)
117
118fn C.FD_ISSET(fd i32, fdset &C.fd_set) i32
119
120fn C.inet_pton(family i32, saddr &char, addr voidptr) i32
121
122fn C.photon_socket(domain i32, typ i32, protocol i32) i32
123fn C.photon_connect(i32, voidptr, u32, timeout u64) i32
124fn C.photon_accept(i32, voidptr, i32, timeout u64) i32
125fn C.photon_send(i32, voidptr, i32, i32, timeout u64) i32
126fn C.photon_recv(i32, voidptr, i32, i32, timeout u64) i32
127
128@[typedef]
129pub struct C.fd_set {}
130