void configuration::sanity_check_values()
{
	if(conf[DTLS_BIND_PORT] == 0 || conf[DTLS_OUTGOING_PORT] == 0) {
		// use random but persistent ports for these
		std::ofstream conffile(conf[CONFIG_FILE], std::ios_base::out | std::ios_base::binary | std::ios_base::app);
		if(conf[DTLS_BIND_PORT] == 0) {
			uint16_t port = ntohs(get_random_port());
			iout() << "Assigned DTLS_BIND_PORT random port " << port;
			conffile << "\nDTLS_BIND_PORT=" << port << '\n';
			assign_value(DTLS_BIND_PORT, port);
		}
		if(conf[DTLS_OUTGOING_PORT] == 0) {
			uint16_t port = ntohs(get_random_port());
			iout() << "Assigned DTLS_OUTGOING_PORT random port " << port;
			conffile << "\nDTLS_OUTGOING_PORT=" << port << '\n';
			assign_value(DTLS_OUTGOING_PORT, port);
		}
	}
	if(conf[DTLS_BIND_PORT] == conf[DTLS_OUTGOING_PORT] || conf[DTLS_BIND6_PORT] == conf[DTLS_OUTGOING_PORT]) {
		eout() << "DTLS_BIND_PORT or DTLS_OUTGOING_PORT not initialized to separate valid ports, these should have been set to random ports during installation."
			<< " You must set DTLS_BIND_PORT and DTLS_OUTGOING_PORT to separate port numbers in the snow configuration file."
			   << " If you have more than one device try to choose different ports for each device.";
		abort();
	}
	check_port(conf[DTLS_OUTGOING_PORT], "DTLS_OUTGOING_PORT");
	check_port(conf[DTLS_BIND_PORT], "DTLS_BIND_PORT");
	check_port(conf[DTLS_BIND6_PORT], "DTLS_BIND6_PORT");
	check_port(conf[DHT_PORT], "DHT_PORT");
	check_port(conf[NAMESERV_PORT], "NAMESERV_PORT");
	check_nonzero(conf[NAMESERV_TIMEOUT_SECS], "NAMESERV_TIMEOUT_SECS");
	check_nonzero(conf[DTLS_IDLE_TIMEOUT_SECS], "DTLS_IDLE_TIMEOUT_SECS");
	check_nonzero(conf[HEARTBEAT_SECONDS], "HEARTBEAT_SECONDS");
	check_nonzero(conf[HEARTBEAT_RETRIES], "HEARTBEAT_RETRIES");
	if(conf[NAT_IP_GRACE_PERIOD_SECONDS] < 1800) {
		wout() << "NAT IP grace period of " << conf[NAT_IP_GRACE_PERIOD_SECONDS] << " from configuration file is too short, using minimum grace period of 1800 seconds";
		assign_value(NAT_IP_GRACE_PERIOD_SECONDS, 1800);
	}
	if(conf[DHT_BOOTSTRAP_TARGET]==0) {
		wout() << "DHT_BOOTSTRAP_TARGET cannot be zero, using default value";
		assign_value(DHT_BOOTSTRAP_TARGET, 6);
	}
	if(conf[DHT_MAX_PEERS] <= 3) {
		wout() << "DHT_MAX_PEERS cannot be " << conf[DHT_MAX_PEERS] << ", must be at least 4, using default value of 99";
		assign_value(DHT_MAX_PEERS, 99);
	}
	if(conf[NATPOOL_NETMASK_BITS] > 20 || conf[NATPOOL_NETMASK_BITS] < 4) {
		eout() << "NATPOOL_NETMASK_BITS must be between 4 and 20";
		abort();
	}
	uint32_t addr, netmask = ~htonl((1 << (32 - conf[NATPOOL_NETMASK_BITS])) - 1);
	if(inet_pton(AF_INET, conf[NATPOOL_NETWORK].c_str(), &addr) != 1 || (addr & ~netmask) != 0) {
		eout() << "NATPOOL_NETWORK/NATPOOL_NETMASK_BITS as " << conf[NATPOOL_NETWORK] << "/" << conf[NATPOOL_NETMASK_BITS] << " is not a valid subnet.";
		abort();
	}
	if(conf[VIRTUAL_INTERFACE_MTU] > 65535 || conf[VIRTUAL_INTERFACE_MTU] < 576) {
		eout() << "VIRTUAL_INTERFACE_MTU cannot be " << conf[VIRTUAL_INTERFACE_MTU];
		abort();
	}
}
Exemple #2
0
TestDebugger::TestDebugger() {
  srand(math_generate_seed());
  m_serverPort = get_random_port();
  do {
    m_adminPort = get_random_port();
  } while (m_adminPort == m_serverPort);
  do {
    m_debugPort = get_random_port();
  } while (m_debugPort == m_serverPort ||
           m_debugPort == m_adminPort);
}
bool TestExtStream::test_stream_socket_shutdown() {
  int port = get_random_port();
  string address = "127.0.0.1:" + boost::lexical_cast<string>(port);
  Variant server = f_stream_socket_server(address);
  VERIFY(f_stream_socket_shutdown(server, 0));
  return Count(true);
}
bool TestExtStream::test_stream_socket_recvfrom() {
  vector<string> addresses;
  int port = get_random_port();
  addresses.push_back("127.0.0.1:" + boost::lexical_cast<string>(port)); // TCP
  addresses.push_back("unix:///tmp/test_stream.sock"); // UNIX domain
  unlink("/tmp/test_stream.sock");

  for (unsigned int i = 0; i < addresses.size(); i++) {
    const string &address = addresses[i];

    Variant server = f_stream_socket_server(address);
    Variant client = f_stream_socket_client(address);

    Variant s = f_stream_socket_accept(server);
    String text = "testing";
    if (i == 1) {
      VERIFY(f_socket_send(client, text, 7, 0));
    } else {
      VERIFY(f_stream_socket_sendto(client, text, 0, address));
    }

    Variant buffer = f_stream_socket_recvfrom(s, 100);
    VS(buffer, "testing");
  }
  return Count(true);
}
bool TestExtStream::test_stream_get_meta_data() {
  int port = get_random_port();
  string address = string("127.0.0.1:") + boost::lexical_cast<string>(port);

  Variant server = f_stream_socket_server(address);
  Variant client = f_stream_socket_client(address);

  f_stream_set_timeout(client, 0, 500 * 1000); // 500ms
  Variant line = f_fgets(client);
  Variant meta = f_stream_get_meta_data(client);
  VS(meta["timed_out"], true);
  VS(meta["blocked"], false);

  return Count(true);
}
bool TestExtStream::test_stream_socket_sendto_issue324() {
  int port = get_random_port();
  const string address = "127.0.0.1:" + boost::lexical_cast<string>(port);

  Variant server = f_stream_socket_server(address);
  Variant client = f_stream_socket_client(address);

  Variant s = f_stream_socket_accept(server);
  String text = "testing";
  VERIFY(f_stream_socket_sendto(client, text, 0, null_string));

  Variant buffer = f_stream_socket_recvfrom(s, 100);
  VS(buffer, "testing");

  return Count(true);
}