diff --git a/Makefile.common b/Makefile.common index 34dd5ee7b6..2dd16838ca 100644 --- a/Makefile.common +++ b/Makefile.common @@ -952,6 +952,7 @@ ifeq ($(HAVE_NETWORKING), 1) DEFINES += -DHAVE_NETWORKING OBJ += libretro-common/net/net_compat.o \ libretro-common/net/net_http.o \ + libretro-common/net/net_socket.o \ net_http_special.o \ tasks/task_http.o diff --git a/command.c b/command.c index 880ebf4407..81e8443521 100644 --- a/command.c +++ b/command.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "msg_hash.h" @@ -122,41 +123,25 @@ static const struct cmd_map map[] = { #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port) { - struct addrinfo hints = {0}; - char port_buf[16] = {0}; + int *file_desc = (int*)&handle->net_fd; struct addrinfo *res = NULL; int yes = 1; - if (!network_init()) - return false; - RARCH_LOG("Bringing up command interface on port %hu.\n", (unsigned short)port); -#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) - hints.ai_family = AF_INET; -#else - hints.ai_family = AF_UNSPEC; -#endif - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_PASSIVE; - - - snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); - if (getaddrinfo_retro(NULL, port_buf, &hints, &res) < 0) + if (!socket_init(res, file_desc, port, NULL)) goto error; - handle->net_fd = socket(res->ai_family, - res->ai_socktype, res->ai_protocol); - if (handle->net_fd < 0) + if (*file_desc < 0) goto error; - if (!socket_nonblock(handle->net_fd)) + if (!socket_nonblock(*file_desc)) goto error; - setsockopt(handle->net_fd, SOL_SOCKET, + setsockopt(*file_desc, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int)); - if (bind(handle->net_fd, res->ai_addr, res->ai_addrlen) < 0) + if (bind(*file_desc, res->ai_addr, res->ai_addrlen) < 0) { RARCH_ERR("Failed to bind socket.\n"); goto error; diff --git a/griffin/griffin.c b/griffin/griffin.c index 4750ff58ed..372ed7e00b 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -789,6 +789,7 @@ NETPLAY #include "../netplay/netplay_common.c" #include "../netplay/netplay.c" #include "../libretro-common/net/net_compat.c" +#include "../libretro-common/net/net_socket.c" #include "../libretro-common/net/net_http.c" #ifndef HAVE_SOCKET_LEGACY #include "../libretro-common/net/net_ifinfo.c" diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h new file mode 100644 index 0000000000..871625d633 --- /dev/null +++ b/libretro-common/include/net/net_socket.h @@ -0,0 +1,38 @@ +/* Copyright (C) 2010-2016 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (net_socket.h). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef _LIBRETRO_SDK_NET_SOCKET_H +#define _LIBRETRO_SDK_NET_SOCKET_H + +#include +#include +#include + +#include + +RETRO_BEGIN_DECLS + +bool socket_init(void *address, int *fd, uint16_t port, const char *server); + +RETRO_END_DECLS + +#endif diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c new file mode 100644 index 0000000000..760aff4b99 --- /dev/null +++ b/libretro-common/net/net_socket.c @@ -0,0 +1,62 @@ +/* Copyright (C) 2010-2016 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (net_socket.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +bool socket_init(void *address, int *fd, uint16_t port, const char *server) +{ + char port_buf[16] = {0}; + struct addrinfo hints = {0}; + struct addrinfo *addr = (struct addrinfo*)address; + + if (!fd) + goto error; + + if (!network_init()) + goto error; + +#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) + hints.ai_family = AF_INET; +#else + hints.ai_family = AF_UNSPEC; +#endif + hints.ai_socktype = SOCK_DGRAM; + if (!server) + hints.ai_flags = AI_PASSIVE; + + snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); + + if (getaddrinfo_retro(server, port_buf, &hints, &addr) < 0) + goto error; + + if (!addr) + goto error; + + *fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + + return true; + +error: + return false; +} diff --git a/netplay/netplay.c b/netplay/netplay.c index d55bf5d2e8..785baf205a 100644 --- a/netplay/netplay.c +++ b/netplay/netplay.c @@ -23,6 +23,7 @@ #include #include +#include #include #include "netplay_private.h" @@ -758,44 +759,6 @@ static bool init_tcp_socket(netplay_t *netplay, const char *server, return ret; } -static int socket_init(void *address, int *fd, uint16_t port, const char *server) -{ - char port_buf[16] = {0}; - struct addrinfo hints = {0}; - struct addrinfo *addr = (struct addrinfo*)address; - - if (!fd) - goto error; - - if (!network_init()) - goto error; - -#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) - hints.ai_family = AF_INET; -#else - hints.ai_family = AF_UNSPEC; -#endif - hints.ai_socktype = SOCK_DGRAM; - if (!server) - hints.ai_flags = AI_PASSIVE; - - snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); - - if (getaddrinfo_retro(server, port_buf, &hints, &addr) < 0) - goto error; - - if (!addr) - goto error; - - *fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - - return true; - -error: - RARCH_ERR("Failed to initialize socket.\n"); - return false; -} - static bool init_udp_socket(netplay_t *netplay, const char *server, uint16_t port) {