int GetFile(struct COMMAND* cmd,struct FTS* fts) { const int BUF_SIZE=1024; FILE* df=NULL; char* dfile; dfile=cmd->args[0]; if(fts->mode==FTS_MODE_ASCII) df=fopen(cmd->args[0],"r"); else if(fts->mode == FTS_MODE_BINARY) df=fopen(cmd->args[0],"rb"); if(df==NULL) { char buf[BUF_SIZE]; sendResponseCode(fts->sockfd,CHANDLER_OPERATION_FAILED); snprintf(buf,BUF_SIZE,"%s : Coulnd't open file for writing\n",dfile); write(fts->sockfd,buf,strlen(buf)); writeEOFToSocket(fts->sockfd); return (CHANDLER_OPERATION_FAILED); } sendResponseCode(fts->sockfd,0); // Sending success response code if(fts->mode==FTS_MODE_ASCII) { sendFileInAsciiMode(fts->sockfd,df); } else if(fts->mode==FTS_MODE_BINARY) { sendFileInBinaryMode(fts->sockfd,df); } return (CHANDLER_SUCCESSFUL); }
int PutFile(struct COMMAND* cmd,struct FTS* fts) { const int BUF_SIZE=1024; FILE* df=NULL; char* dfile; int ret; switch(cmd->numArgs) { case 1: dfile=cmd->args[0]; if(fts->mode==FTS_MODE_ASCII) df=fopen(cmd->args[0],"w"); else if(fts->mode == FTS_MODE_BINARY) df=fopen(cmd->args[0],"wb"); break; case 2: dfile=cmd->args[1]; if(fts->mode==FTS_MODE_ASCII) df=fopen(cmd->args[1],"w"); else if(fts->mode == FTS_MODE_BINARY) df=fopen(cmd->args[1],"wb"); break; } if(df==NULL) { const int BUF_SIZE=1024; char buf[BUF_SIZE]; sendResponseCode (fts->sockfd,-1); snprintf(buf,BUF_SIZE,"%s : Coulnd't open file for writing\n",dfile); write(fts->sockfd,buf,strlen(buf)); writeEOFToSocket (fts->sockfd); sendResponseCode (fts->sockfd,CHANDLER_OPERATION_FAILED); return (CHANDLER_OPERATION_FAILED); } sendResponseCode (fts->sockfd,0); ret=receiveResponseCode(fts->sockfd); if(ret==0) // File ready to be sent { if(fts->mode==FTS_MODE_ASCII) { writeAsciiResponseToFile(fts->sockfd,df); } else if(fts->mode==FTS_MODE_BINARY) { writeBinaryResponseToFile(fts->sockfd,df); } fclose(df); } else // Some error has occurred { printResponseText(fts->sockfd); fclose(df); return (ret); } return (CHANDLER_SUCCESSFUL); }
int serverChangeDirectory(struct COMMAND* cmd,struct FTS* fts) { if(chdir(cmd->args[0]) < 0) { sendResponseCode(fts->sockfd,CHANDLER_OPERATION_FAILED); return (CHANDLER_OPERATION_FAILED); } else { sendResponseCode(fts->sockfd,CHANDLER_SUCCESSFUL); return (CHANDLER_SUCCESSFUL); } }
int serverMakeDirectory(struct COMMAND* cmd,struct FTS* fts) { const int BUF_SIZE=1024; char buf[BUF_SIZE]; const int PERMS=0744; if(mkdir(cmd->args[0],PERMS) < 0) { snprintf(buf,BUF_SIZE,"Make directory : %s\n",strerror(errno)); write(fts->sockfd,buf,strlen(buf)); writeEOFToSocket(fts->sockfd); sendResponseCode(fts->sockfd,CHANDLER_OPERATION_FAILED); return (CHANDLER_OPERATION_FAILED); } writeEOFToSocket(fts->sockfd); sendResponseCode(fts->sockfd,CHANDLER_SUCCESSFUL); return (CHANDLER_SUCCESSFUL); }
int ChangeMode(struct COMMAND* cmd,struct FTS* fts) { if(!strcmp(cmd->cmd,"ascii")) { fts->mode=FTS_MODE_ASCII; sendResponseCode(fts->sockfd,CHANDLER_SUCCESSFUL); return (CHANDLER_SUCCESSFUL); } else if(!strcmp(cmd->cmd,"bin")) { fts->mode=FTS_MODE_BINARY; sendResponseCode(fts->sockfd,CHANDLER_SUCCESSFUL); return (CHANDLER_SUCCESSFUL); } sendResponseCode(fts->sockfd,CHANDLER_UNEXPECTED_ERROR); return (CHANDLER_UNEXPECTED_ERROR); }
int MPutFile(struct COMMAND* cmd,struct FTS* fts) { const int BUF_SIZE=1024; FILE* df=NULL; int ret,i; for(i=0;i<cmd->numArgs;i++) { sendResponseCode (fts->sockfd,i); ret=receiveResponseCode (fts->sockfd); if(ret==-1) continue; if(fts->mode==FTS_MODE_ASCII) df=fopen(cmd->args[i],"w"); else if(fts->mode == FTS_MODE_BINARY) df=fopen(cmd->args[i],"wb"); ret=errno; if(df==NULL) { char buf[BUF_SIZE]; sendResponseCode (fts->sockfd,-1); snprintf(buf,BUF_SIZE,"%s : %s\n",cmd->args[i],strerror(ret)); continue; } sendResponseCode(fts->sockfd,0); ret=receiveResponseCode(fts->sockfd); if(ret==0) // File ready to be sent { if(fts->mode==FTS_MODE_ASCII) { writeAsciiResponseToFile(fts->sockfd,df); } else if(fts->mode==FTS_MODE_BINARY) { writeBinaryResponseToFile(fts->sockfd,df); } fclose(df); } else // Some error has occurred { fclose(df); } } sendResponseCode(fts->sockfd,-1); return (CHANDLER_SUCCESSFUL); }
int MGetFile(struct COMMAND* cmd,struct FTS* fts) { const int BUF_SIZE=1024; FILE* df=NULL; int code; puts("before expanding"); expandCommand(cmd); puts ("after expanding"); sendCommand (cmd,fts->sockfd); while(1) { code=receiveResponseCode(fts->sockfd); if(code == -1) break; if(fts->mode==FTS_MODE_ASCII) df=fopen(cmd->args[code],"r"); else if(fts->mode == FTS_MODE_BINARY) df=fopen(cmd->args[code],"rb"); if(df==NULL) { char buf[BUF_SIZE]; snprintf(buf,BUF_SIZE,"%s : Coulnd't open file for writing\n",cmd->args[code]); sendResponseCode(fts->sockfd,-1); write(fts->sockfd,buf,strlen(buf)); writeEOFToSocket(fts->sockfd); continue; } sendResponseCode(fts->sockfd,0); if(fts->mode==FTS_MODE_ASCII) { sendFileInAsciiMode(fts->sockfd,df); } else if(fts->mode==FTS_MODE_BINARY) { sendFileInBinaryMode(fts->sockfd,df); } fclose(df); } return (CHANDLER_SUCCESSFUL); }
/** @brief reads a char from Serial and acts depending on the current state * @param c char from the request * @see interpretateRequest */ void Comm::readChar( char c ) { if(comm_state==COMM_LISTENING) { if(c==' ' || c=='\n' || c=='\r' ) { //word completed: interpretate interpretateRequest(recv_str); recv_str[0]='\0'; } else { //add c to word if(strlen(recv_str)<COMM_MAX_STRING_LENGTH-1) { byte len = strlen(recv_str); recv_str[ len ] = c; recv_str[ len+1 ] = '\0'; } } }else if(comm_state==COMM_RECEIVING_FILE) { if(file->isOpen()) { if(c==EOT) { file->sync(); file->close(); delete file; sendResponseCode(MSG_200); endTransmission(); comm_state=COMM_LISTENING; onFileReceived(); } else { file->write(c); } } else { delete file; sendResponseCode(MSG_500); endTransmission(); comm_state=COMM_LISTENING; } } }
/** @brief interpretates request * @details Interpretating requests are very simple, the structure is always: ACTION SUBJECT * (with a whitespace separating both). * @param req string containing the next word of the request */ void Comm::interpretateRequest(char *req) { byte response; if(req_state == REQ_ROOT) { //Determinate ACTION if( strcmpP( req, req_GET) ) { req_state = REQ_GET; } else if(strcmpP( req, req_POST )) { req_state = REQ_POST; } else if(strcmpP( req, req_DELETE )) { req_state = REQ_DELETE; } }else { //Determinate SUBJECT and launch callbacks if(req_state == REQ_GET) { if(this->GET==NULL) { sendResponseCode(MSG_500); endTransmission(); } else { response = this->GET(req); sendResponseCode(response); if(response!=MSG_200) endTransmission(); } }else if(req_state == REQ_POST) { if(this->POST==NULL) { sendResponseCode(MSG_500); endTransmission(); } else { response = this->POST(req); if(response!=MSG_200) { sendResponseCode(response); endTransmission(); } //response will be sent AFTER receiving the data } }else if(req_state == REQ_DELETE) { if(this->DELETE==NULL) { sendResponseCode(MSG_500); endTransmission(); } else { sendResponseCode(this->DELETE(req)); endTransmission(); } } req_state = REQ_ROOT; } }