Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/socket/qnativesocketengine_unix.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
6 | ** | - | ||||||||||||
7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
8 | ** | - | ||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
17 | ** | - | ||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
36 | ** | - | ||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||
38 | ** | - | ||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||
40 | - | |||||||||||||
41 | //#define QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
42 | #include "qnativesocketengine_p.h" | - | ||||||||||||
43 | #include "private/qnet_unix_p.h" | - | ||||||||||||
44 | #include "qiodevice.h" | - | ||||||||||||
45 | #include "qhostaddress.h" | - | ||||||||||||
46 | #include "qelapsedtimer.h" | - | ||||||||||||
47 | #include "qvarlengtharray.h" | - | ||||||||||||
48 | #include "qnetworkinterface.h" | - | ||||||||||||
49 | #include <time.h> | - | ||||||||||||
50 | #include <errno.h> | - | ||||||||||||
51 | #include <fcntl.h> | - | ||||||||||||
52 | #ifndef QT_NO_IPV6IFNAME | - | ||||||||||||
53 | #include <net/if.h> | - | ||||||||||||
54 | #endif | - | ||||||||||||
55 | #ifdef QT_LINUXBASE | - | ||||||||||||
56 | #include <arpa/inet.h> | - | ||||||||||||
57 | #endif | - | ||||||||||||
58 | #ifdef Q_OS_BSD4 | - | ||||||||||||
59 | #include <net/if_dl.h> | - | ||||||||||||
60 | #endif | - | ||||||||||||
61 | #ifdef Q_OS_INTEGRITY | - | ||||||||||||
62 | #include <sys/uio.h> | - | ||||||||||||
63 | #endif | - | ||||||||||||
64 | - | |||||||||||||
65 | #if defined QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
66 | #include <qstring.h> | - | ||||||||||||
67 | #include <ctype.h> | - | ||||||||||||
68 | #endif | - | ||||||||||||
69 | - | |||||||||||||
70 | #include <netinet/tcp.h> | - | ||||||||||||
71 | - | |||||||||||||
72 | QT_BEGIN_NAMESPACE | - | ||||||||||||
73 | - | |||||||||||||
74 | #if defined QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
75 | - | |||||||||||||
76 | /* | - | ||||||||||||
77 | Returns a human readable representation of the first \a len | - | ||||||||||||
78 | characters in \a data. | - | ||||||||||||
79 | */ | - | ||||||||||||
80 | static QByteArray qt_prettyDebug(const char *data, int len, int maxSize) | - | ||||||||||||
81 | { | - | ||||||||||||
82 | if (!data) return "(null)"; | - | ||||||||||||
83 | QByteArray out; | - | ||||||||||||
84 | for (int i = 0; i < len; ++i) { | - | ||||||||||||
85 | char c = data[i]; | - | ||||||||||||
86 | if (isprint(c)) { | - | ||||||||||||
87 | out += c; | - | ||||||||||||
88 | } else switch (c) { | - | ||||||||||||
89 | case '\n': out += "\\n"; break; | - | ||||||||||||
90 | case '\r': out += "\\r"; break; | - | ||||||||||||
91 | case '\t': out += "\\t"; break; | - | ||||||||||||
92 | default: | - | ||||||||||||
93 | QString tmp; | - | ||||||||||||
94 | tmp.sprintf("\\%o", c); | - | ||||||||||||
95 | out += tmp.toLatin1(); | - | ||||||||||||
96 | } | - | ||||||||||||
97 | } | - | ||||||||||||
98 | - | |||||||||||||
99 | if (len < maxSize) | - | ||||||||||||
100 | out += "..."; | - | ||||||||||||
101 | - | |||||||||||||
102 | return out; | - | ||||||||||||
103 | } | - | ||||||||||||
104 | #endif | - | ||||||||||||
105 | - | |||||||||||||
106 | /* | - | ||||||||||||
107 | Extracts the port and address from a sockaddr, and stores them in | - | ||||||||||||
108 | \a port and \a addr if they are non-null. | - | ||||||||||||
109 | */ | - | ||||||||||||
110 | static inline void qt_socket_getPortAndAddress(const qt_sockaddr *s, quint16 *port, QHostAddress *addr) | - | ||||||||||||
111 | { | - | ||||||||||||
112 | if (s->a.sa_family == AF_INET6) {
| 1789-8249 | ||||||||||||
113 | Q_IPV6ADDR tmp; | - | ||||||||||||
114 | memcpy(&tmp, &s->a6.sin6_addr, sizeof(tmp)); | - | ||||||||||||
115 | if (addr) {
| 0-1789 | ||||||||||||
116 | QHostAddress tmpAddress; | - | ||||||||||||
117 | tmpAddress.setAddress(tmp); | - | ||||||||||||
118 | *addr = tmpAddress; | - | ||||||||||||
119 | if (s->a6.sin6_scope_id)
| 30-1759 | ||||||||||||
120 | { executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| 30 | ||||||||||||
#ifndef QT_NO_IPV6IFNAME executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| ||||||||||||||
char scopeid[IFNAMSIZ]; executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| ||||||||||||||
if (::if_indextoname(s->a6.sin6_scope_id, scopeid)) { executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| ||||||||||||||
addr->setScopeId(QLatin1String(scopeid)); executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| ||||||||||||||
} else executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| ||||||||||||||
#endif executed 30 times by 2 tests: addr->setScopeId(QStringQNetworkInterface::numberinterfaceNameFromIndex(s->a6.sin6_scope_id));addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
executed 30 times by 2 tests: addr->setScopeId(QNetworkInterface::interfaceNameFromIndex(s->a6.sin6_scope_id)); Executed by:
| ||||||||||||||
121 | }} executed 1789 times by 14 tests: end of block Executed by:
| 1789 | ||||||||||||
122 | if (port)
| 0-1789 | ||||||||||||
123 | *port = ntohs(s->a6.sin6_port); executed 1789 times by 14 tests: *port = ntohs(s->a6.sin6_port); Executed by:
| 1789 | ||||||||||||
124 | return; executed 1789 times by 14 tests: return; Executed by:
| 1789 | ||||||||||||
125 | } | - | ||||||||||||
126 | - | |||||||||||||
127 | if (port)
| 0-8249 | ||||||||||||
128 | *port = ntohs(s->a4.sin_port); executed 8249 times by 30 tests: *port = ntohs(s->a4.sin_port); Executed by:
| 8249 | ||||||||||||
129 | if (addr) {
| 0-8249 | ||||||||||||
130 | QHostAddress tmpAddress; | - | ||||||||||||
131 | tmpAddress.setAddress(ntohl(s->a4.sin_addr.s_addr)); | - | ||||||||||||
132 | *addr = tmpAddress; | - | ||||||||||||
133 | }} executed 8249 times by 30 tests: end of block Executed by:
| 8249 | ||||||||||||
inline uint QNativeSocketEnginePrivate::scopeIdFromString(const QString &scopeid) executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
{ executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
if (scopeid.isEmpty()) executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
return 0; executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
bool ok; executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
uint id = scopeid.toUInt(&ok); executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
#ifndef QT_NO_IPV6IFNAME executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
if (!ok) executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
id = ::if_nametoindex(scopeid.toLatin1()); executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
#endif executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
return id; executed 8249 times by 30 tests: }end of block Executed by:
executed 8249 times by 30 tests: end of block Executed by:
| ||||||||||||||
135 | - | |||||||||||||
136 | static void convertToLevelAndOption(QNativeSocketEngine::SocketOption opt, | - | ||||||||||||
137 | QAbstractSocket::NetworkLayerProtocol socketProtocol, int &level, int &n) | - | ||||||||||||
138 | { | - | ||||||||||||
139 | n = -1; | - | ||||||||||||
140 | level = SOL_SOCKET; // default | - | ||||||||||||
141 | - | |||||||||||||
142 | switch (opt) { | - | ||||||||||||
143 | case QNativeSocketEngine::NonBlockingSocketOption: // fcntl, not setsockopt | - | ||||||||||||
144 | case QNativeSocketEngine::BindExclusively: // not handled on Unix | - | ||||||||||||
145 | Q_UNREACHABLE(); | - | ||||||||||||
146 | - | |||||||||||||
147 | case QNativeSocketEngine::BroadcastSocketOption: | - | ||||||||||||
148 | n = SO_BROADCAST; | - | ||||||||||||
149 | break; | - | ||||||||||||
150 | case QNativeSocketEngine::ReceiveBufferSocketOption: | - | ||||||||||||
151 | n = SO_RCVBUF; | - | ||||||||||||
152 | break; | - | ||||||||||||
153 | case QNativeSocketEngine::SendBufferSocketOption: | - | ||||||||||||
154 | n = SO_SNDBUF; | - | ||||||||||||
155 | break; | - | ||||||||||||
156 | case QNativeSocketEngine::AddressReusable: | - | ||||||||||||
157 | n = SO_REUSEADDR; | - | ||||||||||||
158 | break; | - | ||||||||||||
159 | case QNativeSocketEngine::ReceiveOutOfBandData: | - | ||||||||||||
160 | n = SO_OOBINLINE; | - | ||||||||||||
161 | break; | - | ||||||||||||
162 | case QNativeSocketEngine::LowDelayOption: | - | ||||||||||||
163 | level = IPPROTO_TCP; | - | ||||||||||||
164 | n = TCP_NODELAY; | - | ||||||||||||
165 | break; | - | ||||||||||||
166 | case QNativeSocketEngine::KeepAliveOption: | - | ||||||||||||
167 | n = SO_KEEPALIVE; | - | ||||||||||||
168 | break; | - | ||||||||||||
169 | case QNativeSocketEngine::MulticastTtlOption: | - | ||||||||||||
170 | if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
171 | level = IPPROTO_IPV6; | - | ||||||||||||
172 | n = IPV6_MULTICAST_HOPS; | - | ||||||||||||
173 | } else | - | ||||||||||||
174 | { | - | ||||||||||||
175 | level = IPPROTO_IP; | - | ||||||||||||
176 | n = IP_MULTICAST_TTL; | - | ||||||||||||
177 | } | - | ||||||||||||
178 | break; | - | ||||||||||||
179 | case QNativeSocketEngine::MulticastLoopbackOption: | - | ||||||||||||
180 | if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
181 | level = IPPROTO_IPV6; | - | ||||||||||||
182 | n = IPV6_MULTICAST_LOOP; | - | ||||||||||||
183 | } else | - | ||||||||||||
184 | { | - | ||||||||||||
185 | level = IPPROTO_IP; | - | ||||||||||||
186 | n = IP_MULTICAST_LOOP; | - | ||||||||||||
187 | } | - | ||||||||||||
188 | break; | - | ||||||||||||
189 | case QNativeSocketEngine::TypeOfServiceOption: | - | ||||||||||||
190 | if (socketProtocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
191 | level = IPPROTO_IP; | - | ||||||||||||
192 | n = IP_TOS; | - | ||||||||||||
193 | } | - | ||||||||||||
194 | break; | - | ||||||||||||
195 | case QNativeSocketEngine::ReceivePacketInformation: | - | ||||||||||||
196 | if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
197 | level = IPPROTO_IPV6; | - | ||||||||||||
198 | n = IPV6_RECVPKTINFO; | - | ||||||||||||
199 | } else if (socketProtocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
200 | level = IPPROTO_IP; | - | ||||||||||||
201 | #ifdef IP_PKTINFO | - | ||||||||||||
202 | n = IP_PKTINFO; | - | ||||||||||||
203 | #elif defined(IP_RECVDSTADDR) | - | ||||||||||||
204 | // variant found in QNX and FreeBSD; it will get us only the | - | ||||||||||||
205 | // destination address, not the interface; we need IP_RECVIF for that. | - | ||||||||||||
206 | n = IP_RECVDSTADDR; | - | ||||||||||||
207 | #endif | - | ||||||||||||
208 | } | - | ||||||||||||
209 | break; | - | ||||||||||||
210 | case QNativeSocketEngine::ReceiveHopLimit: | - | ||||||||||||
211 | if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
212 | level = IPPROTO_IPV6; | - | ||||||||||||
213 | n = IPV6_RECVHOPLIMIT; | - | ||||||||||||
214 | } else if (socketProtocol == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
215 | #ifdef IP_RECVTTL // IP_RECVTTL is a non-standard extension supported on some OS | - | ||||||||||||
216 | level = IPPROTO_IP; | - | ||||||||||||
217 | n = IP_RECVTTL; | - | ||||||||||||
218 | #endif | - | ||||||||||||
219 | } | - | ||||||||||||
220 | break; | - | ||||||||||||
221 | } | - | ||||||||||||
222 | } | - | ||||||||||||
223 | - | |||||||||||||
224 | /*! \internal | - | ||||||||||||
225 | - | |||||||||||||
226 | Creates and returns a new socket descriptor of type \a socketType | - | ||||||||||||
227 | and \a socketProtocol. Returns -1 on failure. | - | ||||||||||||
228 | */ | - | ||||||||||||
229 | bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType socketType, | - | ||||||||||||
230 | QAbstractSocket::NetworkLayerProtocol &socketProtocol) | - | ||||||||||||
231 | { | - | ||||||||||||
232 | int protocol = (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) ? AF_INET6 : AF_INET; | - | ||||||||||||
233 | int type = (socketType == QAbstractSocket::UdpSocket) ? SOCK_DGRAM : SOCK_STREAM; | - | ||||||||||||
234 | - | |||||||||||||
235 | int socket = qt_safe_socket(protocol, type, 0, O_NONBLOCK); | - | ||||||||||||
236 | if (socket < 0 && socketProtocol == QAbstractSocket::AnyIPProtocol && errno == EAFNOSUPPORT) { | - | ||||||||||||
237 | protocol = AF_INET; | - | ||||||||||||
238 | socket = qt_safe_socket(protocol, type, 0, O_NONBLOCK); | - | ||||||||||||
239 | socketProtocol = QAbstractSocket::IPv4Protocol; | - | ||||||||||||
240 | } | - | ||||||||||||
241 | - | |||||||||||||
242 | if (socket < 0) { | - | ||||||||||||
243 | int ecopy = errno; | - | ||||||||||||
244 | switch (ecopy) { | - | ||||||||||||
245 | case EPROTONOSUPPORT: | - | ||||||||||||
246 | case EAFNOSUPPORT: | - | ||||||||||||
247 | case EINVAL: | - | ||||||||||||
248 | setError(QAbstractSocket::UnsupportedSocketOperationError, ProtocolUnsupportedErrorString); | - | ||||||||||||
249 | break; | - | ||||||||||||
250 | case ENFILE: | - | ||||||||||||
251 | case EMFILE: | - | ||||||||||||
252 | case ENOBUFS: | - | ||||||||||||
253 | case ENOMEM: | - | ||||||||||||
254 | setError(QAbstractSocket::SocketResourceError, ResourceErrorString); | - | ||||||||||||
255 | break; | - | ||||||||||||
256 | case EACCES: | - | ||||||||||||
257 | setError(QAbstractSocket::SocketAccessError, AccessErrorString); | - | ||||||||||||
258 | break; | - | ||||||||||||
259 | default: | - | ||||||||||||
260 | break; | - | ||||||||||||
261 | } | - | ||||||||||||
262 | - | |||||||||||||
263 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
264 | qDebug("QNativeSocketEnginePrivate::createNewSocket(%d, %d) == false (%s)", | - | ||||||||||||
265 | socketType, socketProtocol, | - | ||||||||||||
266 | strerror(ecopy)); | - | ||||||||||||
267 | #endif | - | ||||||||||||
268 | - | |||||||||||||
269 | return false; | - | ||||||||||||
270 | } | - | ||||||||||||
271 | - | |||||||||||||
272 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
273 | qDebug("QNativeSocketEnginePrivate::createNewSocket(%d, %d) == true", | - | ||||||||||||
274 | socketType, socketProtocol); | - | ||||||||||||
275 | #endif | - | ||||||||||||
276 | - | |||||||||||||
277 | socketDescriptor = socket; | - | ||||||||||||
278 | if (socket != -1) { | - | ||||||||||||
279 | this->socketProtocol = socketProtocol; | - | ||||||||||||
280 | this->socketType = socketType; | - | ||||||||||||
281 | } | - | ||||||||||||
282 | return true; | - | ||||||||||||
283 | } | - | ||||||||||||
284 | - | |||||||||||||
285 | /* | - | ||||||||||||
286 | Returns the value of the socket option \a opt. | - | ||||||||||||
287 | */ | - | ||||||||||||
288 | int QNativeSocketEnginePrivate::option(QNativeSocketEngine::SocketOption opt) const | - | ||||||||||||
289 | { | - | ||||||||||||
290 | Q_Q(const QNativeSocketEngine); | - | ||||||||||||
291 | if (!q->isValid()) | - | ||||||||||||
292 | return -1; | - | ||||||||||||
293 | - | |||||||||||||
294 | // handle non-getsockopt cases first | - | ||||||||||||
295 | if (opt == QNativeSocketEngine::BindExclusively || opt == QNativeSocketEngine::NonBlockingSocketOption | - | ||||||||||||
296 | || opt == QNativeSocketEngine::BroadcastSocketOption) | - | ||||||||||||
297 | return -1; | - | ||||||||||||
298 | - | |||||||||||||
299 | int n, level; | - | ||||||||||||
300 | int v = -1; | - | ||||||||||||
301 | QT_SOCKOPTLEN_T len = sizeof(v); | - | ||||||||||||
302 | - | |||||||||||||
303 | convertToLevelAndOption(opt, socketProtocol, level, n); | - | ||||||||||||
304 | if (n != -1 && ::getsockopt(socketDescriptor, level, n, (char *) &v, &len) != -1) | - | ||||||||||||
305 | return v; | - | ||||||||||||
306 | - | |||||||||||||
307 | return -1; | - | ||||||||||||
308 | } | - | ||||||||||||
309 | - | |||||||||||||
310 | - | |||||||||||||
311 | /* | - | ||||||||||||
312 | Sets the socket option \a opt to \a v. | - | ||||||||||||
313 | */ | - | ||||||||||||
314 | bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt, int v) | - | ||||||||||||
315 | { | - | ||||||||||||
316 | Q_Q(QNativeSocketEngine); | - | ||||||||||||
317 | if (!q->isValid()) | - | ||||||||||||
318 | return false; | - | ||||||||||||
319 | - | |||||||||||||
320 | // handle non-setsockopt cases first | - | ||||||||||||
321 | switch (opt) { | - | ||||||||||||
322 | case QNativeSocketEngine::NonBlockingSocketOption: { | - | ||||||||||||
323 | // Make the socket nonblocking. | - | ||||||||||||
324 | #if !defined(Q_OS_VXWORKS) | - | ||||||||||||
325 | int flags = ::fcntl(socketDescriptor, F_GETFL, 0); | - | ||||||||||||
326 | if (flags == -1) { | - | ||||||||||||
327 | #ifdef QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
328 | perror("QNativeSocketEnginePrivate::setOption(): fcntl(F_GETFL) failed"); | - | ||||||||||||
329 | #endif | - | ||||||||||||
330 | return false; | - | ||||||||||||
331 | } | - | ||||||||||||
332 | if (::fcntl(socketDescriptor, F_SETFL, flags | O_NONBLOCK) == -1) { | - | ||||||||||||
333 | #ifdef QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
334 | perror("QNativeSocketEnginePrivate::setOption(): fcntl(F_SETFL) failed"); | - | ||||||||||||
335 | #endif | - | ||||||||||||
336 | return false; | - | ||||||||||||
337 | } | - | ||||||||||||
338 | #else // Q_OS_VXWORKS | - | ||||||||||||
339 | int onoff = 1; | - | ||||||||||||
340 | - | |||||||||||||
341 | if (qt_safe_ioctl(socketDescriptor, FIONBIO, &onoff) < 0) { | - | ||||||||||||
342 | - | |||||||||||||
343 | #ifdef QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
344 | perror("QNativeSocketEnginePrivate::setOption(): ioctl(FIONBIO, 1) failed"); | - | ||||||||||||
345 | #endif | - | ||||||||||||
346 | return false; | - | ||||||||||||
347 | } | - | ||||||||||||
348 | #endif // Q_OS_VXWORKS | - | ||||||||||||
349 | return true; | - | ||||||||||||
350 | } | - | ||||||||||||
351 | case QNativeSocketEngine::BindExclusively: | - | ||||||||||||
352 | return true; | - | ||||||||||||
353 | - | |||||||||||||
354 | default: | - | ||||||||||||
355 | break; | - | ||||||||||||
356 | } | - | ||||||||||||
357 | - | |||||||||||||
358 | int n, level; | - | ||||||||||||
359 | convertToLevelAndOption(opt, socketProtocol, level, n); | - | ||||||||||||
360 | #if defined(SO_REUSEPORT) && !defined(Q_OS_LINUX) | - | ||||||||||||
361 | if (opt == QNativeSocketEngine::AddressReusable) { | - | ||||||||||||
362 | // on OS X, SO_REUSEADDR isn't sufficient to allow multiple binds to the | - | ||||||||||||
363 | // same port (which is useful for multicast UDP). SO_REUSEPORT is, but | - | ||||||||||||
364 | // we most definitely do not want to use this for TCP. See QTBUG-6305. | - | ||||||||||||
365 | if (socketType == QAbstractSocket::UdpSocket) | - | ||||||||||||
366 | n = SO_REUSEPORT; | - | ||||||||||||
367 | } | - | ||||||||||||
368 | #endif | - | ||||||||||||
369 | - | |||||||||||||
370 | return ::setsockopt(socketDescriptor, level, n, (char *) &v, sizeof(v)) == 0; | - | ||||||||||||
371 | } | - | ||||||||||||
372 | - | |||||||||||||
373 | bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port) | - | ||||||||||||
374 | { | - | ||||||||||||
375 | #ifdef QNATIVESOCKETENGINE_DEBUG | - | ||||||||||||
376 | qDebug() << "QNativeSocketEnginePrivate::nativeConnect() " << socketDescriptor; | - | ||||||||||||
377 | #endif | - | ||||||||||||
378 | - | |||||||||||||
379 | qt_sockaddr aa; | - | ||||||||||||
380 | QT_SOCKLEN_T sockAddrSize; | - | ||||||||||||
381 | setPortAndAddress(port, addr, &aa, &sockAddrSize); | - | ||||||||||||
382 | - | |||||||||||||
383 | int connectResult = qt_safe_connect(socketDescriptor, &aa.a, sockAddrSize); | - | ||||||||||||
384 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
385 | int ecopy = errno; | - | ||||||||||||
386 | #endif | - | ||||||||||||
387 | if (connectResult == -1) { | - | ||||||||||||
388 | switch (errno) { | - | ||||||||||||
389 | case EISCONN: | - | ||||||||||||
390 | socketState = QAbstractSocket::ConnectedState; | - | ||||||||||||
391 | break; | - | ||||||||||||
392 | case ECONNREFUSED: | - | ||||||||||||
393 | case EINVAL: | - | ||||||||||||
394 | setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString); | - | ||||||||||||
395 | socketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
396 | break; | - | ||||||||||||
397 | case ETIMEDOUT: | - | ||||||||||||
398 | setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString); | - | ||||||||||||
399 | break; | - | ||||||||||||
400 | case EHOSTUNREACH: | - | ||||||||||||
401 | setError(QAbstractSocket::NetworkError, HostUnreachableErrorString); | - | ||||||||||||
402 | socketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
403 | break; | - | ||||||||||||
404 | case ENETUNREACH: | - | ||||||||||||
405 | setError(QAbstractSocket::NetworkError, NetworkUnreachableErrorString); | - | ||||||||||||
406 | socketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
407 | break; | - | ||||||||||||
408 | case EADDRINUSE: | - | ||||||||||||
409 | setError(QAbstractSocket::NetworkError, AddressInuseErrorString); | - | ||||||||||||
410 | break; | - | ||||||||||||
411 | case EINPROGRESS: | - | ||||||||||||
412 | case EALREADY: | - | ||||||||||||
413 | setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); | - | ||||||||||||
414 | socketState = QAbstractSocket::ConnectingState; | - | ||||||||||||
415 | break; | - | ||||||||||||
416 | case EAGAIN: | - | ||||||||||||
417 | setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); | - | ||||||||||||
418 | break; | - | ||||||||||||
419 | case EACCES: | - | ||||||||||||
420 | case EPERM: | - | ||||||||||||
421 | setError(QAbstractSocket::SocketAccessError, AccessErrorString); | - | ||||||||||||
422 | socketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
423 | break; | - | ||||||||||||
424 | case EAFNOSUPPORT: | - | ||||||||||||
425 | case EBADF: | - | ||||||||||||
426 | case EFAULT: | - | ||||||||||||
427 | case ENOTSOCK: | - | ||||||||||||
428 | socketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
429 | default: | - | ||||||||||||
430 | break; | - | ||||||||||||
431 | } | - | ||||||||||||
432 | - | |||||||||||||
433 | if (socketState != QAbstractSocket::ConnectedState) { | - | ||||||||||||
434 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
435 | qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == false (%s)", | - | ||||||||||||
436 | addr.toString().toLatin1().constData(), port, | - | ||||||||||||
437 | socketState == QAbstractSocket::ConnectingState | - | ||||||||||||
438 | ? "Connection in progress" : strerror(ecopy)); | - | ||||||||||||
439 | #endif | - | ||||||||||||
440 | return false; | - | ||||||||||||
441 | } | - | ||||||||||||
442 | } | - | ||||||||||||
443 | - | |||||||||||||
444 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
445 | qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == true", | - | ||||||||||||
446 | addr.toString().toLatin1().constData(), port); | - | ||||||||||||
447 | #endif | - | ||||||||||||
448 | - | |||||||||||||
449 | socketState = QAbstractSocket::ConnectedState; | - | ||||||||||||
450 | return true; | - | ||||||||||||
451 | } | - | ||||||||||||
452 | - | |||||||||||||
453 | bool QNativeSocketEnginePrivate::nativeBind(const QHostAddress &address, quint16 port) | - | ||||||||||||
454 | { | - | ||||||||||||
455 | qt_sockaddr aa; | - | ||||||||||||
456 | QT_SOCKLEN_T sockAddrSize; | - | ||||||||||||
457 | setPortAndAddress(port, address, &aa, &sockAddrSize); | - | ||||||||||||
458 | - | |||||||||||||
459 | #ifdef IPV6_V6ONLY | - | ||||||||||||
460 | if (aa.a.sa_family == AF_INET6) { | - | ||||||||||||
461 | int ipv6only = 0; | - | ||||||||||||
462 | if (address.protocol() == QAbstractSocket::IPv6Protocol) | - | ||||||||||||
463 | ipv6only = 1; | - | ||||||||||||
464 | //default value of this socket option varies depending on unix variant (or system configuration on BSD), so always set it explicitly | - | ||||||||||||
465 | ::setsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6only, sizeof(ipv6only) ); | - | ||||||||||||
466 | } | - | ||||||||||||
467 | #endif | - | ||||||||||||
468 | - | |||||||||||||
469 | int bindResult = QT_SOCKET_BIND(socketDescriptor, &aa.a, sockAddrSize); | - | ||||||||||||
470 | if (bindResult < 0 && errno == EAFNOSUPPORT && address.protocol() == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
471 | // retry with v4 | - | ||||||||||||
472 | aa.a4.sin_family = AF_INET; | - | ||||||||||||
473 | aa.a4.sin_port = htons(port); | - | ||||||||||||
474 | aa.a4.sin_addr.s_addr = htonl(address.toIPv4Address()); | - | ||||||||||||
475 | sockAddrSize = sizeof(aa.a4); | - | ||||||||||||
476 | bindResult = QT_SOCKET_BIND(socketDescriptor, &aa.a, sockAddrSize); | - | ||||||||||||
477 | } | - | ||||||||||||
478 | - | |||||||||||||
479 | if (bindResult < 0) { | - | ||||||||||||
480 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
481 | int ecopy = errno; | - | ||||||||||||
482 | #endif | - | ||||||||||||
483 | switch(errno) { | - | ||||||||||||
484 | case EADDRINUSE: | - | ||||||||||||
485 | setError(QAbstractSocket::AddressInUseError, AddressInuseErrorString); | - | ||||||||||||
486 | break; | - | ||||||||||||
487 | case EACCES: | - | ||||||||||||
488 | setError(QAbstractSocket::SocketAccessError, AddressProtectedErrorString); | - | ||||||||||||
489 | break; | - | ||||||||||||
490 | case EINVAL: | - | ||||||||||||
491 | setError(QAbstractSocket::UnsupportedSocketOperationError, OperationUnsupportedErrorString); | - | ||||||||||||
492 | break; | - | ||||||||||||
493 | case EADDRNOTAVAIL: | - | ||||||||||||
494 | setError(QAbstractSocket::SocketAddressNotAvailableError, AddressNotAvailableErrorString); | - | ||||||||||||
495 | break; | - | ||||||||||||
496 | default: | - | ||||||||||||
497 | break; | - | ||||||||||||
498 | } | - | ||||||||||||
499 | - | |||||||||||||
500 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
501 | qDebug("QNativeSocketEnginePrivate::nativeBind(%s, %i) == false (%s)", | - | ||||||||||||
502 | address.toString().toLatin1().constData(), port, strerror(ecopy)); | - | ||||||||||||
503 | #endif | - | ||||||||||||
504 | - | |||||||||||||
505 | return false; | - | ||||||||||||
506 | } | - | ||||||||||||
507 | - | |||||||||||||
508 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
509 | qDebug("QNativeSocketEnginePrivate::nativeBind(%s, %i) == true", | - | ||||||||||||
510 | address.toString().toLatin1().constData(), port); | - | ||||||||||||
511 | #endif | - | ||||||||||||
512 | socketState = QAbstractSocket::BoundState; | - | ||||||||||||
513 | return true; | - | ||||||||||||
514 | } | - | ||||||||||||
515 | - | |||||||||||||
516 | bool QNativeSocketEnginePrivate::nativeListen(int backlog) | - | ||||||||||||
517 | { | - | ||||||||||||
518 | if (qt_safe_listen(socketDescriptor, backlog) < 0) { | - | ||||||||||||
519 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
520 | int ecopy = errno; | - | ||||||||||||
521 | #endif | - | ||||||||||||
522 | switch (errno) { | - | ||||||||||||
523 | case EADDRINUSE: | - | ||||||||||||
524 | setError(QAbstractSocket::AddressInUseError, | - | ||||||||||||
525 | PortInuseErrorString); | - | ||||||||||||
526 | break; | - | ||||||||||||
527 | default: | - | ||||||||||||
528 | break; | - | ||||||||||||
529 | } | - | ||||||||||||
530 | - | |||||||||||||
531 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
532 | qDebug("QNativeSocketEnginePrivate::nativeListen(%i) == false (%s)", | - | ||||||||||||
533 | backlog, strerror(ecopy)); | - | ||||||||||||
534 | #endif | - | ||||||||||||
535 | return false; | - | ||||||||||||
536 | } | - | ||||||||||||
537 | - | |||||||||||||
538 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
539 | qDebug("QNativeSocketEnginePrivate::nativeListen(%i) == true", backlog); | - | ||||||||||||
540 | #endif | - | ||||||||||||
541 | - | |||||||||||||
542 | socketState = QAbstractSocket::ListeningState; | - | ||||||||||||
543 | return true; | - | ||||||||||||
544 | } | - | ||||||||||||
545 | - | |||||||||||||
546 | int QNativeSocketEnginePrivate::nativeAccept() | - | ||||||||||||
547 | { | - | ||||||||||||
548 | int acceptedDescriptor = qt_safe_accept(socketDescriptor, 0, 0); | - | ||||||||||||
549 | if (acceptedDescriptor == -1) { | - | ||||||||||||
550 | switch (errno) { | - | ||||||||||||
551 | case EBADF: | - | ||||||||||||
552 | case EOPNOTSUPP: | - | ||||||||||||
553 | setError(QAbstractSocket::UnsupportedSocketOperationError, InvalidSocketErrorString); | - | ||||||||||||
554 | break; | - | ||||||||||||
555 | case ECONNABORTED: | - | ||||||||||||
556 | setError(QAbstractSocket::NetworkError, RemoteHostClosedErrorString); | - | ||||||||||||
557 | break; | - | ||||||||||||
558 | case EFAULT: | - | ||||||||||||
559 | case ENOTSOCK: | - | ||||||||||||
560 | setError(QAbstractSocket::SocketResourceError, NotSocketErrorString); | - | ||||||||||||
561 | break; | - | ||||||||||||
562 | case EPROTONOSUPPORT: | - | ||||||||||||
563 | #if !defined(Q_OS_OPENBSD) | - | ||||||||||||
564 | case EPROTO: | - | ||||||||||||
565 | #endif | - | ||||||||||||
566 | case EAFNOSUPPORT: | - | ||||||||||||
567 | case EINVAL: | - | ||||||||||||
568 | setError(QAbstractSocket::UnsupportedSocketOperationError, ProtocolUnsupportedErrorString); | - | ||||||||||||
569 | break; | - | ||||||||||||
570 | case ENFILE: | - | ||||||||||||
571 | case EMFILE: | - | ||||||||||||
572 | case ENOBUFS: | - | ||||||||||||
573 | case ENOMEM: | - | ||||||||||||
574 | setError(QAbstractSocket::SocketResourceError, ResourceErrorString); | - | ||||||||||||
575 | break; | - | ||||||||||||
576 | case EACCES: | - | ||||||||||||
577 | case EPERM: | - | ||||||||||||
578 | setError(QAbstractSocket::SocketAccessError, AccessErrorString); | - | ||||||||||||
579 | break; | - | ||||||||||||
580 | #if EAGAIN != EWOULDBLOCK | - | ||||||||||||
581 | case EWOULDBLOCK: | - | ||||||||||||
582 | #endif | - | ||||||||||||
583 | case EAGAIN: | - | ||||||||||||
584 | setError(QAbstractSocket::TemporaryError, TemporaryErrorString); | - | ||||||||||||
585 | break; | - | ||||||||||||
586 | default: | - | ||||||||||||
587 | setError(QAbstractSocket::UnknownSocketError, UnknownSocketErrorString); | - | ||||||||||||
588 | break; | - | ||||||||||||
589 | } | - | ||||||||||||
590 | } | - | ||||||||||||
591 | - | |||||||||||||
592 | return acceptedDescriptor; | - | ||||||||||||
593 | } | - | ||||||||||||
594 | - | |||||||||||||
595 | #ifndef QT_NO_NETWORKINTERFACE | - | ||||||||||||
596 | - | |||||||||||||
597 | static bool multicastMembershipHelper(QNativeSocketEnginePrivate *d, | - | ||||||||||||
598 | int how6, | - | ||||||||||||
599 | int how4, | - | ||||||||||||
600 | const QHostAddress &groupAddress, | - | ||||||||||||
601 | const QNetworkInterface &interface) | - | ||||||||||||
602 | { | - | ||||||||||||
603 | int level = 0; | - | ||||||||||||
604 | int sockOpt = 0; | - | ||||||||||||
605 | void *sockArg; | - | ||||||||||||
606 | int sockArgSize; | - | ||||||||||||
607 | - | |||||||||||||
608 | ip_mreq mreq4; | - | ||||||||||||
609 | ipv6_mreq mreq6; | - | ||||||||||||
610 | - | |||||||||||||
611 | if (groupAddress.protocol() == QAbstractSocket::IPv6Protocol) {
| 6 | ||||||||||||
612 | level = IPPROTO_IPV6; | - | ||||||||||||
613 | sockOpt = how6; | - | ||||||||||||
614 | sockArg = &mreq6; | - | ||||||||||||
615 | sockArgSize = sizeof(mreq6); | - | ||||||||||||
616 | memset(&mreq6, 0, sizeof(mreq6)); | - | ||||||||||||
617 | Q_IPV6ADDR ip6 = groupAddress.toIPv6Address(); | - | ||||||||||||
618 | memcpy(&mreq6.ipv6mr_multiaddr, &ip6, sizeof(ip6)); | - | ||||||||||||
619 | mreq6.ipv6mr_interface = interface.index(); | - | ||||||||||||
620 | } else executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
621 | if (groupAddress.protocol() == QAbstractSocket::IPv4Protocol) {
| 0-6 | ||||||||||||
622 | level = IPPROTO_IP; | - | ||||||||||||
623 | sockOpt = how4; | - | ||||||||||||
624 | sockArg = &mreq4; | - | ||||||||||||
625 | sockArgSize = sizeof(mreq4); | - | ||||||||||||
626 | memset(&mreq4, 0, sizeof(mreq4)); | - | ||||||||||||
627 | mreq4.imr_multiaddr.s_addr = htonl(groupAddress.toIPv4Address()); | - | ||||||||||||
628 | - | |||||||||||||
629 | if (interface.isValid()) {
| 0-6 | ||||||||||||
630 | const QList<QNetworkAddressEntry> addressEntries = interface.addressEntries(); | - | ||||||||||||
631 | if (!addressEntries.isEmpty()) {
| 0 | ||||||||||||
632 | QHostAddress firstIP = addressEntries.first().ip(); | - | ||||||||||||
633 | mreq4.imr_interface.s_addr = htonl(firstIP.toIPv4Address()); | - | ||||||||||||
634 | } else { never executed: end of block | 0 | ||||||||||||
635 | d->setError(QAbstractSocket::NetworkError, | - | ||||||||||||
636 | QNativeSocketEnginePrivate::NetworkUnreachableErrorString); | - | ||||||||||||
637 | return false; never executed: return false; | 0 | ||||||||||||
638 | } | - | ||||||||||||
639 | } else { | - | ||||||||||||
640 | mreq4.imr_interface.s_addr = INADDR_ANY; | - | ||||||||||||
641 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
642 | } else { | - | ||||||||||||
643 | // unreachable | - | ||||||||||||
644 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
645 | QNativeSocketEnginePrivate::ProtocolUnsupportedErrorString); | - | ||||||||||||
646 | return false; never executed: return false; | 0 | ||||||||||||
647 | } | - | ||||||||||||
648 | - | |||||||||||||
649 | int res = setsockopt(d->socketDescriptor, level, sockOpt, sockArg, sockArgSize); | - | ||||||||||||
650 | if (res == -1) {
| 2-10 | ||||||||||||
651 | switch (errno) { | - | ||||||||||||
652 | case ENOPROTOOPT: never executed: case 92: | 0 | ||||||||||||
653 | d->setError(QAbstractSocket::UnsupportedSocketOperationError, | - | ||||||||||||
654 | QNativeSocketEnginePrivate::OperationUnsupportedErrorString); | - | ||||||||||||
655 | break; never executed: break; | 0 | ||||||||||||
656 | case EADDRNOTAVAIL: never executed: case 99: | 0 | ||||||||||||
657 | d->setError(QAbstractSocket::SocketAddressNotAvailableError, | - | ||||||||||||
658 | QNativeSocketEnginePrivate::AddressNotAvailableErrorString); | - | ||||||||||||
659 | break; never executed: break; | 0 | ||||||||||||
660 | default: executed 2 times by 1 test: default: Executed by:
| 2 | ||||||||||||
661 | d->setError(QAbstractSocket::UnknownSocketError, | - | ||||||||||||
662 | QNativeSocketEnginePrivate::UnknownSocketErrorString); | - | ||||||||||||
663 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||
664 | } | - | ||||||||||||
665 | return false; executed 2 times by 1 test: return false; Executed by:
| 2 | ||||||||||||
666 | } | - | ||||||||||||
667 | return true; executed 10 times by 1 test: return true; Executed by:
| 10 | ||||||||||||
668 | } | - | ||||||||||||
669 | - | |||||||||||||
670 | bool QNativeSocketEnginePrivate::nativeJoinMulticastGroup(const QHostAddress &groupAddress, | - | ||||||||||||
671 | const QNetworkInterface &interface) | - | ||||||||||||
672 | { | - | ||||||||||||
673 | return multicastMembershipHelper(this, | - | ||||||||||||
674 | IPV6_JOIN_GROUP, | - | ||||||||||||
675 | IP_ADD_MEMBERSHIP, | - | ||||||||||||
676 | groupAddress, | - | ||||||||||||
677 | interface); | - | ||||||||||||
678 | } | - | ||||||||||||
679 | - | |||||||||||||
680 | bool QNativeSocketEnginePrivate::nativeLeaveMulticastGroup(const QHostAddress &groupAddress, | - | ||||||||||||
681 | const QNetworkInterface &interface) | - | ||||||||||||
682 | { | - | ||||||||||||
683 | return multicastMembershipHelper(this, | - | ||||||||||||
684 | IPV6_LEAVE_GROUP, | - | ||||||||||||
685 | IP_DROP_MEMBERSHIP, | - | ||||||||||||
686 | groupAddress, | - | ||||||||||||
687 | interface); | - | ||||||||||||
688 | } | - | ||||||||||||
689 | - | |||||||||||||
690 | QNetworkInterface QNativeSocketEnginePrivate::nativeMulticastInterface() const | - | ||||||||||||
691 | { | - | ||||||||||||
692 | if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
693 | uint v; | - | ||||||||||||
694 | QT_SOCKOPTLEN_T sizeofv = sizeof(v); | - | ||||||||||||
695 | if (::getsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_MULTICAST_IF, &v, &sizeofv) == -1) | - | ||||||||||||
696 | return QNetworkInterface(); | - | ||||||||||||
697 | return QNetworkInterface::interfaceFromIndex(v); | - | ||||||||||||
698 | } | - | ||||||||||||
699 | - | |||||||||||||
700 | struct in_addr v = { 0 }; | - | ||||||||||||
701 | QT_SOCKOPTLEN_T sizeofv = sizeof(v); | - | ||||||||||||
702 | if (::getsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, &v, &sizeofv) == -1) | - | ||||||||||||
703 | return QNetworkInterface(); | - | ||||||||||||
704 | if (v.s_addr != 0 && sizeofv >= QT_SOCKOPTLEN_T(sizeof(v))) { | - | ||||||||||||
705 | QHostAddress ipv4(ntohl(v.s_addr)); | - | ||||||||||||
706 | QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); | - | ||||||||||||
707 | for (int i = 0; i < ifaces.count(); ++i) { | - | ||||||||||||
708 | const QNetworkInterface &iface = ifaces.at(i); | - | ||||||||||||
709 | QList<QNetworkAddressEntry> entries = iface.addressEntries(); | - | ||||||||||||
710 | for (int j = 0; j < entries.count(); ++j) { | - | ||||||||||||
711 | const QNetworkAddressEntry &entry = entries.at(j); | - | ||||||||||||
712 | if (entry.ip() == ipv4) | - | ||||||||||||
713 | return iface; | - | ||||||||||||
714 | } | - | ||||||||||||
715 | } | - | ||||||||||||
716 | } | - | ||||||||||||
717 | return QNetworkInterface(); | - | ||||||||||||
718 | } | - | ||||||||||||
719 | - | |||||||||||||
720 | bool QNativeSocketEnginePrivate::nativeSetMulticastInterface(const QNetworkInterface &iface) | - | ||||||||||||
721 | { | - | ||||||||||||
722 | if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) { | - | ||||||||||||
723 | uint v = iface.index(); | - | ||||||||||||
724 | return (::setsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_MULTICAST_IF, &v, sizeof(v)) != -1); | - | ||||||||||||
725 | } | - | ||||||||||||
726 | - | |||||||||||||
727 | struct in_addr v; | - | ||||||||||||
728 | if (iface.isValid()) { | - | ||||||||||||
729 | QList<QNetworkAddressEntry> entries = iface.addressEntries(); | - | ||||||||||||
730 | for (int i = 0; i < entries.count(); ++i) { | - | ||||||||||||
731 | const QNetworkAddressEntry &entry = entries.at(i); | - | ||||||||||||
732 | const QHostAddress &ip = entry.ip(); | - | ||||||||||||
733 | if (ip.protocol() == QAbstractSocket::IPv4Protocol) { | - | ||||||||||||
734 | v.s_addr = htonl(ip.toIPv4Address()); | - | ||||||||||||
735 | int r = ::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, &v, sizeof(v)); | - | ||||||||||||
736 | if (r != -1) | - | ||||||||||||
737 | return true; | - | ||||||||||||
738 | } | - | ||||||||||||
739 | } | - | ||||||||||||
740 | return false; | - | ||||||||||||
741 | } | - | ||||||||||||
742 | - | |||||||||||||
743 | v.s_addr = INADDR_ANY; | - | ||||||||||||
744 | return (::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, &v, sizeof(v)) != -1); | - | ||||||||||||
745 | } | - | ||||||||||||
746 | - | |||||||||||||
747 | #endif // QT_NO_NETWORKINTERFACE | - | ||||||||||||
748 | - | |||||||||||||
749 | qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const | - | ||||||||||||
750 | { | - | ||||||||||||
751 | int nbytes = 0; | - | ||||||||||||
752 | // gives shorter than true amounts on Unix domain sockets. | - | ||||||||||||
753 | qint64 available = 0-1; | - | ||||||||||||
754 | - | |||||||||||||
755 | #if defined (SO_NREAD) | - | ||||||||||||
756 | if (socketType == QAbstractSocket::UdpSocket) { | - | ||||||||||||
757 | socklen_t sz = sizeof nbytes; | - | ||||||||||||
758 | if (!::getsockopt(socketDescriptor, SOL_SOCKET, SO_NREAD, &nbytes, &sz)) | - | ||||||||||||
759 | available = nbytes; | - | ||||||||||||
760 | } | - | ||||||||||||
761 | #endif | - | ||||||||||||
762 | - | |||||||||||||
763 | if (available == -1 && qt_safe_ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
| 0-74694 | ||||||||||||
764 | available = (qint64)nbytes; executed 74694 times by 29 tests: available = nbytes; Executed by:
| 74694 | ||||||||||||
765 | - | |||||||||||||
766 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
767 | qDebug("QNativeSocketEnginePrivate::nativeBytesAvailable() == %lli", available); | - | ||||||||||||
768 | #endif | - | ||||||||||||
769 | return available > 0 ? available : 0; executed 74694 times by 29 tests: return available > 0 ? available : 0; Executed by:
| 74694 | ||||||||||||
770 | } | - | ||||||||||||
771 | - | |||||||||||||
772 | bool QNativeSocketEnginePrivate::nativeHasPendingDatagrams() const | - | ||||||||||||
773 | { | - | ||||||||||||
774 | // Create a sockaddr struct and reset its port number. | - | ||||||||||||
775 | qt_sockaddr storage; | - | ||||||||||||
776 | QT_SOCKLEN_T storageSize = sizeof(storage); | - | ||||||||||||
777 | memset(&storage, 0, storageSize); | - | ||||||||||||
778 | - | |||||||||||||
779 | // Peek 0 bytes into the next message. The size of the message may | - | ||||||||||||
780 | // well be 0, so we can't check recvfrom's return value. | - | ||||||||||||
781 | ssize_t readBytes; | - | ||||||||||||
782 | do { | - | ||||||||||||
783 | char c; | - | ||||||||||||
784 | readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, &storage.a, &storageSize); | - | ||||||||||||
785 | } while (readBytes == -1 && errno == EINTR); | - | ||||||||||||
786 | - | |||||||||||||
787 | // If there's no error, or if our buffer was too small, there must be a | - | ||||||||||||
788 | // pending datagram. | - | ||||||||||||
789 | bool result = (readBytes != -1) || errno == EMSGSIZE; | - | ||||||||||||
790 | - | |||||||||||||
791 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
792 | qDebug("QNativeSocketEnginePrivate::nativeHasPendingDatagrams() == %s", | - | ||||||||||||
793 | result ? "true" : "false"); | - | ||||||||||||
794 | #endif | - | ||||||||||||
795 | return result; | - | ||||||||||||
796 | } | - | ||||||||||||
797 | - | |||||||||||||
798 | qint64 QNativeSocketEnginePrivate::nativePendingDatagramSize() const | - | ||||||||||||
799 | { | - | ||||||||||||
800 | QVarLengthArray<char, 8192> udpMessagePeekBuffer(8192); | - | ||||||||||||
801 | ssize_t recvResult = -1; | - | ||||||||||||
802 | - | |||||||||||||
803 | for (;;) { | - | ||||||||||||
804 | // the data written to udpMessagePeekBuffer is discarded, so | - | ||||||||||||
805 | // this function is still reentrant although it might not look | - | ||||||||||||
806 | // so. | - | ||||||||||||
807 | recvResult = ::recv(socketDescriptor, udpMessagePeekBuffer.data(), | - | ||||||||||||
808 | udpMessagePeekBuffer.size(), MSG_PEEK); | - | ||||||||||||
809 | if (recvResult == -1 && errno == EINTR) | - | ||||||||||||
810 | continue; | - | ||||||||||||
811 | - | |||||||||||||
812 | if (recvResult != (ssize_t) udpMessagePeekBuffer.size()) | - | ||||||||||||
813 | break; | - | ||||||||||||
814 | - | |||||||||||||
815 | udpMessagePeekBuffer.resize(udpMessagePeekBuffer.size() * 2); | - | ||||||||||||
816 | } | - | ||||||||||||
817 | - | |||||||||||||
818 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
819 | qDebug("QNativeSocketEnginePrivate::nativePendingDatagramSize() == %zd", recvResult); | - | ||||||||||||
820 | #endif | - | ||||||||||||
821 | - | |||||||||||||
822 | return qint64(recvResult); | - | ||||||||||||
823 | } | - | ||||||||||||
824 | - | |||||||||||||
825 | qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxSize, QIpPacketHeader *header, | - | ||||||||||||
826 | QAbstractSocketEngine::PacketHeaderOptions options) | - | ||||||||||||
827 | { | - | ||||||||||||
828 | // we use quintptr to force the alignment | - | ||||||||||||
829 | quintptr cbuf[(CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) | - | ||||||||||||
830 | #if !defined(IP_PKTINFO) && defined(IP_RECVIF) && defined(Q_OS_BSD4) | - | ||||||||||||
831 | + CMSG_SPACE(sizeof(sockaddr_dl)) | - | ||||||||||||
832 | #endif | - | ||||||||||||
833 | + sizeof(quintptr) - 1) / sizeof(quintptr)]; | - | ||||||||||||
834 | - | |||||||||||||
835 | struct msghdr msg; | - | ||||||||||||
836 | struct iovec vec; | - | ||||||||||||
837 | qt_sockaddr aa; | - | ||||||||||||
838 | char c; | - | ||||||||||||
839 | memset(&msg, 0, sizeof(msg)); | - | ||||||||||||
840 | memset(&aa, 0, sizeof(aa)); | - | ||||||||||||
841 | - | |||||||||||||
842 | // we need to receive at least one byte, even if our user isn't interested in it | - | ||||||||||||
843 | vec.iov_base = maxSize ? data : &c; | - | ||||||||||||
844 | vec.iov_len = maxSize ? maxSize : 1; | - | ||||||||||||
845 | msg.msg_iov = &vec; | - | ||||||||||||
846 | msg.msg_iovlen = 1; | - | ||||||||||||
847 | if (options & QAbstractSocketEngine::WantDatagramSender) { | - | ||||||||||||
848 | msg.msg_name = &aa; | - | ||||||||||||
849 | msg.msg_namelen = sizeof(aa); | - | ||||||||||||
850 | } | - | ||||||||||||
851 | if (options & (QAbstractSocketEngine::WantDatagramHopLimit | QAbstractSocketEngine::WantDatagramDestination)) { | - | ||||||||||||
852 | msg.msg_control = cbuf; | - | ||||||||||||
853 | msg.msg_controllen = sizeof(cbuf); | - | ||||||||||||
854 | } | - | ||||||||||||
855 | - | |||||||||||||
856 | ssize_t recvResult = 0; | - | ||||||||||||
857 | do { | - | ||||||||||||
858 | recvResult = ::recvmsg(socketDescriptor, &msg, 0); | - | ||||||||||||
859 | } while (recvResult == -1 && errno == EINTR); | - | ||||||||||||
860 | - | |||||||||||||
861 | if (recvResult == -1) { | - | ||||||||||||
862 | setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString); | - | ||||||||||||
863 | if (header) | - | ||||||||||||
864 | header->clear(); | - | ||||||||||||
865 | } else if (options != QAbstractSocketEngine::WantNone) { | - | ||||||||||||
866 | Q_ASSERT(header); | - | ||||||||||||
867 | qt_socket_getPortAndAddress(&aa, &header->senderPort, &header->senderAddress); | - | ||||||||||||
868 | header->destinationPort = localPort; | - | ||||||||||||
869 | - | |||||||||||||
870 | // parse the ancillary data | - | ||||||||||||
871 | struct cmsghdr *cmsgptr; | - | ||||||||||||
872 | for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; | - | ||||||||||||
873 | cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { | - | ||||||||||||
874 | if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_PKTINFO | - | ||||||||||||
875 | && cmsgptr->cmsg_len >= CMSG_LEN(sizeof(in6_pktinfo))) { | - | ||||||||||||
876 | in6_pktinfo *info = reinterpret_cast<in6_pktinfo *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
877 | - | |||||||||||||
878 | header->destinationAddress.setAddress(reinterpret_cast<quint8 *>(&info->ipi6_addr)); | - | ||||||||||||
879 | header->ifindex = info->ipi6_ifindex; | - | ||||||||||||
880 | if (header->ifindex) | - | ||||||||||||
881 | header->destinationAddress.setScopeId(QString::number(info->ipi6_ifindex)); | - | ||||||||||||
882 | } | - | ||||||||||||
883 | - | |||||||||||||
884 | #ifdef IP_PKTINFO | - | ||||||||||||
885 | if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_PKTINFO | - | ||||||||||||
886 | && cmsgptr->cmsg_len >= CMSG_LEN(sizeof(in_pktinfo))) { | - | ||||||||||||
887 | in_pktinfo *info = reinterpret_cast<in_pktinfo *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
888 | - | |||||||||||||
889 | header->destinationAddress.setAddress(ntohl(info->ipi_addr.s_addr)); | - | ||||||||||||
890 | header->ifindex = info->ipi_ifindex; | - | ||||||||||||
891 | } | - | ||||||||||||
892 | #else | - | ||||||||||||
893 | # ifdef IP_RECVDSTADDR | - | ||||||||||||
894 | if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_RECVDSTADDR | - | ||||||||||||
895 | && cmsgptr->cmsg_len >= CMSG_LEN(sizeof(in_addr))) { | - | ||||||||||||
896 | in_addr *addr = reinterpret_cast<in_addr *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
897 | - | |||||||||||||
898 | header->destinationAddress.setAddress(ntohl(addr->s_addr)); | - | ||||||||||||
899 | } | - | ||||||||||||
900 | # endif | - | ||||||||||||
901 | # if defined(IP_RECVIF) && defined(Q_OS_BSD4) | - | ||||||||||||
902 | if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_RECVIF | - | ||||||||||||
903 | && cmsgptr->cmsg_len >= CMSG_LEN(sizeof(sockaddr_dl))) { | - | ||||||||||||
904 | sockaddr_dl *sdl = reinterpret_cast<sockaddr_dl *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
905 | header->ifindex = sdl->sdl_index; | - | ||||||||||||
906 | } | - | ||||||||||||
907 | # endif | - | ||||||||||||
908 | #endif | - | ||||||||||||
909 | - | |||||||||||||
910 | if (cmsgptr->cmsg_len == CMSG_LEN(sizeof(int)) | - | ||||||||||||
911 | && ((cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_HOPLIMIT) | - | ||||||||||||
912 | || (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_TTL))) { | - | ||||||||||||
913 | header->hopLimit = *reinterpret_cast<int *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
914 | } | - | ||||||||||||
915 | } | - | ||||||||||||
916 | } | - | ||||||||||||
917 | - | |||||||||||||
918 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
919 | qDebug("QNativeSocketEnginePrivate::nativeReceiveDatagram(%p \"%s\", %lli, %s, %i) == %lli", | - | ||||||||||||
920 | data, qt_prettyDebug(data, qMin(recvResult, ssize_t(16)), recvResult).data(), maxSize, | - | ||||||||||||
921 | (recvResult != -1 && options != QAbstractSocketEngine::WantNone) | - | ||||||||||||
922 | ? header->senderAddress.toString().toLatin1().constData() : "(unknown)", | - | ||||||||||||
923 | (recvResult != -1 && options != QAbstractSocketEngine::WantNone) | - | ||||||||||||
924 | ? header->senderPort : 0, (qint64) recvResult); | - | ||||||||||||
925 | #endif | - | ||||||||||||
926 | - | |||||||||||||
927 | return qint64(maxSize ? recvResult : recvResult == -1 ? -1 : 0); | - | ||||||||||||
928 | } | - | ||||||||||||
929 | - | |||||||||||||
930 | qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 len, const QIpPacketHeader &header) | - | ||||||||||||
931 | { | - | ||||||||||||
932 | // we use quintptr to force the alignment | - | ||||||||||||
933 | quintptr cbuf[(CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + sizeof(quintptr) - 1) / sizeof(quintptr)]; | - | ||||||||||||
934 | - | |||||||||||||
935 | struct cmsghdr *cmsgptr = reinterpret_cast<struct cmsghdr *>(cbuf); | - | ||||||||||||
936 | struct msghdr msg; | - | ||||||||||||
937 | struct iovec vec; | - | ||||||||||||
938 | qt_sockaddr aa; | - | ||||||||||||
939 | - | |||||||||||||
940 | memset(&msg, 0, sizeof(msg)); | - | ||||||||||||
941 | memset(&aa, 0, sizeof(aa)); | - | ||||||||||||
942 | vec.iov_base = const_cast<char *>(data); | - | ||||||||||||
943 | vec.iov_len = len; | - | ||||||||||||
944 | msg.msg_iov = &vec; | - | ||||||||||||
945 | msg.msg_iovlen = 1; | - | ||||||||||||
946 | msg.msg_name = &aa.a; | - | ||||||||||||
947 | msg.msg_control = &cbuf; | - | ||||||||||||
948 | - | |||||||||||||
949 | setPortAndAddress(header.destinationPort, header.destinationAddress, &aa, &msg.msg_namelen); | - | ||||||||||||
950 | - | |||||||||||||
951 | if (msg.msg_namelen == sizeof(aa.a6)) { | - | ||||||||||||
952 | if (header.hopLimit != -1) { | - | ||||||||||||
953 | msg.msg_controllen += CMSG_SPACE(sizeof(int)); | - | ||||||||||||
954 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(int)); | - | ||||||||||||
955 | cmsgptr->cmsg_level = IPPROTO_IPV6; | - | ||||||||||||
956 | cmsgptr->cmsg_type = IPV6_HOPLIMIT; | - | ||||||||||||
957 | memcpy(CMSG_DATA(cmsgptr), &header.hopLimit, sizeof(int)); | - | ||||||||||||
958 | cmsgptr = reinterpret_cast<cmsghdr *>(reinterpret_cast<char *>(cmsgptr) + CMSG_SPACE(sizeof(int))); | - | ||||||||||||
959 | } | - | ||||||||||||
960 | if (header.ifindex != 0 || !header.senderAddress.isNull()) { | - | ||||||||||||
961 | struct in6_pktinfo *data = reinterpret_cast<in6_pktinfo *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
962 | memset(data, 0, sizeof(*data)); | - | ||||||||||||
963 | msg.msg_controllen += CMSG_SPACE(sizeof(*data)); | - | ||||||||||||
964 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(*data)); | - | ||||||||||||
965 | cmsgptr->cmsg_level = IPPROTO_IPV6; | - | ||||||||||||
966 | cmsgptr->cmsg_type = IPV6_PKTINFO; | - | ||||||||||||
967 | data->ipi6_ifindex = header.ifindex; | - | ||||||||||||
968 | - | |||||||||||||
969 | QIPv6Address tmp = header.senderAddress.toIPv6Address(); | - | ||||||||||||
970 | memcpy(&data->ipi6_addr, &tmp, sizeof(tmp)); | - | ||||||||||||
971 | cmsgptr = reinterpret_cast<cmsghdr *>(reinterpret_cast<char *>(cmsgptr) + CMSG_SPACE(sizeof(*data))); | - | ||||||||||||
972 | } | - | ||||||||||||
973 | } else { | - | ||||||||||||
974 | if (header.hopLimit != -1) { | - | ||||||||||||
975 | msg.msg_controllen += CMSG_SPACE(sizeof(int)); | - | ||||||||||||
976 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(int)); | - | ||||||||||||
977 | cmsgptr->cmsg_level = IPPROTO_IP; | - | ||||||||||||
978 | cmsgptr->cmsg_type = IP_TTL; | - | ||||||||||||
979 | memcpy(CMSG_DATA(cmsgptr), &header.hopLimit, sizeof(int)); | - | ||||||||||||
980 | cmsgptr = reinterpret_cast<cmsghdr *>(reinterpret_cast<char *>(cmsgptr) + CMSG_SPACE(sizeof(int))); | - | ||||||||||||
981 | } | - | ||||||||||||
982 | - | |||||||||||||
983 | #if defined(IP_PKTINFO) || defined(IP_SENDSRCADDR) | - | ||||||||||||
984 | if (header.ifindex != 0 || !header.senderAddress.isNull()) { | - | ||||||||||||
985 | # ifdef IP_PKTINFO | - | ||||||||||||
986 | struct in_pktinfo *data = reinterpret_cast<in_pktinfo *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
987 | memset(data, 0, sizeof(*data)); | - | ||||||||||||
988 | cmsgptr->cmsg_type = IP_PKTINFO; | - | ||||||||||||
989 | data->ipi_ifindex = header.ifindex; | - | ||||||||||||
990 | data->ipi_addr.s_addr = htonl(header.senderAddress.toIPv4Address()); | - | ||||||||||||
991 | # elif defined(IP_SENDSRCADDR) | - | ||||||||||||
992 | struct in_addr *data = reinterpret_cast<in_addr *>(CMSG_DATA(cmsgptr)); | - | ||||||||||||
993 | cmsgptr->cmsg_type = IP_SENDSRCADDR; | - | ||||||||||||
994 | data->s_addr = htonl(header.senderAddress.toIPv4Address()); | - | ||||||||||||
995 | # endif | - | ||||||||||||
996 | cmsgptr->cmsg_level = IPPROTO_IP; | - | ||||||||||||
997 | msg.msg_controllen += CMSG_SPACE(sizeof(*data)); | - | ||||||||||||
998 | cmsgptr->cmsg_len = CMSG_LEN(sizeof(*data)); | - | ||||||||||||
999 | cmsgptr = reinterpret_cast<cmsghdr *>(reinterpret_cast<char *>(cmsgptr) + CMSG_SPACE(sizeof(*data))); | - | ||||||||||||
1000 | } | - | ||||||||||||
1001 | #endif | - | ||||||||||||
1002 | } | - | ||||||||||||
1003 | - | |||||||||||||
1004 | if (msg.msg_controllen == 0) | - | ||||||||||||
1005 | msg.msg_control = 0; | - | ||||||||||||
1006 | ssize_t sentBytes = qt_safe_sendmsg(socketDescriptor, &msg, 0); | - | ||||||||||||
1007 | - | |||||||||||||
1008 | if (sentBytes < 0) { | - | ||||||||||||
1009 | switch (errno) { | - | ||||||||||||
1010 | case EMSGSIZE: | - | ||||||||||||
1011 | setError(QAbstractSocket::DatagramTooLargeError, DatagramTooLargeErrorString); | - | ||||||||||||
1012 | break; | - | ||||||||||||
1013 | default: | - | ||||||||||||
1014 | setError(QAbstractSocket::NetworkError, SendDatagramErrorString); | - | ||||||||||||
1015 | } | - | ||||||||||||
1016 | } | - | ||||||||||||
1017 | - | |||||||||||||
1018 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
1019 | qDebug("QNativeSocketEngine::sendDatagram(%p \"%s\", %lli, \"%s\", %i) == %lli", data, | - | ||||||||||||
1020 | qt_prettyDebug(data, qMin<int>(len, 16), len).data(), len, | - | ||||||||||||
1021 | header.destinationAddress.toString().toLatin1().constData(), | - | ||||||||||||
1022 | header.destinationPort, (qint64) sentBytes); | - | ||||||||||||
1023 | #endif | - | ||||||||||||
1024 | - | |||||||||||||
1025 | return qint64(sentBytes); | - | ||||||||||||
1026 | } | - | ||||||||||||
1027 | - | |||||||||||||
1028 | bool QNativeSocketEnginePrivate::fetchConnectionParameters() | - | ||||||||||||
1029 | { | - | ||||||||||||
1030 | localPort = 0; | - | ||||||||||||
1031 | localAddress.clear(); | - | ||||||||||||
1032 | peerPort = 0; | - | ||||||||||||
1033 | peerAddress.clear(); | - | ||||||||||||
1034 | inboundStreamCount = outboundStreamCount = 0; | - | ||||||||||||
1035 | - | |||||||||||||
1036 | if (socketDescriptor == -1)
| 1-5240 | ||||||||||||
1037 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
1038 | - | |||||||||||||
1039 | qt_sockaddr sa; | - | ||||||||||||
1040 | QT_SOCKLEN_T sockAddrSize = sizeof(sa); | - | ||||||||||||
1041 | - | |||||||||||||
1042 | // Determine local address | - | ||||||||||||
1043 | memset(&sa, 0, sizeof(sa)); | - | ||||||||||||
1044 | if (::getsockname(socketDescriptor, &sa.a, &sockAddrSize) == 0) {
| 8-5232 | ||||||||||||
1045 | qt_socket_getPortAndAddress(&sa, &localPort, &localAddress); | - | ||||||||||||
1046 | - | |||||||||||||
1047 | // Determine protocol family | - | ||||||||||||
1048 | switch (sa.a.sa_family) { | - | ||||||||||||
1049 | case AF_INET: executed 3867 times by 28 tests: case 2: Executed by:
| 3867 | ||||||||||||
1050 | socketProtocol = QAbstractSocket::IPv4Protocol; | - | ||||||||||||
1051 | break; executed 3867 times by 28 tests: break; Executed by:
| 3867 | ||||||||||||
1052 | case AF_INET6: executed 1207 times by 14 tests: case 10: Executed by:
| 1207 | ||||||||||||
1053 | socketProtocol = QAbstractSocket::IPv6Protocol; | - | ||||||||||||
1054 | break; executed 1207 times by 14 tests: break; Executed by:
| 1207 | ||||||||||||
1055 | default: executed 158 times by 3 tests: default: Executed by:
| 158 | ||||||||||||
1056 | socketProtocol = QAbstractSocket::UnknownNetworkLayerProtocol; | - | ||||||||||||
1057 | break; executed 158 times by 3 tests: break; Executed by:
| 158 | ||||||||||||
1058 | } | - | ||||||||||||
1059 | - | |||||||||||||
1060 | } else if (errno == EBADF) {
| 0-8 | ||||||||||||
1061 | setError(QAbstractSocket::UnsupportedSocketOperationError, InvalidSocketErrorString); | - | ||||||||||||
1062 | return false; executed 8 times by 2 tests: return false; Executed by:
| 8 | ||||||||||||
1063 | } | - | ||||||||||||
1064 | - | |||||||||||||
1065 | #if defined (IPV6_V6ONLY) | - | ||||||||||||
1066 | // determine if local address is dual mode | - | ||||||||||||
1067 | // On linux, these are returned as "::" (==AnyIPv6) | - | ||||||||||||
1068 | // On OSX, these are returned as "::FFFF:0.0.0.0" (==AnyIPv4) | - | ||||||||||||
1069 | // in either case, the IPV6_V6ONLY option is cleared | - | ||||||||||||
1070 | int ipv6only = 0; | - | ||||||||||||
1071 | socklen_t optlen = sizeof(ipv6only); | - | ||||||||||||
1072 | if (socketProtocol == QAbstractSocket::IPv6Protocol
| 1207-4025 | ||||||||||||
1073 | && (localAddress == QHostAddress::AnyIPv4 || localAddress == QHostAddress::AnyIPv6)
| 0-1207 | ||||||||||||
1074 | && !getsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6only, &optlen )) {
| 0-616 | ||||||||||||
1075 | if (optlen != sizeof(ipv6only))
| 0-616 | ||||||||||||
1076 | qWarning("unexpected size of IPV6_V6ONLY socket option"); never executed: QMessageLogger(__FILE__, 1076, __PRETTY_FUNCTION__).warning("unexpected size of IPV6_V6ONLY socket option"); | 0 | ||||||||||||
1077 | if (!ipv6only) {
| 36-580 | ||||||||||||
1078 | socketProtocol = QAbstractSocket::AnyIPProtocol; | - | ||||||||||||
1079 | localAddress = QHostAddress::Any; | - | ||||||||||||
1080 | } executed 580 times by 13 tests: end of block Executed by:
| 580 | ||||||||||||
1081 | } executed 616 times by 13 tests: end of block Executed by:
| 616 | ||||||||||||
1082 | #endif | - | ||||||||||||
1083 | - | |||||||||||||
1084 | // Determine the remote address | - | ||||||||||||
1085 | if (!::getpeername(socketDescriptor, &sa.a, &sockAddrSize)) {
| 935-4297 | ||||||||||||
1086 | qt_socket_getPortAndAddress(&sa, &peerPort, &peerAddress); | - | ||||||||||||
1087 | inboundStreamCount = outboundStreamCount = 1; | - | ||||||||||||
1088 | } executed 4297 times by 30 tests: end of block Executed by:
| 4297 | ||||||||||||
1089 | - | |||||||||||||
1090 | // Determine the socket type (UDP/TCP) | - | ||||||||||||
1091 | int value = 0; | - | ||||||||||||
1092 | QT_SOCKOPTLEN_T valueSize = sizeof(int); | - | ||||||||||||
1093 | if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, &value, &valueSize) == 0) {
| 0-5232 | ||||||||||||
1094 | if (value == SOCK_STREAM)
| 140-5092 | ||||||||||||
1095 | socketType = QAbstractSocket::TcpSocket; executed 5092 times by 31 tests: socketType = QAbstractSocket::TcpSocket; Executed by:
| 5092 | ||||||||||||
1096 | else if (value == SOCK_DGRAM)
| 0-140 | ||||||||||||
1097 | socketType = QAbstractSocket::UdpSocket; executed 140 times by 4 tests: socketType = QAbstractSocket::UdpSocket; Executed by:
| 140 | ||||||||||||
1098 | else | - | ||||||||||||
1099 | socketType = QAbstractSocket::UnknownSocketType; never executed: socketType = QAbstractSocket::UnknownSocketType; | 0 | ||||||||||||
1100 | } | - | ||||||||||||
1101 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
1102 | QString socketProtocolStr = QStringLiteral("UnknownProtocol"); | - | ||||||||||||
1103 | if (socketProtocol == QAbstractSocket::IPv4Protocol) socketProtocolStr = QStringLiteral("IPv4Protocol"); | - | ||||||||||||
1104 | else if (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) socketProtocolStr = QStringLiteral("IPv6Protocol"); | - | ||||||||||||
1105 | - | |||||||||||||
1106 | QString socketTypeStr = QStringLiteral("UnknownSocketType"); | - | ||||||||||||
1107 | if (socketType == QAbstractSocket::TcpSocket) socketTypeStr = QStringLiteral("TcpSocket"); | - | ||||||||||||
1108 | else if (socketType == QAbstractSocket::UdpSocket) socketTypeStr = QStringLiteral("UdpSocket"); | - | ||||||||||||
1109 | - | |||||||||||||
1110 | qDebug("QNativeSocketEnginePrivate::fetchConnectionParameters() local == %s:%i," | - | ||||||||||||
1111 | " peer == %s:%i, socket == %s - %s"%s, inboundStreamCount == %i, outboundStreamCount == %i", | - | ||||||||||||
1112 | localAddress.toString().toLatin1().constData(), localPort, | - | ||||||||||||
1113 | peerAddress.toString().toLatin1().constData(), peerPort,socketTypeStr.toLatin1().constData(), | - | ||||||||||||
1114 | socketProtocolStr.toLatin1().constData());(), inboundStreamCount, outboundStreamCount); | - | ||||||||||||
1115 | #endif | - | ||||||||||||
1116 | return true; executed 5232 times by 31 tests: return true; Executed by:
| 5232 | ||||||||||||
1117 | } | - | ||||||||||||
1118 | - | |||||||||||||
1119 | void QNativeSocketEnginePrivate::nativeClose() | - | ||||||||||||
1120 | { | - | ||||||||||||
1121 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
1122 | qDebug("QNativeSocketEngine::nativeClose()"); | - | ||||||||||||
1123 | #endif | - | ||||||||||||
1124 | - | |||||||||||||
1125 | qt_safe_close(socketDescriptor); | - | ||||||||||||
1126 | } | - | ||||||||||||
1127 | - | |||||||||||||
1128 | qint64 QNativeSocketEnginePrivate::nativeWrite(const char *data, qint64 len) | - | ||||||||||||
1129 | { | - | ||||||||||||
1130 | Q_Q(QNativeSocketEngine); | - | ||||||||||||
1131 | - | |||||||||||||
1132 | ssize_t writtenBytes; | - | ||||||||||||
1133 | writtenBytes = qt_safe_write_nosignal(socketDescriptor, data, len); | - | ||||||||||||
1134 | - | |||||||||||||
1135 | if (writtenBytes < 0) { | - | ||||||||||||
1136 | switch (errno) { | - | ||||||||||||
1137 | case EPIPE: | - | ||||||||||||
1138 | case ECONNRESET: | - | ||||||||||||
1139 | writtenBytes = -1; | - | ||||||||||||
1140 | setError(QAbstractSocket::RemoteHostClosedError, RemoteHostClosedErrorString); | - | ||||||||||||
1141 | q->close(); | - | ||||||||||||
1142 | break; | - | ||||||||||||
1143 | case EAGAIN: | - | ||||||||||||
1144 | writtenBytes = 0; | - | ||||||||||||
1145 | break; | - | ||||||||||||
1146 | case EMSGSIZE: | - | ||||||||||||
1147 | setError(QAbstractSocket::DatagramTooLargeError, DatagramTooLargeErrorString); | - | ||||||||||||
1148 | break; | - | ||||||||||||
1149 | default: | - | ||||||||||||
1150 | break; | - | ||||||||||||
1151 | } | - | ||||||||||||
1152 | } | - | ||||||||||||
1153 | - | |||||||||||||
1154 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
1155 | qDebug("QNativeSocketEnginePrivate::nativeWrite(%p \"%s\", %llu) == %i", | - | ||||||||||||
1156 | data, qt_prettyDebug(data, qMin((int) len, 16), | - | ||||||||||||
1157 | (int) len).data(), len, (int) writtenBytes); | - | ||||||||||||
1158 | #endif | - | ||||||||||||
1159 | - | |||||||||||||
1160 | return qint64(writtenBytes); | - | ||||||||||||
1161 | } | - | ||||||||||||
1162 | /* | - | ||||||||||||
1163 | */ | - | ||||||||||||
1164 | qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize) | - | ||||||||||||
1165 | { | - | ||||||||||||
1166 | Q_Q(QNativeSocketEngine); | - | ||||||||||||
1167 | if (!q->isValid()) { | - | ||||||||||||
1168 | qWarning("QNativeSocketEngine::nativeRead: Invalid socket"); | - | ||||||||||||
1169 | return -1; | - | ||||||||||||
1170 | } | - | ||||||||||||
1171 | - | |||||||||||||
1172 | ssize_t r = 0; | - | ||||||||||||
1173 | r = qt_safe_read(socketDescriptor, data, maxSize); | - | ||||||||||||
1174 | - | |||||||||||||
1175 | if (r < 0) { | - | ||||||||||||
1176 | r = -1; | - | ||||||||||||
1177 | switch (errno) { | - | ||||||||||||
1178 | #if EWOULDBLOCK-0 && EWOULDBLOCK != EAGAIN | - | ||||||||||||
1179 | case EWOULDBLOCK: | - | ||||||||||||
1180 | #endif | - | ||||||||||||
1181 | case EAGAIN: | - | ||||||||||||
1182 | // No data was available for reading | - | ||||||||||||
1183 | r = -2; | - | ||||||||||||
1184 | break; | - | ||||||||||||
1185 | case EBADF: | - | ||||||||||||
1186 | case EINVAL: | - | ||||||||||||
1187 | case EIO: | - | ||||||||||||
1188 | //error string is now set in read(), not here in nativeRead() | - | ||||||||||||
1189 | break; | - | ||||||||||||
1190 | case ECONNRESET: | - | ||||||||||||
1191 | #if defined(Q_OS_VXWORKS) | - | ||||||||||||
1192 | case ESHUTDOWN: | - | ||||||||||||
1193 | #endif | - | ||||||||||||
1194 | r = 0; | - | ||||||||||||
1195 | break; | - | ||||||||||||
1196 | default: | - | ||||||||||||
1197 | break; | - | ||||||||||||
1198 | } | - | ||||||||||||
1199 | } | - | ||||||||||||
1200 | - | |||||||||||||
1201 | #if defined (QNATIVESOCKETENGINE_DEBUG) | - | ||||||||||||
1202 | qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %llu) == %zd", | - | ||||||||||||
1203 | data, qt_prettyDebug(data, qMin(r, ssize_t(16)), r).data(), | - | ||||||||||||
1204 | maxSize, r); | - | ||||||||||||
1205 | #endif | - | ||||||||||||
1206 | - | |||||||||||||
1207 | return qint64(r); | - | ||||||||||||
1208 | } | - | ||||||||||||
1209 | - | |||||||||||||
#ifdef Q_OS_BLACKBERRYint QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const | ||||||||||||||
{ | ||||||||||||||
fd_set fds; | ||||||||||||||
FD_ZERO(&fds); | ||||||||||||||
FD_SET(socketDescriptor, &fds); | ||||||||||||||
int retval; | ||||||||||||||
QList<QSocketNotifier *> notifiers; | ||||||||||||||
if (selectForRead){ | ||||||||||||||
1212 | notifiers << readNotifierbool dummy; | - | ||||||||||||
1213 | retval = bb_selectreturn nativeSelect(notifiers, socketDescriptor + 1, &fds, 0,timeout); executed 262400 times by 20 tests: return nativeSelect(timeout, selectForRead, !selectForRead, &dummy, &dummy); Executed by:
| 262400 | ||||||||||||
} else { executed 262400 times by 20 tests: return nativeSelect(timeout, selectForRead, !selectForRead, &dummy, &dummy); Executed by:
| ||||||||||||||
notifiers << writeNotifier; executed 262400 times by 20 tests: return nativeSelect(timeout, selectForRead, !selectForRead, &dummy, &dummy); Executed by:
| ||||||||||||||
retval = bb_select(notifiers executed 262400 times by 20 tests: , socketDescriptor + 1selectForRead, 0!selectForRead, &fds&dummy, timeout&dummy);return nativeSelect(timeout, selectForRead, !selectForRead, &dummy, &dummy); Executed by:
executed 262400 times by 20 tests: return nativeSelect(timeout, selectForRead, !selectForRead, &dummy, &dummy); Executed by:
| ||||||||||||||
} | ||||||||||||||
return retval;} | ||||||||||||||
1215 | - | |||||||||||||
1216 | int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite, | - | ||||||||||||
1217 | bool *selectForRead, bool *selectForWrite) const | - | ||||||||||||
1218 | { | - | ||||||||||||
1219 | fd_set fdread; | - | ||||||||||||
FD_ZEROpollfd pfd = qt_make_pollfd(&fdreadsocketDescriptor, 0); | ||||||||||||||
1220 | - | |||||||||||||
1221 | if (checkRead)
| 1102-275986 | ||||||||||||
1222 | FD_SET(socketDescriptor, &fdread); executed 275986 times by 23 tests: pfd.events |= 0x001; Executed by:
| 275986 | ||||||||||||
fd_set fdwrite executed 275986 times by 23 tests: pfd.events |= POLLIN;pfd.events |= 0x001; Executed by:
executed 275986 times by 23 tests: pfd.events |= 0x001; Executed by:
| ||||||||||||||
1223 | - | |||||||||||||
1224 | FD_ZERO(&fdwrite);if (checkWrite)
| 10869-266219 | ||||||||||||
1225 | FD_SET(socketDescriptor, &fdwrite); executed 10869 times by 22 tests: pfd.events |= 0x004; Executed by:
| 10869 | ||||||||||||
QList<QSocketNotifier *> notifiers; executed 10869 times by 22 tests: pfd.events |= 0x004; Executed by:
| ||||||||||||||
notifiers << readNotifier << writeNotifier executed 10869 times by 22 tests: pfd.events |= POLLOUT;pfd.events |= 0x004; Executed by:
executed 10869 times by 22 tests: pfd.events |= 0x004; Executed by:
| ||||||||||||||
1226 | - | |||||||||||||
1227 | const int ret = bb_selectqt_poll_msecs(notifiers&pfd, socketDescriptor +1, &fdread, &fdwrite, timeout); | - | ||||||||||||
1228 | - | |||||||||||||
1229 | if (ret <= 0)
| 138-276950 | ||||||||||||
1230 | return ret; executed 138 times by 10 tests: return ret; Executed by:
| 138 | ||||||||||||
*selectForRead = FD_ISSET(socketDescriptor, &fdread); executed 138 times by 10 tests: return ret; Executed by:
| ||||||||||||||
*selectForWrite = FD_ISSET(socketDescriptor, &fdwrite); executed 138 times by 10 tests: return ret;return ret; Executed by:
executed 138 times by 10 tests: return ret; Executed by:
| ||||||||||||||
1231 | - | |||||||||||||
1232 | }
| 0-276950 | ||||||||||||
#else int QNativeSocketEnginePrivate::nativeSelect
| ||||||||||||||
1233 | fd_set fds; | - | ||||||||||||
FD_ZERO(&fds); | ||||||||||||||
FD_SET(socketDescriptor, &fds); | ||||||||||||||
struct timespec tv; | ||||||||||||||
tv.tv_sec = timeout / 1000; | ||||||||||||||
tv.tv_nsecerrno = (timeout % 1000) * 1000 * 1000; | ||||||||||||||
int retvalEBADF; | ||||||||||||||
1234 | if (selectForRead) never executed: return -1; | 0 | ||||||||||||
retval = qt_safe_select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv); never executed: return -1; | ||||||||||||||
else never executed: return -1; | ||||||||||||||
retval = qt_safe_select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv); never executed: return retval-1;return -1; never executed: return -1; | ||||||||||||||
1235 | } | - | ||||||||||||
1236 | - | |||||||||||||
1237 | int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite, | - | ||||||||||||
bool *selectForRead, bool *selectForWrite)static const { | ||||||||||||||
fd_set fdread; | ||||||||||||||
FD_ZERO(&fdread); | ||||||||||||||
if (checkRead) | ||||||||||||||
FD_SET(socketDescriptor, &fdread); | ||||||||||||||
fd_set fdwrite; | ||||||||||||||
FD_ZERO(&fdwrite); | ||||||||||||||
if (checkWrite) | ||||||||||||||
FD_SET(socketDescriptor, &fdwrite); | ||||||||||||||
struct timespec tv; | ||||||||||||||
tv.tv_sec = timeout / 1000; | ||||||||||||||
tv.tv_nsecshort read_flags = (timeout % 1000) * 1000 * 1000; | ||||||||||||||
int retPOLLIN | POLLHUP | POLLERR; | ||||||||||||||
1238 | retstatic const short write_flags = qt_safe_select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv); | - | ||||||||||||
if (ret <= 0) | ||||||||||||||
return retPOLLOUT | POLLERR; | ||||||||||||||
1239 | - | |||||||||||||
1240 | *selectForRead = FD_ISSET(socketDescriptor, &fdread((pfd.revents & read_flags) != 0); | - | ||||||||||||
1241 | *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite((pfd.revents & write_flags) != 0); | - | ||||||||||||
1242 | - | |||||||||||||
1243 | return ret; executed 276950 times by 23 tests: return ret; Executed by:
| 276950 | ||||||||||||
1244 | }#endif | - | ||||||||||||
1245 | - | |||||||||||||
1246 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |