1 /* Copyright (C) 2010-2022 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (net_socket.c).
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.
28 #include <compat/msvc.h>
31 #include <features/features_cpu.h>
33 #include <net/net_socket.h>
35 int socket_init(void **address, uint16_t port, const char *server,
36 enum socket_type type, int family)
39 struct addrinfo hints = {0};
40 struct addrinfo **addrinfo = (struct addrinfo**)address;
41 struct addrinfo *addr = NULL;
44 #if defined(HAVE_SOCKET_LEGACY) || defined(WIIU)
50 hints.ai_family = family;
54 case SOCKET_TYPE_DATAGRAM:
55 hints.ai_socktype = SOCK_DGRAM;
57 case SOCKET_TYPE_STREAM:
58 hints.ai_socktype = SOCK_STREAM;
65 hints.ai_flags = AI_PASSIVE;
70 snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
71 hints.ai_flags |= AI_NUMERICSERV;
73 if (getaddrinfo_retro(server, port_buf, &hints, addrinfo))
80 return socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
83 int socket_next(void **address)
85 struct addrinfo **addrinfo = (struct addrinfo**)address;
86 struct addrinfo *addr = *addrinfo;
88 if ((*addrinfo = addr = addr->ai_next))
89 return socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
94 ssize_t socket_receive_all_nonblocking(int fd, bool *error,
95 void *data_, size_t size)
97 ssize_t ret = recv(fd, (char*)data_, size, 0);
102 if (ret < 0 && isagain((int)ret))
109 bool socket_receive_all_blocking(int fd, void *data_, size_t size)
111 const uint8_t *data = (const uint8_t*)data_;
115 ssize_t ret = recv(fd, (char*)data, size, 0);
122 if (!isagain((int)ret))
135 bool socket_receive_all_blocking_with_timeout(int fd,
136 void *data_, size_t size,
139 const uint8_t *data = (const uint8_t*)data_;
140 retro_time_t deadline = cpu_features_get_time_usec();
143 deadline += (retro_time_t)timeout * 1000;
149 ssize_t ret = recv(fd, (char*)data, size, 0);
159 if (!isagain((int)ret))
162 _timeout = (int)((deadline - cpu_features_get_time_usec()) / 1000);
166 if (!socket_wait(fd, &ready, NULL, _timeout) || !ready)
179 bool socket_set_block(int fd, bool block)
184 return !ioctlsocket(fd, FIONBIO, &i);
185 #elif defined(__PS3__) || defined(VITA) || defined(WIIU)
188 return !setsockopt(fd, SOL_SOCKET, SO_NBIO, &i, sizeof(i));
192 return !net_ioctl(fd, FIONBIO, &i);
194 int flags = fcntl(fd, F_GETFL);
197 flags &= ~O_NONBLOCK;
201 return !fcntl(fd, F_SETFL, flags);
205 bool socket_nonblock(int fd)
207 return socket_set_block(fd, false);
210 int socket_close(int fd)
212 #if defined(_WIN32) && !defined(_XBOX360)
213 /* WinSock has headers from the stone age. */
214 return closesocket(fd);
215 #elif defined(__PS3__) || defined(WIIU)
216 return socketclose(fd);
218 return sceNetSocketClose(fd);
224 int socket_select(int nfds, fd_set *readfds, fd_set *writefds,
225 fd_set *errorfds, struct timeval *timeout)
228 return socketselect(nfds, readfds, writefds, errorfds, timeout);
231 fd_set rfds, wfds, efds;
233 SceNetEpollEvent *events = NULL;
238 if (nfds < 0 || nfds > 1024)
239 return SCE_NET_ERROR_EINVAL;
240 if (timeout && (timeout->tv_sec < 0 || timeout->tv_usec < 0))
241 return SCE_NET_ERROR_EINVAL;
243 epoll_fd = sceNetEpollCreate("socket_select", 0);
245 return SCE_NET_ERROR_ENOMEM;
251 for (i = 0; i < nfds; i++)
253 if (readfds && FD_ISSET(i, readfds))
255 else if (writefds && FD_ISSET(i, writefds))
257 else if (errorfds && FD_ISSET(i, errorfds))
261 #define ALLOC_EVENTS(count) \
262 events = (SceNetEpollEvent*)calloc((count), sizeof(*events)); \
265 ret = SCE_NET_ERROR_ENOMEM; \
271 ALLOC_EVENTS(event_count)
273 for (i = 0, j = 0; i < nfds && j < event_count; i++)
275 SceNetEpollEvent *event = &events[j];
277 if (readfds && FD_ISSET(i, readfds))
278 event->events |= SCE_NET_EPOLLIN;
279 if (writefds && FD_ISSET(i, writefds))
280 event->events |= SCE_NET_EPOLLOUT;
282 if (event->events || (errorfds && FD_ISSET(i, errorfds)))
286 ret = sceNetEpollControl(epoll_fd, SCE_NET_EPOLL_CTL_ADD,
292 case SCE_NET_ERROR_EBADF:
293 case SCE_NET_ERROR_ENOMEM:
296 ret = SCE_NET_ERROR_EBADF;
306 memset(events, 0, event_count * sizeof(*events));
308 /* Keep a copy of the original sets for lookup later. */
310 memcpy(&rfds, readfds, sizeof(rfds));
312 memcpy(&wfds, writefds, sizeof(wfds));
314 memcpy(&efds, errorfds, sizeof(efds));
318 /* Necessary to work with epoll wait. */
332 /* Vita's epoll takes a microsecond timeout parameter. */
334 timeout_us = (int)(timeout->tv_usec + (timeout->tv_sec * 1000000));
336 ret = sceNetEpollWait(epoll_fd, events, event_count, timeout_us);
340 #define EPOLL_FD_SET(op, in_set, out_set) \
341 if ((event->events & (op)) && FD_ISSET(event->data.fd, (in_set))) \
343 FD_SET(event->data.fd, (out_set)); \
347 for (i = 0, j = 0; i < ret; i++)
349 SceNetEpollEvent *event = &events[i];
352 if (event->data.fd < 0 || event->data.fd >= nfds)
355 EPOLL_FD_SET(SCE_NET_EPOLLIN, &rfds, readfds)
356 EPOLL_FD_SET(SCE_NET_EPOLLOUT, &wfds, writefds)
357 EPOLL_FD_SET(SCE_NET_EPOLLERR, &efds, errorfds)
366 sceNetEpollDestroy(epoll_fd);
370 return select(nfds, readfds, writefds, errorfds, timeout);
374 #ifdef NETWORK_HAVE_POLL
375 int socket_poll(struct pollfd *fds, unsigned nfds, int timeout)
378 return WSAPoll(fds, nfds, timeout);
382 SceNetEpollEvent *events = NULL;
383 int event_count = (int)nfds;
387 return SCE_NET_ERROR_EINVAL;
389 epoll_fd = sceNetEpollCreate("socket_poll", 0);
391 return SCE_NET_ERROR_ENOMEM;
393 #define ALLOC_EVENTS(count) \
394 events = (SceNetEpollEvent*)calloc((count), sizeof(*events)); \
397 ret = SCE_NET_ERROR_ENOMEM; \
403 ALLOC_EVENTS(event_count)
405 for (i = 0; i < event_count; i++)
407 struct pollfd *fd = &fds[i];
408 SceNetEpollEvent *event = &events[i];
415 event->events = fd->events;
416 event->data.fd = fd->fd;
418 ret = sceNetEpollControl(epoll_fd, SCE_NET_EPOLL_CTL_ADD,
424 memset(events, 0, event_count * sizeof(*events));
428 /* Necessary to work with epoll wait. */
435 /* Vita's epoll takes a microsecond timeout parameter. */
439 ret = sceNetEpollWait(epoll_fd, events, event_count, timeout);
443 for (i = 0, j = 0; i < ret; i++)
446 SceNetEpollEvent *event = &events[i];
449 if (event->data.fd < 0)
452 for (k = 0; k < nfds; k++)
454 struct pollfd *fd = &fds[k];
456 if (fd->fd == event->data.fd)
458 fd->revents = event->events;
469 sceNetEpollDestroy(epoll_fd);
474 int timeout_quotient;
475 int timeout_remainder;
478 #define TIMEOUT_DIVISOR 100
479 if (timeout <= TIMEOUT_DIVISOR)
480 return poll(fds, nfds, timeout);
482 timeout_quotient = timeout / TIMEOUT_DIVISOR;
483 for (i = 0; i < timeout_quotient; i++)
485 ret = poll(fds, nfds, TIMEOUT_DIVISOR);
487 /* Success or error. */
492 timeout_remainder = timeout % TIMEOUT_DIVISOR;
493 if (timeout_remainder)
494 ret = poll(fds, nfds, timeout_remainder);
497 #undef TIMEOUT_DIVISOR
500 return net_poll(fds, nfds, timeout);
502 return poll(fds, nfds, timeout);
507 bool socket_wait(int fd, bool *rd, bool *wr, int timeout)
509 #ifdef NETWORK_HAVE_POLL
510 struct pollfd fds = {0};
512 NET_POLL_FD(fd, &fds);
516 NET_POLL_EVENT(POLLIN, &fds);
521 NET_POLL_EVENT(POLLOUT, &fds);
525 if (socket_poll(&fds, 1, timeout) < 0)
528 if (rd && NET_POLL_HAS_EVENT(POLLIN, &fds))
530 if (wr && NET_POLL_HAS_EVENT(POLLOUT, &fds))
533 return !NET_POLL_HAS_EVENT((POLLERR | POLLNVAL), &fds);
535 fd_set rfd, wfd, efd;
536 struct timeval tv, *ptv = NULL;
556 tv.tv_sec = (unsigned)timeout / 1000;
557 tv.tv_usec = ((unsigned)timeout % 1000) * 1000;
561 if (socket_select(fd + 1, &rfd, &wfd, &efd, ptv) < 0)
564 if (rd && FD_ISSET(fd, &rfd))
566 if (wr && FD_ISSET(fd, &wfd))
569 return !FD_ISSET(fd, &efd);
573 bool socket_send_all_blocking(int fd, const void *data_, size_t size,
576 const uint8_t *data = (const uint8_t*)data_;
577 int flags = no_signal ? MSG_NOSIGNAL : 0;
581 ssize_t ret = send(fd, (const char*)data, size, flags);
588 if (!isagain((int)ret))
601 bool socket_send_all_blocking_with_timeout(int fd,
602 const void *data_, size_t size,
603 int timeout, bool no_signal)
605 const uint8_t *data = (const uint8_t*)data_;
606 int flags = no_signal ? MSG_NOSIGNAL : 0;
607 retro_time_t deadline = cpu_features_get_time_usec();
610 deadline += (retro_time_t)timeout * 1000;
616 ssize_t ret = send(fd, (const char*)data, size, flags);
626 if (!isagain((int)ret))
629 _timeout = (int)((deadline - cpu_features_get_time_usec()) / 1000);
633 if (!socket_wait(fd, NULL, &ready, _timeout) || !ready)
646 ssize_t socket_send_all_nonblocking(int fd, const void *data_, size_t size,
649 const uint8_t *data = (const uint8_t*)data_;
650 int flags = no_signal ? MSG_NOSIGNAL : 0;
654 ssize_t ret = send(fd, (const char*)data, size, flags);
661 if (isagain((int)ret))
673 return (ssize_t)((size_t)data - (size_t)data_);
676 bool socket_bind(int fd, void *data)
678 struct addrinfo *addr = (struct addrinfo*)data;
683 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
684 (char*)&on, sizeof(on));
687 return !bind(fd, addr->ai_addr, addr->ai_addrlen);
690 int socket_connect(int fd, void *data)
692 struct addrinfo *addr = (struct addrinfo*)data;
698 setsockopt(fd, SOL_SOCKET, SO_WINSCALE, &op, sizeof(op));
700 if (addr->ai_socktype == SOCK_STREAM)
702 int recvsz = WIIU_RCVBUF;
703 int sendsz = WIIU_SNDBUF;
705 setsockopt(fd, SOL_SOCKET, SO_TCPSACK, &op, sizeof(op));
706 setsockopt(fd, SOL_SOCKET, SO_RUSRBUF, &op, sizeof(op));
707 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &recvsz, sizeof(recvsz));
708 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendsz, sizeof(sendsz));
713 return connect(fd, addr->ai_addr, addr->ai_addrlen);
716 bool socket_connect_with_timeout(int fd, void *data, int timeout)
719 struct addrinfo *addr = (struct addrinfo*)data;
721 if (!socket_nonblock(fd))
728 setsockopt(fd, SOL_SOCKET, SO_WINSCALE, &op, sizeof(op));
730 if (addr->ai_socktype == SOCK_STREAM)
732 int recvsz = WIIU_RCVBUF;
733 int sendsz = WIIU_SNDBUF;
735 setsockopt(fd, SOL_SOCKET, SO_TCPSACK, &op, sizeof(op));
736 setsockopt(fd, SOL_SOCKET, SO_RUSRBUF, &op, sizeof(op));
737 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &recvsz, sizeof(recvsz));
738 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendsz, sizeof(sendsz));
743 res = connect(fd, addr->ai_addr, addr->ai_addrlen);
748 if (!isinprogress(res) && !isagain(res))
754 if (!socket_wait(fd, NULL, &ready, timeout) || !ready)
759 /* libogc does not have getsockopt implemented */
760 res = connect(fd, addr->ai_addr, addr->ai_addrlen);
761 if (res < 0 && -res != EISCONN)
764 /* libctru getsockopt does not return expected value */
765 if ((connect(fd, addr->ai_addr, addr->ai_addrlen) < 0) && errno != EISCONN)
768 /* On WiiU, getsockopt() returns -1 and sets lastsocketerr() (Wii's
769 * equivalent to errno) to 16. */
770 if ((connect(fd, addr->ai_addr, addr->ai_addrlen) == -1)
771 && socketlasterr() != SO_EISCONN)
776 socklen_t errsz = sizeof(error);
778 getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&error, &errsz);
789 enum socket_domain domain_type,
790 enum socket_type socket_type,
791 enum socket_protocol protocol_type)
799 case SOCKET_DOMAIN_INET:
808 case SOCKET_TYPE_DATAGRAM:
811 case SOCKET_TYPE_STREAM:
814 case SOCKET_TYPE_SEQPACKET:
816 /* TODO/FIXME - implement */
820 switch (protocol_type)
822 case SOCKET_PROTOCOL_TCP:
823 protocol = IPPROTO_TCP;
825 case SOCKET_PROTOCOL_UDP:
826 protocol = IPPROTO_UDP;
828 case SOCKET_PROTOCOL_NONE:
834 return sceNetSocket(name, domain, type, protocol);
836 return socket(domain, type, protocol);
840 void socket_set_target(void *data, socket_target_t *in_addr)
842 struct sockaddr_in *out_target = (struct sockaddr_in*)data;
845 out_target->sin_len = 8;
847 switch (in_addr->domain)
849 case SOCKET_DOMAIN_INET:
850 out_target->sin_family = AF_INET;
853 out_target->sin_family = 0;
856 out_target->sin_port = htons(in_addr->port);
857 inet_pton(AF_INET, in_addr->server, &out_target->sin_addr);