socketlib: Fallback to IPv4 when IPv6 not present.

This fixes systems where IPv6 is available in libraries but just not enabled
in the kernel.
This commit is contained in:
Walter Goossens
2011-05-10 23:33:43 +02:00
parent 01d9918ab6
commit a07282b13c
+11 -2
View File
@@ -31,6 +31,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
@@ -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;
}
}