Exemple #1
0
static void wt_parse_command(wi_string_t *buffer) {
	wt_client_t		*client = wt_client();
	wi_string_t		*command;
	wi_array_t		*arguments;
	wi_uinteger_t	index;
	
	wi_parse_wired_command(buffer, &command, &arguments);
	
	index = wt_command_index(command);

	if(index == WI_NOT_FOUND) {
		wt_reply(501, WI_STR("Command Not Recognized"));

		return;
	}

	if(client->state < wt_commands[index].state)
		return;
	
	if(wi_array_count(arguments) < wt_commands[index].args) {
		wt_reply(503, WI_STR("Syntax Error"));

		return;
	}

	((*wt_commands[index].action) (arguments));
}
Exemple #2
0
static void wd_transfer_accept_thread(wi_runtime_instance_t *argument) {
	wi_pool_t			*pool;
	wi_socket_t			*socket = argument;
	wi_array_t			*arguments;
	wi_string_t			*ip, *string, *command;
	wd_transfer_t		*transfer;
	
	pool = wi_pool_init(wi_pool_alloc());
	
	ip = wi_address_string(wi_socket_address(socket));
	
	if(!wi_socket_accept_tls(socket, wd_transfer_socket_tls, 30.0)) {
		wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip);
		
		goto end;
	}
	
	if(!wi_socket_set_timeout(socket, 30.0))
		wi_log_warn(WI_STR("Could not set timeout for %@: %m"), ip);
	
	string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR));
	
	if(!string || wi_string_length(string) == 0) {
		if(!string)
			wi_log_warn(WI_STR("Could not read from %@: %m"), ip);
		
		goto end;
	}
	
	wi_parse_wired_command(string, &command, &arguments);
	
	if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) {
		transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0));
		
		if(!transfer)
			goto end;
		
		if(!wi_is_equal(ip, wd_user_ip(transfer->user)))
			goto end;
		
		wi_lock_lock(transfer->socket_lock);
		
		if(!transfer->socket) {
			transfer->socket = wi_retain(socket);
			wi_lock_unlock(transfer->socket_lock);
		
			wd_transfer_loop(transfer);
		} else {
			wi_lock_unlock(transfer->socket_lock);
		}
	}

end:
	wi_socket_close(socket);

	wi_release(pool);
}
Exemple #3
0
static void wd_parse_command(wi_string_t *buffer) {
	wd_client_t		*client = wd_client();
	wi_array_t		*arguments;
	wi_string_t		*command;
	unsigned int	index;
	
	wi_parse_wired_command(buffer, &command, &arguments);

	index = wd_command_index(command);

	if(index == WI_NOT_FOUND) {
		wd_reply(501, WI_STR("Command Not Recognized"));

		return;
	}

	if(client->state < wd_commands[index].state)
		return;

	if(wi_array_count(arguments) < wd_commands[index].args) {
		wd_reply(503, WI_STR("Syntax Error"));

		return;
	}

	if(wd_commands[index].activate) {
		client->idle_time = wi_time_interval();

		if(client->idle) {
			client->idle = false;

			wd_broadcast_lock();
			wd_client_broadcast_status(client);
			wd_broadcast_unlock();
		}
	}
	
	((*wd_commands[index].action) (arguments));
}
Exemple #4
0
static void wd_transfer_listen_thread(wi_runtime_instance_t *argument) {
	wi_pool_t			*pool;
	wi_socket_t			*socket;
	wi_address_t		*address;
	wi_array_t			*arguments;
	wi_string_t			*ip, *string, *command;
	wd_transfer_t		*transfer;
	
	pool = wi_pool_init(wi_pool_alloc());

	while(wd_running) {
		wi_pool_drain(pool);

		/* accept new connection */
		socket = wi_socket_accept_multiple(wd_transfer_sockets, wd_transfer_socket_context, 30.0, &address);
		
		if(!address) {
			wi_log_err(WI_STR("Could not accept a connection: %m"));
			
			continue;
		}
		
		ip = wi_address_string(address);
		
		if(!socket) {
			wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip);
			
			continue;
		}

		string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR));
		
		if(!string || wi_string_length(string) == 0) {
			if(!string)
				wi_log_warn(WI_STR("Could not read from %@: %m"), ip);

			continue;
		}
		
		/* parse command */
		wi_parse_wired_command(string, &command, &arguments);
		
		if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) {
			/* get transfer by identifier */
			transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0));
			
			if(!transfer)
				continue;
			
			if(!wi_is_equal(ip, wd_user_ip(transfer->user)))
				continue;
			
			transfer->socket = wi_retain(socket);
			
			/* spawn a transfer thread */
			if(!wi_thread_create_thread(wd_transfer_thread, transfer))
				wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip);
		}
	}
	
	wi_release(pool);
}