From a07282b13ca4489331a589f302d563b0f9310982 Mon Sep 17 00:00:00 2001 From: Walter Goossens Date: Tue, 10 May 2011 23:33:43 +0200 Subject: [PATCH] socketlib: Fallback to IPv4 when IPv6 not present. This fixes systems where IPv6 is available in libraries but just not enabled in the kernel. --- socketlib.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/socketlib.c b/socketlib.c index 220c40c5..82e63e81 100644 --- a/socketlib.c +++ b/socketlib.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -43,7 +44,15 @@ int common_setup(struct addrinfo *pAddrInfo) int tSock; //printAddrs(pAddrInfo); tSock = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, 0); - +#ifdef AF_INET6 + if((tSock==-1) && (pAddrInfo->ai_family == AF_INET6) && (errno == EAFNOSUPPORT)) + { + //Fallback to ipv4 + perror("Failed to create ipv6 socket. Trying ipv4"); + pAddrInfo->ai_family = AF_INET; + tSock = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, 0); + } +#endif return tSock; } @@ -265,4 +274,4 @@ char *encode_base64(unsigned char *input, int length) BIO_free_all(b64); return buff; -} \ No newline at end of file +}