Fall back to IPv4 when IPv6 connections fail

This commit is contained in:
Ken 2017-07-01 08:20:50 -04:00
parent 5636b0fdd6
commit edc64991f3
3 changed files with 23 additions and 15 deletions

View File

@ -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_init(void **address, uint16_t port, const char *server, enum socket_type type);
int socket_next(void **address);
int socket_close(int fd); int socket_close(int fd);
bool socket_nonblock(int fd); bool socket_nonblock(int fd);

View File

@ -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) static int net_http_new_socket(const char *domain, int port)
{ {
int ret; int ret;
struct addrinfo *addr = NULL; struct addrinfo *addr = NULL, *next_addr = NULL;
int fd = socket_init( int fd = socket_init(
(void**)&addr, port, domain, SOCKET_TYPE_STREAM); (void**)&addr, port, domain, SOCKET_TYPE_STREAM);
if (fd < 0) next_addr = addr;
return -1; 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 (addr)
freeaddrinfo_retro(addr);
if (ret < 0)
goto error;
if (!socket_nonblock(fd))
goto error;
return fd; return fd;
error:
socket_close(fd);
return -1;
} }
static void net_http_send_str(int fd, bool *error, const char *text) static void net_http_send_str(int fd, bool *error, const char *text)

View File

@ -68,6 +68,15 @@ error:
return -1; 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, ssize_t socket_receive_all_nonblocking(int fd, bool *error,
void *data_, size_t size) void *data_, size_t size)
{ {