1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** Copyright (C) 2010 netlabs.org. OS/2 parts.
|
---|
8 | **
|
---|
9 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
10 | **
|
---|
11 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
12 | ** Commercial Usage
|
---|
13 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
14 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
15 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
16 | ** a written agreement between you and Nokia.
|
---|
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 2.1 as published by the Free Software
|
---|
21 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
22 | ** packaging of this file. Please review the following information to
|
---|
23 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
24 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
25 | **
|
---|
26 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
27 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
29 | **
|
---|
30 | ** GNU General Public License Usage
|
---|
31 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
32 | ** General Public License version 3.0 as published by the Free Software
|
---|
33 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
34 | ** packaging of this file. Please review the following information to
|
---|
35 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
36 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
37 | **
|
---|
38 | ** If you have questions regarding the use of this file, please contact
|
---|
39 | ** Nokia at qt-info@nokia.com.
|
---|
40 | ** $QT_END_LICENSE$
|
---|
41 | **
|
---|
42 | ****************************************************************************/
|
---|
43 |
|
---|
44 | //#define QNATIVESOCKETENGINE_DEBUG
|
---|
45 |
|
---|
46 | #include "qnativesocketengine_p.h"
|
---|
47 | #include "qiodevice.h"
|
---|
48 | #include "qhostaddress.h"
|
---|
49 | #include "qvarlengtharray.h"
|
---|
50 | #include "qdatetime.h"
|
---|
51 | #include <time.h>
|
---|
52 | #include <errno.h>
|
---|
53 | #include <fcntl.h>
|
---|
54 | #include <sys/filio.h>
|
---|
55 | #include <netinet/tcp.h>
|
---|
56 |
|
---|
57 | #if defined QNATIVESOCKETENGINE_DEBUG
|
---|
58 | #include <qstring.h>
|
---|
59 | #include <ctype.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | QT_BEGIN_NAMESPACE
|
---|
63 |
|
---|
64 | #if defined QNATIVESOCKETENGINE_DEBUG
|
---|
65 |
|
---|
66 | /*
|
---|
67 | Returns a human readable representation of the first \a len
|
---|
68 | characters in \a data.
|
---|
69 | */
|
---|
70 | static QByteArray qt_prettyDebug(const char *data, int len, int maxSize)
|
---|
71 | {
|
---|
72 | if (!data) return "(null)";
|
---|
73 | QByteArray out;
|
---|
74 | for (int i = 0; i < len; ++i) {
|
---|
75 | char c = data[i];
|
---|
76 | if (isprint(c)) {
|
---|
77 | out += c;
|
---|
78 | } else switch (c) {
|
---|
79 | case '\n': out += "\\n"; break;
|
---|
80 | case '\r': out += "\\r"; break;
|
---|
81 | case '\t': out += "\\t"; break;
|
---|
82 | default:
|
---|
83 | QString tmp;
|
---|
84 | tmp.sprintf("\\%o", c);
|
---|
85 | out += tmp.toLatin1();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (len < maxSize)
|
---|
90 | out += "...";
|
---|
91 |
|
---|
92 | return out;
|
---|
93 | }
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | static void qt_ignore_sigpipe()
|
---|
97 | {
|
---|
98 | // Set to ignore SIGPIPE once only.
|
---|
99 | static QBasicAtomicInt atom = Q_BASIC_ATOMIC_INITIALIZER(0);
|
---|
100 | if (atom.testAndSetRelaxed(0, 1)) {
|
---|
101 | struct sigaction noaction;
|
---|
102 | memset(&noaction, 0, sizeof(noaction));
|
---|
103 | noaction.sa_handler = SIG_IGN;
|
---|
104 | ::sigaction(SIGPIPE, &noaction, 0);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | Extracts the port and address from a sockaddr, and stores them in
|
---|
110 | \a port and \a addr if they are non-null.
|
---|
111 | */
|
---|
112 | static inline void qt_socket_getPortAndAddress(const qt_sockaddr *s, quint16 *port, QHostAddress *addr)
|
---|
113 | {
|
---|
114 | if (port)
|
---|
115 | *port = ntohs(s->a4.sin_port);
|
---|
116 | if (addr) {
|
---|
117 | QHostAddress tmpAddress;
|
---|
118 | tmpAddress.setAddress(ntohl(s->a4.sin_addr.s_addr));
|
---|
119 | *addr = tmpAddress;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType socketType,
|
---|
124 | QAbstractSocket::NetworkLayerProtocol socketProtocol)
|
---|
125 | {
|
---|
126 | Q_UNUSED(socketProtocol);
|
---|
127 | int protocol = AF_INET;
|
---|
128 | int type = (socketType == QAbstractSocket::UdpSocket) ? SOCK_DGRAM : SOCK_STREAM;
|
---|
129 | int socket = ::socket(protocol, type, 0);
|
---|
130 |
|
---|
131 | if (socket <= 0) {
|
---|
132 | switch (errno) {
|
---|
133 | case EPROTONOSUPPORT:
|
---|
134 | case EAFNOSUPPORT:
|
---|
135 | case EINVAL:
|
---|
136 | setError(QAbstractSocket::UnsupportedSocketOperationError, ProtocolUnsupportedErrorString);
|
---|
137 | break;
|
---|
138 | case ENFILE:
|
---|
139 | case EMFILE:
|
---|
140 | case ENOBUFS:
|
---|
141 | case ENOMEM:
|
---|
142 | setError(QAbstractSocket::SocketResourceError, ResourceErrorString);
|
---|
143 | break;
|
---|
144 | case EACCES:
|
---|
145 | setError(QAbstractSocket::SocketAccessError, AccessErrorString);
|
---|
146 | break;
|
---|
147 | default:
|
---|
148 | break;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // Ensure that the socket is closed on exec*().
|
---|
155 | ::fcntl(socket, F_SETFD, FD_CLOEXEC);
|
---|
156 |
|
---|
157 | socketDescriptor = socket;
|
---|
158 | return true;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | Returns the value of the socket option \a opt.
|
---|
163 | */
|
---|
164 | int QNativeSocketEnginePrivate::option(QNativeSocketEngine::SocketOption opt) const
|
---|
165 | {
|
---|
166 | Q_Q(const QNativeSocketEngine);
|
---|
167 | if (!q->isValid())
|
---|
168 | return -1;
|
---|
169 |
|
---|
170 | int n = -1;
|
---|
171 | int level = SOL_SOCKET; // default
|
---|
172 |
|
---|
173 | switch (opt) {
|
---|
174 | case QNativeSocketEngine::ReceiveBufferSocketOption:
|
---|
175 | n = SO_RCVBUF;
|
---|
176 | break;
|
---|
177 | case QNativeSocketEngine::SendBufferSocketOption:
|
---|
178 | n = SO_SNDBUF;
|
---|
179 | break;
|
---|
180 | case QNativeSocketEngine::NonBlockingSocketOption:
|
---|
181 | break;
|
---|
182 | case QNativeSocketEngine::BroadcastSocketOption:
|
---|
183 | break;
|
---|
184 | case QNativeSocketEngine::AddressReusable:
|
---|
185 | n = SO_REUSEADDR;
|
---|
186 | break;
|
---|
187 | case QNativeSocketEngine::BindExclusively:
|
---|
188 | return true;
|
---|
189 | case QNativeSocketEngine::ReceiveOutOfBandData:
|
---|
190 | n = SO_OOBINLINE;
|
---|
191 | break;
|
---|
192 | case QNativeSocketEngine::LowDelayOption:
|
---|
193 | level = IPPROTO_TCP;
|
---|
194 | n = TCP_NODELAY;
|
---|
195 | break;
|
---|
196 | case QNativeSocketEngine::KeepAliveOption:
|
---|
197 | n = SO_KEEPALIVE;
|
---|
198 | break;
|
---|
199 | }
|
---|
200 |
|
---|
201 | int v = -1;
|
---|
202 | int len = sizeof(v);
|
---|
203 | if (::getsockopt(socketDescriptor, level, n, (char *) &v, &len) != -1)
|
---|
204 | return v;
|
---|
205 |
|
---|
206 | return -1;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /*
|
---|
211 | Sets the socket option \a opt to \a v.
|
---|
212 | */
|
---|
213 | bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt, int v)
|
---|
214 | {
|
---|
215 | Q_Q(QNativeSocketEngine);
|
---|
216 | if (!q->isValid())
|
---|
217 | return false;
|
---|
218 |
|
---|
219 | int n = 0;
|
---|
220 | int level = SOL_SOCKET; // default
|
---|
221 |
|
---|
222 | switch (opt) {
|
---|
223 | case QNativeSocketEngine::ReceiveBufferSocketOption:
|
---|
224 | n = SO_RCVBUF;
|
---|
225 | break;
|
---|
226 | case QNativeSocketEngine::SendBufferSocketOption:
|
---|
227 | n = SO_SNDBUF;
|
---|
228 | break;
|
---|
229 | case QNativeSocketEngine::BroadcastSocketOption:
|
---|
230 | n = SO_BROADCAST;
|
---|
231 | break;
|
---|
232 | case QNativeSocketEngine::NonBlockingSocketOption: {
|
---|
233 | // Make the socket nonblocking.
|
---|
234 | int flags = ::fcntl(socketDescriptor, F_GETFL, 0);
|
---|
235 | if (flags == -1) {
|
---|
236 | #ifdef QNATIVESOCKETENGINE_DEBUG
|
---|
237 | perror("QNativeSocketEnginePrivate::setOption(): fcntl(F_GETFL) failed");
|
---|
238 | #endif
|
---|
239 | return false;
|
---|
240 | }
|
---|
241 | if (::fcntl(socketDescriptor, F_SETFL, flags | O_NONBLOCK) == -1) {
|
---|
242 | #ifdef QNATIVESOCKETENGINE_DEBUG
|
---|
243 | perror("QNativeSocketEnginePrivate::setOption(): fcntl(F_SETFL) failed");
|
---|
244 | #endif
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | return true;
|
---|
249 | }
|
---|
250 | case QNativeSocketEngine::AddressReusable:
|
---|
251 | #ifdef SO_REUSEPORT
|
---|
252 | n = SO_REUSEPORT;
|
---|
253 | #else
|
---|
254 | n = SO_REUSEADDR;
|
---|
255 | #endif
|
---|
256 | break;
|
---|
257 | case QNativeSocketEngine::BindExclusively:
|
---|
258 | return true;
|
---|
259 | case QNativeSocketEngine::ReceiveOutOfBandData:
|
---|
260 | n = SO_OOBINLINE;
|
---|
261 | break;
|
---|
262 | case QNativeSocketEngine::LowDelayOption:
|
---|
263 | level = IPPROTO_TCP;
|
---|
264 | n = TCP_NODELAY;
|
---|
265 | break;
|
---|
266 | case QNativeSocketEngine::KeepAliveOption:
|
---|
267 | n = SO_KEEPALIVE;
|
---|
268 | break;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return ::setsockopt(socketDescriptor, level, n, (char *) &v, sizeof(v)) == 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port)
|
---|
275 | {
|
---|
276 | #ifdef QNATIVESOCKETENGINE_DEBUG
|
---|
277 | qDebug("QNativeSocketEnginePrivate::nativeConnect() : %d ", socketDescriptor);
|
---|
278 | #endif
|
---|
279 |
|
---|
280 | struct sockaddr_in sockAddrIPv4;
|
---|
281 | struct sockaddr *sockAddrPtr = 0;
|
---|
282 | int sockAddrSize = 0;
|
---|
283 |
|
---|
284 | if (addr.protocol() == QAbstractSocket::IPv4Protocol) {
|
---|
285 | memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
|
---|
286 | sockAddrIPv4.sin_family = AF_INET;
|
---|
287 | sockAddrIPv4.sin_port = htons(port);
|
---|
288 | sockAddrIPv4.sin_addr.s_addr = htonl(addr.toIPv4Address());
|
---|
289 |
|
---|
290 | sockAddrSize = sizeof(sockAddrIPv4);
|
---|
291 | sockAddrPtr = (struct sockaddr *) &sockAddrIPv4;
|
---|
292 | } else {
|
---|
293 | // unreachable
|
---|
294 | }
|
---|
295 |
|
---|
296 | int connectResult = ::connect(socketDescriptor, sockAddrPtr, sockAddrSize);
|
---|
297 | if (connectResult == -1) {
|
---|
298 | switch (errno) {
|
---|
299 | case EISCONN:
|
---|
300 | socketState = QAbstractSocket::ConnectedState;
|
---|
301 | break;
|
---|
302 | case ECONNREFUSED:
|
---|
303 | case EINVAL:
|
---|
304 | setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString);
|
---|
305 | socketState = QAbstractSocket::UnconnectedState;
|
---|
306 | break;
|
---|
307 | case ETIMEDOUT:
|
---|
308 | setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString);
|
---|
309 | break;
|
---|
310 | case EHOSTUNREACH:
|
---|
311 | setError(QAbstractSocket::NetworkError, HostUnreachableErrorString);
|
---|
312 | socketState = QAbstractSocket::UnconnectedState;
|
---|
313 | break;
|
---|
314 | case ENETUNREACH:
|
---|
315 | setError(QAbstractSocket::NetworkError, NetworkUnreachableErrorString);
|
---|
316 | socketState = QAbstractSocket::UnconnectedState;
|
---|
317 | break;
|
---|
318 | case EADDRINUSE:
|
---|
319 | setError(QAbstractSocket::NetworkError, AddressInuseErrorString);
|
---|
320 | break;
|
---|
321 | case EINPROGRESS:
|
---|
322 | case EALREADY:
|
---|
323 | setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString);
|
---|
324 | socketState = QAbstractSocket::ConnectingState;
|
---|
325 | break;
|
---|
326 | case EAGAIN:
|
---|
327 | setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString);
|
---|
328 | setError(QAbstractSocket::SocketResourceError, ResourceErrorString);
|
---|
329 | break;
|
---|
330 | case EACCES:
|
---|
331 | case EPERM:
|
---|
332 | setError(QAbstractSocket::SocketAccessError, AccessErrorString);
|
---|
333 | socketState = QAbstractSocket::UnconnectedState;
|
---|
334 | break;
|
---|
335 | case EAFNOSUPPORT:
|
---|
336 | case EBADF:
|
---|
337 | case EFAULT:
|
---|
338 | case ENOTSOCK:
|
---|
339 | socketState = QAbstractSocket::UnconnectedState;
|
---|
340 | default:
|
---|
341 | break;
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (socketState != QAbstractSocket::ConnectedState) {
|
---|
345 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
346 | qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == false (%s)",
|
---|
347 | addr.toString().toLatin1().constData(), port,
|
---|
348 | socketState == QAbstractSocket::ConnectingState
|
---|
349 | ? "Connection in progress" : socketErrorString.toLatin1().constData());
|
---|
350 | #endif
|
---|
351 | return false;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
356 | qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == true",
|
---|
357 | addr.toString().toLatin1().constData(), port);
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | socketState = QAbstractSocket::ConnectedState;
|
---|
361 | return true;
|
---|
362 | }
|
---|
363 |
|
---|
364 | bool QNativeSocketEnginePrivate::nativeBind(const QHostAddress &address, quint16 port)
|
---|
365 | {
|
---|
366 | struct sockaddr_in sockAddrIPv4;
|
---|
367 | struct sockaddr *sockAddrPtr = 0;
|
---|
368 | int sockAddrSize = 0;
|
---|
369 |
|
---|
370 | if (address.protocol() == QAbstractSocket::IPv4Protocol) {
|
---|
371 | memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
|
---|
372 | sockAddrIPv4.sin_family = AF_INET;
|
---|
373 | sockAddrIPv4.sin_port = htons(port);
|
---|
374 | sockAddrIPv4.sin_addr.s_addr = htonl(address.toIPv4Address());
|
---|
375 | sockAddrSize = sizeof(sockAddrIPv4);
|
---|
376 | sockAddrPtr = (struct sockaddr *) &sockAddrIPv4;
|
---|
377 | } else {
|
---|
378 | // unreachable
|
---|
379 | }
|
---|
380 |
|
---|
381 | int bindResult = ::bind(socketDescriptor, sockAddrPtr, sockAddrSize);
|
---|
382 |
|
---|
383 | if (bindResult < 0) {
|
---|
384 | switch(errno) {
|
---|
385 | case EADDRINUSE:
|
---|
386 | setError(QAbstractSocket::AddressInUseError, AddressInuseErrorString);
|
---|
387 | break;
|
---|
388 | case EACCES:
|
---|
389 | setError(QAbstractSocket::SocketAccessError, AddressProtectedErrorString);
|
---|
390 | break;
|
---|
391 | case EINVAL:
|
---|
392 | setError(QAbstractSocket::UnsupportedSocketOperationError, OperationUnsupportedErrorString);
|
---|
393 | break;
|
---|
394 | case EADDRNOTAVAIL:
|
---|
395 | setError(QAbstractSocket::SocketAddressNotAvailableError, AddressNotAvailableErrorString);
|
---|
396 | break;
|
---|
397 | default:
|
---|
398 | break;
|
---|
399 | }
|
---|
400 |
|
---|
401 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
402 | qDebug("QNativeSocketEnginePrivate::nativeBind(%s, %i) == false (%s)",
|
---|
403 | address.toString().toLatin1().constData(), port, socketErrorString.toLatin1().constData());
|
---|
404 | #endif
|
---|
405 |
|
---|
406 | return false;
|
---|
407 | }
|
---|
408 |
|
---|
409 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
410 | qDebug("QNativeSocketEnginePrivate::nativeBind(%s, %i) == true",
|
---|
411 | address.toString().toLatin1().constData(), port);
|
---|
412 | #endif
|
---|
413 | socketState = QAbstractSocket::BoundState;
|
---|
414 | return true;
|
---|
415 | }
|
---|
416 |
|
---|
417 | bool QNativeSocketEnginePrivate::nativeListen(int backlog)
|
---|
418 | {
|
---|
419 | if (::listen(socketDescriptor, backlog) < 0) {
|
---|
420 | switch (errno) {
|
---|
421 | case EADDRINUSE:
|
---|
422 | setError(QAbstractSocket::AddressInUseError,
|
---|
423 | PortInuseErrorString);
|
---|
424 | break;
|
---|
425 | default:
|
---|
426 | break;
|
---|
427 | }
|
---|
428 |
|
---|
429 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
430 | qDebug("QNativeSocketEnginePrivate::nativeListen(%i) == false (%s)",
|
---|
431 | backlog, socketErrorString.toLatin1().constData());
|
---|
432 | #endif
|
---|
433 | return false;
|
---|
434 | }
|
---|
435 |
|
---|
436 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
437 | qDebug("QNativeSocketEnginePrivate::nativeListen(%i) == true", backlog);
|
---|
438 | #endif
|
---|
439 |
|
---|
440 | socketState = QAbstractSocket::ListeningState;
|
---|
441 | return true;
|
---|
442 | }
|
---|
443 |
|
---|
444 | int QNativeSocketEnginePrivate::nativeAccept()
|
---|
445 | {
|
---|
446 | int acceptedDescriptor = ::accept(socketDescriptor, 0, 0);
|
---|
447 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
448 | qDebug("QNativeSocketEnginePrivate::nativeAccept() == %i", acceptedDescriptor);
|
---|
449 | #endif
|
---|
450 | //check if we have vaild descriptor at all
|
---|
451 | if(acceptedDescriptor > 0) {
|
---|
452 | // Ensure that the socket is closed on exec*()
|
---|
453 | ::fcntl(acceptedDescriptor, F_SETFD, FD_CLOEXEC);
|
---|
454 | }
|
---|
455 | return acceptedDescriptor;
|
---|
456 | }
|
---|
457 |
|
---|
458 | qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const
|
---|
459 | {
|
---|
460 | int nbytes = 0;
|
---|
461 | // gives shorter than true amounts on Unix domain sockets.
|
---|
462 | qint64 available = 0;
|
---|
463 | if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
|
---|
464 | available = (qint64) nbytes;
|
---|
465 |
|
---|
466 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
467 | qDebug("QNativeSocketEnginePrivate::nativeBytesAvailable() == %lli", available);
|
---|
468 | #endif
|
---|
469 | return available;
|
---|
470 | }
|
---|
471 |
|
---|
472 | bool QNativeSocketEnginePrivate::nativeHasPendingDatagrams() const
|
---|
473 | {
|
---|
474 | // Create a sockaddr struct and reset its port number.
|
---|
475 | qt_sockaddr storage;
|
---|
476 | int storageSize = sizeof(storage);
|
---|
477 | memset(&storage, 0, storageSize);
|
---|
478 |
|
---|
479 | // Peek 0 bytes into the next message. The size of the message may
|
---|
480 | // well be 0, so we can't check recvfrom's return value.
|
---|
481 | ssize_t readBytes;
|
---|
482 | do {
|
---|
483 | char c;
|
---|
484 | readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, &storage.a, &storageSize);
|
---|
485 | } while (readBytes == -1 && errno == EINTR);
|
---|
486 |
|
---|
487 | // If there's no error, or if our buffer was too small, there must be a
|
---|
488 | // pending datagram.
|
---|
489 | bool result = (readBytes != -1) || errno == EMSGSIZE;
|
---|
490 |
|
---|
491 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
492 | qDebug("QNativeSocketEnginePrivate::nativeHasPendingDatagrams() == %s",
|
---|
493 | result ? "true" : "false");
|
---|
494 | #endif
|
---|
495 | return result;
|
---|
496 | }
|
---|
497 |
|
---|
498 | qint64 QNativeSocketEnginePrivate::nativePendingDatagramSize() const
|
---|
499 | {
|
---|
500 | QVarLengthArray<char, 8192> udpMessagePeekBuffer(8192);
|
---|
501 | ssize_t recvResult = -1;
|
---|
502 |
|
---|
503 | for (;;) {
|
---|
504 | // the data written to udpMessagePeekBuffer is discarded, so
|
---|
505 | // this function is still reentrant although it might not look
|
---|
506 | // so.
|
---|
507 | recvResult = ::recv(socketDescriptor, udpMessagePeekBuffer.data(),
|
---|
508 | udpMessagePeekBuffer.size(), MSG_PEEK);
|
---|
509 | if (recvResult == -1 && errno == EINTR)
|
---|
510 | continue;
|
---|
511 |
|
---|
512 | if (recvResult != (ssize_t) udpMessagePeekBuffer.size())
|
---|
513 | break;
|
---|
514 |
|
---|
515 | udpMessagePeekBuffer.resize(udpMessagePeekBuffer.size() * 2);
|
---|
516 | }
|
---|
517 |
|
---|
518 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
519 | qDebug("QNativeSocketEnginePrivate::nativePendingDatagramSize() == %i", recvResult);
|
---|
520 | #endif
|
---|
521 |
|
---|
522 | return qint64(recvResult);
|
---|
523 | }
|
---|
524 |
|
---|
525 | qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxSize,
|
---|
526 | QHostAddress *address, quint16 *port)
|
---|
527 | {
|
---|
528 | qt_sockaddr aa;
|
---|
529 | memset(&aa, 0, sizeof(aa));
|
---|
530 | int sz;
|
---|
531 | sz = sizeof(aa);
|
---|
532 |
|
---|
533 | ssize_t recvFromResult = 0;
|
---|
534 | do {
|
---|
535 | char c;
|
---|
536 | recvFromResult = ::recvfrom(socketDescriptor, maxSize ? data : &c, maxSize ? maxSize : 1,
|
---|
537 | 0, &aa.a, &sz);
|
---|
538 | } while (recvFromResult == -1 && errno == EINTR);
|
---|
539 |
|
---|
540 | if (recvFromResult == -1) {
|
---|
541 | setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString);
|
---|
542 | } else if (port || address) {
|
---|
543 | qt_socket_getPortAndAddress(&aa, port, address);
|
---|
544 | }
|
---|
545 |
|
---|
546 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
547 | qDebug("QNativeSocketEnginePrivate::nativeReceiveDatagram(%p \"%s\", %lli, %s, %i) == %lli",
|
---|
548 | data, qt_prettyDebug(data, qMin(recvFromResult, ssize_t(16)), recvFromResult).data(), maxSize,
|
---|
549 | address ? address->toString().toLatin1().constData() : "(nil)",
|
---|
550 | port ? *port : 0, (qint64) recvFromResult);
|
---|
551 | #endif
|
---|
552 |
|
---|
553 | return qint64(maxSize ? recvFromResult : recvFromResult == -1 ? -1 : 0);
|
---|
554 | }
|
---|
555 |
|
---|
556 | qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 len,
|
---|
557 | const QHostAddress &host, quint16 port)
|
---|
558 | {
|
---|
559 | struct sockaddr_in sockAddrIPv4;
|
---|
560 | struct sockaddr *sockAddrPtr = 0;
|
---|
561 | int sockAddrSize = 0;
|
---|
562 |
|
---|
563 | if (host.protocol() == QAbstractSocket::IPv4Protocol) {
|
---|
564 | memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
|
---|
565 | sockAddrIPv4.sin_family = AF_INET;
|
---|
566 | sockAddrIPv4.sin_port = htons(port);
|
---|
567 | sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address());
|
---|
568 | sockAddrSize = sizeof(sockAddrIPv4);
|
---|
569 | sockAddrPtr = (struct sockaddr *)&sockAddrIPv4;
|
---|
570 | }
|
---|
571 |
|
---|
572 | // ignore the SIGPIPE signal
|
---|
573 | qt_ignore_sigpipe();
|
---|
574 | ssize_t sentBytes = ::sendto(socketDescriptor, data, len,
|
---|
575 | 0, sockAddrPtr, sockAddrSize);
|
---|
576 |
|
---|
577 | if (sentBytes < 0) {
|
---|
578 | switch (errno) {
|
---|
579 | case EMSGSIZE:
|
---|
580 | setError(QAbstractSocket::DatagramTooLargeError, DatagramTooLargeErrorString);
|
---|
581 | break;
|
---|
582 | default:
|
---|
583 | setError(QAbstractSocket::NetworkError, SendDatagramErrorString);
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
588 | qDebug("QNativeSocketEngine::sendDatagram(%p \"%s\", %lli, \"%s\", %i) == %lli", data,
|
---|
589 | qt_prettyDebug(data, qMin<int>(len, 16), len).data(), len, host.toString().toLatin1().constData(),
|
---|
590 | port, (qint64) sentBytes);
|
---|
591 | #endif
|
---|
592 |
|
---|
593 | return qint64(sentBytes);
|
---|
594 | }
|
---|
595 |
|
---|
596 | bool QNativeSocketEnginePrivate::fetchConnectionParameters()
|
---|
597 | {
|
---|
598 | localPort = 0;
|
---|
599 | localAddress.clear();
|
---|
600 | peerPort = 0;
|
---|
601 | peerAddress.clear();
|
---|
602 |
|
---|
603 | if (socketDescriptor == -1)
|
---|
604 | return false;
|
---|
605 |
|
---|
606 | qt_sockaddr sa;
|
---|
607 | int sockAddrSize = sizeof(sa);
|
---|
608 |
|
---|
609 | // Determine local address
|
---|
610 | memset(&sa, 0, sizeof(sa));
|
---|
611 | if (::getsockname(socketDescriptor, &sa.a, &sockAddrSize) == 0) {
|
---|
612 | qt_socket_getPortAndAddress(&sa, &localPort, &localAddress);
|
---|
613 |
|
---|
614 | // Determine protocol family
|
---|
615 | switch (sa.a.sa_family) {
|
---|
616 | case AF_INET:
|
---|
617 | socketProtocol = QAbstractSocket::IPv4Protocol;
|
---|
618 | break;
|
---|
619 | default:
|
---|
620 | socketProtocol = QAbstractSocket::UnknownNetworkLayerProtocol;
|
---|
621 | break;
|
---|
622 | }
|
---|
623 |
|
---|
624 | } else if (errno == EBADF) {
|
---|
625 | setError(QAbstractSocket::UnsupportedSocketOperationError, InvalidSocketErrorString);
|
---|
626 | return false;
|
---|
627 | }
|
---|
628 |
|
---|
629 | // Determine the remote address
|
---|
630 | if (!::getpeername(socketDescriptor, &sa.a, &sockAddrSize))
|
---|
631 | qt_socket_getPortAndAddress(&sa, &peerPort, &peerAddress);
|
---|
632 |
|
---|
633 | // Determine the socket type (UDP/TCP)
|
---|
634 | int value = 0;
|
---|
635 | int valueSize = sizeof(int);
|
---|
636 | if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, &value, &valueSize) == 0) {
|
---|
637 | if (value == SOCK_STREAM)
|
---|
638 | socketType = QAbstractSocket::TcpSocket;
|
---|
639 | else if (value == SOCK_DGRAM)
|
---|
640 | socketType = QAbstractSocket::UdpSocket;
|
---|
641 | else
|
---|
642 | socketType = QAbstractSocket::UnknownSocketType;
|
---|
643 | }
|
---|
644 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
645 | QString socketProtocolStr = "UnknownProtocol";
|
---|
646 | if (socketProtocol == QAbstractSocket::IPv4Protocol) socketProtocolStr = "IPv4Protocol";
|
---|
647 | else if (socketProtocol == QAbstractSocket::IPv6Protocol) socketProtocolStr = "IPv6Protocol";
|
---|
648 |
|
---|
649 | QString socketTypeStr = "UnknownSocketType";
|
---|
650 | if (socketType == QAbstractSocket::TcpSocket) socketTypeStr = "TcpSocket";
|
---|
651 | else if (socketType == QAbstractSocket::UdpSocket) socketTypeStr = "UdpSocket";
|
---|
652 |
|
---|
653 | qDebug("QNativeSocketEnginePrivate::fetchConnectionParameters() local == %s:%i,"
|
---|
654 | " peer == %s:%i, socket == %s - %s",
|
---|
655 | localAddress.toString().toLatin1().constData(), localPort,
|
---|
656 | peerAddress.toString().toLatin1().constData(), peerPort,socketTypeStr.toLatin1().constData(),
|
---|
657 | socketProtocolStr.toLatin1().constData());
|
---|
658 | #endif
|
---|
659 | return true;
|
---|
660 | }
|
---|
661 |
|
---|
662 | void QNativeSocketEnginePrivate::nativeClose()
|
---|
663 | {
|
---|
664 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
665 | qDebug("QNativeSocketEngine::nativeClose()");
|
---|
666 | #endif
|
---|
667 | ::close(socketDescriptor);
|
---|
668 | }
|
---|
669 |
|
---|
670 | qint64 QNativeSocketEnginePrivate::nativeWrite(const char *data, qint64 len)
|
---|
671 | {
|
---|
672 | Q_Q(QNativeSocketEngine);
|
---|
673 |
|
---|
674 | // ignore the SIGPIPE signal
|
---|
675 | qt_ignore_sigpipe();
|
---|
676 |
|
---|
677 | // loop while ::write() returns -1 and errno == EINTR, in case
|
---|
678 | // of an interrupting signal.
|
---|
679 | ssize_t writtenBytes;
|
---|
680 | do {
|
---|
681 | writtenBytes = ::write(socketDescriptor, data, len);
|
---|
682 | } while (writtenBytes < 0 && errno == EINTR);
|
---|
683 |
|
---|
684 | if (writtenBytes < 0) {
|
---|
685 | switch (errno) {
|
---|
686 | case EPIPE:
|
---|
687 | case ECONNRESET:
|
---|
688 | writtenBytes = -1;
|
---|
689 | setError(QAbstractSocket::RemoteHostClosedError, RemoteHostClosedErrorString);
|
---|
690 | q->close();
|
---|
691 | break;
|
---|
692 | case EAGAIN:
|
---|
693 | writtenBytes = 0;
|
---|
694 | break;
|
---|
695 | case EMSGSIZE:
|
---|
696 | setError(QAbstractSocket::DatagramTooLargeError, DatagramTooLargeErrorString);
|
---|
697 | break;
|
---|
698 | default:
|
---|
699 | break;
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
704 | qDebug("QNativeSocketEnginePrivate::nativeWrite(%p \"%s\", %llu) == %i",
|
---|
705 | data, qt_prettyDebug(data, qMin((int) len, 16),
|
---|
706 | (int) len).data(), len, (int) writtenBytes);
|
---|
707 | #endif
|
---|
708 |
|
---|
709 | return qint64(writtenBytes);
|
---|
710 | }
|
---|
711 | /*
|
---|
712 | */
|
---|
713 | qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize)
|
---|
714 | {
|
---|
715 | Q_Q(QNativeSocketEngine);
|
---|
716 | if (!q->isValid()) {
|
---|
717 | qWarning("QNativeSocketEngine::unbufferedRead: Invalid socket");
|
---|
718 | return -1;
|
---|
719 | }
|
---|
720 |
|
---|
721 | ssize_t r = 0;
|
---|
722 | do {
|
---|
723 | r = ::read(socketDescriptor, data, maxSize);
|
---|
724 | } while (r == -1 && errno == EINTR);
|
---|
725 |
|
---|
726 | if (r < 0) {
|
---|
727 | r = -1;
|
---|
728 | switch (errno) {
|
---|
729 | #if EWOULDBLOCK-0 && EWOULDBLOCK != EAGAIN
|
---|
730 | case EWOULDBLOCK:
|
---|
731 | #endif
|
---|
732 | case EAGAIN:
|
---|
733 | // No data was available for reading
|
---|
734 | r = -2;
|
---|
735 | break;
|
---|
736 | case EBADF:
|
---|
737 | case EINVAL:
|
---|
738 | case EIO:
|
---|
739 | setError(QAbstractSocket::NetworkError, ReadErrorString);
|
---|
740 | break;
|
---|
741 | case ECONNRESET:
|
---|
742 | r = 0;
|
---|
743 | break;
|
---|
744 | default:
|
---|
745 | break;
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | #if defined (QNATIVESOCKETENGINE_DEBUG)
|
---|
750 | qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %llu) == %i",
|
---|
751 | data, qt_prettyDebug(data, qMin(r, ssize_t(16)), r).data(),
|
---|
752 | maxSize, r);
|
---|
753 | #endif
|
---|
754 |
|
---|
755 | return qint64(r);
|
---|
756 | }
|
---|
757 |
|
---|
758 | int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const
|
---|
759 | {
|
---|
760 | fd_set fds;
|
---|
761 | FD_ZERO(&fds);
|
---|
762 | FD_SET(socketDescriptor, &fds);
|
---|
763 |
|
---|
764 | struct timeval tv;
|
---|
765 | tv.tv_sec = timeout / 1000;
|
---|
766 | tv.tv_usec = (timeout % 1000) * 1000;
|
---|
767 |
|
---|
768 | int retval;
|
---|
769 | if (selectForRead)
|
---|
770 | retval = ::select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
|
---|
771 | else
|
---|
772 | retval = ::select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
|
---|
773 |
|
---|
774 | return retval;
|
---|
775 | }
|
---|
776 |
|
---|
777 | int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
|
---|
778 | bool *selectForRead, bool *selectForWrite) const
|
---|
779 | {
|
---|
780 | fd_set fdread;
|
---|
781 | FD_ZERO(&fdread);
|
---|
782 | if (checkRead)
|
---|
783 | FD_SET(socketDescriptor, &fdread);
|
---|
784 |
|
---|
785 | fd_set fdwrite;
|
---|
786 | FD_ZERO(&fdwrite);
|
---|
787 | if (checkWrite)
|
---|
788 | FD_SET(socketDescriptor, &fdwrite);
|
---|
789 |
|
---|
790 | struct timeval tv;
|
---|
791 | tv.tv_sec = timeout / 1000;
|
---|
792 | tv.tv_usec = (timeout % 1000) * 1000;
|
---|
793 |
|
---|
794 | int ret;
|
---|
795 | QTime timer;
|
---|
796 | timer.start();
|
---|
797 |
|
---|
798 | do {
|
---|
799 | ret = ::select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
|
---|
800 |
|
---|
801 | if (ret != -1 || errno != EINTR)
|
---|
802 | break;
|
---|
803 |
|
---|
804 | if (timeout > 0) {
|
---|
805 | // recalculate the timeout
|
---|
806 | int t = timeout - timer.elapsed();
|
---|
807 | if (t < 0) {
|
---|
808 | // oops, timeout turned negative?
|
---|
809 | ret = -1;
|
---|
810 | break;
|
---|
811 | }
|
---|
812 |
|
---|
813 | tv.tv_sec = t / 1000;
|
---|
814 | tv.tv_usec = (t % 1000) * 1000;
|
---|
815 | }
|
---|
816 | } while (true);
|
---|
817 |
|
---|
818 | if (ret <= 0)
|
---|
819 | return ret;
|
---|
820 |
|
---|
821 | *selectForRead = FD_ISSET(socketDescriptor, &fdread);
|
---|
822 | *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite);
|
---|
823 |
|
---|
824 | return ret;
|
---|
825 | }
|
---|
826 |
|
---|
827 | QT_END_NAMESPACE
|
---|