-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use random ports for network client #124
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -114,28 +114,10 @@ bool C4Network2IO::Init(std::shared_ptr<spdlog::logger> logger, const std::uint1 | |||||
} | ||||||
|
||||||
// initialize net i/o classes: TCP first | ||||||
pNetIO_TCP = CreateNetIO(this->logger, "TCP I/O", new C4NetIOTCP{}, iPortTCP, Thread); | ||||||
if (pNetIO_TCP) | ||||||
{ | ||||||
pNetIO_TCP->SetCallback(this); | ||||||
|
||||||
if (Config.Network.EnableUPnP) | ||||||
{ | ||||||
UPnP->AddMapping(P_TCP, iPortTCP, 0); | ||||||
} | ||||||
} | ||||||
pNetIO_TCP = CreateSetupNetIO(P_TCP, "TCP I/O", new C4NetIOTCP{}, Thread, iPortTCP); | ||||||
|
||||||
// then UDP | ||||||
pNetIO_UDP = CreateNetIO(this->logger, "UDP I/O", new C4NetIOUDP{}, iPortUDP, Thread); | ||||||
if (pNetIO_UDP) | ||||||
{ | ||||||
pNetIO_UDP->SetCallback(this); | ||||||
|
||||||
if (Config.Network.EnableUPnP) | ||||||
{ | ||||||
UPnP->AddMapping(P_UDP, iPortUDP, 0); | ||||||
} | ||||||
} | ||||||
pNetIO_UDP = CreateSetupNetIO(P_UDP, "UDP I/O", new C4NetIOUDP{}, Thread, iPortUDP); | ||||||
|
||||||
// no protocols? | ||||||
if (!pNetIO_TCP && !pNetIO_UDP) | ||||||
|
@@ -473,6 +455,40 @@ bool C4Network2IO::IsPuncherAddr(const C4NetIO::addr_t &addr) const | |||||
(!PuncherAddrIPv6.IsNull() && PuncherAddrIPv6 == addr); | ||||||
} | ||||||
|
||||||
template<std::derived_from<C4NetIO> T> | ||||||
T *C4Network2IO::CreateSetupNetIO(const C4Network2IOProtocol protocol, const char *const name, T *const io, C4InteractiveThread &thread, const std::uint16_t defaultPort) | ||||||
{ | ||||||
uint16_t port; | ||||||
|
||||||
if (defaultPort == NULL) | ||||||
{ | ||||||
port = DiscoverFreePort(); | ||||||
} | ||||||
else | ||||||
{ | ||||||
port = defaultPort; | ||||||
if (!CheckPortAvailability(port)) | ||||||
{ | ||||||
logger->info("Port {} is unavailable, discovering a free one", port); | ||||||
port = DiscoverFreePort(); | ||||||
} | ||||||
} | ||||||
|
||||||
T *netIO = CreateNetIO(this->logger, name, io, port, thread); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary
Suggested change
|
||||||
|
||||||
if (netIO) | ||||||
{ | ||||||
netIO->SetCallback(this); | ||||||
|
||||||
if (Config.Network.EnableUPnP) | ||||||
{ | ||||||
UPnP->AddMapping(protocol, port, 0); | ||||||
} | ||||||
} | ||||||
|
||||||
return netIO; | ||||||
} | ||||||
|
||||||
// C4NetIO interface | ||||||
bool C4Network2IO::OnConn(const C4NetIO::addr_t &PeerAddr, const C4NetIO::addr_t &ConnectAddr, const C4NetIO::addr_t *pOwnAddr, C4NetIO *pNetIO) | ||||||
{ | ||||||
|
@@ -1248,6 +1264,43 @@ void C4Network2IO::SendConnPackets() | |||||
} | ||||||
} | ||||||
|
||||||
bool C4Network2IO::CheckPortAvailability(const uint16_t port) | ||||||
{ | ||||||
const auto pNetIO = std::make_unique<C4NetIOUDP>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't check whether a TCP port is available by attempting to bind a UDP socket. |
||||||
|
||||||
if (port <= 0) | ||||||
{ | ||||||
return false; | ||||||
} | ||||||
|
||||||
if (pNetIO->Init(port)) | ||||||
{ | ||||||
pNetIO->Close(); | ||||||
|
||||||
return true; | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
|
||||||
std::uint16_t C4Network2IO::DiscoverFreePort(const uint16_t attempts) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||||||
{ | ||||||
static constexpr uint16_t maxPort = 65535; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a constant and therefore should be spelled |
||||||
uint16_t port = NULL; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. Also, we use
Suggested change
|
||||||
|
||||||
for (uint16_t i = 0; i < attempts; ++i) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||||||
{ | ||||||
port = SafeRandom(maxPort); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can return |
||||||
|
||||||
if (CheckPortAvailability(port)) | ||||||
{ | ||||||
break; | ||||||
} | ||||||
} | ||||||
|
||||||
return port; | ||||||
} | ||||||
|
||||||
// *** C4Network2IOConnection | ||||||
|
||||||
C4Network2IOConnection::C4Network2IOConnection() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never use
NULL
in C++ - defaultPort also isn't a pointer.