From c854645ba9dc13bb1bd41b68992dd18c6dbb3b91 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 23 Jan 2015 06:29:36 +0100 Subject: [PATCH] Create netplay_compat.c and put wrappers around getaddrinfo/freeaddrinfo --- Makefile.common | 9 ++--- griffin/griffin.c | 1 + net_http.c | 6 ++-- netplay.c | 68 +++---------------------------------- netplay_compat.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ netplay_compat.h | 6 ++++ 6 files changed, 105 insertions(+), 70 deletions(-) create mode 100644 netplay_compat.c diff --git a/Makefile.common b/Makefile.common index 4de2aa9497..837df2fdd0 100644 --- a/Makefile.common +++ b/Makefile.common @@ -627,10 +627,11 @@ endif ifeq ($(HAVE_NETPLAY), 1) DEFINES += -DHAVE_NETPLAY -DHAVE_NETWORK_CMD - OBJ += netplay.o - OBJ += http_lib.o \ - http_intf.o \ - net_http.o + OBJ += netplay.o \ + netplay_compat.o \ + http_lib.o \ + http_intf.o \ + net_http.o ifneq ($(findstring Win32,$(OS)),) LIBS += -lws2_32 endif diff --git a/griffin/griffin.c b/griffin/griffin.c index 26f02b4965..2facad919b 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -661,6 +661,7 @@ NETPLAY ============================================================ */ #ifdef HAVE_NETPLAY #include "../netplay.c" +#include "../netplay_compat.c" #include "../http_lib.c" #include "../http_intf.c" #include "../net_http.c" diff --git a/net_http.c b/net_http.c index 470ae7e293..3363023828 100644 --- a/net_http.c +++ b/net_http.c @@ -91,7 +91,7 @@ static int net_http_new_socket(const char * domain, int port) hints.ai_socktype=SOCK_STREAM; hints.ai_flags=0; - getaddrinfo(domain, portstr, &hints, &addr); + getaddrinfo_rarch(domain, portstr, &hints, &addr); if (!addr) return -1; @@ -105,12 +105,12 @@ static int net_http_new_socket(const char * domain, int port) if (connect(fd, addr->ai_addr, addr->ai_addrlen) != 0) { - freeaddrinfo(addr); + freeaddrinfo_rarch(addr); close(fd); return -1; } - freeaddrinfo(addr); + freeaddrinfo_rarch(addr); #ifndef _WIN32 /* Linux claims to not know that select() should only * give sockets where read() is nonblocking */ diff --git a/netplay.c b/netplay.c index e5b38ec62d..1a3790976e 100644 --- a/netplay.c +++ b/netplay.c @@ -715,7 +715,7 @@ static bool init_tcp_socket(netplay_t *netplay, const char *server, bool ret = false; char port_buf[16]; snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); - if (getaddrinfo(server, port_buf, &hints, &res) < 0) + if (getaddrinfo_rarch(server, port_buf, &hints, &res) < 0) return false; if (!res) @@ -740,7 +740,7 @@ static bool init_tcp_socket(netplay_t *netplay, const char *server, } if (res) - freeaddrinfo(res); + freeaddrinfo_rarch(res); if (!ret) RARCH_ERR("Failed to set up netplay sockets.\n"); @@ -764,7 +764,7 @@ static bool init_udp_socket(netplay_t *netplay, const char *server, char port_buf[16]; snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); - if (getaddrinfo(server, port_buf, &hints, &netplay->addr) < 0) + if (getaddrinfo_rarch(server, port_buf, &hints, &netplay->addr) < 0) return false; if (!netplay->addr) @@ -794,7 +794,7 @@ static bool init_udp_socket(netplay_t *netplay, const char *server, netplay->udp_fd = -1; } - freeaddrinfo(netplay->addr); + freeaddrinfo_rarch(netplay->addr); netplay->addr = NULL; } @@ -1382,7 +1382,7 @@ void netplay_free(netplay_t *netplay) } if (netplay->addr) - freeaddrinfo(netplay->addr); + freeaddrinfo_rarch(netplay->addr); free(netplay); } @@ -1679,8 +1679,6 @@ void netplay_post_frame(netplay_t *netplay) #ifdef HAVE_SOCKET_LEGACY -#undef getaddrinfo -#undef freeaddrinfo #undef sockaddr_storage #undef addrinfo @@ -1735,61 +1733,5 @@ error: } #endif -int getaddrinfo_rarch__(const char *node, const char *service, - const struct addrinfo *hints, - struct addrinfo **res) -{ - struct sockaddr_in *in_addr; - struct addrinfo *info = (struct addrinfo*)calloc(1, sizeof(*info)); - if (!info) - return -1; - - info->ai_family = AF_INET; - info->ai_socktype = hints->ai_socktype; - - in_addr = (struct sockaddr_in*)calloc(1, sizeof(*in_addr)); - - if (!in_addr) - { - free(info); - return -1; - } - - info->ai_addrlen = sizeof(*in_addr); - - in_addr->sin_family = AF_INET; - in_addr->sin_port = htons(strtoul(service, NULL, 0)); - - if (!node && (hints->ai_flags & AI_PASSIVE)) - in_addr->sin_addr.s_addr = INADDR_ANY; - else if (node && isdigit(*node)) - in_addr->sin_addr.s_addr = inet_addr(node); - else if (node && !isdigit(*node)) - { - struct hostent *host = gethostbyname(node); - if (!host || !host->h_addr_list[0]) - goto error; - - in_addr->sin_addr.s_addr = inet_addr(host->h_addr_list[0]); - } - else - goto error; - - info->ai_addr = (struct sockaddr*)in_addr; - *res = info; - - return 0; - -error: - free(in_addr); - free(info); - return -1; -} - -void freeaddrinfo_rarch__(struct addrinfo *res) -{ - free(res->ai_addr); - free(res); -} #endif diff --git a/netplay_compat.c b/netplay_compat.c new file mode 100644 index 0000000000..f47bd50e84 --- /dev/null +++ b/netplay_compat.c @@ -0,0 +1,85 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2014 - Hans-Kristian Arntzen + * Copyright (C) 2011-2015 - Daniel De Matteis + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + +#include "netplay_compat.h" +#include "netplay.h" +#include +#include + +int getaddrinfo_rarch(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) +{ +#ifdef HAVE_SOCKET_LEGACY + struct sockaddr_in *in_addr; + struct addrinfo *info = (struct addrinfo*)calloc(1, sizeof(*info)); + if (!info) + return -1; + + info->ai_family = AF_INET; + info->ai_socktype = hints->ai_socktype; + + in_addr = (struct sockaddr_in*)calloc(1, sizeof(*in_addr)); + + if (!in_addr) + { + free(info); + return -1; + } + + info->ai_addrlen = sizeof(*in_addr); + + in_addr->sin_family = AF_INET; + in_addr->sin_port = htons(strtoul(service, NULL, 0)); + + if (!node && (hints->ai_flags & AI_PASSIVE)) + in_addr->sin_addr.s_addr = INADDR_ANY; + else if (node && isdigit(*node)) + in_addr->sin_addr.s_addr = inet_addr(node); + else if (node && !isdigit(*node)) + { + struct hostent *host = gethostbyname(node); + if (!host || !host->h_addr_list[0]) + goto error; + + in_addr->sin_addr.s_addr = inet_addr(host->h_addr_list[0]); + } + else + goto error; + + info->ai_addr = (struct sockaddr*)in_addr; + *res = info; + + return 0; + +error: + free(in_addr); + free(info); + return -1; +#else + return getaddrinfo(node, service, hints, res); +#endif +} + +void freeaddrinfo_rarch(struct addrinfo *res) +{ +#ifdef HAVE_SOCKET_LEGACY + free(res->ai_addr); + free(res); +#else + freeaddrinfo(res); +#endif +} diff --git a/netplay_compat.h b/netplay_compat.h index 3754a6528d..b9027f687f 100644 --- a/netplay_compat.h +++ b/netplay_compat.h @@ -127,5 +127,11 @@ void freeaddrinfo(struct addrinfo *res); /* gai_strerror() not used, so we skip that. */ #endif + +int getaddrinfo_rarch(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res); + +void freeaddrinfo_rarch(struct addrinfo *res); #endif