Skip to content

Rework the opening of UDP sockets for IPv4 and IPv6 - #3893

Open
softins wants to merge 3 commits into
jamulussoftware:mainfrom
softins:socket-opening-correction
Open

Rework the opening of UDP sockets for IPv4 and IPv6#3893
softins wants to merge 3 commits into
jamulussoftware:mainfrom
softins:socket-opening-correction

Conversation

@softins

@softins softins commented Aug 12, 2026

Copy link
Copy Markdown
Member

Short description of changes

Reworks the socket opening logic introduced in #3774, which had potential shortcomings identified by AI.
Not for backporting, as #3774 was not also.

CHANGELOG: Client/Server: reworked and improved UDP socket opening logic

Context: Fixes an issue?

Fixes #3885

Does this change need documentation? What needs to be documented and how?

No, bug fixes only

Status of this Pull Request

Ready for review

What is missing until this pull request can be merged?

Checking that it satisfies CoPilot AI shortcomings that were flagged in #3885 for socket.cpp

Checklist

  • I've verified that this Pull Request follows the general code principles
  • I tested my code and it does what I want
  • My code follows the style guide
  • I waited some time after this Pull Request was opened and all GitHub checks completed without errors.
  • I've filled all the content above

@softins softins self-assigned this Aug 12, 2026
@softins softins added the bug Something isn't working label Aug 12, 2026
@github-project-automation github-project-automation Bot moved this to Triage in Tracking Aug 12, 2026
@softins softins added this to the Release 4.0.0 milestone Aug 12, 2026
Comment thread src/socket.cpp
// faulty router gets stuck and confused by a particular port (like
// the starting port). Might work around frustrating "cannot connect"
// problems (#568)
const quint16 startingPortNumber = iPortNumber + rand() % NUM_SOCKET_PORTS_TO_TRY;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this was the first call to rand() after program startup, and rand() always starts from the same seed, this was not providing any randomness at all. The same start port was chosen on each invocation, so this wouldn't actually address #568 as claimed. Verified on Linux and Mac.

Comment thread src/socket.cpp

quint16 iClientPortIncrement = 0;
bSuccess = false; // initialization for while loop
const quint32 startingPortNumber = static_cast<quint32> ( iPortNumber ) + QRandomGenerator::global()->bounded ( NUM_SOCKET_PORTS_TO_TRY );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This provides a truly random starting port.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't C++ have something inbuilt? I'd prefer something that not depends on Qt.

This corrects logic errors for bind() failure handling found by AI.
bIPv6Available is now only set if bind() succeeds for IPv6.
Previously, rand() was used and is always seeded with 1, providing
no randomness at program start.
@softins
softins force-pushed the socket-opening-correction branch from c56534e to 0d7b521 Compare August 12, 2026 14:52
Comment thread src/socket.cpp
if ( !bSuccess )
{
// we cannot bind socket, throw error
throw CGenErr ( "Cannot bind the socket (maybe "

@pljones pljones Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth expanding this to explain more about what failed as there are more possible causes now. (Not due to this PR -- I've just read it to get more context and realised it's a bit weak.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, bSuccess only now applies to IPv4. If we fail to create and bind IPv4, bSuccess will be false and we throw the exception. If we succeed with IPv4 but fail with IPv6, it's just logged with a message, but bSuccess is still true. So there isn't really any more info to pass to the exception apart from what is already there.

@softins softins changed the title Reword the opening of UDP sockets for IPv4 and IPv6 Rework the opening of UDP sockets for IPv4 and IPv6 Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

Investigate socket.cpp and buffer.cpp findings in AI code quality

3 participants