Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnativesocketengine_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QNATIVESOCKETENGINE_P_H
7#define QNATIVESOCKETENGINE_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtNetwork/private/qtnetworkglobal_p.h>
21#include "QtNetwork/qhostaddress.h"
22#include "QtNetwork/qnetworkinterface.h"
23#include "private/qabstractsocketengine_p.h"
24#include "qplatformdefs.h"
25
26#ifndef Q_OS_WIN
27# include <netinet/in.h>
28#else
29# include <winsock2.h>
30# include <ws2tcpip.h>
31# include <mswsock.h>
32#endif
33
34QT_BEGIN_NAMESPACE
35
36#ifdef Q_OS_WIN
37# define QT_SOCKLEN_T int
38# define QT_SOCKOPTLEN_T int
39#endif
40
41namespace {
42namespace SetSALen {
43 template <typename T> void set(T *sa, typename std::enable_if<(&T::sa_len, true), QT_SOCKLEN_T>::type len)
44 { sa->sa_len = len; }
45 template <typename T> void set(T *sa, typename std::enable_if<(&T::sin_len, true), QT_SOCKLEN_T>::type len)
46 { sa->sin_len = len; }
47 template <typename T> void set(T *sin6, typename std::enable_if<(&T::sin6_len, true), QT_SOCKLEN_T>::type len)
48 { sin6->sin6_len = len; }
49 template <typename T> void set(T *, ...) {}
50}
51
52inline QT_SOCKLEN_T setSockaddr(sockaddr_in *sin, const QHostAddress &addr, quint16 port = 0)
53{
54 *sin = {};
55 SetSALen::set(sin, sizeof(*sin));
56 sin->sin_family = AF_INET;
57 sin->sin_port = htons(port);
58 sin->sin_addr.s_addr = htonl(addr.toIPv4Address());
59 return sizeof(*sin);
60}
61
62inline QT_SOCKLEN_T setSockaddr(sockaddr_in6 *sin6, const QHostAddress &addr, quint16 port = 0)
63{
64 *sin6 = {};
65 SetSALen::set(sin6, sizeof(*sin6));
66 sin6->sin6_family = AF_INET6;
67 sin6->sin6_port = htons(port);
68 memcpy(sin6->sin6_addr.s6_addr, addr.toIPv6Address().c, sizeof(sin6->sin6_addr));
69#if QT_CONFIG(networkinterface)
70 sin6->sin6_scope_id = QNetworkInterface::interfaceIndexFromName(addr.scopeId());
71#else
72 // it had better be a number then, if it is not empty
73 sin6->sin6_scope_id = addr.scopeId().toUInt();
74#endif
75 return sizeof(*sin6);
76}
77
78inline QT_SOCKLEN_T setSockaddr(sockaddr *sa, const QHostAddress &addr, quint16 port = 0)
79{
80 switch (addr.protocol()) {
81 case QHostAddress::IPv4Protocol:
82 return setSockaddr(reinterpret_cast<sockaddr_in *>(sa), addr, port);
83
84 case QHostAddress::IPv6Protocol:
85 case QHostAddress::AnyIPProtocol:
86 return setSockaddr(reinterpret_cast<sockaddr_in6 *>(sa), addr, port);
87
88 case QHostAddress::UnknownNetworkLayerProtocol:
89 break;
90 }
91 *sa = {};
92 sa->sa_family = AF_UNSPEC;
93 return 0;
94}
95} // unnamed namespace
96
98#ifndef QT_NO_NETWORKINTERFACE
99class QNetworkInterface;
100#endif
101
102class Q_AUTOTEST_EXPORT QNativeSocketEngine : public QAbstractSocketEngine
103{
104 Q_OBJECT
105public:
106 QNativeSocketEngine(QObject *parent = nullptr);
107 ~QNativeSocketEngine();
108
109 bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) override;
110 bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) override;
111
112 qintptr socketDescriptor() const override;
113
114 bool isValid() const override;
115
116 bool connectToHost(const QHostAddress &address, quint16 port) override;
117 bool connectToHostByName(const QString &name, quint16 port) override;
118 bool bind(const QHostAddress &address, quint16 port) override;
119 bool listen(int backlog) override;
120 qintptr accept() override;
121 void close() override;
122
123 qint64 bytesAvailable() const override;
124
125 qint64 read(char *data, qint64 maxlen) override;
126 qint64 write(const char *data, qint64 len) override;
127
128#ifndef QT_NO_UDPSOCKET
129#ifndef QT_NO_NETWORKINTERFACE
130 bool joinMulticastGroup(const QHostAddress &groupAddress,
131 const QNetworkInterface &iface) override;
132 bool leaveMulticastGroup(const QHostAddress &groupAddress,
133 const QNetworkInterface &iface) override;
134 QNetworkInterface multicastInterface() const override;
135 bool setMulticastInterface(const QNetworkInterface &iface) override;
136#endif
137
138 bool hasPendingDatagrams() const override;
139 qint64 pendingDatagramSize() const override;
140#endif // QT_NO_UDPSOCKET
141
142 qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader * = nullptr,
143 PacketHeaderOptions = WantNone) override;
144 qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) override;
145 qint64 bytesToWrite() const override;
146
147#if 0 // currently unused
148 qint64 receiveBufferSize() const;
149 void setReceiveBufferSize(qint64 bufferSize);
150
151 qint64 sendBufferSize() const;
152 void setSendBufferSize(qint64 bufferSize);
153#endif
154
155 int option(SocketOption option) const override;
156 bool setOption(SocketOption option, int value) override;
157
158 bool waitForRead(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout},
159 bool *timedOut = nullptr) override;
160 bool waitForWrite(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout},
161 bool *timedOut = nullptr) override;
162 bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
163 bool checkRead, bool checkWrite,
164 QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout},
165 bool *timedOut = nullptr) override;
166
167 bool isReadNotificationEnabled() const override;
168 void setReadNotificationEnabled(bool enable) override;
169 bool isWriteNotificationEnabled() const override;
170 void setWriteNotificationEnabled(bool enable) override;
171 bool isExceptionNotificationEnabled() const override;
172 void setExceptionNotificationEnabled(bool enable) override;
173
174public Q_SLOTS:
175 // non-virtual override;
176 void connectionNotification();
177
178private:
179 Q_DECLARE_PRIVATE(QNativeSocketEngine)
180 Q_DISABLE_COPY_MOVE(QNativeSocketEngine)
181};
182
183QT_END_NAMESPACE
184
185#endif // QNATIVESOCKETENGINE_P_H
QAbstractSocketEngineReceiver * receiver
QAbstractSocket::NetworkLayerProtocol socketProtocol
QAbstractSocket::SocketState socketState
QAbstractSocket::SocketError socketError
QAbstractSocket::SocketType socketType
virtual void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)=0
virtual void writeNotification()=0
virtual void exceptionNotification()=0
virtual void connectionNotification()=0
virtual void closeNotification()=0
virtual void readNotification()=0
The QNativeSocketEngine class provides low level access to a socket.
void set(T *sa, typename std::enable_if<(&T::sa_len, true), QT_SOCKLEN_T >::type len)
QT_SOCKLEN_T setSockaddr(sockaddr_in6 *sin6, const QHostAddress &addr, quint16 port=0)
QT_SOCKLEN_T setSockaddr(sockaddr *sa, const QHostAddress &addr, quint16 port=0)
QT_SOCKLEN_T setSockaddr(sockaddr_in *sin, const QHostAddress &addr, quint16 port=0)
static constexpr std::chrono::seconds DefaultTimeout
#define AF_INET6