tests/qtp/qtp_qftp/ftpserver.cpp
changeset 3 41300fa6a67c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "ftpserver.h"
       
    43 
       
    44 #define FTP_CMND_PORT 21
       
    45 #define FTP_DATA_PORT 9801
       
    46 
       
    47 ftpServer::ftpServer( QObject *parent  )
       
    48          : QTcpServer( parent )
       
    49 {
       
    50     ftpSocket = NULL;
       
    51     dataSocket = NULL;
       
    52 
       
    53     tcpServer = new QTcpServer( /* this */ );
       
    54     not_done = true;
       
    55     bool result = tcpServer->listen(QHostAddress::Any, FTP_CMND_PORT );
       
    56     if ( !result )
       
    57     {
       
    58         delete tcpServer;
       
    59         close();
       
    60         not_done = false;
       
    61         return;
       
    62     }
       
    63     connect(tcpServer, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
       
    64 
       
    65 /*
       
    66     dataServer = new QTcpServer( );
       
    67     result = dataServer->listen(QHostAddress::Any, FTP_DATA_PORT );
       
    68     if ( !result )
       
    69     {
       
    70         delete tcpServer;
       
    71         close();
       
    72         not_done = false;
       
    73 
       
    74         delete dataServer;
       
    75         close();
       
    76         return;
       
    77     }
       
    78 
       
    79     connect(dataServer, SIGNAL(newConnection()), this, SLOT(handleNewDataConnection()));
       
    80 */
       
    81 }
       
    82 
       
    83 ftpServer::~ftpServer()
       
    84 {
       
    85     if ( ftpSocket )
       
    86     {
       
    87         ftpSocket->disconnect(SIGNAL(readyRead()));
       
    88         ftpSocket->disconnect(SIGNAL(error(QAbstractSocket::SocketError)));
       
    89         ftpSocket->close();
       
    90         ftpSocket->abort();
       
    91         ftpSocket->deleteLater();
       
    92         ftpSocket = NULL;
       
    93     }
       
    94 }
       
    95 
       
    96 bool ftpServer::isDone()
       
    97 {
       
    98     return not_done;
       
    99 }
       
   100 void ftpServer::handleNewConnection()
       
   101 {
       
   102     QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
       
   103     clientConnection->setTextModeEnabled(true);
       
   104 
       
   105     if ( !ftpSocket )
       
   106     {
       
   107         ftpSocket = clientConnection;
       
   108         connect( ftpSocket,
       
   109             SIGNAL(readyRead()),
       
   110             this,
       
   111             SLOT(readConnection()));
       
   112         connect(ftpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
       
   113              this, SLOT(displayConnectionError(QAbstractSocket::SocketError)));
       
   114 
       
   115         QString greet("220 Service ready\n");
       
   116         ftpSocket->write(qPrintable(greet));
       
   117 
       
   118     }
       
   119     else if ( !dataSocket )
       
   120     {
       
   121         dataSocket = clientConnection;
       
   122         connect( dataSocket,
       
   123             SIGNAL(readyRead()),
       
   124             this,
       
   125             SLOT(readConnection()));
       
   126         connect(dataSocket, SIGNAL(error(QAbstractSocket::SocketError)),
       
   127              this, SLOT(displayConnectionError(QAbstractSocket::SocketError)));
       
   128 
       
   129         if ( dataSocket->waitForConnected() )
       
   130         {
       
   131         //qDebug() << "Data connection accepted.\n";
       
   132         }
       
   133 
       
   134 
       
   135     }
       
   136     else
       
   137     {
       
   138         // We already are busy with open connection?!
       
   139         // qDebug() << "Server busy.\n";
       
   140         clientConnection->disconnectFromHost();
       
   141     }
       
   142 }
       
   143 
       
   144 void ftpServer::handleNewDataConnection()
       
   145 {
       
   146     QTcpSocket *dataConnection = tcpServer->nextPendingConnection();
       
   147     dataConnection->setTextModeEnabled(true);
       
   148 
       
   149     if ( !dataSocket )
       
   150     {
       
   151         dataSocket = dataConnection;
       
   152         connect( dataSocket,
       
   153             SIGNAL(readyRead()),
       
   154             this,
       
   155             SLOT(readConnection()));
       
   156         connect(dataSocket, SIGNAL(error(QAbstractSocket::SocketError)),
       
   157              this, SLOT(displayConnectionError(QAbstractSocket::SocketError)));
       
   158 
       
   159         QString list(".\n..\nasdf.txt\nqwerty.txt\n");
       
   160         ftpSocket->write(qPrintable(list));
       
   161 
       
   162     }
       
   163     else
       
   164     {
       
   165         // We already are busy with open connection?!
       
   166         //qDebug() << "Data Server busy.\n";
       
   167         dataConnection->disconnectFromHost();
       
   168     }
       
   169 }
       
   170 
       
   171 void ftpServer::displayConnectionError(QAbstractSocket::SocketError socketError)
       
   172 {
       
   173 		Q_UNUSED(socketError);
       
   174     ftpSocket->disconnect(SIGNAL(readyRead()));
       
   175     ftpSocket->disconnect(SIGNAL(error(QAbstractSocket::SocketError)));
       
   176     ftpSocket->close();
       
   177     ftpSocket->abort();
       
   178     ftpSocket->deleteLater();
       
   179     ftpSocket = NULL;
       
   180 }
       
   181 
       
   182 void ftpServer::displayDataConnectionError(QAbstractSocket::SocketError socketError)
       
   183 {
       
   184 		Q_UNUSED(socketError);
       
   185     dataSocket->disconnect(SIGNAL(readyRead()));
       
   186     dataSocket->disconnect(SIGNAL(error(QAbstractSocket::SocketError)));
       
   187     dataSocket->close();
       
   188     dataSocket->abort();
       
   189     dataSocket->deleteLater();
       
   190     dataSocket = NULL;
       
   191 }
       
   192 
       
   193 void ftpServer::readDataConnection()
       
   194 {
       
   195     while (dataSocket->canReadLine())
       
   196     {
       
   197         dataSocket->readAll();
       
   198     }
       
   199 }
       
   200 
       
   201 void ftpServer::readConnection()
       
   202 {
       
   203     while (ftpSocket->canReadLine())
       
   204     {
       
   205         char buf[256];
       
   206         qint64 lineLength = ftpSocket->readLine(buf, sizeof(buf));
       
   207         if (lineLength != -1)
       
   208         {
       
   209             QString tmpCommand(buf);
       
   210             tmpCommand = tmpCommand.simplified();
       
   211 
       
   212             // qDebug() << "Received: '" << buf << "'\n";
       
   213             if ( tmpCommand.left(5) == QString("USER ") )
       
   214             {
       
   215                 QString acceptuser("331 User name ok\n");
       
   216                 ftpSocket->write(qPrintable(acceptuser));
       
   217             }
       
   218             else if ( tmpCommand.left(5) == QString("PASS ") )
       
   219             {
       
   220                 QString acceptCommand("230 User logged in\n");
       
   221                 ftpSocket->write(qPrintable(acceptCommand));
       
   222             }
       
   223             else if ( tmpCommand.left(5) == QString("TYPE ") )
       
   224             {
       
   225                 QString acceptCommand("200 Command OK\n");
       
   226                 // QString acceptCommand("227 Entering Passive Mode. 21,21,21,21,21,21\n");
       
   227                 ftpSocket->write(qPrintable(acceptCommand));
       
   228             }
       
   229             else if ( tmpCommand.left(4) == QString("PASV") )
       
   230             {
       
   231                 QString acceptCommand("227 Entering Passive Mode. (127,0,0,1,0,21)\n");
       
   232                 // QString acceptCommand("200 Command OK\n");
       
   233                 ftpSocket->write(qPrintable(acceptCommand));
       
   234             }
       
   235             else if ( tmpCommand.left(4) == QString("LIST") )
       
   236             {
       
   237                 // QString acceptuser("502 Command not implemented\n");
       
   238                 // QString acceptuser("-rw-r--r--    1 vogel    user     1209041 May  1 13:55 IMPJET24.CAS\n-rw-r--r--    1 vogel    user     10259798 May  1 20:57 IMPJET24.DAT\n");
       
   239             // qDebug() << "sending 200...\n";
       
   240                 QString acceptCommand("200 Command OK\n");
       
   241                 ftpSocket->write(qPrintable(acceptCommand));
       
   242                 // qDebug() << "sending 150...\n";
       
   243                 QString port;
       
   244                 port.setNum(dataSocket->localPort());
       
   245                 QString dataCommand("150 ASCII data connection for /bin/ls (127.0.0.1,");
       
   246                 dataCommand.append( port );
       
   247                 dataCommand.append(") (0 bytes).");
       
   248                 ftpSocket->write(qPrintable(dataCommand));
       
   249                 // not_done = false;
       
   250 
       
   251                 //
       
   252                 //QString list(".\n..\nasdf.txt\nqwerty.txt\n");
       
   253                 QString list("07.12.2009\t13:49\t<DIR>\tinclude\n03.12.2009\t14:01\t721\tINSTALL\n");
       
   254 
       
   255 
       
   256                 dataSocket->write(qPrintable(list));
       
   257                 dataSocket->flush();
       
   258                 dataSocket->disconnectFromHost();
       
   259                 dataSocket = NULL;
       
   260 
       
   261                 // qDebug() << "sending 226...\n";
       
   262                 QString finishCommand("226 Closing data connection, file transfer successful.\n");
       
   263                 ftpSocket->write(qPrintable(finishCommand));
       
   264 
       
   265                 not_done = false;
       
   266 
       
   267             }
       
   268             else
       
   269             {
       
   270                 // qDebug() << "Received unknown:" << buf << "\n";
       
   271             }
       
   272         }
       
   273     }
       
   274 }
       
   275 
       
   276