16 #ifndef INCLUDE_NN_NLIB_PLATFORM_SOCKET_H_ 17 #define INCLUDE_NN_NLIB_PLATFORM_SOCKET_H_ 19 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_ 20 #error Do not include this file directly 23 #if defined(_MSC_VER) || defined(NLIB_UNIX) 24 #define NLIB_SOCKET_ENABLED 28 typedef int nlib_sock;
29 #define NLIB_SOCKET_INVALID (nlib_sock)(-1) // NOLINT 32 #define NLIB_SOCK_NONBLOCK 0x8000 33 #elif defined(SOCK_NONBLOCK) 34 #define NLIB_SOCK_NONBLOCK SOCK_NONBLOCK 36 #define NLIB_SOCK_NONBLOCK O_NONBLOCK 39 #if !defined(TCP_FASTOPEN) && defined(_MSC_VER) 41 # define TCP_FASTOPEN 15 46 typedef struct sockaddr nlib_sockaddr;
47 typedef struct sockaddr_in nlib_sockaddr_in;
48 typedef struct sockaddr_in6 nlib_sockaddr_in6;
53 nlib_fd_iovec* msg_iov;
56 size_t msg_controllen;
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) 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) 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) 85 typedef struct cmsghdr nlib_cmsghdr;
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,
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,
105 errno_t nlib_sendmsg(
size_t* __restrict size, nlib_sock sockfd,
const nlib_msghdr* msg,
int flags);
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);
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);
120 errno_t nlib_recv(
size_t* __restrict size, nlib_sock sockfd,
void* __restrict buf,
size_t len,
122 return nlib_recvfrom(size, sockfd, buf, len, flags, NULL, NULL);
128 # define SHUT_RD SD_RECEIVE 129 # define SHUT_WR SD_SEND 130 # define SHUT_RDWR SD_BOTH 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; }
141 static NLIB_C_INLINE uint32_t nlib_htonl(uint32_t hostlong) {
143 return _byteswap_ulong(hostlong);
145 return __builtin_bswap32(hostlong);
148 static NLIB_C_INLINE uint16_t nlib_htons(uint16_t hostshort) {
150 return _byteswap_ushort(hostshort);
152 return ((hostshort & 0xFF) << 8) | ((hostshort >> 8) & 0xFF);
155 static NLIB_C_INLINE uint32_t nlib_ntohl(uint32_t netlong) {
157 return _byteswap_ulong(netlong);
159 return __builtin_bswap32(netlong);
162 static NLIB_C_INLINE uint16_t nlib_ntohs(uint16_t netshort) {
164 return _byteswap_ushort(netshort);
166 return ((netshort & 0xFF) << 8) | ((netshort >> 8) & 0xFF);
171 typedef struct in_addr nlib_in_addr;
172 typedef struct in6_addr nlib_in6_addr;
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);
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,
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,
220 #define NLIB_FD_CLR FD_CLR 221 #define NLIB_FD_ISSET FD_ISSET 223 #define NLIB_FD_SET(fd, set) FD_SET((SOCKET)fd, set) 225 #define NLIB_FD_SET FD_SET 227 #define NLIB_FD_ZERO FD_ZERO 229 #if defined(_MSC_VER) 230 struct nlib_pollfd_ {
235 typedef struct nlib_pollfd_ nlib_pollfd;
237 typedef struct pollfd nlib_pollfd;
240 errno_t nlib_poll(
size_t* __restrict n, nlib_pollfd* __restrict fds, uint32_t nfds,
244 #define NLIB_MSG_DONTWAIT (0x4000) 246 #define NLIB_MSG_DONTWAIT MSG_DONTWAIT 250 #define NLIB_MSG_FASTOPEN MSG_FASTOPEN 252 #define NLIB_MSG_FASTOPEN 0x20000000 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);
266 #endif // INCLUDE_NN_NLIB_PLATFORM_SOCKET_H_