From bed856425fbfe83e0c3dc3e372d73dc3b32d6031 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 3 Mar 2018 00:08:56 +0100 Subject: [PATCH] Share additional addresses via RsCertificate --- libresapi/src/api/PeersHandler.cpp | 5 + libretroshare/src/pgp/rscertificate.cc | 235 ++++++++++-------- libretroshare/src/pgp/rscertificate.h | 25 +- libretroshare/src/pqi/p3peermgr.cc | 62 ++++- libretroshare/src/pqi/p3peermgr.h | 2 + libretroshare/src/retroshare/rspeers.h | 2 + libretroshare/src/rsserver/p3peers.cc | 16 +- libretroshare/src/rsserver/p3peers.h | 4 +- libretroshare/src/util/rsnet_ss.cc | 11 +- libretroshare/src/util/rsurl.cc | 34 +-- libretroshare/src/util/rsurl.h | 12 +- retroshare-gui/src/gui/common/FriendList.cpp | 4 + .../src/gui/connect/ConnectFriendWizard.cpp | 4 + 13 files changed, 267 insertions(+), 149 deletions(-) diff --git a/libresapi/src/api/PeersHandler.cpp b/libresapi/src/api/PeersHandler.cpp index 66def7eab..f812a54f4 100644 --- a/libresapi/src/api/PeersHandler.cpp +++ b/libresapi/src/api/PeersHandler.cpp @@ -727,6 +727,11 @@ void PeersHandler::handleWildcard(Request &req, Response &resp) peerDetails.localPort ); if (!peerDetails.dyndns.empty()) mRsPeers->setDynDNS(peerDetails.id, peerDetails.dyndns); + for(auto&& ipr : peerDetails.ipAddressList) + mRsPeers->addPeerLocator( + peerDetails.id, + RsUrl(ipr.substr(0, ipr.find(' '))) ); + } } while(false); diff --git a/libretroshare/src/pgp/rscertificate.cc b/libretroshare/src/pgp/rscertificate.cc index 70a00328f..08ea78a31 100644 --- a/libretroshare/src/pgp/rscertificate.cc +++ b/libretroshare/src/pgp/rscertificate.cc @@ -1,3 +1,23 @@ +/* + * RetroShare + * + * Copyright (C) 2012-2014 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include #include #include @@ -11,25 +31,21 @@ //#define DEBUG_RSCERTIFICATE -static const std::string PGP_CERTIFICATE_START ( "-----BEGIN PGP PUBLIC KEY BLOCK-----" ); -static const std::string PGP_CERTIFICATE_END ( "-----END PGP PUBLIC KEY BLOCK-----" ); -static const std::string EXTERNAL_IP_BEGIN_SECTION ( "--EXT--" ); -static const std::string LOCAL_IP_BEGIN_SECTION ( "--LOCAL--" ); -static const std::string SSLID_BEGIN_SECTION ( "--SSLID--" ); -static const std::string LOCATION_BEGIN_SECTION ( "--LOCATION--" ); -static const std::string HIDDEN_NODE_BEGIN_SECTION ( "--HIDDEN--" ); +static const uint8_t CERTIFICATE_VERSION_06 = 0x06; -static const uint8_t CERTIFICATE_PTAG_PGP_SECTION = 0x01 ; -static const uint8_t CERTIFICATE_PTAG_EXTIPANDPORT_SECTION = 0x02 ; -static const uint8_t CERTIFICATE_PTAG_LOCIPANDPORT_SECTION = 0x03 ; -static const uint8_t CERTIFICATE_PTAG_DNS_SECTION = 0x04 ; -static const uint8_t CERTIFICATE_PTAG_SSLID_SECTION = 0x05 ; -static const uint8_t CERTIFICATE_PTAG_NAME_SECTION = 0x06 ; -static const uint8_t CERTIFICATE_PTAG_CHECKSUM_SECTION = 0x07 ; -static const uint8_t CERTIFICATE_PTAG_HIDDENNODE_SECTION = 0x08 ; -static const uint8_t CERTIFICATE_PTAG_VERSION_SECTION = 0x09 ; - -static const uint8_t CERTIFICATE_VERSION_06 = 0x06 ; +enum CertificatePtag : uint8_t +{ + CERTIFICATE_PTAG_PGP_SECTION = 0x01, + CERTIFICATE_PTAG_EXTIPANDPORT_SECTION = 0x02, + CERTIFICATE_PTAG_LOCIPANDPORT_SECTION = 0x03, + CERTIFICATE_PTAG_DNS_SECTION = 0x04, + CERTIFICATE_PTAG_SSLID_SECTION = 0x05, + CERTIFICATE_PTAG_NAME_SECTION = 0x06, + CERTIFICATE_PTAG_CHECKSUM_SECTION = 0x07, + CERTIFICATE_PTAG_HIDDENNODE_SECTION = 0x08, + CERTIFICATE_PTAG_VERSION_SECTION = 0x09, + CERTIFICATE_PTAG_EXTRA_LOCATOR = 10 +}; static bool is_acceptable_radix64Char(char c) { @@ -95,6 +111,14 @@ std::string RsCertificate::toStdString() const addPacket( CERTIFICATE_PTAG_NAME_SECTION , (unsigned char *)location_name.c_str() ,location_name.length() , buf, p, BS ) ; addPacket( CERTIFICATE_PTAG_SSLID_SECTION , location_id.toByteArray() ,location_id.SIZE_IN_BYTES, buf, p, BS ) ; + + for (const RsUrl& locator : mLocators) + { + std::string urlStr(locator.toString()); + addPacket( CERTIFICATE_PTAG_EXTRA_LOCATOR, + (unsigned char *) urlStr.c_str(), urlStr.size(), + buf, p, BS ); + } } uint32_t computed_crc = PGPKeyManagement::compute24bitsCRC(buf,p) ; @@ -195,7 +219,10 @@ RsCertificate::RsCertificate(const RsPeerDetails& Detail, const unsigned char *b memset(ipv4_external_ip_and_port,0,6) ; } - dns_name = Detail.dyndns ; + dns_name = Detail.dyndns; + + for(auto&& ipr : Detail.ipAddressList) + mLocators.insert(RsUrl(ipr.substr(0, ipr.find(' ')))); } } else @@ -254,19 +281,19 @@ bool RsCertificate::initFromString(const std::string& instr,uint32_t& err_code) std::vector bf = Radix64::decode(str) ; size_t size = bf.size(); - bool checksum_check_passed = false ; - unsigned char *buf = bf.data() ; - size_t total_s = 0 ; - only_pgp = true ; - uint8_t certificate_version = 0x00 ; + bool checksum_check_passed = false; + unsigned char *buf = bf.data(); + size_t total_s = 0; + only_pgp = true; + uint8_t certificate_version = 0x00; while(total_s < size) { uint8_t ptag = buf[0]; - buf = &buf[1] ; + buf = &buf[1]; - unsigned char *buf2 = buf ; - uint32_t s = PGPKeyParser::read_125Size(buf) ; + unsigned char *buf2 = buf; + uint32_t s = PGPKeyParser::read_125Size(buf); total_s += 1 + ((unsigned long)buf-(unsigned long)buf2) ; @@ -281,90 +308,80 @@ bool RsCertificate::initFromString(const std::string& instr,uint32_t& err_code) #endif switch(ptag) { - case CERTIFICATE_PTAG_VERSION_SECTION: certificate_version = buf[0] ; - buf = &buf[s] ; - break ; - - - case CERTIFICATE_PTAG_PGP_SECTION: binary_pgp_key = new unsigned char[s] ; - memcpy(binary_pgp_key,buf,s) ; - binary_pgp_key_size = s ; - buf = &buf[s] ; - break ; - - case CERTIFICATE_PTAG_NAME_SECTION: location_name = std::string((char *)buf,s) ; - buf = &buf[s] ; - break ; - - case CERTIFICATE_PTAG_SSLID_SECTION: - if(s != location_id.SIZE_IN_BYTES) - { - err_code = CERTIFICATE_PARSING_ERROR_INVALID_LOCATION_ID ; - return false ; - } - - location_id = RsPeerId(buf) ; - buf = &buf[s] ; - only_pgp = false ; - break ; - - case CERTIFICATE_PTAG_DNS_SECTION: dns_name = std::string((char *)buf,s) ; - buf = &buf[s] ; - break ; - - case CERTIFICATE_PTAG_HIDDENNODE_SECTION: - hidden_node_address = std::string((char *)buf,s); - hidden_node = true; - buf = &buf[s]; - - break ; - - case CERTIFICATE_PTAG_LOCIPANDPORT_SECTION: - if(s != 6) - - { - err_code = CERTIFICATE_PARSING_ERROR_INVALID_LOCAL_IP; - return false ; - } - - memcpy(ipv4_internal_ip_and_port,buf,s) ; - buf = &buf[s] ; - break ; - case CERTIFICATE_PTAG_EXTIPANDPORT_SECTION: - if(s != 6) - { - err_code = CERTIFICATE_PARSING_ERROR_INVALID_EXTERNAL_IP; - return false ; - } - - memcpy(ipv4_external_ip_and_port,buf,s) ; - buf = &buf[s] ; - break ; - case CERTIFICATE_PTAG_CHECKSUM_SECTION: - { - if(s != 3 || total_s+3 != size) - { - err_code = CERTIFICATE_PARSING_ERROR_INVALID_CHECKSUM_SECTION ; - return false ; - } - uint32_t computed_crc = PGPKeyManagement::compute24bitsCRC(bf.data(),size-5) ; - uint32_t certificate_crc = buf[0] + (buf[1] << 8) + (buf[2] << 16) ; - - if(computed_crc != certificate_crc) - { - err_code = CERTIFICATE_PARSING_ERROR_CHECKSUM_ERROR ; - return false ; - } - else - checksum_check_passed = true ; - } - break ; - default: - std::cerr << "(WW) unknwown PTAG 0x" << std::hex << ptag << std::dec << " in certificate! Ignoring it." << std::endl; - buf = &buf[s] ; - break ; + case CERTIFICATE_PTAG_VERSION_SECTION: + certificate_version = buf[0]; + break; + case CERTIFICATE_PTAG_PGP_SECTION: + binary_pgp_key = new unsigned char[s]; + memcpy(binary_pgp_key,buf,s); + binary_pgp_key_size = s; + break; + case CERTIFICATE_PTAG_NAME_SECTION: + location_name = std::string((char *)buf,s); + break; + case CERTIFICATE_PTAG_SSLID_SECTION: + if(s != location_id.SIZE_IN_BYTES) + { + err_code = CERTIFICATE_PARSING_ERROR_INVALID_LOCATION_ID; + return false; + } + location_id = RsPeerId(buf); + only_pgp = false; + break; + case CERTIFICATE_PTAG_DNS_SECTION: + dns_name = std::string((char *)buf,s); + break; + case CERTIFICATE_PTAG_HIDDENNODE_SECTION: + hidden_node_address = std::string((char *)buf,s); + hidden_node = true; + break; + case CERTIFICATE_PTAG_LOCIPANDPORT_SECTION: + if(s != 6) + { + err_code = CERTIFICATE_PARSING_ERROR_INVALID_LOCAL_IP; + return false; + } + memcpy(ipv4_internal_ip_and_port,buf,s); + break; + case CERTIFICATE_PTAG_EXTIPANDPORT_SECTION: + if(s != 6) + { + err_code = CERTIFICATE_PARSING_ERROR_INVALID_EXTERNAL_IP; + return false; + } + memcpy(ipv4_external_ip_and_port,buf,s); + break; + case CERTIFICATE_PTAG_CHECKSUM_SECTION: + { + if(s != 3 || total_s+3 != size) + { + err_code = + CERTIFICATE_PARSING_ERROR_INVALID_CHECKSUM_SECTION; + return false; + } + uint32_t computed_crc = + PGPKeyManagement::compute24bitsCRC(bf.data(),size-5); + uint32_t certificate_crc = + buf[0] + (buf[1] << 8) + (buf[2] << 16); + if(computed_crc != certificate_crc) + { + err_code = CERTIFICATE_PARSING_ERROR_CHECKSUM_ERROR; + return false; + } + else checksum_check_passed = true; + break; + } + case CERTIFICATE_PTAG_EXTRA_LOCATOR: + mLocators.insert(RsUrl(std::string((char *)buf, s))); + break; + default: + std::cerr << "(WW) unknwown PTAG 0x" << std::hex << ptag + << std::dec << " in certificate! Ignoring it." + << std::endl; + break; } + buf = &buf[s]; total_s += s ; } diff --git a/libretroshare/src/pgp/rscertificate.h b/libretroshare/src/pgp/rscertificate.h index a70a1752f..131652d0a 100644 --- a/libretroshare/src/pgp/rscertificate.h +++ b/libretroshare/src/pgp/rscertificate.h @@ -1,7 +1,28 @@ #pragma once +/* + * RetroShare + * + * Copyright (C) 2012-2014 Cyril Soler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "retroshare/rstypes.h" +#include "util/rsurl.h" + +#include #include -#include class RsPeerDetails ; @@ -42,6 +63,7 @@ class RsCertificate size_t pgp_key_size() const { return binary_pgp_key_size ; } static bool cleanCertificate(const std::string& input, std::string& output, RsCertificate::Format& format, int& error_code, bool check_content) ; + const std::set& locators() const { return mLocators; } private: static bool cleanCertificate(const std::string& input,std::string& output,int&) ; // new radix format @@ -69,6 +91,7 @@ class RsCertificate std::string pgp_version ; std::string dns_name ; std::string hidden_node_address; + std::set mLocators; bool only_pgp ; // does the cert contain only pgp info? bool hidden_node; // IP or hidden Node Address. diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index 1927b457e..09758c266 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -1363,16 +1363,63 @@ bool p3PeerMgrIMPL::UpdateOwnAddress( const sockaddr_storage& pLocalAddr, } +bool p3PeerMgrIMPL::addPeerLocator(const RsPeerId &sslId, const RsUrl& locator) +{ + std::string host(locator.host()); + pqiIpAddress ip; + if(!locator.hasPort() || host.empty() || + !sockaddr_storage_inet_pton(ip.mAddr, host) || + !sockaddr_storage_setport(ip.mAddr, locator.port())) return false; + ip.mSeenTime = time(NULL); + bool changed = false; -bool p3PeerMgrIMPL::setLocalAddress(const RsPeerId &id, const struct sockaddr_storage &addr) + if (sslId == AuthSSL::getAuthSSL()->OwnId()) + { + RS_STACK_MUTEX(mPeerMtx); + changed = mOwnState.ipAddrs.updateLocalAddrs(ip); + } + else + { + RS_STACK_MUTEX(mPeerMtx); + auto it = mFriendList.find(sslId); + if (it == mFriendList.end()) + { + it = mOthersList.find(sslId); + if (it == mOthersList.end()) + { +#ifdef PEER_DEBUG + std::cerr << __PRETTY_FUNCTION__ << "cannot add address " + << "info, peer id: " << sslId << " not found in list" + << std::endl; +#endif + return false; + } + } + + changed = it->second.ipAddrs.updateLocalAddrs(ip); + } + + if (changed) + { +#ifdef PEER_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " Added locator: " + << locator.toString() << std::endl; +#endif + IndicateConfigChanged(); + } + return changed; +} + +bool p3PeerMgrIMPL::setLocalAddress( const RsPeerId &id, + const sockaddr_storage &addr ) { bool changed = false; if (id == AuthSSL::getAuthSSL()->OwnId()) { { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); if (!sockaddr_storage_same(mOwnState.localaddr, addr)) { mOwnState.localaddr = addr; @@ -1390,7 +1437,7 @@ bool p3PeerMgrIMPL::setLocalAddress(const RsPeerId &id, const struct sockaddr return changed; } - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); /* check if it is a friend */ std::map::iterator it; if (mFriendList.end() == (it = mFriendList.find(id))) @@ -1398,7 +1445,9 @@ bool p3PeerMgrIMPL::setLocalAddress(const RsPeerId &id, const struct sockaddr if (mOthersList.end() == (it = mOthersList.find(id))) { #ifdef PEER_DEBUG - std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres info : peer id not found in friend list id: " << id << std::endl; + std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres " + << "info : peer id not found in friend list id: " + << id << std::endl; #endif return false; } @@ -1419,10 +1468,7 @@ bool p3PeerMgrIMPL::setLocalAddress(const RsPeerId &id, const struct sockaddr it->second.updateIpAddressList(ipAddressTimed); #endif - if (changed) { - IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/ - } - + if (changed) IndicateConfigChanged(); return changed; } diff --git a/libretroshare/src/pqi/p3peermgr.h b/libretroshare/src/pqi/p3peermgr.h index 8a0ac5235..390030893 100644 --- a/libretroshare/src/pqi/p3peermgr.h +++ b/libretroshare/src/pqi/p3peermgr.h @@ -163,6 +163,7 @@ virtual bool assignPeersToGroup(const RsNodeGroupId &groupId, const std::list * 3) p3disc - reasonable */ + virtual bool addPeerLocator(const RsPeerId &ssl_id, const RsUrl& locator) = 0; virtual bool setLocalAddress(const RsPeerId &id, const struct sockaddr_storage &addr) = 0; virtual bool setExtAddress(const RsPeerId &id, const struct sockaddr_storage &addr) = 0; virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns) = 0; @@ -277,6 +278,7 @@ public: * 3) p3disc - reasonable */ + virtual bool addPeerLocator(const RsPeerId &ssl_id, const RsUrl& locator); virtual bool setLocalAddress(const RsPeerId &id, const struct sockaddr_storage &addr); virtual bool setExtAddress(const RsPeerId &id, const struct sockaddr_storage &addr); virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns); diff --git a/libretroshare/src/retroshare/rspeers.h b/libretroshare/src/retroshare/rspeers.h index f1b274d09..89131e8bf 100644 --- a/libretroshare/src/retroshare/rspeers.h +++ b/libretroshare/src/retroshare/rspeers.h @@ -33,6 +33,7 @@ #include #include #include +#include "util/rsurl.h" /* The Main Interface Class - for information about your Peers * A peer is another RS instance, means associated with an SSL certificate @@ -370,6 +371,7 @@ public: virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port) = 0; virtual bool isHiddenNode(const RsPeerId &id) = 0; + virtual bool addPeerLocator(const RsPeerId &ssl_id, const RsUrl& locator) = 0; virtual bool setLocalAddress(const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0; virtual bool setExtAddress( const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0; virtual bool setDynDNS(const RsPeerId &id, const std::string &addr) = 0; diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 52f6c3dba..10fa0e9e9 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -904,6 +904,9 @@ bool p3Peers::setHiddenNode(const RsPeerId &id, const std::string &address, uin return true; } +bool p3Peers::addPeerLocator(const RsPeerId &ssl_id, const RsUrl& locator) +{ return mPeerMgr->addPeerLocator(ssl_id, locator); } + bool p3Peers::setLocalAddress(const RsPeerId &id, const std::string &addr_str, uint16_t port) { @@ -1148,11 +1151,12 @@ bool p3Peers::loadCertificateFromString(const std::string& cert, RsPeerId& ssl_ return res ; } -bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetails &pd,uint32_t& error_code) +bool p3Peers::loadDetailsFromStringCert( const std::string &certstr, + RsPeerDetails &pd, + uint32_t& error_code ) { #ifdef P3PEERS_DEBUG - std::cerr << "p3Peers::LoadCertificateFromString() "; - std::cerr << std::endl; + std::cerr << "p3Peers::LoadCertificateFromString() " << std::endl; #endif //parse the text to get ip address try @@ -1192,9 +1196,11 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai pd.localPort = cert.loc_port_us(); pd.extAddr = cert.ext_ip_string(); pd.extPort = cert.ext_port_us(); - pd.dyndns = cert.dns_string() ; + pd.dyndns = cert.dns_string(); + for(const RsUrl& locator : cert.locators()) + pd.ipAddressList.push_back(locator.toString()); } - } + } catch(uint32_t e) { std::cerr << "ConnectFriendWizard : Parse ip address error :" << e << std::endl; diff --git a/libretroshare/src/rsserver/p3peers.h b/libretroshare/src/rsserver/p3peers.h index 4f9b661d7..43d45f8a7 100644 --- a/libretroshare/src/rsserver/p3peers.h +++ b/libretroshare/src/rsserver/p3peers.h @@ -36,6 +36,7 @@ #endif #include "retroshare/rspeers.h" +#include "util/rsurl.h" class p3LinkMgr; class p3PeerMgr; @@ -96,7 +97,8 @@ public: virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port); virtual bool isHiddenNode(const RsPeerId &id); - virtual bool setLocalAddress(const RsPeerId &id, const std::string &addr, uint16_t port); + virtual bool addPeerLocator(const RsPeerId &ssl_id, const RsUrl& locator); + virtual bool setLocalAddress(const RsPeerId &id, const std::string &addr, uint16_t port); virtual bool setExtAddress(const RsPeerId &id, const std::string &addr, uint16_t port); virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns); virtual bool setNetworkMode(const RsPeerId &id, uint32_t netMode); diff --git a/libretroshare/src/util/rsnet_ss.cc b/libretroshare/src/util/rsnet_ss.cc index 65f2f3ee7..bc6d4bafb 100644 --- a/libretroshare/src/util/rsnet_ss.cc +++ b/libretroshare/src/util/rsnet_ss.cc @@ -192,8 +192,7 @@ bool sockaddr_storage_copyip(struct sockaddr_storage &dst, const struct sockaddr uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr) { #ifdef SS_DEBUG - std::cerr << "sockaddr_storage_port()"; - std::cerr << std::endl; + std::cerr << "sockaddr_storage_port()" << std::endl; #endif switch(addr.ss_family) { @@ -203,8 +202,10 @@ uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr) return sockaddr_storage_ipv6_port(addr); default: std::cerr << "sockaddr_storage_port() invalid addr.ss_family" << std::endl; +#ifdef SS_DEBUG sockaddr_storage_dump(addr); print_stacktrace(); +#endif break; } return 0; @@ -504,7 +505,7 @@ std::string sockaddr_storage_tostring(const struct sockaddr_storage &addr) url.setScheme("ipv6"); break; default: - return "INVALID_IP"; + return "AF_INVALID"; } url.setHost(sockaddr_storage_iptostring(addr)) @@ -613,9 +614,11 @@ std::string sockaddr_storage_iptostring(const struct sockaddr_storage &addr) break; default: output = "INVALID_IP"; - std::cerr << __PRETTY_FUNCTION__ << " Got invalid IP:" << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " Got invalid IP!" << std::endl; +#ifdef SS_DEBUG sockaddr_storage_dump(addr); print_stacktrace(); +#endif break; } return output; diff --git a/libretroshare/src/util/rsurl.cc b/libretroshare/src/util/rsurl.cc index 3f28a607d..efaab9777 100644 --- a/libretroshare/src/util/rsurl.cc +++ b/libretroshare/src/util/rsurl.cc @@ -34,10 +34,10 @@ RsUrl::RsUrl(const std::string& urlStr) : mPort(0), mHasPort(false) RsUrl& RsUrl::fromString(const std::string& urlStr) { - size_t urlSize = urlStr.size(); + size_t endI = urlStr.size()-1; size_t schemeEndI = urlStr.find(schemeSeparator); - if(schemeEndI == string::npos) + if(schemeEndI >= endI) { mScheme = urlStr; return *this; @@ -46,15 +46,16 @@ RsUrl& RsUrl::fromString(const std::string& urlStr) mScheme = urlStr.substr(0, schemeEndI); size_t hostBeginI = schemeEndI + 3; - if(hostBeginI >= urlSize) return *this; + if(hostBeginI >= endI) return *this; bool hasSquareBr = (urlStr[hostBeginI] == ipv6WrapOpen[0]); size_t hostEndI; if(hasSquareBr) { - if(++hostBeginI >= urlSize) return *this; + if(++hostBeginI >= endI) return *this; hostEndI = urlStr.find(ipv6WrapClose, hostBeginI); mHost = urlStr.substr(hostBeginI, hostEndI - hostBeginI - 1); + ++hostEndI; } else { @@ -64,24 +65,25 @@ RsUrl& RsUrl::fromString(const std::string& urlStr) hostEndI = min(hostEndI, urlStr.find(fragmentSeparator, hostBeginI)); mHost = urlStr.substr(hostBeginI, hostEndI - hostBeginI); - if(hostEndI == string::npos) return *this; } + if( hostEndI >= endI ) return *this; + mHasPort = (sscanf(&urlStr[hostEndI], ":%hu", &mPort) == 1); - size_t pathBeginI = urlStr.find(pathSeparator, hostEndI); + size_t pathBeginI = urlStr.find(pathSeparator, hostBeginI); size_t pathEndI = string::npos; - if(pathBeginI != string::npos) + if(pathBeginI < endI) { pathEndI = urlStr.find(querySeparator, pathBeginI); pathEndI = min(pathEndI, urlStr.find(fragmentSeparator, pathBeginI)); mPath = UrlDecode(urlStr.substr(pathBeginI, pathEndI - pathBeginI)); - if(pathEndI == string::npos) return *this; + if(pathEndI >= endI) return *this; } - size_t queryBeginI = urlStr.find(querySeparator, schemeEndI); - size_t queryEndI = urlStr.find(fragmentSeparator, schemeEndI); - if(queryBeginI != string::npos) + size_t queryBeginI = urlStr.find(querySeparator, hostBeginI); + size_t queryEndI = urlStr.find(fragmentSeparator, hostBeginI); + if(queryBeginI < endI) { string qStr = urlStr.substr(queryBeginI+1, queryEndI-queryBeginI-1); @@ -96,14 +98,16 @@ RsUrl& RsUrl::fromString(const std::string& urlStr) kPos = vEndPos+1; assPos = qStr.find(queryAssign, vEndPos); } - while(assPos != string::npos); + while(assPos < endI); - if(queryEndI == string::npos) return *this; + if(queryEndI >= endI) return *this; } - size_t fragmentBeginI = urlStr.find(fragmentSeparator, schemeEndI); - if(fragmentBeginI != string::npos) + size_t fragmentBeginI = urlStr.find(fragmentSeparator, hostBeginI); + if(fragmentBeginI < endI) mFragment = UrlDecode(urlStr.substr(++fragmentBeginI)); + + return *this; } std::string RsUrl::toString() const diff --git a/libretroshare/src/util/rsurl.h b/libretroshare/src/util/rsurl.h index 6125efba5..49215ffa4 100644 --- a/libretroshare/src/util/rsurl.h +++ b/libretroshare/src/util/rsurl.h @@ -65,17 +65,17 @@ struct RsUrl const std::string& ignoreChars = ""); static std::string UrlDecode(const std::string& str); - inline bool operator<(const RsUrl& rhs) + inline bool operator<(const RsUrl& rhs) const { return toString() < rhs.toString(); } - inline bool operator>(const RsUrl& rhs) + inline bool operator>(const RsUrl& rhs) const { return toString() > rhs.toString(); } - inline bool operator<=(const RsUrl& rhs) + inline bool operator<=(const RsUrl& rhs) const { return toString() <= rhs.toString(); } - inline bool operator>=(const RsUrl& rhs) + inline bool operator>=(const RsUrl& rhs) const { return toString() >= rhs.toString(); } - inline bool operator==(const RsUrl& rhs) + inline bool operator==(const RsUrl& rhs) const { return toString() == rhs.toString(); } - inline bool operator!=(const RsUrl& rhs) + inline bool operator!=(const RsUrl& rhs) const { return toString() != rhs.toString(); } static const std::string schemeSeparator; diff --git a/retroshare-gui/src/gui/common/FriendList.cpp b/retroshare-gui/src/gui/common/FriendList.cpp index 726bf61ea..fb62ff010 100644 --- a/retroshare-gui/src/gui/common/FriendList.cpp +++ b/retroshare-gui/src/gui/common/FriendList.cpp @@ -2067,6 +2067,10 @@ bool FriendList::importFriendlist(QString &fileName, bool &errorPeers, bool &err rsPeers->setDynDNS(rsPeerDetails.id, rsPeerDetails.dyndns); if (!rsPeerDetails.location.empty()) rsPeers->setLocation(rsPeerDetails.id, rsPeerDetails.location); + for(auto&& ipr : rsPeerDetails.ipAddressList) + rsPeers->addPeerLocator( + rsPeerDetails.id, + RsUrl(ipr.substr(0, ipr.find(' '))) ); } } else if (!rsPeerDetails.gpg_id.isNull()) { // only pgp id is avaiable diff --git a/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp b/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp index 15b2f504f..2d994917c 100755 --- a/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp +++ b/retroshare-gui/src/gui/connect/ConnectFriendWizard.cpp @@ -974,6 +974,10 @@ void ConnectFriendWizard::accept() std::cerr << "ConnectFriendWizard::accept() : setting DynDNS." << std::endl; rsPeers->setDynDNS(peerDetails.id, peerDetails.dyndns); } + for(auto&& ipr : peerDetails.ipAddressList) + rsPeers->addPeerLocator( + peerDetails.id, + RsUrl(ipr.substr(0, ipr.find(' '))) ); } }