Add a delay before allowing the next connection if the wrong PIN is entered
This commit is contained in:
@@ -51,6 +51,9 @@ static std::string clean_key(std::string key)
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wivrn::incorrect_pin::incorrect_pin() :
|
||||||
|
std::runtime_error("Incorrect PIN") {}
|
||||||
|
|
||||||
wivrn::wivrn_connection::wivrn_connection(std::stop_token stop_token, encryption_state state, std::string pin, TCP && tcp) :
|
wivrn::wivrn_connection::wivrn_connection(std::stop_token stop_token, encryption_state state, std::string pin, TCP && tcp) :
|
||||||
control(std::move(tcp)),
|
control(std::move(tcp)),
|
||||||
stream(-1),
|
stream(-1),
|
||||||
@@ -217,7 +220,7 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
|
|||||||
control.send(to_headset::pin_check_4{msg4});
|
control.send(to_headset::pin_check_4{msg4});
|
||||||
|
|
||||||
if (not pin_match)
|
if (not pin_match)
|
||||||
throw std::runtime_error("Incorrect PIN");
|
throw incorrect_pin{};
|
||||||
}
|
}
|
||||||
catch (crypto::smp_cheated &)
|
catch (crypto::smp_cheated &)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -32,6 +32,12 @@
|
|||||||
|
|
||||||
namespace wivrn
|
namespace wivrn
|
||||||
{
|
{
|
||||||
|
class incorrect_pin : public std::runtime_error
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
incorrect_pin();
|
||||||
|
};
|
||||||
|
|
||||||
class wivrn_connection
|
class wivrn_connection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
+28
-3
@@ -214,6 +214,10 @@ guint pairing_timeout;
|
|||||||
std::string pin;
|
std::string pin;
|
||||||
NotifyNotification * pin_notification;
|
NotifyNotification * pin_notification;
|
||||||
|
|
||||||
|
// Delay until the next connection is allowed, increased after each incorrect attempt
|
||||||
|
const std::chrono::milliseconds default_delay_next_try = 10ms;
|
||||||
|
std::chrono::milliseconds delay_next_try = default_delay_next_try;
|
||||||
|
|
||||||
WivrnServer * dbus_server;
|
WivrnServer * dbus_server;
|
||||||
|
|
||||||
/* TODO: Document FSM
|
/* TODO: Document FSM
|
||||||
@@ -427,9 +431,11 @@ void update_fsm()
|
|||||||
{
|
{
|
||||||
runtime_setter.reset();
|
runtime_setter.reset();
|
||||||
|
|
||||||
start_listening();
|
g_timeout_add(delay_next_try.count(), [](void *) {
|
||||||
start_publishing();
|
start_listening();
|
||||||
wivrn_server_set_headset_connected(dbus_server, false);
|
start_publishing();
|
||||||
|
wivrn_server_set_headset_connected(dbus_server, false);
|
||||||
|
return G_SOURCE_REMOVE; }, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -462,6 +468,8 @@ gboolean headset_connected_success(void *)
|
|||||||
start_server();
|
start_server();
|
||||||
start_app();
|
start_app();
|
||||||
|
|
||||||
|
delay_next_try = default_delay_next_try;
|
||||||
|
|
||||||
connection.reset();
|
connection.reset();
|
||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
@@ -475,6 +483,18 @@ gboolean headset_connected_failed(void *)
|
|||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean headset_connected_incorrect_pin(void *)
|
||||||
|
{
|
||||||
|
assert(connection_thread);
|
||||||
|
connection_thread.reset();
|
||||||
|
|
||||||
|
delay_next_try = 2 * delay_next_try;
|
||||||
|
std::cout << "Waiting " << delay_next_try << " until the next attempt is allowed" << std::endl;
|
||||||
|
|
||||||
|
update_fsm();
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
|
||||||
gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data)
|
gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data)
|
||||||
{
|
{
|
||||||
assert(server_watch == 0);
|
assert(server_watch == 0);
|
||||||
@@ -492,6 +512,11 @@ gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data)
|
|||||||
connection = std::make_unique<wivrn_connection>(stop_token, enc_state, pin, std::move(tcp));
|
connection = std::make_unique<wivrn_connection>(stop_token, enc_state, pin, std::move(tcp));
|
||||||
g_main_context_invoke(nullptr, &headset_connected_success, nullptr);
|
g_main_context_invoke(nullptr, &headset_connected_success, nullptr);
|
||||||
}
|
}
|
||||||
|
catch (wivrn::incorrect_pin &)
|
||||||
|
{
|
||||||
|
std::cerr << "Incorrect PIN" << std::endl;
|
||||||
|
g_main_context_invoke(nullptr, &headset_connected_incorrect_pin, nullptr);
|
||||||
|
}
|
||||||
catch (std::exception & e)
|
catch (std::exception & e)
|
||||||
{
|
{
|
||||||
std::cerr << "Client connection failed: " << e.what() << std::endl;
|
std::cerr << "Client connection failed: " << e.what() << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user