Coverage Report

Created: 2026-03-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/network/socket/qabstractsocketengine.cpp
Line
Count
Source
1
// Copyright (C) 2016 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
#include "qabstractsocketengine_p.h"
6
7
#include "qnativesocketengine_p.h"
8
9
#include "qmutex.h"
10
#include "qnetworkproxy.h"
11
12
QT_BEGIN_NAMESPACE
13
14
0
QAbstractSocketEngineReceiver::~QAbstractSocketEngineReceiver() = default;
15
16
class QSocketEngineHandlerList : public QList<QSocketEngineHandler*>
17
{
18
public:
19
    QMutex mutex;
20
};
21
22
Q_GLOBAL_STATIC(QSocketEngineHandlerList, socketHandlers)
23
24
QSocketEngineHandler::QSocketEngineHandler()
25
0
{
26
0
    if (!socketHandlers())
27
0
        return;
28
0
    QMutexLocker locker(&socketHandlers()->mutex);
29
0
    socketHandlers()->prepend(this);
30
0
}
31
32
QSocketEngineHandler::~QSocketEngineHandler()
33
0
{
34
0
    if (!socketHandlers())
35
0
        return;
36
0
    QMutexLocker locker(&socketHandlers()->mutex);
37
0
    socketHandlers()->removeAll(this);
38
0
}
39
40
QAbstractSocketEnginePrivate::QAbstractSocketEnginePrivate()
41
0
    : socketError(QAbstractSocket::UnknownSocketError)
42
0
    , hasSetSocketError(false)
43
0
    , socketErrorString(QLatin1StringView(QT_TRANSLATE_NOOP(QSocketLayer, "Unknown error")))
44
0
    , socketState(QAbstractSocket::UnconnectedState)
45
0
    , socketType(QAbstractSocket::UnknownSocketType)
46
0
    , socketProtocol(QAbstractSocket::UnknownNetworkLayerProtocol)
47
0
    , localPort(0)
48
0
    , peerPort(0)
49
0
    , inboundStreamCount(0)
50
0
    , outboundStreamCount(0)
51
0
    , receiver(nullptr)
52
0
{
53
0
}
54
55
QAbstractSocketEngine::QAbstractSocketEngine(QObject *parent)
56
0
    : QObject(*new QAbstractSocketEnginePrivate(), parent)
57
0
{
58
0
}
59
60
QAbstractSocketEngine::QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent)
61
0
    : QObject(dd, parent)
62
0
{
63
0
}
64
65
QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &proxy, QObject *parent)
66
0
{
67
0
#ifndef QT_NO_NETWORKPROXY
68
    // proxy type must have been resolved by now
69
0
    if (proxy.type() == QNetworkProxy::DefaultProxy)
70
0
        return nullptr;
71
0
#endif
72
73
0
    QMutexLocker locker(&socketHandlers()->mutex);
74
0
    for (int i = 0; i < socketHandlers()->size(); i++) {
75
0
        if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketType, proxy, parent))
76
0
            return ret;
77
0
    }
78
79
0
#ifndef QT_NO_NETWORKPROXY
80
    // only NoProxy can have reached here
81
0
    if (proxy.type() != QNetworkProxy::NoProxy)
82
0
        return nullptr;
83
0
#endif
84
85
0
    return new QNativeSocketEngine(parent);
86
0
}
87
88
QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(qintptr socketDescripter, QObject *parent)
89
0
{
90
0
    QMutexLocker locker(&socketHandlers()->mutex);
91
0
    for (int i = 0; i < socketHandlers()->size(); i++) {
92
0
        if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketDescripter, parent))
93
0
            return ret;
94
0
    }
95
0
    return new QNativeSocketEngine(parent);
96
0
}
97
98
QAbstractSocket::SocketError QAbstractSocketEngine::error() const
99
0
{
100
0
    return d_func()->socketError;
101
0
}
102
103
QString QAbstractSocketEngine::errorString() const
104
0
{
105
0
    return d_func()->socketErrorString;
106
0
}
107
108
void QAbstractSocketEngine::setError(QAbstractSocket::SocketError error, const QString &errorString) const
109
0
{
110
0
    Q_D(const QAbstractSocketEngine);
111
0
    d->socketError = error;
112
0
    d->socketErrorString = errorString;
113
0
}
114
115
void QAbstractSocketEngine::setReceiver(QAbstractSocketEngineReceiver *receiver)
116
0
{
117
0
    d_func()->receiver = receiver;
118
0
}
119
120
void QAbstractSocketEngine::readNotification()
121
0
{
122
0
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
123
0
        receiver->readNotification();
124
0
}
125
126
void QAbstractSocketEngine::writeNotification()
127
0
{
128
0
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
129
0
        receiver->writeNotification();
130
0
}
131
132
void QAbstractSocketEngine::exceptionNotification()
133
0
{
134
0
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
135
0
        receiver->exceptionNotification();
136
0
}
137
138
void QAbstractSocketEngine::closeNotification()
139
0
{
140
0
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
141
0
        receiver->closeNotification();
142
0
}
143
144
void QAbstractSocketEngine::connectionNotification()
145
0
{
146
0
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
147
0
        receiver->connectionNotification();
148
0
}
149
150
#ifndef QT_NO_NETWORKPROXY
151
void QAbstractSocketEngine::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
152
0
{
153
0
    if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
154
0
        receiver->proxyAuthenticationRequired(proxy, authenticator);
155
0
}
156
#endif
157
158
159
QAbstractSocket::SocketState QAbstractSocketEngine::state() const
160
0
{
161
0
    return d_func()->socketState;
162
0
}
163
164
void QAbstractSocketEngine::setState(QAbstractSocket::SocketState state)
165
0
{
166
0
    d_func()->socketState = state;
167
0
}
168
169
QAbstractSocket::SocketType QAbstractSocketEngine::socketType() const
170
0
{
171
0
    return d_func()->socketType;
172
0
}
173
174
void QAbstractSocketEngine::setSocketType(QAbstractSocket::SocketType socketType)
175
0
{
176
0
    d_func()->socketType = socketType;
177
0
}
178
179
QAbstractSocket::NetworkLayerProtocol QAbstractSocketEngine::protocol() const
180
0
{
181
0
    return d_func()->socketProtocol;
182
0
}
183
184
void QAbstractSocketEngine::setProtocol(QAbstractSocket::NetworkLayerProtocol protocol)
185
0
{
186
0
    d_func()->socketProtocol = protocol;
187
0
}
188
189
QHostAddress QAbstractSocketEngine::localAddress() const
190
0
{
191
0
    return d_func()->localAddress;
192
0
}
193
194
void QAbstractSocketEngine::setLocalAddress(const QHostAddress &address)
195
0
{
196
0
    d_func()->localAddress = address;
197
0
}
198
199
quint16 QAbstractSocketEngine::localPort() const
200
0
{
201
0
    return d_func()->localPort;
202
0
}
203
204
void QAbstractSocketEngine::setLocalPort(quint16 port)
205
0
{
206
0
    d_func()->localPort = port;
207
0
}
208
209
QHostAddress QAbstractSocketEngine::peerAddress() const
210
0
{
211
0
    return d_func()->peerAddress;
212
0
}
213
214
void QAbstractSocketEngine::setPeerAddress(const QHostAddress &address)
215
0
{
216
0
   d_func()->peerAddress = address;
217
0
}
218
219
quint16 QAbstractSocketEngine::peerPort() const
220
0
{
221
0
    return d_func()->peerPort;
222
0
}
223
224
void QAbstractSocketEngine::setPeerPort(quint16 port)
225
0
{
226
0
    d_func()->peerPort = port;
227
0
}
228
229
int QAbstractSocketEngine::inboundStreamCount() const
230
0
{
231
0
    return d_func()->inboundStreamCount;
232
0
}
233
234
int QAbstractSocketEngine::outboundStreamCount() const
235
0
{
236
0
    return d_func()->outboundStreamCount;
237
0
}
238
239
QT_END_NAMESPACE
240
241
#include "moc_qabstractsocketengine_p.cpp"