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)
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) ? (LPWSACMSGHDR)(msgh)->msg_control \
62  : (LPWSACMSGHDR)NULL)
63 #define NLIB_CMSG_NXTHDR(msg, cmsg) \
64  (((cmsg) == NULL) \
65  ? NLIB_CMSG_FIRSTHDR(msg) \
66  : ((((PUCHAR)(cmsg) + WSA_CMSGHDR_ALIGN((cmsg)->cmsg_len) + sizeof(WSACMSGHDR)) > \
67  (PUCHAR)((msg)->msg_control) + (msg)->msg_controllen) \
68  ? (LPWSACMSGHDR)NULL \
69  : (LPWSACMSGHDR)((PUCHAR)(cmsg) + WSA_CMSGHDR_ALIGN((cmsg)->cmsg_len))))
70 #define NLIB_CMSG_SPACE(length) WSA_CMSG_SPACE(length)
71 #define NLIB_CMSG_LEN(length) WSA_CMSG_LEN(length)
72 #define NLIB_CMSG_DATA(cmsg) WSA_CMSG_DATA(cmsg)
73 #else
74 typedef struct msghdr nlib_msghdr;
75 #define NLIB_CMSG_FIRSTHDR(msgh) CMSG_FIRSTHDR(msgh)
76 #define NLIB_CMSG_NXTHDR(msgh, cmsg) CMSG_NXTHDR(msgh, cmsg)
77 // #define NLIB_CMSG_ALIGN(length) CMSG_ALIGN(length)
78 #define NLIB_CMSG_SPACE(length) CMSG_SPACE(length)
79 #define NLIB_CMSG_LEN(length) CMSG_LEN(length)
80 #define NLIB_CMSG_DATA(cmsg) CMSG_DATA(cmsg)
81 #endif
82 typedef struct cmsghdr nlib_cmsghdr;
83 NLIB_VIS_PUBLIC errno_t nlib_bind(nlib_sock sockfd, const nlib_sockaddr* addr, uint32_t namelen);
84 NLIB_VIS_PUBLIC errno_t nlib_listen(nlib_sock sockfd, int backlog);
86 errno_t nlib_accept(nlib_sock* __restrict s, nlib_sock sockfd, nlib_sockaddr* __restrict addr,
87  uint32_t* __restrict addrlen, int flags);
89 errno_t nlib_accept_for(nlib_sock* __restrict s, nlib_sock sockfd, nlib_sockaddr* __restrict addr,
90  uint32_t* __restrict addrlen, int flags, nlib_duration timeout);
92 errno_t nlib_connect(nlib_sock sockfd, const nlib_sockaddr* addr, uint32_t addrlen);
94 errno_t nlib_connect_for(nlib_sock sockfd, const nlib_sockaddr* addr, uint32_t addrlen, int flags,
95  nlib_duration timeout);
96 
98 errno_t
99 nlib_sendto(size_t* __restrict size, nlib_sock sockfd, const void* __restrict buf, size_t len,
100  int flags, const nlib_sockaddr* __restrict dest_addr, uint32_t addrlen);
102 errno_t nlib_sendmsg(size_t* __restrict size, nlib_sock sockfd, const nlib_msghdr* msg, int flags);
103 static NLIB_C_INLINE errno_t nlib_send(size_t* __restrict size, nlib_sock sockfd,
104  const void* __restrict buf, size_t len, int flags) {
105  return nlib_sendto(size, sockfd, buf, len, flags, NULL, 0);
106 }
107 
109 errno_t nlib_recvfrom(size_t* __restrict size, nlib_sock sockfd, void* __restrict buf, size_t len,
110  int flags, nlib_sockaddr* __restrict dest_addr, uint32_t* __restrict addrlen);
112 errno_t nlib_recvmsg(size_t* __restrict size, nlib_sock sockfd, nlib_msghdr* msg, int flags);
113 static NLIB_C_INLINE errno_t nlib_recv(size_t* __restrict size, nlib_sock sockfd,
114  void* __restrict buf, size_t len, int flags) {
115  return nlib_recvfrom(size, sockfd, buf, len, flags, NULL, NULL);
116 }
117 
118 NLIB_VIS_PUBLIC errno_t nlib_closesocket(nlib_sock sockfd);
119 
120 #ifdef _MSC_VER
121 #define SHUT_RD SD_RECEIVE
122 #define SHUT_WR SD_SEND
123 #define SHUT_RDWR SD_BOTH
124 #endif
125 
126 NLIB_VIS_PUBLIC errno_t nlib_shutdownsocket(nlib_sock sockfd, int how);
127 
128 #ifndef NLIB_LITTLE_ENDIAN
129 static NLIB_C_INLINE uint32_t nlib_htonl(uint32_t hostlong) {
130  return hostlong;
131 }
132 static NLIB_C_INLINE uint16_t nlib_htons(uint16_t hostshort) {
133  return hostshort;
134 }
135 static NLIB_C_INLINE uint32_t nlib_ntohl(uint32_t netlong) {
136  return netlong;
137 }
138 static NLIB_C_INLINE uint16_t nlib_ntohs(uint16_t netshort) {
139  return netshort;
140 }
141 #else
142 static NLIB_C_INLINE uint32_t nlib_htonl(uint32_t hostlong) {
143 #ifdef _MSC_VER
144  return _byteswap_ulong(hostlong);
145 #else
146  return __builtin_bswap32(hostlong);
147 #endif
148 }
149 static NLIB_C_INLINE uint16_t nlib_htons(uint16_t hostshort) {
150 #ifdef _MSC_VER
151  return _byteswap_ushort(hostshort);
152 #else
153  return ((hostshort & 0xFF) << 8) | ((hostshort >> 8) & 0xFF);
154 #endif
155 }
156 static NLIB_C_INLINE uint32_t nlib_ntohl(uint32_t netlong) {
157 #ifdef _MSC_VER
158  return _byteswap_ulong(netlong);
159 #else
160  return __builtin_bswap32(netlong);
161 #endif
162 }
163 static NLIB_C_INLINE uint16_t nlib_ntohs(uint16_t netshort) {
164 #ifdef _MSC_VER
165  return _byteswap_ushort(netshort);
166 #else
167  return ((netshort & 0xFF) << 8) | ((netshort >> 8) & 0xFF);
168 #endif
169 }
170 #endif
171 
172 typedef struct in_addr nlib_in_addr;
173 typedef struct in6_addr nlib_in6_addr;
174 NLIB_VIS_PUBLIC errno_t nlib_inet_pton(int af, const char* __restrict src, void* __restrict dst);
175 NLIB_VIS_PUBLIC errno_t nlib_inet_ntop(int af, const void* __restrict src, char* __restrict dst,
176  uint32_t dst_size);
177 
178 /*
179 # define EAI_ADDRFAMILY 1
180 # define EAI_AGAIN 2
181 # define EAI_BADFLAGS 3
182 # define EAI_FAIL 4
183 # define EAI_FAMILY 5
184 # define EAI_MEMORY 6
185 # define EAI_NODATA 7
186 # define EAI_NONAME 8
187 # define EAI_SERVICE 9
188 # define EAI_SOCKTYPE 10
189 # define EAI_SYSTEM 11
190 # define EAI_BADHINTS 12
191 # define EAI_PROTOCOL 13
192 # define EAI_OVERFLOW 14
193 */
194 
195 // for EAI_ADDRFAMILY, EAI_AGAIN, ... etc.
196 typedef int eai_error_t;
197 typedef struct addrinfo nlib_addrinfo;
199 eai_error_t nlib_getaddrinfo(const char* __restrict node, const char* __restrict service,
200  const nlib_addrinfo* __restrict hints, nlib_addrinfo** __restrict res);
202 eai_error_t
203 nlib_getnameinfo(const nlib_sockaddr* __restrict sa, uint32_t salen, char* __restrict host,
204  uint32_t hostlen, 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
212 nlib_setsockopt(nlib_sock sockfd, int level, int optname, const void* optval, 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, nlib_fd_set* __restrict exceptfds,
218  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
241 nlib_poll(size_t* __restrict n, nlib_pollfd* __restrict fds, uint32_t nfds, 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
258 nlib_getsockname(nlib_sock sockfd, nlib_sockaddr* __restrict addr, uint32_t* __restrict addrlen);
260 errno_t
261 nlib_getpeername(nlib_sock sockfd, nlib_sockaddr* __restrict addr, 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
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
int64_t nlib_duration
100ns刻みで時間を表現する型です。64bit符号付き整数です。
Definition: Platform.h:454
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37