i2p: avoid using Sock::Get() for checking for a valid socket

Peeking at the underlying socket file descriptor of `Sock` and checkig
if it is `INVALID_SOCKET` is bad encapsulation and stands in the way of
testing/mocking/fuzzing.

Instead use an empty unique_ptr to denote that there is no valid socket.
pull/26312/head
Vasil Dimov 4 years ago
parent 083316c4fe
commit 5ac1a51ee5
No known key found for this signature in database
GPG Key ID: 54DF06F64B55CBBF

@ -119,7 +119,6 @@ Session::Session(const fs::path& private_key_file,
: m_private_key_file{private_key_file},
m_control_host{control_host},
m_interrupt{interrupt},
m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)},
m_transient{false}
{
}
@ -127,7 +126,6 @@ Session::Session(const fs::path& private_key_file,
Session::Session(const CService& control_host, CThreadInterrupt* interrupt)
: m_control_host{control_host},
m_interrupt{interrupt},
m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)},
m_transient{true}
{
}
@ -315,7 +313,7 @@ void Session::CheckControlSock()
LOCK(m_mutex);
std::string errmsg;
if (!m_control_sock->IsConnected(errmsg)) {
if (m_control_sock && !m_control_sock->IsConnected(errmsg)) {
Log("Control socket error: %s", errmsg);
Disconnect();
}
@ -364,7 +362,7 @@ Binary Session::MyDestination() const
void Session::CreateIfNotCreatedAlready()
{
std::string errmsg;
if (m_control_sock->IsConnected(errmsg)) {
if (m_control_sock && m_control_sock->IsConnected(errmsg)) {
return;
}
@ -437,14 +435,14 @@ std::unique_ptr<Sock> Session::StreamAccept()
void Session::Disconnect()
{
if (m_control_sock->Get() != INVALID_SOCKET) {
if (m_control_sock) {
if (m_session_id.empty()) {
Log("Destroying incomplete SAM session");
} else {
Log("Destroying SAM session %s", m_session_id);
}
m_control_sock.reset();
}
m_control_sock = std::make_unique<Sock>(INVALID_SOCKET);
m_session_id.clear();
}
} // namespace sam

@ -261,6 +261,7 @@ private:
* ("SESSION CREATE"). With the established session id we later open
* other connections to the SAM service to accept incoming I2P
* connections and make outgoing ones.
* If not connected then this unique_ptr will be empty.
* See https://geti2p.net/en/docs/api/samv3
*/
std::unique_ptr<Sock> m_control_sock GUARDED_BY(m_mutex);

Loading…
Cancel
Save