2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* "$Id: pqinetwork.cc,v 1.18 2007-04-15 18:45:18 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-09 19:07:51 -04:00
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#include "util/rswin.h"
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif // WINDOWS_SYS
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include "pqi/pqinetwork.h"
|
2008-06-07 07:21:00 -04:00
|
|
|
#include "util/rsnet.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
2009-05-27 16:59:48 -04:00
|
|
|
#include <stdio.h>
|
2012-04-12 19:29:39 -04:00
|
|
|
#include <unistd.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2012-04-13 20:30:23 -04:00
|
|
|
#include "util/rsstring.h"
|
2015-03-09 15:00:41 -04:00
|
|
|
#include "util/rsnet.h"
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
static const int pqinetzone = 96184;
|
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
/*****
|
|
|
|
* #define NET_DEBUG 1
|
|
|
|
****/
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
#ifdef WINDOWS_SYS /* Windows - define errno */
|
|
|
|
|
|
|
|
int errno;
|
|
|
|
|
|
|
|
#else /* Windows - define errno */
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-10-20 12:41:40 -04:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#define IFF_RUNNING 0x0001
|
|
|
|
#endif
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
void showSocketError(std::string &out)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
int err = errno;
|
2012-04-13 20:30:23 -04:00
|
|
|
rs_sprintf_append(out, "\tSocket Error(%d) : %s\n", err, socket_errorType(err).c_str());
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string socket_errorType(int err)
|
|
|
|
{
|
|
|
|
if (err == EBADF)
|
|
|
|
{
|
|
|
|
return std::string("EBADF");
|
|
|
|
}
|
|
|
|
else if (err == EINVAL)
|
|
|
|
{
|
|
|
|
return std::string("EINVAL");
|
|
|
|
}
|
|
|
|
else if (err == EFAULT)
|
|
|
|
{
|
|
|
|
return std::string("EFAULT");
|
|
|
|
}
|
|
|
|
else if (err == ENOTSOCK)
|
|
|
|
{
|
|
|
|
return std::string("ENOTSOCK");
|
|
|
|
}
|
|
|
|
else if (err == EISCONN)
|
|
|
|
{
|
|
|
|
return std::string("EISCONN");
|
|
|
|
}
|
|
|
|
else if (err == ECONNREFUSED)
|
|
|
|
{
|
|
|
|
return std::string("ECONNREFUSED");
|
|
|
|
}
|
|
|
|
else if (err == ETIMEDOUT)
|
|
|
|
{
|
|
|
|
return std::string("ETIMEDOUT");
|
|
|
|
}
|
|
|
|
else if (err == ENETUNREACH)
|
|
|
|
{
|
|
|
|
return std::string("ENETUNREACH");
|
|
|
|
}
|
|
|
|
else if (err == EADDRINUSE)
|
|
|
|
{
|
|
|
|
return std::string("EADDRINUSE");
|
|
|
|
}
|
|
|
|
else if (err == EINPROGRESS)
|
|
|
|
{
|
|
|
|
return std::string("EINPROGRESS");
|
|
|
|
}
|
|
|
|
else if (err == EALREADY)
|
|
|
|
{
|
|
|
|
return std::string("EALREADY");
|
|
|
|
}
|
|
|
|
else if (err == EAGAIN)
|
|
|
|
{
|
|
|
|
return std::string("EAGAIN");
|
|
|
|
}
|
|
|
|
else if (err == EISCONN)
|
|
|
|
{
|
|
|
|
return std::string("EISCONN");
|
|
|
|
}
|
|
|
|
else if (err == ENOTCONN)
|
|
|
|
{
|
|
|
|
return std::string("ENOTCONN");
|
|
|
|
}
|
2012-01-18 17:51:38 -05:00
|
|
|
// These ones have been turning up in SSL CONNECTION FAILURES.
|
|
|
|
else if (err == EPIPE)
|
|
|
|
{
|
|
|
|
return std::string("EPIPE");
|
|
|
|
}
|
|
|
|
else if (err == ECONNRESET)
|
|
|
|
{
|
|
|
|
return std::string("ECONNRESET");
|
|
|
|
}
|
2012-01-19 11:23:57 -05:00
|
|
|
else if (err == EHOSTUNREACH)
|
|
|
|
{
|
|
|
|
return std::string("EHOSTUNREACH");
|
|
|
|
}
|
2012-01-19 20:05:46 -05:00
|
|
|
else if (err == EADDRNOTAVAIL)
|
|
|
|
{
|
|
|
|
return std::string("EADDRNOTAVAIL");
|
|
|
|
}
|
2012-01-18 17:51:38 -05:00
|
|
|
//
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-01-18 17:51:38 -05:00
|
|
|
return std::string("UNKNOWN ERROR CODE - ASK RS-DEVS TO ADD IT!");
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
|
|
|
#else
|
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
void showSocketError(std::string &out)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
int err = WSAGetLastError();
|
2012-04-13 20:30:23 -04:00
|
|
|
rs_sprintf_append(out, "\tSocket Error(%d) : %s\n", err, socket_errorType(err).c_str());
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string socket_errorType(int err)
|
|
|
|
{
|
|
|
|
if (err == WSAEBADF)
|
|
|
|
{
|
|
|
|
return std::string("WSABADF");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if (err == WSAEINTR)
|
|
|
|
{
|
|
|
|
return std::string("WSAEINTR");
|
|
|
|
}
|
|
|
|
else if (err == WSAEACCES)
|
|
|
|
{
|
|
|
|
return std::string("WSAEACCES");
|
|
|
|
}
|
|
|
|
else if (err == WSAEFAULT)
|
|
|
|
{
|
|
|
|
return std::string("WSAEFAULT");
|
|
|
|
}
|
|
|
|
else if (err == WSAEINVAL)
|
|
|
|
{
|
|
|
|
return std::string("WSAEINVAL");
|
|
|
|
}
|
|
|
|
else if (err == WSAEMFILE)
|
|
|
|
{
|
|
|
|
return std::string("WSAEMFILE");
|
|
|
|
}
|
|
|
|
else if (err == WSAEWOULDBLOCK)
|
|
|
|
{
|
|
|
|
return std::string("WSAEWOULDBLOCK");
|
|
|
|
}
|
|
|
|
else if (err == WSAEINPROGRESS)
|
|
|
|
{
|
|
|
|
return std::string("WSAEINPROGRESS");
|
|
|
|
}
|
|
|
|
else if (err == WSAEALREADY)
|
|
|
|
{
|
|
|
|
return std::string("WSAEALREADY");
|
|
|
|
}
|
|
|
|
else if (err == WSAENOTSOCK)
|
|
|
|
{
|
|
|
|
return std::string("WSAENOTSOCK");
|
|
|
|
}
|
|
|
|
else if (err == WSAEDESTADDRREQ)
|
|
|
|
{
|
|
|
|
return std::string("WSAEDESTADDRREQ");
|
|
|
|
}
|
|
|
|
else if (err == WSAEMSGSIZE)
|
|
|
|
{
|
|
|
|
return std::string("WSAEMSGSIZE");
|
|
|
|
}
|
|
|
|
else if (err == WSAEPROTOTYPE)
|
|
|
|
{
|
|
|
|
return std::string("WSAEPROTOTYPE");
|
|
|
|
}
|
|
|
|
else if (err == WSAENOPROTOOPT)
|
|
|
|
{
|
|
|
|
return std::string("WSAENOPROTOOPT");
|
|
|
|
}
|
|
|
|
else if (err == WSAENOTSOCK)
|
|
|
|
{
|
|
|
|
return std::string("WSAENOTSOCK");
|
|
|
|
}
|
|
|
|
else if (err == WSAEISCONN)
|
|
|
|
{
|
|
|
|
return std::string("WSAISCONN");
|
|
|
|
}
|
|
|
|
else if (err == WSAECONNREFUSED)
|
|
|
|
{
|
|
|
|
return std::string("WSACONNREFUSED");
|
|
|
|
}
|
|
|
|
else if (err == WSAECONNRESET)
|
|
|
|
{
|
|
|
|
return std::string("WSACONNRESET");
|
|
|
|
}
|
|
|
|
else if (err == WSAETIMEDOUT)
|
|
|
|
{
|
|
|
|
return std::string("WSATIMEDOUT");
|
|
|
|
}
|
|
|
|
else if (err == WSAENETUNREACH)
|
|
|
|
{
|
|
|
|
return std::string("WSANETUNREACH");
|
|
|
|
}
|
|
|
|
else if (err == WSAEADDRINUSE)
|
|
|
|
{
|
|
|
|
return std::string("WSAADDRINUSE");
|
|
|
|
}
|
|
|
|
else if (err == WSAEAFNOSUPPORT)
|
|
|
|
{
|
|
|
|
return std::string("WSAEAFNOSUPPORT (normally UDP related!)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string("----WINDOWS OPERATING SYSTEM FAILURE----");
|
|
|
|
}
|
|
|
|
|
|
|
|
// implement the improved unix inet address fn.
|
|
|
|
// using old one.
|
|
|
|
int inet_aton(const char *name, struct in_addr *addr)
|
|
|
|
{
|
|
|
|
return (((*addr).s_addr = inet_addr(name)) != INADDR_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
|
|
|
|
|
|
|
|
2015-03-09 15:00:41 -04:00
|
|
|
#include <sys/types.h>
|
2015-12-01 17:38:44 -05:00
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#pragma comment(lib, "IPHLPAPI.lib")
|
|
|
|
#else // WINDOWS_SYS
|
2015-03-09 15:00:41 -04:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <net/if.h>
|
2015-12-01 17:38:44 -05:00
|
|
|
#endif // WINDOWS_SYS
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2015-03-09 15:00:41 -04:00
|
|
|
void getLocalAddressesFailed()
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2015-03-09 15:00:41 -04:00
|
|
|
std::cerr << "FATAL ERROR: getLocalAddresses failed!" << std::endl;
|
|
|
|
exit(1);
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2015-03-09 15:00:41 -04:00
|
|
|
bool getLocalAddresses(std::list<sockaddr_storage> & addrs)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2015-03-09 15:00:41 -04:00
|
|
|
addrs.clear();
|
2010-07-10 16:34:03 -04:00
|
|
|
|
2015-03-09 15:00:41 -04:00
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
// Seems strange to me but M$ documentation suggests to allocate this way...
|
|
|
|
DWORD bf_size = 16000;
|
|
|
|
IP_ADAPTER_ADDRESSES* adapter_addresses = (IP_ADAPTER_ADDRESSES*) malloc(bf_size);
|
|
|
|
DWORD error = GetAdaptersAddresses(AF_UNSPEC,
|
|
|
|
GAA_FLAG_SKIP_MULTICAST |
|
|
|
|
GAA_FLAG_SKIP_DNS_SERVER |
|
|
|
|
GAA_FLAG_SKIP_FRIENDLY_NAME,
|
|
|
|
NULL,
|
|
|
|
adapter_addresses,
|
|
|
|
&bf_size);
|
|
|
|
if (error != ERROR_SUCCESS) getLocalAddressesFailed();
|
|
|
|
|
|
|
|
IP_ADAPTER_ADDRESSES* adapter(NULL);
|
|
|
|
for(adapter = adapter_addresses; NULL != adapter; adapter = adapter->Next)
|
|
|
|
{
|
|
|
|
IP_ADAPTER_UNICAST_ADDRESS* address;
|
|
|
|
for ( address = adapter->FirstUnicastAddress; address; address = address->Next)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2015-03-09 15:00:41 -04:00
|
|
|
sockaddr_storage * tmp = new sockaddr_storage;
|
|
|
|
sockaddr_storage_clear(*tmp);
|
2015-12-01 17:38:44 -05:00
|
|
|
if (sockaddr_storage_copyip(* tmp, * reinterpret_cast<sockaddr_storage*>(address->Address.lpSockaddr)))
|
2015-03-09 15:00:41 -04:00
|
|
|
addrs.push_back(*tmp);
|
|
|
|
else delete tmp;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 15:00:41 -04:00
|
|
|
free(adapter_addresses);
|
|
|
|
#else // WINDOWS_SYS
|
|
|
|
struct ifaddrs *ifsaddrs, *ifa;
|
|
|
|
if(getifaddrs(&ifsaddrs) != 0) getLocalAddressesFailed();
|
|
|
|
for ( ifa = ifsaddrs; ifa; ifa = ifa->ifa_next )
|
2015-12-21 11:34:53 -05:00
|
|
|
if ( ifa->ifa_addr && (ifa->ifa_flags & IFF_UP) )
|
2013-09-13 10:35:19 -04:00
|
|
|
{
|
2015-03-09 15:00:41 -04:00
|
|
|
sockaddr_storage * tmp = new sockaddr_storage;
|
|
|
|
sockaddr_storage_clear(*tmp);
|
|
|
|
if (sockaddr_storage_copyip(* tmp, * reinterpret_cast<sockaddr_storage*>(ifa->ifa_addr)))
|
|
|
|
addrs.push_back(*tmp);
|
|
|
|
else delete tmp;
|
2013-09-13 10:35:19 -04:00
|
|
|
}
|
2015-03-09 15:00:41 -04:00
|
|
|
freeifaddrs(ifsaddrs);
|
|
|
|
#endif // WINDOWS_SYS
|
2013-09-13 10:35:19 -04:00
|
|
|
|
2015-12-21 09:48:18 -05:00
|
|
|
#ifdef NET_DEBUG
|
2015-03-09 15:00:41 -04:00
|
|
|
std::list<sockaddr_storage>::iterator it;
|
|
|
|
std::cout << "getLocalAddresses(...) returning: <" ;
|
|
|
|
for(it = addrs.begin(); it != addrs.end(); ++it)
|
|
|
|
std::cout << sockaddr_storage_iptostring(*it) << ", ";
|
|
|
|
std::cout << ">" << std::endl;
|
2015-12-21 09:48:18 -05:00
|
|
|
#endif
|
2013-09-13 10:35:19 -04:00
|
|
|
|
2015-03-09 15:00:41 -04:00
|
|
|
return !addrs.empty();
|
2013-09-13 10:35:19 -04:00
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/*************************************************************
|
|
|
|
* Socket Library Wrapper Functions
|
|
|
|
* to get over the crapness of the windows.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
int unix_close(int fd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
#ifndef WINDOWS_SYS // ie UNIX
|
|
|
|
ret = close(fd);
|
|
|
|
#else
|
2008-07-09 05:55:09 -04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "unix_close()" << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
ret = closesocket(fd);
|
|
|
|
/* translate error */
|
|
|
|
#endif
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-12 09:25:01 -05:00
|
|
|
int unix_socket(int domain, int type, int protocol)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2015-01-12 09:25:01 -05:00
|
|
|
int osock = socket(domain, type, protocol);
|
2008-07-09 05:55:09 -04:00
|
|
|
|
2015-01-12 09:25:01 -05:00
|
|
|
#ifdef WINDOWS_SYS
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "unix_socket()" << std::endl;
|
2015-01-12 09:25:01 -05:00
|
|
|
#endif // NET_DEBUG
|
2008-07-09 05:55:09 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
if ((unsigned) osock == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
// Invalidate socket Unix style.
|
|
|
|
osock = -1;
|
|
|
|
errno = WinToUnixError(WSAGetLastError());
|
|
|
|
}
|
2015-01-12 09:25:01 -05:00
|
|
|
#endif // WINDOWS_SYS
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return osock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int unix_fcntl_nonblock(int fd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
#ifndef WINDOWS_SYS // ie UNIX
|
|
|
|
ret = fcntl(fd, F_SETFL, O_NONBLOCK);
|
2008-07-09 05:55:09 -04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "unix_fcntl_nonblock():" << ret << " errno:" << errno << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
#else
|
|
|
|
unsigned long int on = 1;
|
|
|
|
ret = ioctlsocket(fd, FIONBIO, &on);
|
2008-07-09 05:55:09 -04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "unix_fcntl_nonblock()" << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
/* store unix-style error
|
|
|
|
*/
|
|
|
|
ret = -1;
|
|
|
|
errno = WinToUnixError(WSAGetLastError());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-24 01:30:01 -04:00
|
|
|
int unix_connect(int fd, const struct sockaddr *serv_addr, socklen_t socklen)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2014-12-21 14:12:40 -05:00
|
|
|
#ifdef NET_DEBUG
|
2013-09-24 01:30:01 -04:00
|
|
|
std::cerr << "unix_connect()";
|
|
|
|
std::cerr << std::endl;
|
2014-12-21 14:12:40 -05:00
|
|
|
#endif
|
2013-09-24 01:30:01 -04:00
|
|
|
|
|
|
|
const struct sockaddr_storage *ss_addr = (struct sockaddr_storage *) serv_addr;
|
|
|
|
socklen_t len = socklen;
|
|
|
|
|
|
|
|
switch (ss_addr->ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
len = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
len = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > socklen)
|
|
|
|
{
|
|
|
|
std::cerr << "unix_connect() ERROR len > socklen";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
len = socklen;
|
|
|
|
//return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = connect(fd, serv_addr, len);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
#ifdef WINDOWS_SYS // WINDOWS
|
2008-07-09 05:55:09 -04:00
|
|
|
|
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "unix_connect()" << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
errno = WinToUnixError(WSAGetLastError());
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int unix_getsockopt_error(int sockfd, int *err)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
*err = 1;
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
#ifndef WINDOWS_SYS // ie UNIX
|
|
|
|
socklen_t optlen = 4;
|
|
|
|
ret=getsockopt(sockfd, SOL_SOCKET, SO_ERROR, err, &optlen);
|
|
|
|
#else // WINDOWS_SYS
|
|
|
|
int optlen = 4;
|
|
|
|
ret=getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char *) err, &optlen);
|
|
|
|
/* translate */
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "unix_getsockopt_error() returned: " << (int) err << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
if (*err != 0)
|
|
|
|
{
|
|
|
|
*err = WinToUnixError(*err);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|
|
|
|
#ifdef WINDOWS_SYS // ie WINDOWS.
|
|
|
|
|
|
|
|
int WinToUnixError(int error)
|
|
|
|
{
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "WinToUnixError(" << error << ")" << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
switch(error)
|
|
|
|
{
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
return EINPROGRESS;
|
|
|
|
break;
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
return EINPROGRESS;
|
|
|
|
break;
|
|
|
|
case WSAENETUNREACH:
|
|
|
|
return ENETUNREACH;
|
|
|
|
break;
|
|
|
|
case WSAETIMEDOUT:
|
|
|
|
return ETIMEDOUT;
|
|
|
|
break;
|
|
|
|
case WSAEHOSTDOWN:
|
|
|
|
return EHOSTDOWN;
|
|
|
|
break;
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
return ECONNREFUSED;
|
|
|
|
break;
|
|
|
|
case WSAECONNRESET:
|
|
|
|
return ECONNRESET;
|
|
|
|
break;
|
|
|
|
default:
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef NET_DEBUG
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "WinToUnixError(" << error << ") Code Unknown!";
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ECONNREFUSED; /* sensible default? */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
/******************* WINDOWS SPECIFIC PART ******************/
|