// test only for posix void tst_QSocketNotifier::posixSockets() { QTcpServer server; QVERIFY(server.listen(QHostAddress::LocalHost, 0)); int posixSocket = qt_safe_socket(AF_INET, SOCK_STREAM, 0); sockaddr_in addr; addr.sin_addr.s_addr = htonl(0x7f000001); addr.sin_family = AF_INET; addr.sin_port = htons(server.serverPort()); qt_safe_connect(posixSocket, (const struct sockaddr*)&addr, sizeof(sockaddr_in)); QVERIFY(server.waitForNewConnection(5000)); QScopedPointer<QTcpSocket> passive(server.nextPendingConnection()); ::fcntl(posixSocket, F_SETFL, ::fcntl(posixSocket, F_GETFL) | O_NONBLOCK); { QSocketNotifier rn(posixSocket, QSocketNotifier::Read); connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy readSpy(&rn, SIGNAL(activated(int))); QVERIFY(readSpy.isValid()); // No write notifier, some systems trigger write notification on socket creation, but not all QSocketNotifier en(posixSocket, QSocketNotifier::Exception); connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy errorSpy(&en, SIGNAL(activated(int))); QVERIFY(errorSpy.isValid()); passive->write("hello",6); passive->waitForBytesWritten(5000); QTestEventLoop::instance().enterLoop(3); QCOMPARE(readSpy.count(), 1); QCOMPARE(errorSpy.count(), 0); char buffer[100]; int r = qt_safe_read(posixSocket, buffer, 100); QCOMPARE(r, 6); QCOMPARE(buffer, "hello"); QSocketNotifier wn(posixSocket, QSocketNotifier::Write); connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy writeSpy(&wn, SIGNAL(activated(int))); QVERIFY(writeSpy.isValid()); qt_safe_write(posixSocket, "goodbye", 8); QTestEventLoop::instance().enterLoop(3); QCOMPARE(readSpy.count(), 1); QCOMPARE(writeSpy.count(), 1); QCOMPARE(errorSpy.count(), 0); // Write notifier may have fired before the read notifier inside // QTcpSocket, give QTcpSocket a chance to see the incoming data passive->waitForReadyRead(100); QCOMPARE(passive->readAll(), QByteArray("goodbye",8)); } qt_safe_close(posixSocket); }
void tst_QSocketNotifier::posixSockets() { #ifndef Q_OS_UNIX QSKIP("test only for posix", SkipAll); #else QTcpServer server; QVERIFY(server.listen(QHostAddress::LocalHost, 0)); int posixSocket = qt_safe_socket(AF_INET, SOCK_STREAM, 0); sockaddr_in addr; addr.sin_addr.s_addr = htonl(0x7f000001); addr.sin_family = AF_INET; addr.sin_port = htons(server.serverPort()); qt_safe_connect(posixSocket, (const struct sockaddr*)&addr, sizeof(sockaddr_in)); QVERIFY(server.waitForNewConnection(5000)); QScopedPointer<QTcpSocket> passive(server.nextPendingConnection()); ::fcntl(posixSocket, F_SETFL, ::fcntl(posixSocket, F_GETFL) | O_NONBLOCK); { QSocketNotifier rn(posixSocket, QSocketNotifier::Read); connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy readSpy(&rn, SIGNAL(activated(int))); QSocketNotifier wn(posixSocket, QSocketNotifier::Write); connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy writeSpy(&wn, SIGNAL(activated(int))); QSocketNotifier en(posixSocket, QSocketNotifier::Exception); connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy errorSpy(&en, SIGNAL(activated(int))); passive->write("hello",6); passive->waitForBytesWritten(5000); QTestEventLoop::instance().enterLoop(3); QCOMPARE(readSpy.count(), 1); writeSpy.clear(); //depending on OS, write notifier triggers on creation or not. QCOMPARE(errorSpy.count(), 0); char buffer[100]; qt_safe_read(posixSocket, buffer, 100); QCOMPARE(buffer, "hello"); qt_safe_write(posixSocket, "goodbye", 8); QTestEventLoop::instance().enterLoop(3); QCOMPARE(readSpy.count(), 1); QCOMPARE(writeSpy.count(), 1); QCOMPARE(errorSpy.count(), 0); QCOMPARE(passive->readAll(), QByteArray("goodbye",8)); } qt_safe_close(posixSocket); #endif }
bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port) { #ifdef QNATIVESOCKETENGINE_DEBUG qDebug("QNativeSocketEnginePrivate::nativeConnect() : %d ", socketDescriptor); #endif struct sockaddr_in sockAddrIPv4; struct sockaddr *sockAddrPtr = 0; QT_SOCKLEN_T sockAddrSize = 0; struct sockaddr_in6 sockAddrIPv6; if (addr.protocol() == QAbstractSocket::IPv6Protocol) { memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); QString scopeid = addr.scopeId(); bool ok; sockAddrIPv6.sin6_scope_id = scopeid.toInt(&ok); #ifndef QT_NO_IPV6IFNAME if (!ok) sockAddrIPv6.sin6_scope_id = ::if_nametoindex(scopeid.toLatin1()); #endif Q_IPV6ADDR ip6 = addr.toIPv6Address(); memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &ip6, sizeof(ip6)); sockAddrSize = sizeof(sockAddrIPv6); sockAddrPtr = (struct sockaddr *) &sockAddrIPv6; } else if (addr.protocol() == QAbstractSocket::IPv4Protocol) { memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); sockAddrIPv4.sin_family = AF_INET; sockAddrIPv4.sin_port = htons(port); sockAddrIPv4.sin_addr.s_addr = htonl(addr.toIPv4Address()); sockAddrSize = sizeof(sockAddrIPv4); sockAddrPtr = (struct sockaddr *) &sockAddrIPv4; } else { // unreachable } int connectResult = qt_safe_connect(socketDescriptor, sockAddrPtr, sockAddrSize); if (connectResult == -1) { switch (errno) { case EISCONN: socketState = QAbstractSocket::ConnectedState; break; case ECONNREFUSED: case EINVAL: setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString); socketState = QAbstractSocket::UnconnectedState; break; case ETIMEDOUT: setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString); break; case EHOSTUNREACH: setError(QAbstractSocket::NetworkError, HostUnreachableErrorString); socketState = QAbstractSocket::UnconnectedState; break; case ENETUNREACH: setError(QAbstractSocket::NetworkError, NetworkUnreachableErrorString); socketState = QAbstractSocket::UnconnectedState; break; case EADDRINUSE: setError(QAbstractSocket::NetworkError, AddressInuseErrorString); break; case EINPROGRESS: case EALREADY: setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); socketState = QAbstractSocket::ConnectingState; break; case EAGAIN: setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); setError(QAbstractSocket::SocketResourceError, ResourceErrorString); break; case EACCES: case EPERM: setError(QAbstractSocket::SocketAccessError, AccessErrorString); socketState = QAbstractSocket::UnconnectedState; break; case EAFNOSUPPORT: case EBADF: case EFAULT: case ENOTSOCK: socketState = QAbstractSocket::UnconnectedState; default: break; } if (socketState != QAbstractSocket::ConnectedState) { #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == false (%s)", addr.toString().toLatin1().constData(), port, socketState == QAbstractSocket::ConnectingState ? "Connection in progress" : socketErrorString.toLatin1().constData()); #endif return false; } } #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == true", addr.toString().toLatin1().constData(), port); #endif socketState = QAbstractSocket::ConnectedState; return true; }
bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port) { #ifdef QNATIVESOCKETENGINE_DEBUG qDebug() << "QNativeSocketEnginePrivate::nativeConnect() " << socketDescriptor; #endif qt_sockaddr aa; QT_SOCKLEN_T sockAddrSize; setPortAndAddress(port, addr, &aa, &sockAddrSize); int connectResult = qt_safe_connect(socketDescriptor, &aa.a, sockAddrSize); #if defined (QNATIVESOCKETENGINE_DEBUG) int ecopy = errno; #endif if (connectResult == -1) { switch (errno) { case EISCONN: socketState = QAbstractSocket::ConnectedState; break; case ECONNREFUSED: case EINVAL: setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString); socketState = QAbstractSocket::UnconnectedState; break; case ETIMEDOUT: setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString); break; case EHOSTUNREACH: setError(QAbstractSocket::NetworkError, HostUnreachableErrorString); socketState = QAbstractSocket::UnconnectedState; break; case ENETUNREACH: setError(QAbstractSocket::NetworkError, NetworkUnreachableErrorString); socketState = QAbstractSocket::UnconnectedState; break; case EADDRINUSE: setError(QAbstractSocket::NetworkError, AddressInuseErrorString); break; case EINPROGRESS: case EALREADY: setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); socketState = QAbstractSocket::ConnectingState; break; case EAGAIN: setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); break; case EACCES: case EPERM: setError(QAbstractSocket::SocketAccessError, AccessErrorString); socketState = QAbstractSocket::UnconnectedState; break; case EAFNOSUPPORT: case EBADF: case EFAULT: case ENOTSOCK: socketState = QAbstractSocket::UnconnectedState; default: break; } if (socketState != QAbstractSocket::ConnectedState) { #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == false (%s)", addr.toString().toLatin1().constData(), port, socketState == QAbstractSocket::ConnectingState ? "Connection in progress" : strerror(ecopy)); #endif return false; } } #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == true", addr.toString().toLatin1().constData(), port); #endif socketState = QAbstractSocket::ConnectedState; return true; }
/* Sets the socket option \a opt to \a v. */ bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt, int v) { Q_Q(QNativeSocketEngine); if (!q->isValid()) return false; int n = 0; int level = SOL_SOCKET; // default switch (opt) { case QNativeSocketEngine::ReceiveBufferSocketOption: n = SO_RCVBUF; break; case QNativeSocketEngine::SendBufferSocketOption: n = SO_SNDBUF; break; case QNativeSocketEngine::BroadcastSocketOption: n = SO_BROADCAST; break; case QNativeSocketEngine::NonBlockingSocketOption: { // Make the socket nonblocking. #if !defined(Q_OS_VXWORKS) int flags = ::fcntl(socketDescriptor, F_GETFL, 0); if (flags == -1) { #ifdef QNATIVESOCKETENGINE_DEBUG perror("QNativeSocketEnginePrivate::setOption(): fcntl(F_GETFL) failed"); #endif return false; } if (::fcntl(socketDescriptor, F_SETFL, flags | O_NONBLOCK) == -1) { #ifdef QNATIVESOCKETENGINE_DEBUG perror("QNativeSocketEnginePrivate::setOption(): fcntl(F_SETFL) failed"); #endif return false; } #else // Q_OS_VXWORKS int onoff = 1; #ifdef Q_OS_SYMBIAN if (::ioctl(socketDescriptor, FIONBIO, &onoff) < 0) { #else if (qt_safe_ioctl(socketDescriptor, FIONBIO, &onoff) < 0) { #endif #ifdef QNATIVESOCKETENGINE_DEBUG perror("QNativeSocketEnginePrivate::setOption(): ioctl(FIONBIO, 1) failed"); #endif return false; } #endif // Q_OS_VXWORKS return true; } case QNativeSocketEngine::AddressReusable: #if defined(SO_REUSEPORT) && !defined(Q_OS_SYMBIAN) n = SO_REUSEPORT; #else n = SO_REUSEADDR; #endif break; case QNativeSocketEngine::BindExclusively: return true; case QNativeSocketEngine::ReceiveOutOfBandData: n = SO_OOBINLINE; break; case QNativeSocketEngine::LowDelayOption: level = IPPROTO_TCP; n = TCP_NODELAY; break; case QNativeSocketEngine::KeepAliveOption: n = SO_KEEPALIVE; break; } return ::setsockopt(socketDescriptor, level, n, (char *) &v, sizeof(v)) == 0; } bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port) { #ifdef QNATIVESOCKETENGINE_DEBUG qDebug("QNativeSocketEnginePrivate::nativeConnect() : %d ", socketDescriptor); #endif struct sockaddr_in sockAddrIPv4; struct sockaddr *sockAddrPtr = 0; QT_SOCKLEN_T sockAddrSize = 0; #if !defined(QT_NO_IPV6) struct sockaddr_in6 sockAddrIPv6; if (addr.protocol() == QAbstractSocket::IPv6Protocol) { memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(port); #ifndef QT_NO_IPV6IFNAME sockAddrIPv6.sin6_scope_id = ::if_nametoindex(addr.scopeId().toLatin1().data()); #else sockAddrIPv6.sin6_scope_id = addr.scopeId().toInt(); #endif Q_IPV6ADDR ip6 = addr.toIPv6Address(); memcpy(&sockAddrIPv6.sin6_addr.s6_addr, &ip6, sizeof(ip6)); sockAddrSize = sizeof(sockAddrIPv6); sockAddrPtr = (struct sockaddr *) &sockAddrIPv6; } else #if 0 {} #endif #endif if (addr.protocol() == QAbstractSocket::IPv4Protocol) { memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); sockAddrIPv4.sin_family = AF_INET; sockAddrIPv4.sin_port = htons(port); sockAddrIPv4.sin_addr.s_addr = htonl(addr.toIPv4Address()); sockAddrSize = sizeof(sockAddrIPv4); sockAddrPtr = (struct sockaddr *) &sockAddrIPv4; } else { // unreachable } #ifdef Q_OS_SYMBIAN int connectResult = ::connect(socketDescriptor, sockAddrPtr, sockAddrSize); #else int connectResult = qt_safe_connect(socketDescriptor, sockAddrPtr, sockAddrSize); #endif if (connectResult == -1) { switch (errno) { case EISCONN: socketState = QAbstractSocket::ConnectedState; break; case ECONNREFUSED: case EINVAL: setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString); socketState = QAbstractSocket::UnconnectedState; break; case ETIMEDOUT: setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString); break; case EHOSTUNREACH: setError(QAbstractSocket::NetworkError, HostUnreachableErrorString); socketState = QAbstractSocket::UnconnectedState; break; case ENETUNREACH: setError(QAbstractSocket::NetworkError, NetworkUnreachableErrorString); socketState = QAbstractSocket::UnconnectedState; break; case EADDRINUSE: setError(QAbstractSocket::NetworkError, AddressInuseErrorString); break; case EINPROGRESS: case EALREADY: setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); socketState = QAbstractSocket::ConnectingState; break; case EAGAIN: setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString); setError(QAbstractSocket::SocketResourceError, ResourceErrorString); break; case EACCES: case EPERM: setError(QAbstractSocket::SocketAccessError, AccessErrorString); socketState = QAbstractSocket::UnconnectedState; break; case EAFNOSUPPORT: case EBADF: case EFAULT: case ENOTSOCK: socketState = QAbstractSocket::UnconnectedState; default: break; } if (socketState != QAbstractSocket::ConnectedState) { #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == false (%s)", addr.toString().toLatin1().constData(), port, socketState == QAbstractSocket::ConnectingState ? "Connection in progress" : socketErrorString.toLatin1().constData()); #endif return false; } } #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeConnect(%s, %i) == true", addr.toString().toLatin1().constData(), port); #endif socketState = QAbstractSocket::ConnectedState; return true; }