1 /* Copyright (C) 2010-2022 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (net_compat.h).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef _LIBRETRO_SDK_NET_COMPAT_H
24 #define _LIBRETRO_SDK_NET_COMPAT_H
28 #include <retro_inline.h>
37 #include <retro_common_api.h>
39 #if defined(_WIN32) && !defined(_XBOX)
40 #define WIN32_LEAN_AND_MEAN
46 #if _MSC_VER && _MSC_VER <= 1600
47 /* If we are using MSVC2010 or lower, disable WSAPoll support
48 * to ensure Windows XP and earlier backwards compatibility */
50 #ifndef WIN32_SUPPORTS_POLL
51 #define WIN32_SUPPORTS_POLL 1
55 #if defined(WIN32_SUPPORTS_POLL) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
56 #define NETWORK_HAVE_POLL 1
66 #define SO_KEEPALIVE 0 /* verify if correct */
69 #define socklen_t unsigned int
72 #include <psp2/net/net.h>
73 #include <psp2/net/netctl.h>
75 #define NETWORK_HAVE_POLL 1
78 #define AF_INET SCE_NET_AF_INET
80 #define SOCK_STREAM SCE_NET_SOCK_STREAM
81 #define SOCK_DGRAM SCE_NET_SOCK_DGRAM
83 #define INADDR_ANY SCE_NET_INADDR_ANY
84 #define INADDR_NONE 0xFFFFFFFF
86 #define SOL_SOCKET SCE_NET_SOL_SOCKET
87 #define SO_REUSEADDR SCE_NET_SO_REUSEADDR
88 #define SO_KEEPALIVE SCE_NET_SO_KEEPALIVE
89 #define SO_BROADCAST SCE_NET_SO_BROADCAST
90 #define SO_SNDBUF SCE_NET_SO_SNDBUF
91 #define SO_RCVBUF SCE_NET_SO_RCVBUF
92 #define SO_SNDTIMEO SCE_NET_SO_SNDTIMEO
93 #define SO_RCVTIMEO SCE_NET_SO_RCVTIMEO
94 #define SO_ERROR SCE_NET_SO_ERROR
95 #define SO_NBIO SCE_NET_SO_NBIO
97 #define IPPROTO_IP SCE_NET_IPPROTO_IP
98 #define IP_MULTICAST_TTL SCE_NET_IP_MULTICAST_TTL
100 #define IPPROTO_TCP SCE_NET_IPPROTO_TCP
101 #define TCP_NODELAY SCE_NET_TCP_NODELAY
103 #define IPPROTO_UDP SCE_NET_IPPROTO_UDP
105 #define MSG_DONTWAIT SCE_NET_MSG_DONTWAIT
107 #define POLLIN SCE_NET_EPOLLIN
108 #define POLLOUT SCE_NET_EPOLLOUT
109 #define POLLERR SCE_NET_EPOLLERR
110 #define POLLHUP SCE_NET_EPOLLHUP
113 #define sockaddr SceNetSockaddr
114 #define sockaddr_in SceNetSockaddrIn
115 #define in_addr SceNetInAddr
116 #define socklen_t unsigned int
118 #define socket(a,b,c) sceNetSocket("unknown",a,b,c)
119 #define getsockname sceNetGetsockname
120 #define getsockopt sceNetGetsockopt
121 #define setsockopt sceNetSetsockopt
122 #define bind sceNetBind
123 #define listen sceNetListen
124 #define accept sceNetAccept
125 #define connect sceNetConnect
126 #define send sceNetSend
127 #define sendto sceNetSendto
128 #define recv sceNetRecv
129 #define recvfrom sceNetRecvfrom
130 #define htonl sceNetHtonl
131 #define ntohl sceNetNtohl
132 #define htons sceNetHtons
133 #define ntohs sceNetNtohs
134 #define inet_ntop sceNetInetNtop
135 #define inet_pton sceNetInetPton
140 #define NETWORK_HAVE_POLL 1
142 #define pollfd pollsd
144 #define socket(a,b,c) net_socket(a,b,c)
145 #define getsockopt(a,b,c,d,e) net_getsockopt(a,b,c,d,e)
146 #define setsockopt(a,b,c,d,e) net_setsockopt(a,b,c,d,e)
147 #define bind(a,b,c) net_bind(a,b,c)
148 #define listen(a,b) net_listen(a,b)
149 #define accept(a,b,c) net_accept(a,b,c)
150 #define connect(a,b,c) net_connect(a,b,c)
151 #define send(a,b,c,d) net_send(a,b,c,d)
152 #define sendto(a,b,c,d,e,f) net_sendto(a,b,c,d,e,f)
153 #define recv(a,b,c,d) net_recv(a,b,c,d)
154 #define recvfrom(a,b,c,d,e,f) net_recvfrom(a,b,c,d,e,f)
155 #define select(a,b,c,d,e) net_select(a,b,c,d,e)
156 #define gethostbyname(a) net_gethostbyname(a)
159 #include <sys/types.h>
160 #include <sys/socket.h>
161 #include <sys/select.h>
163 #include <netinet/in.h>
165 #include <netinet/tcp.h>
168 #include <arpa/inet.h>
172 #if !defined(__PSL1GHT__) && defined(__PS3__)
173 #include <netex/libnetctl.h>
174 #include <netex/errno.h>
179 #if defined(__PSL1GHT__)
180 #include <net/poll.h>
182 #define NETWORK_HAVE_POLL 1
185 #define WIIU_RCVBUF 0x40000
186 #define WIIU_SNDBUF 0x40000
188 #elif !defined(__PS3__)
191 #define NETWORK_HAVE_POLL 1
197 #define MSG_NOSIGNAL 0
200 #if defined(AF_INET6) && !defined(HAVE_SOCKET_LEGACY) && !defined(_3DS)
204 #ifdef NETWORK_HAVE_POLL
206 #define NET_POLL_FD(sockfd, sockfds) (sockfds)->socket = (sockfd)
208 #define NET_POLL_FD(sockfd, sockfds) (sockfds)->fd = (sockfd)
210 #define NET_POLL_EVENT(sockev, sockfds) (sockfds)->events |= (sockev)
211 #define NET_POLL_HAS_EVENT(sockev, sockfds) ((sockfds)->revents & (sockev))
216 /* Compatibility layer for legacy or incomplete BSD socket implementations.
217 * Only for IPv4. Mostly useful for the consoles which do not support
218 * anything reasonably modern on the socket API side of things. */
219 #ifdef HAVE_SOCKET_LEGACY
220 #define sockaddr_storage sockaddr_in
228 #ifdef AI_NUMERICHOST
229 #undef AI_NUMERICHOST
231 #ifdef AI_NUMERICSERV
232 #undef AI_NUMERICSERV
235 #ifdef NI_NUMERICHOST
236 #undef NI_NUMERICHOST
238 #ifdef NI_NUMERICSERV
239 #undef NI_NUMERICSERV
252 #define AI_CANONNAME 2
253 #define AI_NUMERICHOST 4
254 #define AI_NUMERICSERV 8
256 #define NI_NUMERICHOST 1
257 #define NI_NUMERICSERV 2
259 #define NI_NAMEREQD 8
269 socklen_t ai_addrlen;
270 struct sockaddr *ai_addr;
272 struct addrinfo *ai_next;
276 /* gai_strerror() not used, so we skip that. */
279 /* Ensure that getaddrinfo and getnameinfo flags are always defined. */
284 #define AI_CANONNAME 0
286 #ifndef AI_NUMERICHOST
287 #define AI_NUMERICHOST 0
289 #ifndef AI_NUMERICSERV
290 #define AI_NUMERICSERV 0
293 #ifndef NI_NUMERICHOST
294 #define NI_NUMERICHOST 0
296 #ifndef NI_NUMERICSERV
297 #define NI_NUMERICSERV 0
303 #define NI_NAMEREQD 0
329 unsigned __pad; /* Align to 64-bits boundary */
345 static INLINE bool isagain(int val)
348 return (val == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK);
349 #elif !defined(__PSL1GHT__) && defined(__PS3__)
350 return (sys_net_errno == SYS_NET_EAGAIN) || (sys_net_errno == SYS_NET_EWOULDBLOCK);
352 return (val == SCE_NET_ERROR_EAGAIN) || (val == SCE_NET_ERROR_EWOULDBLOCK);
354 return (val == -1) && (socketlasterr() == SO_SUCCESS || socketlasterr() == SO_EWOULDBLOCK);
356 return (val < 0) && (errno == EAGAIN || errno == EWOULDBLOCK);
360 static INLINE bool isinprogress(int val)
363 return (val == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK);
364 #elif !defined(__PSL1GHT__) && defined(__PS3__)
365 return (sys_net_errno == SYS_NET_EINPROGRESS);
367 return (val == SCE_NET_ERROR_EINPROGRESS);
369 return (val == -1) && (socketlasterr() == SO_EINPROGRESS);
371 return (val < 0) && (errno == EINPROGRESS);
375 #if defined(_WIN32) && !defined(_XBOX)
376 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
377 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
378 int inet_pton(int af, const char *src, void *dst);
382 struct hostent *gethostbyname(const char *name);
385 char *inet_ntoa(struct in_addr in);
386 int inet_aton(const char *cp, struct in_addr *inp);
387 uint32_t inet_addr(const char *cp);
389 struct hostent *gethostbyname(const char *name);
392 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
393 int inet_pton(int af, const char *src, void *dst);
397 int getaddrinfo_retro(const char *node, const char *service,
398 struct addrinfo *hints, struct addrinfo **res);
400 void freeaddrinfo_retro(struct addrinfo *res);
402 int getnameinfo_retro(const struct sockaddr *addr, socklen_t addrlen,
403 char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags);
405 bool addr_6to4(struct sockaddr_storage *addr);
407 bool ipv4_is_lan_address(const struct sockaddr_in *addr);
408 bool ipv4_is_cgnat_address(const struct sockaddr_in *addr);
413 * Platform specific socket library initialization.
415 * @return true if successful, otherwise false.
417 bool network_init(void);