void FTPServerConnection::onCommand(String cmd, String data) { cmd.toUpperCase(); debugf("FTP : cmd = %s, data = %s",cmd.c_str(),data.c_str()); // We ready to quit always :) if (cmd == "QUIT") { response(221); close(); return; } // Strong security check :) if (state == eFCS_Authorization) { if (cmd == "USER") { userName = data; response(331); } else if (cmd == "PASS") { if (server->checkUser(userName, data)) { userName = ""; state = eFCS_Active; response(230); } else response(430); } else { response(530); } return; } if (state == eFCS_Active) { if (cmd == "SYST") { response(215, "Windows_NT: Sming Framework"); // Why not? It's look like Windows :) } else if (cmd == "PWD") { response(257, "\"/\""); } else if (cmd == "PORT") { cmdPort(data); } else if (cmd == "EPRT") { cmdEPRT(data); } else if (cmd == "CWD") { if (data == "/") { directoryPrefix = ""; response(250); } else { directoryPrefix = directoryPrefix + data + "/"; response(250); } } else if (cmd == "TYPE") { // response(250); response(200); } /*else if (cmd == "SIZE") { response(213, String(fileGetSize(makeFileName(data, false)))); }*/ else if (cmd == "DELE") { String name = makeFileName(data, false); if (fileExist(name)) { fileDelete(name); response(250); } else response(550); } /*else if (cmd == "RNFR") // Bugs! { renameFrom = data; response(350); } else if (cmd == "RNTO") { if (fileExist(renameFrom)) { fileRename(renameFrom, data); response(250); } else response(550); }*/ else if (cmd == "RETR") { String name = makeFileName(data, false); if (fileExist(name)) { createDataConnection(new FTPDataRetrieve(this, makeFileName(data, false))); } else { response(550); } } else if (cmd == "STOR") { createDataConnection(new FTPDataStore(this, makeFileName(data, true))); } else if (cmd == "LIST") { createDataConnection(new FTPDataFileList(this)); } else if (cmd == "PASV") { response(500 , "Passive mode not supported"); } else if (cmd == "NOOP") { response(200); } else if (!server->onCommand(cmd, data, *this)) response(502, "Not supported"); return; } debugf("!!!CASE NOT IMPLEMENTED?!!!"); }
//Main will read in port number to pass to "createconnection", //then send the successful connection to the chatFunction loop. int main(int argc, char * argv[]) { int new_fd; //int dp; int data_fd; int numbytes; char commandIn[MAXCOMMAND]; char tempCommand[MAXCOMMAND]; char* command; char* hostname; char* data_port; char* filename; char* temp; const char delimeters[] = ","; if (argc != 2) { fprintf(stderr,"usage: server <portnumber>\n"); exit(1); } //Connect to client new_fd = createConnection(argv[1]); //Error if failed receipt if ((numbytes = recv(new_fd, commandIn, MAXCOMMAND-1,0)) == -1) { printf("Receipt failed"); perror("recv"); exit(1); } commandIn[numbytes] = '\0'; bzero(tempCommand, MAXCOMMAND); strcpy(tempCommand, commandIn); printf("Server: Received command: %s\n", tempCommand); command = strtok(tempCommand, delimeters); hostname = strtok(NULL, delimeters); data_port = strtok(NULL, delimeters); filename = strtok(NULL, delimeters); //dp = atoi(data_port); //printf("\nPlease type message below after the prompt.\n"); //printf("Type '\\quit' to end chat connection.\n\n"); if(strcmp(command, "-l") == 0) { //directoryListing(dataConnection_fd); printf("directory list control flow\n"); if(send(new_fd, "Command '-l' received. Establishing Data connection\n", 55, 0) == -1) { printf("Problem Establishing connetion.\n"); perror("send"); exit(1); } data_fd = createDataConnection(data_port); sendDirectory(data_fd); } else if(strcmp(command, "-g") == 0) { printf("sendfile control flow\n"); if(send(new_fd, "Command '-g' received. Establishing Data connection\n", 55, 0) == -1) { printf("Problem Establishing connetion.\n"); perror("send"); exit(1); } data_fd = createDataConnection(data_port); sendFile(data_port, filename); //sendRequestedFile(dataConnection_fd, requestedFile); } else { if(send(new_fd, "Invalid command. Closing connection.\n", 38, 0) == -1) { printf("Problem Establishing connetion.\n"); perror("send"); exit(1); } close(new_fd); printf("Command not recognized\n Closing connection.\n"); perror("Invalid Command"); return 1; } printf("Command: %s\n", command); printf("hostname: %s\n", hostname); printf("data_port: %s\n", data_port); //Enter chat loop with client handle and file descriptor for current connection //chatFunction(new_fd, clientHandle); return 0; }