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

@ -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,