Skip to content

Commit

Permalink
Allow service_discovery to continue without random_device (#479)
Browse files Browse the repository at this point in the history
* Allow service_discovery to continue without random_device

Allow service_discovery to continue even if the random_device isn't yet
available.  This is unlikely to occur on linux systems, but not
impossible.  It is more likely on other systems such as QNX.  In the
event that the random device isn't available, an error message is
produced and the middle of the min/max for the delay is used.

Without this change, if the random device is missing, the following
error is thrown:

   terminate called after throwing an instance of 'std::__1::system_error'
   what():  random_device failed to open /dev/urandom: No such file or directory
   Abort (core dumped)

* Remove the check_routing_credentials_ member when security is disabled.  This removes the private error warning turned error by -Werror:

error: private field 'check_routing_credentials_' is not used [-Werror,-Wunused-private-field] error

* Update service_discovery_impl.cpp

---------

Co-authored-by: Diogo Pedrosa <[email protected]>
  • Loading branch information
kheaactua and DiogoPedrozza authored Nov 6, 2023
1 parent 3230caf commit 6e40119
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion implementation/security/include/policy_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ class VSOMEIP_IMPORT_EXPORT policy_manager_impl
mutable boost::shared_mutex policy_extension_paths_mutex_;
//map[hostname, pair[path, map[complete path with UID/GID, control loading]]
std::map<std::string, std::pair<std::string, std::map<std::string, bool>>> policy_extension_paths_;

bool check_routing_credentials_;
#endif // !VSOMEIP_DISABLE_SECURITY

bool is_configured_;
bool check_routing_credentials_;

mutable std::mutex routing_credentials_mutex_;
std::pair<uint32_t, uint32_t> routing_credentials_;
Expand Down
4 changes: 2 additions & 2 deletions implementation/security/src/policy_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ policy_manager_impl::policy_manager_impl()
allow_remote_clients_(true),
check_whitelist_(false),
policy_base_path_(""),
check_routing_credentials_(false),
#endif // !VSOMEIP_DISABLE_SECURITY
is_configured_(false),
check_routing_credentials_(false)
is_configured_(false)
{
}

Expand Down
30 changes: 25 additions & 5 deletions implementation/service_discovery/src/service_discovery_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,31 @@ service_discovery_impl::init() {
initial_delay_max = tmp;
}

std::random_device r;
std::mt19937 e(r());
std::uniform_int_distribution<std::uint32_t> distribution(
initial_delay_min, initial_delay_max);
initial_delay_ = std::chrono::milliseconds(distribution(e));
try {
std::random_device r;
std::mt19937 e(r());
std::uniform_int_distribution<std::uint32_t> distribution(
initial_delay_min, initial_delay_max);
initial_delay_ = std::chrono::milliseconds(distribution(e));
} catch (const std::exception& e) {
VSOMEIP_ERROR << "Failed to generate random initial delay: " << e.what();

// Fallback to the Mersenne Twister engine
const auto seed = static_cast<std::mt19937::result_type>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count());

std::mt19937 mtwister{seed};

// Interpolate between initial_delay bounds
initial_delay_ = std::chrono::milliseconds(
initial_delay_min +
(static_cast<std::int64_t>(mtwister()) *
static_cast<std::int64_t>(initial_delay_max - initial_delay_min) /
static_cast<std::int64_t>(std::mt19937::max() -
std::mt19937::min())));
}


repetitions_base_delay_ = std::chrono::milliseconds(
Expand Down

0 comments on commit 6e40119

Please sign in to comment.