From 923c383a1312735a9e73580acf4242ec2ba140fc Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 2 Mar 2018 19:20:56 +0100 Subject: [PATCH 1/5] Add naif URL manipulation class RsUrl Use RsUrl to convert sockaddre_storage from/to string --- libretroshare/src/libretroshare.pro | 6 +- libretroshare/src/util/rsnet.h | 1 + libretroshare/src/util/rsnet_ss.cc | 31 ++-- libretroshare/src/util/rsurl.cc | 258 ++++++++++++++++++++++++++++ libretroshare/src/util/rsurl.h | 101 +++++++++++ 5 files changed, 383 insertions(+), 14 deletions(-) create mode 100644 libretroshare/src/util/rsurl.cc create mode 100644 libretroshare/src/util/rsurl.h diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index c2604fa4a..5ec795d3d 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -550,7 +550,8 @@ HEADERS += util/folderiterator.h \ util/rstime.h \ util/stacktrace.h \ util/rsdeprecate.h \ - util/cxx11retrocompat.h + util/cxx11retrocompat.h \ + util/rsurl.h SOURCES += ft/ftchunkmap.cc \ ft/ftcontroller.cc \ @@ -695,7 +696,8 @@ SOURCES += util/folderiterator.cc \ util/rsrandom.cc \ util/rstickevent.cc \ util/rsrecogn.cc \ - util/rstime.cc + util/rstime.cc \ + util/rsurl.cc upnp_miniupnpc { diff --git a/libretroshare/src/util/rsnet.h b/libretroshare/src/util/rsnet.h index 1d028e991..95887e61a 100644 --- a/libretroshare/src/util/rsnet.h +++ b/libretroshare/src/util/rsnet.h @@ -133,6 +133,7 @@ bool sockaddr_storage_sameip(const struct sockaddr_storage &addr, const struct s // string, std::string sockaddr_storage_tostring(const struct sockaddr_storage &addr); +bool sockaddr_storage_fromString(const std::string& str, 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); diff --git a/libretroshare/src/util/rsnet_ss.cc b/libretroshare/src/util/rsnet_ss.cc index 581fd06e9..65f2f3ee7 100644 --- a/libretroshare/src/util/rsnet_ss.cc +++ b/libretroshare/src/util/rsnet_ss.cc @@ -24,6 +24,8 @@ * */ +#include "util/rsurl.h" + #include #include #include @@ -491,27 +493,32 @@ bool sockaddr_storage_sameip(const struct sockaddr_storage &addr, const struct s std::string sockaddr_storage_tostring(const struct sockaddr_storage &addr) { - std::string output; - output += sockaddr_storage_familytostring(addr); + RsUrl url; switch(addr.ss_family) { case AF_INET: - output += "="; - output += sockaddr_storage_iptostring(addr); - output += ":"; - output += sockaddr_storage_porttostring(addr); + url.setScheme("ipv4"); break; case AF_INET6: - output += "=["; - output += sockaddr_storage_iptostring(addr); - output += "]:"; - output += sockaddr_storage_porttostring(addr); + url.setScheme("ipv6"); break; default: - break; + return "INVALID_IP"; } - return output; + + url.setHost(sockaddr_storage_iptostring(addr)) + .setPort(sockaddr_storage_port(addr)); + + return url.toString(); +} + +bool sockaddr_storage_fromString(const std::string& str, sockaddr_storage &addr) +{ + RsUrl url(str); + bool valid = sockaddr_storage_inet_pton(addr, url.host()); + if(url.hasPort()) sockaddr_storage_setport(addr, url.port()); + return valid; } void sockaddr_storage_dump(const sockaddr_storage & addr, std::string * outputString) diff --git a/libretroshare/src/util/rsurl.cc b/libretroshare/src/util/rsurl.cc new file mode 100644 index 000000000..31922abbf --- /dev/null +++ b/libretroshare/src/util/rsurl.cc @@ -0,0 +1,258 @@ +/* + * RetroShare + * 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 "util/rsurl.h" +#include "rsurl.h" + +#include +#include +#include +#include +#include + +using namespace std; + +RsUrl::RsUrl() : mPort(0), mHasPort(false) {} + +RsUrl::RsUrl(const std::string& urlStr) : mPort(0), mHasPort(false) +{ fromString(urlStr); } + +RsUrl& RsUrl::fromString(const std::string& urlStr) +{ + size_t urlSize = urlStr.size(); + + size_t schemeEndI = urlStr.find(schemeSeparator); + if(schemeEndI == string::npos) + { + mScheme = urlStr; + return *this; + } + + mScheme = urlStr.substr(0, schemeEndI); + + size_t hostBeginI = schemeEndI + 3; + if(hostBeginI >= urlSize) return *this; + + bool hasSquareBr = (urlStr[hostBeginI] == ipv6WrapOpen[0]); + size_t hostEndI; + if(hasSquareBr) + { + if(++hostBeginI >= urlSize) return *this; + hostEndI = urlStr.find(ipv6WrapClose, hostBeginI); + mHost = urlStr.substr(hostBeginI, hostEndI - hostBeginI - 1); + } + else + { + hostEndI = urlStr.find(pathSeparator, hostBeginI); + hostEndI = min(hostEndI, urlStr.find(portSeparator, hostBeginI)); + hostEndI = min(hostEndI, urlStr.find(querySeparator, hostBeginI)); + hostEndI = min(hostEndI, urlStr.find(fragmentSeparator, hostBeginI)); + + mHost = urlStr.substr(hostBeginI, hostEndI - hostBeginI); + if(hostEndI == string::npos) return *this; + } + + mHasPort = (sscanf(&urlStr[hostEndI], ":%hu", &mPort) == 1); + + size_t pathBeginI = urlStr.find(pathSeparator, hostEndI); + size_t pathEndI = string::npos; + if(pathBeginI != string::npos) + { + 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; + } + + size_t queryBeginI = urlStr.find(querySeparator, schemeEndI); + size_t queryEndI = urlStr.find(fragmentSeparator, schemeEndI); + if(queryBeginI != string::npos) + { + string qStr = urlStr.substr(queryBeginI+1, queryEndI-queryBeginI-1); + + size_t kPos = 0; + size_t assPos = qStr.find(queryAssign); + do + { + size_t vEndPos = qStr.find(queryFieldSep, assPos); + mQuery.insert( std::make_pair( qStr.substr(kPos, assPos-kPos), + UrlDecode(qStr.substr(assPos+1, vEndPos-assPos-1)) + ) ); + kPos = vEndPos+1; + assPos = qStr.find(queryAssign, vEndPos); + } + while(assPos != string::npos); + + if(queryEndI == string::npos) return *this; + } + + size_t fragmentBeginI = urlStr.find(fragmentSeparator, schemeEndI); + if(fragmentBeginI != string::npos) + mFragment = UrlDecode(urlStr.substr(++fragmentBeginI)); +} + +std::string RsUrl::toString() const +{ + std::string urlStr(mScheme); + urlStr += schemeSeparator; + + if(!mHost.empty()) + { + if(mHost.find(ipv6Separator) != string::npos && + mHost[0] != ipv6WrapOpen[0] ) + urlStr += ipv6WrapOpen + mHost + ipv6WrapClose; + else urlStr += mHost; + } + + if(mHasPort) urlStr += portSeparator + std::to_string(mPort); + + urlStr += UrlEncode(mPath, pathSeparator); + + bool hasQuery = !mQuery.empty(); + if(hasQuery) urlStr += querySeparator; + for(auto&& kv : mQuery) + { + urlStr += kv.first; + urlStr += queryAssign; + urlStr += UrlEncode(kv.second); + urlStr += queryFieldSep; + } + if(hasQuery) urlStr.pop_back(); + + if(!mFragment.empty()) urlStr += fragmentSeparator + UrlEncode(mFragment); + + return urlStr; +} + +const std::string& RsUrl::scheme() const { return mScheme; } +RsUrl& RsUrl::setScheme(const std::string& scheme) +{ + mScheme = scheme; + return *this; +} + +const std::string& RsUrl::host() const { return mHost; } +RsUrl& RsUrl::setHost(const std::string& host) +{ + mHost = host; + return *this; +} + +bool RsUrl::hasPort() const { return mHasPort; } + +uint16_t RsUrl::port(uint16_t def) const +{ + if(mHasPort) return mPort; + return def; +} + +RsUrl& RsUrl::setPort(uint16_t port) +{ + mPort = port; + return *this; +} + +const std::string& RsUrl::path() const { return mPath; } +RsUrl& RsUrl::setPath(const std::string& path) +{ + mPath = path; + return *this; +} + +const std::map& RsUrl::query() const +{ return mQuery; } +RsUrl& RsUrl::setQuery(const std::map& query) +{ + mQuery = query; + return *this; +} +RsUrl& RsUrl::setQueryKV(const std::string& key, const std::string& value) +{ + mQuery.insert(std::make_pair(key, value)); + return *this; +} +RsUrl& RsUrl::delQueryK(const std::string& key) +{ + mQuery.erase(key); + return *this; +} + +const std::string& RsUrl::fragment() const { return mFragment; } +RsUrl& RsUrl::setFragment(const std::string& fragment) +{ + mFragment = fragment; + return *this; +} + +/*static*/ std::string RsUrl::UrlEncode( const std::string& str, + const std::string& ignore ) +{ + ostringstream escaped; + escaped.fill('0'); + escaped << hex; + + for (string::value_type c : str) + { + // Keep alphanumeric and other accepted characters intact + if ( isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' + || ignore.find(c) != string::npos ) + { + escaped << c; + continue; + } + + // Any other characters are percent-encoded + escaped << uppercase; + escaped << '%' << setw(2) << int((unsigned char) c); + escaped << nouppercase; + } + + return escaped.str(); +} + +/*static*/ std::string RsUrl::UrlDecode(const std::string& str) +{ + ostringstream decoded; + + size_t len = str.size(); + size_t boundary = len-2; // % Encoded char must be at least 2 hex char + for (size_t i = 0; i < len; ++i) + { + + if(str[i] == '%' && i < boundary) + { + decoded << static_cast(stoi(str.substr(++i, 2), 0, 16)); + ++i; + } + else decoded << str[i]; + } + + return decoded.str(); +} + +/*static*/ const std::string RsUrl::schemeSeparator("://"); +/*static*/ const std::string RsUrl::ipv6WrapOpen("["); +/*static*/ const std::string RsUrl::ipv6Separator(":"); +/*static*/ const std::string RsUrl::ipv6WrapClose("]"); +/*static*/ const std::string RsUrl::portSeparator(":"); +/*static*/ const std::string RsUrl::pathSeparator("/"); +/*static*/ const std::string RsUrl::querySeparator("?"); +/*static*/ const std::string RsUrl::queryAssign("="); +/*static*/ const std::string RsUrl::queryFieldSep("&"); +/*static*/ const std::string RsUrl::fragmentSeparator("#"); + diff --git a/libretroshare/src/util/rsurl.h b/libretroshare/src/util/rsurl.h new file mode 100644 index 000000000..fc9d2992b --- /dev/null +++ b/libretroshare/src/util/rsurl.h @@ -0,0 +1,101 @@ +#pragma once +/* + * RetroShare + * 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 + +/** + * Very simplistic and minimal URL helper class for RetroShare, after looking + * for a small and self-contained C/C++ URL parsing and manipulation library, + * haven't found nothing satisfactory except for implementation like QUrl that + * rely on bigger library. + * ATM this implementation is not standard compliant and doesn't aim to be. + * Improvements to this are welcome. + * + * Anyway this should support most common URLs of the form + * scheme://host[:port][/path][?query][#fragment] + */ +struct RsUrl +{ + RsUrl(); + RsUrl(const std::string& urlStr); + + RsUrl& fromString(const std::string& urlStr); + std::string toString() const; + + const std::string& scheme() const; + RsUrl& setScheme(const std::string& scheme); + + const std::string& host() const; + RsUrl& setHost(const std::string& host); + + bool hasPort() const; + uint16_t port(uint16_t def = 0) const; + RsUrl& setPort(uint16_t port); + + const std::string& path() const; + RsUrl& setPath(const std::string& path); + + const std::map& query() const; + RsUrl& setQuery(const std::map& query); + RsUrl& setQueryKV(const std::string& key, const std::string& value); + RsUrl& delQueryK(const std::string& key); + + const std::string& fragment() const; + RsUrl& setFragment(const std::string& fragment); + + static std::string UrlEncode(const std::string& str, + const std::string& ignoreChars = ""); + static std::string UrlDecode(const std::string& str); + + inline bool operator<(const RsUrl& rhs) + { return toString() < rhs.toString(); } + inline bool operator>(const RsUrl& rhs) + { return toString() > rhs.toString(); } + inline bool operator<=(const RsUrl& rhs) + { return toString() <= rhs.toString(); } + inline bool operator>=(const RsUrl& rhs) + { return toString() >= rhs.toString(); } + inline bool operator==(const RsUrl& rhs) + { return toString() == rhs.toString(); } + inline bool operator!=(const RsUrl& rhs) + { return toString() != rhs.toString(); } + + static const std::string schemeSeparator; + static const std::string ipv6WrapOpen; + static const std::string ipv6Separator; + static const std::string ipv6WrapClose; + static const std::string portSeparator; + static const std::string pathSeparator; + static const std::string querySeparator; + static const std::string queryAssign; + static const std::string queryFieldSep; + static const std::string fragmentSeparator; + +private: + + std::string mScheme; + std::string mHost; + uint16_t mPort; + bool mHasPort; + std::string mPath; + std::map mQuery; + std::string mFragment; +}; + From 8542abd4f09e0f63bee6a06ef0b4ad64f6e45b80 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 2 Mar 2018 20:08:50 +0100 Subject: [PATCH 2/5] Few fixes in RsUrl --- libretroshare/src/util/rsurl.cc | 9 +++++++-- libretroshare/src/util/rsurl.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/util/rsurl.cc b/libretroshare/src/util/rsurl.cc index 31922abbf..3f28a607d 100644 --- a/libretroshare/src/util/rsurl.cc +++ b/libretroshare/src/util/rsurl.cc @@ -154,16 +154,21 @@ RsUrl& RsUrl::setHost(const std::string& host) } bool RsUrl::hasPort() const { return mHasPort; } - uint16_t RsUrl::port(uint16_t def) const { if(mHasPort) return mPort; return def; } - RsUrl& RsUrl::setPort(uint16_t port) { mPort = port; + mHasPort = true; + return *this; +} +RsUrl& RsUrl::unsetPort() +{ + mPort = 0; + mHasPort = false; return *this; } diff --git a/libretroshare/src/util/rsurl.h b/libretroshare/src/util/rsurl.h index fc9d2992b..6125efba5 100644 --- a/libretroshare/src/util/rsurl.h +++ b/libretroshare/src/util/rsurl.h @@ -48,6 +48,7 @@ struct RsUrl bool hasPort() const; uint16_t port(uint16_t def = 0) const; RsUrl& setPort(uint16_t port); + RsUrl& unsetPort(); const std::string& path() const; RsUrl& setPath(const std::string& path); From bed856425fbfe83e0c3dc3e372d73dc3b32d6031 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 3 Mar 2018 00:08:56 +0100 Subject: [PATCH 3/5] 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(' '))) ); } } From 9fbf56e592333d7cb364347ed0ae723ede09ace3 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 3 Mar 2018 01:55:49 +0100 Subject: [PATCH 4/5] Remove outdated misleading comment --- libretroshare/src/pqi/p3netmgr.cc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 7ca1713f7..1daf78e45 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -1119,12 +1119,7 @@ bool p3NetMgrIMPL::checkNetAddress() #ifdef NETMGR_DEBUG std::cerr << "p3NetMgrIMPL::checkNetAddress() Correcting Port to DEFAULT" << std::endl; #endif - // Generate a default port from SSL id. The port will always be the - // same, but appear random from peer to peer. - // Random port avoids clashes, improves anonymity. - // - - int new_port = htons(PQI_MIN_PORT_RNG + (RSRandom::random_u32() % (PQI_MAX_PORT - PQI_MIN_PORT_RNG))); + uint16_t new_port = htons(PQI_MIN_PORT_RNG + (RSRandom::random_u32() % (PQI_MAX_PORT - PQI_MIN_PORT_RNG))); sockaddr_storage_setport(mLocalAddr, new_port); addrChanged = true; From 418c42bd1108b79cf9ad297d3d096858a7f47733 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 2 Jul 2018 13:50:02 +0200 Subject: [PATCH 5/5] Extra locators in cert invite made optional --- libretroshare/src/retroshare/rspeers.h | 23 +++++++++++++-- libretroshare/src/rsserver/p3peers.cc | 41 +++++++++++++++----------- libretroshare/src/rsserver/p3peers.h | 10 +++++-- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/libretroshare/src/retroshare/rspeers.h b/libretroshare/src/retroshare/rspeers.h index 89131e8bf..7824daa11 100644 --- a/libretroshare/src/retroshare/rspeers.h +++ b/libretroshare/src/retroshare/rspeers.h @@ -386,11 +386,30 @@ public: virtual bool resetOwnExternalAddressList() = 0; virtual bool getAllowServerIPDetermination() = 0 ; + /** + * @brief Get RetroShare invite of the given peer + * @param[in] sslId Id of the peer of which we want to generate an invite + * @param[in] includeSignatures true to add key signatures to the invite + * @param[in] includeExtraLocators false to avoid to add extra locators + * @return invite string + */ + virtual std::string GetRetroshareInvite( + const RsPeerId& sslId, bool includeSignatures = false, + bool includeExtraLocators = true ) = 0; + + /** + * @brief Get RetroShare invite of our own peer + * @param[in] includeSignatures true to add key signatures to the invite + * @param[in] includeExtraLocators false to avoid to add extra locators + * @return invite string + */ + virtual std::string GetRetroshareInvite( + bool includeSignatures = false, + bool includeExtraLocators = true ) = 0; + /* Auth Stuff */ - virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures) = 0; virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) = 0; virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) = 0; - virtual std::string GetRetroshareInvite(bool include_signatures) = 0; virtual bool hasExportMinimal() = 0; // Add keys to the keyring diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 60d724179..36509c33b 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -1048,9 +1048,11 @@ bool p3Peers::setProxyServer(const uint32_t type, const std::string &addr_str, c //=========================================================================== /* Auth Stuff */ -std::string p3Peers::GetRetroshareInvite(bool include_signatures) +std::string p3Peers::GetRetroshareInvite( + bool include_signatures, bool includeExtraLocators ) { - return GetRetroshareInvite(getOwnId(),include_signatures); + return GetRetroshareInvite( + getOwnId(), include_signatures, includeExtraLocators ); } std::string p3Peers::getPGPKey(const RsPgpId& pgp_id,bool include_signatures) { @@ -1101,37 +1103,42 @@ bool p3Peers::GetPGPBase64StringAndCheckSum( const RsPgpId& gpg_id, return true ; } -std::string p3Peers::GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures) +std::string p3Peers::GetRetroshareInvite( + const RsPeerId& ssl_id, bool include_signatures, + bool includeExtraLocators ) { #ifdef P3PEERS_DEBUG - std::cerr << "p3Peers::GetRetroshareInvite()" << std::endl; + std::cerr << __PRETTY_FUNCTION__ << std::endl; #endif //add the sslid, location, ip local and external address after the signature - RsPeerDetails Detail; - std::string invite ; + RsPeerDetails detail; + std::string invite; - if (getPeerDetails(ssl_id, Detail)) + if (getPeerDetails(ssl_id, detail)) { - unsigned char *mem_block = NULL; + if(!includeExtraLocators) detail.ipAddressList.clear(); + + unsigned char *mem_block = nullptr; size_t mem_block_size = 0; - if(!AuthGPG::getAuthGPG()->exportPublicKey(RsPgpId(Detail.gpg_id),mem_block,mem_block_size,false,include_signatures)) + if(!AuthGPG::getAuthGPG()->exportPublicKey( + RsPgpId(detail.gpg_id), mem_block, mem_block_size, false, + include_signatures )) { - std::cerr << "Cannot output certificate for id \"" << Detail.gpg_id << "\". Sorry." << std::endl; - return "" ; + std::cerr << "Cannot output certificate for id \"" << detail.gpg_id + << "\". Sorry." << std::endl; + return ""; } - RsCertificate cert( Detail,mem_block,mem_block_size ) ; - - delete[] mem_block ; - - return cert.toStdString() ; + RsCertificate cert(detail, mem_block, mem_block_size); + delete[] mem_block; + return cert.toStdString(); } #ifdef P3PEERS_DEBUG - std::cerr << "p3Peers::GetRetroshareInvite() returns : \n" << invite << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " returns : \n" << invite << std::endl; #endif return invite; } diff --git a/libretroshare/src/rsserver/p3peers.h b/libretroshare/src/rsserver/p3peers.h index 43d45f8a7..d2333c420 100644 --- a/libretroshare/src/rsserver/p3peers.h +++ b/libretroshare/src/rsserver/p3peers.h @@ -115,11 +115,15 @@ public: /* Auth Stuff */ // Get the invitation (GPG cert + local/ext address + SSL id for the given peer) - virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures); - virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) ; + virtual std::string GetRetroshareInvite( + const RsPeerId& ssl_id, bool include_signatures = false, + bool includeExtraLocators = true ); + virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures); // same but for own id - virtual std::string GetRetroshareInvite(bool include_signatures); + virtual std::string GetRetroshareInvite( + bool include_signatures = false, + bool includeExtraLocators = true ); virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum); virtual bool hasExportMinimal();