int main(int argc, char **argv){
    if (argc != 5){
        printf("Wrong args\n");
        exit(0);
    }
    fd = open(argv[4], O_WRONLY|O_CREAT);
    if (fd == -1){
        printf("Error opening log file\n");
        exit(1);
    }
    connected = 0;
    all_get = 0;
    start_seq_num = (unsigned int)rand();
    expected_seq = 0;
    last_seq = 0;
    passive_fin = 0;
    create_sockets();
    ini_send_addr(argv[2]);
    int recv_port = ini_recv_addr();
    conn_server(argv[1], argv[2], argv[3], recv_port);
    printf("Connection established!\n");
    receive_data(argv[1], argv[2], argv[3], recv_port);
    disconnect(argv[1], argv[2], argv[3], recv_port);
    close_sockets();
    close(fd);
    return 0;
}
示例#2
0
static int
establish_connection(struct tac_handle *h)
{
	int i;

	if (h->fd >= 0)		/* Already connected. */
		return 0;
	if (h->num_servers == 0) {
		generr(h, "No TACACS+ servers specified");
		return -1;
	}
	/*
         * Try the servers round-robin.  We begin with the one that
         * worked for us the last time.  That way, once we find a good
         * server, we won't waste any more time trying the bad ones.
	 */
	for (i = 0;  i < h->num_servers;  i++) {
		if (conn_server(h) == 0) {
			h->single_connect = (h->servers[h->cur_server].flags &
			    TAC_SRVR_SINGLE_CONNECT) != 0;
			return 0;
		}
		if (++h->cur_server >= h->num_servers)	/* Wrap around */
			h->cur_server = 0;
	}
	/* Just return whatever error was last reported by conn_server(). */
	return -1;
}
示例#3
0
int main(int argc, char *argv[]) {
  pid_t server[2];
  std::random_device rand;
  std::string server_port = toString(std::uniform_int_distribution<std::uint16_t>(1025)(rand));

  if((server[0] = fork()) == 0) {
    execl(argv[1], argv[1], "-s", "::1", server_port.c_str(), nullptr);
    throw std::system_error(errno, std::system_category());
  } else if(server[0] == -1) {
    throw std::system_error(errno, std::system_category());
  }

  if((server[1] = fork()) == 0) {
    execl(argv[1], argv[1], "-c", "::1", server_port.c_str(), nullptr);
    throw std::system_error(errno, std::system_category());
  } else if(server[1] == -1) {
    throw std::system_error(errno, std::system_category());
  }

  sleep(3);

  for(auto pid : server)
    if(kill(pid, 0) == -1)
      throw std::system_error(errno, std::system_category());

  sleep(std::atoi(argv[2]));

  try {
    unlink("/tmp/test1");
    unlink("/tmp/test2");
    std::cout << "connecting" << std::endl;
    libvindicat::Connection conn_server("/tmp/vindicat."+toString(server[0]), "/tmp/test1"), conn_client("/tmp/vindicat."+toString(server[1]), "/tmp/test2");

    libvindicat::RawSocket *server_socket = conn_server.forward(0xFE);
    libvindicat::RawSocket *client_socket = conn_client.forward(0xFE);

    conn_server.read();
    conn_client.read();

    bool from_server = false, from_client = false;

    server_socket->set_callback([&from_server](const std::string&, const std::string&) {from_server = true;});
    client_socket->set_callback([&from_client](const std::string&, const std::string&) {from_client = true;});
    server_socket->sendto(conn_client.identifier(), "PING");
    client_socket->sendto(conn_server.identifier(), "PING");

    while(!(from_server && from_client)) {
      conn_server.read();
      conn_client.read();
    }

    kill(server[0], SIGINT);
    kill(server[1], SIGINT);

    return (from_server && from_client? 0: 1);
  } catch(...) {
    kill(server[0], SIGINT);
    kill(server[1], SIGINT);
    throw;
  }
  return 1;
}