Пример #1
0
wi_boolean_t wi_p7_message_set_enum_name_for_name(wi_p7_message_t *p7_message, wi_string_t *enum_name, wi_string_t *field_name) {
	wi_p7_spec_field_t	*field;
	wi_dictionary_t		*enums;
	wi_p7_enum_t		enum_value;
	
	field = wi_p7_spec_field_with_name(p7_message->spec, field_name);
	
	if(!field) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No id found for field \"%@\""), field_name);
		
		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("wi_p7_message_set_enum_name_for_name: %m"));
		
		return false;
	}
	
	enums = wi_p7_spec_field_enums_by_name(field);
	
	if(!wi_dictionary_contains_key(enums, enum_name)) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No value found for enum \"%@\""), enum_name);
		
		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("wi_p7_message_set_enum_name_for_name: %m"));
		
		return false;
	}
	
	enum_value = (wi_p7_enum_t) (intptr_t) wi_dictionary_data_for_key(enums, enum_name);
	
	return wi_p7_message_set_enum_for_name(p7_message, enum_value, field_name);
}
Пример #2
0
wi_array_t * wi_p7_message_list_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) {
	wi_p7_spec_field_t		*field;
	wi_p7_spec_type_t		*listtype;
	wi_array_t				*list;
	wi_runtime_instance_t	*instance;
	unsigned char			*binary;
	wi_p7_type_t			listtype_id;
	uint32_t				field_size, list_size, string_size;
	
	field = wi_p7_spec_field_with_name(p7_message->spec, field_name);
	
	if(!field) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No id found for field \"%@\""), field_name);
		
		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("wi_p7_message_list_for_name: %m"));
		
		return NULL;
	}
	
	listtype		= wi_p7_spec_field_listtype(field);
	listtype_id		= wi_p7_spec_type_id(listtype);

	if(listtype_id != WI_P7_STRING) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("Unhandled type %@ in list"), wi_p7_spec_type_name(listtype));
		
		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("wi_p7_message_list_for_name: %m"));
		
		return NULL;
	}
	
	list = wi_mutable_array();
		
	if(!_wi_p7_message_get_binary_buffer_for_reading_for_name(p7_message, field_name, &binary, &field_size))
		return NULL;
	
	list_size = 0;
	
	while(list_size < field_size) {
		if(listtype_id == WI_P7_STRING) {
			string_size = wi_read_swap_big_to_host_int32(binary, list_size);
			
			list_size += 4;
			
			instance = wi_string_with_bytes(binary + list_size, string_size - 1);
			
			list_size += string_size;
		}
		
		wi_mutable_array_add_data(list, instance);
	}
	
	wi_runtime_make_immutable(list);
	
	return list;
}
Пример #3
0
wi_boolean_t wi_p7_message_set_name(wi_p7_message_t *p7_message, wi_string_t *name) {
	wi_p7_spec_message_t		*message;
	
	message = wi_p7_spec_message_with_name(p7_message->spec, name);

	if(!message) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNMESSAGE,
			WI_STR("No id found for message \"%@\""), name);
		
		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("wi_p7_message_set_name: %m"));

		return false;
	}
	
	p7_message->binary_id = wi_p7_spec_message_id(message);
	
	wi_write_swap_host_to_big_int32(p7_message->binary_buffer, 0, p7_message->binary_id);
	
	wi_retain(name);
	wi_release(p7_message->name);
	
	p7_message->name = name;
	
	return true;
}
Пример #4
0
static wi_boolean_t _wi_settings_set_value(wi_settings_t *settings, wi_string_t *name, wi_string_t *value) {
	uint32_t		index;
	wi_boolean_t	result = false;
	
	index = _wi_settings_index_of_name(settings, name);
	
	if(index == WI_NOT_FOUND) {
		wi_error_set_lib_error(WI_ERROR_SETTINGS_UNKNOWNSETTING);
		
		return false;
	}
	
	wi_log_debug(WI_STR("  %@ = %@"), name, value);
	
	switch(settings->spec[index].type) {
		case WI_SETTINGS_NUMBER:
			result = _wi_settings_set_number(settings, index, name, value);
			break;

		case WI_SETTINGS_BOOL:
			result = _wi_settings_set_bool(settings, index, name, value);
			break;

		case WI_SETTINGS_STRING:
			result = _wi_settings_set_string(settings, index, name, value);
			break;

		case WI_SETTINGS_STRING_LIST:
			result = _wi_settings_set_string_list(settings, index, name, value);
			break;

		case WI_SETTINGS_PATH:
			result = _wi_settings_set_path(settings, index, name, value);
			break;

		case WI_SETTINGS_USER:
			result = _wi_settings_set_user(settings, index, name, value);
			break;

		case WI_SETTINGS_GROUP:
			result = _wi_settings_set_group(settings, index, name, value);
			break;

		case WI_SETTINGS_PORT:
			result = _wi_settings_set_port(settings, index, name, value);
			break;
		
		case WI_SETTINGS_REGEXP:
			result = _wi_settings_set_regexp(settings, index, name, value);
			break;
		
		case WI_SETTINGS_TIME_INTERVAL:
			result = _wi_settings_set_time_interval(settings, index, name, value);
			break;
	}
	
	return result;
}
Пример #5
0
static wi_boolean_t _wi_p7_message_get_binary_buffer_for_reading_for_id(wi_p7_message_t *p7_message, uint32_t in_field_id, unsigned char **out_buffer, uint32_t *out_field_size) {
	wi_p7_spec_field_t		*field;
	unsigned char			*buffer, *start;
	uint32_t				message_size, field_id, field_size;
	
	message_size = p7_message->binary_size - WI_P7_MESSAGE_BINARY_HEADER_SIZE;
	buffer = start = p7_message->binary_buffer + WI_P7_MESSAGE_BINARY_HEADER_SIZE;
	
	while((uint32_t) (buffer - start) < message_size) {
		field_id	= wi_read_swap_big_to_host_int32(buffer, 0);
		field		= wi_p7_spec_field_with_id(p7_message->spec, field_id);
		
		if(!field) {
			wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
				WI_STR("No field found for ID %u"), field_id);
			
			if(wi_p7_message_debug)
				wi_log_debug(WI_STR("_wi_p7_message_get_binary_buffer_for_reading_for_id: %m"));
			
			break;
		}
		
		if((uint32_t) (buffer - start) + message_size < sizeof(field_id))
			break;

		buffer		+= sizeof(field_id);
		field_size	= wi_p7_spec_field_size(field);
	
		if(field_size == 0) {
			field_size = wi_read_swap_big_to_host_int32(buffer, 0);
			
			if((uint32_t) (buffer - start) + message_size < sizeof(field_size))
				break;
			
			buffer += sizeof(field_size);
		}
		
		if(field_id == in_field_id) {
			if(out_buffer)
				*out_buffer = buffer;
			
			if(out_field_size)
				*out_field_size = field_size;
			
			return true;
		}
		
		if((uint32_t) (buffer - start) + message_size < field_size)
			break;
		
		buffer += field_size;
	}
	
	return false;
}
Пример #6
0
static wi_boolean_t _wi_p7_message_get_binary_buffer_for_reading_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name, unsigned char **out_buffer, uint32_t *out_field_size) {
	wi_p7_spec_field_t	*field;
	uint32_t			field_id;
	
	field = wi_p7_spec_field_with_name(p7_message->spec, field_name);
	
	if(!field) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No id found for field \"%@\""), field_name);

		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("_wi_p7_message_get_binary_buffer_for_reading_for_name: %m"));
		
		return false;
	}
	
	field_id = wi_p7_spec_field_id(field);

	return _wi_p7_message_get_binary_buffer_for_reading_for_id(p7_message, field_id, out_buffer, out_field_size);
}
Пример #7
0
wi_string_t * wi_p7_message_enum_name_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) {
	wi_p7_spec_field_t	*field;
	wi_dictionary_t		*enums;
	wi_p7_enum_t		enum_value;
	
	if(!wi_p7_message_get_enum_for_name(p7_message, &enum_value, field_name))
		return NULL;
	
	field = wi_p7_spec_field_with_name(p7_message->spec, field_name);
	enums = wi_p7_spec_field_enums_by_value(field);
	
	if(!wi_dictionary_contains_key(enums, (void *) (intptr_t) enum_value)) {
		wi_error_set_libwired_error_with_format(WI_ERROR_P7_UNKNOWNFIELD,
			WI_STR("No name found for enum \"%u\""), enum_value);
		
		if(wi_p7_message_debug)
			wi_log_debug(WI_STR("wi_p7_message_enum_name_for_name: %m"));
		
		return NULL;
	}
	
	return wi_dictionary_data_for_key(enums, (void *) (intptr_t) enum_value);
}
Пример #8
0
void wd_dump_transfers(void) {
	wi_log_debug(WI_STR("Transfers:"));
	wi_log_debug(WI_STR("%@"), wd_transfers);
}
Пример #9
0
wi_boolean_t wi_config_read_file(wi_config_t *config) {
	wi_enumerator_t				*enumerator;
	wi_runtime_instance_t		*instance;
	wi_file_t					*file;
	wi_mutable_array_t			*array;
	wi_mutable_dictionary_t		*previous_values;
	wi_string_t					*string, *name, *value;
	wi_config_type_t			type;
	
	file = wi_file_for_reading(config->path);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %m"),
			config->path);
		
		return false;
	}
	
	wi_log_info(WI_STR("Reading %@"), config->path);
	
	config->line = 0;
	
	wi_lock_lock(config->lock);
	
	previous_values = config->values;
	
	config->values = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	if(config->defaults) {
		enumerator = wi_dictionary_key_enumerator(config->defaults);
		
		while((name = wi_enumerator_next_data(enumerator))) {
//			instance = wi_mutable_copy(wi_dictionary_data_for_key(config->defaults, name));
			instance = wi_dictionary_data_for_key(config->defaults, name);
			if(wi_runtime_id(instance) == wi_array_runtime_id())
				instance = wi_autorelease(wi_mutable_copy(instance));
			wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
//			wi_release(instance);
		}
	}
	
	while((string = wi_file_read_line(file))) {
		config->line++;

		if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#"))) {
			if(_wi_config_parse_string(config, string, &name, &value)) {
				instance = _wi_config_instance_for_setting(config, name, value, &type);
				
				if(instance) {
					wi_log_debug(WI_STR("  %@ = %@"), name, value);
					
					if(type == WI_CONFIG_STRINGLIST) {
						array = wi_dictionary_data_for_key(config->values, name);
						
						if(!array) {
							array = wi_mutable_array();
							
							wi_mutable_dictionary_set_data_for_key(config->values, array, name);
						}
						
						wi_mutable_array_add_data(array, instance);
					} else {
						wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
					}
				} else {
					_wi_config_log_error(config, name);
				}
			} else {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR);
				
				_wi_config_log_error(config, string);
			}
		}
	}
	
	enumerator = wi_dictionary_key_enumerator(config->values);
	
	while((name = wi_enumerator_next_data(enumerator))) {
		instance = wi_dictionary_data_for_key(config->values, name);
		
		if(!previous_values || !wi_is_equal(instance, wi_dictionary_data_for_key(previous_values, name)))
			wi_mutable_set_add_data(config->changes, name);
	}
	
	wi_release(previous_values);

	wi_lock_unlock(config->lock);

	return true;
}
Пример #10
0
int main(int argc, const char **argv) {
	wi_pool_t			*pool;
	wi_string_t			*string;
	int					ch, facility;
	wi_boolean_t		no_chroot, test_config;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool					= wi_pool_init(wi_pool_alloc());
	wi_log_startup			= true;
	wi_log_syslog			= true;
	wi_log_syslog_facility	= LOG_DAEMON;

	/* init core systems */
	wt_init_version();
	wt_status_lock			= wi_lock_init(wi_lock_alloc());
	wt_start_date			= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	wi_root_path			= wi_string_init_with_cstring(wi_string_alloc(), WT_ROOT);
	wi_settings_config_path	= wi_string_init_with_cstring(wi_string_alloc(), WT_CONFIG_PATH);
	no_chroot				= false;
	test_config				= false;

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "46Dd:f:hi:L:ls:tuVv")) != -1) {
		switch(ch) {
			case '4':
				wt_address_family = WI_ADDRESS_IPV4;
				break;

			case '6':
				wt_address_family = WI_ADDRESS_IPV6;
				break;

			case 'D':
				wt_daemonize = false;
				wi_log_stderr = true;
				break;

			case 'd':
				wi_release(wi_root_path);
				wi_root_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'f':
				wi_release(wi_settings_config_path);
				wi_settings_config_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'i':
				wi_log_limit = wi_string_uint32(wi_string_with_cstring(optarg));
				break;

			case 'L':
				wi_log_syslog = false;
				wi_log_file = true;

				wi_release(wi_log_path);
				wi_log_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'l':
				wi_log_level++;
				break;

			case 's':
				string = wi_string_with_cstring(optarg);
				facility = wi_log_syslog_facility_with_name(string);
				
				if(facility < 0) {
					wi_log_err(WI_STR("Could not find syslog facility \"%@\": %m"),
						string);
				}
				
				wi_log_syslog_facility = facility;
				break;

			case 't':
				test_config = true;
				break;

			case 'u':
				no_chroot = true;
				break;

			case 'V':
			case 'v':
				wt_version();
				break;

			case '?':
			case 'h':
			default:
				wt_usage();
				break;
		}
	}
	
	/* open log */
	wi_log_open();

	/* init subsystems */
	wt_init_ssl();
	wt_init_clients();
	wt_init_servers();

	/* read the config file */
	wt_settings_chroot = !no_chroot;
	wt_init_settings();

	if(!wt_read_config())
		exit(1);

	/* change root directory */
	if(!no_chroot) {
		if(!wi_change_root())
			wi_log_err(WI_STR("Could not change root to %@: %m"), wi_root_path);
	}

	/* apply config */
	wt_apply_config();

	if(test_config) {
		printf("Config OK\n");

		exit(0);
	}

	/* dump command line */
	if(wi_log_level >= WI_LOG_DEBUG) {
		wi_log_debug(WI_STR("Started as %@ %@"),
			wi_process_path(wi_process()),
			wi_array_components_joined_by_string(wi_process_arguments(wi_process()), WI_STR(" ")));
	}

	/* init tracker */
	wi_log_info(WI_STR("Starting Wired Tracker version %@"), wt_version_string);
	wt_init_tracker();

	/* detach (don't chdir, don't close i/o channels) */
	if(wt_daemonize) {
		if(!wi_daemon())
			wi_log_err(WI_STR("Could not become a daemon: %m"));
	}

	/* switch user/group */
	wi_switch_user(wt_settings.user, wt_settings.group);
		
	/* create tracker threads after privilege drop */
	wt_init_signals();
	wt_block_signals();
	wt_schedule_servers();
	wt_fork_tracker();
	wt_write_pid();
	wt_write_status(true);
	wi_log_startup = false;
	
	wi_release(pool);
	pool = wi_pool_init(wi_pool_alloc());
	
	/* enter the signal handling thread in the main thread */
	wt_signal_thread(NULL);

	/* dropped out */
	wt_cleanup();
	wi_log_close();
	wi_release(pool);

	return 0;
}