Skip to content

Commit

Permalink
select random port if port not found or specified
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Aug 11, 2022
1 parent f4d6a08 commit 9d123fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
32 changes: 19 additions & 13 deletions libi2pd/RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ namespace i2p
i2p::data::LocalRouterInfo routerInfo;
routerInfo.SetRouterIdentity (GetIdentity ());
uint16_t port; i2p::config::GetOption("port", port);
if (!port)
{
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
if (!port) port = SelectRandomPort ();
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
bool ssu; i2p::config::GetOption("ssu", ssu);
Expand Down Expand Up @@ -200,6 +196,13 @@ namespace i2p
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
}

uint16_t RouterContext::SelectRandomPort () const
{
uint16_t port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
return port;
}

void RouterContext::UpdateRouterInfo ()
{
m_RouterInfo.CreateBuffer (m_Keys);
Expand Down Expand Up @@ -325,12 +328,7 @@ namespace i2p
}
if (isAddr)
{
if (!port && !address->port)
{
// select random port only if address's port is not set
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
if (!port && !address->port) port = SelectRandomPort ();
if (port) address->port = port;
address->published = publish;
memcpy (address->i, m_NTCP2Keys->iv, 16);
Expand Down Expand Up @@ -761,7 +759,11 @@ namespace i2p
}
port = addr->port;
}
if (!port) i2p::config::GetOption("port", port);
if (!port)
{
i2p::config::GetOption("port", port);
if (!port) port = SelectRandomPort ();
}
// SSU
bool ssu; i2p::config::GetOption("ssu", ssu);
if (!foundSSU && ssu)
Expand Down Expand Up @@ -847,7 +849,11 @@ namespace i2p
}
if (addr->port) port = addr->port;
}
if (!port) i2p::config::GetOption("port", port);
if (!port)
{
i2p::config::GetOption("port", port);
if (!port) port = SelectRandomPort ();
}
// SSU
bool ssu; i2p::config::GetOption("ssu", ssu);
if (!foundSSU && ssu)
Expand Down
1 change: 1 addition & 0 deletions libi2pd/RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ namespace garlic
bool IsSSU2Only () const; // SSU2 and no SSU
bool Load ();
void SaveKeys ();
uint16_t SelectRandomPort () const;

bool DecryptECIESTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, size_t clearTextSize);

Expand Down

0 comments on commit 9d123fa

Please sign in to comment.