Ejemplo n.º 1
0
void	run_commands(t_command *list, char ***env)
{
	int			s_stdin;
	t_command	*save;

	save = list;
	s_stdin = dup(STDIN_FILENO);
	while (list)
	{
		choose_command(list, env);
		list = list->next;
	}
	while (save)
	{
		wait(0);
		save = save->next;
	}
	dup2(s_stdin, 0);
}
Ejemplo n.º 2
0
/* main function for snp */
int main(int argc, char** argv){
	int n, port_p, port_a, ret;
	int fd_chat;
	socklen_t addrlen;
	fd_set rfds;
	int maxfd, counter;
	struct hostent *h;
	struct sockaddr_in addr_chat;
	char opt, input[10];
	char buffer[512];
	char options[128];
	char *cmd;
	char *msg;
	char *surname, *snpip, *snpport;
	char *saip, *saport;
	list *start,*temp;
	start = (list *)malloc(sizeof(list)); 
	
	temp = start;
	temp -> prox = NULL;
	temp -> ant = NULL;
	
	surname = NULL;
	snpip = NULL;
	snpport = NULL;
	saip = NULL;
	saport = NULL;
	
	
	/* usage of getopt function which deals with the input (argv)*/
	strcpy(input, "n:s:q:i:p:");
	while((opt = getopt(argc, argv, input))!=-1){
		if(opt == '?'){
			if(optopt == 'n' || optopt == 's' || optopt == 'q' || optopt == 'i' || optopt == 'p'){
				write(1, "Required arg for option -", strlen("Required arg for option -"));
				printf("%c", optopt);
				write(1, "\n", 1);
			}
			else if(isprint(optopt)) {
				write(1,"Unknown option -", strlen("Unknown option -"));
				printf("%c", optopt);
				write(1, "\n", 1);
			}
			else{
				printf("Unknown option character `\\x%x`.\n",optopt);
			}
			return -1;
		}
		if(opt == 'n'){
			surname = optarg;
		}
		if(opt == 's'){
			snpip = optarg;			
		}
		if(opt == 'q'){
			snpport = optarg;			
		}
		if(opt == 'i'){
			saip = optarg;			
		}
		if(opt == 'p'){
			saport = optarg;
		}		
	}

   	/* check if surname, ip and port were given from argv otherwise close */
	if(surname == NULL || snpip == NULL || snpport == NULL){
		exit(1);
	}
	
	/* since it is not mandatory to give the ip of the sa: */ 
	/* we need to check if it was given or if we should use the default (tejo) */
	if(saip == NULL){
		/* connect to the sa, tejo in this case */
		h = gethostbyname("tejo.tecnico.ulisboa.pt");
		if(h == NULL){
			write(1, "Unable to reach sa (tejo)\n", strlen("Unable to reach sa (tejo)\n"));
			exit(1);
		}
	}else{
		/* connect to the sa, other than tejo in this case */
		inet_pton(AF_INET, saip, &temp);
		h = gethostbyaddr(&temp,sizeof(temp),AF_INET);
		if(h == NULL){
			write(1, "Unable to reach sa\n", strlen("Unable to reach sa\n"));
			exit(1);
		}
	}
	
	/* since it is not mandatory to give the port of the sa: */ 
	/* we need to check if it was given or if we should use the default (58000) */
	if(saport == NULL){
		/* no port given */
		port_a = 58000;
	}else{
		/* get port given by input */
		port_a = atoi(saport);
	}

	/* get port given by input for this snp */
	if(sscanf(snpport, "%d\n", &port_p) != 1) {
		write(1, "Not a valid port\n", strlen("Not a valid port\n"));
		exit(1);
	}
	
	/* initializes the socket used to communicate with schats */	
	fd_chat = socket(AF_INET,SOCK_DGRAM, 0);
	if(fd_chat == -1) exit(1);
	
	sscanf(snpport,"%d\n",&port_p);
	memset((void*)&addr_chat, (int)'\0', sizeof(addr_chat));
	addr_chat.sin_family = AF_INET;
	addr_chat.sin_addr.s_addr = htonl(INADDR_ANY);
	addr_chat.sin_port = htons(port_p);

	ret = bind(fd_chat, (struct sockaddr*)&addr_chat, sizeof(addr_chat));
	if(ret == -1) exit(1);
	
	/* register the snp server in the sa serverc*/
	init_sa(h, port_a, surname, snpip, snpport);


	while(1){
		FD_ZERO(&rfds);
		FD_SET(fd_chat,&rfds);
		FD_SET(fileno(stdin), &rfds);
		maxfd = max(fd_chat,fileno(stdin));
		
		/* returns the number of file descriptors ready */
		counter = select(maxfd+1, &rfds, (fd_set*)NULL, (fd_set*)NULL, (struct timeval *)NULL);
		if(counter <= 0) exit(1);
		
		if(FD_ISSET(fileno(stdin), &rfds)){
			fgets(options, 128, stdin);		
			if(strcmp(options, "list\n") == 0){
				/* The typed command is LIST */
				imprime_lista(start->prox);
			}else{
				if(strcmp(options, "exit\n") == 0){
					/* The typed command is EXIT */
					free_sa( h, 58000, surname);
					break;
				}
				else write(1, "Not an option\nThe options are: \n1)list\n2)exit\n", strlen("Not an option\nThe options are: \n1)list\n2)exit\n"));
			}
		}

		if(FD_ISSET(fd_chat, &rfds)){ /*fd_chat is ready*/
			memset(buffer,0,strlen(buffer));
			addrlen = sizeof(addr_chat);
			n = recvfrom(fd_chat, buffer, 128, 0, (struct sockaddr*)&addr_chat, &addrlen);
			if(n == -1) exit(1);
			/* Received command */
			buffer[n] = '\0';
			cmd = (char*) malloc(sizeof(char)*4);
			strncpy(cmd,buffer, 3);
			cmd[3] = '\0';

			if(choose_command(cmd)==0){
				/* The received command is to register a user (REG) */
				msg = register_new(start, buffer, n, surname);
				n = sendto(fd_chat, msg, strlen(msg), 0, (struct sockaddr*)&addr_chat, addrlen);
				if(n==-1) {
					write(1, "NOK message not sent. Error replying!", strlen("NOK message not sent. Error replying!"));
				}
			}
			if(choose_command(cmd) == 1){
				/* The received command is to delete a registered user (UNR) */
				msg = u_unregister(start, buffer, n, surname);
				n = sendto(fd_chat, msg, strlen(msg), 0, (struct sockaddr*)&addr_chat, addrlen);
				if(n==-1) {
					write(1, "NOK message not sent. Error replying!", strlen("NOK message not sent. Error replying!"));
				}
			}
			/* The received command is to query a user (QRY) */
			if(choose_command(cmd) == 2){
				msg = do_qry(start, buffer, n, h, 58000, surname);
				n = sendto(fd_chat, msg, strlen(msg), 0, (struct sockaddr*)&addr_chat, addrlen);		
				if(n==-1) {
					write(1, "NOK message not sent. Error replying!", strlen("NOK message not sent. Error replying!"));
				}
			}
			free(cmd);

		}
		
	}
	close(fd_chat);
	free(start);
    exit(0);
}