TinyFTPD.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 <sys/stat.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <EndpointTypes.h>
00015 #include <TCPEndpointMsg.h>
00016 #include "TinyFTPD.h"
00017 
00018 TinyFTPD::TinyFTPD() : passwd()
00019 {
00020 }
00021 
00022 OStatus
00023 TinyFTPD::DoInit(const OSystemEvent& event)
00024 {
00025     OSYSDEBUG(("TinyFTPD::DoInit()\n"));
00026     return oSUCCESS;
00027 }
00028 
00029 OStatus
00030 TinyFTPD::DoStart(const OSystemEvent& event)
00031 {
00032     OSYSDEBUG(("TinyFTPD::DoStart()\n"));
00033 
00034     ipstackRef = antStackRef("IPStack");
00035 
00036     // Get Passwd
00037     OStatus status = LoadPasswd();
00038     if (oSUCCESS != status) {
00039         OSYSLOG1((osyslogERROR, "TinyFTPD::Load Fail %d", status));
00040         return oFAIL;
00041     }
00042 
00043     // initialize connection
00044     for(int index = 0; index < FTP_CONNECTION_MAX; index++) {
00045         ftpPI[index].Initialize(myOID_, ipstackRef, (void*)index, &passwd);
00046     }
00047 
00048     return oSUCCESS;
00049 }
00050 
00051 OStatus
00052 TinyFTPD::DoStop(const OSystemEvent& event)
00053 {
00054     OSYSDEBUG(("TinyFTPD::DoStop()\n"));
00055     for(int index = 0; index < FTP_CONNECTION_MAX; index++) {
00056         ftpPI[index].Close();
00057     }
00058     return oSUCCESS;
00059 }
00060 
00061 OStatus
00062 TinyFTPD::DoDestroy(const OSystemEvent& event)
00063 {
00064     OSYSDEBUG(("TinyFTPD::DoDestroy()\n"));
00065     return oSUCCESS;
00066 }
00067 
00068 void
00069 TinyFTPD::ListenContforPI(ANTENVMSG msg)
00070 {
00071     TCPEndpointListenMsg* listenMsg
00072         = (TCPEndpointListenMsg*)antEnvMsg::Receive(msg);
00073     int index = (int)(listenMsg->continuation);
00074 
00075     ftpPI[index].ListenCont(listenMsg);
00076 }
00077 
00078 void
00079 TinyFTPD::SendContforPI(ANTENVMSG msg)
00080 {
00081     TCPEndpointSendMsg* sendMsg = (TCPEndpointSendMsg*)antEnvMsg::Receive(msg);
00082     int index = (int)(sendMsg->continuation);
00083 
00084     ftpPI[index].SendCont(sendMsg);
00085 }
00086 
00087 void
00088 TinyFTPD::ReceiveContforPI(ANTENVMSG msg)
00089 {
00090     TCPEndpointReceiveMsg* receiveMsg
00091         = (TCPEndpointReceiveMsg*)antEnvMsg::Receive(msg);
00092     int index = (int)(receiveMsg->continuation);
00093 
00094     ftpPI[index].ReceiveCont(receiveMsg);
00095 }
00096 
00097 void
00098 TinyFTPD::CloseContforPI(ANTENVMSG msg)
00099 {
00100     TCPEndpointCloseMsg* closeMsg
00101         = (TCPEndpointCloseMsg*)antEnvMsg::Receive(msg);
00102     int index = (int)(closeMsg->continuation);
00103    
00104     ftpPI[index].CloseCont(closeMsg);
00105 }
00106 
00107 void
00108 TinyFTPD::ListenContforDTP(ANTENVMSG msg)
00109 {
00110     TCPEndpointListenMsg* listenMsg
00111         = (TCPEndpointListenMsg*)antEnvMsg::Receive(msg);
00112     int index = (int)(listenMsg->continuation);
00113 
00114     ftpPI[index].ListenContforDTP(listenMsg);
00115 }
00116 
00117 void
00118 TinyFTPD::ConnectContforDTP(ANTENVMSG msg)
00119 {
00120     TCPEndpointConnectMsg* connectMsg
00121         = (TCPEndpointConnectMsg*)antEnvMsg::Receive(msg);
00122     int index = (int)(connectMsg->continuation);
00123 
00124     ftpPI[index].ConnectContforDTP(connectMsg);
00125 }
00126 
00127 void
00128 TinyFTPD::SendContforDTP(ANTENVMSG msg)
00129 {
00130     TCPEndpointSendMsg* sendMsg
00131         = (TCPEndpointSendMsg*)antEnvMsg::Receive(msg);
00132     int index = (int)(sendMsg->continuation);
00133 
00134     ftpPI[index].SendContforDTP(sendMsg);
00135 }
00136 
00137 void
00138 TinyFTPD::ReceiveContforDTP(ANTENVMSG msg)
00139 {
00140     TCPEndpointReceiveMsg* receiveMsg
00141         = (TCPEndpointReceiveMsg*)antEnvMsg::Receive(msg);
00142     int index = (int)(receiveMsg->continuation);
00143 
00144     ftpPI[index].ReceiveContforDTP(receiveMsg);
00145 }
00146 
00147 void
00148 TinyFTPD::CloseContforDTP(ANTENVMSG msg)
00149 {
00150     TCPEndpointCloseMsg* closeMsg
00151         = (TCPEndpointCloseMsg*)antEnvMsg::Receive(msg);
00152     int index = (int)(closeMsg->continuation);
00153 
00154     ftpPI[index].CloseContforDTP(closeMsg);
00155 }
00156 
00157 OStatus
00158 TinyFTPD::LoadPasswd()
00159 {
00160     OSYSDEBUG(("TinyFTPD::LoadPasswd()\n"));
00161 
00162     size_t bufsize;
00163     char* buf;
00164 
00165     OStatus result = Load(FTP_PASSWD_PATH, (byte**)&buf, &bufsize);
00166     if (result != oSUCCESS) {
00167         return result;
00168     }
00169 
00170     char* end = buf + bufsize;
00171     char* cur = buf;
00172     char* head = buf;
00173     Passwd pass;
00174 
00175     while (cur < end) {
00176         while (isspace(*cur)) cur++;
00177         head = cur;
00178         while (!isspace(*cur) && (cur < end)) cur++;
00179         if (cur >= end) break;
00180         *cur++ = '\0';
00181         strcpy(pass.user, head);
00182 
00183         while (isspace(*cur)) cur++;
00184         head = cur;
00185         while (!isspace(*cur) && (cur < end)) cur++;
00186         if (cur >= end) break;
00187         *cur++ = '\0';
00188         strcpy(pass.pass, head);
00189 
00190         while (isspace(*cur)) cur++;
00191         head = cur;
00192         while (!isspace(*cur) && (cur < end)) cur++;
00193         if (cur >= end) break;
00194         *cur++ = '\0';
00195         strcpy(pass.home, head);
00196 
00197         if (passwd.PushBack(pass)) break;
00198     }
00199 
00200     DeleteRegion(buf);
00201     return oSUCCESS;
00202 }
00203 
00204 OStatus
00205 TinyFTPD::Load(char* path, byte** data, size_t* size)
00206 {
00207     FILE* fp;
00208     byte* readBuf = 0;
00209     int readBufSize;
00210     
00211     *data = 0;
00212     *size = 0;
00213 
00214     struct stat statbuf;
00215     int ret = stat(path, &statbuf);
00216     if (ret != 0) return oNOT_FOUND;
00217 
00218     readBufSize = statbuf.st_size;
00219 
00220     OSYSDEBUG(("%s size %d readBufSize %d\n",
00221                path, statbuf.st_size, readBufSize));
00222 
00223     readBuf = (byte*)malloc(readBufSize);
00224     if (readBuf == NULL) return oNO_MEMORY;
00225 
00226     fp = fopen(path, "r");
00227     if (fp == 0) {
00228         free(readBuf);
00229         return oFAIL;
00230     }
00231 
00232     ret = fread(readBuf, 1, statbuf.st_size, fp);    
00233     fclose(fp);
00234 
00235     if (ret != statbuf.st_size) {
00236         free(readBuf);
00237         return oFAIL;
00238     }
00239     
00240         *data = readBuf;
00241         *size = statbuf.st_size;
00242 
00243     return oSUCCESS;
00244 }

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