1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QABSTRACTSOCKET_P_H
6#define QABSTRACTSOCKET_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of the QAbstractSocket class. This header file may change from
14// version to version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20#include "QtNetwork/qabstractsocket.h"
21#include "QtCore/qbytearray.h"
22#include "QtCore/qlist.h"
23#include "QtCore/qtimer.h"
24#include "private/qiodevice_p.h"
25#include "private/qabstractsocketengine_p.h"
26#include "qnetworkproxy.h"
27
28QT_BEGIN_NAMESPACE
29
30class QHostInfo;
31
32class QAbstractSocketPrivate : public QIODevicePrivate, public QAbstractSocketEngineReceiver
33{
34 Q_DECLARE_PUBLIC(QAbstractSocket)
35public:
36 QAbstractSocketPrivate(decltype(QObjectPrivateVersion) version = QObjectPrivateVersion);
37 virtual ~QAbstractSocketPrivate();
38
39 // from QAbstractSocketEngineReceiver
40 inline void readNotification() override { canReadNotification(); }
41 inline void writeNotification() override { canWriteNotification(); }
42 inline void exceptionNotification() override {}
43 inline void closeNotification() override { canCloseNotification(); }
44 void connectionNotification() override;
45#ifndef QT_NO_NETWORKPROXY
46 inline void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) override {
47 Q_Q(QAbstractSocket);
48 emit q->proxyAuthenticationRequired(proxy, authenticator);
49 }
50#endif
51
52 virtual bool bind(const QHostAddress &address, quint16 port, QAbstractSocket::BindMode mode);
53
54 virtual bool canReadNotification();
55 bool canWriteNotification();
56 void canCloseNotification();
57
58 // slots
59 void _q_connectToNextAddress();
60 void _q_startConnecting(const QHostInfo &hostInfo);
61 void _q_testConnection();
62 void _q_abortConnectionAttempt();
63
64 bool emittedReadyRead = false;
65 bool emittedBytesWritten = false;
66
67 bool abortCalled = false;
68 bool pendingClose = false;
69
70 QAbstractSocket::PauseModes pauseMode = QAbstractSocket::PauseNever;
71
72 QString hostName;
73 quint16 port = 0;
74 QHostAddress host;
75 QList<QHostAddress> addresses;
76
77 quint16 localPort = 0;
78 quint16 peerPort = 0;
79 QHostAddress localAddress;
80 QHostAddress peerAddress;
81 QString peerName;
82
83 QAbstractSocketEngine *socketEngine = nullptr;
84 qintptr cachedSocketDescriptor = -1;
85
86#ifndef QT_NO_NETWORKPROXY
87 QNetworkProxy proxy;
88 QNetworkProxy proxyInUse;
89 QString protocolTag;
90 void resolveProxy(const QString &hostName, quint16 port);
91#else
92 inline void resolveProxy(const QString &, quint16) { }
93#endif
94 inline void resolveProxy(quint16 port) { resolveProxy(hostName: QString(), port); }
95
96 void resetSocketLayer();
97 virtual bool flush();
98
99 bool initSocketLayer(QAbstractSocket::NetworkLayerProtocol protocol);
100 virtual void configureCreatedSocket();
101 void startConnectingByName(const QString &host);
102 void fetchConnectionParameters();
103 bool readFromSocket();
104 virtual bool writeToSocket();
105 void emitReadyRead(int channel = 0);
106 void emitBytesWritten(qint64 bytes, int channel = 0);
107
108 void setError(QAbstractSocket::SocketError errorCode, const QString &errorString);
109 void setErrorAndEmit(QAbstractSocket::SocketError errorCode, const QString &errorString);
110
111 qint64 readBufferMaxSize = 0;
112 bool isBuffered = false;
113 bool hasPendingData = false;
114 bool hasPendingDatagram = false;
115
116 QTimer *connectTimer = nullptr;
117
118 int hostLookupId = -1;
119
120 QAbstractSocket::SocketType socketType = QAbstractSocket::UnknownSocketType;
121 QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState;
122
123 // Must be kept in sync with QIODevicePrivate::errorString.
124 QAbstractSocket::SocketError socketError = QAbstractSocket::UnknownSocketError;
125
126 QAbstractSocket::NetworkLayerProtocol preferredNetworkLayerProtocol =
127 QAbstractSocket::UnknownNetworkLayerProtocol;
128
129 bool prePauseReadSocketNotifierState = false;
130 bool prePauseWriteSocketNotifierState = false;
131 bool prePauseExceptionSocketNotifierState = false;
132 static void pauseSocketNotifiers(QAbstractSocket*);
133 static void resumeSocketNotifiers(QAbstractSocket*);
134 static QAbstractSocketEngine* getSocketEngine(QAbstractSocket*);
135};
136
137QT_END_NAMESPACE
138
139#endif // QABSTRACTSOCKET_P_H
140

source code of qtbase/src/network/socket/qabstractsocket_p.h