Create netplay_compat.c and put wrappers around getaddrinfo/freeaddrinfo

This commit is contained in:
twinaphex 2015-01-23 06:29:36 +01:00
parent cdc03204e7
commit c854645ba9
6 changed files with 105 additions and 70 deletions

View File

@ -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

View File

@ -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"

View File

@ -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 */

View File

@ -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

85
netplay_compat.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "netplay_compat.h"
#include "netplay.h"
#include <stdlib.h>
#include <string.h>
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
}

View File

@ -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