00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdarg.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <EndpointTypes.h>
00015 #include <TCPEndpointMsg.h>
00016 #include "FtpPI.h"
00017
00018 FtpPI::FtpPI()
00019 {
00020 }
00021
00022 OStatus
00023 FtpPI::Initialize(const OID& myoid, const antStackRef& ipstack,
00024 void* index, OList<Passwd, MAX_LOGIN> *pass)
00025 {
00026 OSYSDEBUG(("FtpPI::Initialize()\n"));
00027
00028 myOID = myoid;
00029 ipstackRef = ipstack;
00030 continuation = index;
00031 passwd = pass;
00032
00033 state = FTP_NOT_LOGIN;
00034 connection.state = CONNECTION_CLOSED;
00035
00036
00037
00038
00039 antEnvCreateSharedBufferMsg sendBufferMsg(FTP_BUFFER_SIZE);
00040
00041 sendBufferMsg.Call(ipstackRef, sizeof(sendBufferMsg));
00042 if (sendBufferMsg.error != ANT_SUCCESS) {
00043 OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00044 "FtpPI::Initialize()",
00045 "Can't allocate send buffer",
00046 index, sendBufferMsg.error));
00047 return oFAIL;
00048 }
00049
00050 connection.sendBuffer = sendBufferMsg.buffer;
00051 connection.sendBuffer.Map();
00052 connection.sendData = (byte*)(connection.sendBuffer.GetAddress());
00053
00054
00055
00056
00057 antEnvCreateSharedBufferMsg recvBufferMsg(FTP_BUFFER_SIZE);
00058
00059 recvBufferMsg.Call(ipstackRef, sizeof(recvBufferMsg));
00060 if (recvBufferMsg.error != ANT_SUCCESS) {
00061 OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00062 "FtpPI::Initialize()",
00063 "Can't allocate receive buffer",
00064 index, recvBufferMsg.error));
00065 return oFAIL;
00066 }
00067
00068 connection.recvBuffer = recvBufferMsg.buffer;
00069 connection.recvBuffer.Map();
00070 connection.recvData = (byte*)(connection.recvBuffer.GetAddress());
00071
00072 ftpDTP.Initialize(myOID, ipstackRef, index);
00073 Listen();
00074
00075 return oSUCCESS;
00076 }
00077
00078 OStatus
00079 FtpPI::Listen()
00080 {
00081 OSYSDEBUG(("FtpPI::Listen()\n"));
00082
00083 if (connection.state != CONNECTION_CLOSED) return oFAIL;
00084
00085
00086
00087
00088 antEnvCreateEndpointMsg tcpCreateMsg(EndpointType_TCP,
00089 FTP_BUFFER_SIZE * 2);
00090
00091 tcpCreateMsg.Call(ipstackRef, sizeof(tcpCreateMsg));
00092 if (tcpCreateMsg.error != ANT_SUCCESS) {
00093 OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00094 "FtpPI::Listen()",
00095 "Can't create endpoint",
00096 (int)continuation, tcpCreateMsg.error));
00097 return oFAIL;
00098 }
00099 connection.endpoint = tcpCreateMsg.moduleRef;
00100
00101
00102
00103
00104 TCPEndpointListenMsg listenMsg(connection.endpoint,
00105 IP_ADDR_ANY, FTP_LISTEN_PORT);
00106 listenMsg.continuation = continuation;
00107
00108 listenMsg.Send(ipstackRef, myOID,
00109 Extra_Entry[entryListenContforPI], sizeof(listenMsg));
00110
00111 connection.state = CONNECTION_LISTENING;
00112
00113 return oSUCCESS;
00114 }
00115
00116 void
00117 FtpPI::ListenCont(TCPEndpointListenMsg* listenMsg)
00118 {
00119 OSYSDEBUG(("FtpPI::ListenCont() %x %d - %x %d\n",
00120 listenMsg->lAddress.Address(),
00121 listenMsg->lPort,
00122 listenMsg->fAddress.Address(),
00123 listenMsg->fPort));
00124
00125 if (listenMsg->error != TCP_SUCCESS) {
00126 OSYSLOG1((osyslogERROR, "%s : %s %d",
00127 "FtpPI::ListenCont()",
00128 "FAILED. listenMsg->error", listenMsg->error));
00129 Close();
00130 return;
00131 }
00132
00133 ipaddr = listenMsg->lAddress;
00134
00135 connection.state = CONNECTION_CONNECTED;
00136 Send(FTP_REPLY_SERVICE_READY, "AIBO FTP Server ready");
00137 connection.recvSize = 0;
00138 Receive();
00139 }
00140
00141 OStatus
00142 FtpPI::Send(FTPReplyCode code, char *format, ...)
00143 {
00144 OSYSDEBUG(("FtpPI::Send()\n"));
00145
00146 if (connection.state != CONNECTION_CONNECTED) return oFAIL;
00147
00148 char buf[MAX_STRING_LENGTH];
00149 va_list ap;
00150 va_start(ap, format);
00151 vsprintf(buf, format, ap);
00152 va_end(ap);
00153
00154 sprintf((char *)connection.sendData, "%03d %s\r\n", code, buf);
00155
00156 TCPEndpointSendMsg sendMsg(connection.endpoint,
00157 connection.sendData,
00158 strlen((char *)connection.sendData));
00159 sendMsg.continuation = continuation;
00160
00161 sendMsg.Send(ipstackRef, myOID,
00162 Extra_Entry[entrySendContforPI], sizeof(sendMsg));
00163
00164 connection.state = CONNECTION_SENDING;
00165 connection.sendSize = 0;
00166 return oSUCCESS;
00167 }
00168
00169 void
00170 FtpPI::SendCont(TCPEndpointSendMsg* sendMsg)
00171 {
00172 OSYSDEBUG(("FtpPI::SendCont()\n"));
00173
00174 if (sendMsg->error != TCP_SUCCESS) {
00175 OSYSLOG1((osyslogERROR, "%s : %s %d",
00176 "FtpPI::SendCont()",
00177 "FAILED. sendMsg->error", sendMsg->error));
00178 Close();
00179 return;
00180 }
00181
00182 connection.state = CONNECTION_CONNECTED;
00183 }
00184
00185 OStatus
00186 FtpPI::Receive()
00187 {
00188 OSYSDEBUG(("FtpPI::Receive()\n"));
00189
00190 if (connection.state != CONNECTION_CONNECTED
00191 && connection.state != CONNECTION_SENDING) return oFAIL;
00192
00193 TCPEndpointReceiveMsg receiveMsg(connection.endpoint,
00194 connection.recvData,
00195 1,
00196 FTP_BUFFER_SIZE);
00197 receiveMsg.continuation = continuation;
00198 receiveMsg.Send(ipstackRef, myOID,
00199 Extra_Entry[entryReceiveContforPI], sizeof(receiveMsg));
00200 return oSUCCESS;
00201 }
00202
00203 void
00204 FtpPI::ReceiveCont(TCPEndpointReceiveMsg* receiveMsg)
00205 {
00206 bool doReceive = false;
00207
00208 if (receiveMsg->error == TCP_CONNECTION_CLOSED) {
00209 Close();
00210 return;
00211 }
00212
00213 if (receiveMsg->error != TCP_SUCCESS) {
00214 OSYSLOG1((osyslogERROR, "%s : %s %d",
00215 "FtpPI::ReceiveCont()",
00216 "FAILED. receiveMsg->error", receiveMsg->error));
00217 Close();
00218 return;
00219 }
00220
00221 connection.recvSize += receiveMsg->sizeMin;
00222
00223 if (RequestComplete()) {
00224 RequestProcess();
00225 } else {
00226 OSYSDEBUG(("FtpPI::Request not complete\n"));
00227 doReceive = true;
00228 }
00229
00230
00231 if (doReceive) {
00232 if (connection.recvSize < FTP_BUFFER_SIZE) {
00233 Receive();
00234 } else {
00235 Close();
00236 }
00237 }
00238 }
00239
00240 OStatus
00241 FtpPI::Close()
00242 {
00243 OSYSDEBUG(("FtpPI::Close()\n"));
00244
00245 if (connection.state == CONNECTION_CLOSED
00246 || connection.state == CONNECTION_CLOSING) return oFAIL;
00247
00248 TCPEndpointCloseMsg closeMsg(connection.endpoint);
00249 closeMsg.continuation = continuation;
00250
00251 closeMsg.Send(ipstackRef, myOID,
00252 Extra_Entry[entryCloseContforPI], sizeof(closeMsg));
00253
00254 connection.state = CONNECTION_CLOSING;
00255 ftpDTP.Close();
00256
00257 return oSUCCESS;
00258 }
00259
00260 void
00261 FtpPI::CloseCont(TCPEndpointCloseMsg* closeMsg)
00262 {
00263 OSYSDEBUG(("FtpPI::CloseCont()\n"));
00264
00265 connection.state = CONNECTION_CLOSED;
00266 Listen();
00267 }
00268
00269 void
00270 FtpPI::ListenContforDTP(TCPEndpointListenMsg* listenMsg)
00271 {
00272 if (listenMsg->error == TCP_SUCCESS) {
00273 switch(ftpDTP.GetType()) {
00274 case FTP_DATA_I:
00275 Send(FTP_REPLY_OPEN_CONNECTION,
00276 "Binary data connection for %s.",
00277 ftpDTP.GetFilename());
00278 break;
00279
00280 case FTP_DATA_A:
00281 Send(FTP_REPLY_OPEN_CONNECTION,
00282 "ASCII data connection for %s.",
00283 ftpDTP.GetFilename());
00284 break;
00285 }
00286 }
00287 ftpDTP.ListenCont(listenMsg);
00288 return;
00289 }
00290
00291 void
00292 FtpPI::ConnectContforDTP(TCPEndpointConnectMsg* connectMsg)
00293 {
00294 if (connectMsg->error == TCP_SUCCESS) {
00295 switch(ftpDTP.GetType()) {
00296 case FTP_DATA_I:
00297 Send(FTP_REPLY_OPEN_CONNECTION,
00298 "Binary data connection for %s.",
00299 ftpDTP.GetFilename());
00300 break;
00301
00302 case FTP_DATA_A:
00303 Send(FTP_REPLY_OPEN_CONNECTION,
00304 "ASCII data connection for %s.",
00305 ftpDTP.GetFilename());
00306 break;
00307 }
00308 }
00309
00310 ftpDTP.ConnectCont(connectMsg);
00311 return;
00312 }
00313
00314 void
00315 FtpPI::SendContforDTP(TCPEndpointSendMsg* sendMsg)
00316 {
00317 if(ftpDTP.SendCont(sendMsg)) {
00318 Send(FTP_REPLY_CLOSE_DATA, "Transfer complete.");
00319 connection.recvSize = 0;
00320 Receive();
00321 } else if (ftpDTP.GetState() != CONNECTION_SENDING) {
00322 Send(FTP_REPLY_TRANSFER_ABORT, "Transfer abort.");
00323 connection.recvSize = 0;
00324 Receive();
00325 }
00326 }
00327
00328 void
00329 FtpPI::ReceiveContforDTP(TCPEndpointReceiveMsg* receiveMsg)
00330 {
00331 if (ftpDTP.ReceiveCont(receiveMsg)) {
00332 Send(FTP_REPLY_CLOSE_DATA, "Transfer complete.");
00333 connection.recvSize = 0;
00334 Receive();
00335 } else if (ftpDTP.GetState() != CONNECTION_RECEIVING) {
00336 Send(FTP_REPLY_TRANSFER_ABORT, "Transfer abort.");
00337 connection.recvSize = 0;
00338 Receive();
00339 }
00340 }
00341
00342 void
00343 FtpPI::CloseContforDTP(TCPEndpointCloseMsg* closeMsg)
00344 {
00345 ftpDTP.CloseCont(closeMsg);
00346 }