Example #1
0
void SmtpConnectionHandler::run()
{
	QTcpSocket tcpSocket; 
	tcpSocket.setSocketDescriptor( _socketDescriptor );

	QString from;
	QString replyAddress;
	QStringList destinations;
	QByteArray mailContent;
	ByteArrayList attachments;

	bool result = receiveMail( tcpSocket, from, replyAddress, destinations, mailContent, attachments );
	tcpSocket.abort();
	if( false == result )
		return;


	// at this point we make some checks to ensure that all the parameters we have are ok

	result = checkMailAddress( from );
	if( false == result )
		return;

	result = checkMailAddress( replyAddress );
	if( false == result )
		return;

	foreach( QString destination, destinations )
	{
		result = checkMailAddress( destination );
		if( false == result )
			return;
	}
Example #2
0
void ApiServer::stopListening()
{
    DEBUG_LOW_LEVEL << Q_FUNC_INFO;

    // Closes the server. The server will no longer listen for incoming connections.
    close();

    m_lockedClient = NULL;

    emit updateDeviceLockStatus(Api::DeviceUnlocked);

    QMap<QTcpSocket*, ClientInfo>::iterator i;
    for (i = m_clients.begin(); i != m_clients.end(); ++i)
    {
        QTcpSocket * client = dynamic_cast<QTcpSocket*>(i.key());

        disconnect(client, SIGNAL(readyRead()), this, SLOT(clientProcessCommands()));
        disconnect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));

        client->abort();
        client->deleteLater();
    }

    m_clients.clear();
}
Example #3
0
void MT500::testConnection()
{
    for(int i = 0; i < 6; i++) {
        if(!ipArray[i].ip.isEmpty()){
            QString msg = "[" + QDateTime::currentDateTime().toString("MM/dd/yyyy hh:mm:ss") + "] ";
            QTcpSocket * test = new QTcpSocket;
            test->connectToHost(QHostAddress(ipArray[i].ip), ipArray[i].port);
            if(test->waitForConnected(2000)) {
                msg += ipArray[i].ip + ": Test Message Sent";
                ui->testBrowser->append("<font color=green>" + msg + "</font>");
                test->abort();
            }
            else {
                msg += ipArray[i].ip + ": Failed to Connect";
                ui->testBrowser->append("<font color=red>" + msg + "</font>");
            }
        }
    }
    if(!initial) {
        QString fips = fipsNo.trimmed().remove("VAZ");
        QString msgToSend = "A 100 " + QDateTime::currentDateTime().toString("MM/dd/yyyy hh:mm:ss") + " " + QString::number(boxGID);
        sendBase(msgToSend, true);
        sendRaw(msgToSend);
        sendRawStrtoIflows(QString(encode(msgToSend)));
    }
    else initial = false;
}
Example #4
0
void Server::recieveConnection()
{
    if (is_init_connection) return;

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    QString client_ip = clientConnection->peerAddress().toString();
    quint32 client_ip_int = clientConnection->peerAddress().toIPv4Address();

    emit write_message(tr("New connection from IP: %1").arg(client_ip));

    connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));

    if (client_ip_int == QHostAddress(remoteIPAddress).toIPv4Address() || is_config_mode) {
        if (remote_server_socket && clientConnection != remote_server_socket) {
            delete remote_server_socket;
        }
        remote_server_socket = clientConnection;
        connect(clientConnection, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
        connect(clientConnection, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
        connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recieveData()));
        connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    } else {
        clientConnection->abort();
    }
}
Example #5
0
bool Server::SendData(QHostAddress ip_to, QString message)
{
    bool result = false;
    QTcpSocket* tcpSocket = sockets->value(ip_to.toIPv4Address(), 0);
    if (tcpSocket && tcpSocket->state()==QAbstractSocket::ConnectedState && tcpSocket->isWritable()) {
        emit write_message(tr("Sending data (size=%1) to %2. Content: \"%3\"").arg(message.length()).arg(ip_to.toString()).arg(message));
        tcpSocket->write(message.toUtf8());
        result = tcpSocket->waitForBytesWritten();
    } else {
        tcpSocket = new QTcpSocket();
        tcpSocket->connectToHost(ip_to, remote_port, QIODevice::ReadWrite);
        tcpSocket->waitForConnected(5000);

        if (tcpSocket->state()==QAbstractSocket::ConnectedState) {
            emit write_message(tr("Sending data (size=%1) from new socket to %2. Content: \"%3\"").arg(message.length()).arg(ip_to.toString()).arg(message));
            tcpSocket->write(message.toUtf8());
            result = tcpSocket->waitForBytesWritten(5000);
        } else {
            emit error(tr("Client \"%1\" not found").arg(ip_to.toString()));
        }

        tcpSocket->abort();
        delete tcpSocket;
    }
    return result;
}
Example #6
0
void streamLoop(qintptr socketDesc, QQueue<QByteArray> &queue, bool& streaming) {

    QTcpSocket* socket = new QTcpSocket();
    // TCP_NODELAY + disable Nagle's algorithm
    socket->setSocketOption(QAbstractSocket::LowDelayOption, QVariant::fromValue(1));
    // Internetwork control
    socket->setSocketOption(QAbstractSocket::TypeOfServiceOption, QVariant::fromValue(192));
    socket->setSocketDescriptor(socketDesc);
    socket->readAll();

    QByteArray ContentType = ("HTTP/1.1 200 OK\r\n" \
                              "Server: test\r\n" \
                              "Cache-Control: no-cache\r\n" \
                              "Cache-Control: private\r\n" \
                              "Connection: close\r\n"\
                              "Pragma: no-cache\r\n"\
                              "Content-Type: multipart/x-mixed-replace; boundary=--boundary\r\n\r\n");

    socket->write(ContentType);

    while((socket->state() != QAbstractSocket::ClosingState ||
           socket->state() != QAbstractSocket::UnconnectedState) &&
           socket->state() == QAbstractSocket::ConnectedState &&
           streaming) {

        if(queue.empty()) { // no new frame available
            continue;
        }

        // make sure that the queue doesn't grow too big or
        // the OOM killer will kick in
        if(queue.length() > 20) {
            queue.clear();
            continue;
        }

        QByteArray boundary = ("--boundary\r\n" \
                               "Content-Type: image/jpeg\r\n" \
                               "Content-Length: ");

        QByteArray img  = queue.dequeue();
        boundary.append(QString::number(img.length()));
        boundary.append("\r\n\r\n");

        socket->write(boundary);
        socket->waitForBytesWritten();
        boundary.clear();
        socket->write(img);
        socket->waitForBytesWritten();
        img.clear();
    }

    socket->flush();
    socket->abort();
    socket->deleteLater();
    streaming = false;
    queue.clear();
    return;
}
Example #7
0
bool FilePrinter::detectCupsService()
{
    QTcpSocket qsock;
    qsock.connectToHost("localhost", 631);
    bool rtn = qsock.waitForConnected() && qsock.isValid();
    qsock.abort();
    return rtn;
}
Example #8
0
void ProtocolSocket::setSocket(QTcpSocket *socket)
{
    if (socket && socket->state() != QAbstractSocket::ConnectedState) {
        qWarning() << "BUG: ProtocolSocket::setSocket with unconnected socket";
        socket = 0;
    }

    if (socket == m_socket)
        return;

    bool wasConnected = isConnected();

    if (m_socket) {
        /* The existing socket is replaced, and all pending commands
         * are considered failed. This could be improved on. */
        QTcpSocket *oldSocket = m_socket;
        m_socket = 0;

        oldSocket->disconnect(this);
        // XXX can this be avoided if none are sent yet?
        abortCommands();
        oldSocket->abort();
        oldSocket->deleteLater();
    }

    m_socket = socket;

    if (socket) {
        socket->setParent(this);
        connect(socket, SIGNAL(readyRead()), this, SLOT(read()));
        // QueuedConnection used to make sure socket states are updated first
        connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()),
                Qt::QueuedConnection);
        connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
                this, SLOT(socketDisconnected()), Qt::QueuedConnection);

        if (!wasConnected) {
            m_connectedTime.restart();
            emit connected();
        }

        flushCommands();
        read();
    } else {
        emit disconnected();
    }

    emit socketChanged();
}
Example #9
0
int tcp_ping(QStringList command) {
    qDebug() << "tcp_ping(" << command.join(" ") << ")" << endl;

    /**
     * Check input
     */
    QTextStream errorStream(stderr);

    if(command.size() != 3 || command.at(0)!="ping" || command.at(1)!="tcp" ) {

        errorStream << "Error: tcp_ping(" << command.join(" ") << ") is no valid call (ping tcp <ip_address> <port> rzv|max|random|default)" << endl;
        return 1;
    }

    QByteArray byteArray;

    /**
     * CIP for "rzv"
     */
    if(command.at(2)=="rzv") {
        qDebug() << "rzv" << endl;

        byteArray.append(QByteArray(42, '\0'));
     }

    /**
     * CIP for "default"
     */
    if(command.at(2)=="default") {
        qDebug() << "default" << endl;

        // Header: request (1), profile (1), version (1), channel (1)
        byteArray.append(QByteArray(4, '\0'));

        // Header: UUID (16)
        QUuid uuid;
        uuid = QUuid::createUuid();
        QByteArray uuid_arr = uuid.toRfc4122();

        for(int j=0; j<16;j++) {

            byteArray.append(uuid_arr.at(j));
        }

        // Header: Empty IP address (4), port number (2), time (8), type (1), size (1)
        byteArray.append(QByteArray(16, '\0'));

        // Contextinformation: type (1), root-CIC (2), size (1)
        byteArray.append(QByteArray(4, '\0'));

        // Application: type (1), size (1)
        byteArray.append(QByteArray(2, '\0'));
     }

    /**
     * CIP for "max"
     */
    if(command.at(2)=="max") {
        qDebug() << "max" << endl;

        // Header: fix
        byteArray.append(QByteArray(34, '\0'));

        // Header: size (1)
        byteArray.append(QByteArray(QByteArray::fromHex("0xff")));
        byteArray.append(QByteArray(255, '\0'));

        // Contextinformation: fix
        byteArray.append(QByteArray(2, '\0'));

        // Contextinformation: size (255)
        byteArray.append(QByteArray(QByteArray::fromHex("0xff")));
        byteArray.append(QByteArray(255*2, '\0'));

        // Application Data: fix
        byteArray.append(QByteArray(0, '\0'));

        // Application Data: size (255)
        byteArray.append(QByteArray(QByteArray::fromHex("0xff")));
        byteArray.append(QByteArray(255, '\0'));


     }

    /**
     * CIP for "random"
     */
    if(command.at(2)=="random") {
        qDebug() << "random" << endl;

        QByteArray randValues;
        qsrand(QTime::currentTime().msec());
        for(int i=0; i <= 1064; i++) {
            randValues.append(qFloor(qrand()/CRN_RANDOM_DIVISOR));
        }


        int i = 0;
        quint8 rand;

        // Header: request (1)
        byteArray.append(randValues.at(i++));


        // Header: profile (1)
        byteArray.append(randValues.at(i++));


        // Header: version (1)
        byteArray.append(randValues.at(i++));


        // Header: channel (1)
        byteArray.append(randValues.at(i++));

        // Header: UUID (16)
        QUuid uuid;
        uuid = QUuid::createUuid();
        QByteArray uuid_arr = uuid.toRfc4122();

        for(int j=0; j<16;j++) {

            byteArray.append(uuid_arr.at(j));
        }

        // Header: Empty IP address (4) and port number (2)
        byteArray.append(QByteArray(6, '\0'));

        // Header: time (8)
        byteArray.append(QByteArray(8, '\0'));

        // Header: type (1)
        byteArray.append(randValues.at(i++));

        // Header: size (1) and data
        rand = randValues.at(i++);
        byteArray.append(rand);
        byteArray.append(QByteArray(rand, rand));

        // Contextinformation: type (1)
        byteArray.append(randValues.at(i++));

        // Contextinformation: root-CIC (2)
        byteArray.append(randValues.at(i++));
        byteArray.append(randValues.at(i++));

        // Contextinformation: size (1)
        rand = randValues.at(i++);
        byteArray.append(rand);
        byteArray.append(QByteArray(rand*2, rand));

        // Application: type (1)
        byteArray.append(randValues.at(i++));

        // Application: size (1)
        rand = randValues.at(i++);
        byteArray.append(rand);
        byteArray.append(QByteArray(rand, rand));

    } // rand

    /**
     * Sent via TCP
     */
    QTcpSocket *tcpSocket;
    tcpSocket = new QTcpSocket();
    QTextStream outStream(stdout);

    QString out;

    tcpSocket->abort();
    tcpSocket->connectToHost("127.0.0.1", 22365);

    qDebug() << "waitForConnected!";
    if (tcpSocket->waitForConnected(5000)) {

        qDebug() << "Connected!";
    }
    else {
        errorStream << "Error: tcp_ping(" << command.join(" ") << ") No connection available!" << endl;
        return 1;
    }

    out = QString("BytesWritten: %1").arg(tcpSocket->write(byteArray, byteArray.length()));

    qDebug() << out;

    int numRead = 0, numReadTotal = 0;
    char buffer[MAXMSG];

    forever {
        numRead  = tcpSocket->read(buffer, MAXMSG);
        qDebug() << "read buffer";

        qDebug() << buffer;

        numReadTotal += numRead;
        if (numRead == 0 && !tcpSocket->waitForReadyRead())
            break;
    }
    qDebug() << numReadTotal << " bytes red";

    tcpSocket->flush();
    tcpSocket->disconnectFromHost();
    tcpSocket->close();

    outStream << out << endl;

    return 0;
}