patch from sehraf to fix compilation on windows

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.6-IPv6-2@8248 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2015-05-15 09:27:25 +00:00
parent e7252e670e
commit b6dc45a00b
5 changed files with 133 additions and 92 deletions

View File

@ -145,6 +145,34 @@ std::string socket_errorType(int err)
return std::string("UNKNOWN ERROR CODE - ASK RS-DEVS TO ADD IT!");
}
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if.h>
bool getLocalAddresses(std::list<struct sockaddr_storage> & addrs)
{
struct ifaddrs *ifsaddrs, *ifa;
if(getifaddrs(&ifsaddrs) != 0)
{
freeifaddrs(ifsaddrs);
return false;
}
addrs.clear();
for ( ifa = ifsaddrs; ifa; ifa = ifa->ifa_next )
if ( (ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_LOOPBACK) )
{
const sockaddr_storage & ifaaddr = * (const struct sockaddr_storage *) ifa->ifa_addr;
if (sockaddr_storage_isLinkLocal(ifaaddr)) continue;
sockaddr_storage tmp;
if (sockaddr_storage_copyip(tmp, ifaaddr)) addrs.push_back(tmp);
}
freeifaddrs(ifsaddrs);
return !addrs.empty();
}
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#else
@ -257,16 +285,8 @@ std::string socket_errorType(int err)
// A function to determine the interfaces on your computer....
// No idea of how to do this in windows....
// see if it compiles.
bool getLocalInterfaces_ipv4(struct in_addr &routeAddr, std::list<struct in_addr> &addrs)
bool getLocalInterfaces_ipv4(std::list<struct in_addr> &addrs)
{
// Get the best interface for transport to routeAddr
// This interface should be first in list!
DWORD bestInterface;
if (GetBestInterface((IPAddr) routeAddr.s_addr, &bestInterface) != NO_ERROR)
{
bestInterface = 0;
}
/* USE MIB IPADDR Interface */
PMIB_IPADDRTABLE iptable = NULL;
DWORD dwSize = 0;
@ -308,16 +328,8 @@ bool getLocalInterfaces_ipv4(struct in_addr &routeAddr, std::list<struct in_addr
continue;
}
if (ipaddr.dwIndex == bestInterface)
{
pqioutput(PQL_DEBUG_BASIC, pqinetzone, "Best address, " + out);
addrs.push_front(addr);
}
else
{
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
addrs.push_back(addr);
}
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
addrs.push_back(addr);
}
free (iptable);
@ -325,6 +337,24 @@ bool getLocalInterfaces_ipv4(struct in_addr &routeAddr, std::list<struct in_addr
return (addrs.size() > 0);
}
// temporary fix
// wrapper for getLocalInterfaces_ipv4
bool getLocalAddresses(std::list<struct sockaddr_storage> &addrs)
{
addrs.clear();
std::list<struct in_addr> addrs2;
std::list<struct in_addr>::iterator addrs2_it;
getLocalInterfaces_ipv4(addrs2);
for(addrs2_it = addrs2.begin(); addrs2_it != addrs2.end(); ++addrs2_it) {
struct sockaddr_storage *ss = new sockaddr_storage;
sockaddr_in *ss_ptr = (sockaddr_in *) ss;
ss_ptr->sin_family = AF_INET;
ss_ptr->sin_addr = *addrs2_it;
addrs.push_back(*ss);
}
return !addrs.empty();
}
// implement the improved unix inet address fn.
// using old one.
int inet_aton(const char *name, struct in_addr *addr)
@ -332,67 +362,57 @@ int inet_aton(const char *name, struct in_addr *addr)
return (((*addr).s_addr = inet_addr(name)) != INADDR_NONE);
}
// This returns in Net Byte Order.
// NB: Linux man page claims it is in Host Byte order, but
// this is blatantly wrong!..... (for Debian anyway)
// Making this consistent with the Actual behavior (rather than documented).
in_addr_t inet_netof(struct in_addr addr)
int inet_pton(int af, const char *src, void *dst)
{
return pqi_inet_netof(addr);
}
struct sockaddr_storage ss;
int size = sizeof(ss);
char src_copy[INET6_ADDRSTRLEN+1];
// This returns in Host Byte Order. (as the man page says)
// Again, to be consistent with Linux.
in_addr_t inet_network(const char *inet_name)
{
struct in_addr addr;
if (inet_aton(inet_name, &addr))
{
#ifdef NET_DEBUG
// std::cerr << "inet_network(" << inet_name << ") : ";
// std::cerr << rs_inet_ntoa(addr) << std::endl;
#endif
return ntohl(inet_netof(addr));
ZeroMemory(&ss, sizeof(ss));
/* stupid non-const API */
strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
src_copy[INET6_ADDRSTRLEN] = 0;
if (WSAStringToAddressA(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
switch(af) {
case AF_INET:
*(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
return 1;
case AF_INET6:
*(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
return 1;
}
}
return 0xffffffff;
//return -1;
return 0;
}
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
{
struct sockaddr_storage ss;
unsigned long s = size;
ZeroMemory(&ss, sizeof(ss));
ss.ss_family = af;
switch(af) {
case AF_INET:
((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
break;
case AF_INET6:
((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
break;
default:
return NULL;
}
/* cannot direclty use &size because of strict aliasing rules */
int ret = WSAAddressToStringA((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s);
return (ret == 0) ? dst : NULL;
}
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if.h>
bool getLocalAddresses(std::list<struct sockaddr_storage> & addrs)
{
struct ifaddrs *ifsaddrs, *ifa;
if(getifaddrs(&ifsaddrs) != 0)
{
freeifaddrs(ifsaddrs);
return false;
}
addrs.clear();
for ( ifa = ifsaddrs; ifa; ifa = ifa->ifa_next )
if ( (ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_LOOPBACK) )
{
const sockaddr_storage & ifaaddr = * (const struct sockaddr_storage *) ifa->ifa_addr;
if (sockaddr_storage_isLinkLocal(ifaaddr)) continue;
sockaddr_storage tmp;
if (sockaddr_storage_copyip(tmp, ifaaddr)) addrs.push_back(tmp);
}
freeifaddrs(ifsaddrs);
return !addrs.empty();
}
/*************************************************************
* Socket Library Wrapper Functions
* to get over the crapness of the windows.

View File

@ -109,7 +109,9 @@ int unix_getsockopt_error(int sockfd, int *err);
#ifdef WINDOWS_SYS // WINDOWS
/******************* WINDOWS SPECIFIC PART ******************/
int WinToUnixError(int error);
int WinToUnixError(int error);
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size_param);
int inet_pton(int af, const char *src, void *dst);
#endif

View File

@ -736,7 +736,11 @@ int pqissl::Initiate_Connection()
#ifdef IPV6_V6ONLY
int no = 0;
#ifdef WINDOWS_SYS
err = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&no, sizeof(no));
#else
err = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&no, sizeof(no));
#endif // WINDOWS_SYS
#ifdef PQISSL_DEBUG
if (err) std::cerr << "pqissl::Initiate_Connection: Error setting IPv6 socket dual stack" << std::endl;
else std::cerr << "pqissl::Initiate_Connection: Setting IPv6 socket dual stack" << std::endl;

View File

@ -102,7 +102,11 @@ int pqissllistenbase::setuplisten()
#ifdef IPV6_V6ONLY
int no = 0;
#ifdef WINDOWS_SYS
err = setsockopt(lsock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&no, sizeof(no));
#else
err = setsockopt(lsock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&no, sizeof(no));
#endif // WINDOWS_SYS
if (err) std::cerr << "pqissl::Initiate_Connection: Error setting IPv6 socket dual stack" << std::endl;
else std::cerr << "pqissl::Initiate_Connection: Setting IPv6 socket dual stack" << std::endl;
#endif // IPV6_V6ONLY

View File

@ -24,11 +24,15 @@
*/
#include <sstream>
#ifdef WINDOWS_SYS
// u.Word (Windows) is the same as s6_addr16 (Linux)
#define s6_addr16 u.Word
#else
#include <netinet/in.h>
#endif // WINDOWS_SYS
#include "util/rsnet.h"
#include "util/rsstring.h"
#include "util/stacktrace.h"
#include "pqi/pqinetwork.h"
/***************************** Internal Helper Fns ******************************/
@ -287,16 +291,16 @@ bool sockaddr_storage_ipv4_to_ipv6(sockaddr_storage &addr)
if ( addr.ss_family == AF_INET )
{
sockaddr_in & addr_ipv4 = (sockaddr_in &) addr;
const char *ip[4];
memcpy(ip, (void *)(&addr_ipv4.sin_addr.s_addr), 4);
uint16_t port = addr_ipv4.sin_port;
sockaddr_in6 & addr_ipv6 = (sockaddr_in6 &) addr;
u_int32_t ip = addr_ipv4.sin_addr.s_addr;
u_int16_t port = addr_ipv4.sin_port;
sockaddr_storage_clear(addr);
addr_ipv6.sin6_family = AF_INET6;
addr_ipv6.sin6_port = port;
addr_ipv6.sin6_addr.s6_addr32[3] = ip;
addr_ipv6.sin6_addr.s6_addr16[5] = (u_int16_t) 0xffff;
memcpy((void *)&(addr_ipv6.sin6_addr.s6_addr16[6]), ip, 4);
addr_ipv6.sin6_addr.s6_addr16[5] = (uint16_t) 0xffff;
return true;
}
@ -315,15 +319,15 @@ bool sockaddr_storage_ipv6_to_ipv4(sockaddr_storage &addr)
if(sockaddr_storage_isIPv4Mapped(addr))
{
sockaddr_in6 & addr_ipv6 = (sockaddr_in6 &) addr;
u_int32_t ip = addr_ipv6.sin6_addr.s6_addr32[3];
u_int16_t port = addr_ipv6.sin6_port;
const char *ip[4];
memcpy(ip, (void *)&(addr_ipv6.sin6_addr.s6_addr16[6]), 4);
uint16_t port = addr_ipv6.sin6_port;
sockaddr_in & addr_ipv4 = (sockaddr_in &) addr;
sockaddr_storage_clear(addr);
addr_ipv4.sin_family = AF_INET;
memcpy((void *)&(addr_ipv4.sin_addr.s_addr), ip, 4);
addr_ipv4.sin_port = port;
addr_ipv4.sin_addr.s_addr = ip;
return true;
}
@ -500,8 +504,8 @@ bool sockaddr_storage_isLinkLocal(const struct sockaddr_storage &addr)
case AF_INET6:
{
const sockaddr_in6 * addr6 = (const sockaddr_in6 *) &addr;
u_int16_t mask = htons(0xffc0);
u_int16_t llPrefix = htons(0xfe80);
uint16_t mask = htons(0xffc0);
uint16_t llPrefix = htons(0xfe80);
return ((addr6->sin6_addr.s6_addr16[0] & mask ) == llPrefix);
}
@ -515,9 +519,9 @@ bool sockaddr_storage_isIPv4Mapped(const struct sockaddr_storage &addr)
if (addr.ss_family == AF_INET6)
{
sockaddr_in6 & addr_ipv6 = (sockaddr_in6 &) addr;
bool ipv4m = addr_ipv6.sin6_addr.s6_addr16[5] == (u_int16_t) 0xffff;
bool ipv4m = addr_ipv6.sin6_addr.s6_addr16[5] == (uint16_t) 0xffff;
for ( int i = 0; ipv4m && i < 5 ; ++i )
ipv4m &= addr_ipv6.sin6_addr.s6_addr16[i] == (u_int16_t) 0x0000;
ipv4m &= addr_ipv6.sin6_addr.s6_addr16[i] == (uint16_t) 0x0000;
return ipv4m;
}
@ -933,8 +937,15 @@ void sockaddr_storage_dump(const sockaddr_storage & addr, std::string * outputSt
output << addr.ss_family;
output << " addr.__ss_align = ";
output << addr.__ss_align;
#ifdef WINDOWS_SYS
output << " addr.__ss_pad1 = ";
output << addr.__ss_pad1;
output << " addr.__ss_pad2 = ";
output << addr.__ss_pad2;
#else
output << " addr.__ss_padding = ";
output << addr.__ss_padding;
#endif // WINDOWS_SYS
}}
if(outputString)
@ -1033,9 +1044,9 @@ bool sockaddr_storage_ipv6_isnull(const struct sockaddr_storage & addr)
#endif
const sockaddr_in6 & addr6 = (const sockaddr_in6 &) addr;
bool isNull = (addr6.sin6_addr.s6_addr32[3] == 0x00000000);
for (int i=0; isNull && i<3; ++i)
isNull &= (addr6.sin6_addr.s6_addr32[i] == 0x00000000);
bool isNull = (addr6.sin6_addr.s6_addr16[7] == 0x0000);
for (int i=0; isNull && i<7; ++i)
isNull &= (addr6.sin6_addr.s6_addr16[i] == 0x0000);
return isNull;
}
@ -1052,9 +1063,9 @@ bool sockaddr_storage_ipv6_isValidNet(const struct sockaddr_storage & )
bool sockaddr_storage_ipv6_isLoopbackNet(const struct sockaddr_storage & addr )
{
sockaddr_in6 & addr6 = (sockaddr_in6 &) addr;
bool isLp = (addr6.sin6_addr.s6_addr32[3] == htonl(0x00000001));
for (int i=0; isLp && i<3; ++i)
isLp &= (addr6.sin6_addr.s6_addr32[i] == 0x00000000);
bool isLp = (addr6.sin6_addr.s6_addr16[7] == htonl(0x0001));
for (int i=0; isLp && i<7; ++i)
isLp &= (addr6.sin6_addr.s6_addr16[i] == 0x0000);
#ifdef SS_DEBUG
sockaddr_storage_dump(addr);