diff options
Diffstat (limited to 'contrib/epee')
-rw-r--r-- | contrib/epee/include/hex.h | 1 | ||||
-rw-r--r-- | contrib/epee/include/misc_os_dependent.h | 2 | ||||
-rw-r--r-- | contrib/epee/include/net/abstract_tcp_server2.inl | 24 | ||||
-rw-r--r-- | contrib/epee/include/span.h | 46 | ||||
-rw-r--r-- | contrib/epee/include/wipeable_string.h | 19 | ||||
-rw-r--r-- | contrib/epee/src/wipeable_string.cpp | 3 |
6 files changed, 82 insertions, 13 deletions
diff --git a/contrib/epee/include/hex.h b/contrib/epee/include/hex.h index 02600c320..901c666a9 100644 --- a/contrib/epee/include/hex.h +++ b/contrib/epee/include/hex.h @@ -44,6 +44,7 @@ namespace epee static std::string string(const span<const std::uint8_t> src); //! \return A epee::wipeable_string containing hex of `src`. static epee::wipeable_string wipeable_string(const span<const std::uint8_t> src); + template<typename T> static epee::wipeable_string wipeable_string(const T &pod) { return wipeable_string(span<const uint8_t>((const uint8_t*)&pod, sizeof(pod))); } //! \return An array containing hex of `src`. template<std::size_t N> diff --git a/contrib/epee/include/misc_os_dependent.h b/contrib/epee/include/misc_os_dependent.h index ffe575501..0d09683d6 100644 --- a/contrib/epee/include/misc_os_dependent.h +++ b/contrib/epee/include/misc_os_dependent.h @@ -24,7 +24,7 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // #ifdef _WIN32 -#include <Winsock2.h> +#include <winsock2.h> #endif #ifdef WIN32 diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index 59a126163..3a5c83017 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -1109,8 +1109,16 @@ POP_WARNINGS sock_.open(remote_endpoint.protocol()); if(bind_ip != "0.0.0.0" && bind_ip != "0" && bind_ip != "" ) { - boost::asio::ip::tcp::endpoint local_endpoint(boost::asio::ip::address::from_string(adr.c_str()), 0); - sock_.bind(local_endpoint); + boost::asio::ip::tcp::endpoint local_endpoint(boost::asio::ip::address::from_string(bind_ip.c_str()), 0); + boost::system::error_code ec; + sock_.bind(local_endpoint, ec); + if (ec) + { + MERROR("Error binding to " << bind_ip << ": " << ec.message()); + if (sock_.is_open()) + sock_.close(); + return false; + } } /* @@ -1215,8 +1223,16 @@ POP_WARNINGS sock_.open(remote_endpoint.protocol()); if(bind_ip != "0.0.0.0" && bind_ip != "0" && bind_ip != "" ) { - boost::asio::ip::tcp::endpoint local_endpoint(boost::asio::ip::address::from_string(adr.c_str()), 0); - sock_.bind(local_endpoint); + boost::asio::ip::tcp::endpoint local_endpoint(boost::asio::ip::address::from_string(bind_ip.c_str()), 0); + boost::system::error_code ec; + sock_.bind(local_endpoint, ec); + if (ec) + { + MERROR("Error binding to " << bind_ip << ": " << ec.message()); + if (sock_.is_open()) + sock_.close(); + return false; + } } boost::shared_ptr<boost::asio::deadline_timer> sh_deadline(new boost::asio::deadline_timer(io_service_)); diff --git a/contrib/epee/include/span.h b/contrib/epee/include/span.h index 452cc088f..174915ecf 100644 --- a/contrib/epee/include/span.h +++ b/contrib/epee/include/span.h @@ -28,6 +28,7 @@ #pragma once +#include <algorithm> #include <cstdint> #include <memory> #include <type_traits> @@ -52,11 +53,15 @@ namespace epee template<typename T> class span { - /* Supporting class types is tricky - the {ptr,len} constructor will allow - derived-to-base conversions. This is NOT desireable because an array of - derived types is not an array of base types. It is possible to handle - this case, implement when/if needed. */ - static_assert(!std::is_class<T>(), "no class types are currently allowed"); + template<typename U> + static constexpr bool safe_conversion() noexcept + { + // Allow exact matches or `T*` -> `const T*`. + using with_const = typename std::add_const<U>::type; + return std::is_same<T, U>() || + (std::is_const<T>() && std::is_same<T, with_const>()); + } + public: using value_type = T; using size_type = std::size_t; @@ -71,7 +76,9 @@ namespace epee constexpr span() noexcept : ptr(nullptr), len(0) {} constexpr span(std::nullptr_t) noexcept : span() {} - constexpr span(T* const src_ptr, const std::size_t count) noexcept + //! Prevent derived-to-base conversions; invalid in this context. + template<typename U, typename = typename std::enable_if<safe_conversion<U>()>::type> + constexpr span(U* const src_ptr, const std::size_t count) noexcept : ptr(src_ptr), len(count) {} //! Conversion from C-array. Prevents common bugs with sizeof + arrays. @@ -81,6 +88,16 @@ namespace epee constexpr span(const span&) noexcept = default; span& operator=(const span&) noexcept = default; + /*! Try to remove `amount` elements from beginning of span. + \return Number of elements removed. */ + std::size_t remove_prefix(std::size_t amount) noexcept + { + amount = std::min(len, amount); + ptr += amount; + len -= amount; + return amount; + } + constexpr iterator begin() const noexcept { return ptr; } constexpr const_iterator cbegin() const noexcept { return ptr; } @@ -105,6 +122,14 @@ namespace epee return {src.data(), src.size()}; } + //! \return `span<T::value_type>` from a STL compatible `src`. + template<typename T> + constexpr span<typename T::value_type> to_mut_span(T& src) + { + // compiler provides diagnostic if size() is not size_t. + return {src.data(), src.size()}; + } + template<typename T> constexpr bool has_padding() noexcept { @@ -127,4 +152,13 @@ namespace epee static_assert(!has_padding<T>(), "source type may have padding"); return {reinterpret_cast<const std::uint8_t*>(std::addressof(src)), sizeof(T)}; } + + //! \return `span<std::uint8_t>` which represents the bytes at `&src`. + template<typename T> + span<std::uint8_t> as_mut_byte_span(T& src) noexcept + { + static_assert(!std::is_empty<T>(), "empty types will not work -> sizeof == 1"); + static_assert(!has_padding<T>(), "source type may have padding"); + return {reinterpret_cast<std::uint8_t*>(std::addressof(src)), sizeof(T)}; + } } diff --git a/contrib/epee/include/wipeable_string.h b/contrib/epee/include/wipeable_string.h index 4cebe5fdf..31854fe2e 100644 --- a/contrib/epee/include/wipeable_string.h +++ b/contrib/epee/include/wipeable_string.h @@ -28,10 +28,11 @@ #pragma once -#include <boost/optional/optional_fwd.hpp> +#include <boost/optional/optional.hpp> #include <stddef.h> #include <vector> #include <string> +#include "memwipe.h" #include "fnv1.h" namespace epee @@ -65,6 +66,8 @@ namespace epee void trim(); void split(std::vector<wipeable_string> &fields) const; boost::optional<wipeable_string> parse_hexstr() const; + template<typename T> inline bool hex_to_pod(T &pod) const; + template<typename T> inline bool hex_to_pod(tools::scrubbed<T> &pod) const { return hex_to_pod(unwrap(pod)); } void resize(size_t sz); void reserve(size_t sz); void clear(); @@ -79,6 +82,20 @@ namespace epee private: std::vector<char> buffer; }; + + template<typename T> inline bool wipeable_string::hex_to_pod(T &pod) const + { + static_assert(std::is_pod<T>::value, "expected pod type"); + if (size() != sizeof(T) * 2) + return false; + boost::optional<epee::wipeable_string> blob = parse_hexstr(); + if (!blob) + return false; + if (blob->size() != sizeof(T)) + return false; + pod = *(const T*)blob->data(); + return true; + } } namespace std diff --git a/contrib/epee/src/wipeable_string.cpp b/contrib/epee/src/wipeable_string.cpp index 7c9722765..69f92e106 100644 --- a/contrib/epee/src/wipeable_string.cpp +++ b/contrib/epee/src/wipeable_string.cpp @@ -32,6 +32,8 @@ #include "misc_log_ex.h" #include "wipeable_string.h" +static constexpr const char hex[] = u8"0123456789abcdef"; + namespace { int atolower(int c) @@ -197,7 +199,6 @@ boost::optional<epee::wipeable_string> wipeable_string::parse_hexstr() const const size_t len = size(); const char *d = data(); res->grow(0, len / 2); - static constexpr const char hex[] = u8"0123456789abcdef"; for (size_t i = 0; i < len; i += 2) { char c = atolower(d[i]); |