1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QNATIVESOCKETENGINE_WINRT_P_H
41 #define QNATIVESOCKETENGINE_WINRT_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of the QLibrary class.  This header file may change from
49 // version to version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtNetwork/private/qtnetworkglobal_p.h>
55 #include <QtCore/QEventLoop>
56 #include <QtCore/QBuffer>
57 #include <QtCore/QLoggingCategory>
58 #include <QtCore/QMutex>
59 #include <QtCore/QAtomicInteger>
60 #include "QtNetwork/qhostaddress.h"
61 #include "private/qabstractsocketengine_p.h"
62 #include <wrl.h>
63 #include <windows.networking.sockets.h>
64 
65 QT_BEGIN_NAMESPACE
66 
Q_DECLARE_LOGGING_CATEGORY(lcNetworkSocket)67 Q_DECLARE_LOGGING_CATEGORY(lcNetworkSocket)
68 Q_DECLARE_LOGGING_CATEGORY(lcNetworkSocketVerbose)
69 
70 namespace WinRTSocketEngine {
71     enum ErrorString {
72         NonBlockingInitFailedErrorString,
73         BroadcastingInitFailedErrorString,
74         NoIpV6ErrorString,
75         RemoteHostClosedErrorString,
76         TimeOutErrorString,
77         ResourceErrorString,
78         OperationUnsupportedErrorString,
79         ProtocolUnsupportedErrorString,
80         InvalidSocketErrorString,
81         HostUnreachableErrorString,
82         NetworkUnreachableErrorString,
83         AccessErrorString,
84         ConnectionTimeOutErrorString,
85         ConnectionRefusedErrorString,
86         AddressInuseErrorString,
87         AddressNotAvailableErrorString,
88         AddressProtectedErrorString,
89         DatagramTooLargeErrorString,
90         SendDatagramErrorString,
91         ReceiveDatagramErrorString,
92         WriteErrorString,
93         ReadErrorString,
94         PortInuseErrorString,
95         NotSocketErrorString,
96         InvalidProxyTypeString,
97         TemporaryErrorString,
98 
99         UnknownSocketErrorString = -1
100     };
101 }
102 
103 class QNativeSocketEnginePrivate;
104 class SocketEngineWorker;
105 
106 struct WinRtDatagram {
107     QByteArray data;
108     QIpPacketHeader header;
109 };
110 
111 class Q_AUTOTEST_EXPORT QNativeSocketEngine : public QAbstractSocketEngine
112 {
113     Q_OBJECT
114 public:
115     QNativeSocketEngine(QObject *parent = 0);
116     ~QNativeSocketEngine();
117 
118     bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol);
119     bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState);
120 
121     qintptr socketDescriptor() const;
122 
123     bool isValid() const;
124 
125     bool connectToHost(const QHostAddress &address, quint16 port);
126     bool connectToHostByName(const QString &name, quint16 port);
127     bool bind(const QHostAddress &address, quint16 port);
128     bool listen();
129     int accept();
130     void close();
131 
132 #ifndef QT_NO_NETWORKINTERFACE
133     bool joinMulticastGroup(const QHostAddress &groupAddress,
134                             const QNetworkInterface &iface);
135     bool leaveMulticastGroup(const QHostAddress &groupAddress,
136                              const QNetworkInterface &iface);
137     QNetworkInterface multicastInterface() const;
138     bool setMulticastInterface(const QNetworkInterface &iface);
139 #endif
140 
141     qint64 bytesAvailable() const;
142 
143     qint64 read(char *data, qint64 maxlen);
144     qint64 write(const char *data, qint64 len);
145 
146     qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader * = 0, PacketHeaderOptions = WantNone);
147     qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header);
148     bool hasPendingDatagrams() const;
149     qint64 pendingDatagramSize() const;
150 
151     qint64 bytesToWrite() const;
152 
153     qint64 receiveBufferSize() const;
154     void setReceiveBufferSize(qint64 bufferSize);
155 
156     qint64 sendBufferSize() const;
157     void setSendBufferSize(qint64 bufferSize);
158 
159     int option(SocketOption option) const;
160     bool setOption(SocketOption option, int value);
161 
162     bool waitForRead(int msecs = 30000, bool *timedOut = 0);
163     bool waitForWrite(int msecs = 30000, bool *timedOut = 0);
164     bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
165                             bool checkRead, bool checkWrite,
166                             int msecs = 30000, bool *timedOut = 0);
167 
168     bool isReadNotificationEnabled() const;
169     void setReadNotificationEnabled(bool enable);
170     bool isWriteNotificationEnabled() const;
171     void setWriteNotificationEnabled(bool enable);
172     bool isExceptionNotificationEnabled() const;
173     void setExceptionNotificationEnabled(bool enable);
174 
175 signals:
176     void connectionReady();
177     void readReady();
178     void writeReady();
179     void newDatagramReceived(const WinRtDatagram &datagram);
180 
181 private slots:
182     void establishRead();
183     void handleConnectOpFinished(bool success, QAbstractSocket::SocketError error,
184                                  WinRTSocketEngine::ErrorString errorString);
185     void handleNewData();
186     void handleTcpError(QAbstractSocket::SocketError error);
187     void processReadReady();
188 
189 private:
190     Q_DECLARE_PRIVATE(QNativeSocketEngine)
191     Q_DISABLE_COPY_MOVE(QNativeSocketEngine)
192 };
193 
194 class QNativeSocketEnginePrivate : public QAbstractSocketEnginePrivate
195 {
196     Q_DECLARE_PUBLIC(QNativeSocketEngine)
197 public:
198     QNativeSocketEnginePrivate();
199     ~QNativeSocketEnginePrivate();
200 
201     qintptr socketDescriptor;
202     SocketEngineWorker *worker;
203 
204     bool notifyOnRead, notifyOnWrite, notifyOnException;
205     QAtomicInt closingDown;
206 
207     void setError(QAbstractSocket::SocketError error, WinRTSocketEngine::ErrorString errorString) const;
208 
209     // native functions
210     int option(QNativeSocketEngine::SocketOption option) const;
211     bool setOption(QNativeSocketEngine::SocketOption option, int value);
212 
213     bool createNewSocket(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol &protocol);
214 
215     bool checkProxy(const QHostAddress &address);
216     bool fetchConnectionParameters();
217 
218 private:
tcpSocket()219     inline ABI::Windows::Networking::Sockets::IStreamSocket *tcpSocket() const
220         { return reinterpret_cast<ABI::Windows::Networking::Sockets::IStreamSocket *>(socketDescriptor); }
udpSocket()221     inline ABI::Windows::Networking::Sockets::IDatagramSocket *udpSocket() const
222         { return reinterpret_cast<ABI::Windows::Networking::Sockets::IDatagramSocket *>(socketDescriptor); }
223     Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocketListener> tcpListener;
224 
225     QList<ABI::Windows::Networking::Sockets::IStreamSocket *> pendingConnections;
226     QList<ABI::Windows::Networking::Sockets::IStreamSocket *> currentConnections;
227     QEventLoop eventLoop;
228     QAbstractSocket *sslSocket;
229     EventRegistrationToken connectionToken;
230 
231     bool emitReadReady = true;
232     bool pendingReadNotification = false;
233 
234     HRESULT handleClientConnection(ABI::Windows::Networking::Sockets::IStreamSocketListener *tcpListener,
235                                    ABI::Windows::Networking::Sockets::IStreamSocketListenerConnectionReceivedEventArgs *args);
236 };
237 
238 QT_END_NAMESPACE
239 
240 Q_DECLARE_METATYPE(WinRtDatagram)
241 Q_DECLARE_METATYPE(WinRTSocketEngine::ErrorString)
242 
243 #endif // QNATIVESOCKETENGINE_WINRT_P_H
244