diff --git a/command.c b/command.c index d1070fb1f3..27a1243a74 100644 --- a/command.c +++ b/command.c @@ -209,7 +209,7 @@ command_t* command_network_new(uint16_t port) command_network_t *netcmd = (command_network_t*)calloc( 1, sizeof(command_network_t)); int fd = socket_init( - (void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM); + (void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM, AF_INET); RARCH_LOG("[NetCMD]: %s %hu.\n", msg_hash_to_str(MSG_BRINGING_UP_COMMAND_INTERFACE_ON_PORT), diff --git a/gfx/drivers/network_gfx.c b/gfx/drivers/network_gfx.c index 3243b1e359..a659d5aa73 100644 --- a/gfx/drivers/network_gfx.c +++ b/gfx/drivers/network_gfx.c @@ -117,7 +117,7 @@ static void *network_gfx_init(const video_info_t *video, RARCH_LOG("[Network]: Connecting to host %s:%d\n", network->address, network->port); try_connect: - fd = socket_init((void**)&addr, network->port, network->address, SOCKET_TYPE_STREAM); + fd = socket_init((void**)&addr, network->port, network->address, SOCKET_TYPE_STREAM, 0); next_addr = addr; diff --git a/input/input_driver.c b/input/input_driver.c index dc96e6ef7e..96cd3e82a4 100644 --- a/input/input_driver.c +++ b/input/input_driver.c @@ -1033,7 +1033,7 @@ static bool input_remote_init_network(input_remote_t *handle, RARCH_LOG("Bringing up remote interface on port %hu.\n", (unsigned short)port); - if ((fd = socket_init((void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM)) < 0) + if ((fd = socket_init((void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM, AF_INET)) < 0) goto error; handle->net_fd[user] = fd; diff --git a/libretro-common/include/net/net_compat.h b/libretro-common/include/net/net_compat.h index 737f71191c..cefa71dee8 100644 --- a/libretro-common/include/net/net_compat.h +++ b/libretro-common/include/net/net_compat.h @@ -70,6 +70,7 @@ struct hostent int h_length; char **h_addr_list; char *h_addr; + char *h_end; }; #elif defined(GEKKO) @@ -172,6 +173,7 @@ struct hostent int h_length; char **h_addr_list; char *h_addr; + char *h_end; }; struct SceNetInAddr inet_aton(const char *ip_addr); diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h index 6df9a8bd6b..7d17595803 100644 --- a/libretro-common/include/net/net_socket.h +++ b/libretro-common/include/net/net_socket.h @@ -60,7 +60,8 @@ typedef struct socket_target enum socket_protocol prot; } socket_target_t; -int socket_init(void **address, uint16_t port, const char *server, enum socket_type type); +int socket_init(void **address, uint16_t port, const char *server, + enum socket_type type, int family); int socket_next(void **address); diff --git a/libretro-common/net/net_compat.c b/libretro-common/net/net_compat.c index 146f4c630b..b44fff680a 100644 --- a/libretro-common/net/net_compat.c +++ b/libretro-common/net/net_compat.c @@ -36,8 +36,8 @@ #if defined(_XBOX) struct hostent *gethostbyname(const char *name) { - static struct in_addr addr; - static struct hostent he = {0}; + static struct in_addr addr = {0}; + static struct hostent he = {0}; WSAEVENT event; XNDNS *dns = NULL; struct hostent *ret = NULL; @@ -114,30 +114,34 @@ unsigned int inet_addr(const char *cp) struct hostent *gethostbyname(const char *name) { - int err; - static struct hostent ent; - static char sname[MAX_NAME] = {0}; - static struct SceNetInAddr saddr = {0}; - static char *addrlist[2] = {(char *) &saddr, NULL }; - int rid = sceNetResolverCreate("resolver", NULL, 0); + static struct SceNetInAddr addr = {0}; + static struct hostent he = {0}; + int rid; + struct hostent *ret = NULL; + if (!name) + return NULL; + + rid = sceNetResolverCreate("resolver", NULL, 0); if(rid < 0) return NULL; - err = sceNetResolverStartNtoa(rid, name, &saddr, 0,0,0); + if (sceNetResolverStartNtoa(rid, name, &addr, 0, 0, 0) < 0) + goto done; + + he.h_name = NULL; + he.h_aliases = NULL; + he.h_addrtype = AF_INET; + he.h_length = sizeof(addr); + he.h_addr_list = &he.h_addr; + he.h_addr = (char*)&addr; + + ret = &he; + +done: sceNetResolverDestroy(rid); - if(err < 0) - return NULL; - addrlist[0] = inet_ntoa(saddr); - ent.h_name = sname; - ent.h_aliases = 0; - ent.h_addrtype = AF_INET; - ent.h_length = sizeof(struct in_addr); - ent.h_addr_list = addrlist; - ent.h_addr = addrlist[0]; - - return &ent; + return ret; } #elif defined(_3DS) @@ -207,11 +211,7 @@ int getaddrinfo_retro(const char *node, const char *service, addr->sin_family = AF_INET; if (service) addr->sin_port = inet_htons((uint16_t)strtoul(service, NULL, 10)); -#ifdef VITA - addr->sin_addr.s_addr = inet_addr(host->h_addr); -#else memcpy(&addr->sin_addr, host->h_addr, sizeof(addr->sin_addr)); -#endif *res = info; diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 503feac98b..00a1d31ac5 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -417,7 +417,7 @@ static int net_http_new_socket(struct http_connection_t *conn) { struct addrinfo *addr = NULL, *next_addr = NULL; int fd = socket_init( - (void**)&addr, conn->port, conn->domain, SOCKET_TYPE_STREAM); + (void**)&addr, conn->port, conn->domain, SOCKET_TYPE_STREAM, 0); #ifdef HAVE_SSL if (conn->sock_state.ssl) { diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c index 2c802b02ad..050f953dc1 100644 --- a/libretro-common/net/net_socket.c +++ b/libretro-common/net/net_socket.c @@ -32,15 +32,22 @@ #include -int socket_init(void **address, uint16_t port, const char *server, enum socket_type type) +int socket_init(void **address, uint16_t port, const char *server, + enum socket_type type, int family) { char port_buf[6]; struct addrinfo hints = {0}; struct addrinfo **addrinfo = (struct addrinfo**)address; struct addrinfo *addr = NULL; - if (!network_init()) - return -1; + if (!family) +#if defined(HAVE_SOCKET_LEGACY) || defined(WIIU) + family = AF_INET; +#else + family = AF_UNSPEC; +#endif + + hints.ai_family = family; switch (type) { @@ -50,14 +57,16 @@ int socket_init(void **address, uint16_t port, const char *server, enum socket_t case SOCKET_TYPE_STREAM: hints.ai_socktype = SOCK_STREAM; break; - case SOCKET_TYPE_SEQPACKET: - /* TODO/FIXME - implement? */ - break; + default: + return -1; } if (!server) hints.ai_flags = AI_PASSIVE; + if (!network_init()) + return -1; + snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); if (getaddrinfo_retro(server, port_buf, &hints, addrinfo)) diff --git a/network/natt.c b/network/natt.c index 63264d29a5..a89fbb0dd2 100644 --- a/network/natt.c +++ b/network/natt.c @@ -90,7 +90,8 @@ bool natt_init(struct natt_discovery *discovery) if (!msearch_addr) goto failure; - fd = socket_init((void **) &bind_addr, 0, NULL, SOCKET_TYPE_DATAGRAM); + fd = socket_init((void**)&bind_addr, 0, NULL, + SOCKET_TYPE_DATAGRAM, AF_INET); if (fd < 0) goto failure; if (!bind_addr) diff --git a/network/netplay/netplay_frontend.c b/network/netplay/netplay_frontend.c index dc245be38d..7cf0a77f91 100644 --- a/network/netplay/netplay_frontend.c +++ b/network/netplay/netplay_frontend.c @@ -214,7 +214,7 @@ bool init_netplay_discovery(void) struct addrinfo *addr = NULL; net_driver_state_t *net_st = &networking_driver_st; int fd = socket_init((void**)&addr, 0, NULL, - SOCKET_TYPE_DATAGRAM); + SOCKET_TYPE_DATAGRAM, AF_INET); bool ret = fd >= 0 && addr; if (ret) @@ -457,7 +457,7 @@ static bool init_lan_ad_server_socket(void) struct addrinfo *addr = NULL; net_driver_state_t *net_st = &networking_driver_st; int fd = socket_init((void**)&addr, RARCH_DISCOVERY_PORT, - NULL, SOCKET_TYPE_DATAGRAM); + NULL, SOCKET_TYPE_DATAGRAM, AF_INET); bool ret = fd >= 0 && addr && socket_bind(fd, addr) && socket_nonblock(fd); diff --git a/tools/ranetplayer/ranetplayer.c b/tools/ranetplayer/ranetplayer.c index 7ff0952f71..8d692964fe 100644 --- a/tools/ranetplayer/ranetplayer.c +++ b/tools/ranetplayer/ranetplayer.c @@ -271,7 +271,7 @@ int main(int argc, char **argv) } /* Connect to the netplay server */ - if ((sock = socket_init((void **) &addr, port, host, SOCKET_PROTOCOL_TCP)) < 0) + if ((sock = socket_init((void**)&addr, port, host, SOCKET_PROTOCOL_TCP, 0)) < 0) { perror("socket"); return 1;