merged with upstream/master

This commit is contained in:
csoler 2019-11-24 18:38:50 +01:00
commit 4a257614f1
No known key found for this signature in database
GPG key ID: 7BCA522266C0804C
32 changed files with 482 additions and 365 deletions

View file

@ -509,6 +509,7 @@ HEADERS += util/folderiterator.h \
util/stacktrace.h \
util/rsdeprecate.h \
util/cxx11retrocompat.h \
util/cxx17retrocompat.h \
util/rsurl.h
SOURCES += ft/ftchunkmap.cc \

View file

@ -1170,8 +1170,8 @@ bool p3PeerMgrIMPL::addSslOnlyFriend(
* previously known IP addresses */
if(!dt.isHiddenNode)
{
for(const std::string& locator : dt.ipAddressList)
addPeerLocator(sslId, locator);
for(const std::string& ipStr : dt.ipAddressList)
addPeerLocator(sslId, RsUrl(ipStr));
if(dt.extPort && !dt.extAddr.empty())
{

View file

@ -3,7 +3,8 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2012-2019 by Retroshare Team <contact@retroshare.cc> *
* Copyright (C) 2012-2019 by Retroshare Team <contact@retroshare.cc> *
* Copyright (C) 2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -21,18 +22,129 @@
*******************************************************************************/
#pragma once
#include <type_traits>
#include <ostream>
/** Check if given type is a scoped enum */
template<typename E>
using rs_is_scoped_enum = std::integral_constant< bool,
std::is_enum<E>::value && !std::is_convertible<E, int>::value >;
/**
* @brief Register enum class as flags type
* To use this macro define a scoped enum with your flag values, then register
* it as flags type passing it as parameter of this macro.
* The result will be type safe flags, that cannot be mixed up with flag of a
* different type, but that are very comfortable to operate like plain old
* integers.
* This macro support flag fields of different lenght depending on what
* underlining type (usually from uint8_t up to uint64_t) has been declared for
* the enum class.
* If you plan to serialize those flags it is important to specify the
* underlining type of the enum otherwise different compilers may serialize a
* flag variable with different lenght, potentially causing interoperability
* issues between differents builds.
* Usage example:
@code{.cpp}
enum class RsGrouterItemFlags : uint32_t
{
NONE = 0x0,
ENCRYPTED = 0x1,
SERVICE_UNKNOWN = 0x2
};
RS_REGISTER_ENUM_FLAGS_TYPE(RsGrouterItemFlags)
@endcode
*/
#define RS_REGISTER_ENUM_FLAGS_TYPE(eft) \
template<> struct Rs__BitFlagsOps<eft> \
{ \
static_assert( std::is_enum<eft>::value, \
"Are you trying to register a non-enum type as flags?" ); \
static_assert( rs_is_scoped_enum<eft>::value, \
"Are you trying to register an unscoped enum as flags?" ); \
static constexpr bool enabled = true; \
};
// By defaults types are not valid flags, so bit flags operators are disabled
template<typename> struct Rs__BitFlagsOps
{ static constexpr bool enabled = false; };
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
/*EFT*/ operator &(EFT lhs, EFT rhs)
{
using u_t = typename std::underlying_type<EFT>::type;
return static_cast<EFT>(static_cast<u_t>(lhs) & static_cast<u_t>(rhs));
}
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
/*EFT*/ operator &=(EFT& lhs, EFT rhs) { lhs = lhs & rhs; return lhs; }
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
/*EFT*/ operator |(EFT lhs, EFT rhs)
{
using u_t = typename std::underlying_type<EFT>::type;
return static_cast<EFT>(static_cast<u_t>(lhs) | static_cast<u_t>(rhs));
}
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
/*EFT*/ operator |=(EFT& lhs, EFT rhs) { lhs = lhs | rhs; return lhs; }
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
/*EFT*/ operator ^(EFT lhs, EFT rhs)
{
using u_t = typename std::underlying_type<EFT>::type;
return static_cast<EFT>(static_cast<u_t>(lhs) ^ static_cast<u_t>(rhs));
}
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
/*EFT*/ operator ^=(EFT& lhs, EFT rhs) { lhs = lhs ^ rhs; return lhs; }
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, EFT>::type
operator ~(EFT val)
{
using u_t = typename std::underlying_type<EFT>::type;
return static_cast<EFT>(~static_cast<u_t>(val));
}
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, bool>::type
operator !(EFT val)
{
using u_t = typename std::underlying_type<EFT>::type;
return static_cast<u_t>(val) == 0;
}
/// Nicely print flags bits as 1 and 0
template<typename EFT>
typename std::enable_if<Rs__BitFlagsOps<EFT>::enabled, std::ostream>::type&
operator <<(std::ostream& stream, EFT flags)
{
using u_t = typename std::underlying_type<EFT>::type;
for(int i = sizeof(u_t); i>=0; --i)
{
stream << (flags & ( 1 << i ) ? "1" : "0");
if( i % 8 == 0 ) stream << " ";
}
return stream;
}
#include <cstdint>
#include "util/rsdeprecate.h"
/* G10h4ck: TODO we should redefine flags in a way that the flag declaration and
* the flags values (bit fields) would be strongly logically linked.
* A possible way is to take an enum class containing the names of each
* bitfield and corresponding value as template parameter, this way would also
* avoid the need of dumb template parameter that is used only to make the
* types incompatible but that doesn't help finding what are the possible values
* for a kind of flag. Another appealing approach seems the first one described
* here https://softwareengineering.stackexchange.com/questions/194412/using-scoped-enums-for-bit-flags-in-c
* a few simple macros could be used instead of the template class */
/**
* @deprecated t_RsFlags32 has been deprecated because the newer
* @see RS_REGISTER_ENUM_FLAGS_TYPE provide more convenient flags facilities.
*
// This class provides a representation for flags that can be combined with bitwise
// operations. However, because the class is templated with an id, it's not possible to
// mixup flags belonging to different classes. This avoids many bugs due to confusion of flags types
@ -51,7 +163,8 @@
// - an explicit constructor from uint32_t
// - an implicit bool operator, that allows test like if(flags & FLAGS_VALUE)
//
template<int n> class t_RsFlags32
*/
template<int n> class RS_DEPRECATED_FOR(RS_REGISTER_ENUM_FLAGS_TYPE) t_RsFlags32
{
public:
inline t_RsFlags32() : _bits(0) {}

View file

@ -0,0 +1,35 @@
/*******************************************************************************
* libretroshare/src/util: cxx17retrocompat.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#pragma once
#if __cplusplus < 201703L
#include <type_traits>
namespace std
{
using namespace std;
template <class T> constexpr typename add_const<T>::type& as_const(T& t) noexcept
{ return t; }
template <class T> void as_const(const T&&) = delete;
}
#endif // __cplusplus < 201703L

View file

@ -62,6 +62,35 @@ bool myFunnyFunction(
*/
#define RS_DEFAULT_STORAGE_PARAM(Type,...) *std::unique_ptr<Type>(new Type(__VA_ARGS__))
/** @brief Safely dynamic cast between std::unique_ptr of different types
* std::unique_ptr semantic rely on the invariant that only one instance own
* the object, when casting between differents types one would be tempted to do
* it in a one liner that easly end up breaking that condition ending up in a
* double delete and crash or in a silent memleak.
* With this function one can do that with same comfort of a plain dynamic_cast,
* plus the std::unique_ptr safety.
* @param[inout] src reference to source pointer. If the cast is successfull it
* is released, otherwise it is left untouched.
* @param[out] dst reference to destination pointer. If the cast is successful
* it get reseated to the object address, otherwise it is left untouched.
* @return true on success, false otherwise
*/
template <class T_SRC, class T_DST>
bool rs_unique_cast(
std::unique_ptr<T_SRC>& src, std::unique_ptr<T_DST>& dst )
{
T_DST* dstPtr = dynamic_cast<T_DST*>(src.get());
if(dstPtr)
{
src.release();
dst.reset(dstPtr);
return true;
}
return false;
}
void *rs_malloc(size_t size) ;
// This is a scope guard to release the memory block when going of of the current scope.

View file

@ -1,6 +1,6 @@
/*
* RetroShare
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@ -17,15 +17,16 @@
*/
#include "rsurl.h"
#include "serialiser/rstypeserializer.h"
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "rsurl.h"
#include "serialiser/rstypeserializer.h"
#include "util/rsnet.h"
using namespace std;
RsUrl::RsUrl() : mPort(0), mHasPort(false) {}
@ -33,6 +34,26 @@ RsUrl::RsUrl() : mPort(0), mHasPort(false) {}
RsUrl::RsUrl(const std::string& urlStr) : mPort(0), mHasPort(false)
{ fromString(urlStr); }
RsUrl::RsUrl(const sockaddr_storage& addr): mPort(0), mHasPort(false)
{
switch(addr.ss_family)
{
case AF_INET: setScheme("ipv4"); break;
case AF_INET6: setScheme("ipv6"); break;
default:
{
std::string addrDump;
sockaddr_storage_dump(addr, &addrDump);
RsErr() << __PRETTY_FUNCTION__ << " got invalid addr: " << addrDump
<< std::endl;
return;
}
}
setHost(sockaddr_storage_iptostring(addr));
setPort(sockaddr_storage_port(addr));
}
RsUrl& RsUrl::fromString(const std::string& urlStr)
{
size_t endI = urlStr.size()-1;
@ -55,7 +76,7 @@ RsUrl& RsUrl::fromString(const std::string& urlStr)
{
if(++hostBeginI >= endI) return *this;
hostEndI = urlStr.find(ipv6WrapClose, hostBeginI);
mHost = urlStr.substr(hostBeginI, hostEndI - hostBeginI - 1);
mHost = urlStr.substr(hostBeginI, hostEndI - hostBeginI);
++hostEndI;
}
else
@ -250,10 +271,10 @@ RsUrl& RsUrl::setFragment(const std::string& fragment)
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<char>(std::stoi(str.substr(++i, 2), 0, 16));
decoded << static_cast<char>(std::stoi(
str.substr(++i, 2), nullptr, 16 ));
++i;
}
else decoded << str[i];

View file

@ -22,6 +22,8 @@
#include "serialiser/rsserializable.h"
struct sockaddr_storage;
/**
* Very simplistic and minimal URL helper class for RetroShare, after looking
* for a small and self-contained C/C++ URL parsing and manipulation library,
@ -36,7 +38,8 @@
struct RsUrl : RsSerializable
{
RsUrl();
RsUrl(const std::string& urlStr);
explicit RsUrl(const std::string& urlStr);
explicit RsUrl(const sockaddr_storage& ssas);
RsUrl& fromString(const std::string& urlStr);
std::string toString() const;