Ejemplo n.º 1
0
static void
test_connect_first (void)
{
  int s = open_server_socket ();
  struct sockaddr_in ia;
  socklen_t addrlen;

  int c1, c2;

  if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
    failed ("can read, socket not connected");

  c1 = connect_to_socket (false);

  if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
    failed ("expecting readability on passive socket");
  if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
    failed ("expecting readability on passive socket");

  addrlen = sizeof (ia);
  c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
  close (s);
  close (c1);
  close (c2);
}
Ejemplo n.º 2
0
static void
test_connect_first (void)
{
    int s = open_server_socket ();
    struct sockaddr_in ia;
    socklen_t addrlen;

    int c1, c2;

    if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
        failed ("can read, socket not connected");

    c1 = connect_to_socket (false);

    if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
        failed ("expecting POLLIN | POLLRDNORM on passive socket");
    if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
        failed ("expecting POLLIN on passive socket");
    if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
        failed ("expecting POLLRDNORM on passive socket");

    addrlen = sizeof (ia);
    c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
    close (s);
    close (c1);
    close (c2);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    if (argc > 1) // devo fare il dump in un file anziché a schermo
        dumpFile = argv[1];

    // Maschera i segnali
    int retval;
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigaddset(&set, SIGTERM);
    SC_OR_FAIL(pthread_sigmask(SIG_SETMASK, &set, NULL), retval, "Impossibile gestire i segnali in visualizer");

    // Installa un handler per il segnale di terminazione
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = terminate;
    SC_OR_FAIL(sigaction(SIGUSR2, &sa, NULL), retval, "Impossibile gestire i segnali in visualizer");

    unsigned int nrow = UINT_MAX; // Numero di colonne della matrice
    unsigned int ncol = UINT_MAX; // Numero di righe della matrice
    while (!mustTerminate) {
        if (connect_to_socket(&nrow, &ncol) && new_planet_matrix(nrow, ncol) && read_from_socket(nrow, ncol))
            print_or_dump_planet_matrix(nrow, ncol);
        close(visualizerSocket);
    }

    DEBUG_PRINT("Visualizer sta per terminare...\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
void *
serve_client (void *data)
{
  struct thread_data *thread_data = data;
  int client_sock = thread_data->client_sock;
  int server_sock;
  FILE *protocol = thread_data->protocol_file;
  FILE *client;
  FILE *server;
  int err;

  client = NULL;
  server = NULL;

  /* Connect to server.  */
  err = connect_to_socket (opt.server_spec, &server_sock);
  if (err)
    goto out;

  /* Set IO mode to nonblicking.  */
  err = set_nonblock_flag (server_sock, 1);
  if (err)
    goto out;

  client = fdopen (client_sock, "r+");
  if (! client)
    {
      err = errno;
      goto out;
    }

  server = fdopen (server_sock, "r+");
  if (! server)
    {
      err = errno;
      goto out;
    }

  err = io_loop (client, server, protocol);

 out:

  if (client)
    fclose (client);
  else
    close (client_sock);

  if (server)
    fclose (server);
  else
    close (server_sock);

  free (data);

  return NULL;
}
Ejemplo n.º 5
0
static void
test_socket_pair (void)
{
  struct sockaddr_in ia;

  socklen_t addrlen = sizeof (ia);
  int s = open_server_socket ();
  int c1 = connect_to_socket (false);
  int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);

  close (s);

  test_pair (c1, c2);
  close (c1);
  write (c2, "foo", 3);
  close (c2);
}
Ejemplo n.º 6
0
static void
test_accept_first (void)
{
#ifndef WINDOWS_NATIVE
    int s = open_server_socket ();
    struct sockaddr_in ia;
    socklen_t addrlen;
    char buf[3];
    int c, pid;

    pid = fork ();
    if (pid < 0)
        return;

    if (pid == 0)
    {
        addrlen = sizeof (ia);
        c = accept (s, (struct sockaddr *) &ia, &addrlen);
        ASSERT (c >= 0);
        close (s);
        ASSERT (write (c, "foo", 3) == 3);
        ASSERT (read (c, buf, 3) == 3);
        shutdown (c, SHUT_RD);
        close (c);
        exit (0);
    }
    else
    {
        close (s);
        c = connect_to_socket (true);
        ASSERT (c >= 0);
        if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
                != (POLLOUT | POLLWRNORM))
            failed ("cannot write after blocking connect");
        ASSERT (write (c, "foo", 3) == 3);
        wait (&pid);
        if (poll1_wait (c, POLLIN) != POLLIN)
            failed ("cannot read data left in the socket by closed process");
        ASSERT (read (c, buf, 3) == 3);
        ASSERT (write (c, "foo", 3) == 3);
        if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
            failed ("expecting POLLHUP after shutdown");
        close (c);
    }
#endif
}
Ejemplo n.º 7
0
connection *comm_connect(char *ipaddress, int connections) {
	int i, port;
	connection *conn;
	
	if (in_thread == NULL) start_threads();
	if (connections < 1) return NULL;
	if (ipaddress == NULL) return NULL;
	
	conn = (connection *) malloc(sizeof(connection));
	
	conn->label = strdup(ipaddress);
	conn->connected = 0;
	conn->sockets = (out_socket *) malloc(connections * sizeof(out_socket));
	conn->num_sockets = connections;
	
	// setup the outgoing connections
	for (i = 0; i < connections; i++) {
		conn->sockets[i].fd = connect_to_socket(ipaddress, SOPHIA_PORT);
		set_non_blocking(conn->sockets[i].fd);
		pthread_mutex_init(&(conn->sockets[i].fd_lock), 0);
		conn->sockets[i].backlog = 0;
		conn->sockets[i].done = 0;
	}
	
	conn->connected = 1;
	
	// setup the rget connection
	conn->rget_settings.buffer = cbuf_new();
	conn->rget_settings.client_fd = -1;
	
	conn->rget_settings.server_fd = -1;
	while( conn->rget_settings.server_fd < 0 ) {
		port = randomise_port();
		conn->rget_settings.server_fd = listen_on_socket(port);
	}
	
	// comm_send(conn, 1, "void", 1, NULL, NULL, "[RGetRegisterClient \"%d\" \"%s\"]", port, "10.0.0.12"); // REMOTE get_local_ip());
	comm_send(conn, 1, "void", 1, NULL, NULL, "[RGetRegisterClient \"%d\" \"%s\"]", port, get_local_ip(conn->sockets[0].fd));
	// wait to accept the rget connection
	while (conn->rget_settings.client_fd < 0) { conn->rget_settings.client_fd = accept(conn->rget_settings.server_fd, NULL, 0); }
	
	// add this connect to the inbound subsystem
	comm_in_add(&(conn->rget_settings));
	
	return conn;
}
Ejemplo n.º 8
0
static void
test_accept_first (void)
{
#ifndef WIN32_NATIVE
  int s = open_server_socket ();
  struct sockaddr_in ia;
  socklen_t addrlen;
  char buf[3];
  int c, pid;

  pid = fork ();
  if (pid < 0)
    return;

  if (pid == 0)
    {
      addrlen = sizeof (ia);
      c = accept (s, (struct sockaddr *) &ia, &addrlen);
      close (s);
      write (c, "foo", 3);
      read (c, buf, 3);
      shutdown (c, SHUT_RD);
      close (c);
      exit (0);
    }
  else
    {
      close (s);
      c = connect_to_socket (true);
      if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
        failed ("cannot write after blocking connect");
      write (c, "foo", 3);
      wait (&pid);
      if (do_select_wait (c, SEL_IN) != SEL_IN)
        failed ("cannot read data left in the socket by closed process");
      read (c, buf, 3);
      write (c, "foo", 3);
      close (c);
    }
#endif
}
Ejemplo n.º 9
0
static void
test_socket_pair (void)
{
    struct sockaddr_in ia;

    socklen_t addrlen = sizeof (ia);
    int s = open_server_socket ();
    int c1 = connect_to_socket (false);
    int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
    ASSERT (s >= 0);
    ASSERT (c1 >= 0);
    ASSERT (c2 >= 0);

    close (s);

    test_pair (c1, c2);
    close (c1);
    ASSERT (write (c2, "foo", 3) == 3);
    if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
        failed ("expecting POLLHUP after shutdown");

    close (c2);
}
Ejemplo n.º 10
0
/** Connect to a Wayland display
 *
 * \param name Name of the Wayland display to connect to
 * \return A \ref wl_display object or \c NULL on failure
 *
 * Connect to the Wayland display named \c name. If \c name is \c NULL,
 * its value will be replaced with the WAYLAND_DISPLAY environment
 * variable if it is set, otherwise display "wayland-0" will be used.
 *
 * \memberof wl_display
 */
WL_EXPORT struct wl_display *
wl_display_connect(const char *name)
{
	char *connection, *end;
	int flags, fd;

	connection = getenv("WAYLAND_SOCKET");
	if (connection) {
		fd = strtol(connection, &end, 0);
		if (*end != '\0')
			return NULL;

		flags = fcntl(fd, F_GETFD);
		if (flags != -1)
			fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
		unsetenv("WAYLAND_SOCKET");
	} else {
		fd = connect_to_socket(name);
		if (fd < 0)
			return NULL;
	}

	return wl_display_connect_to_fd(fd);
}
Ejemplo n.º 11
0
int main(int argc, char **argv, char **envp) {
    int envpc;
    kern_return_t kr;
    mach_port_t mp;
    string_array_t newenvp;
    string_array_t newargv;
    size_t i;
    int launchd_fd;
    string_t handoff_socket_filename;
    sig_t handler;

    if(argc == 2 && !strcmp(argv[1], "-version")) {
        fprintf(stderr, "X.org Release 7.5\n");
        fprintf(stderr, "X.Org X Server %s\n", XSERVER_VERSION);
        fprintf(stderr, "Build Date: %s\n", BUILD_DATE);
        return EXIT_SUCCESS;
    }

    if(getenv("X11_PREFS_DOMAIN"))
        server_bootstrap_name = getenv("X11_PREFS_DOMAIN");
    
    /* We don't have a mechanism in place to handle this interrupt driven
     * server-start notification, so just send the signal now, so xinit doesn't
     * time out waiting for it and will just poll for the server.
     */
    handler = signal(SIGUSR1, SIG_IGN);
    if(handler == SIG_IGN)
        kill(getppid(), SIGUSR1);
    signal(SIGUSR1, handler);

    /* Pass on SIGs to X11.app */
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);
    
    /* Get the $DISPLAY FD */
    launchd_fd = launchd_display_fd();

    kr = bootstrap_look_up(bootstrap_port, server_bootstrap_name, &mp);
    if(kr != KERN_SUCCESS) {
        pid_t child;

        fprintf(stderr, "Xquartz: Unable to locate waiting server: %s\n", server_bootstrap_name);
        set_x11_path();

        /* This forking is ugly and will be cleaned up later */
        child = fork();
        if(child == -1) {
            fprintf(stderr, "Xquartz: Could not fork: %s\n", strerror(errno));
            return EXIT_FAILURE;
        }

        if(child == 0) {
            char *_argv[3];
            _argv[0] = x11_path;
            _argv[1] = "--listenonly";
            _argv[2] = NULL;
            fprintf(stderr, "Xquartz: Starting X server: %s --listenonly\n", x11_path);
            return execvp(x11_path, _argv);
        }

        /* Try connecting for 10 seconds */
        for(i=0; i < 80; i++) {
            usleep(250000);
            kr = bootstrap_look_up(bootstrap_port, server_bootstrap_name, &mp);
            if(kr == KERN_SUCCESS)
                break;
        }

        if(kr != KERN_SUCCESS) {
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
            fprintf(stderr, "Xquartz: bootstrap_look_up(): %s\n", bootstrap_strerror(kr));
#else
            fprintf(stderr, "Xquartz: bootstrap_look_up(): %ul\n", (unsigned long)kr);
#endif
            return EXIT_FAILURE;
        }
    }
    
    /* Get X11.app's pid */
    request_pid(mp, &x11app_pid);

    /* Handoff the $DISPLAY FD */
    if(launchd_fd != -1) {
        size_t try, try_max;
        int handoff_fd = -1;

        for(try=0, try_max=5; try < try_max; try++) {
            if(request_fd_handoff_socket(mp, handoff_socket_filename) != KERN_SUCCESS) {
                fprintf(stderr, "Xquartz: Failed to request a socket from the server to send the $DISPLAY fd over (try %d of %d)\n", (int)try+1, (int)try_max);
                continue;
            }

            handoff_fd = connect_to_socket(handoff_socket_filename);
            if(handoff_fd == -1) {
                fprintf(stderr, "Xquartz: Failed to connect to socket (try %d of %d)\n", (int)try+1, (int)try_max);
                continue;
            }

#ifdef DEBUG
            fprintf(stderr, "Xquartz: Handoff connection established (try %d of %d) on fd %d, \"%s\".  Sending message.\n", (int)try+1, (int)try_max, handoff_fd, handoff_socket_filename);
#endif

            send_fd_handoff(handoff_fd, launchd_fd);            
            close(handoff_fd);
            break;
        }
    }

    /* Count envp */
    for(envpc=0; envp[envpc]; envpc++);
    
    /* We have fixed-size string lengths due to limitations in IPC,
     * so we need to copy our argv and envp.
     */
    newargv = (string_array_t)malloc(argc * sizeof(string_t));
    newenvp = (string_array_t)malloc(envpc * sizeof(string_t));

    if(!newargv || !newenvp) {
        fprintf(stderr, "Xquartz: Memory allocation failure\n");
        return EXIT_FAILURE;
    }
    
    for(i=0; i < argc; i++) {
        strlcpy(newargv[i], argv[i], STRING_T_SIZE);
    }
    for(i=0; i < envpc; i++) {
        strlcpy(newenvp[i], envp[i], STRING_T_SIZE);
    }

    kr = start_x11_server(mp, newargv, argc, newenvp, envpc);

    free(newargv);
    free(newenvp);

    if (kr != KERN_SUCCESS) {
        fprintf(stderr, "Xquartz: start_x11_server: %s\n", mach_error_string(kr));
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
Ejemplo n.º 12
0
static void connect_to_server(void)
{
	/*Check for loops. Won't work if Listen undefined */
	if(info->server_control.address.sin_addr.s_addr
	   == config.listen_address.sin_addr.s_addr &&
	   info->server_control.address.sin_port
	   == config.listen_address.sin_port)
		die(ERROR, "Attempt to connect to self. "
		    "Do you need to set DoNTP to yes?",
		    421, "Proxy tried to loop. Closing connection", 0);
	if(info->server_control.address.sin_addr.s_addr == 0) {
		if(!config.ntp)
			die(ERROR,
			    "Frox unable to determine destination address. "
			    "Do you need to set DoNTP to yes?",
			    501, "Unable to contact server", 0);
		else if(config.ntpdest.sin_addr.s_addr)
			die(ERROR,
			    "Frox unable to determine destination address. "
			    "Try commenting out NTPAddress",
			    501, "Unable to contact server", 0);
		else
			die(ERROR,
			    "Frox unable to determine detination address. "
			    "See FAQ for troubleshooting.", 501,
			    "Unable to contact server", 0);
	}

	resolve_addr(&info->final_server_address.sin_addr, info->server_name);

	if(!config_connectionok(&info->client_control.address,
				&info->final_server_address,
				info->username ? sstr_buf(info->
							  username) : 0))
		die(ATTACK, "Denied by ACLs.", 501, "Connection denied. Bye",
		    0);

	if(config.ftpproxy.sin_addr.s_addr)
		info->server_control.address = config.ftpproxy;

	write_log(VERBOSE, "Connecting to server...");

	info->server_control.fd =
		connect_to_socket(&info->server_control.address,
				  &config.tcpoutaddr, config.contports);

	if(info->server_control.fd == -1)
		die(ERROR, "Connection closed -- unable to contact server",
		    501, "Proxy unable to contact ftp server", 0);

	write_log(VERBOSE, "     OK");

	if(config.loglevel >= VERBOSE) {	/*Save the overhead of DNS lookups */
		write_log(VERBOSE, "Apparent address = %s",
			  sstr_buf(addr2name
				   (info->apparent_server_address.sin_addr)));
		write_log(VERBOSE, "Real address = %s",
			  sstr_buf(addr2name
				   (info->final_server_address.sin_addr)));
		write_log(VERBOSE, "Proxy address = %s",
			  sstr_buf(addr2name
				   (info->server_control.address.sin_addr)));
	}
	ssl_init();
}