Skip to content

Commit 5ac1a51

Browse files
committed
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.
1 parent 083316c commit 5ac1a51

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/i2p.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,13 @@ Session::Session(const fs::path& private_key_file,
119119
: m_private_key_file{private_key_file},
120120
m_control_host{control_host},
121121
m_interrupt{interrupt},
122-
m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)},
123122
m_transient{false}
124123
{
125124
}
126125

127126
Session::Session(const CService& control_host, CThreadInterrupt* interrupt)
128127
: m_control_host{control_host},
129128
m_interrupt{interrupt},
130-
m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)},
131129
m_transient{true}
132130
{
133131
}
@@ -315,7 +313,7 @@ void Session::CheckControlSock()
315313
LOCK(m_mutex);
316314

317315
std::string errmsg;
318-
if (!m_control_sock->IsConnected(errmsg)) {
316+
if (m_control_sock && !m_control_sock->IsConnected(errmsg)) {
319317
Log("Control socket error: %s", errmsg);
320318
Disconnect();
321319
}
@@ -364,7 +362,7 @@ Binary Session::MyDestination() const
364362
void Session::CreateIfNotCreatedAlready()
365363
{
366364
std::string errmsg;
367-
if (m_control_sock->IsConnected(errmsg)) {
365+
if (m_control_sock && m_control_sock->IsConnected(errmsg)) {
368366
return;
369367
}
370368

@@ -437,14 +435,14 @@ std::unique_ptr<Sock> Session::StreamAccept()
437435

438436
void Session::Disconnect()
439437
{
440-
if (m_control_sock->Get() != INVALID_SOCKET) {
438+
if (m_control_sock) {
441439
if (m_session_id.empty()) {
442440
Log("Destroying incomplete SAM session");
443441
} else {
444442
Log("Destroying SAM session %s", m_session_id);
445443
}
444+
m_control_sock.reset();
446445
}
447-
m_control_sock = std::make_unique<Sock>(INVALID_SOCKET);
448446
m_session_id.clear();
449447
}
450448
} // namespace sam

src/i2p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class Session
261261
* ("SESSION CREATE"). With the established session id we later open
262262
* other connections to the SAM service to accept incoming I2P
263263
* connections and make outgoing ones.
264+
* If not connected then this unique_ptr will be empty.
264265
* See https://geti2p.net/en/docs/api/samv3
265266
*/
266267
std::unique_ptr<Sock> m_control_sock GUARDED_BY(m_mutex);

0 commit comments

Comments
 (0)