diff --git a/libretroshare/src/dht/p3bitdht.cc b/libretroshare/src/dht/p3bitdht.cc index 1bced9726..e366fbad3 100644 --- a/libretroshare/src/dht/p3bitdht.cc +++ b/libretroshare/src/dht/p3bitdht.cc @@ -349,7 +349,7 @@ bool p3BitDht::dropPeer(std::string pid) /* extract current peer status */ bool p3BitDht::getPeerStatus(std::string id, - struct sockaddr_in &/*laddr*/, struct sockaddr_in &/*raddr*/, + struct sockaddr_storage &/*laddr*/, struct sockaddr_storage &/*raddr*/, uint32_t &/*type*/, uint32_t &/*mode*/) { /* remove unused parameter warnings */ @@ -363,7 +363,7 @@ bool p3BitDht::getPeerStatus(std::string id, return false; } -bool p3BitDht::getExternalInterface(struct sockaddr_in &/*raddr*/, +bool p3BitDht::getExternalInterface(struct sockaddr_storage &/*raddr*/, uint32_t &/*mode*/) { diff --git a/libretroshare/src/dht/p3bitdht.h b/libretroshare/src/dht/p3bitdht.h index 608478c45..d53c9c745 100644 --- a/libretroshare/src/dht/p3bitdht.h +++ b/libretroshare/src/dht/p3bitdht.h @@ -197,8 +197,8 @@ virtual bool getNetworkStats(uint32_t &netsize, uint32_t &localnetsize); virtual bool findPeer(std::string id); virtual bool dropPeer(std::string id); -virtual int addBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age); -virtual int addKnownPeer(const std::string &pid, const struct sockaddr_in &addr, uint32_t flags); +virtual int addBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age); +virtual int addKnownPeer(const std::string &pid, const struct sockaddr_storage &addr, uint32_t flags); //virtual int addFriend(const std::string pid); //virtual int addFriendOfFriend(const std::string pid); //virtual int addOther(const std::string pid); @@ -208,10 +208,10 @@ virtual void ConnectionFeedback(std::string pid, int state); /* extract current peer status */ virtual bool getPeerStatus(std::string id, - struct sockaddr_in &laddr, struct sockaddr_in &raddr, + struct sockaddr_storage &laddr, struct sockaddr_storage &raddr, uint32_t &type, uint32_t &mode); -virtual bool getExternalInterface(struct sockaddr_in &raddr, +virtual bool getExternalInterface(struct sockaddr_storage &raddr, uint32_t &mode); diff --git a/libretroshare/src/dht/p3bitdht_peernet.cc b/libretroshare/src/dht/p3bitdht_peernet.cc index 5420726f8..2a1915ddd 100644 --- a/libretroshare/src/dht/p3bitdht_peernet.cc +++ b/libretroshare/src/dht/p3bitdht_peernet.cc @@ -72,7 +72,13 @@ int p3BitDht::InfoCallback(const bdId *id, uint32_t /*type*/, uint32_t /*flags*/ if (mPeerSharer) { - mPeerSharer->updatePeer(rsid, addr, outtype, outreason, outage); + struct sockaddr_storage tmpaddr; + struct sockaddr_in *ap = (struct sockaddr_in *) &tmpaddr; + ap->sin_family = AF_INET; + ap->sin_addr = addr.sin_addr; + ap->sin_port = addr.sin_port; + + mPeerSharer->updatePeer(rsid, tmpaddr, outtype, outreason, outage); } /* call to the Stunners to drop the address as well */ @@ -1768,14 +1774,25 @@ int p3BitDht::checkConnectionAllowed(const bdId *peerId, int mode) */ -void p3BitDht::ConnectCalloutTCPAttempt(const std::string &peerId, struct sockaddr_in raddr) +void p3BitDht::ConnectCalloutTCPAttempt(const std::string &peerId, struct sockaddr_in raddrv4) { - struct sockaddr_in proxyaddr; - struct sockaddr_in srcaddr; - - sockaddr_clear(&proxyaddr); - sockaddr_clear(&srcaddr); + struct sockaddr_storage raddr; + struct sockaddr_storage proxyaddr; + struct sockaddr_storage srcaddr; + + sockaddr_storage_clear(proxyaddr); + sockaddr_storage_clear(proxyaddr); + sockaddr_storage_clear(srcaddr); + struct sockaddr_in *rap = (struct sockaddr_in *) &raddr; + struct sockaddr_in *pap = (struct sockaddr_in *) &proxyaddr; + struct sockaddr_in *sap = (struct sockaddr_in *) &srcaddr; + + // only one to translate + rap->sin_family = AF_INET; + rap->sin_addr = raddrv4.sin_addr; + rap->sin_port = raddrv4.sin_port; + uint32_t source = RS_CB_DHT; uint32_t connectFlags = RS_CB_FLAG_ORDER_UNSPEC | RS_CB_FLAG_MODE_TCP; uint32_t delay = 0; @@ -1785,14 +1802,25 @@ void p3BitDht::ConnectCalloutTCPAttempt(const std::string &peerId, struct sockad } -void p3BitDht::ConnectCalloutDirectOrProxy(const std::string &peerId, struct sockaddr_in raddr, uint32_t connectFlags, uint32_t delay) +void p3BitDht::ConnectCalloutDirectOrProxy(const std::string &peerId, struct sockaddr_in raddrv4, uint32_t connectFlags, uint32_t delay) { - struct sockaddr_in proxyaddr; - struct sockaddr_in srcaddr; - - sockaddr_clear(&proxyaddr); - sockaddr_clear(&srcaddr); - + struct sockaddr_storage raddr; + struct sockaddr_storage proxyaddr; + struct sockaddr_storage srcaddr; + + sockaddr_storage_clear(proxyaddr); + sockaddr_storage_clear(proxyaddr); + sockaddr_storage_clear(srcaddr); + + struct sockaddr_in *rap = (struct sockaddr_in *) &raddr; + struct sockaddr_in *pap = (struct sockaddr_in *) &proxyaddr; + struct sockaddr_in *sap = (struct sockaddr_in *) &srcaddr; + + // only one to translate + rap->sin_family = AF_INET; + rap->sin_addr = raddrv4.sin_addr; + rap->sin_port = raddrv4.sin_port; + uint32_t source = RS_CB_DHT; uint32_t bandwidth = 0; @@ -1800,9 +1828,33 @@ void p3BitDht::ConnectCalloutDirectOrProxy(const std::string &peerId, struct soc } void p3BitDht::ConnectCalloutRelay(const std::string &peerId, - struct sockaddr_in srcaddr, struct sockaddr_in proxyaddr, struct sockaddr_in destaddr, + struct sockaddr_in srcaddrv4, struct sockaddr_in proxyaddrv4, struct sockaddr_in destaddrv4, uint32_t connectFlags, uint32_t bandwidth) { + struct sockaddr_storage destaddr; + struct sockaddr_storage proxyaddr; + struct sockaddr_storage srcaddr; + + sockaddr_storage_clear(proxyaddr); + sockaddr_storage_clear(proxyaddr); + sockaddr_storage_clear(srcaddr); + + struct sockaddr_in *dap = (struct sockaddr_in *) &destaddr; + struct sockaddr_in *pap = (struct sockaddr_in *) &proxyaddr; + struct sockaddr_in *sap = (struct sockaddr_in *) &srcaddr; + + dap->sin_family = AF_INET; + dap->sin_addr = destaddrv4.sin_addr; + dap->sin_port = destaddrv4.sin_port; + + pap->sin_family = AF_INET; + pap->sin_addr = proxyaddrv4.sin_addr; + pap->sin_port = proxyaddrv4.sin_port; + + sap->sin_family = AF_INET; + sap->sin_addr = srcaddrv4.sin_addr; + sap->sin_port = srcaddrv4.sin_port; + uint32_t source = RS_CB_DHT; uint32_t delay = 0; diff --git a/libretroshare/src/dht/p3bitdht_peers.cc b/libretroshare/src/dht/p3bitdht_peers.cc index c16a742a9..f488af671 100644 --- a/libretroshare/src/dht/p3bitdht_peers.cc +++ b/libretroshare/src/dht/p3bitdht_peers.cc @@ -184,26 +184,59 @@ bool p3BitDht::dropPeer(std::string pid) ********************************* Basic Peer Details ************************************* ******************************************************************************************/ -int p3BitDht::addBadPeer(const struct sockaddr_in &addr, uint32_t /*reason*/, uint32_t /*flags*/, uint32_t /*age*/) +int p3BitDht::addBadPeer(const struct sockaddr_storage &addr, uint32_t /*reason*/, uint32_t /*flags*/, uint32_t /*age*/) { //mUdpBitDht->updateKnownPeer(&id, 0, bdflags); + struct sockaddr_in addrv4; + if (addr.ss_family != AF_INET) + { + std::cerr << "p3BitDht::addBadPeer() cannot handle IPV6 Yet, aborting"; + std::cerr << std::endl; + abort(); + } + struct sockaddr_in *ap = (struct sockaddr_in *) &addr; + + // convert. + addrv4.sin_family = ap->sin_family; + addrv4.sin_addr = ap->sin_addr; + addrv4.sin_port = ap->sin_port; + + if (mDhtStunner) { - mDhtStunner->dropStunPeer(addr); + mDhtStunner->dropStunPeer(addrv4); } if (mProxyStunner) { - mProxyStunner->dropStunPeer(addr); + mProxyStunner->dropStunPeer(addrv4); } return 1; } -int p3BitDht::addKnownPeer(const std::string &pid, const struct sockaddr_in &addr, uint32_t flags) +int p3BitDht::addKnownPeer(const std::string &pid, const struct sockaddr_storage &addr, uint32_t flags) { - + struct sockaddr_in addrv4; + if (addr.ss_family != AF_INET) + { + std::cerr << "p3BitDht::addKnownPeer() cannot handle IPV6 Yet, aborting"; + std::cerr << std::endl; + abort(); + } + struct sockaddr_in *ap = (struct sockaddr_in *) &addr; + + // convert. + addrv4.sin_family = ap->sin_family; + addrv4.sin_addr = ap->sin_addr; + addrv4.sin_port = ap->sin_port; + + + + + + int p3type = 0; int bdflags = 0; bdId id; @@ -262,13 +295,13 @@ int p3BitDht::addKnownPeer(const std::string &pid, const struct sockaddr_in &add id.id = dpd->mDhtId.id; - id.addr = addr; + id.addr = addrv4; } else { // shouldn't use own id without mutex - but it is static! id.id = mOwnDhtId; - id.addr = addr; + id.addr = addrv4; } mUdpBitDht->updateKnownPeer(&id, 0, bdflags); diff --git a/libretroshare/src/dht/stunaddrassist.h b/libretroshare/src/dht/stunaddrassist.h index 780267427..1df0bfabc 100644 --- a/libretroshare/src/dht/stunaddrassist.h +++ b/libretroshare/src/dht/stunaddrassist.h @@ -39,9 +39,21 @@ class stunAddrAssist: public pqiAddrAssist mStunner = stunner; } -virtual bool getExternalAddr(struct sockaddr_in &remote, uint8_t &stable) +virtual bool getExternalAddr(struct sockaddr_storage &remote, uint8_t &stable) { - return mStunner->externalAddr(remote, stable); + // IPV4 ONLY. + struct sockaddr_in remotev4; + if (mStunner->externalAddr(remotev4, stable)) + { + sockaddr_storage_clear(remote); + struct sockaddr_in *addr = (struct sockaddr_in *) &remote; + addr->sin_family = AF_INET; + addr->sin_addr = remotev4.sin_addr; + addr->sin_port = remotev4.sin_port; + + return true; + } + return false; } virtual int tick() diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 3fd616849..643f0964f 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -323,7 +323,6 @@ HEADERS += pqi/authssl.h \ pqi/p3peermgr.h \ pqi/p3linkmgr.h \ pqi/p3netmgr.h \ - pqi/p3dhtmgr.h \ pqi/p3notify.h \ pqi/p3upnpmgr.h \ pqi/pqiqos.h \ @@ -357,6 +356,8 @@ HEADERS += pqi/authssl.h \ pqi/sslfns.h \ pqi/pqinetstatebox.h +# pqi/p3dhtmgr.h \ + HEADERS += rsserver/p3discovery.h \ rsserver/p3face.h \ rsserver/p3history.h \ @@ -449,7 +450,6 @@ SOURCES += pqi/authgpg.cc \ pqi/p3peermgr.cc \ pqi/p3linkmgr.cc \ pqi/p3netmgr.cc \ - pqi/p3dhtmgr.cc \ pqi/p3notify.cc \ pqi/pqiqos.cc \ pqi/pqiarchive.cc \ @@ -475,6 +475,8 @@ SOURCES += pqi/authgpg.cc \ pqi/sslfns.cc \ pqi/pqinetstatebox.cc +# pqi/p3dhtmgr.cc \ + SOURCES += rsserver/p3discovery.cc \ rsserver/p3face-config.cc \ rsserver/p3face-msgs.cc \ diff --git a/libretroshare/src/pqi/authssl.cc b/libretroshare/src/pqi/authssl.cc index b7c87ba25..7cf8734e0 100644 --- a/libretroshare/src/pqi/authssl.cc +++ b/libretroshare/src/pqi/authssl.cc @@ -1329,11 +1329,10 @@ void AuthSSLimpl::getCurrentConnectionAttemptInfo(std::string& gpg_id,std::strin bool AuthSSLimpl::FailedCertificate(X509 *x509, const std::string& gpgid, const std::string& sslid, const std::string& sslcn, - const struct sockaddr_in& addr, + const struct sockaddr_storage& addr, bool incoming) { - std::string ip_address ; - rs_sprintf_append(ip_address, "%s:%u", rs_inet_ntoa(addr.sin_addr).c_str(), ntohs(addr.sin_port)); + std::string ip_address = sockaddr_storage_tostring(addr); bool authed = (x509 != NULL && AuthX509WithGPG(x509)) ; diff --git a/libretroshare/src/pqi/authssl.h b/libretroshare/src/pqi/authssl.h index 99c9d0488..bf05b1793 100644 --- a/libretroshare/src/pqi/authssl.h +++ b/libretroshare/src/pqi/authssl.h @@ -153,7 +153,7 @@ virtual SSL_CTX *getCTX() = 0; virtual void setCurrentConnectionAttemptInfo(const std::string& gpg_id,const std::string& ssl_id,const std::string& ssl_cn) = 0 ; virtual void getCurrentConnectionAttemptInfo( std::string& gpg_id, std::string& ssl_id, std::string& ssl_cn) = 0 ; -virtual bool FailedCertificate(X509 *x509, const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_in &addr, bool incoming) = 0; /* store for discovery */ +virtual bool FailedCertificate(X509 *x509, const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_storage &addr, bool incoming) = 0; /* store for discovery */ virtual bool CheckCertificate(std::string peerId, X509 *x509) = 0; /* check that they are exact match */ static void setAuthSSL_debug(AuthSSL*) ; // used for debug only. The real function is InitSSL() @@ -232,7 +232,7 @@ virtual SSL_CTX *getCTX(); /* Restored these functions: */ virtual void setCurrentConnectionAttemptInfo(const std::string& gpg_id,const std::string& ssl_id,const std::string& ssl_cn) ; virtual void getCurrentConnectionAttemptInfo( std::string& gpg_id, std::string& ssl_id, std::string& ssl_cn) ; -virtual bool FailedCertificate(X509 *x509, const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_in &addr, bool incoming); /* store for discovery */ +virtual bool FailedCertificate(X509 *x509, const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_storage &addr, bool incoming); /* store for discovery */ virtual bool CheckCertificate(std::string peerId, X509 *x509); /* check that they are exact match */ diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index 0f95d11e0..8e268c207 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -58,6 +58,11 @@ const int p3connectzone = 3431; * #define LINKMGR_DEBUG_LINKTYPE 1 ***/ +#define LINKMGR_DEBUG 1 +#define LINKMGR_DEBUG_CONNFAIL 1 +#define LINKMGR_DEBUG_ACTIONS 1 +#define LINKMGR_DEBUG_LINKTYPE 1 + /**** * #define DISABLE_UDP_CONNECTIONS 1 ***/ @@ -87,7 +92,7 @@ void printConnectState(std::ostream &out, peerConnectState &peer); peerConnectAddress::peerConnectAddress() :delay(0), period(0), type(0), flags(0), ts(0), domain_port(0) { - sockaddr_clear(&addr); + sockaddr_storage_clear(addr); } @@ -144,10 +149,13 @@ p3LinkMgrIMPL::p3LinkMgrIMPL(p3PeerMgrIMPL *peerMgr, p3NetMgrIMPL *netMgr) /* setup Banned Ip Address - static for now */ - struct in_addr bip; - memset(&bip, 0, sizeof(bip)); - bip.s_addr = 1; - + struct sockaddr_storage bip; + sockaddr_storage_clear(bip); + struct sockaddr_in *addr = (struct sockaddr_in *) &bip; + addr->sin_family = AF_INET; + addr->sin_addr.s_addr = 1; + addr->sin_port = htons(0); + mBannedIpList.push_back(bip); } @@ -158,7 +166,7 @@ p3LinkMgrIMPL::p3LinkMgrIMPL(p3PeerMgrIMPL *peerMgr, p3NetMgrIMPL *netMgr) return; } -bool p3LinkMgrIMPL::setLocalAddress(struct sockaddr_in addr) +bool p3LinkMgrIMPL::setLocalAddress(const struct sockaddr_storage &addr) { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ mLocalAddress = addr; @@ -166,10 +174,11 @@ bool p3LinkMgrIMPL::setLocalAddress(struct sockaddr_in addr) return true ; } -struct sockaddr_in p3LinkMgrIMPL::getLocalAddress() +bool p3LinkMgrIMPL::getLocalAddress(struct sockaddr_storage &addr) { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ - return mLocalAddress; + addr = mLocalAddress; + return true; } @@ -572,9 +581,9 @@ const std::string p3LinkMgrIMPL::getOwnId() } -bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &raddr, - struct sockaddr_in &proxyaddr, - struct sockaddr_in &srcaddr, +bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_storage &raddr, + struct sockaddr_storage &proxyaddr, + struct sockaddr_storage &srcaddr, uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags, uint32_t &bandwidth, std::string &domain_addr, uint16_t &domain_port) @@ -731,7 +740,7 @@ bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &ra std::cerr << "p3LinkMgrIMPL::connectAttempt() found an address: id: " << id << std::endl; - std::cerr << " laddr: " << rs_inet_ntoa(raddr.sin_addr) << " lport: " << ntohs(raddr.sin_port) << " delay: " << delay << " period: " << period; + std::cerr << " laddr: " << sockaddr_storage_tostring(raddr) << " delay: " << delay << " period: " << period; std::cerr << " type: " << type << std::endl; std::cerr << "p3LinkMgrIMPL::connectAttempt() set LinkType to: " << it->second.linkType << std::endl; #endif @@ -741,11 +750,11 @@ bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &ra #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::connectAttempt() found an address: id: " << id << std::endl; - std::cerr << " laddr: " << rs_inet_ntoa(addr.sin_addr) << " lport: " << ntohs(addr.sin_port) << " delay: " << delay << " period: " << period; + std::cerr << " laddr: " << sockaddr_storage_tostring(raddr) << " delay: " << delay << " period: " << period; std::cerr << " type: " << type << std::endl; std::cerr << "p3LinkMgrIMPL::connectAttempt() set LinkType to: " << it->second.linkType << std::endl; #endif - if (raddr.sin_addr.s_addr == 0 || raddr.sin_port == 0) { + if (sockaddr_storage_isnull(raddr)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::connectAttempt() WARNING: address or port is null" << std::endl; std::cerr << " type: " << type << std::endl; @@ -764,7 +773,7 @@ bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &ra * */ -bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address) +bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t flags, const struct sockaddr_storage &remote_peer_address) { bool doDhtAssist = false ; bool updatePeerAddr = false; @@ -857,10 +866,7 @@ bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t // This means we only update connections that we've made.. so maybe not too bad? if ((it->second.inConnAttempt) && - (it->second.currentConnAddrAttempt.addr.sin_addr.s_addr - == remote_peer_address.sin_addr.s_addr) && - (it->second.currentConnAddrAttempt.addr.sin_port - == remote_peer_address.sin_port)) + (sockaddr_storage_same(it->second.currentConnAddrAttempt.addr, remote_peer_address))) { updatePeerAddr = true; #ifdef LINKMGR_DEBUG @@ -986,7 +992,7 @@ bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t mNetMgr->netAssistFriend(id,false); /* inform NetMgr that we know this peers address: but only if external address */ - if (isExternalNet(&(remote_peer_address.sin_addr))) + if (sockaddr_storage_isExternalNet(remote_peer_address)) { mNetMgr->netAssistKnownPeer(id,remote_peer_address, NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_ONLINE); @@ -1022,7 +1028,7 @@ bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t */ // from pqissl, when a connection failed due to security -void p3LinkMgrIMPL::notifyDeniedConnection(const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_in &addr, bool incoming) +void p3LinkMgrIMPL::notifyDeniedConnection(const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_storage &addr, bool incoming) { std::cerr << "p3LinkMgrIMPL::notifyDeniedConnection()"; std::cerr << " pgpid: " << gpgid; @@ -1335,14 +1341,14 @@ void p3LinkMgrIMPL::peerStatus(std::string id, const pqiIpAddrSet &addrs, } /* This has become very unwieldy - as extra arguments are required for UDP connections */ -void p3LinkMgrIMPL::peerConnectRequest(std::string id, struct sockaddr_in raddr, struct sockaddr_in proxyaddr, struct sockaddr_in srcaddr, +void p3LinkMgrIMPL::peerConnectRequest(std::string id, const struct sockaddr_storage &raddr, const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::peerConnectRequest() id: " << id; - std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port); - std::cerr << " proxyaddr: " << rs_inet_ntoa(proxyaddr.sin_addr) << ":" << ntohs(proxyaddr.sin_port); - std::cerr << " srcaddr: " << rs_inet_ntoa(srcaddr.sin_addr) << ":" << ntohs(srcaddr.sin_port); + std::cerr << " raddr: " << sockaddr_storage_tostring(raddr); + std::cerr << " proxyaddr: " << sockaddr_storage_tostring(proxyaddr); + std::cerr << " srcaddr: " << sockaddr_storage_tostring(srcaddr); std::cerr << " source: " << source; std::cerr << " flags: " << flags; std::cerr << " delay: " << delay; @@ -1352,9 +1358,13 @@ void p3LinkMgrIMPL::peerConnectRequest(std::string id, struct sockaddr_in rad { /* Log */ std::string out = "p3LinkMgrIMPL::peerConnectRequest() id: " + id; - rs_sprintf_append(out, " raddr: %s:%u", rs_inet_ntoa(raddr.sin_addr).c_str(), ntohs(raddr.sin_port)); - rs_sprintf_append(out, " proxyaddr: %s:%u", rs_inet_ntoa(proxyaddr.sin_addr).c_str(), ntohs(proxyaddr.sin_port)); - rs_sprintf_append(out, " srcaddr: %s:%u", rs_inet_ntoa(srcaddr.sin_addr).c_str(), ntohs(srcaddr.sin_port)); + out += " raddr: "; + out += sockaddr_storage_tostring(raddr); + out += " proxyaddr: "; + out += sockaddr_storage_tostring(proxyaddr); + out += " srcaddr: "; + out += sockaddr_storage_tostring(srcaddr); + rs_sprintf_append(out, " source: %lu", source); rs_sprintf_append(out, " flags: %lu", flags); rs_sprintf_append(out, " delay: %lu", delay); @@ -1402,7 +1412,7 @@ void p3LinkMgrIMPL::peerConnectRequest(std::string id, struct sockaddr_in rad return; } /* setup specific attempt for DHT found address. */ - locked_ConnectAttempt_SpecificAddress(&(it->second), &raddr); + locked_ConnectAttempt_SpecificAddress(&(it->second), raddr); } retryConnect(id); @@ -1454,8 +1464,8 @@ bool p3LinkMgrIMPL::retryConnect(const std::string &id) -bool p3LinkMgrIMPL::tryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr, - struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr, +bool p3LinkMgrIMPL::tryConnectUDP(const std::string &id, const struct sockaddr_storage &rUdpAddr, + const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t flags, uint32_t delay, uint32_t bandwidth) { @@ -1520,12 +1530,11 @@ bool p3LinkMgrIMPL::tryConnectUDP(const std::string &id, struct sockaddr_in &r } /* Explicit Request to start the UDP connection */ - if (isValidNet(&(rUdpAddr.sin_addr))) + if (sockaddr_storage_isValidNet(rUdpAddr)) { #ifdef LINKMGR_DEBUG std::cerr << "Adding udp connection attempt: "; - std::cerr << "Addr: " << rs_inet_ntoa(rUdpAddr.sin_addr); - std::cerr << ":" << ntohs(rUdpAddr.sin_port); + std::cerr << "Addr: " << sockaddr_storage_tostring(rUdpAddr); std::cerr << std::endl; #endif peerConnectAddress pca; @@ -1605,7 +1614,7 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) /* first possibility - is it a hidden peer */ if (mPeerMgr->isHiddenPeer(id)) { - struct sockaddr_in proxy_addr; + struct sockaddr_storage proxy_addr; std::string domain_addr; uint16_t domain_port; @@ -1617,7 +1626,7 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) std::map::iterator it; if (mFriendList.end() != (it = mFriendList.find(id))) { - locked_ConnectAttempt_ProxyAddress(&(it->second), &proxy_addr, domain_addr, domain_port); + locked_ConnectAttempt_ProxyAddress(&(it->second), proxy_addr, domain_addr, domain_port); return locked_ConnectAttempt_Complete(&(it->second)); } } @@ -1631,8 +1640,8 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) return false; } - struct sockaddr_in lAddr; - struct sockaddr_in eAddr; + struct sockaddr_storage lAddr; + struct sockaddr_storage eAddr; pqiIpAddrSet histAddrs; std::string dyndns; @@ -1643,11 +1652,11 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) std::map::iterator it; if (mFriendList.end() != (it = mFriendList.find(id))) { - locked_ConnectAttempt_CurrentAddresses(&(it->second), &lAddr, &eAddr); + locked_ConnectAttempt_CurrentAddresses(&(it->second), lAddr, eAddr); - uint16_t dynPort = ntohs(eAddr.sin_port); + uint16_t dynPort = sockaddr_storage_port(eAddr); if (!dynPort) - dynPort = ntohs(lAddr.sin_port); + dynPort = sockaddr_storage_port(lAddr); if (dynPort) { locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort); @@ -1677,12 +1686,11 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) #define MAX_TCP_ADDR_AGE (3600 * 24 * 14) // two weeks in seconds. -bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, time_t age) +bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_storage &addr, time_t age) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr("; - std::cerr << rs_inet_ntoa(addr->sin_addr); - std::cerr << ":" << ntohs(addr->sin_port); + std::cerr << sockaddr_storage_tostring(addr); std::cerr << ", " << age << ")"; std::cerr << std::endl; #endif @@ -1699,10 +1707,10 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, t return false; } - bool isValid = isValidNet(&(addr->sin_addr)); - // bool isLoopback = isLoopbackNet(&(addr->sin_addr)); - // bool isPrivate = isPrivateNet(&(addr->sin_addr)); - bool isExternal = isExternalNet(&(addr->sin_addr)); + bool isValid = sockaddr_storage_isValidNet(addr); + // bool isLoopback = sockaddr_storage_isLoopbackNet(addr); + // bool isPrivate = sockaddr_storage_isPrivateNet(addr); + bool isExternal = sockaddr_storage_isExternalNet(addr); /* if invalid - quick rejection */ if (!isValid) @@ -1717,10 +1725,10 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, t /* if it is on the ban list - ignore */ /* checks - is it the dreaded 1.0.0.0 */ - std::list::const_iterator it; + std::list::const_iterator it; for(it = mBannedIpList.begin(); it != mBannedIpList.end(); it++) { - if (it->s_addr == addr->sin_addr.s_addr) + if (sockaddr_storage_sameip(*it, addr)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - ON BANNED IPLIST"; @@ -1751,12 +1759,12 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, t #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() Checking sameNet against: "; - std::cerr << rs_inet_ntoa(mLocalAddress.sin_addr); + std::cerr << sockaddr_storage_iptostring(mLocalAddress); std::cerr << ")"; std::cerr << std::endl; #endif - if (sameNet(&(mLocalAddress.sin_addr), &(addr->sin_addr))) + if (sockaddr_storage_samenet(mLocalAddress, addr)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() ACCEPTING - PRIVATE & sameNET"; @@ -1776,29 +1784,28 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, t } -void p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, struct sockaddr_in *remoteAddr) +void p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, const struct sockaddr_storage &remoteAddr) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses()"; std::cerr << std::endl; #endif - if ((remoteAddr) && (locked_CheckPotentialAddr(remoteAddr, 0))) + if (locked_CheckPotentialAddr(remoteAddr, 0)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses() "; std::cerr << "Adding tcp connection attempt: "; - std::cerr << "Addr: " << rs_inet_ntoa(remoteAddr->sin_addr); - std::cerr << ":" << ntohs(remoteAddr->sin_port); + std::cerr << "Addr: " << sockaddr_storage_tostring(remoteAddr); std::cerr << std::endl; #endif peerConnectAddress pca; - pca.addr = *remoteAddr; + pca.addr = remoteAddr; pca.type = RS_NET_CONN_TCP_EXTERNAL; pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; addAddressIfUnique(peer->connAddrs, pca, false); @@ -1806,7 +1813,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddress(peerConnectState *pee } -void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, struct sockaddr_in *localAddr, struct sockaddr_in *serverAddr) +void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, const struct sockaddr_storage &localAddr, const struct sockaddr_storage &serverAddr) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses()"; @@ -1814,45 +1821,43 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *pe #endif // Just push all the addresses onto the stack. /* try "current addresses" first */ - if ((localAddr) && (locked_CheckPotentialAddr(localAddr, 0))) + if (locked_CheckPotentialAddr(localAddr, 0)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() "; std::cerr << "Adding tcp connection attempt: "; - std::cerr << "Current Local Addr: " << rs_inet_ntoa(localAddr->sin_addr); - std::cerr << ":" << ntohs(localAddr->sin_port); + std::cerr << "Current Local Addr: " << sockaddr_storage_tostring(localAddr); std::cerr << std::endl; #endif peerConnectAddress pca; - pca.addr = *localAddr; + pca.addr = localAddr; pca.type = RS_NET_CONN_TCP_LOCAL; pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; addAddressIfUnique(peer->connAddrs, pca, false); } - if ((serverAddr) && (locked_CheckPotentialAddr(serverAddr, 0))) + if (locked_CheckPotentialAddr(serverAddr, 0)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() "; std::cerr << "Adding tcp connection attempt: "; - std::cerr << "Current Ext Addr: " << rs_inet_ntoa(serverAddr->sin_addr); - std::cerr << ":" << ntohs(serverAddr->sin_port); + std::cerr << "Current Ext Addr: " << sockaddr_storage_tostring(serverAddr); std::cerr << std::endl; #endif peerConnectAddress pca; - pca.addr = *serverAddr; + pca.addr = serverAddr; pca.type = RS_NET_CONN_TCP_EXTERNAL; pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; addAddressIfUnique(peer->connAddrs, pca, false); @@ -1874,14 +1879,13 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState for(ait = ipAddrs.mLocal.mAddrs.begin(); ait != ipAddrs.mLocal.mAddrs.end(); ait++) { - if (locked_CheckPotentialAddr(&(ait->mAddr), now - ait->mSeenTime)) + if (locked_CheckPotentialAddr(ait->mAddr, now - ait->mSeenTime)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() "; std::cerr << "Adding tcp connection attempt: "; - std::cerr << "Local Addr: " << rs_inet_ntoa(ait->mAddr.sin_addr); - std::cerr << ":" << ntohs(ait->mAddr.sin_port); + std::cerr << "Local Addr: " << sockaddr_storage_tostring(ait->mAddr); std::cerr << std::endl; #endif @@ -1891,8 +1895,8 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; addAddressIfUnique(peer->connAddrs, pca, false); @@ -1902,14 +1906,13 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState for(ait = ipAddrs.mExt.mAddrs.begin(); ait != ipAddrs.mExt.mAddrs.end(); ait++) { - if (locked_CheckPotentialAddr(&(ait->mAddr), now - ait->mSeenTime)) + if (locked_CheckPotentialAddr(ait->mAddr, now - ait->mSeenTime)) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() "; std::cerr << "Adding tcp connection attempt: "; - std::cerr << "Ext Addr: " << rs_inet_ntoa(ait->mAddr.sin_addr); - std::cerr << ":" << ntohs(ait->mAddr.sin_port); + std::cerr << "Ext Addr: " << sockaddr_storage_tostring(ait->mAddr); std::cerr << std::endl; #endif peerConnectAddress pca; @@ -1918,8 +1921,8 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; addAddressIfUnique(peer->connAddrs, pca, false); @@ -1931,7 +1934,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState void p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std::string dyndns, uint16_t port) { /* try dyndns address too */ - struct in_addr addr; + struct sockaddr_storage addr; if (!dyndns.empty() && port) { #ifdef LINKMGR_DEBUG @@ -1942,26 +1945,25 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() "; std::cerr << "Adding tcp connection attempt: "; - std::cerr << "DynDNS Addr: " << rs_inet_ntoa(addr); - std::cerr << ":" << ntohs(port); + std::cerr << "DynDNS Addr: " << sockaddr_storage_iptostring(addr); + std::cerr << ":" << port; std::cerr << std::endl; #endif peerConnectAddress pca; - pca.addr.sin_family = AF_INET; - pca.addr.sin_addr.s_addr = addr.s_addr; - pca.addr.sin_port = htons(port); + sockaddr_storage_copyip(pca.addr, addr); + sockaddr_storage_setport(pca.addr, port); pca.type = RS_NET_CONN_TCP_EXTERNAL; //for the delay, we add a random time and some more time when the friend list is big pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; /* check address validity */ - if (locked_CheckPotentialAddr(&(pca.addr), 0)) + if (locked_CheckPotentialAddr(pca.addr, 0)) { addAddressIfUnique(peer->connAddrs, pca, true); } @@ -1984,15 +1986,13 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std } -void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, struct sockaddr_in *proxy_addr, const std::string &domain_addr, uint16_t domain_port) +void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, const struct sockaddr_storage &proxy_addr, const std::string &domain_addr, uint16_t domain_port) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress() trying address: " << domain_addr << ":" << domain_port << std::endl; #endif peerConnectAddress pca; - pca.addr.sin_family = AF_INET; - pca.addr.sin_addr.s_addr = proxy_addr->sin_addr.s_addr; - pca.addr.sin_port = proxy_addr->sin_port; + pca.addr = proxy_addr; pca.type = RS_NET_CONN_TCP_HIDDEN; @@ -2001,15 +2001,15 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - sockaddr_clear(&(pca.proxyaddr)); - sockaddr_clear(&(pca.srcaddr)); + sockaddr_storage_clear(pca.proxyaddr); + sockaddr_storage_clear(pca.srcaddr); pca.bandwidth = 0; pca.domain_addr = domain_addr; pca.domain_port = domain_port; /* check address validity */ - if (locked_CheckPotentialAddr(&(pca.addr), 0)) + if (locked_CheckPotentialAddr(pca.addr, 0)) { addAddressIfUnique(peer->connAddrs, pca, true); } @@ -2022,15 +2022,14 @@ bool p3LinkMgrIMPL::addAddressIfUnique(std::list &addrList, * in the list */ #ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::addAddressIfUnique() Checking Address: " << rs_inet_ntoa(pca.addr.sin_addr); + std::cerr << "p3LinkMgrIMPL::addAddressIfUnique() Checking Address: " << sockaddr_storage_iptostring(pca.addr); std::cerr << std::endl; #endif std::list::iterator it; for(it = addrList.begin(); it != addrList.end(); it++) { - if ((pca.addr.sin_addr.s_addr == it->addr.sin_addr.s_addr) && - (pca.addr.sin_port == it->addr.sin_port) && + if (sockaddr_storage_same(pca.addr, it->addr) && (pca.type == it->type)) { #ifdef LINKMGR_DEBUG diff --git a/libretroshare/src/pqi/p3linkmgr.h b/libretroshare/src/pqi/p3linkmgr.h index c5918fa2d..a4513ea83 100644 --- a/libretroshare/src/pqi/p3linkmgr.h +++ b/libretroshare/src/pqi/p3linkmgr.h @@ -70,7 +70,7 @@ class peerConnectAddress public: peerConnectAddress(); /* init */ - struct sockaddr_in addr; + struct sockaddr_storage addr; uint32_t delay; /* to stop simultaneous connects */ uint32_t period; /* UDP only */ uint32_t type; @@ -78,8 +78,8 @@ class peerConnectAddress time_t ts; // Extra Parameters for Relay connections. - struct sockaddr_in proxyaddr; - struct sockaddr_in srcaddr; + struct sockaddr_storage proxyaddr; + struct sockaddr_storage srcaddr; uint32_t bandwidth; // Extra Parameters for Proxy/Hidden connection. @@ -162,19 +162,19 @@ virtual void addMonitor(pqiMonitor *mon) = 0; virtual void removeMonitor(pqiMonitor *mon) = 0; /****************** Connections *******************/ -virtual bool connectAttempt(const std::string &id, struct sockaddr_in &raddr, - struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr, +virtual bool connectAttempt(const std::string &id, struct sockaddr_storage &raddr, + struct sockaddr_storage &proxyaddr, struct sockaddr_storage &srcaddr, uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags, uint32_t &bandwidth, std::string &domain_addr, uint16_t &domain_port) = 0; -virtual bool connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address) = 0; +virtual bool connectResult(const std::string &id, bool success, uint32_t flags, const struct sockaddr_storage &remote_peer_address) = 0; virtual bool retryConnect(const std::string &id) = 0; -virtual void notifyDeniedConnection(const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_in &addr, bool incoming) = 0; +virtual void notifyDeniedConnection(const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_storage &addr, bool incoming) = 0; /* Network Addresses */ -virtual bool setLocalAddress(struct sockaddr_in addr) = 0; -virtual struct sockaddr_in getLocalAddress() = 0; +virtual bool setLocalAddress(const struct sockaddr_storage &addr) = 0; +virtual bool getLocalAddress(struct sockaddr_storage &addr) = 0; /************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/ @@ -188,8 +188,8 @@ virtual int addFriend(const std::string &ssl_id, bool isVisible) = 0; // THESE MUSTn't BE specfied HERE - as overloaded from pqiConnectCb. //virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, // uint32_t type, uint32_t flags, uint32_t source) = 0; -//virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, -// struct sockaddr_in proxyaddr, struct sockaddr_in srcaddr, +//virtual void peerConnectRequest(std::string id, const struct sockaddr_storage &raddr, +// const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, // uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth) = 0; /****************************************************************************/ @@ -220,25 +220,25 @@ virtual void addMonitor(pqiMonitor *mon); virtual void removeMonitor(pqiMonitor *mon); /****************** Connections *******************/ -virtual bool connectAttempt(const std::string &id, struct sockaddr_in &raddr, - struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr, +virtual bool connectAttempt(const std::string &id, struct sockaddr_storage &raddr, + struct sockaddr_storage &proxyaddr, struct sockaddr_storage &srcaddr, uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags, uint32_t &bandwidth, std::string &domain_addr, uint16_t &domain_port); -virtual bool connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address); +virtual bool connectResult(const std::string &id, bool success, uint32_t flags, const struct sockaddr_storage &remote_peer_address); virtual bool retryConnect(const std::string &id); -virtual void notifyDeniedConnection(const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_in &addr, bool incoming); +virtual void notifyDeniedConnection(const std::string& gpgid,const std::string& sslid,const std::string& sslcn,const struct sockaddr_storage &addr, bool incoming); /* Network Addresses */ -virtual bool setLocalAddress(struct sockaddr_in addr); -virtual struct sockaddr_in getLocalAddress(); +virtual bool setLocalAddress(const struct sockaddr_storage &addr); +virtual bool getLocalAddress(struct sockaddr_storage &addr); /******* overloaded from pqiConnectCb *************/ virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, uint32_t type, uint32_t flags, uint32_t source); -virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, - struct sockaddr_in proxyaddr, struct sockaddr_in srcaddr, +virtual void peerConnectRequest(std::string id, const struct sockaddr_storage &raddr, + const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth); @@ -280,23 +280,23 @@ void statusTick(); void tickMonitors(); /* connect attempts UDP */ -bool tryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr, - struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr, +bool tryConnectUDP(const std::string &id, const struct sockaddr_storage &rUdpAddr, + const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t flags, uint32_t delay, uint32_t bandwidth); /* connect attempts TCP */ bool retryConnectTCP(const std::string &id); -void locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, struct sockaddr_in *remoteAddr); -void locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, struct sockaddr_in *localAddr, struct sockaddr_in *serverAddr); +void locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, const struct sockaddr_storage &remoteAddr); +void locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, const struct sockaddr_storage &localAddr, const struct sockaddr_storage &serverAddr); void locked_ConnectAttempt_HistoricalAddresses(peerConnectState *peer, const pqiIpAddrSet &ipAddrs); void locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std::string dyndns, uint16_t dynPort); void locked_ConnectAttempt_AddTunnel(peerConnectState *peer); -void locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, struct sockaddr_in *proxy_addr, const std::string &domain_addr, uint16_t domain_port); +void locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, const struct sockaddr_storage &proxy_addr, const std::string &domain_addr, uint16_t domain_port); bool locked_ConnectAttempt_Complete(peerConnectState *peer); -bool locked_CheckPotentialAddr(const struct sockaddr_in *addr, time_t age); +bool locked_CheckPotentialAddr(const struct sockaddr_storage &addr, time_t age); bool addAddressIfUnique(std::list &addrList, peerConnectAddress &pca, bool pushFront); @@ -314,7 +314,7 @@ private: bool mStatusChanged; - struct sockaddr_in mLocalAddress; + struct sockaddr_storage mLocalAddress; std::list clients; @@ -333,7 +333,7 @@ private: uint32_t lastGroupId; /* relatively static list of banned ip addresses */ - std::list mBannedIpList; + std::list mBannedIpList; }; #endif // MRK_PQI_LINK_MANAGER_HEADER diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 554c535a5..382c0f417 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -74,6 +74,11 @@ const uint32_t MIN_TIME_BETWEEN_NET_RESET = 5; * #define NETMGR_DEBUG_STATEBOX 1 ***/ +#define NETMGR_DEBUG 1 +#define NETMGR_DEBUG_RESET 1 +#define NETMGR_DEBUG_TICK 1 +#define NETMGR_DEBUG_STATEBOX 1 + pqiNetStatus::pqiNetStatus() :mLocalAddrOk(false), mExtAddrOk(false), mExtAddrStableOk(false), mUpnpOk(false), mDhtOk(false), mResetReq(false) @@ -81,8 +86,8 @@ pqiNetStatus::pqiNetStatus() mDhtNetworkSize = 0; mDhtRsNetworkSize = 0; - sockaddr_clear(&mLocalAddr); - sockaddr_clear(&mExtAddr); + sockaddr_storage_clear(mLocalAddr); + sockaddr_storage_clear(mExtAddr); return; } @@ -101,8 +106,8 @@ void pqiNetStatus::print(std::ostream &out) out << std::endl; out << "mDhtNetworkSize: " << mDhtNetworkSize << " mDhtRsNetworkSize: " << mDhtRsNetworkSize; out << std::endl; - 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 << "mLocalAddr: " << sockaddr_storage_tostring(mLocalAddr) << " "; + out << "mExtAddr: " << sockaddr_storage_tostring(mExtAddr) << " "; out << " NetOk: " << NetOk(); out << std::endl; } @@ -128,12 +133,8 @@ p3NetMgrIMPL::p3NetMgrIMPL() mLastSlowTickTime = 0; mOldNatType = RSNET_NATTYPE_UNKNOWN; mOldNatHole = RSNET_NATHOLE_UNKNOWN; - mLocalAddr.sin_port = 0; - mLocalAddr.sin_addr.s_addr = 0; - mLocalAddr.sin_family = AF_INET ; - mExtAddr.sin_port = 0; - mExtAddr.sin_addr.s_addr = 0; - mExtAddr.sin_family = AF_INET ; + sockaddr_storage_clear(mLocalAddr); + sockaddr_storage_clear(mExtAddr); mVisState = 0 ; } @@ -269,7 +270,7 @@ void p3NetMgrIMPL::netReset() RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ - struct sockaddr_in iaddr = mLocalAddr; + struct sockaddr_storage iaddr = mLocalAddr; #ifdef NETMGR_DEBUG_RESET std::cerr << "p3NetMgrIMPL::netReset() resetting listeners" << std::endl; @@ -582,8 +583,8 @@ void p3NetMgrIMPL::netUpnpInit() /* get the ports from the configuration */ mNetStatus = RS_NET_UPNP_SETUP; - iport = ntohs(mLocalAddr.sin_port); - eport = ntohs(mExtAddr.sin_port); + iport = sockaddr_storage_port(mLocalAddr); + eport = sockaddr_storage_port(mExtAddr); if ((eport < 1000) || (eport > 30000)) { eport = iport; @@ -608,7 +609,7 @@ void p3NetMgrIMPL::netUpnpCheck() mNetMtx.unlock(); /* UNLOCK MUTEX */ - struct sockaddr_in extAddr; + struct sockaddr_storage extAddr; int upnpState = netAssistFirewallActive(); if (((upnpState == 0) && (delta > (time_t)MAX_UPNP_INIT)) || @@ -640,12 +641,11 @@ void p3NetMgrIMPL::netUpnpCheck() * we now have external upnp address. Golden! * don't set netOk flag until have seen some traffic. */ - if (isValidNet(&(extAddr.sin_addr))) + if (sockaddr_storage_isValidNet(extAddr)) { #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netUpnpCheck() "; - std::cerr << "UpnpAddr: " << rs_inet_ntoa(extAddr.sin_addr); - std::cerr << ":" << ntohs(extAddr.sin_port); + std::cerr << "UpnpAddr: " << sockaddr_storage_tostring(extAddr); std::cerr << std::endl; #endif mNetFlags.mUpnpOk = true; @@ -680,7 +680,7 @@ void p3NetMgrIMPL::netExtCheck() { RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ bool isStable = false; - struct sockaddr_in tmpip ; + struct sockaddr_storage tmpip ; /* check for External Address */ /* in order of importance */ @@ -697,7 +697,7 @@ void p3NetMgrIMPL::netExtCheck() #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied from netAssistExternalAddress()" << std::endl; #endif - if (isValidNet(&(tmpip.sin_addr))) + if (sockaddr_storage_isValidNet(tmpip)) { // must be stable??? isStable = true; @@ -722,8 +722,8 @@ void p3NetMgrIMPL::netExtCheck() std::cerr << "p3NetMgrIMPL::netExtCheck() Ext Not Ok, Checking DhtStunner" << std::endl; #endif uint8_t isstable = 0; - struct sockaddr_in tmpaddr; - sockaddr_clear(&tmpaddr); + struct sockaddr_storage tmpaddr; + sockaddr_storage_clear(tmpaddr); if (mDhtStunner) { @@ -738,7 +738,7 @@ void p3NetMgrIMPL::netExtCheck() #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::netExtCheck() From DhtStunner: "; - std::cerr << rs_inet_ntoa(tmpaddr.sin_addr) << ":" << htons(tmpaddr.sin_port); + std::cerr << sockaddr_storage_tostring(tmpaddr); std::cerr << " Stable: " << (uint32_t) isstable; std::cerr << std::endl; #endif @@ -755,18 +755,18 @@ void p3NetMgrIMPL::netExtCheck() #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() checking ExtAddrFinder" << std::endl; #endif - bool extFinderOk = mExtAddrFinder->hasValidIP(&(tmpip.sin_addr)); + bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip); if (extFinderOk) { #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl; #endif /* best guess at port */ - tmpip.sin_port = mNetFlags.mLocalAddr.sin_port; + sockaddr_storage_setport(tmpip, sockaddr_storage_port(mLocalAddr)); + #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() "; - std::cerr << "ExtAddr: " << rs_inet_ntoa(tmpip.sin_addr); - std::cerr << ":" << ntohs(tmpip.sin_port); + std::cerr << "ExtAddr: " << sockaddr_storage_tostring(tmpip); std::cerr << std::endl; #endif @@ -790,8 +790,7 @@ void p3NetMgrIMPL::netExtCheck() #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() "; - std::cerr << "ExtAddr: " << rs_inet_ntoa(mNetFlags.mExtAddr.sin_addr); - std::cerr << ":" << ntohs(mNetFlags.mExtAddr.sin_port); + std::cerr << "ExtAddr: " << sockaddr_storage_tostring(mNetFlags.mExtAddr); std::cerr << std::endl; #endif //update ip address list @@ -910,19 +909,19 @@ bool p3NetMgrIMPL::checkNetAddress() bool addrChanged = false; bool validAddr = false; - struct in_addr prefAddr; - struct sockaddr_in oldAddr; + struct sockaddr_storage prefAddr; + struct sockaddr_storage oldAddr; if (mNetMode & RS_NET_MODE_TRY_LOOPBACK) { std::cerr << "p3NetMgrIMPL::checkNetAddress() LOOPBACK ... forcing to 127.0.0.1"; std::cerr << std::endl; - inet_aton("127.0.0.1", &prefAddr); + sockaddr_storage_ipv4_aton(prefAddr, "127.0.0.1"); validAddr = true; } else { - validAddr = getPreferredInterface(mLocalAddr.sin_addr, prefAddr); + validAddr = getPreferredInterface(mLocalAddr, prefAddr); } @@ -945,15 +944,14 @@ bool p3NetMgrIMPL::checkNetAddress() RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ oldAddr = mLocalAddr; - addrChanged = (prefAddr.s_addr != mLocalAddr.sin_addr.s_addr); + addrChanged = !sockaddr_storage_sameip(prefAddr, mLocalAddr); #ifdef NETMGR_DEBUG_TICK std::cerr << "p3NetMgrIMPL::checkNetAddress()"; std::cerr << std::endl; - std::cerr << "Current Local: " << rs_inet_ntoa(mLocalAddr.sin_addr); - std::cerr << ":" << ntohs(mLocalAddr.sin_port); + std::cerr << "Current Local: " << sockaddr_storage_tostring(mLocalAddr); std::cerr << std::endl; - std::cerr << "Current Preferred: " << rs_inet_ntoa(prefAddr); + std::cerr << "Current Preferred: " << sockaddr_storage_iptostring(prefAddr); std::cerr << std::endl; #endif @@ -962,19 +960,18 @@ bool p3NetMgrIMPL::checkNetAddress() { std::cerr << "p3NetMgrIMPL::checkNetAddress() Address Changed!"; std::cerr << std::endl; - std::cerr << "Current Local: " << rs_inet_ntoa(mLocalAddr.sin_addr); - std::cerr << ":" << ntohs(mLocalAddr.sin_port); + std::cerr << "Current Local: " << sockaddr_storage_tostring(mLocalAddr); std::cerr << std::endl; - std::cerr << "Current Preferred: " << rs_inet_ntoa(prefAddr); + std::cerr << "Current Preferred: " << sockaddr_storage_iptostring(prefAddr); std::cerr << std::endl; } #endif // update address. - mLocalAddr.sin_addr = prefAddr; + sockaddr_storage_copyip(mLocalAddr, prefAddr); mNetFlags.mLocalAddr = mLocalAddr; - if(isLoopbackNet(&(mLocalAddr.sin_addr))) + if(sockaddr_storage_isLoopbackNet(mLocalAddr)) { #ifdef NETMGR_DEBUG std::cerr << "p3NetMgrIMPL::checkNetAddress() laddr: Loopback" << std::endl; @@ -982,7 +979,7 @@ bool p3NetMgrIMPL::checkNetAddress() mNetFlags.mLocalAddrOk = false; mNetStatus = RS_NET_LOOPBACK; } - else if (!isValidNet(&mLocalAddr.sin_addr)) + else if (!sockaddr_storage_isValidNet(mLocalAddr)) { #ifdef NETMGR_DEBUG std::cerr << "p3NetMgrIMPL::checkNetAddress() laddr: invalid" << std::endl; @@ -998,7 +995,7 @@ bool p3NetMgrIMPL::checkNetAddress() } - int port = ntohs(mLocalAddr.sin_port); + int port = sockaddr_storage_port(mLocalAddr); if ((port < PQI_MIN_PORT) || (port > PQI_MAX_PORT)) { #ifdef NETMGR_DEBUG @@ -1008,8 +1005,9 @@ bool p3NetMgrIMPL::checkNetAddress() // same, but appear random from peer to peer. // Random port avoids clashes, improves anonymity. // - - mLocalAddr.sin_port = htons(PQI_MIN_PORT + (RSRandom::random_u32() % (PQI_MAX_PORT - PQI_MIN_PORT))); + + int new_port = htons(PQI_MIN_PORT + (RSRandom::random_u32() % (PQI_MAX_PORT - PQI_MIN_PORT))); + sockaddr_storage_setport(mLocalAddr, new_port); addrChanged = true; } @@ -1018,18 +1016,17 @@ bool p3NetMgrIMPL::checkNetAddress() * are the same (modify server)... this mismatch can * occur when the local port is changed.... */ - if (mLocalAddr.sin_addr.s_addr == mExtAddr.sin_addr.s_addr) + if (sockaddr_storage_sameip(mLocalAddr, mExtAddr)) { - mExtAddr.sin_port = mLocalAddr.sin_port; + sockaddr_storage_setport(mExtAddr, sockaddr_storage_port(mLocalAddr)); } // ensure that address family is set, otherwise windows Barfs. - mLocalAddr.sin_family = AF_INET; - mExtAddr.sin_family = AF_INET; + //mLocalAddr.sin_family = AF_INET; + //mExtAddr.sin_family = AF_INET; #ifdef NETMGR_DEBUG_TICK - std::cerr << "p3NetMgrIMPL::checkNetAddress() Final Local Address: " << rs_inet_ntoa(mLocalAddr.sin_addr); - std::cerr << ":" << ntohs(mLocalAddr.sin_port) << std::endl; + std::cerr << "p3NetMgrIMPL::checkNetAddress() Final Local Address: " << sockaddr_storage_tostring(mLocalAddr); std::cerr << std::endl; #endif @@ -1068,13 +1065,12 @@ void p3NetMgrIMPL::addNetListener(pqiNetListener *listener) -bool p3NetMgrIMPL::setLocalAddress(struct sockaddr_in addr) +bool p3NetMgrIMPL::setLocalAddress(const struct sockaddr_storage &addr) { bool changed = false; { RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ - if ((mLocalAddr.sin_addr.s_addr != addr.sin_addr.s_addr) || - (mLocalAddr.sin_port != addr.sin_port)) + if (sockaddr_storage_same(mLocalAddr, addr)) { changed = true; } @@ -1093,13 +1089,12 @@ bool p3NetMgrIMPL::setLocalAddress(struct sockaddr_in addr) return true; } -bool p3NetMgrIMPL::setExtAddress(struct sockaddr_in addr) +bool p3NetMgrIMPL::setExtAddress(const struct sockaddr_storage &addr) { bool changed = false; { RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ - if ((mExtAddr.sin_addr.s_addr != addr.sin_addr.s_addr) || - (mExtAddr.sin_port != addr.sin_port)) + if (sockaddr_storage_same(mExtAddr, addr)) { changed = true; } @@ -1250,7 +1245,7 @@ bool p3NetMgrIMPL::netAssistFirewallPorts(uint16_t iport, uint16_t eport) } -bool p3NetMgrIMPL::netAssistExtAddress(struct sockaddr_in &extAddr) +bool p3NetMgrIMPL::netAssistExtAddress(struct sockaddr_storage &extAddr) { std::map::iterator it; for(it = mFwAgents.begin(); it != mFwAgents.end(); it++) @@ -1398,7 +1393,7 @@ bool p3NetMgrIMPL::netAssistFriend(const std::string &id, bool on) } -bool p3NetMgrIMPL::netAssistKnownPeer(const std::string &id, const struct sockaddr_in &addr, uint32_t flags) +bool p3NetMgrIMPL::netAssistKnownPeer(const std::string &id, const struct sockaddr_storage &addr, uint32_t flags) { std::map::iterator it; @@ -1414,12 +1409,12 @@ bool p3NetMgrIMPL::netAssistKnownPeer(const std::string &id, const struct sockad return true; } -bool p3NetMgrIMPL::netAssistBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age) +bool p3NetMgrIMPL::netAssistBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age) { std::map::iterator it; #ifdef NETMGR_DEBUG - std::cerr << "p3NetMgrIMPL::netAssistBadPeer(" << rs_inet_ntoa(addr.sin_addr) << ")"; + std::cerr << "p3NetMgrIMPL::netAssistBadPeer(" << sockaddr_storage_iptostring(addr) << ")"; std::cerr << std::endl; #endif @@ -1454,7 +1449,7 @@ bool p3NetMgrIMPL::netAssistStatusUpdate(const std::string &id, int state) std::map::iterator it; #ifdef NETMGR_DEBUG - std::cerr << "p3NetMgrIMPL::netAssistFriend(" << id << ", " << on << ")"; + std::cerr << "p3NetMgrIMPL::netAssistStatusUpdate(" << id << ", " << state << ")"; std::cerr << std::endl; #endif @@ -1466,8 +1461,8 @@ bool p3NetMgrIMPL::netAssistStatusUpdate(const std::string &id, int state) } -bool p3NetMgrIMPL::netAssistSetAddress( struct sockaddr_in &/*laddr*/, - struct sockaddr_in &/*eaddr*/, +bool p3NetMgrIMPL::netAssistSetAddress( const struct sockaddr_storage & /*laddr*/, + const struct sockaddr_storage & /*eaddr*/, uint32_t /*mode*/) { #if 0 @@ -1621,8 +1616,8 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() #endif uint8_t isstable = 0; - struct sockaddr_in tmpaddr; - sockaddr_clear(&tmpaddr); + struct sockaddr_storage tmpaddr; + sockaddr_storage_clear(tmpaddr); if (mDhtStunner) { @@ -1631,11 +1626,11 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() if (mDhtStunner->getExternalAddr(tmpaddr, isstable)) { RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ - mNetStateBox.setAddressStunDht(&tmpaddr, isstable); + mNetStateBox.setAddressStunDht(tmpaddr, isstable); #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() DhtStunner: "; - std::cerr << rs_inet_ntoa(tmpaddr.sin_addr) << ":" << htons(tmpaddr.sin_port); + std::cerr << sockaddr_storage_tostring(tmpaddr); std::cerr << " Stable: " << (uint32_t) isstable; std::cerr << std::endl; #endif @@ -1650,11 +1645,11 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() if (mProxyStunner->getExternalAddr(tmpaddr, isstable)) { RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ - mNetStateBox.setAddressStunProxy(&tmpaddr, isstable); + mNetStateBox.setAddressStunProxy(tmpaddr, isstable); #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() ProxyStunner: "; - std::cerr << rs_inet_ntoa(tmpaddr.sin_addr) << ":" << htons(tmpaddr.sin_port); + std::cerr << sockaddr_storage_tostring(tmpaddr); std::cerr << " Stable: " << (uint32_t) isstable; std::cerr << std::endl; #endif @@ -1826,7 +1821,7 @@ void p3NetMgrIMPL::updateNetStateBox_startup() RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ /* fill in the data */ - struct sockaddr_in tmpip; + struct sockaddr_storage tmpip; /* net Assist */ if (netAssistExtAddress(tmpip)) @@ -1838,19 +1833,18 @@ void p3NetMgrIMPL::updateNetStateBox_startup() std::cerr << std::endl; #endif - if (isValidNet(&(tmpip.sin_addr))) + if (sockaddr_storage_isValidNet(tmpip)) { #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_startup() "; - std::cerr << "netAssist Returned: " << rs_inet_ntoa(tmpip.sin_addr); - std::cerr << ":" << ntohs(tmpip.sin_port); + std::cerr << "netAssist Returned: " << sockaddr_storage_tostring(tmpip); std::cerr << std::endl; #endif - mNetStateBox.setAddressUPnP(true, &tmpip); + mNetStateBox.setAddressUPnP(true, tmpip); } else { - mNetStateBox.setAddressUPnP(false, &tmpip); + mNetStateBox.setAddressUPnP(false, tmpip); #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_startup() "; std::cerr << "ERROR Bad Address supplied from netAssistExternalAddress()"; @@ -1865,30 +1859,30 @@ void p3NetMgrIMPL::updateNetStateBox_startup() std::cerr << " netAssistExtAddress() is not active"; std::cerr << std::endl; #endif - mNetStateBox.setAddressUPnP(false, &tmpip); + mNetStateBox.setAddressUPnP(false, tmpip); } /* ExtAddrFinder */ if (mUseExtAddrFinder) { - bool extFinderOk = mExtAddrFinder->hasValidIP(&(tmpip.sin_addr)); + bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip); if (extFinderOk) { /* best guess at port */ - tmpip.sin_port = mNetFlags.mLocalAddr.sin_port; + sockaddr_storage_setport(tmpip, sockaddr_storage_port(mNetFlags.mLocalAddr)); #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_startup() "; - std::cerr << "ExtAddrFinder Returned: " << rs_inet_ntoa(tmpip.sin_addr); + std::cerr << "ExtAddrFinder Returned: " << sockaddr_storage_iptostring(tmpip); std::cerr << std::endl; #endif - mNetStateBox.setAddressWebIP(true, &tmpip); + mNetStateBox.setAddressWebIP(true, tmpip); } else { - mNetStateBox.setAddressWebIP(false, &tmpip); + mNetStateBox.setAddressWebIP(false, tmpip); #ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_startup() "; std::cerr << " ExtAddrFinder hasn't found an address yet"; @@ -1903,7 +1897,7 @@ void p3NetMgrIMPL::updateNetStateBox_startup() std::cerr << " ExtAddrFinder is not active"; std::cerr << std::endl; #endif - mNetStateBox.setAddressWebIP(false, &tmpip); + mNetStateBox.setAddressWebIP(false, tmpip); } diff --git a/libretroshare/src/pqi/p3netmgr.h b/libretroshare/src/pqi/p3netmgr.h index 823bd64b7..b8ca67907 100644 --- a/libretroshare/src/pqi/p3netmgr.h +++ b/libretroshare/src/pqi/p3netmgr.h @@ -68,8 +68,8 @@ class pqiNetStatus uint32_t mDhtNetworkSize; uint32_t mDhtRsNetworkSize; - struct sockaddr_in mLocalAddr; // percieved ext addr. - struct sockaddr_in mExtAddr; // percieved ext addr. + struct sockaddr_storage mLocalAddr; // percieved ext addr. + struct sockaddr_storage mExtAddr; // percieved ext addr. bool mResetReq; // Not Used yet!. @@ -119,8 +119,8 @@ virtual bool setVisState(uint32_t visState) = 0; // Switch DHT On/Off. virtual bool netAssistFriend(const std::string &id, bool on) = 0; -virtual bool netAssistKnownPeer(const std::string &id, const struct sockaddr_in &addr, uint32_t flags) = 0; -virtual bool netAssistBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age) = 0; +virtual bool netAssistKnownPeer(const std::string &id, const struct sockaddr_storage &addr, uint32_t flags) = 0; +virtual bool netAssistBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age) = 0; virtual bool netAssistStatusUpdate(const std::string &id, int mode) = 0; /* Get Network State */ @@ -175,8 +175,8 @@ virtual bool setVisState(uint32_t visState); // Switch DHT On/Off. virtual bool netAssistFriend(const std::string &id, bool on); -virtual bool netAssistKnownPeer(const std::string &id, const struct sockaddr_in &addr, uint32_t flags); -virtual bool netAssistBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age); +virtual bool netAssistKnownPeer(const std::string &id, const struct sockaddr_storage &addr, uint32_t flags); +virtual bool netAssistBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age); virtual bool netAssistStatusUpdate(const std::string &id, int mode); /* Get Network State */ @@ -214,8 +214,8 @@ void setAddrAssist(pqiAddrAssist *dhtStun, pqiAddrAssist *proxyStun); void tick(); // THESE MIGHT BE ADDED TO INTERFACE. -bool setLocalAddress(struct sockaddr_in addr); -bool setExtAddress(struct sockaddr_in addr); +bool setLocalAddress(const struct sockaddr_storage &addr); +bool setExtAddress(const struct sockaddr_storage &addr); /*************** Setup ***************************/ void addNetAssistConnect(uint32_t type, pqiNetAssistConnect *); @@ -262,13 +262,13 @@ bool netAssistConnectStats(uint32_t &netsize, uint32_t &localnetsize); void netAssistTick(); /* Assist Firewall */ -bool netAssistExtAddress(struct sockaddr_in &extAddr); +bool netAssistExtAddress(struct sockaddr_storage &extAddr); bool netAssistFirewallPorts(uint16_t iport, uint16_t eport); /* Assist Connect */ //virtual bool netAssistFriend(std::string id, bool on); (PUBLIC) -bool netAssistSetAddress( struct sockaddr_in &laddr, - struct sockaddr_in &eaddr, +bool netAssistSetAddress(const struct sockaddr_storage &laddr, + const struct sockaddr_storage &eaddr, uint32_t mode); bool netAssistAttach(bool on); @@ -326,8 +326,8 @@ private: void netStatusReset_locked(); - struct sockaddr_in mLocalAddr; - struct sockaddr_in mExtAddr; + struct sockaddr_storage mLocalAddr; + struct sockaddr_storage mExtAddr; uint32_t mNetMode; uint32_t mVisState; diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index 272bef347..fb5d2c425 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -68,6 +68,7 @@ const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 4; /**** * #define PEER_DEBUG 1 ***/ +#define PEER_DEBUG 1 #define MAX_AVAIL_PERIOD 230 //times a peer stay in available state when not connected #define MIN_RETRY_PERIOD 140 @@ -80,8 +81,8 @@ peerState::peerState() netMode(RS_NET_MODE_UNKNOWN), visState(RS_VIS_STATE_STD), lastcontact(0), hiddenNode(false), hiddenPort(0) { - sockaddr_clear(&localaddr); - sockaddr_clear(&serveraddr); + sockaddr_storage_clear(localaddr); + sockaddr_storage_clear(serveraddr); return; } @@ -91,9 +92,13 @@ std::string textPeerConnectState(peerState &state) std::string out = "Id: " + state.id + "\n"; rs_sprintf_append(out, "NetMode: %lu\n", state.netMode); rs_sprintf_append(out, "VisState: %lu\n", state.visState); - rs_sprintf_append(out, "laddr: %s:%u\n", rs_inet_ntoa(state.localaddr.sin_addr).c_str(), ntohs(state.localaddr.sin_port)); - rs_sprintf_append(out, "eaddr: %s:%u\n", rs_inet_ntoa(state.serveraddr.sin_addr).c_str(), ntohs(state.serveraddr.sin_port)); - + + out += "laddr: "; + out += sockaddr_storage_tostring(state.localaddr); + out += "\neaddr: "; + out += sockaddr_storage_tostring(state.serveraddr); + out += "\n"; + return out; } @@ -122,9 +127,12 @@ p3PeerMgrIMPL::p3PeerMgrIMPL( const std::string& ssl_own_id, lastGroupId = 1; // setup default ProxyServerAddress. - sockaddr_clear(&mProxyServerAddress); - inet_aton("127.0.0.1", &(mProxyServerAddress.sin_addr)); - mProxyServerAddress.sin_port = htons(9100); + sockaddr_storage_clear(mProxyServerAddress); + sockaddr_storage_ipv4_aton(mProxyServerAddress, "127.0.0.1"); + sockaddr_storage_ipv4_setport(mProxyServerAddress, 9100); + + //inet_aton("127.0.0.1", &(mProxyServerAddress.sin_addr)); + //mProxyServerAddress.sin_port = htons(9100); } @@ -158,10 +166,13 @@ bool p3PeerMgrIMPL::setupHiddenNode(const std::string &hiddenAddress, const uint setOwnVisState(RS_VIS_STATE_GRAY); // Force the Port. - struct sockaddr_in loopback; - sockaddr_clear(&loopback); - inet_aton("127.0.0.1", &(loopback.sin_addr)); - loopback.sin_port = htons(hiddenPort); + struct sockaddr_storage loopback; + sockaddr_storage_clear(loopback); + sockaddr_storage_ipv4_aton(loopback, "127.0.0.1"); + sockaddr_storage_ipv4_setport(loopback, hiddenPort); + + //inet_aton("127.0.0.1", &(loopback.sin_addr)); + //loopback.sin_port = htons(hiddenPort); setLocalAddress(AuthSSL::getAuthSSL()->OwnId(), loopback); @@ -206,7 +217,7 @@ bool p3PeerMgrIMPL::setOwnVisState(uint32_t visState) rslog(RSL_WARNING, p3peermgrzone, out); #ifdef PEER_DEBUG - std::cerr << out.str() << std::endl; + std::cerr << out.c_str() << std::endl; #endif if (mOwnState.visState != visState) { @@ -371,7 +382,7 @@ bool p3PeerMgrIMPL::setHiddenDomainPort(const std::string &ssl_id, const std::st return true; } -bool p3PeerMgrIMPL::setProxyServerAddress(const struct sockaddr_in &proxy_addr) +bool p3PeerMgrIMPL::setProxyServerAddress(const struct sockaddr_storage &proxy_addr) { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ @@ -379,8 +390,7 @@ bool p3PeerMgrIMPL::setProxyServerAddress(const struct sockaddr_in &proxy_addr) return true; } - -bool p3PeerMgrIMPL::getProxyAddress(const std::string &ssl_id, struct sockaddr_in &proxy_addr, std::string &domain_addr, uint16_t &domain_port) +bool p3PeerMgrIMPL::getProxyAddress(const std::string &ssl_id, struct sockaddr_storage &proxy_addr, std::string &domain_addr, uint16_t &domain_port) { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ @@ -403,8 +413,6 @@ bool p3PeerMgrIMPL::getProxyAddress(const std::string &ssl_id, struct sockaddr_i proxy_addr = mProxyServerAddress; return true; } - - // Placeholder until we implement this functionality. uint32_t p3PeerMgrIMPL::getConnectionType(const std::string &/*sslId*/) @@ -526,7 +534,7 @@ void p3PeerMgrIMPL::getOthersList(std::list &peers) int p3PeerMgrIMPL::getConnectAddresses(const std::string &id, - struct sockaddr_in &lAddr, struct sockaddr_in &eAddr, + struct sockaddr_storage &lAddr, struct sockaddr_storage &eAddr, pqiIpAddrSet &histAddrs, std::string &dyndns) { @@ -906,12 +914,12 @@ bool p3PeerMgrIMPL::addNeighbour(std::string id) * as it doesn't call back to there. */ -bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_in &localAddr, const struct sockaddr_in &extAddr) +bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_storage &localAddr, const struct sockaddr_storage &extAddr) { std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress("; - std::cerr << rs_inet_ntoa(localAddr.sin_addr) << ":" << htons(localAddr.sin_port); + std::cerr << sockaddr_storage_tostring(localAddr); std::cerr << ", "; - std::cerr << rs_inet_ntoa(extAddr.sin_addr) << ":" << htons(extAddr.sin_port); + std::cerr << sockaddr_storage_tostring(extAddr); std::cerr << ")" << std::endl; { @@ -952,19 +960,18 @@ bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_in &localAddr, const std::cerr << " as MANUAL FORWARD Mode (ERROR - SHOULD NOT BE TRIGGERED: TRY_EXT_MODE)"; std::cerr << std::endl; std::cerr << "Address is Now: "; - std::cerr << rs_inet_ntoa(mOwnState.serveraddr.sin_addr); - std::cerr << ":" << htons(mOwnState.serveraddr.sin_port); + std::cerr << sockaddr_storage_tostring(mOwnState.serveraddr); std::cerr << std::endl; } else if (mOwnState.netMode & RS_NET_MODE_EXT) { - mOwnState.serveraddr.sin_addr.s_addr = extAddr.sin_addr.s_addr; + sockaddr_storage_copyip(mOwnState.serveraddr,extAddr); + std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress() Disabling Update of Server Port "; std::cerr << " as MANUAL FORWARD Mode"; std::cerr << std::endl; std::cerr << "Address is Now: "; - std::cerr << rs_inet_ntoa(mOwnState.serveraddr.sin_addr); - std::cerr << ":" << htons(mOwnState.serveraddr.sin_port); + std::cerr << sockaddr_storage_tostring(mOwnState.serveraddr); std::cerr << std::endl; } else @@ -982,7 +989,7 @@ bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_in &localAddr, const -bool p3PeerMgrIMPL::setLocalAddress(const std::string &id, struct sockaddr_in addr) +bool p3PeerMgrIMPL::setLocalAddress(const std::string &id, const struct sockaddr_storage &addr) { bool changed = false; @@ -990,8 +997,7 @@ bool p3PeerMgrIMPL::setLocalAddress(const std::string &id, struct sockaddr_in { { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ - if (mOwnState.localaddr.sin_addr.s_addr != addr.sin_addr.s_addr || - mOwnState.localaddr.sin_port != addr.sin_port) + if (!sockaddr_storage_same(mOwnState.localaddr, addr)) { mOwnState.localaddr = addr; changed = true; @@ -1023,8 +1029,8 @@ bool p3PeerMgrIMPL::setLocalAddress(const std::string &id, struct sockaddr_in } /* "it" points to peer */ - if ((it->second.localaddr.sin_addr.s_addr != addr.sin_addr.s_addr) || - (it->second.localaddr.sin_port != addr.sin_port)) { + if (!sockaddr_storage_same(it->second.localaddr, addr)) + { it->second.localaddr = addr; changed = true; } @@ -1044,7 +1050,7 @@ bool p3PeerMgrIMPL::setLocalAddress(const std::string &id, struct sockaddr_in return changed; } -bool p3PeerMgrIMPL::setExtAddress(const std::string &id, struct sockaddr_in addr) +bool p3PeerMgrIMPL::setExtAddress(const std::string &id, const struct sockaddr_storage &addr) { bool changed = false; @@ -1052,8 +1058,7 @@ bool p3PeerMgrIMPL::setExtAddress(const std::string &id, struct sockaddr_in a { { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ - if (mOwnState.serveraddr.sin_addr.s_addr != addr.sin_addr.s_addr || - mOwnState.serveraddr.sin_port != addr.sin_port) + if (!sockaddr_storage_same(mOwnState.serveraddr, addr)) { mOwnState.serveraddr = addr; changed = true; @@ -1080,8 +1085,8 @@ bool p3PeerMgrIMPL::setExtAddress(const std::string &id, struct sockaddr_in a } /* "it" points to peer */ - if ((it->second.serveraddr.sin_addr.s_addr != addr.sin_addr.s_addr) || - (it->second.serveraddr.sin_port != addr.sin_port)) { + if (!sockaddr_storage_same(it->second.serveraddr, addr)) + { it->second.serveraddr = addr; changed = true; } @@ -1175,7 +1180,9 @@ bool p3PeerMgrIMPL::updateAddressList(const std::string& id, const pqiIpAddrS #ifdef PEER_DEBUG std::cerr << "p3PeerMgrIMPL::setLocalAddress() Updated Address for: " << id; std::cerr << std::endl; - it->second.ipAddrs.printAddrs(std::cerr); + std::string addrstr; + it->second.ipAddrs.printAddrs(addrstr); + std::cerr << addrstr; std::cerr << std::endl; #endif @@ -1206,7 +1213,7 @@ bool p3PeerMgrIMPL::updateCurrentAddress(const std::string& id, const pqiIpAd } } - if (isPrivateNet(&(addr.mAddr.sin_addr))) + if (sockaddr_storage_isPrivateNet(addr.mAddr)) { it->second.ipAddrs.updateLocalAddrs(addr); it->second.localaddr = addr.mAddr; @@ -1220,7 +1227,9 @@ bool p3PeerMgrIMPL::updateCurrentAddress(const std::string& id, const pqiIpAd #ifdef PEER_DEBUG std::cerr << "p3PeerMgrIMPL::updatedCurrentAddress() Updated Address for: " << id; std::cerr << std::endl; - it->second.ipAddrs.printAddrs(std::cerr); + std::string addrstr; + it->second.ipAddrs.printAddrs(addrstr); + std::cerr << addrstr; std::cerr << std::endl; #endif @@ -1465,11 +1474,11 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) item->visState = mOwnState.visState; item->lastContact = mOwnState.lastcontact; - item->currentlocaladdr = mOwnState.localaddr; - item->currentremoteaddr = mOwnState.serveraddr; - item->dyndns = mOwnState.dyndns; - mOwnState.ipAddrs.mLocal.loadTlv(item->localAddrList); - mOwnState.ipAddrs.mExt.loadTlv(item->extAddrList); + item->localAddr.addr = mOwnState.localaddr; + item->extAddr.addr = mOwnState.serveraddr; + item->dyndns = mOwnState.dyndns; + mOwnState.ipAddrs.mLocal.loadTlv(item->localAddrList); + mOwnState.ipAddrs.mExt.loadTlv(item->extAddrList); item->domain_addr = mOwnState.hiddenDomain; item->domain_port = mOwnState.hiddenPort; @@ -1495,8 +1504,8 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) item->netMode = (it->second).netMode; item->visState = (it->second).visState; item->lastContact = (it->second).lastcontact; - item->currentlocaladdr = (it->second).localaddr; - item->currentremoteaddr = (it->second).serveraddr; + item->localAddr.addr = (it->second).localaddr; + item->extAddr.addr = (it->second).serveraddr; item->dyndns = (it->second).dyndns; (it->second).ipAddrs.mLocal.loadTlv(item->localAddrList); (it->second).ipAddrs.mExt.loadTlv(item->extAddrList); @@ -1629,8 +1638,8 @@ bool p3PeerMgrIMPL::loadList(std::list& load) } else { - setLocalAddress(pitem->pid, pitem->currentlocaladdr); - setExtAddress(pitem->pid, pitem->currentremoteaddr); + setLocalAddress(pitem->pid, pitem->localAddr.addr); + setExtAddress(pitem->pid, pitem->extAddr.addr); setDynDNS (pitem->pid, pitem->dyndns); /* convert addresses */ diff --git a/libretroshare/src/pqi/p3peermgr.h b/libretroshare/src/pqi/p3peermgr.h index dc20830ca..8d8fcee95 100644 --- a/libretroshare/src/pqi/p3peermgr.h +++ b/libretroshare/src/pqi/p3peermgr.h @@ -93,8 +93,8 @@ class peerState uint32_t visState; /* STD, GRAY, DARK */ - struct sockaddr_in localaddr; - struct sockaddr_in serveraddr; + struct sockaddr_storage localaddr; + struct sockaddr_storage serveraddr; std::string dyndns; time_t lastcontact; @@ -161,8 +161,8 @@ virtual bool assignPeersToGroup(const std::string &groupId, const std::list mFriendsPermissionFlags ; // permission flags for each gpg key - struct sockaddr_in mProxyServerAddress; + struct sockaddr_storage mProxyServerAddress; }; #endif // MRK_PQI_PEER_MANAGER_HEADER diff --git a/libretroshare/src/pqi/p3upnpmgr.h b/libretroshare/src/pqi/p3upnpmgr.h index 99640d4f0..47b95d5cc 100644 --- a/libretroshare/src/pqi/p3upnpmgr.h +++ b/libretroshare/src/pqi/p3upnpmgr.h @@ -50,8 +50,8 @@ virtual void setInternalPort(unsigned short iport_in) = 0; virtual void setExternalPort(unsigned short eport_in) = 0; /* as determined by uPnP */ -virtual bool getInternalAddress(struct sockaddr_in &addr) = 0; -virtual bool getExternalAddress(struct sockaddr_in &addr) = 0; +virtual bool getInternalAddress(struct sockaddr_storage &addr) = 0; +virtual bool getExternalAddress(struct sockaddr_storage &addr) = 0; }; diff --git a/libretroshare/src/pqi/pqi_base.h b/libretroshare/src/pqi/pqi_base.h index 8bb563a77..6f1b90e67 100644 --- a/libretroshare/src/pqi/pqi_base.h +++ b/libretroshare/src/pqi/pqi_base.h @@ -351,17 +351,17 @@ public: virtual ~NetInterface() { return; } -virtual int connect(struct sockaddr_in raddr) = 0; +virtual int connect(const struct sockaddr_storage &raddr) = 0; virtual int listen() = 0; virtual int stoplistening() = 0; virtual int disconnect() = 0; virtual int reset() = 0; virtual std::string PeerId() { return peerId; } -virtual int getConnectAddress(struct sockaddr_in &raddr) = 0; +virtual int getConnectAddress(struct sockaddr_storage &raddr) = 0; virtual bool connect_parameter(uint32_t type, uint32_t value) = 0; virtual bool connect_parameter(uint32_t /* type */ , const std::string & /* value */ ) { return false; } // not generally used. -virtual bool connect_additional_address(uint32_t /*type*/, struct sockaddr_in * /*addr*/) { return false; } // only needed by udp. +virtual bool connect_additional_address(uint32_t /*type*/, const struct sockaddr_storage & /*addr*/) { return false; } // only needed by udp. protected: PQInterface *parent() { return p; } diff --git a/libretroshare/src/pqi/pqiassist.h b/libretroshare/src/pqi/pqiassist.h index 3bf984f8d..2823571b7 100644 --- a/libretroshare/src/pqi/pqiassist.h +++ b/libretroshare/src/pqi/pqiassist.h @@ -89,8 +89,8 @@ virtual void setInternalPort(unsigned short iport_in) = 0; virtual void setExternalPort(unsigned short eport_in) = 0; /* as determined by uPnP */ -virtual bool getInternalAddress(struct sockaddr_in &addr) = 0; -virtual bool getExternalAddress(struct sockaddr_in &addr) = 0; +virtual bool getInternalAddress(struct sockaddr_storage &addr) = 0; +virtual bool getExternalAddress(struct sockaddr_storage &addr) = 0; /* New Port Forward interface to support as many ports as necessary */ @@ -109,7 +109,7 @@ class pqiNetAssistPeerShare public: /* share Addresses for various reasons (bad peers, etc) */ -virtual void updatePeer(std::string id, struct sockaddr_in addr, int type, int reason, int age) = 0; +virtual void updatePeer(std::string id, const struct sockaddr_storage &addr, int type, int reason, int age) = 0; }; @@ -126,7 +126,7 @@ class pqiAddrAssist pqiAddrAssist() { return; } virtual ~pqiAddrAssist() { return; } -virtual bool getExternalAddr(struct sockaddr_in &remote, uint8_t &stable) = 0; +virtual bool getExternalAddr(struct sockaddr_storage &remote, uint8_t &stable) = 0; virtual void setRefreshPeriod(int32_t period) = 0; virtual int tick() = 0; /* for internal accounting */ @@ -163,14 +163,14 @@ virtual bool findPeer(std::string id) = 0; virtual bool dropPeer(std::string id) = 0; /* add non-active peers (can still toggle active/non-active via above) */ -virtual int addBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age) = 0; -virtual int addKnownPeer(const std::string &pid, const struct sockaddr_in &addr, uint32_t flags) = 0; +virtual int addBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age) = 0; +virtual int addKnownPeer(const std::string &pid, const struct sockaddr_storage &addr, uint32_t flags) = 0; virtual void ConnectionFeedback(std::string pid, int mode) = 0; /* extract current peer status */ virtual bool getPeerStatus(std::string id, - struct sockaddr_in &laddr, struct sockaddr_in &raddr, + struct sockaddr_storage &laddr, struct sockaddr_storage &raddr, uint32_t &type, uint32_t &mode) = 0; // DEPRECIATE. virtual bool setAttachMode(bool on) = 0; // FIXUP. diff --git a/libretroshare/src/pqi/pqibin.cc b/libretroshare/src/pqi/pqibin.cc index 668188693..f7a7c32af 100644 --- a/libretroshare/src/pqi/pqibin.cc +++ b/libretroshare/src/pqi/pqibin.cc @@ -528,11 +528,10 @@ NetBinDummy::NetBinDummy(PQInterface *parent, std::string id, uint32_t t) } // Net Interface -int NetBinDummy::connect(struct sockaddr_in raddr) +int NetBinDummy::connect(const struct sockaddr_storage &raddr) { std::cerr << "NetBinDummy::connect("; - std::cerr << rs_inet_ntoa(raddr.sin_addr) << ":"; - std::cerr << htons(raddr.sin_port); + std::cerr << sockaddr_storage_tostring(raddr); std::cerr << ") "; printNetBinID(std::cerr, PeerId(), type); std::cerr << std::endl; diff --git a/libretroshare/src/pqi/pqibin.h b/libretroshare/src/pqi/pqibin.h index 55479e297..bcab734c7 100644 --- a/libretroshare/src/pqi/pqibin.h +++ b/libretroshare/src/pqi/pqibin.h @@ -193,7 +193,7 @@ public: virtual ~NetBinDummy() { return; } // Net Interface -virtual int connect(struct sockaddr_in raddr); +virtual int connect(const struct sockaddr_storage &raddr); virtual int listen(); virtual int stoplistening(); virtual int disconnect(); @@ -204,7 +204,7 @@ virtual bool connect_parameter(uint32_t type, uint32_t value) (void) value; /* suppress unused parameter warning */ return false; } -virtual int getConnectAddress(struct sockaddr_in &raddr) +virtual int getConnectAddress(struct sockaddr_storage &raddr) { (void) raddr; /* suppress unused parameter warning */ return 0; diff --git a/libretroshare/src/pqi/pqiipset.cc b/libretroshare/src/pqi/pqiipset.cc index f7bfdfe3b..fa6e7c543 100644 --- a/libretroshare/src/pqi/pqiipset.cc +++ b/libretroshare/src/pqi/pqiipset.cc @@ -29,15 +29,14 @@ bool pqiIpAddress::sameAddress(const pqiIpAddress &a) const { - return ((mAddr.sin_addr.s_addr == a.mAddr.sin_addr.s_addr) && - (mAddr.sin_port == a.mAddr.sin_port)); + return sockaddr_storage_same(mAddr, a.mAddr); } bool pqiIpAddress::validAddress() const { /* filter for unlikely addresses */ - if(isLoopbackNet(&(mAddr.sin_addr))) + if(sockaddr_storage_isLoopbackNet(mAddr)) { #ifdef IPADDR_DEBUG std::cerr << "pqiIpAddress::validAddress() ip parameter is loopback: disgarding." << std::endl ; @@ -45,7 +44,7 @@ bool pqiIpAddress::validAddress() const return false; } - if(mAddr.sin_addr.s_addr == 0 || mAddr.sin_addr.s_addr == 1 || mAddr.sin_port == 0) + if(sockaddr_storage_isnull(mAddr)) { #ifdef IPADDR_DEBUG std::cerr << "pqiIpAddress::validAddress() ip parameter is 0.0.0.0/1, or port is 0, ignoring." << std::endl; @@ -168,7 +167,7 @@ void pqiIpAddrList::extractFromTlv(const RsTlvIpAddrSet &tlvAddrs) for(it = tlvAddrs.addrs.begin(); it != tlvAddrs.addrs.end() ; ++it) { pqiIpAddress addr; - addr.mAddr = it->addr; + addr.mAddr = it->addr.addr; addr.mSeenTime = it->seenTime; addr.mSrc = it->source; @@ -183,7 +182,7 @@ void pqiIpAddrList::loadTlv(RsTlvIpAddrSet &tlvAddrs) for(it = mAddrs.begin(); it != mAddrs.end() ; ++it) { RsTlvIpAddressInfo addr; - addr.addr = it->mAddr; + addr.addr.addr = it->mAddr; addr.seenTime = it->mSeenTime; addr.source = it->mSrc; @@ -199,7 +198,8 @@ void pqiIpAddrList::printIpAddressList(std::string &out) const time_t now = time(NULL); for(it = mAddrs.begin(); it != mAddrs.end(); it++) { - rs_sprintf_append(out, "%s:%u ( %ld old)\n", rs_inet_ntoa(it->mAddr.sin_addr).c_str(), ntohs(it->mAddr.sin_port), now - it->mSeenTime); + out += sockaddr_storage_tostring(it->mAddr); + rs_sprintf_append(out, "( %ld old)\n", now - it->mSeenTime); } return; } diff --git a/libretroshare/src/pqi/pqiipset.h b/libretroshare/src/pqi/pqiipset.h index 863ed8739..f20dc1a5f 100644 --- a/libretroshare/src/pqi/pqiipset.h +++ b/libretroshare/src/pqi/pqiipset.h @@ -37,7 +37,7 @@ class pqiIpAddress bool sameAddress(const pqiIpAddress &a) const; bool validAddress() const; - struct sockaddr_in mAddr; + struct sockaddr_storage mAddr; time_t mSeenTime; uint32_t mSrc; }; diff --git a/libretroshare/src/pqi/pqilistener.h b/libretroshare/src/pqi/pqilistener.h index 0e0b980ee..46e030206 100644 --- a/libretroshare/src/pqi/pqilistener.h +++ b/libretroshare/src/pqi/pqilistener.h @@ -38,7 +38,7 @@ virtual ~pqilistener() { return; } virtual int tick() { return 1; } virtual int status() { return 1; } -virtual int setListenAddr(struct sockaddr_in addr) +virtual int setListenAddr(const struct sockaddr_storage &addr) { (void) addr; /* suppress unused parameter warning */ return 1; diff --git a/libretroshare/src/pqi/pqimonitor.cc b/libretroshare/src/pqi/pqimonitor.cc index 433f9d862..e76d3f6a2 100644 --- a/libretroshare/src/pqi/pqimonitor.cc +++ b/libretroshare/src/pqi/pqimonitor.cc @@ -62,23 +62,21 @@ void pqiConnectCbDummy::peerStatus(std::string id, const pqiIpAddrSet &addrs, } void pqiConnectCbDummy::peerConnectRequest(std::string id, - struct sockaddr_in raddr, uint32_t source) + const struct sockaddr_storage &raddr, uint32_t source) { std::cerr << "pqiConnectCbDummy::peerConnectRequest()"; std::cerr << " id: " << id; - std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr); - std::cerr << ":" << ntohs(raddr.sin_port); + std::cerr << " raddr: " << sockaddr_storage_tostring(raddr); std::cerr << " source: " << source; std::cerr << std::endl; } #if 0 -void pqiConnectCbDummy::stunStatus(std::string id, struct sockaddr_in raddr, +void pqiConnectCbDummy::stunStatus(std::string id, const struct sockaddr_storage *raddr, uint32_t type, uint32_t flags) { std::cerr << "pqiConnectCbDummy::stunStatus()"; - std::cerr << " idhash: " << RsUtil::BinToHex(id) << " raddr: " << rs_inet_ntoa(raddr.sin_addr); - std::cerr << ":" << ntohs(raddr.sin_port); + std::cerr << " idhash: " << RsUtil::BinToHex(id) << " raddr: " << sockaddr_storage_tostring(raddr); std::cerr << " type: " << type; std::cerr << " flags: " << flags; std::cerr << std::endl; diff --git a/libretroshare/src/pqi/pqimonitor.h b/libretroshare/src/pqi/pqimonitor.h index edf53d49f..3016308db 100644 --- a/libretroshare/src/pqi/pqimonitor.h +++ b/libretroshare/src/pqi/pqimonitor.h @@ -156,11 +156,11 @@ virtual ~pqiConnectCb() { return; } virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, uint32_t type, uint32_t flags, uint32_t source) = 0; -virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, - struct sockaddr_in proxyaddr, struct sockaddr_in srcaddr, +virtual void peerConnectRequest(std::string id, const struct sockaddr_storage &raddr, + const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth) = 0; -//virtual void stunStatus(std::string id, struct sockaddr_in raddr, uint32_t type, uint32_t flags) = 0; +//virtual void stunStatus(std::string id, const struct sockaddr_storage &raddr, uint32_t type, uint32_t flags) = 0; }; @@ -174,9 +174,9 @@ virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, uint32_t type, uint32_t mode, uint32_t source); virtual void peerConnectRequest(std::string id, - struct sockaddr_in raddr, uint32_t source); + const struct sockaddr_storage &raddr, uint32_t source); -//virtual void stunStatus(std::string id, struct sockaddr_in raddr, uint32_t type, uint32_t flags); +//virtual void stunStatus(std::string id, const struct sockaddr_storage &raddr, uint32_t type, uint32_t flags); }; @@ -184,7 +184,7 @@ virtual void peerConnectRequest(std::string id, class pqiNetListener { public: -virtual bool resetListener(struct sockaddr_in &local) = 0; +virtual bool resetListener(const struct sockaddr_storage &local) = 0; }; diff --git a/libretroshare/src/pqi/pqinetstatebox.cc b/libretroshare/src/pqi/pqinetstatebox.cc index 22bd41134..fa265dc12 100644 --- a/libretroshare/src/pqi/pqinetstatebox.cc +++ b/libretroshare/src/pqi/pqinetstatebox.cc @@ -6,15 +6,14 @@ // External Interface. -void pqiNetStateBox::setAddressStunDht(struct sockaddr_in *addr, bool stable) +void pqiNetStateBox::setAddressStunDht(const struct sockaddr_storage &addr, bool stable) { if ((!mStunDhtSet) || (mStunDhtStable != stable) || - (addr->sin_addr.s_addr != mStunDhtAddr.sin_addr.s_addr) || - (addr->sin_port != mStunDhtAddr.sin_port)) + (!sockaddr_storage_same(addr, mStunDhtAddr))) { mStunDhtSet = true; mStunDhtStable = stable; - mStunDhtAddr = *addr; + mStunDhtAddr = addr; mStatusOkay = false; } @@ -22,15 +21,13 @@ void pqiNetStateBox::setAddressStunDht(struct sockaddr_in *addr, bool stable) } -void pqiNetStateBox::setAddressStunProxy(struct sockaddr_in *addr, bool stable) +void pqiNetStateBox::setAddressStunProxy(const struct sockaddr_storage &addr, bool stable) { if ((!mStunProxySet) || (mStunProxyStable != stable) || - (addr->sin_addr.s_addr != mStunProxyAddr.sin_addr.s_addr) || - (addr->sin_port != mStunProxyAddr.sin_port)) - + (!sockaddr_storage_same(addr, mStunProxyAddr))) { - if (addr->sin_addr.s_addr == mStunProxyAddr.sin_addr.s_addr) + if (sockaddr_storage_sameip(addr,mStunProxyAddr)) { if (mStunProxyStable != stable) { @@ -44,22 +41,20 @@ void pqiNetStateBox::setAddressStunProxy(struct sockaddr_in *addr, bool stable) mStunProxySet = true; mStunProxyStable = stable; - mStunProxyAddr = *addr; + mStunProxyAddr = addr; mStatusOkay = false; } mStunProxyTS = time(NULL); } -void pqiNetStateBox::setAddressUPnP(bool active, struct sockaddr_in *addr) +void pqiNetStateBox::setAddressUPnP(bool active, const struct sockaddr_storage &addr) { if ((!mUPnPSet) || (mUPnPActive != active) || - (addr->sin_addr.s_addr != mUPnPAddr.sin_addr.s_addr) || - (addr->sin_port != mUPnPAddr.sin_port)) - + (!sockaddr_storage_same(addr, mUPnPAddr))) { mUPnPSet = true; - mUPnPAddr = *addr; + mUPnPAddr = addr; mUPnPActive = active; mStatusOkay = false; @@ -68,15 +63,13 @@ void pqiNetStateBox::setAddressUPnP(bool active, struct sockaddr_in *addr) } -void pqiNetStateBox::setAddressNatPMP(bool active, struct sockaddr_in *addr) +void pqiNetStateBox::setAddressNatPMP(bool active, const struct sockaddr_storage &addr) { if ((!mNatPMPSet) || (mNatPMPActive != active) || - (addr->sin_addr.s_addr != mNatPMPAddr.sin_addr.s_addr) || - (addr->sin_port != mNatPMPAddr.sin_port)) - + (!sockaddr_storage_same(addr, mNatPMPAddr))) { mNatPMPSet = true; - mNatPMPAddr = *addr; + mNatPMPAddr = addr; mNatPMPActive = active; mStatusOkay = false; @@ -86,15 +79,13 @@ void pqiNetStateBox::setAddressNatPMP(bool active, struct sockaddr_in *addr) -void pqiNetStateBox::setAddressWebIP(bool active, struct sockaddr_in *addr) +void pqiNetStateBox::setAddressWebIP(bool active, const struct sockaddr_storage &addr) { if ((!mWebIPSet) || (mWebIPActive != active) || - (addr->sin_addr.s_addr != mWebIPAddr.sin_addr.s_addr) || - (addr->sin_port != mWebIPAddr.sin_port)) - + (!sockaddr_storage_same(addr, mWebIPAddr))) { mWebIPSet = true; - mWebIPAddr = *addr; + mWebIPAddr = addr; mWebIPActive = active; mStatusOkay = false; @@ -188,35 +179,25 @@ void pqiNetStateBox::reset() mStunDhtSet = false; mStunDhtTS = 0; mStunDhtStable = false; - mStunDhtAddr.sin_addr.s_addr = 0 ; - mStunDhtAddr.sin_port = 0 ; - //struct sockaddr_in mStunDhtAddr; + sockaddr_storage_clear(mStunDhtAddr); mStunProxySet = false; mStunProxySemiStable = false; mStunProxyTS = 0; mStunProxyStable = false; - mStunProxyAddr.sin_addr.s_addr = 0 ; - mStunProxyAddr.sin_port = 0 ; - //struct sockaddr_in mStunProxyAddr; + sockaddr_storage_clear(mStunProxyAddr); mUPnPSet = false; mUPnPActive = false; - mUPnPAddr.sin_addr.s_addr = 0 ; - mUPnPAddr.sin_port = 0 ; - //struct sockaddr_in mUPnPAddr; + sockaddr_storage_clear(mUPnPAddr); mNatPMPSet = false; mNatPMPActive = false; - mNatPMPAddr.sin_addr.s_addr = 0 ; - mNatPMPAddr.sin_port = 0 ; - //struct sockaddr_in mNatPMPAddr; + sockaddr_storage_clear(mNatPMPAddr); mWebIPSet = false; mWebIPActive = false; - mWebIPAddr.sin_addr.s_addr = 0 ; - mWebIPAddr.sin_port = 0 ; - //struct sockaddr_in mWebIPAddr; + sockaddr_storage_clear(mWebIPAddr); mPortForwardSet = false; mPortForwarded = false; diff --git a/libretroshare/src/pqi/pqinetstatebox.h b/libretroshare/src/pqi/pqinetstatebox.h index 9a56b975d..9d6f43e55 100644 --- a/libretroshare/src/pqi/pqinetstatebox.h +++ b/libretroshare/src/pqi/pqinetstatebox.h @@ -21,12 +21,12 @@ class pqiNetStateBox void reset(); /* input network bits */ - void setAddressStunDht(struct sockaddr_in *, bool stable); - void setAddressStunProxy(struct sockaddr_in *, bool stable); + void setAddressStunDht(const struct sockaddr_storage &addr, bool stable); + void setAddressStunProxy(const struct sockaddr_storage &addr, bool stable); - void setAddressUPnP(bool active, struct sockaddr_in *addr); - void setAddressNatPMP(bool active, struct sockaddr_in *addr); - void setAddressWebIP(bool active, struct sockaddr_in *addr); + void setAddressUPnP(bool active, const struct sockaddr_storage &addr); + void setAddressNatPMP(bool active, const struct sockaddr_storage &addr); + void setAddressWebIP(bool active, const struct sockaddr_storage &addr); void setPortForwarded(bool active, uint16_t port); @@ -63,13 +63,13 @@ class pqiNetStateBox bool mStunDhtSet; time_t mStunDhtTS; bool mStunDhtStable; - struct sockaddr_in mStunDhtAddr; + struct sockaddr_storage mStunDhtAddr; bool mStunProxySet; time_t mStunProxyTS; bool mStunProxyStable; bool mStunProxySemiStable; - struct sockaddr_in mStunProxyAddr; + struct sockaddr_storage mStunProxyAddr; bool mDhtSet; time_t mDhtTS; @@ -77,17 +77,17 @@ class pqiNetStateBox bool mDhtActive; bool mUPnPSet; - struct sockaddr_in mUPnPAddr; + struct sockaddr_storage mUPnPAddr; bool mUPnPActive; time_t mUPnPTS; bool mNatPMPSet; - struct sockaddr_in mNatPMPAddr; + struct sockaddr_storage mNatPMPAddr; bool mNatPMPActive; time_t mNatPMPTS; bool mWebIPSet; - struct sockaddr_in mWebIPAddr; + struct sockaddr_storage mWebIPAddr; bool mWebIPActive; time_t mWebIPTS; diff --git a/libretroshare/src/pqi/pqinetwork.cc b/libretroshare/src/pqi/pqinetwork.cc index b57db7ce9..4e3567e45 100644 --- a/libretroshare/src/pqi/pqinetwork.cc +++ b/libretroshare/src/pqi/pqinetwork.cc @@ -147,7 +147,7 @@ std::string socket_errorType(int err) #include #include -bool getLocalInterfaces(struct in_addr &/*routeAddr*/, std::list &addrs) +bool getLocalInterfaces_ipv4(struct in_addr &/*routeAddr*/, std::list &addrs) { int sock = 0; struct ifreq ifreq; @@ -328,7 +328,7 @@ std::string socket_errorType(int err) // A function to determine the interfaces on your computer.... // No idea of how to do this in windows.... // see if it compiles. -bool getLocalInterfaces(struct in_addr &routeAddr, std::list &addrs) +bool getLocalInterfaces_ipv4(struct in_addr &routeAddr, std::list &addrs) { // Get the best interface for transport to routeAddr // This interface should be first in list! @@ -556,8 +556,7 @@ int inaddr_cmp(struct sockaddr_in addr1, unsigned long addr2) return 1; } - -bool getPreferredInterface(in_addr &routeAddr, struct in_addr &prefAddr) // returns best addr. +bool getPreferredInterface_ipv4(in_addr &routeAddr, struct in_addr &prefAddr) // returns best addr. { std::list addrs; std::list::iterator it; @@ -572,7 +571,7 @@ bool getPreferredInterface(in_addr &routeAddr, struct in_addr &prefAddr) // ret bool found_priv = false; bool found_ext = false; - if (!getLocalInterfaces(routeAddr, addrs)) + if (!getLocalInterfaces_ipv4(routeAddr, addrs)) { return false; } @@ -686,6 +685,78 @@ bool getPreferredInterface(in_addr &routeAddr, struct in_addr &prefAddr) // ret return false; } + + +bool getPreferredInterface(struct sockaddr_storage &existAddr, struct sockaddr_storage &prefAddr) +{ + struct in_addr existing_addr; + struct in_addr pref_addr; + + { + struct sockaddr_in *eaddr = (sockaddr_in *) &existAddr; + if (eaddr->sin_family != AF_INET) + { + std::cerr << "getPreferredInterface() ERROR only valid for IPv4 for now"; + abort(); + return false; + } + existing_addr = eaddr->sin_addr; + } + + if (getPreferredInterface_ipv4(existing_addr, pref_addr)) + { + /* store into prefAddr */ + sockaddr_storage_clear(prefAddr); + struct sockaddr_in *addr = (sockaddr_in *) &prefAddr; + addr->sin_family = AF_INET; + addr->sin_addr = pref_addr; + addr->sin_port = htons(0); + + return true; + } + return false; +} + + +bool getLocalInterfaces(struct sockaddr_storage &existAddr, std::list &addrs) +{ + struct in_addr existing_addr; + std::list local_addrs; + + { + struct sockaddr_in *eaddr = (sockaddr_in *) &existAddr; + if (eaddr->sin_family != AF_INET) + { + std::cerr << "getLocalInterfaces() ERROR only valid for IPv4 for now"; + abort(); + return false; + } + existing_addr = eaddr->sin_addr; + } + + if (getLocalInterfaces_ipv4(existing_addr, local_addrs)) + { + std::list::iterator it; + for(it = local_addrs.begin(); it != local_addrs.end(); it++) + { + /* store into prefAddr */ + + sockaddr_storage localAddr; + sockaddr_storage_clear(localAddr); + struct sockaddr_in *addr = (sockaddr_in *) &localAddr; + addr->sin_family = AF_INET; + addr->sin_addr = *it; + addr->sin_port = htons(0); + addrs.push_back(localAddr); + } + return true; + } + return false; +} + + + + bool sameNet(const struct in_addr *addr, const struct in_addr *addr2) { #ifdef NET_DEBUG diff --git a/libretroshare/src/pqi/pqinetwork.h b/libretroshare/src/pqi/pqinetwork.h index 9c1be7473..4a48729dc 100644 --- a/libretroshare/src/pqi/pqinetwork.h +++ b/libretroshare/src/pqi/pqinetwork.h @@ -101,8 +101,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); -bool getPreferredInterface(struct in_addr &routeAddr, struct in_addr &prefAddr); // returns best addr. -bool getLocalInterfaces(in_addr &routeAddr, std::list &addrs); // returns all possible addrs. +bool getPreferredInterface(struct sockaddr_storage &existAddr, struct sockaddr_storage &prefAddr); // returns best addr. +bool getLocalInterfaces(struct sockaddr_storage &existAddr, std::list &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); diff --git a/libretroshare/src/pqi/pqiperson.cc b/libretroshare/src/pqi/pqiperson.cc index 6783bd46d..d104e1326 100644 --- a/libretroshare/src/pqi/pqiperson.cc +++ b/libretroshare/src/pqi/pqiperson.cc @@ -33,9 +33,11 @@ const int pqipersonzone = 82371; #include "util/rsstring.h" /**** - * #define PERSON_DEBUG + * #define PERSON_DEBUG 1 ****/ +#define PERSON_DEBUG 1 + pqiperson::pqiperson(std::string id, pqipersongrp *pg) :PQInterface(id), active(false), activepqi(NULL), inConnectAttempt(false), waittimes(0), @@ -217,7 +219,7 @@ int pqiperson::notifyEvent(NetInterface *ni, int newState) /* notify */ if (pqipg) { - struct sockaddr_in remote_peer_address; + struct sockaddr_storage remote_peer_address; pqi->getConnectAddress(remote_peer_address); pqipg->notifyConnect(PeerId(), type, true, remote_peer_address); } @@ -287,8 +289,8 @@ int pqiperson::notifyEvent(NetInterface *ni, int newState) /* notify up */ if (pqipg) { - struct sockaddr_in raddr; - sockaddr_clear(&raddr); + struct sockaddr_storage raddr; + sockaddr_storage_clear(raddr); pqipg->notifyConnect(PeerId(), type, false, raddr); } @@ -366,8 +368,8 @@ int pqiperson::stoplistening() return 1; } -int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, - struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr, +int pqiperson::connect(uint32_t type, const struct sockaddr_storage &raddr, + const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags, uint32_t bandwidth, const std::string &domain_addr, uint16_t domain_port) { @@ -376,9 +378,12 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, { std::string out = "pqiperson::connect() Id: " + PeerId(); rs_sprintf_append(out, " type: %u", type); - rs_sprintf_append(out, " addr: %s:%u", rs_inet_ntoa(raddr.sin_addr).c_str(), ntohs(raddr.sin_port)); - rs_sprintf_append(out, " proxyaddr: %s:%u", rs_inet_ntoa(proxyaddr.sin_addr).c_str(), ntohs(proxyaddr.sin_port)); - rs_sprintf_append(out, " srcaddr: %s:%u", rs_inet_ntoa(srcaddr.sin_addr).c_str(), ntohs(srcaddr.sin_port)); + out += " addr: "; + out += sockaddr_storage_tostring(raddr); + out += " proxyaddr: "; + out += sockaddr_storage_tostring(proxyaddr); + out += " srcaddr: "; + out += sockaddr_storage_tostring(srcaddr); rs_sprintf_append(out, " delay: %u", delay); rs_sprintf_append(out, " period: %u", period); rs_sprintf_append(out, " timeout: %u", timeout); @@ -428,8 +433,8 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, (it->second)->connect_parameter(NET_PARAM_CONNECT_FLAGS, flags); (it->second)->connect_parameter(NET_PARAM_CONNECT_BANDWIDTH, bandwidth); - (it->second)->connect_additional_address(NET_PARAM_CONNECT_PROXY, &proxyaddr); - (it->second)->connect_additional_address(NET_PARAM_CONNECT_SOURCE, &srcaddr); + (it->second)->connect_additional_address(NET_PARAM_CONNECT_PROXY, proxyaddr); + (it->second)->connect_additional_address(NET_PARAM_CONNECT_SOURCE, srcaddr); // These are used by Proxy/Hidden (it->second)->connect_parameter(NET_PARAM_CONNECT_DOMAIN_ADDRESS, domain_addr); diff --git a/libretroshare/src/pqi/pqiperson.h b/libretroshare/src/pqi/pqiperson.h index c2a495656..d3eec1866 100644 --- a/libretroshare/src/pqi/pqiperson.h +++ b/libretroshare/src/pqi/pqiperson.h @@ -65,18 +65,18 @@ public: virtual ~pqiconnect() { return; } // presents a virtual NetInterface -> passes to ni. -virtual int connect(struct sockaddr_in raddr) { return ni->connect(raddr); } +virtual int connect(const struct sockaddr_storage &raddr) { return ni->connect(raddr); } virtual int listen() { return ni -> listen(); } virtual int stoplistening() { return ni -> stoplistening(); } virtual int reset() { return ni -> reset(); } virtual int disconnect() { return ni -> reset(); } virtual bool connect_parameter(uint32_t type, uint32_t value) { return ni -> connect_parameter(type, value);} virtual bool connect_parameter(uint32_t type, std::string value) { return ni -> connect_parameter(type, value);} -virtual bool connect_additional_address(uint32_t type, struct sockaddr_in *addr) { return ni -> connect_additional_address(type, addr);} +virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr) { return ni -> connect_additional_address(type, addr);} -virtual int getConnectAddress(struct sockaddr_in &raddr){ return ni->getConnectAddress(raddr); } +virtual int getConnectAddress(struct sockaddr_storage &raddr){ return ni->getConnectAddress(raddr); } // get the contact from the net side! virtual std::string PeerId() @@ -112,8 +112,8 @@ int reset(); int listen(); int stoplistening(); -int connect(uint32_t type, struct sockaddr_in raddr, - struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr, +int connect(uint32_t type, const struct sockaddr_storage &raddr, + const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags, uint32_t bandwidth, const std::string &domain_addr, uint16_t domain_port); diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index 5738cb046..061a0fb7e 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -43,7 +43,7 @@ static std::list waitingIds; /**** *#define PGRP_DEBUG 1 ****/ -//#define PGRP_DEBUG 1 +#define PGRP_DEBUG 1 #define DEFAULT_DOWNLOAD_KB_RATE (200.0) #define DEFAULT_UPLOAD_KB_RATE (50.0) @@ -184,7 +184,8 @@ int pqipersongrp::init_listener() { /* extract details from */ - struct sockaddr_in laddr = mLinkMgr->getLocalAddress(); + struct sockaddr_storage laddr; + mLinkMgr->getLocalAddress(laddr); RsStackMutex stack(coreMtx); /******* LOCKED MUTEX **********/ pqil = locked_createListener(laddr); @@ -192,7 +193,7 @@ int pqipersongrp::init_listener() return 1; } -bool pqipersongrp::resetListener(struct sockaddr_in &local) +bool pqipersongrp::resetListener(const struct sockaddr_storage &local) { #ifdef PGRP_DEBUG std::cerr << "pqipersongrp::resetListener()" << std::endl; @@ -536,15 +537,15 @@ int pqipersongrp::connectPeer(std::string id /////////////////////////////////////////////////////////// #endif - struct sockaddr_in addr; + struct sockaddr_storage addr; uint32_t delay; uint32_t period; uint32_t timeout; uint32_t type; uint32_t flags = 0 ; - struct sockaddr_in proxyaddr; - struct sockaddr_in srcaddr; + struct sockaddr_storage proxyaddr; + struct sockaddr_storage srcaddr; uint32_t bandwidth; std::string domain_addr; uint16_t domain_port; @@ -560,7 +561,7 @@ int pqipersongrp::connectPeer(std::string id #ifdef PGRP_DEBUG std::cerr << " pqipersongrp::connectPeer() connectAttempt data id: " << id; - std::cerr << " addr: " << rs_inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port); + std::cerr << " addr: " << sockaddr_storage_tostring(addr); std::cerr << " delay: " << delay; std::cerr << " period: " << period; std::cerr << " type: " << type; @@ -611,7 +612,7 @@ int pqipersongrp::connectPeer(std::string id return 1; } -bool pqipersongrp::notifyConnect(std::string id, uint32_t ptype, bool success, struct sockaddr_in raddr) +bool pqipersongrp::notifyConnect(std::string id, uint32_t ptype, bool success, const struct sockaddr_storage &raddr) { uint32_t type = 0; if (ptype == PQI_CONNECT_TCP) @@ -633,7 +634,7 @@ bool pqipersongrp::notifyConnect(std::string id, uint32_t ptype, bool success #include "pqi/pqibin.h" -pqilistener * pqipersongrpDummy::locked_createListener(struct sockaddr_in /*laddr*/) +pqilistener * pqipersongrpDummy::locked_createListener(const struct sockaddr_storage & /*laddr*/) { pqilistener *listener = new pqilistener(); return listener; diff --git a/libretroshare/src/pqi/pqipersongrp.h b/libretroshare/src/pqi/pqipersongrp.h index 5245106fe..fdaaf5bfa 100644 --- a/libretroshare/src/pqi/pqipersongrp.h +++ b/libretroshare/src/pqi/pqipersongrp.h @@ -55,7 +55,7 @@ class pqipersongrp: public pqihandler, public pqiMonitor, public p3ServiceServer /*************************** Setup *************************/ /* pqilistener */ -virtual bool resetListener(struct sockaddr_in &local); // overloaded from pqiNetListener +virtual bool resetListener(const struct sockaddr_storage &local); // overloaded from pqiNetListener int init_listener(); /*************** pqiMonitor callback ***********************/ @@ -84,7 +84,7 @@ int connectPeer(std::string id int tagHeartbeatRecvd(std::string id); /*** callback from children ****/ -bool notifyConnect(std::string id, uint32_t type, bool success, struct sockaddr_in remote_peer_address); +bool notifyConnect(std::string id, uint32_t type, bool success, const struct sockaddr_storage &remote_peer_address); //bool notifyConnect(std::string id, uint32_t type, bool success); // tick interfaces. @@ -98,7 +98,7 @@ virtual bool locked_getCryptoParams(const std::string&, RsPeerCryptoParams&) { r /********* FUNCTIONS to OVERLOAD for specialisation ********/ // THESE NEED TO BE LOCKED UNTIL PQILISTENER IS THREAD-SAFE. -virtual pqilistener *locked_createListener(struct sockaddr_in laddr) = 0; +virtual pqilistener *locked_createListener(const struct sockaddr_storage &laddr) = 0; virtual pqiperson *locked_createPerson(std::string id, pqilistener *listener) = 0; /********* FUNCTIONS to OVERLOAD for specialisation ********/ @@ -131,7 +131,7 @@ class pqipersongrpDummy: public pqipersongrp protected: /********* FUNCTIONS to OVERLOAD for specialisation ********/ -virtual pqilistener *locked_createListener(struct sockaddr_in laddr); +virtual pqilistener *locked_createListener(const struct sockaddr_storage &laddr); virtual pqiperson *locked_createPerson(std::string id, pqilistener *listener); /********* FUNCTIONS to OVERLOAD for specialisation ********/ }; diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 47f6bcc43..10f35d946 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -104,7 +104,7 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) { /* set address to zero */ - sockaddr_clear(&remote_addr); + sockaddr_storage_clear(remote_addr); #ifdef PQISSL_LOG_DEBUG rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissl for PeerId: " + PeerId()); @@ -142,11 +142,10 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) /********** Implementation of NetInterface *************************/ -int pqissl::connect(struct sockaddr_in raddr) +int pqissl::connect(const struct sockaddr_storage &raddr) { // reset failures remote_addr = raddr; - remote_addr.sin_family = AF_INET; return ConnectAttempt(); } @@ -176,9 +175,10 @@ int pqissl::disconnect() return reset(); } -int pqissl::getConnectAddress(struct sockaddr_in &raddr) { +int pqissl::getConnectAddress(struct sockaddr_storage &raddr) { raddr = remote_addr; - return (remote_addr.sin_addr.s_addr == 0); + // TODO. + return (!sockaddr_storage_isnull(remote_addr)); } /* BinInterface version of reset() for pqistreamer */ @@ -267,6 +267,7 @@ int pqissl::reset() bool pqissl::connect_parameter(uint32_t type, const std::string &value) { + (void) value; return false; } @@ -584,7 +585,7 @@ int pqissl::Delay_Connection() int pqissl::Initiate_Connection() { int err; - struct sockaddr_in addr = remote_addr; + struct sockaddr_storage addr = remote_addr; #ifdef PQISSL_LOG_DEBUG rslog(RSL_DEBUG_BASIC, pqisslzone, @@ -644,11 +645,12 @@ int pqissl::Initiate_Connection() { std::string out; - rs_sprintf(out, "pqissl::Initiate_Connection() Connecting To: %s via: %s:%u", PeerId().c_str(), rs_inet_ntoa(addr.sin_addr).c_str(), ntohs(addr.sin_port)); + rs_sprintf(out, "pqissl::Initiate_Connection() Connecting To: %s via: ", PeerId().c_str()); + out += sockaddr_storage_tostring(addr); rslog(RSL_WARNING, pqisslzone, out); } - if (addr.sin_addr.s_addr == 0) + if (sockaddr_storage_isnull(addr)) { rslog(RSL_WARNING, pqisslzone, "pqissl::Initiate_Connection() Invalid (0.0.0.0) Remote Address, Aborting Connect."); waiting = WAITING_FAIL_INTERFACE; @@ -1279,7 +1281,7 @@ int pqissl::Authorise_SSL_Connection() return 0; } -int pqissl::accept(SSL *ssl, int fd, struct sockaddr_in foreign_addr) // initiate incoming connection. +int pqissl::accept(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr) // initiate incoming connection. { if (waiting != WAITING_NOT) { @@ -1373,13 +1375,14 @@ int pqissl::accept(SSL *ssl, int fd, struct sockaddr_in foreign_addr) // initiat /* check whether it is on the same LAN */ - struct sockaddr_in localaddr = mLinkMgr->getLocalAddress(); - sameLAN = isSameSubnet(&(remote_addr.sin_addr), &(localaddr.sin_addr)); + struct sockaddr_storage localaddr; + mLinkMgr->getLocalAddress(localaddr); + sameLAN = sockaddr_storage_samesubnet(remote_addr, localaddr); { std::string out = "pqissl::accept() SUCCESSFUL connection to: " + PeerId(); - out += " localaddr: " + rs_inet_ntoa(localaddr.sin_addr); - out += " remoteaddr: " + rs_inet_ntoa(remote_addr.sin_addr); + out += " localaddr: " + sockaddr_storage_iptostring(localaddr); + out += " remoteaddr: " + sockaddr_storage_iptostring(remote_addr); if (sameLAN) { diff --git a/libretroshare/src/pqi/pqissl.h b/libretroshare/src/pqi/pqissl.h index 58aa84f17..af9068a0a 100644 --- a/libretroshare/src/pqi/pqissl.h +++ b/libretroshare/src/pqi/pqissl.h @@ -93,12 +93,12 @@ public: virtual ~pqissl(); // NetInterface -virtual int connect(struct sockaddr_in raddr); +virtual int connect(const struct sockaddr_storage &raddr); virtual int listen(); virtual int stoplistening(); virtual int reset(); virtual int disconnect(); -virtual int getConnectAddress(struct sockaddr_in &raddr); +virtual int getConnectAddress(struct sockaddr_storage &raddr); virtual bool connect_parameter(uint32_t type, const std::string &value); virtual bool connect_parameter(uint32_t type, uint32_t value); @@ -152,7 +152,7 @@ public: * the listener (should make friends??) */ -int accept(SSL *ssl, int fd, struct sockaddr_in foreign_addr); +int accept(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr); void getCryptoParams(RsPeerCryptoParams& params) ; @@ -175,7 +175,7 @@ virtual int net_internal_fcntl_nonblock(int fd) { return unix_fcntl_nonblock(fd) int sockfd; pqissllistener *pqil; - struct sockaddr_in remote_addr; + struct sockaddr_storage remote_addr; void *readpkt; int pktlen; @@ -199,7 +199,7 @@ virtual int net_internal_fcntl_nonblock(int fd) { return unix_fcntl_nonblock(fd) private: // ssl only fns. -int connectInterface(sockaddr_in&); +int connectInterface(const struct sockaddr_storage &addr); }; diff --git a/libretroshare/src/pqi/pqissllistener.cc b/libretroshare/src/pqi/pqissllistener.cc index 9d0a4288a..49db80942 100644 --- a/libretroshare/src/pqi/pqissllistener.cc +++ b/libretroshare/src/pqi/pqissllistener.cc @@ -55,7 +55,7 @@ const int pqissllistenzone = 49787; */ -pqissllistenbase::pqissllistenbase(struct sockaddr_in addr, p3PeerMgr *pm) +pqissllistenbase::pqissllistenbase(const struct sockaddr_storage &addr, p3PeerMgr *pm) :laddr(addr), active(false), mPeerMgr(pm) { @@ -87,7 +87,7 @@ int pqissllistenbase::tick() int pqissllistenbase::status() { std::string out; - rs_sprintf(out, "pqissllistenbase::status(): Listening on port: %u", ntohs(laddr.sin_port)); + rs_sprintf(out, "pqissllistenbase::status(): Listening on port: %u", sockaddr_storage_port(laddr)); pqioutput(PQL_DEBUG_ALL, pqissllistenzone, out); return 1; } @@ -146,16 +146,11 @@ int pqissllistenbase::setuplisten() // setup listening address. - // fill in fconstant bits. - - laddr.sin_family = AF_INET; - { std::string out = "pqissllistenbase::setuplisten()\n"; - rs_sprintf_append(out, "\tAddress Family: %d\n", (int) laddr.sin_family); - rs_sprintf_append(out, "\tSetup Address: %s\n", rs_inet_ntoa(laddr.sin_addr).c_str()); - rs_sprintf_append(out, "\tSetup Port: %u", ntohs(laddr.sin_port)); - + out += "\t FAMILY: " + sockaddr_storage_familytostring(laddr); + out += "\t ADDRESS: " + sockaddr_storage_tostring(laddr); + pqioutput(PQL_DEBUG_BASIC, pqissllistenzone, out); //std::cerr << out.str() << std::endl; } @@ -185,8 +180,8 @@ int pqissllistenbase::setuplisten() } #ifdef OPEN_UNIVERSAL_PORT - struct sockaddr_in tmpaddr = laddr; - tmpaddr.sin_addr.s_addr = 0; + struct sockaddr_storage tmpaddr = laddr; + sockaddr_storage_zeroip(tmpaddr); if (0 != (err = bind(lsock, (struct sockaddr *) &tmpaddr, sizeof(tmpaddr)))) #else if (0 != (err = bind(lsock, (struct sockaddr *) &laddr, sizeof(laddr)))) @@ -268,7 +263,7 @@ int pqissllistenbase::setuplisten() return 1; } -int pqissllistenbase::setListenAddr(struct sockaddr_in addr) +int pqissllistenbase::setListenAddr(const struct sockaddr_storage &addr) { laddr = addr; return 1; @@ -305,7 +300,7 @@ int pqissllistenbase::acceptconnection() // These are local but temp variables... // can't be arsed making them all the time. - struct sockaddr_in remote_addr; + struct sockaddr_storage remote_addr; socklen_t addrlen = sizeof(remote_addr); int fd = accept(lsock, (struct sockaddr *) &remote_addr, &addrlen); int err = 0; @@ -356,7 +351,8 @@ int pqissllistenbase::acceptconnection() { std::string out; - rs_sprintf(out, "Accepted Connection from %s:%u", rs_inet_ntoa(remote_addr.sin_addr).c_str(), ntohs(remote_addr.sin_port)); + out += "Accepted Connection from "; + out += sockaddr_storage_tostring(remote_addr); pqioutput(PQL_DEBUG_BASIC, pqissllistenzone, out); } @@ -507,12 +503,14 @@ int pqissllistenbase::Extract_Failed_SSL_Certificate(const IncomingSSLInfo& inf std::cerr << " GPG id = " << info.gpgid << std::endl; std::cerr << " SSL id = " << info.sslid << std::endl; std::cerr << " SSL cn = " << info.sslcn << std::endl; - std::cerr << " addr+p = " << rs_inet_ntoa(info.addr.sin_addr) << ":" << ntohs(info.addr.sin_port) << std::endl; + std::cerr << " addr+p = " << sockaddr_storage_tostring(info.addr) << std::endl; if (peercert == NULL) { std::string out; - rs_sprintf(out, "pqissllistenbase::Extract_Failed_SSL_Certificate() from: %s:%u ERROR Peer didn't give Cert!", rs_inet_ntoa(info.addr.sin_addr).c_str(), ntohs(info.addr.sin_port)); + out += "pqissllistenbase::Extract_Failed_SSL_Certificate() from: "; + out += sockaddr_storage_tostring(info.addr); + out += " ERROR Peer didn't give Cert!"; std::cerr << out << std::endl; AuthSSL::getAuthSSL()->FailedCertificate(peercert, info.gpgid,info.sslid,info.sslcn,info.addr, true); @@ -525,7 +523,9 @@ int pqissllistenbase::Extract_Failed_SSL_Certificate(const IncomingSSLInfo& inf { std::string out; - rs_sprintf(out, "pqissllistenbase::Extract_Failed_SSL_Certificate() from: %s:%u Passing Cert to AuthSSL() for analysis", rs_inet_ntoa(info.addr.sin_addr).c_str(), ntohs(info.addr.sin_port)); + out += "pqissllistenbase::Extract_Failed_SSL_Certificate() from: "; + out += sockaddr_storage_tostring(info.addr); + out += " Passing Cert to AuthSSL() for analysis"; std::cerr << out << std::endl; pqioutput(PQL_WARNING, pqissllistenzone, out); @@ -675,7 +675,7 @@ int pqissllistenbase::isSSLActive(int /*fd*/, SSL *ssl) * */ -pqissllistener::pqissllistener(struct sockaddr_in addr, p3PeerMgr *lm) +pqissllistener::pqissllistener(const struct sockaddr_storage &addr, p3PeerMgr *lm) :pqissllistenbase(addr, lm) { return; @@ -743,8 +743,9 @@ int pqissllistener::status() // print certificates we are listening for. std::map::iterator it; - std::string out = "pqissllistener::status(): "; - rs_sprintf(out, " Listening (%u) for Certs:", ntohs(laddr.sin_port)); + std::string out = "pqissllistener::status(): Listening ("; + out += sockaddr_storage_tostring(laddr); + out += ") for Certs:"; for(it = listenaddr.begin(); it != listenaddr.end(); it++) { out += "\n" + it -> first ; @@ -820,7 +821,9 @@ int pqissllistener::completeConnection(int fd, IncomingSSLInfo& info) if (found == false) { - std::string out = "No Matching Certificate for Connection:" + rs_inet_ntoa(info.addr.sin_addr) +"\npqissllistenbase: Will shut it down!"; + std::string out = "No Matching Certificate for Connection:"; + out += sockaddr_storage_tostring(info.addr); + out += "\npqissllistenbase: Will shut it down!"; pqioutput(PQL_WARNING, pqissllistenzone, out); // but as it passed the authentication step, @@ -850,13 +853,15 @@ int pqissllistener::completeConnection(int fd, IncomingSSLInfo& info) accepted_ssl.push_back(as); std::string out = "pqissllistener::completeConnection() Successful Connection with: " + newPeerId; - out += " for Connection:" + rs_inet_ntoa(info.addr.sin_addr) + " Adding to WAIT-ACCEPT Queue"; + out += " for Connection:"; + out += sockaddr_storage_tostring(info.addr); + out += " Adding to WAIT-ACCEPT Queue"; pqioutput(PQL_WARNING, pqissllistenzone, out); return 1; } -int pqissllistener::finaliseConnection(int fd, SSL *ssl, std::string peerId, struct sockaddr_in &remote_addr) +int pqissllistener::finaliseConnection(int fd, SSL *ssl, std::string peerId, const struct sockaddr_storage &remote_addr) { std::map::iterator it; @@ -867,13 +872,15 @@ int pqissllistener::finaliseConnection(int fd, SSL *ssl, std::string peerId, str it = listenaddr.find(peerId); if (it == listenaddr.end()) { - out += "No Matching Peer for Connection:" + rs_inet_ntoa(remote_addr.sin_addr); + out += "No Matching Peer for Connection:"; + out += sockaddr_storage_tostring(remote_addr); out += "\npqissllistener => Shutting Down!"; pqioutput(PQL_WARNING, pqissllistenzone, out); return -1; } - out += "Found Matching Peer for Connection:" + rs_inet_ntoa(remote_addr.sin_addr); + out += "Found Matching Peer for Connection:"; + out += sockaddr_storage_tostring(remote_addr); out += "\npqissllistener => Passing to pqissl module!"; pqioutput(PQL_WARNING, pqissllistenzone, out); diff --git a/libretroshare/src/pqi/pqissllistener.h b/libretroshare/src/pqi/pqissllistener.h index 85f6df5af..74260e6e2 100644 --- a/libretroshare/src/pqi/pqissllistener.h +++ b/libretroshare/src/pqi/pqissllistener.h @@ -55,7 +55,7 @@ class AcceptedSSL SSL *mSSL; std::string mPeerId; - struct sockaddr_in mAddr; + struct sockaddr_storage mAddr; time_t mAcceptTS; }; @@ -67,7 +67,7 @@ class pqissllistenbase: public pqilistener public: - pqissllistenbase(struct sockaddr_in addr, p3PeerMgr *pm); + pqissllistenbase(const struct sockaddr_storage &addr, p3PeerMgr *pm); virtual ~pqissllistenbase(); /*************************************/ @@ -75,7 +75,7 @@ virtual ~pqissllistenbase(); virtual int tick(); virtual int status(); -virtual int setListenAddr(struct sockaddr_in addr); +virtual int setListenAddr(const struct sockaddr_storage &addr); virtual int setuplisten(); virtual int resetlisten(); @@ -88,7 +88,7 @@ int finaliseAccepts(); struct IncomingSSLInfo { SSL *ssl ; - sockaddr_in addr ; + sockaddr_storage addr ; std::string gpgid ; std::string sslid ; std::string sslcn ; @@ -100,10 +100,10 @@ int closeConnection(int fd, SSL *ssl); int isSSLActive(int fd, SSL *ssl); virtual int completeConnection(int sockfd, IncomingSSLInfo&) = 0; -virtual int finaliseConnection(int fd, SSL *ssl, std::string peerId, struct sockaddr_in &raddr) = 0; +virtual int finaliseConnection(int fd, SSL *ssl, std::string peerId, const struct sockaddr_storage &raddr) = 0; protected: - struct sockaddr_in laddr; + struct sockaddr_storage laddr; std::list accepted_ssl; private: @@ -126,7 +126,7 @@ class pqissllistener: public pqissllistenbase { public: - pqissllistener(struct sockaddr_in addr, p3PeerMgr *pm); + pqissllistener(const struct sockaddr_storage &addr, p3PeerMgr *pm); virtual ~pqissllistener(); int addlistenaddr(std::string id, pqissl *acc); @@ -136,7 +136,7 @@ int removeListenPort(std::string id); virtual int status(); virtual int completeConnection(int sockfd, IncomingSSLInfo&); -virtual int finaliseConnection(int fd, SSL *ssl, std::string peerId, struct sockaddr_in &raddr); +virtual int finaliseConnection(int fd, SSL *ssl, std::string peerId, const struct sockaddr_storage &raddr); private: diff --git a/libretroshare/src/pqi/pqisslpersongrp.cc b/libretroshare/src/pqi/pqisslpersongrp.cc index 4b771b7b2..5930f5dde 100644 --- a/libretroshare/src/pqi/pqisslpersongrp.cc +++ b/libretroshare/src/pqi/pqisslpersongrp.cc @@ -48,7 +48,7 @@ const int pqipersongrpzone = 354; #include "pqi/pqisslproxy.h" -pqilistener * pqisslpersongrp::locked_createListener(struct sockaddr_in laddr) +pqilistener * pqisslpersongrp::locked_createListener(const struct sockaddr_storage &laddr) { pqilistener *listener = new pqissllistener(laddr, mPeerMgr); return listener; diff --git a/libretroshare/src/pqi/pqisslpersongrp.h b/libretroshare/src/pqi/pqisslpersongrp.h index 02c197eef..eafd2fb73 100644 --- a/libretroshare/src/pqi/pqisslpersongrp.h +++ b/libretroshare/src/pqi/pqisslpersongrp.h @@ -45,7 +45,7 @@ class pqisslpersongrp: public pqipersongrp protected: /********* FUNCTIONS to OVERLOAD for specialisation ********/ -virtual pqilistener *locked_createListener(struct sockaddr_in laddr); +virtual pqilistener *locked_createListener(const struct sockaddr_storage &laddr); virtual pqiperson *locked_createPerson(std::string id, pqilistener *listener); /********* FUNCTIONS to OVERLOAD for specialisation ********/ diff --git a/libretroshare/src/pqi/pqisslproxy.cc b/libretroshare/src/pqi/pqisslproxy.cc index 5cfd391cd..a450d3cb1 100755 --- a/libretroshare/src/pqi/pqisslproxy.cc +++ b/libretroshare/src/pqi/pqisslproxy.cc @@ -51,7 +51,7 @@ const int pqisslproxyzone = 3517; pqisslproxy::pqisslproxy(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) :pqissl(l, parent, lm) { - sockaddr_clear(&remote_addr); + sockaddr_storage_clear(remote_addr); return; } diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index a7b6ab354..bf2d8747f 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -54,7 +54,7 @@ pqissludp::pqissludp(PQInterface *parent, p3LinkMgr *lm) :pqissl(NULL, parent, lm), tou_bio(NULL), listen_checktime(0), mConnectPeriod(PQI_SSLUDP_DEF_CONN_PERIOD) { - sockaddr_clear(&remote_addr); + sockaddr_storage_clear(remote_addr); return; } @@ -150,7 +150,7 @@ int pqissludp::Initiate_Connection() int err; attach(); /* open socket */ - remote_addr.sin_family = AF_INET; + //remote_addr.sin_family = AF_INET; rslog(RSL_DEBUG_BASIC, pqissludpzone, "pqissludp::Initiate_Connection() Attempting Outgoing Connection...."); @@ -196,7 +196,10 @@ int pqissludp::Initiate_Connection() { std::string out = "pqissludp::Initiate_Connection() Connecting To: " + PeerId(); - rs_sprintf(out, " via: %s:%u", rs_inet_ntoa(remote_addr.sin_addr).c_str(), ntohs(remote_addr.sin_port)); + out += " via: "; + out += sockaddr_storage_tostring(remote_addr); + out += " "; + if (sslmode) { out += "ACTIVE Connect (SSL_Connect)"; @@ -208,7 +211,7 @@ int pqissludp::Initiate_Connection() rslog(RSL_WARNING, pqissludpzone, out); } - if (remote_addr.sin_addr.s_addr == 0) + if (sockaddr_storage_isnull(remote_addr)) { rslog(RSL_WARNING, pqissludpzone, "pqissludp::Initiate_Connection() Invalid (0.0.0.0) Remote Address, Aborting Connect."); waiting = WAITING_FAIL_INTERFACE; @@ -234,11 +237,48 @@ int pqissludp::Initiate_Connection() else if (mConnectFlags & RS_CB_FLAG_MODE_UDP_RELAY) { std::cerr << "Calling tou_connect_via_relay("; - std::cerr << mConnectSrcAddr << ","; - std::cerr << mConnectProxyAddr << ","; - std::cerr << remote_addr << ")" << std::endl; + std::cerr << sockaddr_storage_tostring(mConnectSrcAddr) << ","; + std::cerr << sockaddr_storage_tostring(mConnectProxyAddr) << ","; + std::cerr << sockaddr_storage_tostring(remote_addr) << ")" << std::endl; - tou_connect_via_relay(sockfd, &(mConnectSrcAddr), &(mConnectProxyAddr), &(remote_addr)); + + { + std::cerr << "CONVERTING ALL ADDRESSES TO IPV4: TODO make IPV6"; + std::cerr << std::endl; + + struct sockaddr_in srcaddr; + struct sockaddr_in proxyaddr; + struct sockaddr_in remoteaddr; + + if ((mConnectSrcAddr.ss_family != AF_INET) || + (mConnectProxyAddr.ss_family != AF_INET) || + (remote_addr.ss_family != AF_INET)) + { + std::cerr << "Error One Address is not IPv4. aborting"; + std::cerr << std::endl; + abort(); + } + + struct sockaddr_in *rap = (struct sockaddr_in *) &remote_addr; + struct sockaddr_in *pap = (struct sockaddr_in *) &mConnectProxyAddr; + struct sockaddr_in *sap = (struct sockaddr_in *) &mConnectSrcAddr; + + srcaddr.sin_family = AF_INET; + proxyaddr.sin_family = AF_INET; + remoteaddr.sin_family = AF_INET; + + srcaddr.sin_addr = sap->sin_addr; + proxyaddr.sin_addr = pap->sin_addr; + remoteaddr.sin_addr = rap->sin_addr; + + srcaddr.sin_port = sap->sin_port; + proxyaddr.sin_port = pap->sin_port; + remoteaddr.sin_port = rap->sin_port; + + tou_connect_via_relay(sockfd, &srcaddr, &proxyaddr, &remoteaddr); + + } + /*** It seems that the UDP Layer sees x 1.2 the traffic of the SSL layer. * We need to compensate somewhere... we drop the maximum traffic to 75% of limit @@ -472,15 +512,16 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) return pqissl::connect_parameter(type, value); } -bool pqissludp::connect_additional_address(uint32_t type, struct sockaddr_in *addr) +bool pqissludp::connect_additional_address(uint32_t type, const struct sockaddr_storage &addr) { if (type == NET_PARAM_CONNECT_PROXY) { std::string out; - rs_sprintf(out, "pqissludp::connect_additional_address() Peer: %s PROXYADDR: %s:%u", PeerId().c_str(), rs_inet_ntoa(addr->sin_addr).c_str(), ntohs(addr->sin_port)); + rs_sprintf(out, "pqissludp::connect_additional_address() Peer: %s PROXYADDR: ", PeerId().c_str()); + out += sockaddr_storage_tostring(addr); rslog(RSL_WARNING, pqissludpzone, out); - mConnectProxyAddr = *addr; + mConnectProxyAddr = addr; std::cerr << out << std::endl; return true; @@ -488,10 +529,11 @@ bool pqissludp::connect_additional_address(uint32_t type, struct sockaddr_in *ad else if (type == NET_PARAM_CONNECT_SOURCE) { std::string out; - rs_sprintf(out, "pqissludp::connect_additional_address() Peer: %s SRCADDR: %s:%u", PeerId().c_str(), rs_inet_ntoa(addr->sin_addr).c_str(), ntohs(addr->sin_port)); + rs_sprintf(out, "pqissludp::connect_additional_address() Peer: %s SRCADDR: ", PeerId().c_str()); + out += sockaddr_storage_tostring(addr); rslog(RSL_WARNING, pqissludpzone, out); - mConnectSrcAddr = *addr; + mConnectSrcAddr = addr; std::cerr << out << std::endl; return true; diff --git a/libretroshare/src/pqi/pqissludp.h b/libretroshare/src/pqi/pqissludp.h index f07632dd4..10498840a 100644 --- a/libretroshare/src/pqi/pqissludp.h +++ b/libretroshare/src/pqi/pqissludp.h @@ -67,7 +67,7 @@ virtual int tick(); virtual int reset(); virtual bool connect_parameter(uint32_t type, uint32_t value); -virtual bool connect_additional_address(uint32_t type, struct sockaddr_in *addr); +virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr); // BinInterface. // These are reimplemented. @@ -103,8 +103,8 @@ private: uint32_t mConnectFlags; uint32_t mConnectBandwidth; - struct sockaddr_in mConnectProxyAddr; - struct sockaddr_in mConnectSrcAddr; + struct sockaddr_storage mConnectProxyAddr; + struct sockaddr_storage mConnectSrcAddr; }; #endif // MRK_PQI_SSL_UDP_HEADER diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index e0385fd35..8854c3f25 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -324,10 +324,10 @@ bool p3Peers::getPeerDetails(const std::string &id, RsPeerDetails &d) d.isHiddenNode = false; d.hiddenNodeAddress = ""; - d.localAddr = rs_inet_ntoa(ps.localaddr.sin_addr); - d.localPort = ntohs(ps.localaddr.sin_port); - d.extAddr = rs_inet_ntoa(ps.serveraddr.sin_addr); - d.extPort = ntohs(ps.serveraddr.sin_port); + d.localAddr = sockaddr_storage_iptostring(ps.localaddr); + d.localPort = sockaddr_storage_port(ps.localaddr); + d.extAddr = sockaddr_storage_iptostring(ps.serveraddr); + d.extPort = sockaddr_storage_port(ps.serveraddr); d.dyndns = ps.dyndns; std::list::iterator it; @@ -335,15 +335,19 @@ bool p3Peers::getPeerDetails(const std::string &id, RsPeerDetails &d) it != ps.ipAddrs.mLocal.mAddrs.end(); it++) { std::string toto; - rs_sprintf(toto, "%u %ld sec", ntohs(it->mAddr.sin_port), time(NULL) - it->mSeenTime); - d.ipAddressList.push_back("L:" + rs_inet_ntoa(it->mAddr.sin_addr) + ":" + toto); + toto += "L:"; + toto += sockaddr_storage_tostring(it->mAddr); + rs_sprintf_append(toto, " %ld sec", time(NULL) - it->mSeenTime); + d.ipAddressList.push_back(toto); } for(it = ps.ipAddrs.mExt.mAddrs.begin(); it != ps.ipAddrs.mExt.mAddrs.end(); it++) { std::string toto; - rs_sprintf(toto, "%u %ld sec", ntohs(it->mAddr.sin_port), time(NULL) - it->mSeenTime); - d.ipAddressList.push_back("E:" + rs_inet_ntoa(it->mAddr.sin_addr) + ":" + toto); + toto += "E:"; + toto += sockaddr_storage_tostring(it->mAddr); + rs_sprintf_append(toto, " %ld sec", time(NULL) - it->mSeenTime); + d.ipAddressList.push_back(toto); } } @@ -420,10 +424,10 @@ bool p3Peers::getPeerDetails(const std::string &id, RsPeerDetails &d) { if (pcs.currentConnAddrAttempt.type & RS_NET_CONN_TCP_ALL) { d.connectState = RS_PEER_CONNECTSTATE_TRYING_TCP; - rs_sprintf(d.connectStateString, "%s:%u", rs_inet_ntoa(pcs.currentConnAddrAttempt.addr.sin_addr).c_str(), ntohs(pcs.currentConnAddrAttempt.addr.sin_port)); + d.connectStateString = sockaddr_storage_tostring(pcs.currentConnAddrAttempt.addr); } else if (pcs.currentConnAddrAttempt.type & RS_NET_CONN_UDP_ALL) { d.connectState = RS_PEER_CONNECTSTATE_TRYING_UDP; - rs_sprintf(d.connectStateString, "%s:%u", rs_inet_ntoa(pcs.currentConnAddrAttempt.addr.sin_addr).c_str(), ntohs(pcs.currentConnAddrAttempt.addr.sin_port)); + d.connectStateString = sockaddr_storage_tostring(pcs.currentConnAddrAttempt.addr); } } else if (pcs.state & RS_PEER_S_CONNECTED) @@ -782,16 +786,17 @@ bool p3Peers::setLocalAddress(const std::string &id, const std::string &addr_st std::cerr << "p3Peers::setLocalAddress() " << id << std::endl; #endif - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(port); + struct sockaddr_storage addr; + struct sockaddr_in *addrv4p = (struct sockaddr_in *) &addr; + addrv4p->sin_family = AF_INET; + addrv4p->sin_port = htons(port); int ret = 1; /********************************** WINDOWS/UNIX SPECIFIC PART *******************/ #ifndef WINDOWS_SYS - if (ret && (0 != inet_aton(addr_str.c_str(), &(addr.sin_addr)))) + if (ret && (0 != inet_aton(addr_str.c_str(), &(addrv4p->sin_addr)))) #else - addr.sin_addr.s_addr = inet_addr(addr_str.c_str()); + addrv4p->sin_addr.s_addr = inet_addr(addr_str.c_str()); if (ret) #endif /********************************** WINDOWS/UNIX SPECIFIC PART *******************/ @@ -807,16 +812,18 @@ bool p3Peers::setExtAddress(const std::string &id, const std::string &addr_str, std::cerr << "p3Peers::setExtAddress() " << id << std::endl; #endif - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(port); + // NOTE THIS IS IPV4 FOR NOW. + struct sockaddr_storage addr; + struct sockaddr_in *addrv4p = (struct sockaddr_in *) &addr; + addrv4p->sin_family = AF_INET; + addrv4p->sin_port = htons(port); int ret = 1; /********************************** WINDOWS/UNIX SPECIFIC PART *******************/ #ifndef WINDOWS_SYS - if (ret && (0 != inet_aton(addr_str.c_str(), &(addr.sin_addr)))) + if (ret && (0 != inet_aton(addr_str.c_str(), &(addrv4p->sin_addr)))) #else - addr.sin_addr.s_addr = inet_addr(addr_str.c_str()); + addrv4p->sin_addr.s_addr = inet_addr(addr_str.c_str()); if (ret) #endif /********************************** WINDOWS/UNIX SPECIFIC PART *******************/ diff --git a/libretroshare/src/rsserver/p3serverconfig.cc b/libretroshare/src/rsserver/p3serverconfig.cc index 617a0d611..180483bc2 100644 --- a/libretroshare/src/rsserver/p3serverconfig.cc +++ b/libretroshare/src/rsserver/p3serverconfig.cc @@ -150,11 +150,11 @@ int p3ServerConfig::getConfigNetStatus(RsConfigNetStatus &status) peerState pstate; mPeerMgr->getOwnNetStatus(pstate); - status.localAddr = rs_inet_ntoa(pstate.localaddr.sin_addr); - status.localPort = ntohs(pstate.localaddr.sin_port); + status.localAddr = sockaddr_storage_iptostring(pstate.localaddr); + status.localPort = sockaddr_storage_port(pstate.localaddr); - status.extAddr = rs_inet_ntoa(pstate.serveraddr.sin_addr); - status.extPort = ntohs(pstate.serveraddr.sin_port); + status.extAddr = sockaddr_storage_iptostring(pstate.serveraddr); + status.extPort = sockaddr_storage_port(pstate.serveraddr); status.extDynDns = pstate.dyndns; status.firewalled = true; diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index b1e4b146d..ec772ec2f 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -2568,16 +2568,18 @@ int RsServer::StartupRetroShare() if (RsInitConfig::forceLocalAddr) { - struct sockaddr_in laddr; + struct sockaddr_storage laddr; /* clean sockaddr before setting values (MaxOSX) */ - sockaddr_clear(&laddr); + sockaddr_storage_clear(laddr); - laddr.sin_family = AF_INET; - laddr.sin_port = htons(RsInitConfig::port); + struct sockaddr_in *lap = (struct sockaddr_in *) &laddr; + + lap->sin_family = AF_INET; + lap->sin_port = htons(RsInitConfig::port); // universal - laddr.sin_addr.s_addr = inet_addr(RsInitConfig::inet.c_str()); + lap->sin_addr.s_addr = inet_addr(RsInitConfig::inet.c_str()); mPeerMgr->setLocalAddress(ownId, laddr); } diff --git a/libretroshare/src/serialiser/rsconfigitems.cc b/libretroshare/src/serialiser/rsconfigitems.cc index 774b7c433..a5b5ab03a 100644 --- a/libretroshare/src/serialiser/rsconfigitems.cc +++ b/libretroshare/src/serialiser/rsconfigitems.cc @@ -37,8 +37,6 @@ #include -// For transition. -RsPeerNetItem *convertToNetItem(RsPeerOldNetItem *old); /*************************************************************************/ @@ -683,16 +681,11 @@ RsPeerConfigSerialiser::~RsPeerConfigSerialiser() uint32_t RsPeerConfigSerialiser::size(RsItem *i) { - RsPeerOldNetItem *oldpni; RsPeerStunItem *psi; RsPeerNetItem *pni; RsPeerGroupItem *pgi; RsPeerServicePermissionItem *pri; - if (NULL != (oldpni = dynamic_cast(i))) - { - return sizeOldNet(oldpni); - } if (NULL != (pni = dynamic_cast(i))) { return sizeNet(pni); @@ -716,16 +709,11 @@ uint32_t RsPeerConfigSerialiser::size(RsItem *i) /* serialise the data to the buffer */ bool RsPeerConfigSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize) { - RsPeerOldNetItem *oldpni; RsPeerNetItem *pni; RsPeerStunItem *psi; RsPeerGroupItem *pgi; RsPeerServicePermissionItem *pri; - if (NULL != (oldpni = dynamic_cast(i))) - { - return serialiseOldNet(oldpni, data, pktsize); - } if (NULL != (pni = dynamic_cast(i))) { return serialiseNet(pni, data, pktsize); @@ -761,13 +749,8 @@ RsItem *RsPeerConfigSerialiser::deserialise(void *data, uint32_t *pktsize) return NULL; /* wrong type */ } - RsPeerOldNetItem *old = NULL; switch(getRsItemSubType(rstype)) { - case RS_PKT_SUBTYPE_PEER_OLD_NET: - old = deserialiseOldNet(data, pktsize); - /* upgrade mechanism */ - return convertToNetItem(old); case RS_PKT_SUBTYPE_PEER_NET: return deserialiseNet(data, pktsize); case RS_PKT_SUBTYPE_PEER_STUN: @@ -783,260 +766,6 @@ RsItem *RsPeerConfigSerialiser::deserialise(void *data, uint32_t *pktsize) } -/*************************************************************************/ - -RsPeerOldNetItem::~RsPeerOldNetItem() -{ - return; -} - -void RsPeerOldNetItem::clear() -{ - pid.clear(); - gpg_id.clear(); - location.clear(); - netMode = 0; - visState = 0; - lastContact = 0; - - sockaddr_clear(¤tlocaladdr); - sockaddr_clear(¤tremoteaddr); - dyndns.clear(); -} - -std::ostream &RsPeerOldNetItem::print(std::ostream &out, uint16_t indent) -{ - printRsItemBase(out, "RsPeerOldNetItem", indent); - uint16_t int_Indent = indent + 2; - - printIndent(out, int_Indent); - out << "PeerId: " << pid << std::endl; - - printIndent(out, int_Indent); - out << "GPGid: " << gpg_id << std::endl; - - printIndent(out, int_Indent); - out << "location: " << location << std::endl; - - printIndent(out, int_Indent); - out << "netMode: " << netMode << std::endl; - - printIndent(out, int_Indent); - out << "visState: " << visState << std::endl; - - printIndent(out, int_Indent); - out << "lastContact: " << lastContact << std::endl; - - printIndent(out, int_Indent); - out << "currentlocaladdr: " << rs_inet_ntoa(currentlocaladdr.sin_addr); - out << ":" << htons(currentlocaladdr.sin_port) << std::endl; - - printIndent(out, int_Indent); - out << "currentremoteaddr: " << rs_inet_ntoa(currentremoteaddr.sin_addr); - out << ":" << htons(currentremoteaddr.sin_port) << std::endl; - - printIndent(out, int_Indent); - out << "DynDNS: " << dyndns << std::endl; - - printIndent(out, int_Indent); - out << "ipAdressList: size : " << ipAddressList.size() << ", adresses : " << std::endl; - for (std::list::iterator ipListIt = ipAddressList.begin(); ipListIt!=(ipAddressList.end()); ipListIt++) { - printIndent(out, int_Indent); - out << rs_inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl; - } - - printRsItemEnd(out, "RsPeerOldNetItem", indent); - return out; -} - -/*************************************************************************/ - -uint32_t RsPeerConfigSerialiser::sizeOldNet(RsPeerOldNetItem *i) -{ - uint32_t s = 8; /* header */ - s += GetTlvStringSize(i->pid); /* peerid */ - s += GetTlvStringSize(i->gpg_id); - s += GetTlvStringSize(i->location); - s += 4; /* netMode */ - s += 4; /* visState */ - s += 4; /* lastContact */ - s += GetTlvIpAddrPortV4Size(); /* localaddr */ - s += GetTlvIpAddrPortV4Size(); /* remoteaddr */ - s += GetTlvStringSize(i->dyndns); - - //add the size of the ip list - int ipListSize = i->ipAddressList.size(); - s += ipListSize * GetTlvIpAddrPortV4Size(); - s += ipListSize * 8; //size of an uint64 - - return s; - -} - -bool RsPeerConfigSerialiser::serialiseOldNet(RsPeerOldNetItem *item, void *data, uint32_t *size) -{ - uint32_t tlvsize = RsPeerConfigSerialiser::sizeOldNet(item); - uint32_t offset = 0; - -#ifdef RSSERIAL_ERROR_DEBUG - std::cerr << "RsPeerConfigSerialiser::serialiseOldNet() ERROR should never use this function" << std::endl; -#endif - - if(*size < tlvsize) - return false; /* not enough space */ - - *size = tlvsize; - - bool ok = true; - - // serialise header - - ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize); - -#ifdef RSSERIAL_DEBUG - std::cerr << "RsPeerConfigSerialiser::serialiseOldNet() Header: " << ok << std::endl; - std::cerr << "RsPeerConfigSerialiser::serialiseOldNet() Header test: " << tlvsize << std::endl; -#endif - - /* skip the header */ - offset += 8; - - /* add mandatory parts first */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->pid); /* Mandatory */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GPGID, item->gpg_id); /* Mandatory */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LOCATION, item->location); /* Mandatory */ - ok &= setRawUInt32(data, tlvsize, &offset, item->netMode); /* Mandatory */ - ok &= setRawUInt32(data, tlvsize, &offset, item->visState); /* Mandatory */ - ok &= setRawUInt32(data, tlvsize, &offset, item->lastContact); /* Mandatory */ - ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentlocaladdr)); - ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentremoteaddr)); - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); - - //store the ip list - std::list::iterator ipListIt; - for (ipListIt = item->ipAddressList.begin(); ipListIt!=(item->ipAddressList.end()); ipListIt++) { - ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(ipListIt->ipAddr)); - ok &= setRawUInt64(data, tlvsize, &offset, ipListIt->seenTime); - } - - if(offset != tlvsize) - { - ok = false; -#ifdef RSSERIAL_ERROR_DEBUG - std::cerr << "RsPeerConfigSerialiser::serialise() Size Error! " << std::endl; -#endif - } - - return ok; - -} - -RsPeerOldNetItem *RsPeerConfigSerialiser::deserialiseOldNet(void *data, uint32_t *size) -{ - /* get the type and size */ - uint32_t rstype = getRsItemId(data); - uint32_t rssize = getRsItemSize(data); - - uint32_t offset = 0; - -#ifdef RSSERIAL_ERROR_DEBUG - std::cerr << "RsPeerConfigSerialiser::serialiseOldNet() ERROR should never use this function" << std::endl; -#endif - - if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) || - (RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || - (RS_PKT_TYPE_PEER_CONFIG != getRsItemType(rstype)) || - (RS_PKT_SUBTYPE_PEER_OLD_NET != getRsItemSubType(rstype))) - { - return NULL; /* wrong type */ - } - - if (*size < rssize) /* check size */ - return NULL; /* not enough data */ - - /* set the packet length */ - *size = rssize; - - bool ok = true; - - RsPeerOldNetItem *item = new RsPeerOldNetItem(); - item->clear(); - - /* skip the header */ - offset += 8; - - /* get mandatory parts first */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->pid); /* Mandatory */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GPGID, item->gpg_id); /* Mandatory */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LOCATION, item->location); /* Mandatory */ - ok &= getRawUInt32(data, rssize, &offset, &(item->netMode)); /* Mandatory */ - ok &= getRawUInt32(data, rssize, &offset, &(item->visState)); /* Mandatory */ - ok &= getRawUInt32(data, rssize, &offset, &(item->lastContact)); /* Mandatory */ - ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentlocaladdr)); - ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentremoteaddr)); - //ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); - GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); //use this line for backward compatibility - - //get the ip adress list - std::list ipTimedList; - while (offset < rssize) { - IpAddressTimed ipTimed; - sockaddr_clear(&ipTimed.ipAddr); - ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &ipTimed.ipAddr); - if (!ok) { break;} - uint64_t time = 0; - ok &= getRawUInt64(data, rssize, &offset, &time); - if (!ok) { break;} - ipTimed.seenTime = time; - ipTimedList.push_back(ipTimed); - } - item->ipAddressList = ipTimedList; - - //if (offset != rssize) - if (false) //use this line for backward compatibility - { - - /* error */ - delete item; - return NULL; - } - - return item; -} - -/****************************************************************************/ -RsPeerNetItem *convertToNetItem(RsPeerOldNetItem *old) -{ - RsPeerNetItem *item = new RsPeerNetItem(); - - /* copy over data */ - item->pid = old->pid; - item->gpg_id = old->gpg_id; - item->location = old->location; - item->netMode = old->netMode; - item->visState = old->visState; - item->lastContact = old->lastContact; - - item->currentlocaladdr = old->currentlocaladdr; - item->currentremoteaddr = old->currentremoteaddr; - item->dyndns = old->dyndns; - - std::list::iterator it; - for(it = old->ipAddressList.begin(); it != old->ipAddressList.end(); it++) - { - RsTlvIpAddressInfo info; - info.addr = it->ipAddr; - info.seenTime = it->seenTime; - info.source = 0; - - item->extAddrList.addrs.push_back(info); - } - - /* delete old data */ - delete old; - - return item; -} /****************************************************************************/ @@ -1054,9 +783,9 @@ void RsPeerNetItem::clear() visState = 0; lastContact = 0; - sockaddr_clear(¤tlocaladdr); - sockaddr_clear(¤tremoteaddr); - dyndns.clear(); + localAddr.TlvClear(); + extAddr.TlvClear(); + dyndns.clear(); localAddrList.TlvClear(); extAddrList.TlvClear(); @@ -1089,15 +818,16 @@ std::ostream &RsPeerNetItem::print(std::ostream &out, uint16_t indent) out << "lastContact: " << lastContact << std::endl; printIndent(out, int_Indent); - out << "currentlocaladdr: " << rs_inet_ntoa(currentlocaladdr.sin_addr); - out << ":" << htons(currentlocaladdr.sin_port) << std::endl; + out << "localAddr: " << std::endl; + localAddr.print(out, int_Indent); printIndent(out, int_Indent); - out << "currentremoteaddr: " << rs_inet_ntoa(currentremoteaddr.sin_addr); - out << ":" << htons(currentremoteaddr.sin_port) << std::endl; + out << "extAddr: " << std::endl; + extAddr.print(out, int_Indent); + - printIndent(out, int_Indent); - out << "DynDNS: " << dyndns << std::endl; + printIndent(out, int_Indent); + out << "DynDNS: " << dyndns << std::endl; localAddrList.print(out, int_Indent); extAddrList.print(out, int_Indent); @@ -1117,9 +847,9 @@ uint32_t RsPeerConfigSerialiser::sizeNet(RsPeerNetItem *i) s += 4; /* netMode */ s += 4; /* visState */ s += 4; /* lastContact */ - s += GetTlvIpAddrPortV4Size(); /* localaddr */ - s += GetTlvIpAddrPortV4Size(); /* remoteaddr */ - s += GetTlvStringSize(i->dyndns); + s += i->localAddr.TlvSize(); /* localaddr */ + s += i->extAddr.TlvSize(); /* remoteaddr */ + s += GetTlvStringSize(i->dyndns); //add the size of the ip list s += i->localAddrList.TlvSize(); @@ -1166,21 +896,22 @@ bool RsPeerConfigSerialiser::serialiseNet(RsPeerNetItem *item, void *data, uint3 offset += 8; /* add mandatory parts first */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->pid); /* Mandatory */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GPGID, item->gpg_id); /* Mandatory */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LOCATION, item->location); /* Mandatory */ - ok &= setRawUInt32(data, tlvsize, &offset, item->netMode); /* Mandatory */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->pid); /* Mandatory */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GPGID, item->gpg_id); /* Mandatory */ + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LOCATION, item->location); /* Mandatory */ + ok &= setRawUInt32(data, tlvsize, &offset, item->netMode); /* Mandatory */ ok &= setRawUInt32(data, tlvsize, &offset, item->visState); /* Mandatory */ ok &= setRawUInt32(data, tlvsize, &offset, item->lastContact); /* Mandatory */ - ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentlocaladdr)); - ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentremoteaddr)); - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); + ok &= item->localAddr.SetTlv(data, tlvsize, &offset); + ok &= item->extAddr.SetTlv(data, tlvsize, &offset); + + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); ok &= item->localAddrList.SetTlv(data, tlvsize, &offset); ok &= item->extAddrList.SetTlv(data, tlvsize, &offset); // New for V0.6. - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DOMADDR, item->domain_addr); + ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DOMADDR, item->domain_addr); ok &= setRawUInt16(data, tlvsize, &offset, item->domain_port); /* Mandatory */ if(offset != tlvsize) @@ -1239,15 +970,17 @@ RsPeerNetItem *RsPeerConfigSerialiser::deserialiseNet(void *data, uint32_t *size offset += 8; /* get mandatory parts first */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->pid); /* Mandatory */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GPGID, item->gpg_id); /* Mandatory */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LOCATION, item->location); /* Mandatory */ - ok &= getRawUInt32(data, rssize, &offset, &(item->netMode)); /* Mandatory */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->pid); /* Mandatory */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GPGID, item->gpg_id); /* Mandatory */ + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LOCATION, item->location); /* Mandatory */ + ok &= getRawUInt32(data, rssize, &offset, &(item->netMode)); /* Mandatory */ ok &= getRawUInt32(data, rssize, &offset, &(item->visState)); /* Mandatory */ ok &= getRawUInt32(data, rssize, &offset, &(item->lastContact)); /* Mandatory */ - ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentlocaladdr)); - ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentremoteaddr)); - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); + + ok &= item->localAddr.GetTlv(data, rssize, &offset); + ok &= item->extAddr.GetTlv(data, rssize, &offset); + + ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DYNDNS, item->dyndns); ok &= item->localAddrList.GetTlv(data, rssize, &offset); ok &= item->extAddrList.GetTlv(data, rssize, &offset); diff --git a/libretroshare/src/serialiser/rsconfigitems.h b/libretroshare/src/serialiser/rsconfigitems.h index b979cf8fc..74ce1e9a8 100644 --- a/libretroshare/src/serialiser/rsconfigitems.h +++ b/libretroshare/src/serialiser/rsconfigitems.h @@ -48,9 +48,8 @@ const uint8_t RS_PKT_TYPE_HISTORY_CONFIG = 0x06; const uint8_t RS_PKT_SUBTYPE_KEY_VALUE = 0x01; /* PEER CONFIG SUBTYPES */ -const uint8_t RS_PKT_SUBTYPE_PEER_OLD_NET = 0x01; const uint8_t RS_PKT_SUBTYPE_PEER_STUN = 0x02; -const uint8_t RS_PKT_SUBTYPE_PEER_NET = 0x03; /* replacement for OLD_NET */ +const uint8_t RS_PKT_SUBTYPE_PEER_NET = 0x03; const uint8_t RS_PKT_SUBTYPE_PEER_GROUP = 0x04; const uint8_t RS_PKT_SUBTYPE_PEER_PERMISSIONS = 0x05; @@ -60,38 +59,6 @@ const uint8_t RS_PKT_SUBTYPE_FILE_ITEM = 0x02; /**************************************************************************/ -struct IpAddressTimed { - struct sockaddr_in ipAddr; - time_t seenTime; -}; - -class RsPeerOldNetItem: public RsItem -{ - public: - RsPeerOldNetItem() - :RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, - RS_PKT_TYPE_PEER_CONFIG, - RS_PKT_SUBTYPE_PEER_OLD_NET) - { return; } -virtual ~RsPeerOldNetItem(); -virtual void clear(); -std::ostream &print(std::ostream &out, uint16_t indent = 0); - - /* networking information */ - std::string pid; /* Mandatory */ - std::string gpg_id; /* Mandatory */ - std::string location; /* not Mandatory */ - uint32_t netMode; /* Mandatory */ - uint32_t visState; /* Mandatory */ - uint32_t lastContact; /* Mandatory */ - - struct sockaddr_in currentlocaladdr; /* Mandatory */ - struct sockaddr_in currentremoteaddr; /* Mandatory */ - std::string dyndns; - - std::list ipAddressList; -}; - class RsPeerNetItem: public RsItem { public: @@ -112,8 +79,8 @@ std::ostream &print(std::ostream &out, uint16_t indent = 0); uint32_t visState; /* Mandatory */ uint32_t lastContact; /* Mandatory */ - struct sockaddr_in currentlocaladdr; /* Mandatory */ - struct sockaddr_in currentremoteaddr; /* Mandatory */ + RsTlvIpAddress localAddr; /* Mandatory */ + RsTlvIpAddress extAddr; /* Mandatory */ std::string dyndns; RsTlvIpAddrSet localAddrList; @@ -195,11 +162,6 @@ virtual RsItem * deserialise(void *data, uint32_t *size); private: -/* These are depreciated ... conversion functions used to seemlessly ungrade. - */ -virtual uint32_t sizeOldNet(RsPeerOldNetItem *); -virtual bool serialiseOldNet (RsPeerOldNetItem *item, void *data, uint32_t *size); -virtual RsPeerOldNetItem *deserialiseOldNet(void *data, uint32_t *size); virtual uint32_t sizeNet(RsPeerNetItem *); virtual bool serialiseNet (RsPeerNetItem *item, void *data, uint32_t *size); diff --git a/libretroshare/src/serialiser/rstlvaddrs.cc b/libretroshare/src/serialiser/rstlvaddrs.cc index 6be3b5ec0..12c48f3d5 100644 --- a/libretroshare/src/serialiser/rstlvaddrs.cc +++ b/libretroshare/src/serialiser/rstlvaddrs.cc @@ -35,18 +35,161 @@ #include +/************************************* RsTlvIpAddress ************************************/ + +RsTlvIpAddress::RsTlvIpAddress() + :RsTlvItem() +{ + sockaddr_storage_clear(addr); + return; +} + +void RsTlvIpAddress::TlvClear() +{ + sockaddr_storage_clear(addr); +} + +uint32_t RsTlvIpAddress::TlvSize() +{ + uint32_t s = TLV_HEADER_SIZE; + switch(addr.ss_family) + { + default: + case 0: + break; + case AF_INET: + s += GetTlvIpAddrPortV4Size(); + break; + case AF_INET6: + s += GetTlvIpAddrPortV6Size(); + break; + } + return s; +} + +bool RsTlvIpAddress::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + /* must check sizes */ + uint32_t tlvsize = TlvSize(); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) + return false; /* not enough space */ + + bool ok = true; + + /* start at data[offset] */ + /* add mandatory parts first */ + + ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_ADDRESS, tlvsize); + + switch(addr.ss_family) + { + default: + case 0: + break; + case AF_INET: + ok &= SetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4, (struct sockaddr_in *) &addr); + break; + + case AF_INET6: + ok &= SetTlvIpAddrPortV6(data, tlvend, offset, TLV_TYPE_IPV6, (struct sockaddr_in6 *) &addr); + break; + } + return ok; + +} + + +bool RsTlvIpAddress::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */ +{ + if (size < *offset + TLV_HEADER_SIZE) + return false; + + uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) ); + uint32_t tlvend = *offset + tlvsize; + + if (size < tlvend) /* check size */ + return false; /* not enough space */ + + if (tlvtype != TLV_TYPE_ADDRESS) /* check type */ + return false; + + bool ok = true; + + /* ready to load */ + TlvClear(); + + /* skip the header */ + (*offset) += TLV_HEADER_SIZE; + + if (*offset == tlvend) + { + /* empty address */ + return ok; + } + + uint16_t iptype = GetTlvType( &(((uint8_t *) data)[*offset]) ); + switch(iptype) + { + case TLV_TYPE_IPV4: + ok &= GetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4, (struct sockaddr_in *) &addr); + break; + case TLV_TYPE_IPV6: + ok &= GetTlvIpAddrPortV6(data, tlvend, offset, TLV_TYPE_IPV6, (struct sockaddr_in6 *) &addr); + break; + default: + break; + } + + /*************************************************************************** + * NB: extra components could be added (for future expansion of the type). + * or be present (if this code is reading an extended version). + * + * We must chew up the extra characters to conform with TLV specifications + ***************************************************************************/ + if (*offset != tlvend) + { +#ifdef TLV_DEBUG + std::cerr << "RsTlvIpAddress::GetTlv() Warning extra bytes at end of item"; + std::cerr << std::endl; +#endif + *offset = tlvend; + } + + return ok; + +} + + +std::ostream &RsTlvIpAddress::print(std::ostream &out, uint16_t indent) +{ + printBase(out, "RsTlvIpAddress", indent); + uint16_t int_Indent = indent + 2; + + printIndent(out, int_Indent); + out << "Address:" << sockaddr_storage_tostring(addr) << std::endl; + + printEnd(out, "RsTlvIpAddress", indent); + return out; +} + + + + /************************************* RsTlvIpAddressInfo ************************************/ RsTlvIpAddressInfo::RsTlvIpAddressInfo() :RsTlvItem(), seenTime(0), source(0) { - sockaddr_clear(&addr); + addr.TlvClear(); return; } void RsTlvIpAddressInfo::TlvClear() { - sockaddr_clear(&addr); + addr.TlvClear(); seenTime = 0; source = 0; } @@ -55,7 +198,7 @@ uint32_t RsTlvIpAddressInfo::TlvSize() { uint32_t s = TLV_HEADER_SIZE; /* header + IpAddr + 8 for time & 4 for size */ - s += GetTlvIpAddrPortV4Size(); + s += addr.TlvSize(); s += 8; // seenTime s += 4; // source @@ -79,7 +222,7 @@ bool RsTlvIpAddressInfo::SetTlv(void *data, uint32_t size, uint32_t *offset) /* ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_ADDRESS_INFO, tlvsize); - ok &= SetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4_LAST, &addr); + ok &= addr.SetTlv(data, tlvend, offset); ok &= setRawUInt64(data, tlvend, offset, seenTime); ok &= setRawUInt32(data, tlvend, offset, source); @@ -111,7 +254,7 @@ bool RsTlvIpAddressInfo::GetTlv(void *data, uint32_t size, uint32_t *offset) /* /* skip the header */ (*offset) += TLV_HEADER_SIZE; - ok &= GetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4_LAST, &addr); + ok &= addr.GetTlv(data, tlvend, offset); ok &= getRawUInt64(data, tlvend, offset, &(seenTime)); ok &= getRawUInt32(data, tlvend, offset, &(source)); @@ -141,9 +284,7 @@ std::ostream &RsTlvIpAddressInfo::print(std::ostream &out, uint16_t indent) printBase(out, "RsTlvIpAddressInfo", indent); uint16_t int_Indent = indent + 2; - printIndent(out, int_Indent); - out << "Address:" << rs_inet_ntoa(addr.sin_addr); - out << ":" << htons(addr.sin_port) << std::endl; + addr.print(out, int_Indent); printIndent(out, int_Indent); out << "SeenTime:" << seenTime; diff --git a/libretroshare/src/serialiser/rstlvaddrs.h b/libretroshare/src/serialiser/rstlvaddrs.h index 3de93ad66..e4234108d 100644 --- a/libretroshare/src/serialiser/rstlvaddrs.h +++ b/libretroshare/src/serialiser/rstlvaddrs.h @@ -34,6 +34,20 @@ #include "serialiser/rstlvtypes.h" #include "util/rsnet.h" +class RsTlvIpAddress: public RsTlvItem +{ + public: + RsTlvIpAddress(); +virtual ~RsTlvIpAddress() { return; } +virtual uint32_t TlvSize(); +virtual void TlvClear(); +virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */ +virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ +virtual std::ostream &print(std::ostream &out, uint16_t indent); + + struct sockaddr_storage addr; // Mandatory : +}; + class RsTlvIpAddressInfo: public RsTlvItem { @@ -46,7 +60,7 @@ virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* seriali virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ virtual std::ostream &print(std::ostream &out, uint16_t indent); - struct sockaddr_in addr; // Mandatory : + RsTlvIpAddress addr; // Mandatory : uint64_t seenTime; // Mandatory : uint32_t source; // Mandatory : }; diff --git a/libretroshare/src/serialiser/rstlvbanlist.cc b/libretroshare/src/serialiser/rstlvbanlist.cc index 3e3446ad0..c69db6b79 100644 --- a/libretroshare/src/serialiser/rstlvbanlist.cc +++ b/libretroshare/src/serialiser/rstlvbanlist.cc @@ -45,7 +45,7 @@ RsTlvBanListEntry::RsTlvBanListEntry() void RsTlvBanListEntry::TlvClear() { - sockaddr_clear(&addr); + addr.TlvClear(); level = 0; reason = 0; age = 0; @@ -55,7 +55,7 @@ uint32_t RsTlvBanListEntry::TlvSize() { uint32_t s = TLV_HEADER_SIZE; - s += GetTlvIpAddrPortV4Size(); + s += addr.TlvSize(); s += 4; // level; s += 4; // reason; s += 4; // age; @@ -80,7 +80,7 @@ bool RsTlvBanListEntry::SetTlv(void *data, uint32_t size, uint32_t *offset) /* ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_BAN_ENTRY, tlvsize); - ok &= SetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4_REMOTE, &addr); + ok &= addr.SetTlv(data, tlvend, offset); ok &= setRawUInt32(data, tlvend, offset, level); ok &= setRawUInt32(data, tlvend, offset, reason); ok &= setRawUInt32(data, tlvend, offset, age); @@ -112,7 +112,7 @@ bool RsTlvBanListEntry::GetTlv(void *data, uint32_t size, uint32_t *offset) /* /* skip the header */ (*offset) += TLV_HEADER_SIZE; - ok &= GetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4_REMOTE, &addr); + ok &= addr.GetTlv(data, tlvend, offset); ok &= getRawUInt32(data, tlvend, offset, &(level)); ok &= getRawUInt32(data, tlvend, offset, &(reason)); ok &= getRawUInt32(data, tlvend, offset, &(age)); @@ -143,8 +143,8 @@ std::ostream &RsTlvBanListEntry::print(std::ostream &out, uint16_t indent) uint16_t int_Indent = indent + 2; printIndent(out, int_Indent); - out << "addr:" << rs_inet_ntoa(addr.sin_addr) << ":" << htons(addr.sin_port); - out << std::endl; + out << "addr:" << std::endl; + addr.print(out, int_Indent); printIndent(out, int_Indent); out << "level:" << level; diff --git a/libretroshare/src/serialiser/rstlvbanlist.h b/libretroshare/src/serialiser/rstlvbanlist.h index 51695f919..40a31e9cb 100644 --- a/libretroshare/src/serialiser/rstlvbanlist.h +++ b/libretroshare/src/serialiser/rstlvbanlist.h @@ -32,6 +32,7 @@ #include #include "serialiser/rstlvtypes.h" +#include "serialiser/rstlvaddrs.h" #include "util/rsnet.h" #define RSDSDV_MAX_ROUTE_TABLE 1000 @@ -47,7 +48,7 @@ virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* seriali virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ virtual std::ostream &print(std::ostream &out, uint16_t indent); - struct sockaddr_in addr; + RsTlvIpAddress addr; uint32_t level; uint32_t reason; uint32_t age; diff --git a/libretroshare/src/serialiser/rstlvbase.cc b/libretroshare/src/serialiser/rstlvbase.cc index 801a2e5ed..f0547c3f8 100644 --- a/libretroshare/src/serialiser/rstlvbase.cc +++ b/libretroshare/src/serialiser/rstlvbase.cc @@ -697,7 +697,7 @@ bool SetTlvIpAddrPortV6(void *data, uint32_t size, uint32_t *offset, { ok &= setRawUInt32(data, tlvend, offset, ip6addr[i]); } - ok &= setRawUInt16(data, tlvend, offset, out->sin_port6); + ok &= setRawUInt16(data, tlvend, offset, out->sin6_port); return ok; } @@ -752,7 +752,7 @@ bool GetTlvIpAddrPortV6(void *data, uint32_t size, uint32_t *offset, uint32_t *ip6addr = (uint32_t *) in->sin6_addr.s6_addr; for(int i = 0; i < 4; i++) { - ok &= getRawUInt32(data, tlvend, offset, ip6addr[i]); + ok &= getRawUInt32(data, tlvend, offset, &(ip6addr[i])); } in->sin6_family = AF_INET6; /* set FAMILY */ diff --git a/libretroshare/src/serialiser/rstlvbase.h b/libretroshare/src/serialiser/rstlvbase.h index efe7c3aac..a7697c6cd 100644 --- a/libretroshare/src/serialiser/rstlvbase.h +++ b/libretroshare/src/serialiser/rstlvbase.h @@ -159,6 +159,10 @@ const uint16_t TLV_TYPE_IPV4_LAST = 0x0082; const uint16_t TLV_TYPE_STR_DYNDNS = 0x0083; const uint16_t TLV_TYPE_STR_DOMADDR = 0x0084; +// rearrange these in the future. +const uint16_t TLV_TYPE_IPV4 = 0x0085; +const uint16_t TLV_TYPE_IPV6 = 0x0086; + /*** MORE STRING IDS ****/ const uint16_t TLV_TYPE_STR_GROUPID = 0x00a0; const uint16_t TLV_TYPE_STR_MSGID = 0x00a1; @@ -202,6 +206,9 @@ const uint16_t TLV_TYPE_WKEYVALUESET = 0x1013; const uint16_t TLV_TYPE_STRINGSET = 0x1020; /* dummy non-existant */ const uint16_t TLV_TYPE_PEERSET = 0x1021; const uint16_t TLV_TYPE_HASHSET = 0x1022; + +const uint16_t TLV_TYPE_PGPIDSET = 0x1023; + const uint16_t TLV_TYPE_SERVICESET = 0x1030; const uint16_t TLV_TYPE_SECURITYKEY = 0x1040; @@ -215,6 +222,7 @@ const uint16_t TLV_TYPE_IMAGE = 0x1060; const uint16_t TLV_TYPE_ADDRESS_INFO = 0x1070; const uint16_t TLV_TYPE_ADDRESS_SET = 0x1071; +const uint16_t TLV_TYPE_ADDRESS = 0x1072; const uint16_t TLV_TYPE_DSDV_ENDPOINT = 0x1080; const uint16_t TLV_TYPE_DSDV_ENTRY = 0x1081; diff --git a/libretroshare/src/services/p3banlist.cc b/libretroshare/src/services/p3banlist.cc index 52beb6ecd..d531a1d3e 100644 --- a/libretroshare/src/services/p3banlist.cc +++ b/libretroshare/src/services/p3banlist.cc @@ -136,14 +136,14 @@ bool p3BanList::recvBanItem(RsBanListItem *item) for(it = item->peerList.entries.begin(); it != item->peerList.entries.end(); it++) { // Order is important!. - updated = (addBanEntry(item->PeerId(), it->addr, it->level, + updated = (addBanEntry(item->PeerId(), it->addr.addr, it->level, it->reason, it->age) || updated); } return updated; } /* overloaded from pqiNetAssistSharePeer */ -void p3BanList::updatePeer(std::string /*id*/, struct sockaddr_in addr, int /*type*/, int /*reason*/, int age) +void p3BanList::updatePeer(std::string /*id*/, const struct sockaddr_storage &addr, int /*type*/, int /*reason*/, int age) { std::string ownId = mLinkMgr->getOwnId(); @@ -160,7 +160,7 @@ void p3BanList::updatePeer(std::string /*id*/, struct sockaddr_in addr, int /*ty } -bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in &addr, int level, uint32_t reason, uint32_t age) +bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_storage &addr, int level, uint32_t reason, uint32_t age) { RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ @@ -174,10 +174,10 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in #endif /* Only Accept it - if external address */ - if (!isExternalNet(&(addr.sin_addr))) + if (!sockaddr_storage_isExternalNet(addr)) { #ifdef DEBUG_BANLIST - std::cerr << "p3BanList::addBanEntry() Ignoring Non External Addr: " << rs_inet_ntoa(addr.sin_addr); + std::cerr << "p3BanList::addBanEntry() Ignoring Non External Addr: " << sockaddr_storage_iptostring(addr); std::cerr << std::endl; #endif return false; @@ -197,8 +197,14 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in updated = true; } - std::map::iterator mit; - mit = it->second.mBanPeers.find(addr.sin_addr.s_addr); + // index is FAMILY + IP - the rest should be Zeros.. + struct sockaddr_storage bannedaddr; + sockaddr_storage_clear(bannedaddr); + sockaddr_storage_copyip(bannedaddr, addr); + sockaddr_storage_setport(bannedaddr, 0); + + std::map::iterator mit; + mit = it->second.mBanPeers.find(bannedaddr); if (mit == it->second.mBanPeers.end()) { /* add in */ @@ -208,7 +214,8 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in blp.level = level; blp.mTs = now - age; - it->second.mBanPeers[addr.sin_addr.s_addr] = blp; + + it->second.mBanPeers[bannedaddr] = blp; it->second.mLastUpdate = now; updated = true; } @@ -266,7 +273,7 @@ int p3BanList::condenseBanSources_locked() std::cerr << std::endl; #endif - std::map::const_iterator lit; + std::map::const_iterator lit; for(lit = it->second.mBanPeers.begin(); lit != it->second.mBanPeers.end(); lit++) { @@ -276,7 +283,7 @@ int p3BanList::condenseBanSources_locked() #ifdef DEBUG_BANLIST_CONDENSE std::cerr << "p3BanList::condenseBanSources_locked()"; std::cerr << " Ignoring Out-Of-Date Entry for: "; - std::cerr << rs_inet_ntoa(lit->second.addr.sin_addr); + std::cerr << sockaddr_storage_iptostring(lit->second.addr); std::cerr << std::endl; #endif continue; @@ -289,19 +296,25 @@ int p3BanList::condenseBanSources_locked() lvl++; } + struct sockaddr_storage bannedaddr; + sockaddr_storage_clear(bannedaddr); + sockaddr_storage_copyip(bannedaddr, lit->second.addr); + sockaddr_storage_setport(bannedaddr, 0); + + /* check if it exists in the Set already */ - std::map::iterator sit; - sit = mBanSet.find(lit->second.addr.sin_addr.s_addr); + std::map::iterator sit; + sit = mBanSet.find(bannedaddr); if ((sit == mBanSet.end()) || (lvl < sit->second.level)) { BanListPeer bp = lit->second; bp.level = lvl; - bp.addr.sin_port = 0; - mBanSet[lit->second.addr.sin_addr.s_addr] = bp; + sockaddr_storage_setport(bp.addr, 0); + mBanSet[bannedaddr] = bp; #ifdef DEBUG_BANLIST_CONDENSE std::cerr << "p3BanList::condenseBanSources_locked()"; std::cerr << " Added New Entry for: "; - std::cerr << rs_inet_ntoa(lit->second.addr.sin_addr); + std::cerr << sockaddr_storage_iptostring(bannedaddr); std::cerr << std::endl; #endif } @@ -310,7 +323,7 @@ int p3BanList::condenseBanSources_locked() #ifdef DEBUG_BANLIST_CONDENSE std::cerr << "p3BanList::condenseBanSources_locked()"; std::cerr << " Merging Info for: "; - std::cerr << rs_inet_ntoa(lit->second.addr.sin_addr); + std::cerr << sockaddr_storage_iptostring(bannedaddr); std::cerr << std::endl; #endif /* update if necessary */ @@ -405,7 +418,7 @@ int p3BanList::sendBanSet(std::string peerid) { RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ - std::map::iterator it; + std::map::iterator it; for(it = mBanSet.begin(); it != mBanSet.end(); it++) { if (it->second.level >= RSBANLIST_SOURCE_FRIEND) @@ -414,7 +427,7 @@ int p3BanList::sendBanSet(std::string peerid) } RsTlvBanListEntry bi; - bi.addr = it->second.addr; + bi.addr.addr = it->second.addr; bi.reason = it->second.reason; bi.level = it->second.level; bi.age = now - it->second.mTs; @@ -435,10 +448,10 @@ int p3BanList::printBanSet_locked(std::ostream &out) time_t now = time(NULL); - std::map::iterator it; + std::map::iterator it; for(it = mBanSet.begin(); it != mBanSet.end(); it++) { - out << "Ban: " << rs_inet_ntoa(it->second.addr.sin_addr); + out << "Ban: " << sockaddr_storage_iptostring(it->second.addr); out << " Reason: " << it->second.reason; out << " Level: " << it->second.level; if (it->second.level > RSBANLIST_SOURCE_FRIEND) @@ -465,12 +478,12 @@ int p3BanList::printBanSources_locked(std::ostream &out) out << " LastUpdate: " << now - it->second.mLastUpdate; out << std::endl; - std::map::const_iterator lit; + std::map::const_iterator lit; for(lit = it->second.mBanPeers.begin(); lit != it->second.mBanPeers.end(); lit++) { out << "\t"; - out << "Ban: " << rs_inet_ntoa(lit->second.addr.sin_addr); + out << "Ban: " << sockaddr_storage_iptostring(lit->second.addr); out << " Reason: " << lit->second.reason; out << " Level: " << lit->second.level; out << " Age: " << now - lit->second.mTs; diff --git a/libretroshare/src/services/p3banlist.h b/libretroshare/src/services/p3banlist.h index 56d3651d3..042a71308 100644 --- a/libretroshare/src/services/p3banlist.h +++ b/libretroshare/src/services/p3banlist.h @@ -42,7 +42,7 @@ class BanListPeer { public: - struct sockaddr_in addr; + struct sockaddr_storage addr; uint32_t reason; // Dup Self, Dup Friend int level; // LOCAL, FRIEND, FoF. time_t mTs; @@ -54,7 +54,7 @@ class BanList std::string mPeerId; /* from */ time_t mLastUpdate; - std::map mBanPeers; + std::map mBanPeers; }; @@ -74,7 +74,7 @@ class p3BanList: /* public RsBanList, */ public p3Service, public pqiNetAssistPe /***** overloaded from pqiNetAssistPeerShare *****/ - virtual void updatePeer(std::string id, struct sockaddr_in addr, int type, int reason, int age); + virtual void updatePeer(std::string id, const struct sockaddr_storage &addr, int type, int reason, int age); /***** overloaded from p3Service *****/ @@ -93,7 +93,7 @@ class p3BanList: /* public RsBanList, */ public p3Service, public pqiNetAssistPe bool processIncoming(); bool recvBanItem(RsBanListItem *item); - bool addBanEntry(const std::string &peerId, const struct sockaddr_in &addr, + bool addBanEntry(const std::string &peerId, const struct sockaddr_storage &addr, int level, uint32_t reason, uint32_t age); void sendBanLists(); int sendBanSet(std::string peerid); @@ -123,7 +123,7 @@ class p3BanList: /* public RsBanList, */ public p3Service, public pqiNetAssistPe time_t mSentListTime; std::map mBanSources; - std::map mBanSet; + std::map mBanSet; p3LinkMgr *mLinkMgr; p3NetMgr *mNetMgr; diff --git a/libretroshare/src/services/p3disc.cc b/libretroshare/src/services/p3disc.cc index 5b3af8d90..710c5c929 100644 --- a/libretroshare/src/services/p3disc.cc +++ b/libretroshare/src/services/p3disc.cc @@ -496,8 +496,8 @@ RsDiscReply *p3disc::createDiscReply(const std::string &to, const std::string &a rsPeerNetItem.netMode = detail.netMode; rsPeerNetItem.visState = detail.visState; rsPeerNetItem.lastContact = detail.lastcontact; - rsPeerNetItem.currentlocaladdr = detail.localaddr; - rsPeerNetItem.currentremoteaddr = detail.serveraddr; + rsPeerNetItem.localAddr.addr = detail.localaddr; + rsPeerNetItem.extAddr.addr = detail.serveraddr; rsPeerNetItem.dyndns = detail.dyndns; detail.ipAddrs.mLocal.loadTlv(rsPeerNetItem.localAddrList); detail.ipAddrs.mExt.loadTlv(rsPeerNetItem.extAddrList); @@ -526,8 +526,8 @@ RsDiscReply *p3disc::createDiscReply(const std::string &to, const std::string &a rsPeerNetItem.netMode = detail.netMode; rsPeerNetItem.visState = detail.visState; rsPeerNetItem.lastContact = time(NULL); - rsPeerNetItem.currentlocaladdr = detail.localaddr; - rsPeerNetItem.currentremoteaddr = detail.serveraddr; + rsPeerNetItem.localAddr.addr = detail.localaddr; + rsPeerNetItem.extAddr.addr = detail.serveraddr; rsPeerNetItem.dyndns = detail.dyndns; detail.ipAddrs.mLocal.loadTlv(rsPeerNetItem.localAddrList); detail.ipAddrs.mExt.loadTlv(rsPeerNetItem.extAddrList); @@ -677,7 +677,7 @@ void p3disc::recvPeerDetails(RsDiscReply *item, const std::string &certGpgId) bool new_info = false; addDiscoveryData(item->PeerId(), pit->pid,item_gpg_id, - item->aboutId, pit->currentlocaladdr, pit->currentremoteaddr, 0, time(NULL),new_info); + item->aboutId, pit->localAddr.addr, pit->extAddr.addr, 0, time(NULL),new_info); if(new_info) should_notify_discovery = true ; @@ -724,7 +724,7 @@ void p3disc::recvPeerDetails(RsDiscReply *item, const std::string &certGpgId) mNetMgr->netAssistFriend(pit->pid,false); /* inform NetMgr that we know this peer */ - mNetMgr->netAssistKnownPeer(pit->pid, pit->currentremoteaddr, + mNetMgr->netAssistKnownPeer(pit->pid, pit->extAddr.addr, NETASSIST_KNOWN_PEER_FOF | NETASSIST_KNOWN_PEER_OFFLINE); continue; @@ -752,10 +752,10 @@ void p3disc::recvPeerDetails(RsDiscReply *item, const std::string &certGpgId) if (mPeerMgr->setLocation(pit->pid, pit->location)) { peerDataChanged = true; } - if (mPeerMgr->setLocalAddress(pit->pid, pit->currentlocaladdr)) { + if (mPeerMgr->setLocalAddress(pit->pid, pit->localAddr.addr)) { peerDataChanged = true; } - if (mPeerMgr->setExtAddress(pit->pid, pit->currentremoteaddr)) { + if (mPeerMgr->setExtAddress(pit->pid, pit->extAddr.addr)) { peerDataChanged = true; } if (mPeerMgr->setVisState(pit->pid, pit->visState)) { @@ -1024,7 +1024,7 @@ void p3disc::setGPGOperation(AuthGPGOperation *operation) /*************************************************************************************/ /* Storing Network Graph */ /*************************************************************************************/ -int p3disc::addDiscoveryData(const std::string& fromId, const std::string& aboutId,const std::string& from_gpg_id,const std::string& about_gpg_id, const struct sockaddr_in& laddr, const struct sockaddr_in& raddr, uint32_t flags, time_t ts,bool& new_info) +int p3disc::addDiscoveryData(const std::string& fromId, const std::string& aboutId,const std::string& from_gpg_id,const std::string& about_gpg_id, const struct sockaddr_storage &laddr, const struct sockaddr_storage &raddr, uint32_t flags, time_t ts,bool& new_info) { RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/ diff --git a/libretroshare/src/services/p3disc.h b/libretroshare/src/services/p3disc.h index 5cf4663e1..4a3e8aca5 100644 --- a/libretroshare/src/services/p3disc.h +++ b/libretroshare/src/services/p3disc.h @@ -51,8 +51,8 @@ class autoserver :ts(0), discFlags(0) { return;} std::string id; - struct sockaddr_in localAddr; - struct sockaddr_in remoteAddr; + struct sockaddr_storage localAddr; + struct sockaddr_storage remoteAddr; time_t ts; uint32_t discFlags; @@ -137,7 +137,7 @@ void removeFriend(std::string ssl_id); //keep tracks of removed friend so we're /* handle network shape */ int addDiscoveryData(const std::string& fromId, const std::string& aboutId, const std::string& fromGPGId,const std::string& aboutGPGId, - const struct sockaddr_in& laddr, const struct sockaddr_in& raddr, + const struct sockaddr_storage &laddr, const struct sockaddr_storage &raddr, uint32_t flags, time_t ts,bool& new_info); //int idServers(); diff --git a/libretroshare/src/tcponudp/rsudpstack.h b/libretroshare/src/tcponudp/rsudpstack.h index e4e4668bc..f8d645450 100644 --- a/libretroshare/src/tcponudp/rsudpstack.h +++ b/libretroshare/src/tcponudp/rsudpstack.h @@ -62,13 +62,20 @@ class rsUdpStack: public UdpStack, public pqiNetListener :UdpStack(testmode, local) { return; } /* from pqiNetListener */ -virtual bool resetListener(struct sockaddr_in &local) +virtual bool resetListener(const struct sockaddr_storage &local) { - // The const_cast below is not so nice but without it, the compiler can't - // find the correct operator<<(). No idea why! - std::cerr << "rsUdpStack::resetListener(" << const_cast(local) << ")"; + std::cerr << "rsUdpStack::resetListener(" << sockaddr_storage_tostring(local) << ")"; std::cerr << std::endl; - return resetAddress(local); + + if (local.ss_family != AF_INET) + { + std::cerr << "rsUdpStack::resetListener() NOT IPv4 ERROR"; + std::cerr << std::endl; + abort(); + } + + struct sockaddr_in *addr = (struct sockaddr_in *) &local; + return resetAddress(*addr); } }; @@ -83,14 +90,14 @@ class rsFixedUdpStack: public UdpStack, public pqiNetListener :UdpStack(testmode, local) { return; } /* from pqiNetListener */ -virtual bool resetListener(struct sockaddr_in &local) +virtual bool resetListener(const struct sockaddr_storage &local) { struct sockaddr_in addr; getLocalAddress(addr); // The const_cast below is not so nice but without it, the compiler can't // find the correct operator<<(). No idea why! - std::cerr << "rsFixedUdpStack::resetListener(" << const_cast(local) << ")"; + std::cerr << "rsFixedUdpStack::resetListener(" << sockaddr_storage_tostring(local) << ")"; std::cerr << " Resetting with original addr: " << const_cast(addr); std::cerr << std::endl; diff --git a/libretroshare/src/util/dnsresolver.cc b/libretroshare/src/util/dnsresolver.cc index cd9b312d5..e318b38cb 100644 --- a/libretroshare/src/util/dnsresolver.cc +++ b/libretroshare/src/util/dnsresolver.cc @@ -1,6 +1,8 @@ #include "dnsresolver.h" #include "pqi/pqinetwork.h" +#include "util/rsnet.h" + #ifndef WIN32 #include @@ -64,13 +66,19 @@ void *solveDNSEntries(void *p) { RsStackMutex mut(dnsr->_rdnsMtx) ; + DNSResolver::AddrInfo &info = (*dnsr->_addr_map)[next_call]; + if(pHost) { - (*dnsr->_addr_map)[next_call].state = DNSResolver::DNS_HAVE ; - (*dnsr->_addr_map)[next_call].addr.s_addr = *(unsigned long*) (pHost->h_addr); + info.state = DNSResolver::DNS_HAVE ; + // IPv4 for the moment. + struct sockaddr_in *addrv4p = (struct sockaddr_in *) &(info.addr); + addrv4p->sin_family = AF_INET; + addrv4p->sin_addr.s_addr = *(unsigned long*) (pHost->h_addr); + addrv4p->sin_port = htons(0); } else - (*dnsr->_addr_map)[next_call].state = DNSResolver::DNS_LOOKUP_ERROR ; + info.state = DNSResolver::DNS_LOOKUP_ERROR ; } } } @@ -103,9 +111,9 @@ void DNSResolver::reset() _addr_map->clear(); } -bool DNSResolver::getIPAddressFromString(const std::string& server_name,struct in_addr& addr) +bool DNSResolver::getIPAddressFromString(const std::string& server_name,struct sockaddr_storage &addr) { - addr.s_addr = 0 ; + sockaddr_storage_clear(addr); bool running = false; { RsStackMutex mut(_rdnsMtx) ; diff --git a/libretroshare/src/util/dnsresolver.h b/libretroshare/src/util/dnsresolver.h index 51aa77dc2..1f9581d1e 100644 --- a/libretroshare/src/util/dnsresolver.h +++ b/libretroshare/src/util/dnsresolver.h @@ -18,7 +18,7 @@ class DNSResolver DNSResolver() ; ~DNSResolver() ; - bool getIPAddressFromString(const std::string& server_name,struct in_addr& addr) ; + bool getIPAddressFromString(const std::string& server_name,struct sockaddr_storage &addr) ; void start_request() ; void reset() ; @@ -30,7 +30,7 @@ class DNSResolver { uint32_t state ; // state: Looked-up, not found, have time_t last_lookup_time ; // last lookup time - struct in_addr addr ; + struct sockaddr_storage addr ; }; friend void *solveDNSEntries(void *p) ; diff --git a/libretroshare/src/util/extaddrfinder.cc b/libretroshare/src/util/extaddrfinder.cc index f6fd43b33..f41db4ef8 100644 --- a/libretroshare/src/util/extaddrfinder.cc +++ b/libretroshare/src/util/extaddrfinder.cc @@ -150,11 +150,11 @@ void* doExtAddrSearch(void *p) // thread safe copy results. // { - RsStackMutex mtx(af->_addrMtx) ; + RsStackMutex mtx(af->mAddrMtx) ; - *(af->_found) = false ; - *(af->mFoundTS) = time(NULL) ; - *(af->_searching) = false ; + af->mFound = false ; + af->mFoundTS = time(NULL) ; + af->mSearching = false ; } pthread_exit(NULL); return NULL ; @@ -163,24 +163,25 @@ void* doExtAddrSearch(void *p) sort(res.begin(),res.end()) ; // eliminates outliers. - if(!inet_aton(res[res.size()/2].c_str(),af->_addr)) + + if(!sockaddr_storage_ipv4_aton(af->mAddr, res[res.size()/2].c_str())) { std::cerr << "ExtAddrFinder: Could not convert " << res[res.size()/2] << " into an address." << std::endl ; { - RsStackMutex mtx(af->_addrMtx) ; - *(af->_found) = false ; - *(af->mFoundTS) = time(NULL) ; - *(af->_searching) = false ; + RsStackMutex mtx(af->mAddrMtx) ; + af->mFound = false ; + af->mFoundTS = time(NULL) ; + af->mSearching = false ; } pthread_exit(NULL); return NULL ; } { - RsStackMutex mtx(af->_addrMtx) ; - *(af->_found) = true ; - *(af->mFoundTS) = time(NULL) ; - *(af->_searching) = false ; + RsStackMutex mtx(af->mAddrMtx) ; + af->mFound = true ; + af->mFoundTS = time(NULL) ; + af->mSearching = false ; } pthread_exit(NULL); @@ -196,44 +197,44 @@ void ExtAddrFinder::start_request() pthread_detach(tid); /* so memory is reclaimed in linux */ } -bool ExtAddrFinder::hasValidIP(struct in_addr *addr) +bool ExtAddrFinder::hasValidIP(struct sockaddr_storage &addr) { #ifdef EXTADDRSEARCH_DEBUG std::cerr << "ExtAddrFinder: Getting ip." << std::endl ; #endif { - RsStackMutex mut(_addrMtx) ; - if(*_found) + RsStackMutex mut(mAddrMtx) ; + if(mFound) { #ifdef EXTADDRSEARCH_DEBUG std::cerr << "ExtAddrFinder: Has stored ip: responding with this ip." << std::endl ; #endif - *addr = *_addr; + addr = mAddr; } } time_t delta; { - RsStackMutex mut(_addrMtx) ; + RsStackMutex mut(mAddrMtx) ; //timeout the current ip - delta = time(NULL) - *mFoundTS; + delta = time(NULL) - mFoundTS; } if((uint32_t)delta > MAX_IP_STORE) {//launch a research - if( _addrMtx.trylock()) + if( mAddrMtx.trylock()) { - if(!*_searching) + if(!mSearching) { #ifdef EXTADDRSEARCH_DEBUG std::cerr << "ExtAddrFinder: No stored ip: Initiating new search." << std::endl ; #endif - *_searching = true ; + mSearching = true ; start_request() ; } #ifdef EXTADDRSEARCH_DEBUG else std::cerr << "ExtAddrFinder: Already searching." << std::endl ; #endif - _addrMtx.unlock(); + mAddrMtx.unlock(); } #ifdef EXTADDRSEARCH_DEBUG else @@ -241,24 +242,17 @@ bool ExtAddrFinder::hasValidIP(struct in_addr *addr) #endif } - RsStackMutex mut(_addrMtx) ; - return *_found ; + RsStackMutex mut(mAddrMtx) ; + return mFound ; } void ExtAddrFinder::reset() { -// while(*_searching) -//#ifdef WIN32 -// Sleep(1000) ; -//#else -// sleep(1) ; -//#endif + RsStackMutex mut(mAddrMtx) ; - RsStackMutex mut(_addrMtx) ; - - *_found = false ; - *_searching = false ; - *mFoundTS = time(NULL) - MAX_IP_STORE; + mFound = false ; + mSearching = false ; + mFoundTS = time(NULL) - MAX_IP_STORE; } ExtAddrFinder::~ExtAddrFinder() @@ -266,37 +260,20 @@ ExtAddrFinder::~ExtAddrFinder() #ifdef EXTADDRSEARCH_DEBUG std::cerr << "ExtAddrFinder: Deleting ExtAddrFinder." << std::endl ; #endif -// while(*_searching) -//#ifdef WIN32 -// Sleep(1000) ; -//#else -// sleep(1) ; -//#endif - RsStackMutex mut(_addrMtx) ; - - delete _found ; - delete _searching ; - free (_addr) ; } -ExtAddrFinder::ExtAddrFinder() : _addrMtx("ExtAddrFinder") +ExtAddrFinder::ExtAddrFinder() : mAddrMtx("ExtAddrFinder") { #ifdef EXTADDRSEARCH_DEBUG std::cerr << "ExtAddrFinder: Creating new ExtAddrFinder." << std::endl ; #endif - RsStackMutex mut(_addrMtx) ; + RsStackMutex mut(mAddrMtx) ; - _found = new bool ; - *_found = false ; - - _searching = new bool ; - *_searching = false ; - - mFoundTS = new time_t; - *mFoundTS = time(NULL) - MAX_IP_STORE; - - _addr = (in_addr*)malloc(sizeof(in_addr)) ; + mFound = false; + mSearching = false; + mFoundTS = time(NULL) - MAX_IP_STORE; + sockaddr_storage_clear(mAddr); _ip_servers.push_back(std::string( "checkip.dyndns.org" )) ; _ip_servers.push_back(std::string( "www.myip.dk" )) ; diff --git a/libretroshare/src/util/extaddrfinder.h b/libretroshare/src/util/extaddrfinder.h index ec8782c7c..203e1ff9e 100644 --- a/libretroshare/src/util/extaddrfinder.h +++ b/libretroshare/src/util/extaddrfinder.h @@ -3,6 +3,7 @@ #include #include #include "util/rsthreads.h" +#include "util/rsnet.h" struct sockaddr ; @@ -12,7 +13,7 @@ class ExtAddrFinder ExtAddrFinder() ; ~ExtAddrFinder() ; - bool hasValidIP(struct in_addr *addr) ; + bool hasValidIP(struct sockaddr_storage &addr) ; void getIPServersList(std::list& ip_servers) { ip_servers = _ip_servers ; } void start_request() ; @@ -22,10 +23,10 @@ class ExtAddrFinder private: friend void* doExtAddrSearch(void *p) ; - time_t *mFoundTS; - RsMutex _addrMtx ; - struct in_addr *_addr ; - bool *_found ; - bool *_searching ; + RsMutex mAddrMtx ; + time_t mFoundTS; + struct sockaddr_storage mAddr; + bool mFound ; + bool mSearching ; std::list _ip_servers ; }; diff --git a/libretroshare/src/util/rsnet.h b/libretroshare/src/util/rsnet.h index 15bf5749a..7b854c00c 100644 --- a/libretroshare/src/util/rsnet.h +++ b/libretroshare/src/util/rsnet.h @@ -75,4 +75,46 @@ std::ostream& operator<<(std::ostream& o,const struct sockaddr_in&) ; std::string rs_inet_ntoa(struct in_addr in); +/***************************/ +// sockaddr_storage fns. + +void sockaddr_storage_clear(struct sockaddr_storage &addr); + +// mods. +bool sockaddr_storage_zeroip(struct sockaddr_storage &addr); +bool sockaddr_storage_copyip(struct sockaddr_storage &dst, const struct sockaddr_storage &src); +uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr); +bool sockaddr_storage_setport(struct sockaddr_storage &addr, uint16_t port); + +bool sockaddr_storage_ipv4_aton(struct sockaddr_storage &addr, const char *name); +bool sockaddr_storage_ipv4_setport(struct sockaddr_storage &addr, const uint16_t port); + +// comparisons. +bool operator<(const struct sockaddr_storage &a, const struct sockaddr_storage &b); + +bool sockaddr_storage_same(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2); +bool sockaddr_storage_samefamily(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2); +bool sockaddr_storage_sameip(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2); +bool sockaddr_storage_samenet(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2); +bool sockaddr_storage_samesubnet(const struct sockaddr_storage &addr, const struct sockaddr_storage &addr2); + +// string, +std::string sockaddr_storage_tostring(const struct sockaddr_storage &addr); +std::string sockaddr_storage_familytostring(const struct sockaddr_storage &addr); +std::string sockaddr_storage_iptostring(const struct sockaddr_storage &addr); +std::string sockaddr_storage_porttostring(const struct sockaddr_storage &addr); + +// output +//void sockaddr_storage_output(const struct sockaddr_storage &addr, std::ostream &out); +//void sockaddr_storage_ipoutput(const struct sockaddr_storage &addr, std::ostream &out); + +// net checks. +bool sockaddr_storage_isnull(const struct sockaddr_storage &addr); +bool sockaddr_storage_isValidNet(const struct sockaddr_storage &addr); +bool sockaddr_storage_isLoopbackNet(const struct sockaddr_storage &addr); +bool sockaddr_storage_isPrivateNet(const struct sockaddr_storage &addr); +bool sockaddr_storage_isExternalNet(const struct sockaddr_storage &addr); + + + #endif /* RS_UNIVERSAL_NETWORK_HEADER */ diff --git a/libretroshare/src/zeroconf/p3zcnatassist.cc b/libretroshare/src/zeroconf/p3zcnatassist.cc index 5bd45048a..1e35de50b 100644 --- a/libretroshare/src/zeroconf/p3zcnatassist.cc +++ b/libretroshare/src/zeroconf/p3zcnatassist.cc @@ -177,7 +177,7 @@ void p3zcNatAssist::setExternalPort(unsigned short eport_in) } } -bool p3zcNatAssist::getInternalAddress(struct sockaddr_in &addr) +bool p3zcNatAssist::getInternalAddress(struct sockaddr_storage &addr) { #ifdef DEBUG_ZCNATASSIST @@ -189,7 +189,7 @@ bool p3zcNatAssist::getInternalAddress(struct sockaddr_in &addr) } -bool p3zcNatAssist::getExternalAddress(struct sockaddr_in &addr) +bool p3zcNatAssist::getExternalAddress(struct sockaddr_storage &addr) { RsStackMutex stack(mZcMtx); /****** STACK LOCK MUTEX *******/ @@ -397,11 +397,18 @@ void p3zcNatAssist::callbackMapping(DNSServiceRef sdRef, DNSServiceFlags flags, mMapped = true; - mExternalAddress.sin_addr.s_addr = externalAddress; - mExternalAddress.sin_port = externalPort; + + // THIS APPEARS TO BE IPv4 ONLY!. + + sockaddr_storage_clear(mExternalAddress); + struct sockaddr_in *addr = (struct sockaddr_in *) &mExternalAddress; + + addr->sin_family = AF_INET; + addr->sin_addr.s_addr = externalAddress; + addr->sin_port = externalPort; + mTTL = ttl; - std::cerr << "p3zcNatAssist::callbackMapping() Success"; std::cerr << std::endl; @@ -411,8 +418,7 @@ void p3zcNatAssist::callbackMapping(DNSServiceRef sdRef, DNSServiceFlags flags, std::cerr << "internalPort: " << ntohs(internalPort); std::cerr << std::endl; - std::cerr << "externalAddress: " << rs_inet_ntoa(mExternalAddress.sin_addr); - std::cerr << ":" << ntohs(mExternalAddress.sin_port); + std::cerr << "externalAddress: " << sockaddr_storage_tostring(mExternalAddress); std::cerr << std::endl; std::cerr << "protocol: " << protocol; diff --git a/libretroshare/src/zeroconf/p3zcnatassist.h b/libretroshare/src/zeroconf/p3zcnatassist.h index 72bde8dea..ad92d9a2a 100644 --- a/libretroshare/src/zeroconf/p3zcnatassist.h +++ b/libretroshare/src/zeroconf/p3zcnatassist.h @@ -61,8 +61,8 @@ virtual bool getActive(); virtual void setInternalPort(unsigned short iport_in); virtual void setExternalPort(unsigned short eport_in); -virtual bool getInternalAddress(struct sockaddr_in &addr); -virtual bool getExternalAddress(struct sockaddr_in &addr); +virtual bool getInternalAddress(struct sockaddr_storage &addr); +virtual bool getExternalAddress(struct sockaddr_storage &addr); /* TO IMPLEMENT: New Port Forward interface to support as many ports as necessary */ virtual bool requestPortForward(const PortForwardParams ¶ms) { return false; } @@ -101,7 +101,7 @@ virtual bool statusPortForward(const uint32_t fwdId, PortForwardParams ¶m uint16_t mExternalPort; bool mExternalPortSet; - struct sockaddr_in mExternalAddress; + struct sockaddr_storage mExternalAddress; int mTTL; DNSServiceRef mMappingRef; diff --git a/libretroshare/src/zeroconf/p3zeroconf.cc b/libretroshare/src/zeroconf/p3zeroconf.cc index 82a9f8ee1..34cd22105 100644 --- a/libretroshare/src/zeroconf/p3zeroconf.cc +++ b/libretroshare/src/zeroconf/p3zeroconf.cc @@ -236,7 +236,7 @@ bool p3ZeroConf::dropPeer(std::string pid) /* extract current peer status */ bool p3ZeroConf::getPeerStatus(std::string id, - struct sockaddr_in &/*laddr*/, struct sockaddr_in &/*raddr*/, + struct sockaddr_storage &/*laddr*/, struct sockaddr_storage &/*raddr*/, uint32_t &/*type*/, uint32_t &/*mode*/) { /* remove unused parameter warnings */ @@ -251,7 +251,7 @@ bool p3ZeroConf::getPeerStatus(std::string id, } #if 0 -bool p3ZeroConf::getExternalInterface(struct sockaddr_in &/*raddr*/, +bool p3ZeroConf::getExternalInterface(struct sockaddr_storage &/*raddr*/, uint32_t &/*mode*/) { @@ -277,12 +277,12 @@ bool p3ZeroConf::setAttachMode(bool on) return true; } -int p3ZeroConf::addBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age) +int p3ZeroConf::addBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age) { return 1; } -int p3ZeroConf::addKnownPeer(const std::string &pid, const struct sockaddr_in &addr, uint32_t flags) +int p3ZeroConf::addKnownPeer(const std::string &pid, const struct sockaddr_storage &addr, uint32_t flags) { return 1; } @@ -563,7 +563,9 @@ int p3ZeroConf::checkQueryResults() time_t now = time(NULL); uint32_t flags = RS_CB_FLAG_MODE_TCP; uint32_t source = RS_CB_DHT; // SHOULD ADD NEW SOURCE ZC??? - struct sockaddr_in dummyProxyAddr, dummySrcAddr; + struct sockaddr_storage dummyProxyAddr, dummySrcAddr; + sockaddr_storage_clear(dummyProxyAddr); + sockaddr_storage_clear(dummySrcAddr); mConnCb->peerConnectRequest(qr.sslId, qr.addr, dummyProxyAddr, dummySrcAddr, source, flags, 0, 0); @@ -1145,8 +1147,13 @@ void p3ZeroConf::callbackQueryIp( DNSServiceRef /* sdRef */, DNSServiceFlags fla if ((rrtype == kDNSServiceType_A) && (rdlen == 4)) { - qr.addr.sin_addr.s_addr = *((uint32_t *) rdata); - + // IPV4 type. + sockaddr_storage_clear(qr.addr); + struct sockaddr_in *addr = (struct sockaddr_in *) &(qr.addr); + addr->sin_family = AF_INET; + addr->sin_addr.s_addr = *((uint32_t *) rdata); + addr->sin_port = htons(0); + if (locked_completeQueryResult(qr)) // Saves Query Results, and fills in Port details. { mQueryResults.push_back(qr); @@ -1206,7 +1213,7 @@ int p3ZeroConf::locked_completeQueryResult(zcQueryResult &qr) std::cerr << "p3ZeroConf::locked_completeQueryResults() Filling in Peer IpAddress"; std::cerr << std::endl; - qr.addr.sin_port = htons(lit->second.mPort); + sockaddr_storage_setport(qr.addr, lit->second.mPort); lit->second.mAddress = qr.addr; lit->second.mStatus |= ZC_STATUS_IPADDRESS; lit->second.mAddrTs = time(NULL); diff --git a/libretroshare/src/zeroconf/p3zeroconf.h b/libretroshare/src/zeroconf/p3zeroconf.h index d1130cf94..46365633b 100644 --- a/libretroshare/src/zeroconf/p3zeroconf.h +++ b/libretroshare/src/zeroconf/p3zeroconf.h @@ -91,7 +91,7 @@ class zcQueryResult // Extra ones. std::string sslId; std::string gpgId; - struct sockaddr_in addr; + struct sockaddr_storage addr; }; @@ -121,7 +121,7 @@ class zcLocationDetails std::string mFullName; uint16_t mPort; - struct sockaddr_in mAddress; + struct sockaddr_storage mAddress; time_t mAddrTs; }; @@ -171,15 +171,15 @@ virtual bool getNetworkStats(uint32_t &netsize, uint32_t &localnetsize); virtual bool findPeer(std::string id); virtual bool dropPeer(std::string id); -virtual int addBadPeer(const struct sockaddr_in &addr, uint32_t reason, uint32_t flags, uint32_t age); -virtual int addKnownPeer(const std::string &pid, const struct sockaddr_in &addr, uint32_t flags); +virtual int addBadPeer(const struct sockaddr_storage &addr, uint32_t reason, uint32_t flags, uint32_t age); +virtual int addKnownPeer(const std::string &pid, const struct sockaddr_storage &addr, uint32_t flags); /* feedback on success failure of Connections */ virtual void ConnectionFeedback(std::string pid, int state); /* extract current peer status */ virtual bool getPeerStatus(std::string id, - struct sockaddr_in &laddr, struct sockaddr_in &raddr, + struct sockaddr_storage &laddr, struct sockaddr_storage &raddr, uint32_t &type, uint32_t &mode); virtual bool setAttachMode(bool on);