Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-47011

[Reg 5.4.2 -> 5.5.0] QUdpSocket: binding to port already in use does not work anymore

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.6.0 RC
    • 5.5.0
    • Network
    • Debian Jessie 64bit, Ubuntu 14.04 LTS 64bit
    • c28bc5f11361960da1bc94a5ea6566ee0fc8397c

    Description

      For some reason binding the "any" address 0.0.0.0 does not work anymore. I have tested with the following settings:

      QUdpSocket::BindMode mode;
      mode |= QUdpSocket::ShareAddress;
      mode |= QUdpSocket::ReuseAddressHint;
      bool bound = sock->bind(QHostAddress::Any, 5353, mode);

      I get similar results with QHostAddress::AnyIPv4. The same code works with Qt 5.4.

      I should mention that the port 5353 is used for Multicast DNS:
      udp 0 0 0.0.0.0:5353 0.0.0.0:* 590/avahi-daemon: r
      udp6 0 0 :::5353 :::* 590/avahi-daemon: r

      Binding works on an unused port. Looks like the BindFlags are somehow ignored or not properly interpreted.

      UPDATE:
      I researched in the source code to identify the problem. It seems a new binding flag was introduced somewhere between 5.4 and 5.5:
      https://github.com/qtproject/qtbase/blob/5.5.0/src/network/socket/qnativesocketengine_unix.cpp#L340
      The problem is that SO_REUSEPORT behaves differently than SO_REUSEADDR on Linux. See http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t for reference. Therefore, this option should not be set on Linux or an additinal socket flag to enforce this behavior should be introduced.

      The problem seems to be that other processes that are already listening on a specific port need to have set this exact socket flag too and need to belong to the same user. The behavior is different to SO_REUSEADDR. The example what I am referring to is an avahi-daemon process started by the system. When I try to bind to the same port/address with a custom mDNS discovery application I will get this error.

      This is the project I tested this with:
      https://github.com/psi-im/jdns

      UPDATE2:
      Verified the source of the problem by deactivating the SO_REUSEPORT part of the code. This PR fixes the problem:
      https://github.com/qtproject/qtbase/pull/5

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            richmoore Richard Moore (qtnetwork)
            strahlex Alexander Rössler
            Votes:
            6 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes