diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h index 58ad050d74..5d6dd8784e 100644 --- a/libretro-common/include/net/net_socket.h +++ b/libretro-common/include/net/net_socket.h @@ -60,6 +60,8 @@ typedef struct socket_target int socket_init(void **address, uint16_t port, const char *server, enum socket_type type); +int socket_next(void **address); + int socket_close(int fd); bool socket_nonblock(int fd); diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index a4ec2c4652..17f267eecc 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -120,28 +120,25 @@ void net_http_urlencode_full(char **dest, const char *source) static int net_http_new_socket(const char *domain, int port) { int ret; - struct addrinfo *addr = NULL; + struct addrinfo *addr = NULL, *next_addr = NULL; int fd = socket_init( (void**)&addr, port, domain, SOCKET_TYPE_STREAM); - if (fd < 0) - return -1; + next_addr = addr; + while(fd >= 0) + { + ret = socket_connect(fd, (void*)next_addr, true); + if (ret >= 0 && socket_nonblock(fd)) + break; - ret = socket_connect(fd, (void*)addr, true); + socket_close(fd); + fd = socket_next((void**)&next_addr); + } - freeaddrinfo_retro(addr); - - if (ret < 0) - goto error; - - if (!socket_nonblock(fd)) - goto error; + if (addr) + freeaddrinfo_retro(addr); return fd; - -error: - socket_close(fd); - return -1; } static void net_http_send_str(int fd, bool *error, const char *text) diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c index 82e48070e4..8993ecf202 100644 --- a/libretro-common/net/net_socket.c +++ b/libretro-common/net/net_socket.c @@ -68,6 +68,15 @@ error: return -1; } +int socket_next(void **addrinfo) +{ + struct addrinfo *addr = (struct addrinfo*)*addrinfo; + if ((*addrinfo = addr = addr->ai_next)) + return socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + else + return -1; +} + ssize_t socket_receive_all_nonblocking(int fd, bool *error, void *data_, size_t size) {