Found cause of OSX network resets / lost connections: inet_ntoa is not thread-safe.

* Added thread-safe rs_inet_ntoa function (util/rsnet.cc)
 * Switched all calls to this function (most of the changes)
 * Modified getLocalInterfaces() and getPreferredInterface() to avoid string conversions.
   - NB: Modified windows functions too, but unable to test (hope it compiles!)

Also:
 * Added  EVP_CIPHER_CTX_rand_key() replacement function for old versions of SSL (pre 0.9.8)  (for OSX 10.5)
   - NB: This code should be reworked to remove these calls anyway.
 * Updated tests/pqi to handle above changes.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3281 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2010-07-10 20:34:03 +00:00
parent 0b298f6ca1
commit 648e15bcf7
34 changed files with 360 additions and 235 deletions

View File

@ -30,7 +30,7 @@ std::ostream &operator<<(std::ostream &out, dhtentry &ent)
{
out << "DHTENTRY(" << ent.id << "): Status: " << ent.status;
out << std::endl;
out << "\taddr: " << inet_ntoa(ent.addr.sin_addr) << ":" << ntohs(ent.addr.sin_port);
out << "\taddr: " << rs_inet_ntoa(ent.addr.sin_addr) << ":" << ntohs(ent.addr.sin_port);
out << std::endl;
out << "\tlastTS: " << time(NULL) - ent.lastTs << " secs ago";
out << "\tFlags: " << ent.flags;
@ -522,8 +522,9 @@ int dhthandler::publishOwnId()
/* to store the ip address and flags */
char metalist[1024];
std::string addr = rs_inet_ntoa(ownId.addr.sin_addr),
sprintf(metalist, "rsid=%s:%d;flags=%04X;",
inet_ntoa(ownId.addr.sin_addr),
addr.c_str(),
ntohs(ownId.addr.sin_port),
ownId.flags);

View File

@ -573,7 +573,7 @@ bool OpenDHTClient::openDHT_sendMessage(std::string msg, std::string &response)
#ifdef OPENDHT_DEBUG
std::cerr << "OpenDHTClient::openDHT_sendMessage()";
std::cerr << " Connecting to:" << host << ":" << port;
std::cerr << " (" << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << ")";
std::cerr << " (" << rs_inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << ")";
std::cerr << std::endl;
#endif
@ -724,7 +724,7 @@ bool OpenDHTClient::openDHT_getDHTList(std::string &response)
#ifdef OPENDHT_DEBUG
std::cerr << "OpenDHTClient::openDHT_getDHTList()";
std::cerr << " Connecting to:" << host << ":" << port;
std::cerr << " (" << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << ")";
std::cerr << " (" << rs_inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << ")";
std::cerr << std::endl;
#endif

View File

@ -27,7 +27,7 @@
* #define FT_DEBUG 1
*****/
#define FT_DEBUG 1
//#define FT_DEBUG 1
#include "fttransfermodule.h"
/*************************************************************************

View File

@ -193,7 +193,8 @@ mac {
SOURCES += upnp/upnputil.c
# Beautiful Hack to fix 64bit file access.
QMAKE_CXXFLAGS *= -Dfseeko64=fseeko -Dftello64=ftello -Dfopen64=fopen
QMAKE_CXXFLAGS *= -Dfseeko64=fseeko -Dftello64=ftello -Dfopen64=fopen -Dvstatfs64=vstatfs
UPNPC_DIR = ../../../miniupnpc-1.0
GPG_ERROR_DIR = ../../../../libgpg-error-1.7
GPGME_DIR = ../../../../gpgme-1.1.8

View File

@ -72,6 +72,8 @@ const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 4;
* #define CONN_DEBUG_TICK 1
***/
#define CONN_DEBUG_RESET 1
/****
* #define P3CONNMGR_NO_TCP_CONNECTIONS 1
***/
@ -129,9 +131,9 @@ std::string textPeerConnectState(peerConnectState &state)
out << "Id: " << state.id << std::endl;
out << "NetMode: " << state.netMode << std::endl;
out << "VisState: " << state.visState << std::endl;
out << "laddr: " << inet_ntoa(state.currentlocaladdr.sin_addr)
out << "laddr: " << rs_inet_ntoa(state.currentlocaladdr.sin_addr)
<< ":" << ntohs(state.currentlocaladdr.sin_port) << std::endl;
out << "eaddr: " << inet_ntoa(state.currentserveraddr.sin_addr)
out << "eaddr: " << rs_inet_ntoa(state.currentserveraddr.sin_addr)
<< ":" << ntohs(state.currentserveraddr.sin_port) << std::endl;
std::string output = out.str();
@ -161,8 +163,8 @@ void pqiNetStatus::print(std::ostream &out)
out << " mDhtOk: " << mDhtOk;
out << " mResetReq: " << mResetReq;
out << std::endl;
out << "mLocalAddr: " << inet_ntoa(mLocalAddr.sin_addr) << ":" << ntohs(mLocalAddr.sin_port) << " ";
out << "mExtAddr: " << inet_ntoa(mExtAddr.sin_addr) << ":" << ntohs(mExtAddr.sin_port) << " ";
out << "mLocalAddr: " << rs_inet_ntoa(mLocalAddr.sin_addr) << ":" << ntohs(mLocalAddr.sin_port) << " ";
out << "mExtAddr: " << rs_inet_ntoa(mExtAddr.sin_addr) << ":" << ntohs(mExtAddr.sin_port) << " ";
out << " NetOk: " << NetOk();
out << std::endl;
}
@ -842,7 +844,7 @@ void p3ConnectMgr::netUpnpCheck()
{
#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET)
std::cerr << "p3ConnectMgr::netUpnpCheck() ";
std::cerr << "UpnpAddr: " << inet_ntoa(extAddr.sin_addr);
std::cerr << "UpnpAddr: " << rs_inet_ntoa(extAddr.sin_addr);
std::cerr << ":" << ntohs(extAddr.sin_port);
std::cerr << std::endl;
#endif
@ -930,7 +932,7 @@ void p3ConnectMgr::netExtCheck()
tmpip.sin_port = mNetFlags.mLocalAddr.sin_port;
#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET)
std::cerr << "p3ConnectMgr::netExtCheck() ";
std::cerr << "ExtAddr: " << inet_ntoa(tmpip.sin_addr);
std::cerr << "ExtAddr: " << rs_inet_ntoa(tmpip.sin_addr);
std::cerr << ":" << ntohs(tmpip.sin_port);
std::cerr << std::endl;
#endif
@ -955,7 +957,7 @@ void p3ConnectMgr::netExtCheck()
#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET)
std::cerr << "p3ConnectMgr::netExtCheck() ";
std::cerr << "ExtAddr: " << inet_ntoa(mNetFlags.mExtAddr.sin_addr);
std::cerr << "ExtAddr: " << rs_inet_ntoa(mNetFlags.mExtAddr.sin_addr);
std::cerr << ":" << ntohs(mNetFlags.mExtAddr.sin_port);
std::cerr << std::endl;
#endif
@ -1637,7 +1639,7 @@ bool p3ConnectMgr::connectAttempt(std::string id, struct sockaddr_in &addr,
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::connectAttempt() found an address: id: " << id << std::endl;
std::cerr << " laddr: " << inet_ntoa(addr.sin_addr) << " lport: " << ntohs(addr.sin_port) << " delay: " << delay << " period: " << period;
std::cerr << " laddr: " << rs_inet_ntoa(addr.sin_addr) << " lport: " << ntohs(addr.sin_port) << " delay: " << delay << " period: " << period;
std::cerr << " type: " << type << std::endl;
#endif
if (addr.sin_addr.s_addr == 0 || addr.sin_port == 0) {
@ -2035,13 +2037,13 @@ void p3ConnectMgr::peerConnectRequest(std::string id, struct sockaddr_in radd
uint32_t source)
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::peerConnectRequest() id: " << id << " raddr: " << inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port);
std::cerr << "p3ConnectMgr::peerConnectRequest() id: " << id << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port);
std::cerr << " source: " << source << std::endl;
#endif
{
/* Log */
std::ostringstream out;
out << "p3ConnectMgr::peerConnectRequest() id: " << id << " raddr: " << inet_ntoa(raddr.sin_addr);
out << "p3ConnectMgr::peerConnectRequest() id: " << id << " raddr: " << rs_inet_ntoa(raddr.sin_addr);
out << ":" << ntohs(raddr.sin_port) << " source: " << source;
rslog(RSL_WARNING, p3connectzone, out.str());
}
@ -2364,7 +2366,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id)
{
#ifdef CONN_DEBUG
std::cerr << "Adding tcp connection attempt: ";
std::cerr << "Current Local Addr: " << inet_ntoa(it->second.currentlocaladdr.sin_addr);
std::cerr << "Current Local Addr: " << rs_inet_ntoa(it->second.currentlocaladdr.sin_addr);
std::cerr << ":" << ntohs(it->second.currentlocaladdr.sin_port);
std::cerr << std::endl;
#endif
@ -2381,7 +2383,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id)
{
#ifdef CONN_DEBUG
std::cerr << "Adding tcp connection attempt: ";
std::cerr << "Current Ext Addr: " << inet_ntoa(it->second.currentserveraddr.sin_addr);
std::cerr << "Current Ext Addr: " << rs_inet_ntoa(it->second.currentserveraddr.sin_addr);
std::cerr << ":" << ntohs(it->second.currentserveraddr.sin_port);
std::cerr << std::endl;
#endif
@ -2402,7 +2404,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id)
{
#ifdef CONN_DEBUG
std::cerr << "Adding tcp connection attempt: ";
std::cerr << "Local Addr: " << inet_ntoa(ait->mAddr.sin_addr);
std::cerr << "Local Addr: " << rs_inet_ntoa(ait->mAddr.sin_addr);
std::cerr << ":" << ntohs(ait->mAddr.sin_port);
std::cerr << std::endl;
#endif
@ -2420,7 +2422,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id)
{
#ifdef CONN_DEBUG
std::cerr << "Adding tcp connection attempt: ";
std::cerr << "Ext Addr: " << inet_ntoa(ait->mAddr.sin_addr);
std::cerr << "Ext Addr: " << rs_inet_ntoa(ait->mAddr.sin_addr);
std::cerr << ":" << ntohs(ait->mAddr.sin_port);
std::cerr << std::endl;
#endif
@ -2445,7 +2447,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id)
{
#ifdef CONN_DEBUG
std::cerr << "Adding tcp connection attempt: ";
std::cerr << "DynDNS Addr: " << inet_ntoa(addr);
std::cerr << "DynDNS Addr: " << rs_inet_ntoa(addr);
std::cerr << ":" << ntohs(port);
std::cerr << std::endl;
#endif
@ -2817,25 +2819,58 @@ bool p3ConnectMgr::setVisState(std::string id, uint32_t visState)
bool p3ConnectMgr::checkNetAddress()
{
struct in_addr prefAddr = getPreferredInterface();
bool addrChanged = false;
bool validAddr = false;
struct in_addr prefAddr;
struct sockaddr_in oldAddr;
bool addrChanged = false;
validAddr = getPreferredInterface(prefAddr);
/* if we don't have a valid address - reset */
if (!validAddr)
{
#ifdef CONN_DEBUG_RESET
std::cerr << "p3ConnectMgr::checkNetAddress() no Valid Network Address, resetting network." << std::endl;
std::cerr << std::endl;
#endif
netReset();
IndicateConfigChanged();
return false;
}
/* check addresses */
{
RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/
oldAddr = mOwnState.currentlocaladdr;
addrChanged = (prefAddr.s_addr != mOwnState.currentlocaladdr.sin_addr.s_addr);
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::checkNetAddress()";
std::cerr << std::endl;
std::cerr << "Current Local: " << inet_ntoa(mOwnState.currentlocaladdr.sin_addr);
std::cerr << "Current Local: " << rs_inet_ntoa(mOwnState.currentlocaladdr.sin_addr);
std::cerr << ":" << ntohs(mOwnState.currentlocaladdr.sin_port);
std::cerr << std::endl;
std::cerr << "Current Preferred: " << inet_ntoa(prefAddr);
std::cerr << "Current Preferred: " << rs_inet_ntoa(prefAddr);
std::cerr << std::endl;
#endif
oldAddr = mOwnState.currentlocaladdr;
addrChanged = (prefAddr.s_addr != mOwnState.currentlocaladdr.sin_addr.s_addr);
#ifdef CONN_DEBUG_RESET
if (addrChanged)
{
std::cerr << "p3ConnectMgr::checkNetAddress() Address Changed!";
std::cerr << std::endl;
std::cerr << "Current Local: " << rs_inet_ntoa(mOwnState.currentlocaladdr.sin_addr);
std::cerr << ":" << ntohs(mOwnState.currentlocaladdr.sin_port);
std::cerr << std::endl;
std::cerr << "Current Preferred: " << rs_inet_ntoa(prefAddr);
std::cerr << std::endl;
}
#endif
// update address.
mOwnState.currentlocaladdr.sin_addr = prefAddr;
@ -2897,7 +2932,7 @@ bool p3ConnectMgr::checkNetAddress()
mOwnState.ipAddrs.mLocal.updateIpAddressList(addrInfo);
#ifdef CONN_DEBUG_TICK
std::cerr << "p3ConnectMgr::checkNetAddress() Final Local Address: " << inet_ntoa(mOwnState.currentlocaladdr.sin_addr);
std::cerr << "p3ConnectMgr::checkNetAddress() Final Local Address: " << rs_inet_ntoa(mOwnState.currentlocaladdr.sin_addr);
std::cerr << ":" << ntohs(mOwnState.currentlocaladdr.sin_port) << std::endl;
std::cerr << "p3ConnectMgr::checkNetAddress() Addres History: ";
std::cerr << std::endl;

View File

@ -203,9 +203,9 @@ bool p3DhtMgr::setExternalInterface(
#ifdef DHT_DEBUG
std::cerr << "p3DhtMgr::setExternalInterface()";
std::cerr << " laddr: " << inet_ntoa(ownEntry.laddr.sin_addr);
std::cerr << " laddr: " << rs_inet_ntoa(ownEntry.laddr.sin_addr);
std::cerr << " lport: " << ntohs(ownEntry.laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(ownEntry.raddr.sin_addr);
std::cerr << " raddr: " << rs_inet_ntoa(ownEntry.raddr.sin_addr);
std::cerr << " rport: " << ntohs(ownEntry.raddr.sin_port);
std::cerr << " type: " << ownEntry.type;
std::cerr << " state: " << ownEntry.state;
@ -215,9 +215,9 @@ bool p3DhtMgr::setExternalInterface(
/* Log External Interface too */
std::ostringstream out;
out << "p3DhtMgr::setExternalInterface()";
out << " laddr: " << inet_ntoa(ownEntry.laddr.sin_addr);
out << " laddr: " << rs_inet_ntoa(ownEntry.laddr.sin_addr);
out << " lport: " << ntohs(ownEntry.laddr.sin_port);
out << " raddr: " << inet_ntoa(ownEntry.raddr.sin_addr);
out << " raddr: " << rs_inet_ntoa(ownEntry.raddr.sin_addr);
out << " rport: " << ntohs(ownEntry.raddr.sin_port);
out << " type: " << ownEntry.type;
out << " state: " << ownEntry.state;
@ -608,9 +608,9 @@ int p3DhtMgr::checkOwnDHTKeys()
#ifdef DHT_DEBUG
std::cerr << "PUBLISH: ";
std::cerr << " hash1: " << RsUtil::BinToHex(peer.hash1);
std::cerr << " laddr: " << inet_ntoa(peer.laddr.sin_addr);
std::cerr << " laddr: " << rs_inet_ntoa(peer.laddr.sin_addr);
std::cerr << ":" << ntohs(peer.laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(peer.raddr.sin_addr);
std::cerr << " raddr: " << rs_inet_ntoa(peer.raddr.sin_addr);
std::cerr << ":" << ntohs(peer.raddr.sin_port);
std::cerr << " type: " << peer.type;
std::cerr << std::endl;
@ -621,9 +621,9 @@ int p3DhtMgr::checkOwnDHTKeys()
std::ostringstream out;
out << "p3DhtMgr::checkOwnDHTKeys() PUBLISH OWN ADDR:";
out << " hash1: " << RsUtil::BinToHex(peer.hash1);
out << " laddr: " << inet_ntoa(peer.laddr.sin_addr);
out << " laddr: " << rs_inet_ntoa(peer.laddr.sin_addr);
out << " :" << ntohs(peer.laddr.sin_port);
out << " raddr: " << inet_ntoa(peer.raddr.sin_addr);
out << " raddr: " << rs_inet_ntoa(peer.raddr.sin_addr);
out << ":" << ntohs(peer.raddr.sin_port);
out << " type: " << peer.type;
@ -1358,9 +1358,9 @@ bool p3DhtMgr::dhtPublish(std::string idhash,
std::cerr << "p3DhtMgr::dhtPublish()" << std::endl;
std::cerr << "PUBLISHing: idhash: " << RsUtil::BinToHex(idhash);
std::cerr << " laddr: " << inet_ntoa(laddr.sin_addr);
std::cerr << " laddr: " << rs_inet_ntoa(laddr.sin_addr);
std::cerr << ":" << ntohs(laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr);
std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr);
std::cerr << ":" << ntohs(raddr.sin_port);
std::cerr << " type: " << type;
std::cerr << " sign: " << sign;
@ -1372,17 +1372,17 @@ bool p3DhtMgr::dhtPublish(std::string idhash,
std::ostringstream out;
out << "RSDHT:" << std::setw(2) << std::setfill('0') << DHT_MODE_SEARCH << ": ";
out << "IPL=" << inet_ntoa(laddr.sin_addr) << ":" << ntohs(laddr.sin_port) << ", ";
out << "IPE=" << inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port) << ", ";
out << "IPL=" << rs_inet_ntoa(laddr.sin_addr) << ":" << ntohs(laddr.sin_port) << ", ";
out << "IPE=" << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port) << ", ";
out << "type=" << std::setw(4) << std::setfill('0') << std::hex << type << ", ";
/*******
char valuearray[1024];
snprintf(valuearray, 1024, "RSDHT:%02d: IPL=%s:%d, IPE=%s:%d, type=%04X,",
DHT_MODE_SEARCH,
inet_ntoa(laddr.sin_addr),
rs_inet_ntoa(laddr.sin_addr),
ntohs(laddr.sin_port),
inet_ntoa(raddr.sin_addr),
rs_inet_ntoa(raddr.sin_addr),
ntohs(raddr.sin_port),
type);
@ -1712,9 +1712,9 @@ bool p3DhtMgr::dhtResultSearch(std::string idhash,
/* Log */
std::ostringstream out;
out << "p3DhtMgr::dhtSearchResult() for Id: " << it->first;
out << " laddr: " << inet_ntoa(laddr.sin_addr);
out << " laddr: " << rs_inet_ntoa(laddr.sin_addr);
out << ":" << ntohs(laddr.sin_port);
out << " raddr: " << inet_ntoa(raddr.sin_addr);
out << " raddr: " << rs_inet_ntoa(raddr.sin_addr);
out << ":" << ntohs(raddr.sin_port);
out << " type: " << ownEntry.type;
@ -1798,8 +1798,8 @@ void printDhtPeerEntry(dhtPeerEntry *ent, std::ostream &out)
out << " notifyPending: " << ent->notifyPending;
out << " notifyTS: " << ent->notifyTS;
out << std::endl;
out << " laddr: " << inet_ntoa(ent->laddr.sin_addr) << ":" << ntohs(ent->laddr.sin_port);
out << " raddr: " << inet_ntoa(ent->raddr.sin_addr) << ":" << ntohs(ent->raddr.sin_port);
out << " laddr: " << rs_inet_ntoa(ent->laddr.sin_addr) << ":" << ntohs(ent->laddr.sin_port);
out << " raddr: " << rs_inet_ntoa(ent->raddr.sin_addr) << ":" << ntohs(ent->raddr.sin_port);
out << " type: " << ent->type;
out << std::endl;
out << " hash1: " << RsUtil::BinToHex(ent->hash1);

View File

@ -27,6 +27,8 @@
#include "pqi/pqibin.h"
#include "util/rsnet.h"
BinFileInterface::BinFileInterface(const char *fname, int flags)
:bin_flags(flags), buf(NULL), hash(NULL), bcount(0)
@ -376,7 +378,7 @@ NetBinDummy::NetBinDummy(PQInterface *parent, std::string id, uint32_t t)
int NetBinDummy::connect(struct sockaddr_in raddr)
{
std::cerr << "NetBinDummy::connect(";
std::cerr << inet_ntoa(raddr.sin_addr) << ":";
std::cerr << rs_inet_ntoa(raddr.sin_addr) << ":";
std::cerr << htons(raddr.sin_port);
std::cerr << ") ";
printNetBinID(std::cerr, PeerId(), type);

View File

@ -198,7 +198,7 @@ void pqiIpAddrList::printIpAddressList(std::ostream &out) const
time_t now = time(NULL);
for(it = mAddrs.begin(); it != mAddrs.end(); it++)
{
out << inet_ntoa(it->mAddr.sin_addr) << ":"
out << rs_inet_ntoa(it->mAddr.sin_addr) << ":"
<< ntohs(it->mAddr.sin_port) << " ( "
<< now - it->mSeenTime << " old) " << std::endl;

View File

@ -65,7 +65,7 @@ void pqiConnectCbDummy::peerConnectRequest(std::string id,
{
std::cerr << "pqiConnectCbDummy::peerConnectRequest()";
std::cerr << " id: " << id;
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr);
std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr);
std::cerr << ":" << ntohs(raddr.sin_port);
std::cerr << " source: " << source;
std::cerr << std::endl;
@ -76,7 +76,7 @@ void pqiConnectCbDummy::stunStatus(std::string id, struct sockaddr_in raddr,
uint32_t type, uint32_t flags)
{
std::cerr << "pqiConnectCbDummy::stunStatus()";
std::cerr << " idhash: " << RsUtil::BinToHex(id) << " raddr: " << inet_ntoa(raddr.sin_addr);
std::cerr << " idhash: " << RsUtil::BinToHex(id) << " raddr: " << rs_inet_ntoa(raddr.sin_addr);
std::cerr << ":" << ntohs(raddr.sin_port);
std::cerr << " type: " << type;
std::cerr << " flags: " << flags;

View File

@ -128,10 +128,8 @@ std::string socket_errorType(int err)
#include <net/if.h>
#include <sys/ioctl.h>
std::list<std::string> getLocalInterfaces()
bool getLocalInterfaces(std::list<struct in_addr> &addrs)
{
std::list<std::string> addrs;
int sock = 0;
struct ifreq ifreq;
@ -143,17 +141,17 @@ std::list<std::string> getLocalInterfaces()
{
pqioutput(PQL_ALERT, pqinetzone,
"Cannot Determine Local Addresses!");
return addrs;
return false;
}
if (!ifptr)
{
pqioutput(PQL_ALERT, pqinetzone,
"getLocalInterfaces(): ERROR if_nameindex == NULL");
return false;
}
// loop through the interfaces.
//for(; *(char *)ifptr != 0; ifptr++)
for(; ifptr->if_index != 0; ifptr++)
{
//copy in the interface name to look up address of
@ -170,7 +168,7 @@ std::list<std::string> getLocalInterfaces()
{
struct sockaddr_in *aptr =
(struct sockaddr_in *) &ifreq.ifr_addr;
const char *astr=inet_ntoa(aptr -> sin_addr);
std::string astr =rs_inet_ntoa(aptr -> sin_addr);
std::ostringstream out;
out << "Iface: ";
@ -193,14 +191,14 @@ std::list<std::string> getLocalInterfaces()
if((ifreq.ifr_flags & IFF_UP) == 0) continue ;
if((ifreq.ifr_flags & IFF_RUNNING) == 0) continue ;
addrs.push_back(astr);
addrs.push_back(aptr->sin_addr);
}
}
// free socket -> or else run out of fds.
close(sock);
if_freenameindex(iflist);
return addrs;
return (addrs.size() > 0);
}
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
@ -317,10 +315,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.
std::list<std::string> getLocalInterfaces()
bool getLocalInterfaces(std::list<struct in_addr> &addrs)
{
std::list<std::string> addrs;
/* USE MIB IPADDR Interface */
PMIB_IPADDRTABLE iptable = NULL;
@ -345,16 +341,16 @@ std::list<std::string> getLocalInterfaces()
out << "Iface(" << iptable->table[i].dwIndex << ") ";
addr.s_addr = iptable->table[i].dwAddr;
out << " => " << inet_ntoa(addr);
out << " => " << rs_inet_ntoa(addr);
out << std::endl;
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out.str());
addrs.push_back(inet_ntoa(addr));
addrs.push_back(addr.sin_addr));
}
free (iptable);
return addrs;
return (addrs.size() > 0);
}
// implement the improved unix inet address fn.
@ -383,7 +379,7 @@ in_addr_t inet_network(const char *inet_name)
{
#ifdef NET_DEBUG
// std::cerr << "inet_network(" << inet_name << ") : ";
// std::cerr << inet_ntoa(addr) << std::endl;
// std::cerr << rs_inet_ntoa(addr) << std::endl;
#endif
return ntohl(inet_netof(addr));
}
@ -411,7 +407,7 @@ in_addr_t pqi_inet_netof(struct in_addr addr)
unsigned long cbit = haddr & 0xffffff00UL;
#ifdef NET_DEBUG
std::cerr << "inet_netof(" << inet_ntoa(addr) << ") ";
std::cerr << "inet_netof(" << rs_inet_ntoa(addr) << ") ";
#endif
if (!((haddr >> 31) | 0x0UL)) // MSB = 0
@ -487,9 +483,9 @@ int inaddr_cmp(struct sockaddr_in addr1, struct sockaddr_in addr2 )
{
#ifdef NET_DEBUG
std::ostringstream out;
out << "inaddr_cmp(" << inet_ntoa(addr1.sin_addr);
out << "inaddr_cmp(" << rs_inet_ntoa(addr1.sin_addr);
out << "-" << addr1.sin_addr.s_addr;
out << "," << inet_ntoa(addr2.sin_addr);
out << "," << rs_inet_ntoa(addr2.sin_addr);
out << "-" << addr2.sin_addr.s_addr << ")" << std::endl;
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out.str());
#endif
@ -511,8 +507,8 @@ int inaddr_cmp(struct sockaddr_in addr1, unsigned long addr2)
inaddr_tmp.s_addr = addr2;
std::ostringstream out;
out << "inaddr_cmp2(" << inet_ntoa(addr1.sin_addr);
out << " vs " << inet_ntoa(inaddr_tmp);
out << "inaddr_cmp2(" << rs_inet_ntoa(addr1.sin_addr);
out << " vs " << rs_inet_ntoa(inaddr_tmp);
out << " /or/ ";
out << std::hex << std::setw(10) << addr1.sin_addr.s_addr;
out << " vs " << std::setw(10) << addr2 << ")" << std::endl;
@ -529,11 +525,10 @@ int inaddr_cmp(struct sockaddr_in addr1, unsigned long addr2)
}
struct in_addr getPreferredInterface() // returns best addr.
bool getPreferredInterface(struct in_addr &prefAddr) // returns best addr.
{
std::list<std::string> addrs = getLocalInterfaces();
std::list<std::string>::iterator it;
std::list<struct in_addr> addrs;
std::list<struct in_addr>::iterator it;
struct in_addr addr_zero = {0}, addr_loop = {0}, addr_priv = {0}, addr_ext = {0}, addr = {0};
bool found_zero = false;
@ -541,22 +536,38 @@ struct in_addr getPreferredInterface() // returns best addr.
bool found_priv = false;
bool found_ext = false;
if (!getLocalInterfaces(addrs))
{
return false;
}
// find the first of each of these.
// if ext - take first.
// if no ext -> first priv
// if no priv -> first loopback.
#ifdef NET_DEBUG
std::cerr << "getPreferredInterface() " << addrs.size() << " interfaces." << std::endl;
#endif
for(it = addrs.begin(); it != addrs.end(); it++)
{
inet_aton((*it).c_str(), &addr);
struct in_addr addr = *it;
//std::cout << "Examining addr = " << (void*)addr.s_addr << std::endl ;
#ifdef NET_DEBUG
std::cerr << "Examining addr: " << rs_inet_ntoa(addr);
std::cerr << " => " << (uint32_t) addr.s_addr << std::endl ;
#endif
// for windows silliness (returning 0.0.0.0 as valid addr!).
if (addr.s_addr == 0)
{
if (!found_zero)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Zero Address" << std::endl ;
#endif
found_zero = true;
addr_zero = addr;
}
@ -565,6 +576,10 @@ struct in_addr getPreferredInterface() // returns best addr.
{
if (!found_loopback)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Loopback Address" << std::endl ;
#endif
found_loopback = true;
addr_loop = addr;
}
@ -573,6 +588,10 @@ struct in_addr getPreferredInterface() // returns best addr.
{
if (!found_priv)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Private Address" << std::endl ;
#endif
found_priv = true;
addr_priv = addr;
}
@ -582,7 +601,7 @@ struct in_addr getPreferredInterface() // returns best addr.
if (!found_ext)
{
#ifdef NET_DEBUG
std::cerr << "Found external address " << (void*)addr.s_addr << std::endl ;
std::cerr << "\tFound Other Address (Ext?) " << std::endl ;
#endif
found_ext = true;
addr_ext = addr;
@ -591,30 +610,42 @@ struct in_addr getPreferredInterface() // returns best addr.
}
if(found_ext) // external address is best.
return addr_ext ;
{
prefAddr = addr_ext;
return true;
}
if (found_priv)
return addr_priv;
{
prefAddr = addr_priv;
return true;
}
// next bit can happen under windows,
// a general address is still
// preferable to a loopback device.
if (found_zero)
return addr_zero;
{
prefAddr = addr_zero;
return true;
}
if (found_loopback)
return addr_loop;
{
prefAddr = addr_loop;
return true;
}
// shound be 255.255.255.255 (error).
addr.s_addr = 0xffffffff;
return addr;
prefAddr.s_addr = 0xffffffff;
return false;
}
bool sameNet(struct in_addr *addr, struct in_addr *addr2)
{
#ifdef NET_DEBUG
std::cerr << "sameNet: " << inet_ntoa(*addr);
std::cerr << " VS " << inet_ntoa(*addr2);
std::cerr << "sameNet: " << rs_inet_ntoa(*addr);
std::cerr << " VS " << rs_inet_ntoa(*addr2);
std::cerr << std::endl;
#endif
struct in_addr addrnet, addrnet2;
@ -623,8 +654,8 @@ bool sameNet(struct in_addr *addr, struct in_addr *addr2)
addrnet2.s_addr = inet_netof(*addr2);
#ifdef NET_DEBUG
std::cerr << " (" << inet_ntoa(addrnet);
std::cerr << " =?= " << inet_ntoa(addrnet2);
std::cerr << " (" << rs_inet_ntoa(addrnet);
std::cerr << " =?= " << rs_inet_ntoa(addrnet2);
std::cerr << ")" << std::endl;
#endif
@ -703,7 +734,7 @@ bool LookupDNSAddr(std::string name, struct sockaddr_in &addr)
#ifdef NET_DEBUG
std::cerr << "LookupDNSAddr() getaddrinfo found address" << std::endl;
std::cerr << "addr: " << inet_ntoa(addr.sin_addr) << std::endl;
std::cerr << "addr: " << rs_inet_ntoa(addr.sin_addr) << std::endl;
std::cerr << "port: " << ntohs(addr.sin_port) << std::endl;
#endif
return true;

View File

@ -103,8 +103,8 @@ int sockaddr_cmp(struct sockaddr_in &addr1, struct sockaddr_in &addr2 );
int inaddr_cmp(struct sockaddr_in addr1, struct sockaddr_in addr2 );
int inaddr_cmp(struct sockaddr_in addr1, unsigned long);
struct in_addr getPreferredInterface(); // returns best addr.
std::list<std::string> getLocalInterfaces(); // returns all possible addrs.
bool getPreferredInterface(struct in_addr &prefAddr); // returns best addr.
bool getLocalInterfaces(std::list<struct in_addr> &addrs); // returns all possible addrs.
// checks (addr1 & 255.255.255.0) == (addr2 & 255.255.255.0)
bool isSameSubnet(struct in_addr *addr1, struct in_addr *addr2);

View File

@ -384,7 +384,7 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay,
std::ostringstream out;
out << "pqiperson::connect() Id: " << PeerId();
out << " type: " << type;
out << " addr: " << inet_ntoa(raddr.sin_addr);
out << " addr: " << rs_inet_ntoa(raddr.sin_addr);
out << ":" << ntohs(raddr.sin_port);
out << " delay: " << delay;
out << " period: " << period;

View File

@ -447,7 +447,7 @@ int pqipersongrp::connectPeer(std::string id)
#ifdef PGRP_DEBUG
std::cerr << " pqipersongrp::connectPeer() connectAttempt data id: " << id;
std::cerr << " addr: " << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port);
std::cerr << " addr: " << rs_inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port);
std::cerr << " delay: " << delay;
std::cerr << " period: " << period;
std::cerr << " type: " << type;

View File

@ -601,7 +601,7 @@ int pqissl::Initiate_Connection()
out << "pqissl::Initiate_Connection() ";
out << "Connecting To: ";
out << PeerId() << " via: ";
out << inet_ntoa(addr.sin_addr);
out << rs_inet_ntoa(addr.sin_addr);
out << ":" << ntohs(addr.sin_port);
rslog(RSL_WARNING, pqisslzone, out.str());
}
@ -1267,9 +1267,9 @@ int pqissl::accept(SSL *ssl, int fd, struct sockaddr_in foreign_addr) // initiat
out << std::endl;
out << "\t\tchecking for same LAN";
out << std::endl;
out << "\t localaddr: " << inet_ntoa(details.currentlocaladdr.sin_addr);
out << "\t localaddr: " << rs_inet_ntoa(details.currentlocaladdr.sin_addr);
out << std::endl;
out << "\t remoteaddr: " << inet_ntoa(remote_addr.sin_addr);
out << "\t remoteaddr: " << rs_inet_ntoa(remote_addr.sin_addr);
out << std::endl;
if (sameLAN)
{

View File

@ -165,7 +165,7 @@ int pqissllistenbase::setuplisten()
out << "pqissllistenbase::setuplisten()";
out << "\tAddress Family: " << (int) laddr.sin_family;
out << std::endl;
out << "\tSetup Address: " << inet_ntoa(laddr.sin_addr);
out << "\tSetup Address: " << rs_inet_ntoa(laddr.sin_addr);
out << std::endl;
out << "\tSetup Port: " << ntohs(laddr.sin_port);
@ -339,7 +339,7 @@ int pqissllistenbase::acceptconnection()
{
std::ostringstream out;
out << "Accepted Connection from ";
out << inet_ntoa(remote_addr.sin_addr) << ":" << ntohs(remote_addr.sin_port);
out << rs_inet_ntoa(remote_addr.sin_addr) << ":" << ntohs(remote_addr.sin_port);
pqioutput(PQL_DEBUG_BASIC, pqissllistenzone, out.str());
}
@ -676,7 +676,7 @@ int pqissllistener::completeConnection(int fd, SSL *ssl, struct sockaddr_in &rem
{
std::ostringstream out;
out << "No Matching Certificate";
out << " for Connection:" << inet_ntoa(remote_addr.sin_addr);
out << " for Connection:" << rs_inet_ntoa(remote_addr.sin_addr);
out << std::endl;
out << "pqissllistenbase: Will shut it down!" << std::endl;
pqioutput(PQL_WARNING, pqissllistenzone, out.str());

View File

@ -158,7 +158,7 @@ int pqissludp::Initiate_Connection()
std::ostringstream out;
out << "pqissludp::Initiate_Connection() ";
out << "Connecting To: " << PeerId();
out << " via: " << inet_ntoa(remote_addr.sin_addr) << ":";
out << " via: " << rs_inet_ntoa(remote_addr.sin_addr) << ":";
out << ntohs(remote_addr.sin_port) << " ";
if (sslmode)
{

View File

@ -48,6 +48,20 @@
***/
/********************************************************************************/
#if defined(SSLFNS_ADD_CIPHER_CTX_RAND_KEY)
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
{
//if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
// return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
if (RAND_bytes(key, ctx->key_len) <= 0)
return 0;
return 1;
}
#endif
/********************************************************************************/
/********************************************************************************/

View File

@ -35,6 +35,7 @@
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/ssl.h>
#include <inttypes.h>
#include <string>
@ -47,6 +48,21 @@
/********************************************************************************/
/********************************************************************************/
// IF we are compiling against ssl0.9.7 - these functions don't exist.
#if !defined(EVP_CIPHER_CTX_rand_key)
#define SSLFNS_ADD_CIPHER_CTX_RAND_KEY 1
#endif
#if defined(SSLFNS_ADD_CIPHER_CTX_RAND_KEY)
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
#endif
X509_REQ *GenerateX509Req(
std::string pkey_file, std::string passwd,
std::string name, std::string email, std::string org,

View File

@ -110,13 +110,13 @@ int RsServer::UpdateAllConfig()
mConnMgr->getOwnNetStatus(pstate);
/* ports */
config.localAddr = inet_ntoa(pstate.currentlocaladdr.sin_addr);
config.localAddr = rs_inet_ntoa(pstate.currentlocaladdr.sin_addr);
config.localPort = ntohs(pstate.currentlocaladdr.sin_port);
config.firewalled = true;
config.forwardPort = true;
config.extAddr = inet_ntoa(pstate.currentserveraddr.sin_addr);
config.extAddr = rs_inet_ntoa(pstate.currentserveraddr.sin_addr);
config.extPort = ntohs(pstate.currentserveraddr.sin_port);
/* data rates */

View File

@ -257,6 +257,7 @@ bool p3Peers::isFriend(std::string ssl_id)
return mConnMgr->isFriend(ssl_id);
}
#if 0
static struct sockaddr_in getPreferredAddress( const struct sockaddr_in& addr1,time_t ts1,
const struct sockaddr_in& addr2,time_t ts2,
const struct sockaddr_in& addr3,time_t ts3)
@ -264,11 +265,12 @@ static struct sockaddr_in getPreferredAddress( const struct sockaddr_in& addr1,t
time_t ts = ts1 ;
struct sockaddr_in addr = addr1 ;
if(ts2 > ts && strcmp(inet_ntoa(addr2.sin_addr),"0.0.0.0")) { ts = ts2 ; addr = addr2 ; }
if(ts3 > ts && strcmp(inet_ntoa(addr3.sin_addr),"0.0.0.0")) { ts = ts3 ; addr = addr3 ; }
if(ts2 > ts && strcmp(rs_inet_ntoa(addr2.sin_addr),"0.0.0.0")) { ts = ts2 ; addr = addr2 ; }
if(ts3 > ts && strcmp(rs_inet_ntoa(addr3.sin_addr),"0.0.0.0")) { ts = ts3 ; addr = addr3 ; }
return addr ;
}
#endif
bool p3Peers::getPeerDetails(std::string id, RsPeerDetails &d)
{
@ -309,9 +311,9 @@ bool p3Peers::getPeerDetails(std::string id, RsPeerDetails &d)
/* fill from pcs */
d.localAddr = inet_ntoa(pcs.currentlocaladdr.sin_addr);
d.localAddr = rs_inet_ntoa(pcs.currentlocaladdr.sin_addr);
d.localPort = ntohs(pcs.currentlocaladdr.sin_port);
d.extAddr = inet_ntoa(pcs.currentserveraddr.sin_addr);
d.extAddr = rs_inet_ntoa(pcs.currentserveraddr.sin_addr);
d.extPort = ntohs(pcs.currentserveraddr.sin_port);
d.dyndns = pcs.dyndns;
d.lastConnect = pcs.lastcontact;
@ -324,14 +326,14 @@ bool p3Peers::getPeerDetails(std::string id, RsPeerDetails &d)
{
std::ostringstream toto;
toto << ntohs(it->mAddr.sin_port) << " " << (time(NULL) - it->mSeenTime) << " sec";
d.ipAddressList.push_back("L:" + std::string(inet_ntoa(it->mAddr.sin_addr)) + ":" + toto.str());
d.ipAddressList.push_back("L:" + std::string(rs_inet_ntoa(it->mAddr.sin_addr)) + ":" + toto.str());
}
for(it = pcs.ipAddrs.mExt.mAddrs.begin();
it != pcs.ipAddrs.mExt.mAddrs.end(); it++)
{
std::ostringstream toto;
toto << ntohs(it->mAddr.sin_port) << " " << (time(NULL) - it->mSeenTime) << " sec";
d.ipAddressList.push_back("E:" + std::string(inet_ntoa(it->mAddr.sin_addr)) + ":" + toto.str());
d.ipAddressList.push_back("E:" + std::string(rs_inet_ntoa(it->mAddr.sin_addr)) + ":" + toto.str());
}
@ -398,9 +400,9 @@ bool p3Peers::getPeerDetails(std::string id, RsPeerDetails &d)
if (pcs.currentConnAddrAttempt.type & RS_NET_CONN_TUNNEL) {
autostr << "Trying tunnel connection";
} else if (pcs.currentConnAddrAttempt.type & RS_NET_CONN_TCP_ALL) {
autostr << "Trying TCP : " << inet_ntoa(pcs.currentConnAddrAttempt.addr.sin_addr) << ":" << ntohs(pcs.currentConnAddrAttempt.addr.sin_port);
autostr << "Trying TCP : " << rs_inet_ntoa(pcs.currentConnAddrAttempt.addr.sin_addr) << ":" << ntohs(pcs.currentConnAddrAttempt.addr.sin_port);
} else if (pcs.currentConnAddrAttempt.type & RS_NET_CONN_UDP_ALL) {
autostr << "Trying UDP : " << inet_ntoa(pcs.currentConnAddrAttempt.addr.sin_addr) << ":" << ntohs(pcs.currentConnAddrAttempt.addr.sin_port);
autostr << "Trying UDP : " << rs_inet_ntoa(pcs.currentConnAddrAttempt.addr.sin_addr) << ":" << ntohs(pcs.currentConnAddrAttempt.addr.sin_port);
}
}
else if (pcs.state & RS_PEER_S_CONNECTED)

View File

@ -786,11 +786,11 @@ std::ostream &RsPeerOldNetItem::print(std::ostream &out, uint16_t indent)
out << "lastContact: " << lastContact << std::endl;
printIndent(out, int_Indent);
out << "currentlocaladdr: " << inet_ntoa(currentlocaladdr.sin_addr);
out << "currentlocaladdr: " << rs_inet_ntoa(currentlocaladdr.sin_addr);
out << ":" << htons(currentlocaladdr.sin_port) << std::endl;
printIndent(out, int_Indent);
out << "currentremoteaddr: " << inet_ntoa(currentremoteaddr.sin_addr);
out << "currentremoteaddr: " << rs_inet_ntoa(currentremoteaddr.sin_addr);
out << ":" << htons(currentremoteaddr.sin_port) << std::endl;
printIndent(out, int_Indent);
@ -800,7 +800,7 @@ std::ostream &RsPeerOldNetItem::print(std::ostream &out, uint16_t indent)
out << "ipAdressList: size : " << ipAddressList.size() << ", adresses : " << std::endl;
for (std::list<IpAddressTimed>::iterator ipListIt = ipAddressList.begin(); ipListIt!=(ipAddressList.end()); ipListIt++) {
printIndent(out, int_Indent);
out << inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl;
out << rs_inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl;
}
printRsItemEnd(out, "RsPeerOldNetItem", indent);
@ -1037,11 +1037,11 @@ std::ostream &RsPeerNetItem::print(std::ostream &out, uint16_t indent)
out << "lastContact: " << lastContact << std::endl;
printIndent(out, int_Indent);
out << "currentlocaladdr: " << inet_ntoa(currentlocaladdr.sin_addr);
out << "currentlocaladdr: " << rs_inet_ntoa(currentlocaladdr.sin_addr);
out << ":" << htons(currentlocaladdr.sin_port) << std::endl;
printIndent(out, int_Indent);
out << "currentremoteaddr: " << inet_ntoa(currentremoteaddr.sin_addr);
out << "currentremoteaddr: " << rs_inet_ntoa(currentremoteaddr.sin_addr);
out << ":" << htons(currentremoteaddr.sin_port) << std::endl;
printIndent(out, int_Indent);

View File

@ -143,7 +143,7 @@ std::ostream &RsTlvIpAddressInfo::print(std::ostream &out, uint16_t indent)
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Address:" << inet_ntoa(addr.sin_addr);
out << "Address:" << rs_inet_ntoa(addr.sin_addr);
out << ":" << htons(addr.sin_port) << std::endl;
printIndent(out, int_Indent);

View File

@ -878,10 +878,10 @@ int p3disc::idServers()
out << "Neighbour: " << (nit->second).id;
out << std::endl;
out << "-> LocalAddr: ";
out << inet_ntoa(nit->second.localAddr.sin_addr);
out << rs_inet_ntoa(nit->second.localAddr.sin_addr);
out << ":" << ntohs(nit->second.localAddr.sin_port) << std::endl;
out << "-> RemoteAddr: ";
out << inet_ntoa(nit->second.remoteAddr.sin_addr);
out << rs_inet_ntoa(nit->second.remoteAddr.sin_addr);
out << ":" << ntohs(nit->second.remoteAddr.sin_port) << std::endl;
out << " Last Contact: ";
out << cts - (nit->second.ts) << " sec ago";
@ -896,11 +896,11 @@ int p3disc::idServers()
out << "\tConnected via: " << (sit->first);
out << std::endl;
out << "\t\tLocalAddr: ";
out << inet_ntoa(sit->second.localAddr.sin_addr);
out << rs_inet_ntoa(sit->second.localAddr.sin_addr);
out <<":"<< ntohs(sit->second.localAddr.sin_port);
out << std::endl;
out << "\t\tRemoteAddr: ";
out << inet_ntoa(sit->second.remoteAddr.sin_addr);
out << rs_inet_ntoa(sit->second.remoteAddr.sin_addr);
out <<":"<< ntohs(sit->second.remoteAddr.sin_port);
out << std::endl;

View File

@ -40,6 +40,7 @@
#include "util/rsdir.h"
#include "pqi/pqinotify.h"
#include "pqi/pqibin.h"
#include "pqi/sslfns.h"
#include "pqi/authssl.h"
#include "pqi/authgpg.h"

View File

@ -69,7 +69,7 @@ int tou_init(const struct sockaddr *my_addr, socklen_t addrlen)
if (!(udps->okay()))
{
std::cerr << "tou_init() FATAL ERROR: Cannot reset Udp Socket to: "
<< inet_ntoa(addr->sin_addr) << ":" << ntohs(addr->sin_port);
<< rs_inet_ntoa(addr->sin_addr) << ":" << ntohs(addr->sin_port);
std::cerr << std::endl;
return 0;

View File

@ -254,7 +254,7 @@ bool UdpSorter::locked_handleStunPkt(void *data, int size, struct sockaddr_in &f
{
#ifdef DEBUG_UDP_SORTER
std::cerr << "UdpSorter::handleStunPkt() got Request from: ";
std::cerr << inet_ntoa(from.sin_addr) << ":" << ntohs(from.sin_port);
std::cerr << rs_inet_ntoa(from.sin_addr) << ":" << ntohs(from.sin_port);
std::cerr << std::endl;
#endif
{
@ -292,7 +292,7 @@ bool UdpSorter::locked_handleStunPkt(void *data, int size, struct sockaddr_in &f
{
#ifdef DEBUG_UDP_SORTER
std::cerr << "UdpSorter::handleStunPkt() got Ext Addr: ";
std::cerr << inet_ntoa(eAddr.sin_addr) << ":" << ntohs(eAddr.sin_port);
std::cerr << rs_inet_ntoa(eAddr.sin_addr) << ":" << ntohs(eAddr.sin_port);
std::cerr << std::endl;
#endif
{
@ -339,7 +339,7 @@ bool UdpSorter::externalAddr(struct sockaddr_in &external, uint8_t &stable)
stable = 0;
#ifdef DEBUG_UDP_SORTER
std::cerr << "UdpSorter::externalAddr() eaddr:" << inet_ntoa(external.sin_addr);
std::cerr << "UdpSorter::externalAddr() eaddr:" << rs_inet_ntoa(external.sin_addr);
std::cerr << ":" << ntohs(external.sin_port) << " stable: " << (int) stable;
std::cerr << std::endl;
#endif
@ -395,9 +395,9 @@ int UdpSorter::doStun(struct sockaddr_in stun_addr)
#ifdef DEBUG_UDP_SORTER
std::ostringstream out;
out << "UdpSorter::doStun() Sent Stun Packet(" << sentlen << ") from:";
out << inet_ntoa(laddr.sin_addr) << ":" << ntohs(laddr.sin_port);
out << rs_inet_ntoa(laddr.sin_addr) << ":" << ntohs(laddr.sin_port);
out << " to:";
out << inet_ntoa(stun_addr.sin_addr) << ":" << ntohs(stun_addr.sin_port);
out << rs_inet_ntoa(stun_addr.sin_addr) << ":" << ntohs(stun_addr.sin_port);
std::cerr << out.str() << std::endl;
@ -436,7 +436,7 @@ bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr)
#ifdef DEBUG_UDP_SORTER
std::ostringstream out;
out << "UdpSorter::response() Recvd a Stun Response, ext_addr: ";
out << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port);
out << rs_inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port);
std::cerr << out.str() << std::endl;
#endif
@ -722,9 +722,9 @@ bool UdpSorter::locked_recvdStun(const struct sockaddr_in &remote, const stru
#ifdef DEBUG_UDP_SORTER
std::ostringstream out;
out << "UdpSorter::locked_recvdStun() from:";
out << inet_ntoa(remote.sin_addr) << ":" << ntohs(remote.sin_port);
out << rs_inet_ntoa(remote.sin_addr) << ":" << ntohs(remote.sin_port);
out << " claiming ExtAddr is:";
out << inet_ntoa(extaddr.sin_addr) << ":" << ntohs(extaddr.sin_port);
out << rs_inet_ntoa(extaddr.sin_addr) << ":" << ntohs(extaddr.sin_port);
std::cerr << out.str() << std::endl;
#endif
@ -841,9 +841,9 @@ bool UdpSorter::locked_printStunList()
std::list<TouStunPeer>::iterator it;
for(it = mStunList.begin(); it != mStunList.end(); it++)
{
out << "id:" << RsUtil::BinToHex(it->id) << " addr: " << inet_ntoa(it->remote.sin_addr);
out << "id:" << RsUtil::BinToHex(it->id) << " addr: " << rs_inet_ntoa(it->remote.sin_addr);
out << ":" << htons(it->remote.sin_port);
out << " eaddr: " << inet_ntoa(it->eaddr.sin_addr);
out << " eaddr: " << rs_inet_ntoa(it->eaddr.sin_addr);
out << ":" << htons(it->eaddr.sin_port);
out << " failCount: " << it->failCount;
out << " lastSend: " << now - it->lastsend;

View File

@ -174,8 +174,8 @@ virtual bool dhtPublish(std::string id,
uint32_t type, std::string sign)
{
std::cerr << "DhtMgrTester::dhtPublish() id: " << RsUtil::BinToHex(id);
std::cerr << " laddr: " << inet_ntoa(laddr.sin_addr) << " lport: " << ntohs(laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr) << " rport: " << ntohs(raddr.sin_port);
std::cerr << " laddr: " << rs_inet_ntoa(laddr.sin_addr) << " lport: " << ntohs(laddr.sin_port);
std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << " rport: " << ntohs(raddr.sin_port);
std::cerr << " type: " << type << " sign: " << sign;
std::cerr << std::endl;

View File

@ -170,8 +170,8 @@ virtual bool dhtPublish(std::string id,
uint32_t type, std::string sign)
{
std::cerr << "DhtMgrTester::dhtPublish() id: " << RsUtil::BinToHex(id);
std::cerr << " laddr: " << inet_ntoa(laddr.sin_addr) << " lport: " << ntohs(laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr) << " rport: " << ntohs(raddr.sin_port);
std::cerr << " laddr: " << rs_inet_ntoa(laddr.sin_addr) << " lport: " << ntohs(laddr.sin_port);
std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << " rport: " << ntohs(raddr.sin_port);
std::cerr << " type: " << type << " sign: " << sign;
std::cerr << std::endl;

View File

@ -112,16 +112,16 @@ bool test_local_address_manipulation()
inet_aton(localnet4_addrstr, &(localnet4_addr.sin_addr));
std::cerr << "Loopback Addr " << inet_ntoa(loopback_addr.sin_addr);
std::cerr << "Loopback Addr " << rs_inet_ntoa(loopback_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet1 Addr " << inet_ntoa(localnet1_addr.sin_addr);
std::cerr << "Localnet1 Addr " << rs_inet_ntoa(localnet1_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet2 Addr " << inet_ntoa(localnet2_addr.sin_addr);
std::cerr << "Localnet2 Addr " << rs_inet_ntoa(localnet2_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet3 Addr " << inet_ntoa(localnet3_addr.sin_addr);
std::cerr << "Localnet3 Addr " << rs_inet_ntoa(localnet3_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet4 Addr " << inet_ntoa(localnet4_addr.sin_addr);
std::cerr << "Localnet4 Addr " << rs_inet_ntoa(localnet4_addr.sin_addr);
std::cerr << std::endl;
std::cerr << std::endl;
@ -137,13 +137,13 @@ bool test_local_address_manipulation()
addr2.sin_addr.s_addr = htonl(inet_network(loopback_addrstr));
std::cerr << "Loopback Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << " -> " << rs_inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << rs_inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << rs_inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr2.sin_addr)));
addr_ans_str = "192.168.0.0";
@ -152,13 +152,13 @@ bool test_local_address_manipulation()
addr2.sin_addr.s_addr = htonl(inet_network(localnet1_addrstr));
std::cerr << "Localnet1 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << " -> " << rs_inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << rs_inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << rs_inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr2.sin_addr)));
addr_ans_str = "10.0.0.0";
inet_aton(addr_ans_str.c_str(), &(addr_ans.sin_addr));
@ -166,14 +166,14 @@ bool test_local_address_manipulation()
addr2.sin_addr.s_addr = htonl(inet_network(localnet2_addrstr));
std::cerr << "Localnet2 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << " -> " << rs_inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << rs_inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << rs_inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr2.sin_addr)));
addr_ans_str = "10.0.0.0";
@ -182,14 +182,14 @@ bool test_local_address_manipulation()
addr2.sin_addr.s_addr = htonl(inet_network(localnet3_addrstr));
std::cerr << "Localnet3 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << " -> " << rs_inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << rs_inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << rs_inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr2.sin_addr)));
addr_ans_str = "192.168.0.0";
@ -198,14 +198,14 @@ bool test_local_address_manipulation()
addr2.sin_addr.s_addr = htonl(inet_network(localnet4_addrstr));
std::cerr << "Localnet4 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << " -> " << rs_inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << rs_inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << rs_inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), rs_inet_ntoa(addr2.sin_addr)));
REPORT("Test Local Address Manipulation");
return true;
@ -264,12 +264,12 @@ bool test_address_listen()
sockaddr_clear(&addr2);
addr2.sin_family = AF_INET;
addr2.sin_addr = getPreferredInterface(); // returns best addr.
getPreferredInterface(addr2.sin_addr); // returns best addr.
addr2.sin_port = htons(13245);
sockaddr_clear(&addr3);
addr3.sin_family = AF_INET;
addr3.sin_addr = getPreferredInterface(); // returns best addr.
getPreferredInterface(addr3.sin_addr); // returns best addr.
addr3.sin_port = htons(23451);
/* test bind to loopback, and preferred interfaces */
@ -293,7 +293,7 @@ bool test_bind_addr(struct sockaddr_in addr)
std::cerr << "\tAddress Family: " << (int) addr.sin_family;
std::cerr << std::endl;
std::cerr << "\tAddress: " << inet_ntoa(addr.sin_addr);
std::cerr << "\tAddress: " << rs_inet_ntoa(addr.sin_addr);
std::cerr << std::endl;
std::cerr << "\tPort: " << ntohs(addr.sin_port);
std::cerr << std::endl;

View File

@ -203,7 +203,7 @@ int test_isValidNet()
CHECK(isValidNet(&localnet1_addr)==true);
CHECK(inet_aton(invalid_addrstr, &invalid_addr)==0);
std::cerr << inet_ntoa(invalid_addr) << std::endl;
std::cerr << rs_inet_ntoa(invalid_addr) << std::endl;
//CHECK(isValidNet(&invalid_addr)==false);
REPORT("isValidNet()");

View File

@ -36,28 +36,87 @@
bool test_iface();
bool test_iface_loop();
int main(int argc, char **argv)
{
bool repeat = false;
if (argc > 1)
{
repeat = true;
}
test_iface();
if (repeat)
{
test_iface_loop();
}
return 1;
}
/* test 1: byte manipulation */
/* test 1: single check if interfaces */
bool test_iface()
{
struct in_addr pref_iface = getPreferredInterface();
std::list<std::string> ifaces = getLocalInterfaces();
std::list<std::string>::iterator it;
struct in_addr pref_iface;
std::list<struct in_addr> ifaces;
std::list<struct in_addr>::iterator it;
getPreferredInterface(pref_iface);
getLocalInterfaces(ifaces);
std::cerr << "test_iface()" << std::endl;
for(it = ifaces.begin(); it != ifaces.end(); it++)
{
std::cerr << "available iface: " << *it << std::endl;
std::cerr << "available iface: " << rs_inet_ntoa(*it) << std::endl;
}
std::cerr << "preferred " << inet_ntoa(pref_iface) << std::endl;
std::cerr << "preferred " << rs_inet_ntoa(pref_iface) << std::endl;
return true;
}
/* test 2: catch changing interfaces */
bool test_iface_loop()
{
std::cerr << "test_iface_loop()" << std::endl;
struct in_addr curr_pref_iface;
getPreferredInterface(curr_pref_iface);
time_t start = time(NULL);
while(1)
{
struct in_addr pref_iface;
std::list<struct in_addr> ifaces;
std::list<struct in_addr>::iterator it;
getPreferredInterface(pref_iface);
getLocalInterfaces(ifaces);
std::cerr << "T(" << time(NULL) - start << ") ";
for(it = ifaces.begin(); it != ifaces.end(); it++)
{
std::cerr << " I: " << rs_inet_ntoa(*it);
}
std::cerr << " P: " << rs_inet_ntoa(pref_iface) << std::endl;
if (curr_pref_iface.s_addr !=
pref_iface.s_addr)
{
std::cerr << "+++++++++++++ Address CHANGED ++++++++++";
std::cerr << std::endl;
std::cerr << "+++++++++++++ Address CHANGED ++++++++++";
std::cerr << std::endl;
std::cerr << "+++++++++++++ Address CHANGED ++++++++++";
std::cerr << std::endl;
curr_pref_iface = pref_iface;
}
sleep(1);
}
return true;
}

View File

@ -19,25 +19,16 @@ endif
# MAC_I386_BUILD = 1
# MAC_PPC_BUILD = 1
#MAC_I386_BUILD = 1
MAC_I386_BUILD = 1
#MAC_PPC_BUILD = 1
ifndef MAC_I386_BUILD
MAC_PPC_BUILD = 1
endif
# flags for components....
#PQI_USE_SSLONLY = 1
#PQI_USE_XPGP = 1
UPNPC_DIR=../../../../../../src/miniupnpc-1.0
#PQI_USE_PROXY = 1
#PQI_USE_CHANNELS = 1
#USE_FILELOOK = 1
SSL_DIR=../../../../../src/openssl-0.9.7g-xpgp-0.1c
UPNPC_DIR=../../../../../src/miniupnpc-1.0
include $(RS_TOP_DIR)/scripts/checks.mk
include $(RS_TOP_DIR)/tests/scripts/checks.mk
############ ENFORCE DIRECTORY NAMING ########################
@ -73,29 +64,7 @@ CFLAGS += $(INCLUDE)
# This Line is for Universal BUILD for 10.4 + 10.5
# (but unlikely to work unless Qt Libraries are build properly)
# CFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk
ifdef PQI_USE_XPGP
INCLUDE += -I $(SSL_DIR)/include
CFLAGS += -DPQI_USE_XPGP
endif
ifdef PQI_USE_SSLONLY
CFLAGS += -DPQI_USE_SSLONLY
endif
ifdef PQI_USE_PROXY
CFLAGS += -DPQI_USE_PROXY
endif
ifdef PQI_USE_CHANNELS
CFLAGS += -DPQI_USE_CHANNELS
endif
ifdef USE_FILELOOK
CFLAGS += -DUSE_FILELOOK
endif
#CFLAGS += -isysroot /Developer/SDKs/MacOSX10.5.sdk
# RSCFLAGS = -Wall -O3 $(INCLUDE)
@ -103,30 +72,6 @@ endif
# OS Compile Options
#########################################################################
# For the SSL BIO compilation. (Copied from OpenSSL compilation flags)
BIOCC = gcc
# Flags for architecture builds.
ifdef MAC_I386_BUILD
BIOCFLAGS = -arch i386 -I $(SSL_DIR)/include -DOPENSSL_SYSNAME_MACOSX -DOPENSSL_THREADS -D_REENTRANT -DOPENSSL_NO_KRB5 -O3 -fomit-frame-pointer -fno-common
endif
ifdef MAC_PPC_BUILD
BIOCFLAGS = -arch ppc -I $(SSL_DIR)/include -DOPENSSL_SYSNAME_MACOSX -DOPENSSL_THREADS -D_REENTRANT -DOPENSSL_NO_KRB5 -O3 -fomit-frame-pointer -fno-common -DB_ENDIAN
endif
# MacOSX flags
# BIOCFLAGS = -I $(SSL_DIR)/include -DOPENSSL_SYSNAME_MACOSX -DOPENSSL_THREADS -D_REENTRANT -DOPENSSL_NO_KRB5 -O3 -fomit-frame-pointer -fno-common -DB_ENDIAN
# This is for the Universal Build...
# but is unlikely to work... as options are PPC specific....
#
# BIOCFLAGS = -arch ppc -arch i386 -I $(SSL_DIR)/include -DOPENSSL_SYSNAME_MACOSX -DOPENSSL_THREADS -D_REENTRANT -DOPENSSL_NO_KRB5 -O3 -fomit-frame-pointer -fno-common -DB_ENDIAN
#########################################################################
# OS specific Linking.
#########################################################################
@ -135,7 +80,7 @@ LIBS = -Wl,-search_paths_first
# for Univeral BUILD
# LIBS += -arch ppc -arch i386
# LIBS += -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386
#LIBS += -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5u.sdk
LIBS += -L$(LIBDIR) -lretroshare
ifdef PQI_USE_XPGP

View File

@ -24,6 +24,7 @@
*/
#include "util/rsnet.h"
#include "util/rsthreads.h"
#include <string.h>
#ifdef WINDOWS_SYS
@ -148,3 +149,16 @@ std::ostream &operator<<(std::ostream &out, const struct sockaddr_in &addr)
return out;
}
/* thread-safe version of inet_ntoa */
static RsMutex inetMtx;
std::string rs_inet_ntoa(struct in_addr in)
{
RsStackMutex stack(inetMtx);
std::string addr(inet_ntoa(in));
return addr;
}

View File

@ -76,4 +76,8 @@ bool getIPAddressFromString (const char *addr_str, struct in_addr *addr);
std::ostream& operator<<(std::ostream& o,const struct sockaddr_in&) ;
/* thread-safe version of inet_ntoa */
std::string rs_inet_ntoa(struct in_addr in);
#endif /* RS_UNIVERSAL_NETWORK_HEADER */