FtpDTP.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002,2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <OPENR/OSyslog.h>
00013 #include <OPENR/ODebug.h>
00014 #include "FtpDTP.h"
00015 
00016 FtpDTP::FtpDTP()
00017 {
00018 }
00019 
00020 OStatus
00021 FtpDTP::Initialize(const OID& myoid, const antStackRef& ipstack, void* index)
00022 {
00023     OSYSDEBUG(("FtpDTP::Initialize()\n"));
00024 
00025     myOID        = myoid;
00026     ipstackRef   = ipstack;
00027     continuation = index;
00028 
00029     // initialize FTP connection
00030     method   = FTP_METHOD_NOOP;
00031     dataType = FTP_DATA_A;
00032     passive  = false;
00033     connectPort = FTP_DATA_PORT;
00034 
00035     // initialize File System
00036     ResetFilename();
00037     strcpy(ftpDir, "/MS/");
00038 
00039     connection.state = CONNECTION_CLOSED;
00040 
00041     // 
00042     // Allocate send buffer
00043     //
00044     antEnvCreateSharedBufferMsg sendBufferMsg(FTP_BUFFER_SIZE);
00045 
00046     sendBufferMsg.Call(ipstackRef, sizeof(sendBufferMsg));
00047     if (sendBufferMsg.error != ANT_SUCCESS) {
00048         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00049                   "FtpDTP::Initialize()",
00050                   "Can't allocate send buffer",
00051                   index, sendBufferMsg.error));
00052         return oFAIL;
00053     }
00054 
00055     connection.sendBuffer = sendBufferMsg.buffer;
00056     connection.sendBuffer.Map();
00057     connection.sendData = (byte*)(connection.sendBuffer.GetAddress());
00058 
00059     //
00060     // Allocate receive buffer
00061     //
00062     antEnvCreateSharedBufferMsg recvBufferMsg(FTP_BUFFER_SIZE);
00063 
00064     recvBufferMsg.Call(ipstackRef, sizeof(recvBufferMsg));
00065     if (recvBufferMsg.error != ANT_SUCCESS) {
00066         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00067                   "FtpDTP::Initialize()",
00068                   "Can't allocate receive buffer",
00069                   index, recvBufferMsg.error));
00070         return oFAIL;
00071     }
00072 
00073     connection.recvBuffer = recvBufferMsg.buffer;
00074     connection.recvBuffer.Map();
00075     connection.recvData = (byte*)(connection.recvBuffer.GetAddress());
00076 
00077     return oSUCCESS;
00078 }
00079 
00080 OStatus
00081 FtpDTP::Listen()
00082 {
00083     OSYSDEBUG(("FtpDTP::Listen()\n"));
00084 
00085     if (connection.state != CONNECTION_CLOSED) return oFAIL;
00086 
00087     //
00088     // Create endpoint
00089     //
00090     antEnvCreateEndpointMsg tcpCreateMsg(EndpointType_TCP,
00091                                          FTP_BUFFER_SIZE * 2);
00092 
00093     tcpCreateMsg.Call(ipstackRef, sizeof(tcpCreateMsg));
00094     if (tcpCreateMsg.error != ANT_SUCCESS) {
00095         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00096                   "FtpDTP::Listen()",
00097                   "Can't create endpoint",
00098                   (int)continuation, tcpCreateMsg.error));
00099         return oFAIL;
00100     }
00101     connection.endpoint = tcpCreateMsg.moduleRef;
00102 
00103     //
00104     // Listen
00105     //
00106     TCPEndpointListenMsg listenMsg(connection.endpoint,
00107                                    IP_ADDR_ANY, connectPort);
00108     listenMsg.continuation = continuation;
00109 
00110     listenMsg.Send(ipstackRef, myOID,
00111                    Extra_Entry[entryListenContforDTP], sizeof(listenMsg));
00112     
00113     connection.state = CONNECTION_LISTENING;
00114 
00115     return oSUCCESS;
00116 }
00117 
00118 OStatus
00119 FtpDTP::Connect()
00120 {
00121     OSYSDEBUG(("FtpDTP::Connect()\n"));
00122 
00123     if (connection.state != CONNECTION_CLOSED) return oFAIL;
00124 
00125     //
00126     // Create endpoint
00127     //
00128     antEnvCreateEndpointMsg tcpCreateMsg(EndpointType_TCP,
00129                                          FTP_BUFFER_SIZE * 2);
00130 
00131     tcpCreateMsg.Call(ipstackRef, sizeof(tcpCreateMsg));
00132     if (tcpCreateMsg.error != ANT_SUCCESS) {
00133         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00134                   "FtpDTP::Connect()",
00135                   "Can't create endpoint",
00136                   (int)continuation, tcpCreateMsg.error));
00137         return oFAIL;
00138     }
00139     connection.endpoint = tcpCreateMsg.moduleRef;
00140 
00141     //
00142     // Connect
00143     //
00144     TCPEndpointConnectMsg connectMsg(connection.endpoint,
00145                                      0, FTP_DATA_PORT,
00146                                      connectIP, connectPort);
00147     connectMsg.continuation = continuation;
00148 
00149     connectMsg.Send(ipstackRef, myOID,
00150                     Extra_Entry[entryConnectContforDTP], sizeof(connectMsg));
00151     
00152     connection.state = CONNECTION_CONNECTING;
00153 
00154     return oSUCCESS;
00155 }
00156 
00157 bool
00158 FtpDTP::ListenCont(TCPEndpointListenMsg* listenMsg)
00159 {
00160     OSYSDEBUG(("FtpDTP::ListenCont() %x %d - %x %d\n",
00161                listenMsg->lAddress.Address(),
00162                listenMsg->lPort,
00163                listenMsg->fAddress.Address(),
00164                listenMsg->fPort
00165                ));
00166 
00167     if (listenMsg->error != TCP_SUCCESS) {
00168         OSYSLOG1((osyslogERROR, "%s : %s %d",
00169                   "FtpDTP::ListenCont()",
00170                   "FAILED. listenMsg->error", listenMsg->error));
00171         Close();
00172         return false;
00173     }
00174 
00175     connection.state = CONNECTION_CONNECTED;
00176 
00177     switch (method) {
00178     case FTP_METHOD_RETR:
00179         RetrieveSend();
00180         break;
00181 
00182     case FTP_METHOD_STOR:
00183         connection.recvSize = 0;
00184         Receive();
00185         break;
00186 
00187     case FTP_METHOD_LIST:
00188         if (!ListSend()) {
00189             Close();
00190         }
00191         break;
00192     default:
00193         OSYSDEBUG(("not yet\n"));
00194         break;
00195     }
00196 
00197     return true;
00198 }
00199 
00200 bool
00201 FtpDTP::ConnectCont(TCPEndpointConnectMsg* connectMsg)
00202 {
00203     OSYSDEBUG(("FtpDTP::ConnectCont()\n"));
00204 
00205     if (connectMsg->error != TCP_SUCCESS) {
00206         OSYSLOG1((osyslogERROR, "%s : %s %d",
00207                   "FtpDTP::ConnectCont()",
00208                   "FAILED. connectMsg->error", connectMsg->error));
00209         Close();
00210         return false;
00211     }
00212 
00213     connection.state = CONNECTION_CONNECTED;
00214 
00215     switch (method) {
00216     case FTP_METHOD_RETR:
00217         RetrieveSend();
00218         break;
00219 
00220     case FTP_METHOD_STOR:
00221         connection.recvSize = 0;
00222         Receive();
00223         break;
00224 
00225     case FTP_METHOD_LIST:
00226         if (!ListSend()) {
00227             Close();
00228         }
00229         break;
00230     }
00231     return true;
00232 }
00233 
00234 OStatus
00235 FtpDTP::Send()
00236 {
00237     OSYSDEBUG(("FtpDTP::Send() "));
00238 
00239     if (connection.state != CONNECTION_CONNECTED) return oFAIL;
00240 
00241     OSYSDEBUG(("%d\n", connection.sendSize));
00242 
00243     TCPEndpointSendMsg sendMsg(connection.endpoint,
00244                                connection.sendData,
00245                                connection.sendSize);
00246     sendMsg.continuation = continuation;
00247 
00248     sendMsg.Send(ipstackRef, myOID,
00249                  Extra_Entry[entrySendContforDTP],
00250                  sizeof(TCPEndpointSendMsg));
00251 
00252     connection.state = CONNECTION_SENDING;
00253     connection.sendSize = 0;
00254     return oSUCCESS;
00255 }
00256 
00257 bool
00258 FtpDTP::SendCont(TCPEndpointSendMsg* sendMsg)
00259 {
00260     OSYSDEBUG(("FtpDTP::SendCont()\n"));
00261 
00262     if (sendMsg->error != TCP_SUCCESS) {
00263         if (sendMsg->error == TCP_BUFFER_INVALID
00264             && method == FTP_METHOD_NOOP) {
00265             Close();
00266             return true;
00267         } else {
00268             OSYSLOG1((osyslogERROR, "%s : %s %d",
00269                       "FtpDTP::SendCont()",
00270                       "FAILED. sendMsg->error", sendMsg->error));
00271             Close();
00272             return false;
00273         }
00274     }
00275 
00276     connection.state = CONNECTION_CONNECTED;
00277 
00278     switch (method) {
00279     case FTP_METHOD_RETR:
00280         if (!RetrieveSend()) {
00281             Close();
00282             return true;
00283         }
00284         break;
00285 
00286     case FTP_METHOD_LIST:
00287         if (!ListSend()) {
00288             Close();
00289             return true;
00290         }
00291         break;
00292 
00293     default :
00294         Close();
00295         return true;
00296     }
00297     return false;
00298 }
00299 
00300 OStatus
00301 FtpDTP::Receive()
00302 {
00303 
00304     if (connection.state != CONNECTION_CONNECTED
00305         && connection.state != CONNECTION_SENDING) return oFAIL;
00306 
00307     OSYSDEBUG(("FtpDTP::Receive()\n"));
00308 
00309     TCPEndpointReceiveMsg receiveMsg(connection.endpoint,
00310                                      connection.recvData,
00311                                      1, FTP_BUFFER_SIZE);
00312     receiveMsg.continuation = continuation;
00313 
00314     receiveMsg.Send(ipstackRef, myOID,
00315                     Extra_Entry[entryReceiveContforDTP], sizeof(receiveMsg));
00316 
00317     connection.state = CONNECTION_RECEIVING;
00318     return oSUCCESS;
00319 }
00320 
00321 bool
00322 FtpDTP::ReceiveCont(TCPEndpointReceiveMsg* receiveMsg)
00323 {
00324     OSYSDEBUG(("FtpDTP::ReceiveCont()\n"));
00325 
00326     connection.recvSize = receiveMsg->sizeMin;
00327     if (receiveMsg->error == TCP_CONNECTION_CLOSED) {
00328         Save(connection.recvData, 0);
00329         Close();
00330         return true;
00331     }
00332 
00333     if (receiveMsg->error != TCP_SUCCESS) {
00334         OSYSLOG1((osyslogERROR, "%s : %s %d",
00335                   "FtpDTP::ReceiveCont()",
00336                   "FAILED. receiveMsg->error", receiveMsg->error));
00337         Close();
00338         return false;
00339     }
00340 
00341     // receive some more
00342     Save(connection.recvData, receiveMsg->sizeMin, false);
00343     connection.state = CONNECTION_CONNECTED;
00344     connection.recvSize = 0;
00345     Receive();
00346 
00347     return false;
00348 }
00349 
00350 OStatus
00351 FtpDTP::Close()
00352 {
00353     OSYSDEBUG(("FtpDTP::Close() %d\n", connection.state));
00354 
00355     if (connection.state == CONNECTION_CLOSED
00356         || connection.state == CONNECTION_CLOSING) return oFAIL;
00357 
00358     TCPEndpointCloseMsg closeMsg(connection.endpoint);
00359     closeMsg.continuation = continuation;
00360 
00361     closeMsg.Send(ipstackRef, myOID,
00362                   Extra_Entry[entryCloseContforDTP], sizeof(closeMsg));
00363 
00364     connection.state = CONNECTION_CLOSING;
00365     ResetFilename();
00366     method   = FTP_METHOD_NOOP;
00367     dataType = FTP_DATA_A;
00368     passive  = false;
00369     connectPort = FTP_DATA_PORT;
00370 
00371     return oSUCCESS;
00372 }
00373 
00374 void
00375 FtpDTP::CloseCont(TCPEndpointCloseMsg* closeMsg)
00376 {
00377     OSYSDEBUG(("FtpDTP::CloseCont()\n"));
00378     
00379     connection.state = CONNECTION_CLOSED;
00380 }
00381 

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1