示例#1
0
int main()
{
	/*Defining the prompt*/
	char prompt[] = "kapotFTP:~>";
	strcpy(send_data, prompt);
	
	control_port = CONTROL_PORT_NO;
	//file_port = FILE_PORT_NO;

	/* Function Calls for actual working of the server*/	
	setupServerPrimaries();
	serveClients();
}
示例#2
0
int main(int argc, char **argv)
{
	int portNumber;
	int listenedFD;

	checkArgument(argc, argv);
	Signal(SIGCHLD, sigChildHandler);

	portNumber = getPortNumber(argc, argv);

	listenedFD = Open_listenfd(portNumber);

	for (;;) 
		serveClients(listenedFD);
}
示例#3
0
void executeCommand(char *command)
{
		char BYE[] = "bye";
		char GET[] = "get";
		char PUT[] = "put";
		char DIR[] = "dir";
		char LS[] = "ls";
		char DELETE[] = "delete";
		char MKDIR[] = "mkdir";
		char RMDIR[] = "rmdir";
		char CD[] = "cd";
		char HELP[] = "help";
		
		char * first_arg ,  * second_arg ;
		int i;

		
		printf("\n\n=======New Command Entered======\n");

		if(command == NULL)
		{
			printf("The client did not enter any valid command or went down unexpectedly going to waiting mode\n");
			serveClients();
		}	
	
		printf("\n Raw command=%s \n\n", command);
		first_arg = strtok(command, DELIM);
		second_arg = strtok(NULL, DELIM);
		
		if(first_arg == NULL)
		{
			printf("The client did not enter any valid command or went down unexpectedly going to waiting mode\n");
			serveClients();
		}

		printf("first %s, second %s\n", first_arg, second_arg);


		if(!strcmp(first_arg, GET))
		{
			printf("The user entered GET command\n");	
			strcpy(response, "You just entered GET command\n");	

			if(second_arg != NULL)			
			{
				transferFile(second_arg);	
				
				for(i=0 ; i<4095 ; i++)		
				{
				    
				    response[i] = '\0';
				}
		
				send(connected, response,strlen(response), 0); 

				for(i=0 ; i<4095 ; i++)		
				{
				    
				    response[i] = '\0';
				}
						

			}
			else
				strcpy(response, "Bad command for get : Usage >> get <dir_name>\n");
		}			
		
		else if(!strcmp(first_arg, PUT))
		{   
		    if(second_arg != NULL)
			{  
			printf("The user entered PUT command\n");
			file_bytes_recieved = recv(connected, file_recv_data, 4096, 0);
		        file_recv_data[file_bytes_recieved] = '\0';
			writeToFile(second_arg, file_recv_data); //second_arg=filename; file_recv_data=actual recieved data
			printf("recieved %d bytes\n", file_bytes_recieved);
			strcpy(response, "Data bytes recieved successfully at the server.\n");		
			}
		
		    else
			strcpy(response, "Bad command for put : Usage >> put <dir_name>\n");	 	
		}
		
		else if(!strcmp(first_arg, LS) || !strcmp(first_arg, DIR))
		{
			printf("The user entered LS command\n");
			int link[2];
			pid_t pid;
  			char output_of_ls[4096];

  			if (pipe(link)==-1)
    				die("pipe");

			if ((pid = fork()) == -1)
    				die("fork");

 			if(pid == 0) {

			    dup2 (link[1], STDOUT_FILENO);
			    close(link[0]);
				if(second_arg != NULL)				
				    execl("/bin/ls", "/bin/ls", "-r", second_arg, "-l", (char *) 0);
				else
					execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);			
				    

			} 
			else {

			    close(link[1]);
				int i;
			    for(i=0 ; i<4095 ; i++)		
				{
				    output_of_ls[i] = '\0';
				    response[i] = '\0';
				}
			    printf("The value of output_of_ls after flush: %s \n", output_of_ls);	
			    read(link[0], output_of_ls, sizeof(output_of_ls));
			    printf("Output: (%s)\n", output_of_ls);
			    strcpy(response, "");
			    strcpy(response, output_of_ls);
			    
			    wait(NULL);

			}

		}
		
		else if(!strcmp(first_arg, DELETE))
		{
			printf("The user entered DELETE command\n");
			int link[2];
			pid_t pid;
  			char output_of_delete[4];

  			if (pipe(link)==-1)
    				die("pipe");

			if ((pid = fork()) == -1)
    				die("fork");

 			if(pid == 0) {

			    dup2 (link[1], STDOUT_FILENO);
			    close(link[0]);
				if(second_arg != NULL)				
				    execl("/bin/rm", "/bin/rm", "-rf", "", second_arg, (char *) 0);
				else
					strcpy(response, "Bad command for delete : Usage >> delete <filename>\n");

			} else {

			    close(link[1]);	
			    read(link[0], output_of_delete, sizeof(output_of_delete));
			    printf("Output: (%s)\n", output_of_delete);
				strcpy(response, "");
			    strcpy(response, output_of_delete);
			    wait(NULL);

			}

		}

		else if(!strcmp(first_arg, MKDIR))
		{
			printf("The user entered MKDIR command\n");
			int link[2];
			pid_t pid;
  			char output_of_mkdir[4096];

  			if (pipe(link)==-1)
    				die("pipe");

			if ((pid = fork()) == -1)
    				die("fork");

 			if(pid == 0) {

			    dup2 (link[1], STDOUT_FILENO);
			    close(link[0]);
			    
				if(second_arg != NULL)				
				    execl("/bin/mkdir", "/bin/mkdir", "", "", second_arg, (char *) 0);
				else
					strcpy(response, "Bad command for mkdir : Usage >> mkdir <dir_name>\n");

			} else {

			    close(link[1]);	
			    read(link[0], output_of_mkdir, sizeof(output_of_mkdir));
			    printf("Output: (%s)\n", output_of_mkdir);
			    strcpy(response, "");
			    wait(NULL);

			}

		}
	
		else if(!strcmp(first_arg, RMDIR))
		{
			printf("The user entered RMDIR command\n");
			int link[2];
			pid_t pid;
  			char output_of_rmdir[4];

  			if (pipe(link)==-1)
    				die("pipe");

			if ((pid = fork()) == -1)
    				die("fork");

 			if(pid == 0) {

			    dup2 (link[1], STDOUT_FILENO);
			    close(link[0]);
			    
				if(second_arg != NULL)				
				   {
					 execl("/bin/rm", "/bin/rm", "-rf", "", second_arg, (char *) 0);
					 
				    }
				else
					strcpy(response, "Bad command for mkdir : Usage >> mkdir <dir_name>\n");
				
			} else {

			    close(link[1]);	
			    read(link[0], output_of_rmdir, sizeof(output_of_rmdir));
			    printf("Output: (%s)\n", output_of_rmdir);
			    strcpy(response, "");
			    wait(NULL);

			}

		}
		
		else if(!strcmp(first_arg, CD))
		{
			printf("The user entered CD command\n");
			int link[2];
			pid_t pid;
  			char output_of_cd[4096];

  			if (pipe(link)==-1)
    				die("pipe");

			if ((pid = fork()) == -1)
    				die("fork");

 			if(pid == 0) {

			    dup2 (link[1], STDOUT_FILENO);
			    close(link[0]);
			    
				if(second_arg != NULL)				
				   {
					int result =chdir(second_arg);
					if(result != -1)					
					{strcat(response, "PWD changed to ");
					strcat(response, second_arg);
					strcat(response, "\n");
					}
					else
					{
						perror("Couldn't execute\n");
						strcat(response, "Bad directory name\n");
					}
					 
				    }
				else
					strcpy(response, "Bad command for mkdir : Usage >> mkdir <dir_name>\n");
				
			} else {

			    close(link[1]);	
			    read(link[0], output_of_cd, sizeof(output_of_cd));
			    printf("Output: (%s)\n", output_of_cd);
			    strcpy(response, "");
			    wait(NULL);

			}

		}
		
		else if(!strcmp(first_arg, BYE))
		{		
			printf("The user entered BYE command\n");
			//free(first_arg); 
			//free(second_arg);
			printf("reaching here\n");
			//closeSocket();
			//close(connected);
			serveClients();
			//exit(0);
		}
		else if(!strcmp(first_arg, HELP))
		{
			printf("The user entered BYE command\n");
			strcpy(response, "Supported commands: ls, dir, get, put, delete <filename>, mkdir <dirname>, rmdir <dirname>, cd <dirname>, bye.\n");
		}

		else
		{
			printf("Invalid command entered\n");
			strcpy(response, "Bad command : type 'help' for more info.\n");
		}
}