nlib
Platform_socket.h
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_PLATFORM_SOCKET_H_
17 #define INCLUDE_NN_NLIB_PLATFORM_SOCKET_H_
18 
19 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
20 #error Do not include this file directly
21 #endif
22 
23 #if defined(_MSC_VER) || defined(NLIB_UNIX)
24 #define NLIB_SOCKET_ENABLED
25 
26 // sizeof(SOCKET) is 8 on Win64, but it's safe because the upper 32bit of SOCKET is always 0.
27 // https://stackoverflow.com/questions/1953639/is-it-safe-to-cast-socket-to-int-under-win64
28 typedef int nlib_sock;
29 #define NLIB_SOCKET_INVALID (nlib_sock)(-1) // NOLINT
30 
31 #if defined(_MSC_VER)
32 #define NLIB_SOCK_NONBLOCK 0x8000
33 #elif defined(SOCK_NONBLOCK)
34 #define NLIB_SOCK_NONBLOCK SOCK_NONBLOCK
35 #else
36 #define NLIB_SOCK_NONBLOCK O_NONBLOCK
37 #endif
38 
39 #if !defined(TCP_FASTOPEN) && defined(_MSC_VER)
40 // (older) Windows SDK may not define TCP_FASTOPEN even if available
41 # define TCP_FASTOPEN 15
42 #endif
43 
44 NLIB_VIS_PUBLIC errno_t nlib_socket(nlib_sock* sockfd, int af, int type, int protocol);
45 
46 typedef struct sockaddr nlib_sockaddr;
47 typedef struct sockaddr_in nlib_sockaddr_in;
48 typedef struct sockaddr_in6 nlib_sockaddr_in6;
49 #ifdef _MSC_VER
50 struct nlib_msghdr_ {
51  void* msg_name;
52  uint32_t msg_namelen;
53  nlib_fd_iovec* msg_iov;
54  size_t msg_iovlen;
55  void* msg_control;
56  size_t msg_controllen;
57  int msg_flags;
58 };
59 typedef struct nlib_msghdr_ nlib_msghdr;
60 #define NLIB_CMSG_FIRSTHDR(msgh) \
61  ((msgh)->msg_controllen >= sizeof(WSACMSGHDR) ? \
62  (LPWSACMSGHDR)(msgh)->msg_control : (LPWSACMSGHDR)NULL)
63 #define NLIB_CMSG_NXTHDR(msg, cmsg) \
64  ( ((cmsg) == NULL) ? NLIB_CMSG_FIRSTHDR(msg) \
65  : ( ( ((PUCHAR)(cmsg) + \
66  WSA_CMSGHDR_ALIGN((cmsg)->cmsg_len) + \
67  sizeof(WSACMSGHDR) ) > \
68  (PUCHAR)((msg)->msg_control) + \
69  (msg)->msg_controllen) \
70  ? (LPWSACMSGHDR)NULL \
71  : (LPWSACMSGHDR)((PUCHAR)(cmsg) + \
72  WSA_CMSGHDR_ALIGN((cmsg)->cmsg_len)) ) )
73 #define NLIB_CMSG_SPACE(length) WSA_CMSG_SPACE(length)
74 #define NLIB_CMSG_LEN(length) WSA_CMSG_LEN(length)
75 #define NLIB_CMSG_DATA(cmsg) WSA_CMSG_DATA(cmsg)
76 #else
77 typedef struct msghdr nlib_msghdr;
78 #define NLIB_CMSG_FIRSTHDR(msgh) CMSG_FIRSTHDR(msgh)
79 #define NLIB_CMSG_NXTHDR(msgh, cmsg) CMSG_NXTHDR(msgh, cmsg)
80 // #define NLIB_CMSG_ALIGN(length) CMSG_ALIGN(length)
81 #define NLIB_CMSG_SPACE(length) CMSG_SPACE(length)
82 #define NLIB_CMSG_LEN(length) CMSG_LEN(length)
83 #define NLIB_CMSG_DATA(cmsg) CMSG_DATA(cmsg)
84 #endif
85 typedef struct cmsghdr nlib_cmsghdr;
86 NLIB_VIS_PUBLIC errno_t nlib_bind(nlib_sock sockfd, const nlib_sockaddr* addr, uint32_t namelen);
87 NLIB_VIS_PUBLIC errno_t nlib_listen(nlib_sock sockfd, int backlog);
89 errno_t nlib_accept(nlib_sock* __restrict s, nlib_sock sockfd, nlib_sockaddr* __restrict addr,
90  uint32_t* __restrict addrlen, int flags);
92 errno_t nlib_accept_for(nlib_sock* __restrict s, nlib_sock sockfd, nlib_sockaddr* __restrict addr,
93  uint32_t* __restrict addrlen, int flags, nlib_duration timeout);
95 errno_t nlib_connect(nlib_sock sockfd, const nlib_sockaddr* addr, uint32_t addrlen);
97 errno_t nlib_connect_for(nlib_sock sockfd, const nlib_sockaddr* addr, uint32_t addrlen,
98  int flags, nlib_duration timeout);
99 
101 errno_t nlib_sendto(size_t* __restrict size, nlib_sock sockfd, const void* __restrict buf,
102  size_t len, int flags, const nlib_sockaddr* __restrict dest_addr,
103  uint32_t addrlen);
105 errno_t nlib_sendmsg(size_t* __restrict size, nlib_sock sockfd, const nlib_msghdr* msg, int flags);
106 static NLIB_C_INLINE
107 errno_t nlib_send(size_t* __restrict size, nlib_sock sockfd, const void* __restrict buf,
108  size_t len, int flags) {
109  return nlib_sendto(size, sockfd, buf, len, flags, NULL, 0);
110 }
111 
113 errno_t nlib_recvfrom(size_t* __restrict size, nlib_sock sockfd,
114  void* __restrict buf, size_t len, int flags,
115  nlib_sockaddr* __restrict dest_addr,
116  uint32_t* __restrict addrlen);
118 errno_t nlib_recvmsg(size_t* __restrict size, nlib_sock sockfd, nlib_msghdr* msg, int flags);
119 static NLIB_C_INLINE
120 errno_t nlib_recv(size_t* __restrict size, nlib_sock sockfd, void* __restrict buf, size_t len,
121  int flags) {
122  return nlib_recvfrom(size, sockfd, buf, len, flags, NULL, NULL);
123 }
124 
125 NLIB_VIS_PUBLIC errno_t nlib_closesocket(nlib_sock sockfd);
126 
127 #ifdef _MSC_VER
128 # define SHUT_RD SD_RECEIVE
129 # define SHUT_WR SD_SEND
130 # define SHUT_RDWR SD_BOTH
131 #endif
132 
133 NLIB_VIS_PUBLIC errno_t nlib_shutdownsocket(nlib_sock sockfd, int how);
134 
135 #ifndef NLIB_LITTLE_ENDIAN
136 static NLIB_C_INLINE uint32_t nlib_htonl(uint32_t hostlong) { return hostlong; }
137 static NLIB_C_INLINE uint16_t nlib_htons(uint16_t hostshort) { return hostshort; }
138 static NLIB_C_INLINE uint32_t nlib_ntohl(uint32_t netlong) { return netlong; }
139 static NLIB_C_INLINE uint16_t nlib_ntohs(uint16_t netshort) { return netshort; }
140 #else
141 static NLIB_C_INLINE uint32_t nlib_htonl(uint32_t hostlong) {
142 #ifdef _MSC_VER
143  return _byteswap_ulong(hostlong);
144 #else
145  return __builtin_bswap32(hostlong);
146 #endif
147 }
148 static NLIB_C_INLINE uint16_t nlib_htons(uint16_t hostshort) {
149 #ifdef _MSC_VER
150  return _byteswap_ushort(hostshort);
151 #else
152  return ((hostshort & 0xFF) << 8) | ((hostshort >> 8) & 0xFF);
153 #endif
154 }
155 static NLIB_C_INLINE uint32_t nlib_ntohl(uint32_t netlong) {
156 #ifdef _MSC_VER
157  return _byteswap_ulong(netlong);
158 #else
159  return __builtin_bswap32(netlong);
160 #endif
161 }
162 static NLIB_C_INLINE uint16_t nlib_ntohs(uint16_t netshort) {
163 #ifdef _MSC_VER
164  return _byteswap_ushort(netshort);
165 #else
166  return ((netshort & 0xFF) << 8) | ((netshort >> 8) & 0xFF);
167 #endif
168 }
169 #endif
170 
171 typedef struct in_addr nlib_in_addr;
172 typedef struct in6_addr nlib_in6_addr;
173 NLIB_VIS_PUBLIC errno_t nlib_inet_pton(int af, const char* __restrict src, void* __restrict dst);
174 NLIB_VIS_PUBLIC errno_t nlib_inet_ntop(int af, const void* __restrict src, char* __restrict dst,
175  uint32_t dst_size);
176 
177 /*
178 # define EAI_ADDRFAMILY 1
179 # define EAI_AGAIN 2
180 # define EAI_BADFLAGS 3
181 # define EAI_FAIL 4
182 # define EAI_FAMILY 5
183 # define EAI_MEMORY 6
184 # define EAI_NODATA 7
185 # define EAI_NONAME 8
186 # define EAI_SERVICE 9
187 # define EAI_SOCKTYPE 10
188 # define EAI_SYSTEM 11
189 # define EAI_BADHINTS 12
190 # define EAI_PROTOCOL 13
191 # define EAI_OVERFLOW 14
192 */
193 
194 // for EAI_ADDRFAMILY, EAI_AGAIN, ... etc.
195 typedef int eai_error_t;
196 typedef struct addrinfo nlib_addrinfo;
198 eai_error_t nlib_getaddrinfo(const char* __restrict node, const char* __restrict service,
199  const nlib_addrinfo* __restrict hints,
200  nlib_addrinfo** __restrict res);
202 eai_error_t nlib_getnameinfo(const nlib_sockaddr* __restrict sa, uint32_t salen,
203  char* __restrict host, uint32_t hostlen,
204  char* __restrict serv, uint32_t servlen, int flags);
205 NLIB_VIS_PUBLIC void nlib_freeaddrinfo(nlib_addrinfo* res);
206 
208 errno_t nlib_getsockopt(nlib_sock sockfd, int level, int optname, void* __restrict optval,
209  uint32_t* __restrict optlen);
211 errno_t nlib_setsockopt(nlib_sock sockfd, int level, int optname, const void* optval,
212  uint32_t optlen);
213 
214 typedef fd_set nlib_fd_set;
216 errno_t nlib_select(size_t* __restrict n, int nfds, nlib_fd_set* __restrict readfds,
217  nlib_fd_set* __restrict writefds,
218  nlib_fd_set* __restrict exceptfds, nlib_duration timeout);
219 
220 #define NLIB_FD_CLR FD_CLR
221 #define NLIB_FD_ISSET FD_ISSET
222 #ifdef _MSC_VER
223 #define NLIB_FD_SET(fd, set) FD_SET((SOCKET)fd, set)
224 #else
225 #define NLIB_FD_SET FD_SET
226 #endif
227 #define NLIB_FD_ZERO FD_ZERO
228 
229 #if defined(_MSC_VER)
230 struct nlib_pollfd_ {
231  nlib_sock fd;
232  int16_t events;
233  int16_t revents;
234 };
235 typedef struct nlib_pollfd_ nlib_pollfd;
236 #else
237 typedef struct pollfd nlib_pollfd;
238 #endif
240 errno_t nlib_poll(size_t* __restrict n, nlib_pollfd* __restrict fds, uint32_t nfds,
241  nlib_duration timeout);
242 
243 #ifdef _MSC_VER
244 #define NLIB_MSG_DONTWAIT (0x4000)
245 #else
246 #define NLIB_MSG_DONTWAIT MSG_DONTWAIT
247 #endif
248 
249 #ifdef MSG_FASTOPEN
250 #define NLIB_MSG_FASTOPEN MSG_FASTOPEN
251 #else
252 #define NLIB_MSG_FASTOPEN 0x20000000
253 #endif
254 
255 // NLIB_VIS_PUBLIC errno_t nlib_sockatmark(int* is_marked, nlib_sock sockfd);
257 errno_t nlib_getsockname(nlib_sock sockfd, nlib_sockaddr* __restrict addr,
258  uint32_t* __restrict addrlen);
260 errno_t nlib_getpeername(nlib_sock sockfd, nlib_sockaddr* __restrict addr,
261  uint32_t* __restrict addrlen);
262 NLIB_VIS_PUBLIC errno_t nlib_setnonblocking(nlib_sock sockfd, int nonblock);
263 
264 #endif
265 
266 #endif // INCLUDE_NN_NLIB_PLATFORM_SOCKET_H_
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
int64_t nlib_duration
The type expressing the time in increments of 100 ns. A 64-bit signed integer.
Definition: Platform.h:452
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37