Exemplo n.º 1
0
static void on_session_established(const std::string& name, fscp::server& server, const fscp::server::ep_type& host, bool is_new, const fscp::cipher_suite_type& cs, const fscp::elliptic_curve_type& ec)
{
	mutex::scoped_lock lock(output_mutex);

	std::cout << "[" << name << "] Session established with " << host << std::endl;
	std::cout << "[" << name << "] New session: " << is_new << std::endl;
	std::cout << "[" << name << "] Cipher suite: " << cs << std::endl;
	std::cout << "[" << name << "] Elliptic curve: " << ec << std::endl;

	static const std::string HELLO = "Hello you !";

	server.async_send_data(host, fscp::CHANNEL_NUMBER_3, boost::asio::buffer(HELLO), boost::bind(&simple_handler, name, "async_send_data()", _1));

	if (name == "alice")
	{
		using cryptoplus::file;

		cryptoplus::x509::certificate cert = cryptoplus::x509::certificate::from_certificate(file::open("chris.crt", "r"));

		fscp::hash_list_type hash_list;
		hash_list.insert(fscp::get_certificate_hash(cert));

		server.async_send_contact_request(host, hash_list, boost::bind(&simple_handler, name, "async_send_contact_request()", _1));
	}
}
Exemplo n.º 2
0
static void on_data(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, fscp::channel_number_type channel_number, boost::asio::const_buffer data)
{
	const std::string str_data(boost::asio::buffer_cast<const char*>(data), boost::asio::buffer_size(data));

	std::cout << "[" << name << "] Received DATA on channel " << static_cast<unsigned int>(channel_number) << " from " << sender << ": " << str_data << std::endl;

	if (name == "alice")
	{
		if (str_data == "Hello ! I'm chris")
		{
			const std::string common_name = server.identity().signature_certificate().subject().find(NID_commonName)->data().str();
			const std::string new_common_name = "denis";

			if (common_name != new_common_name)
			{
				std::cout << "[" << name << "] My current name is " << common_name << ". Switching to " << new_common_name << "." << std::endl;

				server.set_identity(load_identity_store(new_common_name));
			}
		}
		else
		{
			using cryptoplus::file;

			cryptoplus::x509::certificate cert = cryptoplus::x509::certificate::from_certificate(file::open("chris.crt", "r"));

			server.async_send_contact_request(sender, cert);
		}
	}
}
Exemplo n.º 3
0
static void on_data(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, fscp::channel_number_type channel_number, fscp::server::shared_buffer_type, boost::asio::const_buffer data)
{
	static_cast<void>(server);

	static std::atomic<int> send_counter;

	if (channel_number == fscp::CHANNEL_NUMBER_3) {
		const std::string str_data(boost::asio::buffer_cast<const char*>(data), boost::asio::buffer_size(data));

		mutex::scoped_lock lock(output_mutex);

		std::cout << "[" << name << "] Received DATA on channel " << static_cast<unsigned int>(channel_number) << " from " << sender << ": " << str_data << std::endl;

	} else if (channel_number == fscp::CHANNEL_NUMBER_4) {

		const int receive_counter = *boost::asio::buffer_cast<const int*>(data);

		mutex::scoped_lock lock(output_mutex);

		std::cout << "[" << name << "] Received DATA on channel " << static_cast<unsigned int>(channel_number) << " from " << sender << ": #" << receive_counter << std::endl;
	}

	if ((name == "alice") || (name == "chris")) {
		const boost::shared_ptr<int> local_counter(new int(send_counter++));

		server.async_send_data(
			sender,
			fscp::CHANNEL_NUMBER_4,
			boost::asio::buffer(local_counter.get(), sizeof(int)),
			[name, local_counter](const boost::system::error_code& ec) {
				simple_handler(name, "async_send_data()", ec);
			}
		);
	}
}
Exemplo n.º 4
0
static void on_contact_message(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, fscp::hash_type hash, const fscp::server::ep_type& target)
{
	mutex::scoped_lock lock(output_mutex);

	std::cout << "[" << name << "] Received CONTACT from " << sender << ": " << hash << " is at " << target << std::endl;

	server.async_greet(target, boost::bind(&on_hello_response, name, boost::ref(server), target, _1, _2));
}
Exemplo n.º 5
0
static bool on_presentation(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, fscp::server::cert_type sig_cert, fscp::server::cert_type /*enc_cert*/, bool /*is_new*/)
{
	std::cout << "[" << name << "] Received PRESENTATION from " << sender << " (" << sig_cert.subject().oneline() << ")" << std::endl;

	server.async_request_session(sender);

  return true;
}
Exemplo n.º 6
0
static bool on_hello_request(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, bool default_accept)
{
	std::cout << "[" << name << "] Received HELLO request from " << sender << std::endl;

  server.async_introduce_to(sender);

	return default_accept;
}
Exemplo n.º 7
0
static bool on_session(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, bool default_accept)
{
	std::cout << "[" << name << "] Received SESSION from " << sender << std::endl;

	server.async_send_data(sender, fscp::CHANNEL_NUMBER_3, boost::asio::buffer(std::string("Hello ! I'm " + name)));

	return default_accept;
}
Exemplo n.º 8
0
static bool on_presentation(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, fscp::server::cert_type sig_cert, fscp::server::presentation_status_type status)
{
	mutex::scoped_lock lock(output_mutex);

	std::cout << "[" << name << "] Received PRESENTATION from " << sender << " (" << sig_cert.subject().oneline() << ") - " << status << std::endl;

	server.async_request_session(sender, boost::bind(&simple_handler, name, "async_request_session()", _1));

	return true;
}
Exemplo n.º 9
0
static bool on_hello(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, bool default_accept)
{
	static_cast<void>(server);

	mutex::scoped_lock lock(output_mutex);

	std::cout << "[" << name << "] Received HELLO request from " << sender << " (default accept is: " << default_accept << ")" << std::endl;

	server.async_introduce_to(sender, boost::bind(&simple_handler, name, "async_introduce_to()", _1));

	return default_accept;
}
Exemplo n.º 10
0
static void on_hello_response(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, const boost::posix_time::time_duration& time_duration, bool success)
{
	if (!success)
	{
		std::cout << "[" << name << "] Received no HELLO response from " << sender << " after " << time_duration.total_milliseconds() << " ms" << std::endl;
	} else
	{
		std::cout << "[" << name << "] Received HELLO response from " << sender << " (" << time_duration.total_milliseconds() << " ms)" << std::endl;

		server.async_introduce_to(sender);
	}
}
Exemplo n.º 11
0
static void on_hello_response(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, const boost::system::error_code& ec, const boost::posix_time::time_duration& duration)
{
	mutex::scoped_lock lock(output_mutex);

	if (ec)
	{
		std::cout << "[" << name << "] Received no HELLO response from " << sender << " after " << duration << ": " << ec.message() << std::endl;
	}
	else
	{
		std::cout << "[" << name << "] Received HELLO response from " << sender << " after " << duration << ": " << ec.message() << std::endl;

		server.async_introduce_to(sender, boost::bind(&simple_handler, name, "async_introduce_to()", _1));

		std::cout << "[" << name << "] Sending a presentation message to " << sender << std::endl;
	}
}
Exemplo n.º 12
0
static void _stop_function(fscp::server& s1, fscp::server& s2, fscp::server& s3)
{
	s1.close();
	s2.close();
	s3.close();
}
Exemplo n.º 13
0
static void on_contact_message(const std::string& name, fscp::server& server, const fscp::server::ep_type& sender, fscp::server::cert_type cert, const fscp::server::ep_type& target)
{
	std::cout << "[" << name << "] Received CONTACT from " << sender << ": " << cert.subject().oneline() << " is at " << target << std::endl;

	server.async_greet(target, boost::bind(&on_hello_response, name, boost::ref(server), _1, _2, _3));
}