Exemplo n.º 1
0
wi_boolean_t wi_p7_message_set_data_for_name(wi_p7_message_t *p7_message, wi_data_t *data, wi_string_t *field_name) {
	unsigned char	*binary;
	uint32_t		field_size, field_id;
	
	if(!data)
		data = wi_data();
	
	if(p7_message->serialization == WI_P7_BINARY) {
		field_size = wi_data_length(data);
		
		if(!_wi_p7_message_get_binary_buffer_for_writing_for_name(p7_message, field_name, field_size, &binary, &field_id))
			return false;
		
		wi_write_swap_host_to_big_int32(binary, 0, field_id);
		wi_write_swap_host_to_big_int32(binary, 4, field_size);
		
		memcpy(binary + 8, wi_data_bytes(data), field_size);
	} else {
		_wi_p7_message_set_xml_field(p7_message, WI_P7_DATA, field_name, wi_data_base64(data));
	}
		
	return true;
}
Exemplo n.º 2
0
void DRUser::setObjectWithMessage(wi_p7_message_t *message) {
    if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_list")) ||
       wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_join")))
    {
        wi_p7_message_get_int32_for_name(message, &userID, WI_STR("wired.user.id"));
        wi_p7_message_get_bool_for_name(message, &this->isIdle, WI_STR("wired.user.idle"));
        wi_p7_message_get_uint32_for_name(message, &this->color, WI_STR("wired.account.color"));

        this->nick = QString(wi_string_cstring(wi_p7_message_string_for_name(message, WI_STR("wired.user.nick"))));
        this->status = QString(wi_string_cstring(wi_p7_message_string_for_name(message, WI_STR("wired.user.status"))));

        QString base64icon = QString(wi_string_cstring(wi_data_base64(wi_p7_message_data_for_name(message, WI_STR("wired.user.icon")))));
        this->icon = DR::iconForBase64String(base64icon);
    }
    else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_status")))
    {
        wi_p7_message_get_bool_for_name(message, &this->isIdle, WI_STR("wired.user.idle"));
        wi_p7_message_get_uint32_for_name(message, &this->color, WI_STR("wired.account.color"));

        this->nick = QString(wi_string_cstring(wi_p7_message_string_for_name(message, WI_STR("wired.user.nick"))));
        this->status = QString(wi_string_cstring(wi_p7_message_string_for_name(message, WI_STR("wired.user.status"))));
    }
}
Exemplo n.º 3
0
void wi_p7_message_serialize(wi_p7_message_t *p7_message, wi_p7_serialization_t serialization) {
	xmlDocPtr				doc;
	xmlNsPtr				ns;
	xmlNodePtr				root_node, list_node, item_node;
	wi_p7_spec_field_t		*field;
	wi_p7_spec_type_t		*type;
	wi_runtime_instance_t	*instance;
	wi_string_t				*field_name, *field_value;
	unsigned char			*buffer, *start;
	wi_uuid_t				*uuid;
	wi_date_t				*date;
	wi_p7_boolean_t			p7_bool;
	wi_p7_int32_t			p7_int32;
	wi_p7_uint32_t			p7_uint32;
	wi_p7_int64_t			p7_int64;
	wi_p7_uint64_t			p7_uint64;
	wi_p7_double_t			p7_double;
	wi_p7_oobdata_t			p7_oobdata;
	wi_string_t				*string;
	wi_data_t				*data;
	wi_array_t				*list;
	wi_uinteger_t			i, count;
	uint32_t				message_size, field_id, field_size;
	
	if(serialization == WI_P7_XML && !p7_message->xml_buffer) {
		doc			= xmlNewDoc((xmlChar *) "1.0");
		root_node	= xmlNewNode(NULL, (xmlChar *) "message");
		
		xmlDocSetRootElement(doc, root_node);
		
		ns = xmlNewNs(root_node, (xmlChar *) "http://www.zankasoftware.com/P7/Message", (xmlChar *) "p7");
		xmlSetNs(root_node, ns);
		
		xmlSetProp(root_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(p7_message->name));
		
		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);
			buffer			+= sizeof(field_id);
			field			= wi_p7_spec_field_with_id(p7_message->spec, field_id);

			if(!field)
				continue;
			
			field_size		= wi_p7_spec_field_size(field);
			
			if(field_size == 0) {
				field_size = wi_read_swap_big_to_host_int32(buffer, 0);
				
				buffer += sizeof(field_size);
			}
			
			field_name		= wi_p7_spec_field_name(field);
			field_value		= NULL;
			type			= wi_p7_spec_field_type(field);

			switch(wi_p7_spec_type_id(type)) {
				case WI_P7_BOOL:
					if(wi_p7_message_get_bool_for_name(p7_message, &p7_bool, field_name))
						field_value = wi_string_with_format(WI_STR("%u"), p7_bool ? 1 : 0);
					break;
					
				case WI_P7_ENUM:
					string = wi_p7_message_enum_name_for_name(p7_message, field_name);
					
					if(string)
						field_value = string;
					break;
					
				case WI_P7_INT32:
					if(wi_p7_message_get_int32_for_name(p7_message, &p7_int32, field_name))
						field_value = wi_string_with_format(WI_STR("%u"), p7_int32);
					break;
					
				case WI_P7_UINT32:
					if(wi_p7_message_get_uint32_for_name(p7_message, &p7_uint32, field_name))
						field_value = wi_string_with_format(WI_STR("%u"), p7_uint32);
					break;
					
				case WI_P7_INT64:
					if(wi_p7_message_get_int64_for_name(p7_message, &p7_int64, field_name))
						field_value = wi_string_with_format(WI_STR("%lld"), p7_int64);
					break;
					
				case WI_P7_UINT64:
					if(wi_p7_message_get_uint64_for_name(p7_message, &p7_uint64, field_name))
						field_value = wi_string_with_format(WI_STR("%llu"), p7_uint64);
					break;
					
				case WI_P7_DOUBLE:
					if(wi_p7_message_get_double_for_name(p7_message, &p7_double, field_name))
						field_value = wi_string_with_format(WI_STR("%f"), p7_double);
					break;
					
				case WI_P7_STRING:
					string = wi_p7_message_string_for_name(p7_message, field_name);
					
					if(string)
						field_value = string;
					break;
					
				case WI_P7_UUID:
					uuid = wi_p7_message_uuid_for_name(p7_message, field_name);
					
					if(uuid)
						field_value = wi_uuid_string(uuid);
					break;
					
				case WI_P7_DATE:
					date = wi_p7_message_date_for_name(p7_message, field_name);
					
					if(date)
						field_value = wi_string_with_format(WI_STR("%f"), wi_date_time_interval(date));
					break;
					
				case WI_P7_DATA:
					data = wi_p7_message_data_for_name(p7_message, field_name);
					
					if(data)
						field_value = wi_data_base64(data);
					break;
					
				case WI_P7_OOBDATA:
					if(wi_p7_message_get_oobdata_for_name(p7_message, &p7_oobdata, field_name))
						field_value = wi_string_with_format(WI_STR("%llu"), p7_oobdata);
					break;
					
				case WI_P7_LIST:
					list = wi_p7_message_list_for_name(p7_message, field_name);
					
					if(list) {
						list_node = wi_xml_node_child_with_name(root_node, field_name);
						
						if(!list_node) {
							list_node = xmlNewNode(ns, (xmlChar *) "field");
							xmlSetProp(list_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(field_name));
							xmlAddChild(root_node, list_node);
						}
						
						count = wi_array_count(list);
						
						for(i = 0; i < count; i++) {
							item_node = xmlNewNode(ns, (xmlChar *) "item");
							instance = WI_ARRAY(list, i);

							if(wi_runtime_id(instance) == wi_string_runtime_id())
								xmlNodeSetContent(item_node, (xmlChar *) wi_string_cstring(instance));
							
							xmlAddChild(list_node, item_node);
						}
					}
					break;
			}
			
			if(field_value) {
				item_node = wi_xml_node_child_with_name(root_node, field_name);
				
				if(!item_node) {
					item_node = xmlNewNode(ns, (xmlChar *) "field");
					xmlSetProp(item_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(field_name));
					xmlAddChild(root_node, item_node);
				}
				
				xmlNodeSetContent(item_node, (xmlChar *) wi_string_cstring(field_value));

			}
			
			buffer += field_size;
		}
		
		xmlDocDumpMemoryEnc(doc, &p7_message->xml_buffer, &p7_message->xml_length, "UTF-8");
		xmlFreeDoc(doc);
	}
}
Exemplo n.º 4
0
void wd_server_apply_settings(void) {
	wi_data_t		*data;
	wi_string_t		*hostname;

	/* reload banner */
	if(wd_settings.banner) {
		if(wd_settings.banner_changed) {
			data = wi_data_init_with_contents_of_file(wi_data_alloc(), wd_settings.banner);
			
			if(data) {
				wi_release(wd_banner);
				wd_banner = wi_retain(wi_data_base64(data));
			} else {
				wi_log_err(WI_STR("Could not open %@: %m"), wd_settings.banner);
			}

			wi_release(data);
		}
	} else {
		wi_release(wd_banner);
		wd_banner = NULL;
	}

	/* reload server name/description */
	if(wd_settings.name_changed || wd_settings.description_changed)
		wd_server_send_server_info(true);

	/* set SSL cipher list */
	if(wd_settings.controlcipher) {
		if(!wi_socket_tls_set_ciphers(wd_control_socket_tls, wd_settings.controlcipher)) {
			wi_log_err(WI_STR("Could not set TLS cipher list \"%@\": %m"),
				wd_settings.controlcipher);
		}
	}

	if(wd_settings.transfercipher) {
		if(!wi_socket_tls_set_ciphers(wd_transfer_socket_tls, wd_settings.transfercipher)) {
			wi_log_err(WI_STR("Could not set TLS cipher list \"%@\": %m"),
				wd_settings.transfercipher);
	   }
	}

	/* load SSL certificate */
	if(!wd_certificate && !wd_private_key) {
		if(wd_settings.certificate) {
			wd_private_key = wi_rsa_init_with_pem_file(wi_rsa_alloc(), wd_settings.certificate);
			
			if(!wd_private_key)
				wi_log_warn(WI_STR("Could not find RSA key in %@, creating one..."), wd_settings.certificate);
			
			wd_certificate = wi_x509_init_with_pem_file(wi_x509_alloc(), wd_settings.certificate);
			
			if(!wd_certificate)
				wi_log_warn(WI_STR("Could not find certificate in %@, creating one..."), wd_settings.certificate);
		}
		
		if(!wd_private_key) {
			wd_private_key = wi_rsa_init_with_bits(wi_rsa_alloc(), 1024);
			
			if(wd_private_key)
				wi_log_info(WI_STR("Created 1024-bit RSA key"));
			else
				wi_log_err(WI_STR("Could not create RSA key: %m"));
		}
		
		if(!wd_certificate) {
			hostname = wi_process_hostname(wi_process());
			wd_certificate = wi_x509_init_with_common_name(wi_x509_alloc(), wd_private_key, hostname);
			
			if(wd_certificate)
				wi_log_info(WI_STR("Created self-signed certificate for %@"), hostname);
			else
				wi_log_err(WI_STR("Could not create self-signed certificate: %m"));
		}
		
		if(!wi_socket_tls_set_private_key(wd_control_socket_tls, wd_private_key) ||
		   !wi_socket_tls_set_private_key(wd_transfer_socket_tls, wd_private_key)) {
			wi_log_err(WI_STR("Could not set TLS private key: %m"));
		}
		
		if(!wi_socket_tls_set_certificate(wd_control_socket_tls, wd_certificate) ||
		   !wi_socket_tls_set_certificate(wd_transfer_socket_tls, wd_certificate)) {
			wi_log_err(WI_STR("Could not set TLS certificate: %m"));
		}
	}
}
Exemplo n.º 5
0
static wi_boolean_t _wi_plist_write_instance_to_node(wi_runtime_instance_t *instance, xmlNodePtr node) {
	wi_enumerator_t			*enumerator;
	wi_array_t				*keys;
	wi_runtime_instance_t	*value;
	xmlNodePtr				child_node;
	void					*key;
	wi_runtime_id_t			id;
	wi_number_type_t		type;
	
	id = wi_runtime_id(instance);
	
	if(id == wi_string_runtime_id()) {
		wi_xml_node_new_child(node, WI_STR("string"), instance);
	}
	else if(id == wi_number_runtime_id()) {
		type = wi_number_type(instance);
		
		if(type == WI_NUMBER_BOOL) {
			if(wi_number_bool(instance))
				wi_xml_node_new_child(node, WI_STR("true"), NULL);
			else
				wi_xml_node_new_child(node, WI_STR("false"), NULL);
		} else {
			if(type == WI_NUMBER_FLOAT || type == WI_NUMBER_DOUBLE)
				wi_xml_node_new_child(node, WI_STR("real"), wi_number_string(instance));
			else
				wi_xml_node_new_child(node, WI_STR("integer"), wi_number_string(instance));
		}
	}
	else if(id == wi_data_runtime_id()) {
		wi_xml_node_new_child(node, WI_STR("data"), wi_data_base64(instance));
	}
	else if(id == wi_date_runtime_id()) {
		wi_xml_node_new_child(node, WI_STR("date"), wi_date_string_with_format(instance, WI_STR("%Y-%m-%dT%H:%M:%SZ")));
	}
	else if(id == wi_dictionary_runtime_id()) {
		child_node = wi_xml_node_new_child(node, WI_STR("dict"), NULL);
		
		keys = wi_dictionary_all_keys(instance);

		wi_array_sort(keys, wi_string_compare);
		
		enumerator = wi_array_data_enumerator(keys);
		
		while((key = wi_enumerator_next_data(enumerator))) {
			value = wi_dictionary_data_for_key(instance, key);
			
			wi_xml_node_new_child(child_node, WI_STR("key"), key);
			
			if(!_wi_plist_write_instance_to_node(value, child_node))
				return false;
		}
	}
	else if(id == wi_array_runtime_id()) {
		child_node = wi_xml_node_new_child(node, WI_STR("array"), NULL);
		
		xmlAddChild(node, child_node);
		
		enumerator = wi_array_data_enumerator(instance);
		
		while((value = wi_enumerator_next_data(enumerator))) {
			if(!_wi_plist_write_instance_to_node(value, child_node))
				return false;
		}
	}
	else {
		wi_error_set_libwired_error_with_format(WI_ERROR_PLIST_WRITEFAILED,
			WI_STR("Unhandled class %@"), wi_runtime_class_name(instance));
		
		return false;
	}
	
	return true;
}
Exemplo n.º 6
0
void wd_server_apply_settings(void) {
	wi_string_t		*string;
	wi_data_t		*data;

	/* reload banner */
	if(wd_settings.banner) {
		if(wd_settings.banner_changed) {
			data = wi_data_init_with_contents_of_file(wi_data_alloc(), wd_settings.banner);
			
			if(data) {
				wi_release(wd_banner);
				wd_banner = wi_retain(wi_data_base64(data));
			} else {
				wi_log_err(WI_STR("Could not open %@: %m"), wd_settings.banner);
			}

			wi_release(data);
		}
	} else {
		wi_release(wd_banner);
		wd_banner = NULL;
	}

	/* reload server name/description */
	if(wd_settings.name_changed || wd_settings.description_changed) {
		string = wi_date_iso8601_string(wd_start_date);
		
		wd_broadcast(wd_public_chat, 200, WI_STR("%#@%c%#@%c%#@%c%#@%c%#@%c%u%c%llu"),
					 wd_server_version_string,		WD_FIELD_SEPARATOR,
					 wd_protocol_version_string,	WD_FIELD_SEPARATOR,
					 wd_settings.name,				WD_FIELD_SEPARATOR,
					 wd_settings.description,		WD_FIELD_SEPARATOR,
					 string,						WD_FIELD_SEPARATOR,
					 wd_files_unique_count,			WD_FIELD_SEPARATOR,
					 wd_files_unique_size);
	}

	/* set SSL cipher list */
	if(wd_settings.controlcipher) {
		if(!wi_socket_context_set_ssl_ciphers(wd_control_socket_context, wd_settings.controlcipher)) {
			wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"),
				wd_settings.controlcipher);
		}
	}

	if(wd_settings.transfercipher) {
		if(!wi_socket_context_set_ssl_ciphers(wd_transfer_socket_context, wd_settings.transfercipher)) {
			wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"),
				wd_settings.transfercipher);
	   }
	}

	/* load SSL certificate */
	if(wd_settings.certificate) {
		if(!wi_socket_context_set_ssl_certificate(wd_control_socket_context, wd_settings.certificate) ||
		   !wi_socket_context_set_ssl_certificate(wd_transfer_socket_context, wd_settings.certificate)) {
			wi_log_err(WI_STR("Could not load certificate %@: %m"),
				wd_settings.certificate);
		}
	}
}