summaryrefslogtreecommitdiffstats
path: root/src/network/socket/qabstractsocket.cpp
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2025-09-22 11:47:14 +0200
committerIvan Solovev <ivan.solovev@qt.io>2025-10-09 16:43:12 +0200
commit83fc4ff9c20eb922113e08ba8566e077ce44dea3 (patch)
tree61a399b1f23e06cba5d5fcb3e8eedbd30d3f1074 /src/network/socket/qabstractsocket.cpp
parente9778dfe6ead0c4d704570816a56aead084a0263 (diff)
Allow binding a socket to a specific QNetworkInterface
For now, via a private API. Provide a new socket engine option - BindInterfaceIndex, and use it if a valid interface is passed to QAbstractSocketPrivate::bind(). Note that this feature is not fully functional on macOS: both SO_BINDTODEVICE and IP{V6}_BOUND_IF only work for limiting outgoing datagrams. The patch uses IP{V6}_BOUND_IF, because it allows to avoid number -> string -> number conversion. As of now, extra filtering of incoming data should be done on the user side. Task-number: QTBUG-80704 Task-number: QTBUG-139697 Pick-to: 6.10 6.8 Change-Id: Ic207908313d9d25f96c23ecc363181ff0ae8232a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/network/socket/qabstractsocket.cpp')
-rw-r--r--src/network/socket/qabstractsocket.cpp7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp
index ce3626304c8..f2474ad7e97 100644
--- a/src/network/socket/qabstractsocket.cpp
+++ b/src/network/socket/qabstractsocket.cpp
@@ -432,6 +432,7 @@
#include "qabstractsocket.h"
#include "qabstractsocket_p.h"
+#include "qnetworkinterface.h"
#include "private/qhostinfo_p.h"
@@ -1505,8 +1506,6 @@ bool QAbstractSocketPrivate::bind(const QHostAddress &address, quint16 port, QAb
{
Q_Q(QAbstractSocket);
- Q_UNUSED(iface); // will be used in a follow-up patch
-
// now check if the socket engine is initialized and to the right type
if (!socketEngine || !socketEngine->isValid()) {
QHostAddress nullAddress;
@@ -1538,6 +1537,10 @@ bool QAbstractSocketPrivate::bind(const QHostAddress &address, quint16 port, QAb
socketEngine->setOption(QAbstractSocketEngine::BindExclusively, 0);
#endif
}
+#if QT_CONFIG(networkinterface)
+ if (iface && iface->isValid())
+ socketEngine->setOption(QAbstractSocketEngine::BindInterfaceIndex, iface->index());
+#endif
bool result = socketEngine->bind(address, port);
cachedSocketDescriptor = socketEngine->socketDescriptor();